1 (** Wrapper around Perl [LWP::UserAgent] class.
3 * Copyright (C) 2003 Merjis Ltd.
5 * $Id: pl_LWP_UserAgent.ml,v 1.5 2004-11-25 21:24:51 rich Exp $
14 let _ = eval "use LWP::UserAgent"
16 class lwp_useragent sv =
20 method simple_request (request : http_request) =
21 let sv = call_method sv "simple_request" [request#sv] in
23 method request (request : http_request) =
24 let sv = call_method sv "request" [request#sv] in
27 string_of_sv (call_method sv "agent" [])
29 call_method_void sv "agent" [sv_of_string v]
31 string_of_sv (call_method sv "from" [])
33 call_method_void sv "from" [sv_of_string v]
35 let sv = call_method sv "cookie_jar" [] in
37 method set_cookie_jar (v : http_cookies) =
38 call_method_void sv "cookie_jar" [v#sv]
39 method set_cookie_jar_file filename =
40 let hv = hv_empty () in
41 hv_set hv "file" (sv_of_string filename);
42 call_method_void sv "cookie_jar" [hashref hv]
43 method requests_redirectable =
44 let sv = call_method sv "requests_redirectable" [] in
45 let av = deref_array sv in
46 List.map string_of_sv (list_of_av av)
47 method set_requests_redirectable methods =
48 let av = av_empty () in
49 List.iter (av_push av) (List.map sv_of_string methods);
50 call_method_void sv "requests_redirectable" [arrayref av]
51 method add_requests_redirectable method_ =
52 let sv = call_method sv "requests_redirectable" [] in
53 let av = deref_array sv in
54 av_push av (sv_of_string method_)
56 int_of_sv (call_method sv "timeout" [])
57 method set_timeout v =
58 call_method_void sv "timeout" [sv_of_int v]
60 bool_of_sv (call_method sv "parse_head" [])
61 method set_parse_head v =
62 call_method_void sv "parse_head" [sv_of_bool v]
64 int_of_sv (call_method sv "max_size" [])
65 method set_max_size v =
66 call_method_void sv "max_size" [sv_of_int v]
68 call_method_void sv "env_proxy" []
72 (* Note that "new" is a reserved word, so I've appended an _ character. *)
73 let new_ ?agent ?from ?timeout ?use_eval ?parse_head ?max_size
74 ?env_proxy ?keep_alive ?cookie_jar ?cookie_jar_file () =
76 let may f = function None -> () | Some v -> f v in
78 args := sv_of_string "agent" :: sv_of_string v :: !args) agent;
80 args := sv_of_string "from" :: sv_of_string v :: !args) from;
82 args := sv_of_string "timeout" :: sv_of_int v :: !args) timeout;
84 args := sv_of_string "use_eval" :: sv_of_bool v :: !args) use_eval;
86 args := sv_of_string "parse_head" :: sv_of_bool v :: !args)parse_head;
88 args := sv_of_string "max_size" :: sv_of_int v :: !args) max_size;
90 args := sv_of_string "env_proxy" :: sv_of_bool v :: !args) env_proxy;
92 args := sv_of_string "keep_alive" :: sv_of_int v :: !args) keep_alive;
93 may (fun (v : http_cookies) ->
94 args := sv_of_string "cookie_jar" :: v#sv :: !args) cookie_jar;
96 let hv = hv_empty () in
97 hv_set hv "file" (sv_of_string v);
98 let sv = hashref hv in
99 args := sv_of_string "cookie_jar" :: sv :: !args) cookie_jar_file;
100 let sv = call_class_method "LWP::UserAgent" "new" !args in