home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / lifeos2.zip / LIFE-1.02 / LIB / CLIENT-S.LF < prev    next >
Text File  |  1996-06-04  |  2KB  |  64 lines

  1. % Copyright by Denys Duchier, Jan 1995
  2. % Simon Fraser University
  3. % --------------------------------------------------------------------
  4. %    $Id: client-server.lf,v 1.3 1995/01/28 00:11:34 duchier Exp $    
  5. % --------------------------------------------------------------------
  6.  
  7. module("client-server")?
  8. import("regexp")?
  9. public(connect_to_server,http_query,start_server)?
  10.  
  11. connect_to_server(Host:string,Port:int) -> S:sys#socket |
  12.     sys#connect(S,host=>Host,port=>Port).
  13.  
  14. http_query(URL:string) -> @(headers=>H,body=>B) |
  15.     (re_split("^http://([^/:]+):([0-9]+)(/.*)$",URL,
  16.           @(1=>Host,2=>PortString,3=>Document)),
  17.      Port=parse(PortString)
  18.     ;
  19.      re_split("^http://([^/:]+)(/.*)$",URL,
  20.           @(1=>Host,2=>Document)),
  21.      Port=80),!,
  22.     S=connect_to_server(Host,Port),
  23.     (succeed;sys#fclose(S),fail),
  24.     sys#fwrite(S,"GET "),
  25.     sys#fwrite(S,Document),
  26.     sys#fwrite(S," HTTP/1.0
  27.  
  28. "),
  29.     sys#fflush(S),
  30.     Status=sys#get_record(S,"
  31. "),
  32.     re_match("HTTP/[^ ]+ 200",Status),
  33.     get_headers(S,H),
  34.     B=sys#get_record(S,""),
  35.     sys#fclose(S),!.
  36.  
  37. get_headers(S,H) :-
  38.     Line=sys#get_record(S,"
  39. "),
  40.     (Line="
  41. ",
  42.      !
  43.     ;
  44.      re_split("^([^:]+): (.*)
  45. $",Line,@(Field,Value)),
  46.      H.Field<-Value,
  47.      get_headers(S,H)
  48.     ).
  49.  
  50. E:start_server(port=>P:int,handler=>H) :-
  51.     (is_value(P);
  52.      write("port feature must be an actual integer in: ",E),
  53.      abort),!,
  54.     S=sys#socket,
  55.     sys#bind(S,port=>P),
  56.     sys#listen(S,1),
  57.     repeat,
  58.     call_once(handle_connection(NS:sys#accept(S),H);succeed),
  59.     call_once(sys#fflush(NS);succeed),
  60.     call_once(sys#fclose(NS);succeed),
  61.     fail.
  62.  
  63. handle_connection(S:sys#socket_stream,H) -> H(S).
  64.