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 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
|
(* This is the HTTP client example from the User's Manual *)
#use "topfind";;
#require "equeue";;
open Uq_engines
open Uq_engines.Operators
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 = Uq_client.connect_e
(`Socket(`Sock_inet_byname(Unix.SOCK_STREAM,
"www.camlcity.org", 80),
Uq_client.default_connect_options
)) ues in
let b = Buffer.create 10000 in
let e =
c ++
(fun connstat ->
match connstat with
| `Socket(fd, _) ->
prerr_endline "CONNECTED"; (* debug output *)
let d = `Polldescr(Netsys.get_fd_style fd, fd, ues) in
Uq_io.output_string_e d "GET / HTTP/1.0\n\n" ++
(fun () ->
Uq_io.write_eof_e d ++
(fun _ ->
let buffer = new async_buffer b in
new Uq_transfer.receiver ~src:fd ~dst:buffer ues
)
)
| _ -> assert false
) in
when_state
~is_done:(fun _ ->
prerr_endline "HTTP RESPONSE RECEIVED!";
print_endline (Buffer.contents b)
)
~is_error:(fun _ ->
prerr_endline "ERROR!"
)
e;
Unixqueue.run ues;
b
;;
main();;
|