Annotation of gforth/proxy.fs, revision 1.14

1.1       pazsan      1: \ a http proxy
                      2: 
1.13      anton       3: \ Copyright (C) 2000,2002,2003,2006,2007 Free Software Foundation, Inc.
1.1       pazsan      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
1.12      anton       9: \ as published by the Free Software Foundation, either version 3
1.1       pazsan     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
1.12      anton      18: \ along with this program. If not, see http://www.gnu.org/licenses/.
1.1       pazsan     19: 
                     20: require unix/socket.fs
1.14    ! pazsan     21: require string.fs
1.1       pazsan     22: 
                     23: Create crlf #cr c, #lf c,
                     24: 
                     25: : writeln ( addr u fd -- )
                     26:     dup >r write-file throw crlf 2 r> write-file throw ;
                     27: 
                     28: : request ( host u request u proxy-host u port -- fid )
                     29:     open-socket >r
                     30:     r@ write-file throw s"  HTTP/1.1" r@ writeln
                     31:     s" Host: " r@ write-file throw r@ writeln
                     32:     s" Connection: close" r@ writeln
                     33:     s" User-Agent: " r@ write-file throw
                     34:     User-Agent @ IF
                     35:        User-Agent $@ r@ write-file throw s" via Gforth Proxy 0.1"
                     36:     ELSE  s" Gforth Proxy 0.1"  THEN  r@ writeln
                     37:     s" " r@ writeln r> ;
                     38: 
1.10      pazsan     39: Variable proxy          \ s" proxy" proxy $! \ replace that with your proxy host
                     40: Variable proxy-port     \ 8080 proxy-port !  \ replace that with your proxy port
1.1       pazsan     41: 
1.10      pazsan     42: \ set proxy to your local proxy, and proxy-port to your local proxy port
                     43: \ if you need any.
1.1       pazsan     44: 
                     45: : http-open ( host u request u -- fid )
1.10      pazsan     46:     proxy @ 0= IF  2over 80  ELSE  proxy $@ proxy-port @  THEN request ;
1.1       pazsan     47: 
                     48: wordlist Constant response
                     49: wordlist Constant response-values
                     50: 
                     51: Variable response-string
                     52: 
                     53: : response:  ( -- )  name
                     54:     Forth definitions 2dup 1- nextname Variable
                     55:     response-values set-current nextname here cell - Create ,
                     56: DOES> @ get-rest ;
                     57: : >response  response-values 1 set-order ;
                     58: 
                     59: response set-current
                     60: 
                     61: : HTTP/1.1 response-string get-rest >response ;
                     62: : HTTP/1.0 response-string get-rest >response ;
                     63: 
                     64: \ response variables
                     65: 
                     66: Forth definitions
                     67: 
                     68: response: Allow:
                     69: response: Age:
1.4       pazsan     70: response: Accept-Ranges:
1.1       pazsan     71: response: Cache-Control:
                     72: response: Connection:
                     73: response: Proxy-Connection:
                     74: response: Content-Base:
                     75: response: Content-Encoding:
                     76: response: Content-Language:
                     77: response: Content-Length:
                     78: response: Content-Location:
                     79: response: Content-MD5:
                     80: response: Content-Range:
                     81: response: Content-Type:
                     82: response: Date:
                     83: response: ETag:
                     84: response: Expires:
1.4       pazsan     85: response: Last-Modified:
1.1       pazsan     86: response: Location:
                     87: response: Mime-Version:
                     88: response: Proxy-Authenticate:
