[gforth] / gforth / httpd.fs  

gforth: gforth/httpd.fs


1 : pazsan 1.1 #! /usr/local/bin/gforth
2 :    
3 : anton 1.6 \ Copyright (C) 2000 Free Software Foundation, Inc.
4 :    
5 :     \ This file is part of Gforth.
6 :    
7 :     \ Gforth is free software; you can redistribute it and/or
8 :     \ modify it under the terms of the GNU General Public License
9 :     \ as published by the Free Software Foundation; either version 2
10 :     \ of the License, or (at your option) any later version.
11 :    
12 :     \ This program is distributed in the hope that it will be useful,
13 :     \ but WITHOUT ANY WARRANTY; without even the implied warranty of
14 :     \ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 :     \ GNU General Public License for more details.
16 :    
17 :     \ You should have received a copy of the GNU General Public License
18 :     \ along with this program; if not, write to the Free Software
19 : anton 1.7 \ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
20 : anton 1.6
21 : pazsan 1.1 warnings off
22 :    
23 : pazsan 1.8 require string.fs
24 : pazsan 1.1
25 :     Variable url
26 : pazsan 1.4 Variable posted
27 :     Variable url-args
28 : pazsan 1.1 Variable protocol
29 : pazsan 1.2 Variable data
30 : pazsan 1.4 Variable active
31 : pazsan 1.2 Variable command?
32 : pazsan 1.1
33 :     : get ( addr -- ) name rot $! ;
34 :     : get-rest ( addr -- ) source >in @ /string dup >in +! rot $! ;
35 :    
36 : pazsan 1.4 wordlist constant values
37 :     wordlist constant commands
38 : pazsan 1.1
39 : pazsan 1.2 : value: ( -- ) name
40 : pazsan 1.1 Forth definitions 2dup 1- nextname Variable
41 : pazsan 1.2 values set-current nextname here cell - Create ,
42 : pazsan 1.1 DOES> @ get-rest ;
43 : pazsan 1.2 : >values values 1 set-order command? off ;
44 : pazsan 1.1
45 : pazsan 1.2 \ HTTP protocol commands 26mar00py
46 : pazsan 1.1
47 : pazsan 1.4 : rework-% ( add -- ) { url } base @ >r hex
48 : pazsan 1.2 0 url $@len 0 ?DO
49 :     url $@ drop I + c@ dup '% = IF
50 :     drop 0. url $@ I 1+ /string
51 :     2 min dup >r >number r> swap - >r 2drop
52 :     ELSE 0 >r THEN over url $@ drop + c! 1+
53 :     r> 1+ +LOOP url $!len
54 :     r> base ! ;
55 :    
56 : pazsan 1.5 : rework-? ( addr -- )
57 :     dup >r $@ '? $split url-args $! nip r> $!len ;
58 : pazsan 1.4
59 : pazsan 1.5 : get-url ( -- ) url get protocol get-rest
60 : pazsan 1.4 url rework-? url rework-% >values ;
61 :    
62 : pazsan 1.2 commands set-current
63 :    
64 : pazsan 1.4 : GET get-url data on active off ;
65 :     : POST get-url data on active on ;
66 :     : HEAD get-url data off active off ;
67 : pazsan 1.2
68 :     \ HTTP protocol values 26mar00py
69 :    
70 :     values set-current
71 :    
72 :     value: User-Agent:
73 :     value: Pragma:
74 :     value: Host:
75 :     value: Accept:
76 :     value: Accept-Encoding:
77 :     value: Accept-Language:
78 :     value: Accept-Charset:
79 :     value: Via:
80 :     value: X-Forwarded-For:
81 :     value: Cache-Control:
82 :     value: Connection:
83 :     value: Referer:
84 : pazsan 1.4 value: Content-Type:
85 :     value: Content-Length:
86 : pazsan 1.1
87 :     definitions
88 :    
89 :     Variable maxnum
90 :    
91 :     : ?cr ( -- )
92 :     #tib @ 1 >= IF source 1- + c@ #cr = #tib +! THEN ;
93 :     : refill-loop ( -- flag )
94 :     BEGIN refill ?cr WHILE interpret >in @ 0= UNTIL
95 :     true ELSE maxnum off false THEN ;
96 :     : get-input ( -- flag ior )
97 : pazsan 1.3 s" /nosuchfile" url $! s" HTTP/1.0" protocol $!
98 : pazsan 1.2 s" close" connection $!
99 : pazsan 1.3 infile-id push-file loadfile ! loadline off blk off
100 : pazsan 1.2 commands 1 set-order command? on ['] refill-loop catch
101 : pazsan 1.4 active @ IF s" " posted $! Content-Length $@ snumber? drop
102 :     posted $!len posted $@ infile-id read-file throw drop
103 :     THEN only forth also pop-file ;
104 : pazsan 1.1
105 :     \ Rework HTML directory 26mar00py
106 :    
107 :     Variable htmldir
108 :    
109 :     : rework-htmldir ( addr u -- addr' u' / ior )
110 :     htmldir $!
111 :     htmldir $@ 1 min s" ~" compare 0=
112 :     IF s" /.html-data" htmldir dup $@ 2dup '/ scan
113 :     nip - nip $ins
114 :     ELSE s" /usr/local/httpd/htdocs/" htmldir 0 $ins THEN
115 :     htmldir $@ 1- 0 max + c@ '/ = htmldir $@len 0= or
116 :     IF s" index.html" htmldir dup $@len $ins THEN
117 :     htmldir $@ file-status nip ?dup ?EXIT
118 :     htmldir $@ ;
119 :    
120 :     \ MIME type handling 26mar00py
121 :    
122 :     : >mime ( addr u -- mime u' ) 2dup tuck over + 1- ?DO
123 :     I c@ '. = ?LEAVE 1- -1 +LOOP /string ;
124 :    
125 :     : >file ( addr u -- size fd )
126 :     r/o bin open-file throw >r
127 :     r@ file-size throw drop
128 :     ." Accept-Ranges: bytes" cr
129 :     ." Content-Length: " dup 0 .r cr r> ;
130 : pazsan 1.4 : transparent ( size fd -- ) { fd }
131 :     $4000 allocate throw swap dup 0 ?DO
132 :     2dup over swap $4000 min fd read-file throw type
133 :     $4000 - $4000 +LOOP drop
134 :     free fd close-file throw throw ;
135 : pazsan 1.1
136 : pazsan 1.5 \ Keep-Alive handling 26mar00py
137 :    
138 :     : .connection ( -- )
139 :     ." Connection: "
140 :     connection $@ s" Keep-Alive" compare 0= maxnum @ 0> and
141 :     IF connection $@ type cr
142 :     ." Keep-Alive: timeout=15, max=" maxnum @ 0 .r cr
143 :     -1 maxnum +! ELSE ." close" cr maxnum off THEN ;
144 :    
145 : pazsan 1.2 : transparent: ( addr u -- ) Create here over 1+ allot place
146 :     DOES> >r >file
147 : pazsan 1.1 .connection
148 :     ." Content-Type: " r> count type cr cr
149 : pazsan 1.2 data @ IF transparent ELSE nip close-file throw THEN ;
150 : pazsan 1.1
151 :     \ mime types 26mar00py
152 :    
153 : pazsan 1.2 : mime-read ( addr u -- ) r/o open-file throw
154 :     push-file loadfile ! 0 loadline ! blk off
155 :     BEGIN refill WHILE name
156 :     BEGIN >in @ >r name nip WHILE
157 :     r> >in ! 2dup transparent: REPEAT
158 :     2drop rdrop
159 :     REPEAT loadfile @ close-file pop-file throw ;
160 :    
161 : pazsan 1.1 : lastrequest
162 :     ." Connection: close" cr maxnum off
163 :     ." Content-Type: text/html" cr cr ;
164 :    
165 :     wordlist constant mime
166 :     mime set-current
167 :    
168 : pazsan 1.2 : shtml ( addr u -- ) lastrequest
169 :     data @ IF included ELSE 2drop THEN ;
170 : pazsan 1.1
171 : pazsan 1.2 s" application/pgp-signature" transparent: sig
172 :     s" application/x-bzip2" transparent: bz2
173 :     s" application/x-gzip" transparent: gz
174 :     s" /etc/mime.types" mime-read
175 : pazsan 1.1
176 :     definitions
177 :    
178 : pazsan 1.2 s" text/plain" transparent: txt
179 : pazsan 1.1
180 :     \ http errors 26mar00py
181 :    
182 : pazsan 1.5 : .server ( -- ) ." Server: Gforth httpd/0.1 ("
183 : pazsan 1.2 s" os-class" environment? IF type THEN ." )" cr ;
184 : pazsan 1.5 : .ok ( -- ) ." HTTP/1.1 200 OK" cr .server ;
185 : pazsan 1.1 : html-error ( n addr u -- )
186 : pazsan 1.2 ." HTTP/1.1 " 2 pick . 2dup type cr .server
187 : pazsan 1.5 2 pick &405 = IF ." Allow: GET, HEAD, POST" cr THEN
188 :     lastrequest
189 :     ." <HTML><HEAD><TITLE>" 2 pick . 2dup type
190 :     ." </TITLE></HEAD>" cr
191 : pazsan 1.1 ." <BODY><H1>" type drop ." </H1>" cr ;
192 :     : .trailer ( -- )
193 :     ." <HR><ADDRESS>Gforth httpd 0.1</ADDRESS>" cr
194 :     ." </BODY></HTML>" cr ;
195 : pazsan 1.5 : .nok ( -- ) command? @ IF &405 s" Method Not Allowed"
196 : pazsan 1.2 ELSE &400 s" Bad Request" THEN html-error
197 : pazsan 1.5 ." <P>Your browser sent a request that this server "
198 :     ." could not understand.</P>" cr
199 :     ." <P>Invalid request in: <CODE>"
200 :     error-stack cell+ 2@ swap type
201 : pazsan 1.1 ." </CODE></P>" cr .trailer ;
202 : pazsan 1.5 : .nofile ( -- ) &404 s" Not Found" html-error
203 : pazsan 1.1 ." <P>The requested URL <CODE>" url $@ type
204 :     ." </CODE> was not found on this server</P>" cr .trailer ;
205 :    
206 :     \ http server 26mar00py
207 :    
208 : pazsan 1.9 Defer redirect? ( addr u -- addr' u' t / f )
209 :     Defer redirect ( addr u -- )
210 :     :noname 2drop false ; IS redirect?
211 :    
212 : pazsan 1.5 : http ( -- ) get-input IF .nok ELSE
213 : pazsan 1.9 IF url $@ 1 /string 2dup redirect? IF redirect 2drop ELSE
214 :     rework-htmldir
215 : pazsan 1.1 dup 0< IF drop .nofile
216 :     ELSE .ok 2dup >mime mime search-wordlist
217 : pazsan 1.2 0= IF ['] txt THEN catch IF maxnum off THEN
218 : pazsan 1.9 THEN THEN THEN THEN outfile-id flush-file throw ;
219 : pazsan 1.1
220 :     : httpd ( n -- ) maxnum !
221 : pazsan 1.3 BEGIN ['] http catch maxnum @ 0= or UNTIL ;
222 : pazsan 1.1
223 : pazsan 1.4 script? [IF] :noname &100 httpd bye ; is bootmessage [THEN]
224 : pazsan 1.5
225 :     \ Use Forth as server-side script language 26mar00py
226 :    
227 :     : $> ( -- )
228 :     BEGIN source >in @ /string s" <$" search 0= WHILE
229 :     type cr refill 0= UNTIL EXIT THEN
230 :     nip source >in @ /string rot - dup 2 + >in +! type ;
231 :     : <HTML> ( -- ) ." <HTML>" $> ;
232 : pazsan 1.9
233 :     \ provide transparent proxying
234 :    
235 : pazsan 1.10 include ./proxy.fs

CVS Admin

Powered by ViewCVS 1.0-dev
(Powered by ViewCVS)

ViewCVS and CVS Help