File:  [gforth] / gforth / proxy.fs
Revision 1.3: download - view: text, annotated - select for diffs
Sun Nov 19 22:47:54 2000 UTC (23 years, 4 months ago) by pazsan
Branches: MAIN
CVS tags: HEAD
Removed debugging code

    1: \ a http proxy
    2: 
    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
   19: \ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
   20: 
   21: require unix/socket.fs
   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: 
   39: Variable proxy s" localhost" proxy $!
   40: Variable proxy-port     3128 proxy-port !
   41: 
   42: : proxy-open ( host u request u -- fid )
   43:     proxy $@ proxy-port @ request ;
   44: 
   45: : http-open ( host u request u -- fid )
   46:     2over 80 request ;
   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:
   70: response: Cache-Control:
   71: response: Connection:
   72: response: Proxy-Connection:
   73: response: Content-Base:
   74: response: Content-Encoding:
   75: response: Content-Language:
   76: response: Content-Length:
   77: response: Content-Location:
   78: response: Content-MD5:
   79: response: Content-Range:
   80: response: Content-Type:
   81: response: Date:
   82: response: ETag:
   83: response: Expires:
   84: response: Last-modified:
   85: response: Location:
   86: response: Mime-Version:
   87: response: Proxy-Authenticate:
   88: response: Public:
   89: response: Retry-After:
   90: response: Server:
   91: response: Transfer-Encoding:
   92: response: Upgrade:
   93: response: Via:
   94: response: Warning:
   95: response: WWW-Authenticate:
   96: response: X-Cache:
   97: response: X-Powered-By:
   98: 
   99: Forth definitions
  100: 
  101: \ response handling
  102: 
  103: : get-response ( fid -- ior )
  104:     push-file loadfile !  loadline off  blk off
  105:     response 1 set-order  ['] refill-loop catch
  106:     only forth also  pop-file ;
  107: 
  108: \ data handling
  109: 
  110: Variable data-buffer
  111: 
  112: : clear-data ( -- )
  113:     s" " data-buffer $! ;
  114: : add-chunk ( u fid -- u' )
  115:     swap data-buffer $@len dup >r + data-buffer $!len
  116:     data-buffer $@ r@ /string rot read-file throw
  117:     dup r> + data-buffer $!len ;
  118: : read-sized ( u fid -- )
  119:     add-chunk drop ;
  120: : read-to-end ( fid -- )
  121:     >r BEGIN  $1000 r@ add-chunk $1000 <> UNTIL  rdrop ;
  122: 
  123: : read-chunked ( fid -- ) base @ >r hex >r
  124:     BEGIN  pad $100 r@ read-line throw  WHILE
  125: 	pad swap s>number drop dup WHILE  r@ add-chunk drop
  126: 	pad 1 r@ read-line throw  nip 0= UNTIL
  127:     ELSE  drop  THEN  THEN  rdrop r> base ! ;
  128: 
  129: : read-data ( fid -- ) clear-data >r
  130:     Content-Length @ IF
  131: 	Content-Length $@ s>number drop r> read-sized  EXIT  THEN
  132:     Transfer-Encoding @ IF
  133: 	Transfer-Encoding $@ s" chunked" compare 0= IF
  134: 	    r> read-chunked  EXIT  THEN  THEN
  135:     r> read-to-end ;
  136: 
  137: \ convert data
  138: 
  139: : convert-data ( -- )
  140:     \ stub
  141: ;
  142: 
  143: \ write response
  144: 
  145: : write-response ( -- ) \ stub -- we really want to mirror what we got
  146:     .ok
  147:     ." Connection: close" cr
  148:     ." Accept-Ranges: bytes" cr
  149:     ." Content-Type: " Content-Type $@ type cr
  150:     ." Content-Length: " data-buffer $@len 0 .r cr cr ;
  151: 
  152: \ write data
  153: 
  154: : write-data ( -- )
  155:     data-buffer $@ type ;
  156: 
  157: \ handle proxy request
  158: 
  159: : proxy-request ( host u request u -- )
  160:     proxy-open
  161:     dup >r get-response throw
  162:     r@ read-data r> close-file throw
  163:     convert-data write-response write-data ;
  164: 
  165: : http-request ( host u request u -- )
  166:     http-open
  167:     dup >r get-response throw
  168:     r@ read-data r> close-file throw
  169:     convert-data write-response write-data ;
  170: 
  171: \ request redirection
  172: 
  173: wordlist Constant redirects
  174: 
  175: Variable redir$
  176: Variable host$
  177: 
  178: : redirect: ( "path" host<"> redirecton<"> -- )  Create
  179:     [char] " parse  here over char+ allot  place
  180:     [char] " parse  here over char+ allot  place
  181: DOES> ( -- addr u )
  182:     data @ IF s" GET " ELSE s" HEAD " THEN redir$ $!
  183:     count 2dup host$ $! +
  184:     count redir$ $+!
  185:     source >in @ /string dup >in +!
  186:     2dup bounds ?DO  I c@ #lf = IF  '/ I c!  THEN  LOOP
  187:     redir$ $+! redir$ $@ ;
  188: 
  189: : (redirect?) ( addr u -- addr' u' t / f )
  190:     htmldir $! htmldir $@ bounds ?DO
  191: 	I c@ '/ = IF  #lf I c!  THEN  LOOP
  192:     redirects 1 set-order
  193:     htmldir $@ ['] evaluate catch
  194:     IF  2drop false  ELSE  true  THEN ;
  195: 
  196: : (redirect) ( addr u -- )
  197:     host$ $@ 2swap proxy-request maxnum off ;
  198: 
  199: ' (redirect?) IS redirect?
  200: ' (redirect) IS redirect
  201: 
  202: \ example
  203: 
  204: redirects set-current
  205: get-order redirects swap 1+ set-order
  206: 
  207: Vocabulary systems
  208: 
  209: also systems definitions
  210: 
  211: redirect: bigforth www.jwdt.com"http://www.jwdt.com/~paysan/"
  212: 
  213: previous previous definitions

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