Annotation of gforth/httpd.fs, revision 1.8
1.1 pazsan 1: #! /usr/local/bin/gforth
2:
1.6 anton 3: \ 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
1.7 anton 19: \ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
1.6 anton 20:
1.1 pazsan 21: warnings off
22:
1.8 ! pazsan 23: require string.fs
1.1 pazsan 24:
25: Variable url
1.4 pazsan 26: Variable posted
27: Variable url-args
1.1 pazsan 28: Variable protocol
1.2 pazsan 29: Variable data
1.4 pazsan 30: Variable active
1.2 pazsan 31: Variable command?
1.1 pazsan 32:
33: : get ( addr -- ) name rot $! ;
34: : get-rest ( addr -- ) source >in @ /string dup >in +! rot $! ;
35:
1.4 pazsan 36: wordlist constant values
37: wordlist constant commands
1.1 pazsan 38:
1.2 pazsan 39: : value: ( -- ) name
1.1 pazsan 40: Forth definitions 2dup 1- nextname Variable
1.2 pazsan 41: values set-current nextname here cell - Create ,
1.1 pazsan 42: DOES> @ get-rest ;
1.2 pazsan 43: : >values values 1 set-order command? off ;
1.1 pazsan 44:
1.2 pazsan 45: \ HTTP protocol commands 26mar00py
1.1 pazsan 46:
1.4 pazsan 47: : rework-% ( add -- ) { url } base @ >r hex
1.2 pazsan 48: 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:
1.5 pazsan 56: : rework-? ( addr -- )
57: dup >r $@ '? $split url-args $! nip r> $!len ;
1.4 pazsan 58:
1.5 pazsan 59: : get-url ( -- ) url get protocol get-rest
1.4 pazsan 60: url rework-? url rework-% >values ;
61:
1.2 pazsan 62: commands set-current
63:
1.4 pazsan 64: : GET get-url data on active off ;
65: : POST get-url data on active on ;
66: : HEAD get-url data off active off ;
1.2 pazsan 67:
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:
1.4 pazsan 84: value: Content-Type:
85: value: Content-Length:
1.1 pazsan 86:
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 )
1.3 pazsan 97: s" /nosuchfile" url $! s" HTTP/1.0" protocol $!
1.2 pazsan 98: s" close" connection $!
1.3 pazsan 99: infile-id push-file loadfile ! loadline off blk off
1.2 pazsan 100: commands 1 set-order command? on ['] refill-loop catch
1.4 pazsan 101: 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 ;
1.1 pazsan 104:
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> ;
1.4 pazsan 130: : 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 ;
1.1 pazsan 135:
1.5 pazsan 136: \ 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:
1.2 pazsan 145: : transparent: ( addr u -- ) Create here over 1+ allot place
146: DOES> >r >file
1.1 pazsan 147: .connection
148: ." Content-Type: " r> count type cr cr
1.2 pazsan 149: data @ IF transparent ELSE nip close-file throw THEN ;
1.1 pazsan 150:
151: \ mime types 26mar00py
152:
1.2 pazsan 153: : 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:
1.1 pazsan 161: : lastrequest
162: ." Connection: close" cr maxnum off
163: ." Content-Type: text/html" cr cr ;
164:
165: wordlist constant mime
166: mime set-current
167:
1.2 pazsan 168: : shtml ( addr u -- ) lastrequest
169: data @ IF included ELSE 2drop THEN ;
1.1 pazsan 170:
1.2 pazsan 171: 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
1.1 pazsan 175:
176: definitions
177:
1.2 pazsan 178: s" text/plain" transparent: txt
1.1 pazsan 179:
180: \ http errors 26mar00py
181:
1.5 pazsan 182: : .server ( -- ) ." Server: Gforth httpd/0.1 ("
1.2 pazsan 183: s" os-class" environment? IF type THEN ." )" cr ;
1.5 pazsan 184: : .ok ( -- ) ." HTTP/1.1 200 OK" cr .server ;
1.1 pazsan 185: : html-error ( n addr u -- )
1.2 pazsan 186: ." HTTP/1.1 " 2 pick . 2dup type cr .server
1.5 pazsan 187: 2 pick &405 = IF ." Allow: GET, HEAD, POST" cr THEN
188: lastrequest
189: ." <HTML><HEAD><TITLE>" 2 pick . 2dup type
190: ." </TITLE></HEAD>" cr
1.1 pazsan 191: ." <BODY><H1>" type drop ." </H1>" cr ;
192: : .trailer ( -- )
193: ." <HR><ADDRESS>Gforth httpd 0.1</ADDRESS>" cr
194: ." </BODY></HTML>" cr ;
1.5 pazsan 195: : .nok ( -- ) command? @ IF &405 s" Method Not Allowed"
1.2 pazsan 196: ELSE &400 s" Bad Request" THEN html-error
1.5 pazsan 197: ." <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
1.1 pazsan 201: ." </CODE></P>" cr .trailer ;
1.5 pazsan 202: : .nofile ( -- ) &404 s" Not Found" html-error
1.1 pazsan 203: ." <P>The requested URL <CODE>" url $@ type
204: ." </CODE> was not found on this server</P>" cr .trailer ;
205:
206: \ http server 26mar00py
207:
1.5 pazsan 208: : http ( -- ) get-input IF .nok ELSE
1.1 pazsan 209: IF url $@ 1 /string rework-htmldir
210: dup 0< IF drop .nofile
211: ELSE .ok 2dup >mime mime search-wordlist
1.2 pazsan 212: 0= IF ['] txt THEN catch IF maxnum off THEN
1.1 pazsan 213: THEN THEN THEN outfile-id flush-file throw ;
214:
215: : httpd ( n -- ) maxnum !
1.3 pazsan 216: BEGIN ['] http catch maxnum @ 0= or UNTIL ;
1.1 pazsan 217:
1.4 pazsan 218: script? [IF] :noname &100 httpd bye ; is bootmessage [THEN]
1.5 pazsan 219:
220: \ Use Forth as server-side script language 26mar00py
221:
222: : $> ( -- )
223: BEGIN source >in @ /string s" <$" search 0= WHILE
224: type cr refill 0= UNTIL EXIT THEN
225: nip source >in @ /string rot - dup 2 + >in +! type ;
226: : <HTML> ( -- ) ." <HTML>" $> ;
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>