version 1.5, 2000/04/12 20:53:46
|
version 1.22, 2006/12/31 13:39:13
|
Line 1
|
Line 1
|
#! /usr/local/bin/gforth |
#! /usr/local/bin/gforth |
|
|
|
\ Copyright (C) 2000,2002,2003,2004,2006 Free Software Foundation, Inc. |
|
|
|
\ This file is part of Gforth. |
|
|
|
\ Gforth is free software; you can redistribute it and/or |
|
\ modify it under the terms of the GNU General Public License |
|
\ as published by the Free Software Foundation; either version 2 |
|
\ of the License, or (at your option) any later version. |
|
|
|
\ This program is distributed in the hope that it will be useful, |
|
\ but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
\ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
\ GNU General Public License for more details. |
|
|
|
\ You should have received a copy of the GNU General Public License |
|
\ along with this program; if not, write to the Free Software |
|
\ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA. |
|
|
|
\ This relies on inetd or xinetd: |
|
|
|
\ To run the server on port 4444, do the following: |
|
|
|
\ Add the following line to /etc/services: |
|
\ gforth 4444/tcp |
|
|
|
\ If you use inetd, add the following line to /etc/inetd.conf: |
|
\ gforth stream tcp nowait.10000 wwwrun /usr/users/bernd/bin/httpd |
|
|
|
\ If you use xinetd, create the folliwing service in /etc/xinetd.d: |
|
\ service gforth |
|
\ { |
|
\ socket_type = stream |
|
\ protocol = tcp |
|
\ wait = no |
|
\ user = wwwrun |
|
\ server = /home/bernd/bin/httpd |
|
\ } |
|
|
|
\ If you want port 80, replace the service "gforth" with "http" |
|
|
warnings off |
warnings off |
|
|
include string.fs |
require string.fs |
|
|
|
Variable DocumentRoot s" /srv/www/htdocs/" DocumentRoot $! |
|
Variable UserDir s" public_html/" UserDir $! |
|
|
Variable url |
Variable url |
Variable posted |
Variable posted |
Line 65 value: Connection:
|
Line 108 value: Connection:
|
value: Referer: |
value: Referer: |
value: Content-Type: |
value: Content-Type: |
value: Content-Length: |
value: Content-Length: |
|
value: Keep-Alive: |
|
|
definitions |
definitions |
|
|
Line 72 Variable maxnum
|
Line 116 Variable maxnum
|
|
|
: ?cr ( -- ) |
: ?cr ( -- ) |
#tib @ 1 >= IF source 1- + c@ #cr = #tib +! THEN ; |
#tib @ 1 >= IF source 1- + c@ #cr = #tib +! THEN ; |
: refill-loop ( -- flag ) |
: refill-loop ( -- flag ) base @ >r base off |
BEGIN refill ?cr WHILE interpret >in @ 0= UNTIL |
BEGIN refill ?cr WHILE ['] interpret catch drop >in @ 0= UNTIL |
true ELSE maxnum off false THEN ; |
true ELSE maxnum off false THEN r> base ! ; |
: get-input ( -- flag ior ) |
: get-input ( -- flag ior ) |
s" /nosuchfile" url $! s" HTTP/1.0" protocol $! |
s" /nosuchfile" url $! s" HTTP/1.0" protocol $! |
s" close" connection $! |
s" close" connection $! |
infile-id push-file loadfile ! loadline off blk off |
infile-id push-file loadfile ! loadline off blk off |
commands 1 set-order command? on ['] refill-loop catch |
commands 1 set-order command? on ['] refill-loop catch |
|
Keep-Alive $@ snumber? dup 0> IF nip THEN IF maxnum ! THEN |
active @ IF s" " posted $! Content-Length $@ snumber? drop |
active @ IF s" " posted $! Content-Length $@ snumber? drop |
posted $!len posted $@ infile-id read-file throw drop |
posted $!len posted $@ infile-id read-file throw drop |
THEN only forth also pop-file ; |
THEN only forth also pop-file ; |
Line 89 Variable maxnum
|
Line 134 Variable maxnum
|
Variable htmldir |
Variable htmldir |
|
|
: rework-htmldir ( addr u -- addr' u' / ior ) |
: rework-htmldir ( addr u -- addr' u' / ior ) |
htmldir $! |
htmldir $! htmldir $@ compact-filename htmldir $!len drop |
htmldir $@ 1 min s" ~" compare 0= |
htmldir $@ s" ../" string-prefix? |
IF s" /.html-data" htmldir dup $@ 2dup '/ scan |
IF -1 EXIT THEN \ can't access below current directory |
|
htmldir $@ s" ~" string-prefix? |
|
IF UserDir $@ htmldir dup $@ 2dup '/ scan '/ skip |
nip - nip $ins |
nip - nip $ins |
ELSE s" /usr/local/httpd/htdocs/" htmldir 0 $ins THEN |
ELSE DocumentRoot $@ htmldir 0 $ins THEN |
htmldir $@ 1- 0 max + c@ '/ = htmldir $@len 0= or |
htmldir $@ 1- 0 max + c@ '/ = htmldir $@len 0= or |
IF s" index.html" htmldir dup $@len $ins THEN |
IF s" index.html" htmldir dup $@len $ins THEN |
htmldir $@ file-status nip ?dup ?EXIT |
htmldir $@ file-status nip ?dup ?EXIT |
Line 119 Variable htmldir
|
Line 166 Variable htmldir
|
|
|
: .connection ( -- ) |
: .connection ( -- ) |
." Connection: " |
." Connection: " |
connection $@ s" Keep-Alive" compare 0= maxnum @ 0> and |
connection $@ s" Keep-Alive" str= maxnum @ 0> and |
IF connection $@ type cr |
IF connection $@ type cr |
." Keep-Alive: timeout=15, max=" maxnum @ 0 .r cr |
." Keep-Alive: timeout=15, max=" maxnum @ 0 .r cr |
-1 maxnum +! ELSE ." close" cr maxnum off THEN ; |
-1 maxnum +! ELSE ." close" cr maxnum off THEN ; |
Line 134 Variable htmldir
|
Line 181 Variable htmldir
|
|
|
: mime-read ( addr u -- ) r/o open-file throw |
: mime-read ( addr u -- ) r/o open-file throw |
push-file loadfile ! 0 loadline ! blk off |
push-file loadfile ! 0 loadline ! blk off |
BEGIN refill WHILE name |
BEGIN refill WHILE |
BEGIN >in @ >r name nip WHILE |
char '# <> >in off name nip 0<> and IF |
r> >in ! 2dup transparent: REPEAT |
>in off name |
2drop rdrop |
BEGIN >in @ >r name nip WHILE |
|
r> >in ! 2dup transparent: REPEAT |
|
2drop rdrop |
|
THEN |
REPEAT loadfile @ close-file pop-file throw ; |
REPEAT loadfile @ close-file pop-file throw ; |
|
|
: lastrequest |
: lastrequest |
Line 153 mime set-current
|
Line 203 mime set-current
|
s" application/pgp-signature" transparent: sig |
s" application/pgp-signature" transparent: sig |
s" application/x-bzip2" transparent: bz2 |
s" application/x-bzip2" transparent: bz2 |
s" application/x-gzip" transparent: gz |
s" application/x-gzip" transparent: gz |
s" /etc/mime.types" mime-read |
s" /etc/mime.types" ' mime-read catch [IF] 2drop |
|
\ no /etc/mime.types found on this machine, |
|
\ generating the most important types: |
|
s" text/html" transparent: html |
|
s" image/gif" transparent: gif |
|
s" image/png" transparent: png |
|
s" image/jpg" transparent: jpg |
|
[THEN] |
|
|
definitions |
definitions |
|
|
Line 161 s" text/plain" transparent: txt
|
Line 218 s" text/plain" transparent: txt
|
|
|
\ http errors 26mar00py |
\ http errors 26mar00py |
|
|
: .server ( -- ) ." Server: Gforth httpd/0.1 (" |
: .server ( -- ) ." Server: Gforth httpd/1.0 (" |
s" os-class" environment? IF type THEN ." )" cr ; |
s" os-class" environment? IF type THEN ." )" cr ; |
: .ok ( -- ) ." HTTP/1.1 200 OK" cr .server ; |
: .ok ( -- ) ." HTTP/1.1 200 OK" cr .server ; |
: html-error ( n addr u -- ) |
: html-error ( n addr u -- ) |
Line 172 s" text/plain" transparent: txt
|
Line 229 s" text/plain" transparent: txt
|
." </TITLE></HEAD>" cr |
." </TITLE></HEAD>" cr |
." <BODY><H1>" type drop ." </H1>" cr ; |
." <BODY><H1>" type drop ." </H1>" cr ; |
: .trailer ( -- ) |
: .trailer ( -- ) |
." <HR><ADDRESS>Gforth httpd 0.1</ADDRESS>" cr |
." <HR><ADDRESS>Gforth httpd 1.0</ADDRESS>" cr |
." </BODY></HTML>" cr ; |
." </BODY></HTML>" cr ; |
: .nok ( -- ) command? @ IF &405 s" Method Not Allowed" |
: .nok ( -- ) command? @ IF &405 s" Method Not Allowed" |
ELSE &400 s" Bad Request" THEN html-error |
ELSE &400 s" Bad Request" THEN html-error |
Line 187 s" text/plain" transparent: txt
|
Line 244 s" text/plain" transparent: txt
|
|
|
\ http server 26mar00py |
\ http server 26mar00py |
|
|
|
Defer redirect? ( addr u -- addr' u' t / f ) |
|
Defer redirect ( addr u -- ) |
|
:noname 2drop false ; IS redirect? |
|
|
: http ( -- ) get-input IF .nok ELSE |
: http ( -- ) get-input IF .nok ELSE |
IF url $@ 1 /string rework-htmldir |
IF url $@ 1 /string 2dup redirect? IF redirect 2drop ELSE |
|
rework-htmldir |
dup 0< IF drop .nofile |
dup 0< IF drop .nofile |
ELSE .ok 2dup >mime mime search-wordlist |
ELSE .ok 2dup >mime mime search-wordlist |
0= IF ['] txt THEN catch IF maxnum off THEN |
0= IF ['] txt THEN catch IF maxnum off THEN |
THEN THEN THEN outfile-id flush-file throw ; |
THEN THEN THEN THEN outfile-id flush-file throw ; |
|
|
: httpd ( n -- ) maxnum ! |
: httpd ( n -- ) dup maxnum ! 0 <# #S #> Keep-Alive $! |
BEGIN ['] http catch maxnum @ 0= or UNTIL ; |
maxnum @ 0 DO ['] http catch maxnum @ 0= or ?LEAVE LOOP ; |
|
|
script? [IF] :noname &100 httpd bye ; is bootmessage [THEN] |
script? [IF] :noname &100 httpd bye ; is bootmessage [THEN] |
|
|
Line 206 script? [IF] :noname &100 httpd bye ; i
|
Line 268 script? [IF] :noname &100 httpd bye ; i
|
type cr refill 0= UNTIL EXIT THEN |
type cr refill 0= UNTIL EXIT THEN |
nip source >in @ /string rot - dup 2 + >in +! type ; |
nip source >in @ /string rot - dup 2 + >in +! type ; |
: <HTML> ( -- ) ." <HTML>" $> ; |
: <HTML> ( -- ) ." <HTML>" $> ; |
|
|
|
\ provide transparent proxying |
|
|
|
include ./proxy.fs |