… | |
… | |
52 | =item http_get $url, key => value..., $cb->($data, $headers) |
52 | =item http_get $url, key => value..., $cb->($data, $headers) |
53 | |
53 | |
54 | Executes an HTTP-GET request. See the http_request function for details on |
54 | Executes an HTTP-GET request. See the http_request function for details on |
55 | additional parameters. |
55 | additional parameters. |
56 | |
56 | |
|
|
57 | =item http_head $url, key => value..., $cb->($data, $headers) |
|
|
58 | |
|
|
59 | Executes an HTTP-HEAD request. See the http_request function for details on |
|
|
60 | additional parameters. |
|
|
61 | |
57 | =item http_get $url, $body, key => value..., $cb->($data, $headers) |
62 | =item http_post $url, $body, key => value..., $cb->($data, $headers) |
58 | |
63 | |
59 | Executes an HTTP-POST request with a requets body of C<$bod>. See the |
64 | Executes an HTTP-POST request with a request body of C<$bod>. See the |
60 | http_request function for details on additional parameters. |
65 | http_request function for details on additional parameters. |
61 | |
66 | |
62 | =item http_request $method => $url, key => value..., $cb->($data, $headers) |
67 | =item http_request $method => $url, key => value..., $cb->($data, $headers) |
63 | |
68 | |
64 | Executes a HTTP request of type C<$method> (e.g. C<GET>, C<POST>). The URL |
69 | Executes a HTTP request of type C<$method> (e.g. C<GET>, C<POST>). The URL |
… | |
… | |
66 | |
71 | |
67 | The callback will be called with the response data as first argument |
72 | The callback will be called with the response data as first argument |
68 | (or C<undef> if it wasn't available due to errors), and a hash-ref with |
73 | (or C<undef> if it wasn't available due to errors), and a hash-ref with |
69 | response headers as second argument. |
74 | response headers as second argument. |
70 | |
75 | |
71 | All the headers in that has are lowercased. In addition to the response |
76 | All the headers in that hash are lowercased. In addition to the response |
72 | headers, the three "pseudo-headers" C<HTTPVersion>, C<Status> and |
77 | headers, the three "pseudo-headers" C<HTTPVersion>, C<Status> and |
73 | C<Reason> contain the three parts of the HTTP Status-Line of the same |
78 | C<Reason> contain the three parts of the HTTP Status-Line of the same |
74 | name. |
79 | name. |
75 | |
80 | |
76 | If an internal error occurs, such as not being able to resolve a hostname, |
81 | If an internal error occurs, such as not being able to resolve a hostname, |
77 | then C<$data> will be C<undef>, C<< $headers->{Status} >> will be C<599> |
82 | then C<$data> will be C<undef>, C<< $headers->{Status} >> will be C<599> |
78 | and the C<Reason> pseudo-header will contain an error message. |
83 | and the C<Reason> pseudo-header will contain an error message. |
|
|
84 | |
|
|
85 | A typical callback might look like this: |
|
|
86 | |
|
|
87 | sub { |
|
|
88 | my ($body, $hdr) = @_; |
|
|
89 | |
|
|
90 | if ($hdr->{Status} =~ /^2/) { |
|
|
91 | ... everything should be ok |
|
|
92 | } else { |
|
|
93 | print "error, $hdr->{Status} $hdr->{Reason}\n"; |
|
|
94 | } |
|
|
95 | } |
79 | |
96 | |
80 | Additional parameters are key-value pairs, and are fully optional. They |
97 | Additional parameters are key-value pairs, and are fully optional. They |
81 | include: |
98 | include: |
82 | |
99 | |
83 | =over 4 |
100 | =over 4 |
… | |
… | |
241 | } |
258 | } |
242 | |
259 | |
243 | substr $_, 0, 1, "" |
260 | substr $_, 0, 1, "" |
244 | for values %hdr; |
261 | for values %hdr; |
245 | |
262 | |
246 | if ($method ne "HEAD") { |
263 | if ($method eq "HEAD") { |
|
|
264 | %state = (); |
|
|
265 | $cb->(undef, \%hdr); |
|
|
266 | } else { |
247 | if (exists $hdr{"content-length"}) { |
267 | if (exists $hdr{"content-length"}) { |
248 | $_[0]->unshift_read (chunk => $hdr{"content-length"}, sub { |
268 | $_[0]->unshift_read (chunk => $hdr{"content-length"}, sub { |
249 | # could cache persistent connection now |
269 | # could cache persistent connection now |
250 | if ($hdr{connection} =~ /\bkeep-alive\b/i) { |
270 | if ($hdr{connection} =~ /\bkeep-alive\b/i) { |
251 | # but we don't, due to misdesigns, this is annoyingly complex |
271 | # but we don't, due to misdesigns, this is annoyingly complex |
… | |
… | |
277 | sub http_get($$;@) { |
297 | sub http_get($$;@) { |
278 | unshift @_, "GET"; |
298 | unshift @_, "GET"; |
279 | &http_request |
299 | &http_request |
280 | } |
300 | } |
281 | |
301 | |
|
|
302 | sub http_head($$;@) { |
|
|
303 | unshift @_, "HEAD"; |
|
|
304 | &http_request |
|
|
305 | } |
|
|
306 | |
282 | sub http_post($$$;@) { |
307 | sub http_post($$$;@) { |
283 | unshift @_, "POST", "body"; |
308 | unshift @_, "POST", "body"; |
284 | &http_request |
309 | &http_request |
285 | } |
310 | } |
286 | |
311 | |