Annotation of gforth/httpd.fs, revision 1.3
1.1 pazsan 1: #! /usr/local/bin/gforth
2:
3: warnings off
4:
5: include string.fs
6:
7: Variable url
8: Variable protocol
1.2 pazsan 9: Variable data
10: Variable command?
1.1 pazsan 11:
12: : get ( addr -- ) name rot $! ;
13: : get-rest ( addr -- ) source >in @ /string dup >in +! rot $! ;
14:
1.2 pazsan 15: Table constant values
16: Table constant commands
1.1 pazsan 17:
1.2 pazsan 18: : value: ( -- ) name
1.1 pazsan 19: Forth definitions 2dup 1- nextname Variable
1.2 pazsan 20: values set-current nextname here cell - Create ,
1.1 pazsan 21: DOES> @ get-rest ;
1.2 pazsan 22: : >values values 1 set-order command? off ;
1.1 pazsan 23:
1.2 pazsan 24: \ HTTP protocol commands 26mar00py
1.1 pazsan 25:
1.2 pazsan 26: : rework-% ( -- ) base @ >r hex
27: 0 url $@len 0 ?DO
28: url $@ drop I + c@ dup '% = IF
29: drop 0. url $@ I 1+ /string
30: 2 min dup >r >number r> swap - >r 2drop
31: ELSE 0 >r THEN over url $@ drop + c! 1+
32: r> 1+ +LOOP url $!len
33: r> base ! ;
34:
35: commands set-current
36:
37: : GET url get rework-% protocol get-rest >values data on ;
38: : HEAD url get rework-% protocol get-rest >values data off ;
39:
40: \ HTTP protocol values 26mar00py
41:
42: values set-current
43:
44: value: User-Agent:
45: value: Pragma:
46: value: Host:
47: value: Accept:
48: value: Accept-Encoding:
49: value: Accept-Language:
50: value: Accept-Charset:
51: value: Via:
52: value: X-Forwarded-For:
53: value: Cache-Control:
54: value: Connection:
55: value: Referer:
1.1 pazsan 56:
57: definitions
58:
59:
60: Variable maxnum
61:
62: : ?cr ( -- )
63: #tib @ 1 >= IF source 1- + c@ #cr = #tib +! THEN ;
64: : refill-loop ( -- flag )
65: BEGIN refill ?cr WHILE interpret >in @ 0= UNTIL
66: true ELSE maxnum off false THEN ;
67: : get-input ( -- flag ior )
1.3 ! pazsan 68: s" /nosuchfile" url $! s" HTTP/1.0" protocol $!
1.2 pazsan 69: s" close" connection $!
1.3 ! pazsan 70: infile-id push-file loadfile ! loadline off blk off
1.2 pazsan 71: commands 1 set-order command? on ['] refill-loop catch
1.1 pazsan 72: only forth also pop-file ;
73:
74: \ Keep-Alive handling 26mar00py
75:
76: : .connection ( -- )
77: ." Connection: "
78: connection $@ s" Keep-Alive" compare 0= maxnum @ 0> and
79: IF connection $@ type cr
80: ." Keep-Alive: timeout=15, max=" maxnum @ 0 .r cr
81: -1 maxnum +! ELSE ." close" cr maxnum off THEN ;
82:
83: \ Use Forth as server-side script language 26mar00py
84:
85: : $> ( -- )
86: BEGIN source >in @ /string s" <$" search 0= WHILE
87: type cr refill 0= UNTIL EXIT THEN
88: nip source >in @ /string rot - dup 2 + >in +! type ;
89: : <HTML> ( -- ) ." <HTML>" $> ;
90:
91: \ Rework HTML directory 26mar00py
92:
93: Variable htmldir
94:
95: : rework-htmldir ( addr u -- addr' u' / ior )
96: htmldir $!
97: htmldir $@ 1 min s" ~" compare 0=
98: IF s" /.html-data" htmldir dup $@ 2dup '/ scan
99: nip - nip $ins
100: ELSE s" /usr/local/httpd/htdocs/" htmldir 0 $ins THEN
101: htmldir $@ 1- 0 max + c@ '/ = htmldir $@len 0= or
102: IF s" index.html" htmldir dup $@len $ins THEN
103: htmldir $@ file-status nip ?dup ?EXIT
104: htmldir $@ ;
105:
106: \ MIME type handling 26mar00py
107:
108: : >mime ( addr u -- mime u' ) 2dup tuck over + 1- ?DO
109: I c@ '. = ?LEAVE 1- -1 +LOOP /string ;
110:
111: : >file ( addr u -- size fd )
112: r/o bin open-file throw >r
113: r@ file-size throw drop
114: ." Accept-Ranges: bytes" cr
115: ." Content-Length: " dup 0 .r cr r> ;
116: : transparent ( size fd -- ) >r
117: dup allocate throw swap
118: over swap r@ read-file throw over swap type
119: free r> close-file throw throw ;
120:
1.2 pazsan 121: : transparent: ( addr u -- ) Create here over 1+ allot place
122: DOES> >r >file
1.1 pazsan 123: .connection
124: ." Content-Type: " r> count type cr cr
1.2 pazsan 125: data @ IF transparent ELSE nip close-file throw THEN ;
1.1 pazsan 126:
127: \ mime types 26mar00py
128:
1.2 pazsan 129: : mime-read ( addr u -- ) r/o open-file throw
130: push-file loadfile ! 0 loadline ! blk off
131: BEGIN refill WHILE name
132: BEGIN >in @ >r name nip WHILE
133: r> >in ! 2dup transparent: REPEAT
134: 2drop rdrop
135: REPEAT loadfile @ close-file pop-file throw ;
136:
1.1 pazsan 137: : lastrequest
138: ." Connection: close" cr maxnum off
139: ." Content-Type: text/html" cr cr ;
140:
141: wordlist constant mime
142: mime set-current
143:
1.2 pazsan 144: : shtml ( addr u -- ) lastrequest
145: data @ IF included ELSE 2drop THEN ;
1.1 pazsan 146:
1.2 pazsan 147: s" application/pgp-signature" transparent: sig
148: s" application/x-bzip2" transparent: bz2
149: s" application/x-gzip" transparent: gz
150: s" /etc/mime.types" mime-read
1.1 pazsan 151:
152: definitions
153:
1.2 pazsan 154: s" text/plain" transparent: txt
1.1 pazsan 155:
156: \ http errors 26mar00py
157:
1.2 pazsan 158: : .server ." Server: Gforth httpd/0.1 ("
159: s" os-class" environment? IF type THEN ." )" cr ;
160: : .ok ." HTTP/1.1 200 OK" cr .server ;
1.1 pazsan 161: : html-error ( n addr u -- )
1.2 pazsan 162: ." HTTP/1.1 " 2 pick . 2dup type cr .server
163: 2 pick &405 = IF ." Allow: GET, HEAD" cr THEN lastrequest
1.1 pazsan 164: ." <HTML><HEAD><TITLE>" 2 pick . 2dup type ." </TITLE></HEAD>" cr
165: ." <BODY><H1>" type drop ." </H1>" cr ;
166: : .trailer ( -- )
167: ." <HR><ADDRESS>Gforth httpd 0.1</ADDRESS>" cr
168: ." </BODY></HTML>" cr ;
1.2 pazsan 169: : .nok command? @ IF &405 s" Method Not Allowed"
170: ELSE &400 s" Bad Request" THEN html-error
1.1 pazsan 171: ." <P>Your browser sent a request that this server could not understand.</P>" cr
172: ." <P>Invalid request in: <CODE>" error-stack cell+ 2@ swap type
173: ." </CODE></P>" cr .trailer ;
174: : .nofile &404 s" Not Found" html-error
175: ." <P>The requested URL <CODE>" url $@ type
176: ." </CODE> was not found on this server</P>" cr .trailer ;
177:
178: \ http server 26mar00py
179:
180: : http get-input IF .nok ELSE
181: IF url $@ 1 /string rework-htmldir
182: dup 0< IF drop .nofile
183: ELSE .ok 2dup >mime mime search-wordlist
1.2 pazsan 184: 0= IF ['] txt THEN catch IF maxnum off THEN
1.1 pazsan 185: THEN THEN THEN outfile-id flush-file throw ;
186:
187: : httpd ( n -- ) maxnum !
1.3 ! pazsan 188: BEGIN ['] http catch maxnum @ 0= or UNTIL ;
1.1 pazsan 189:
1.2 pazsan 190: script? [IF] &100 httpd bye [THEN]
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>