Diff for /gforth/httpd.fs between versions 1.2 and 1.5

version 1.2, 2000/04/02 20:18:27 version 1.5, 2000/04/12 20:53:46
Line 5  warnings off Line 5  warnings off
 include string.fs  include string.fs
   
 Variable url  Variable url
   Variable posted
   Variable url-args
 Variable protocol  Variable protocol
 Variable data  Variable data
   Variable active
 Variable command?  Variable command?
   
 : get ( addr -- )  name rot $! ;  : get ( addr -- )  name rot $! ;
 : get-rest ( addr -- )  source >in @ /string dup >in +! rot $! ;  : get-rest ( addr -- )  source >in @ /string dup >in +! rot $! ;
   
 Table constant values  wordlist constant values
 Table constant commands  wordlist constant commands
   
 : value:  ( -- )  name  : value:  ( -- )  name
   Forth definitions 2dup 1- nextname Variable    Forth definitions 2dup 1- nextname Variable
Line 23  Table constant commands Line 26  Table constant commands
   
 \ HTTP protocol commands                               26mar00py  \ HTTP protocol commands                               26mar00py
   
 : rework-% ( -- )  base @ >r hex  : rework-% ( add -- ) { url }  base @ >r hex
     0 url $@len 0 ?DO      0 url $@len 0 ?DO
         url $@ drop I + c@ dup '% = IF          url $@ drop I + c@ dup '% = IF
             drop 0. url $@ I 1+ /string              drop 0. url $@ I 1+ /string
Line 32  Table constant commands Line 35  Table constant commands
     r> 1+ +LOOP  url $!len      r> 1+ +LOOP  url $!len
     r> base ! ;      r> base ! ;
   
   : rework-? ( addr -- )
       dup >r $@ '? $split url-args $! nip r> $!len ;
   
   : get-url ( -- ) url get protocol get-rest
       url rework-? url rework-% >values ;
   
 commands set-current  commands set-current
   
 : GET   url get rework-% protocol get-rest >values data on  ;  : GET   get-url data on  active off ;
 : HEAD  url get rework-% protocol get-rest >values data off ;  : POST  get-url data on  active on  ;
   : HEAD  get-url data off active off ;
   
 \ HTTP protocol values                                 26mar00py  \ HTTP protocol values                                 26mar00py
   
Line 53  value: X-Forwarded-For: Line 63  value: X-Forwarded-For:
 value: Cache-Control:  value: Cache-Control:
 value: Connection:  value: Connection:
 value: Referer:  value: Referer:
   value: Content-Type:
   value: Content-Length:
   
 definitions  definitions
   
 s" HTTP/1.0" protocol $!  
   
 Variable maxnum  Variable maxnum
   
 : ?cr ( -- )  : ?cr ( -- )
Line 66  Variable maxnum Line 76  Variable maxnum
   BEGIN  refill ?cr  WHILE  interpret  >in @ 0=  UNTIL    BEGIN  refill ?cr  WHILE  interpret  >in @ 0=  UNTIL
   true  ELSE  maxnum off false  THEN ;    true  ELSE  maxnum off false  THEN ;
 : get-input ( -- flag ior )  : get-input ( -- flag ior )
   s" /nosuchfile" url $!    s" /nosuchfile" url $!  s" HTTP/1.0" protocol $!
   s" close" connection $!    s" close" connection $!
   infile-id push-file loadfile !  0 loadline ! 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
   only forth also  pop-file ;    active @ IF  s" " posted $! Content-Length $@ snumber? drop
         posted $!len  posted $@ infile-id read-file throw drop
 \ Keep-Alive handling                                  26mar00py    THEN  only forth also  pop-file ;
   
 : .connection ( -- )  
   ." Connection: "  
   connection $@ s" Keep-Alive" compare 0= maxnum @ 0> and  
   IF  connection $@ type cr  
       ." Keep-Alive: timeout=15, max=" maxnum @ 0 .r cr  
       -1 maxnum +!  ELSE  ." close" cr maxnum off  THEN ;  
   
 \ Use Forth as server-side script language             26mar00py  
   
 : $> ( -- )  
     BEGIN  source >in @ /string s" <$" search  0= WHILE  
         type cr refill  0= UNTIL  EXIT  THEN  
     nip source >in @ /string rot - dup 2 + >in +! type ;  
 : <HTML> ( -- )  ." <HTML>" $> ;  
   
 \ Rework HTML directory                                26mar00py  \ Rework HTML directory                                26mar00py
   
