File: http_client.ml

package info (click to toggle)
ocamlnet 2.2.9-8
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 17,724 kB
  • ctags: 10,053
  • sloc: ml: 63,928; ansic: 1,973; makefile: 800; sh: 651
file content (44 lines) | stat: -rw-r--r-- 1,219 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
(* This is the HTTP client example from the User's Manual *)

open Uq_engines;;

class async_buffer b =
object (self)
  inherit Netchannels.output_buffer b
  method can_output = true
  method request_notification (f : unit->bool) = ()
end ;;


let main() =
  let ues = Unixqueue.create_unix_event_system() in
  let c = connector (`Socket(`Sock_inet_byname(Unix.SOCK_STREAM,
					       "www.npc.de", 80),
			     default_connect_options
			    )) ues in
  let b = Buffer.create 10000 in

  when_state
    ~is_done:(fun connstat ->
		match connstat with
		    `Socket(fd, _) ->
		      prerr_endline "CONNECTED";
		      let printer = new output_async_descr ~dst:fd ues in
		      let buffer = new async_buffer b in
		      let receiver = new receiver ~src:fd ~dst:buffer ues in
		      let s = "GET / HTTP/1.0\n\n" in
		      ignore(printer # output s 0 (String.length s));
	              when_state
                        ~is_done:(fun _ ->
                                    prerr_endline "HTTP RESPONSE RECEIVED!")
	                ~is_error:(fun _ ->
                                    prerr_endline "ERROR!")
                        receiver
		  | _ -> assert false
	     )
    c;

  Unixqueue.run ues;

  b
;;