]> Dogcows Code - chaz/p5-HTTP-AnyUA/blob - lib/HTTP/AnyUA/Util.pm
initial commit
[chaz/p5-HTTP-AnyUA] / lib / HTTP / AnyUA / Util.pm
1 package HTTP::AnyUA::Util;
2 # ABSTRACT: Utility subroutines for HTTP::AnyUA backends
3
4 use warnings;
5 use strict;
6
7 our $VERSION = '9999.999'; # VERSION
8
9 use Exporter qw(import);
10
11
12 our @EXPORT_OK = qw(
13 http_headers_to_native
14 native_to_http_request
15 coderef_content_to_string
16 internal_exception
17 http_date
18 parse_http_date
19 uri_escape
20 www_form_urlencode
21 );
22
23
24 sub _croak { require Carp; Carp::croak(@_) }
25 sub _usage { _croak("Usage: @_\n") }
26
27 =func coderef_content_to_string
28
29 $content = coderef_content_to_string(\&code);
30 $content = coderef_content_to_string($content); # noop
31
32 Convert a coderef into a string of content by iteratively calling the coderef and concatenating the
33 chunks it provides until the coderef returns undef or an empty string.
34
35 =cut
36
37 sub coderef_content_to_string {
38 my $content = shift;
39
40 return $content if !$content;
41
42 if (ref($content) eq 'CODE') {
43 # drain the request body
44 my $body = '';
45 while (my $chunk = $content->()) {
46 $body .= $chunk;
47 }
48 $content = $body;
49 }
50
51 return $content;
52 }
53
54 =func native_to_http_request
55
56 $http_request = native_to_http_request($method, $url);
57 $http_request = native_to_http_request($method, $url, \%options);
58
59 Convert a "native" request tuple to an L<HTTP::Request> object.
60
61 =cut
62
63 sub native_to_http_request {
64 my $method = shift;
65 my $url = shift;
66 my $args = shift || {};
67
68 my $headers = [];
69 my $content = $args->{content}; # works as either scalar or coderef
70
71 # flatten headers
72 for my $header (keys %{$args->{headers} || {}}) {
73 my $value = $args->{headers}{$header};
74 my @values = ref($value) eq 'ARRAY' ? @$value : ($value);
75 for my $v (@values) {
76 push @$headers, ($header => $v);
77 }
78 }
79
80 require HTTP::Request;
81 return HTTP::Request->new($method, $url, $headers, $content);
82 }
83
84 =func http_headers_to_native
85
86 $headers = http_headers_to_native($http_headers);
87
88 Convert an L<HTTP::Headers> object to a "native" hashref.
89
90 =cut
91
92 sub http_headers_to_native {
93 my $http_headers = shift;
94
95 my $native;
96
97 for my $header ($http_headers->header_field_names) {
98 my @values = $http_headers->header($header);
99 $native->{lc($header)} = @values == 1 ? $values[0] : [@values];
100 }
101
102 return $native;
103 }
104
105 =func internal_exception
106
107 $response = internal_exception($content);
108 $response = internal_exception($content, $response);
109
110 Create an internal exception response. If an existing response is passed, that response will have
111 its fields modified to become an internal exception.
112
113 =cut
114
115 sub internal_exception {
116 my $e = shift or _usage(q{internal_exception($exception)});
117 my $resp = shift || {};
118
119 $e = "$e";
120
121 $resp->{headers}{'client-original-status'} = $resp->{status} if $resp->{status};
122 $resp->{headers}{'client-original-reason'} = $resp->{reason} if $resp->{reason};
123
124 $resp->{success} = '';
125 $resp->{status} = 599;
126 $resp->{reason} = 'Internal Exception';
127 $resp->{content} = $e;
128 $resp->{headers}{'content-type'} = 'text/plain';
129 $resp->{headers}{'content-length'} = length $e;
130
131 return $resp;
132 }
133
134 =func split_url
135
136 ($scheme, $host, $port, $path_query, $auth) = split_url($url);
137
138 Split a URL into its components.
139
140 =cut
141
142 # adapted from HTTP/Tiny.pm
143 sub split_url {
144 my $url = shift or _usage(q{split_url($url)});
145
146 # URI regex adapted from the URI module
147 my ($scheme, $host, $path_query) = $url =~ m<\A([^:/?#]+)://([^/?#]*)([^#]*)>
148 or die(qq/Cannot parse URL: '$url'\n/);
149
150 $scheme = lc $scheme;
151 $path_query = "/$path_query" unless $path_query =~ m<\A/>;
152
153 my $auth = '';
154 if ( (my $i = index $host, '@') != -1 ) {
155 # user:pass@host
156 $auth = substr $host, 0, $i, ''; # take up to the @ for auth
157 substr $host, 0, 1, ''; # knock the @ off the host
158
159 # userinfo might be percent escaped, so recover real auth info
160 $auth =~ s/%([0-9A-Fa-f]{2})/chr(hex($1))/eg;
161 }
162 my $port = $host =~ s/:(\d*)\z// && length $1 ? $1
163 : $scheme eq 'http' ? 80
164 : $scheme eq 'https' ? 443
165 : undef;
166
167 return ($scheme, (length $host ? lc $host : "localhost") , $port, $path_query, $auth);
168 }
169
170 =func http_date
171
172 $http_date = http_date($epoch_time);
173
174 Convert an epoch time into a date format suitable for HTTP.
175
176 =cut
177
178 # Date conversions adapted from HTTP::Date
179 # adapted from HTTP/Tiny.pm
180 my $DoW = 'Sun|Mon|Tue|Wed|Thu|Fri|Sat';
181 my $MoY = 'Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec';
182 sub http_date {
183 my $time = shift or _usage(q{http_date($time)});
184 my ($sec, $min, $hour, $mday, $mon, $year, $wday) = gmtime($time);
185 return sprintf('%s, %02d %s %04d %02d:%02d:%02d GMT',
186 substr($DoW,$wday*4,3),
187 $mday, substr($MoY,$mon*4,3), $year+1900,
188 $hour, $min, $sec
189 );
190 }
191
192 =func parse_http_date
193
194 $epoch_time = parse_http_date($http_date);
195
196 Convert an HTTP date into an epoch time. Returns undef if the date cannot be parsed.
197
198 =cut
199
200 # adapted from HTTP/Tiny.pm
201 sub parse_http_date {
202 my $str = shift or _usage(q{parse_http_date($str)});
203 my @tl_parts;
204 if ($str =~ /^[SMTWF][a-z]+, +(\d{1,2}) ($MoY) +(\d\d\d\d) +(\d\d):(\d\d):(\d\d) +GMT$/) {
205 @tl_parts = ($6, $5, $4, $1, (index($MoY,$2)/4), $3);
206 }
207 elsif ($str =~ /^[SMTWF][a-z]+, +(\d\d)-($MoY)-(\d{2,4}) +(\d\d):(\d\d):(\d\d) +GMT$/ ) {
208 @tl_parts = ($6, $5, $4, $1, (index($MoY,$2)/4), $3);
209 }
210 elsif ($str =~ /^[SMTWF][a-z]+ +($MoY) +(\d{1,2}) +(\d\d):(\d\d):(\d\d) +(?:[^0-9]+ +)?(\d\d\d\d)$/ ) {
211 @tl_parts = ($5, $4, $3, $2, (index($MoY,$1)/4), $6);
212 }
213 require Time::Local;
214 return eval {
215 my $t = @tl_parts ? Time::Local::timegm(@tl_parts) : -1;
216 $t < 0 ? undef : $t;
217 };
218 }
219
220 =func uri_escape
221
222 $escaped = uri_escape($unescaped);
223
224 Escape a string for use in a URL query param or as C<application/x-www-form-urlencoded> data.
225
226 =cut
227
228 # URI escaping adapted from URI::Escape
229 # c.f. http://www.w3.org/TR/html4/interact/forms.html#h-17.13.4.1
230 # perl 5.6 ready UTF-8 encoding adapted from JSON::PP
231 # adapted from HTTP/Tiny.pm
232 my %escapes = map { chr($_) => sprintf('%%%02X', $_) } 0..255;
233 $escapes{' '} = '+';
234 my $unsafe_char = qr/[^A-Za-z0-9\-\._~]/;
235
236 sub uri_escape {
237 my $str = shift or _usage(q{uri_escape($str)});
238 if ($] ge '5.008') {
239 utf8::encode($str);
240 }
241 else {
242 $str = pack('U*', unpack('C*', $str)) # UTF-8 encode a byte string
243 if (length $str == do { use bytes; length $str });
244 $str = pack('C*', unpack('C*', $str)); # clear UTF-8 flag
245 }
246 $str =~ s/($unsafe_char)/$escapes{$1}/ge;
247 return $str;
248 }
249
250 =func www_form_urlencode
251
252 $bytes = www_form_urlencode(\%form_data);
253 $bytes = www_form_urlencode(\@form_data);
254
255 Encode a hashref or arrayref as C<application/x-www-form-urlencoded> data.
256
257 =cut
258
259 # adapted from HTTP/Tiny.pm
260 sub www_form_urlencode {
261 my $data = shift;
262 ($data && ref $data)
263 or _usage(q{www_form_urlencode($dataref)});
264 (ref $data eq 'HASH' || ref $data eq 'ARRAY')
265 or _croak("form data must be a hash or array reference\n");
266
267 my @params = ref $data eq 'HASH' ? %$data : @$data;
268 @params % 2 == 0
269 or _croak("form data reference must have an even number of terms\n");
270
271 my @terms;
272 while (@params) {
273 my ($key, $value) = splice(@params, 0, 2);
274 if (ref $value eq 'ARRAY') {
275 unshift @params, map { $key => $_ } @$value;
276 }
277 else {
278 push @terms, join('=', map { uri_escape($_) } $key, $value);
279 }
280 }
281
282 return join('&', ref($data) eq 'ARRAY' ? @terms : sort @terms);
283 }
284
285 1;
This page took 0.049668 seconds and 5 git commands to generate.