Line 114  Variable htmldir Line 109  Variable htmldir
   r@ file-size throw drop    r@ file-size throw drop
   ." Accept-Ranges: bytes" cr    ." Accept-Ranges: bytes" cr
   ." Content-Length: " dup 0 .r cr r> ;    ." Content-Length: " dup 0 .r cr r> ;
 : transparent ( size fd -- ) >r  : transparent ( size fd -- ) { fd }
   dup allocate throw swap      $4000 allocate throw swap dup 0 ?DO
   over swap r@ read-file throw over swap type          2dup over swap $4000 min fd read-file throw type
   free r> close-file throw throw ;          $4000 - $4000 +LOOP  drop
       free fd close-file throw throw ;
   
   \ Keep-Alive handling                                  26mar00py
   
   : .connection ( -- )
     ." Connection: "
     connection $@ s" Keep-Alive" compare 0= maxnum @ 0> and
     IF  connection $@ type cr
         ." Keep-Alive: timeout=15, max=" maxnum @ 0 .r cr
         -1 maxnum +!  ELSE  ." close" cr maxnum off  THEN ;
   
 : transparent: ( addr u -- ) Create  here over 1+ allot place  : transparent: ( addr u -- ) Create  here over 1+ allot place
   DOES>  >r  >file    DOES>  >r  >file
Line 156  s" text/plain" transparent: txt Line 161  s" text/plain" transparent: txt
   
 \ http errors                                          26mar00py  \ http errors                                          26mar00py
   
 : .server ." Server: Gforth httpd/0.1 ("  : .server ( -- )  ." Server: Gforth httpd/0.1 ("
     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 -- )
     ." HTTP/1.1 " 2 pick . 2dup type cr .server      ." HTTP/1.1 " 2 pick . 2dup type cr .server
     2 pick &405 = IF ." Allow: GET, HEAD" cr  THEN  lastrequest      2 pick &405 = IF ." Allow: GET, HEAD, POST" cr  THEN
     ." <HTML><HEAD><TITLE>" 2 pick . 2dup type ." </TITLE></HEAD>" cr      lastrequest
       ." <HTML><HEAD><TITLE>" 2 pick . 2dup type
       ." </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 0.1</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
     ." <P>Your browser sent a request that this server could not understand.</P>" cr      ." <P>Your browser sent a request that this server "
     ." <P>Invalid request in: <CODE>" error-stack cell+ 2@ swap type      ." could not understand.</P>" cr
       ." <P>Invalid request in: <CODE>"
       error-stack cell+ 2@ swap type
     ." </CODE></P>" cr .trailer ;      ." </CODE></P>" cr .trailer ;
 : .nofile  &404 s" Not Found" html-error  : .nofile ( -- ) &404 s" Not Found" html-error
     ." <P>The requested URL <CODE>" url $@ type      ." <P>The requested URL <CODE>" url $@ type
     ." </CODE> was not found on this server</P>" cr .trailer ;      ." </CODE> was not found on this server</P>" cr .trailer ;
   
 \ http server                                          26mar00py  \ http server                                          26mar00py
   
 : http  get-input  IF  .nok  ELSE  : http ( -- )  get-input  IF  .nok  ELSE
     IF  url $@ 1 /string rework-htmldir      IF  url $@ 1 /string 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
Line 186  s" text/plain" transparent: txt Line 195  s" text/plain" transparent: txt
         THEN  THEN  THEN  outfile-id flush-file throw ;          THEN  THEN  THEN  outfile-id flush-file throw ;
   
 : httpd  ( n -- )  maxnum !  : httpd  ( n -- )  maxnum !
   BEGIN  http  maxnum @ 0=  UNTIL ;    BEGIN  ['] http catch  maxnum @ 0= or  UNTIL ;
   
   script? [IF]  :noname &100 httpd bye ; is bootmessage  [THEN]
   
 script? [IF]  &100 httpd bye  [THEN]  \ Use Forth as server-side script language             26mar00py
   
   : $> ( -- )
       BEGIN  source >in @ /string s" <$" search  0= WHILE
           type cr refill  0= UNTIL  EXIT  THEN
       nip source >in @ /string rot - dup 2 + >in +! type ;
   : <HTML> ( -- )  ." <HTML>" $> ;

Removed from v.1.2  
changed lines
  Added in v.1.5


FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>