1.4       pazsan     89: response: Proxy-Connection:
1.1       pazsan     90: response: Public:
                     91: response: Retry-After:
                     92: response: Server:
                     93: response: Transfer-Encoding:
                     94: response: Upgrade:
                     95: response: Via:
                     96: response: Warning:
                     97: response: WWW-Authenticate:
                     98: response: X-Cache:
                     99: response: X-Powered-By:
                    100: 
                    101: Forth definitions
                    102: 
                    103: \ response handling
                    104: 
                    105: : get-response ( fid -- ior )
                    106:     push-file loadfile !  loadline off  blk off
                    107:     response 1 set-order  ['] refill-loop catch
                    108:     only forth also  pop-file ;
                    109: 
                    110: \ data handling
                    111: 
                    112: Variable data-buffer
                    113: 
                    114: : clear-data ( -- )
                    115:     s" " data-buffer $! ;
                    116: : add-chunk ( u fid -- u' )
                    117:     swap data-buffer $@len dup >r + data-buffer $!len
                    118:     data-buffer $@ r@ /string rot read-file throw
                    119:     dup r> + data-buffer $!len ;
                    120: : read-sized ( u fid -- )
                    121:     add-chunk drop ;
                    122: : read-to-end ( fid -- )
                    123:     >r BEGIN  $1000 r@ add-chunk $1000 <> UNTIL  rdrop ;
                    124: 
                    125: : read-chunked ( fid -- ) base @ >r hex >r
                    126:     BEGIN  pad $100 r@ read-line throw  WHILE
                    127:        pad swap s>number drop dup WHILE  r@ add-chunk drop
                    128:        pad 1 r@ read-line throw  nip 0= UNTIL
                    129:     ELSE  drop  THEN  THEN  rdrop r> base ! ;
                    130: 
                    131: : read-data ( fid -- ) clear-data >r
                    132:     Content-Length @ IF
                    133:        Content-Length $@ s>number drop r> read-sized  EXIT  THEN
                    134:     Transfer-Encoding @ IF
1.14    ! pazsan    135:        Transfer-Encoding $@ s" chunked" str= IF
1.1       pazsan    136:            r> read-chunked  EXIT  THEN  THEN
                    137:     r> read-to-end ;
                    138: 
                    139: \ convert data
                    140: 
                    141: : convert-data ( -- )
                    142:     \ stub
                    143: ;
                    144: 
                    145: \ write response
                    146: 
                    147: : write-response ( -- ) \ stub -- we really want to mirror what we got
                    148:     .ok
                    149:     ." Connection: close" cr
                    150:     ." Accept-Ranges: bytes" cr
                    151:     ." Content-Type: " Content-Type $@ type cr
                    152:     ." Content-Length: " data-buffer $@len 0 .r cr cr ;
                    153: 
                    154: \ write data
                    155: 
                    156: : write-data ( -- )
                    157:     data-buffer $@ type ;
                    158: 
                    159: \ handle proxy request
                    160: 
1.5       pazsan    161: : handle-request ( fid -- )
1.1       pazsan    162:     dup >r get-response throw
                    163:     r@ read-data r> close-file throw
                    164:     convert-data write-response write-data ;
                    165: 
                    166: \ request redirection
                    167: 
                    168: wordlist Constant redirects
                    169: 
                    170: Variable redir$
                    171: Variable host$
                    172: 
                    173: : redirect: ( "path" host<"> redirecton<"> -- )  Create
                    174:     [char] " parse  here over char+ allot  place
                    175:     [char] " parse  here over char+ allot  place
                    176: DOES> ( -- addr u )
                    177:     data @ IF s" GET " ELSE s" HEAD " THEN redir$ $!
                    178:     count 2dup host$ $! +
                    179:     count redir$ $+!
                    180:     source >in @ /string dup >in +!
                    181:     2dup bounds ?DO  I c@ #lf = IF  '/ I c!  THEN  LOOP
                    182:     redir$ $+! redir$ $@ ;
                    183: 
                    184: : (redirect?) ( addr u -- addr' u' t / f )
                    185:     htmldir $! htmldir $@ bounds ?DO
                    186:        I c@ '/ = IF  #lf I c!  THEN  LOOP
1.5       pazsan    187:     redirects 1 set-order redir$ $off
1.1       pazsan    188:     htmldir $@ ['] evaluate catch
1.6       pazsan    189:     IF  2drop false  ELSE  redir$ @ 0<>  THEN ;
1.1       pazsan    190: 
1.5       pazsan    191: : (redirect) ( -- )
1.10      pazsan    192:     host$ $@ redir$ $@ http-open handle-request maxnum off ;
1.1       pazsan    193: 
                    194: ' (redirect?) IS redirect?
                    195: ' (redirect) IS redirect
                    196: 
                    197: \ example
                    198: 
                    199: redirects set-current
                    200: get-order redirects swap 1+ set-order
                    201: 
                    202: Vocabulary systems
1.10      pazsan    203: Vocabulary humor
1.1       pazsan    204: 
                    205: also systems definitions
                    206: 
1.10      pazsan    207: redirect: bigforth bigforth.sourceforge.net"/"
                    208: 
                    209: humor definitions
                    210: 
                    211: redirect: bush www.jwdt.com"/~paysan/bush/"
1.1       pazsan    212: 
                    213: previous previous definitions

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