1 (** Wrapper around Perl [LWP::UserAgent] class. *)
2 (* Copyright (C) 2003 Merjis Ltd.
4 This library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Library General Public
6 License as published by the Free Software Foundation; either
7 version 2 of the License, or (at your option) any later version.
9 This library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Library General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this library; see the file COPYING. If not, write to
16 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 Boston, MA 02111-1307, USA.
19 $Id: pl_LWP_UserAgent.ml,v 1.6 2008-03-01 13:02:21 rich Exp $
28 let _ = eval "use LWP::UserAgent"
30 class lwp_useragent sv =
34 method simple_request (request : http_request) =
35 let sv = call_method sv "simple_request" [request#sv] in
37 method request (request : http_request) =
38 let sv = call_method sv "request" [request#sv] in
41 string_of_sv (call_method sv "agent" [])
43 call_method_void sv "agent" [sv_of_string v]
45 string_of_sv (call_method sv "from" [])
47 call_method_void sv "from" [sv_of_string v]
49 let sv = call_method sv "cookie_jar" [] in
51 method set_cookie_jar (v : http_cookies) =
52 call_method_void sv "cookie_jar" [v#sv]
53 method set_cookie_jar_file filename =
54 let hv = hv_empty () in
55 hv_set hv "file" (sv_of_string filename);
56 call_method_void sv "cookie_jar" [hashref hv]
57 method requests_redirectable =
58 let sv = call_method sv "requests_redirectable" [] in
59 let av = deref_array sv in
60 List.map string_of_sv (list_of_av av)
61 method set_requests_redirectable methods =
62 let av = av_empty () in
63 List.iter (av_push av) (List.map sv_of_string methods);
64 call_method_void sv "requests_redirectable" [arrayref av]
65 method add_requests_redirectable method_ =
66 let sv = call_method sv "requests_redirectable" [] in
67 let av = deref_array sv in
68 av_push av (sv_of_string method_)
70 int_of_sv (call_method sv "timeout" [])
71 method set_timeout v =
72 call_method_void sv "timeout" [sv_of_int v]
74 bool_of_sv (call_method sv "parse_head" [])
75 method set_parse_head v =
76 call_method_void sv "parse_head" [sv_of_bool v]
78 int_of_sv (call_method sv "max_size" [])
79 method set_max_size v =
80 call_method_void sv "max_size" [sv_of_int v]
82 call_method_void sv "env_proxy" []
86 (* Note that "new" is a reserved word, so I've appended an _ character. *)
87 let new_ ?agent ?from ?timeout ?use_eval ?parse_head ?max_size
88 ?env_proxy ?keep_alive ?cookie_jar ?cookie_jar_file () =
90 let may f = function None -> () | Some v -> f v in
92 args := sv_of_string "agent" :: sv_of_string v :: !args) agent;
94 args := sv_of_string "from" :: sv_of_string v :: !args) from;
96 args := sv_of_string "timeout" :: sv_of_int v :: !args) timeout;
98 args := sv_of_string "use_eval" :: sv_of_bool v :: !args) use_eval;
100 args := sv_of_string "parse_head" :: sv_of_bool v :: !args)parse_head;
102 args := sv_of_string "max_size" :: sv_of_int v :: !args) max_size;
104 args := sv_of_string "env_proxy" :: sv_of_bool v :: !args) env_proxy;
106 args := sv_of_string "keep_alive" :: sv_of_int v :: !args) keep_alive;
107 may (fun (v : http_cookies) ->
108 args := sv_of_string "cookie_jar" :: v#sv :: !args) cookie_jar;
110 let hv = hv_empty () in
111 hv_set hv "file" (sv_of_string v);
112 let sv = hashref hv in
113 args := sv_of_string "cookie_jar" :: sv :: !args) cookie_jar_file;
114 let sv = call_class_method "LWP::UserAgent" "new" !args in