1 package HTTP
::AnyUA
::Backend
::AnyEvent
::HTTP
;
2 # ABSTRACT: A unified programming interface for AnyEvent::HTTP
8 our $VERSION = '0.900'; # VERSION
10 use parent
'HTTP::AnyUA::Backend';
13 use HTTP
::AnyUA
::Util
;
17 sub options
{ @_ == 2 ? $_[0]->{options
} = pop : $_[0]->{options
} }
19 sub response_is_future
{ 1 }
23 my ($method, $url, $args) = @_;
25 my %opts = $self->_munge_request($method, $url, $args);
26 my $future = Future-
>new;
28 require AnyEvent
::HTTP
;
29 AnyEvent
::HTTP
::http_request
($method => $url, %opts, sub {
30 my $resp = $self->_munge_response(@_, $args->{data_callback
});
32 if ($resp->{success
}) {
48 my $args = shift || {};
50 my %opts = %{$self->options || {}};
52 if (my $headers = $args->{headers
}) {
55 for my $header (keys %$headers) {
56 my $value = $headers->{$header};
57 $value = join(', ', @$value) if ref($value) eq 'ARRAY';
58 $headers{$header} = $value;
60 $opts{headers
} = \
%headers;
63 my @url_parts = HTTP
::AnyUA
::Util
::split_url
($url);
64 if (my $auth = $url_parts[4] and !$opts{headers
}{'authorization'}) {
65 # handle auth in the URL
67 $opts{headers
}{'authorization'} = 'Basic ' . MIME
::Base64
::encode_base64
($auth, '');
70 my $content = HTTP
::AnyUA
::Util
::coderef_content_to_string
($args->{content
});
71 $opts{body
} = $content if $content;
73 if (my $data_cb = $args->{data_callback
}) {
75 $opts{on_body
} = sub {
77 $data_cb->($data, $self->_munge_response(undef, @_));
91 # copy headers because http_request will continue to use the original
92 my %headers = %$headers;
94 my $code = delete $headers{Status
};
95 my $reason = delete $headers{Reason
};
96 my $url = delete $headers{URL
};
99 success
=> 200 <= $code && $code <= 299,
103 headers
=> \
%headers,
106 my $version = delete $headers{HTTPVersion
};
107 $resp->{protocol
} = "HTTP/$version" if $version;
109 $resp->{content
} = $data if $data && !$data_cb;
112 my $redirect = delete $headers{Redirect
};
114 # delete pseudo-header first so redirects aren't recursively munged
115 my $next = delete $redirect->[1]{Redirect
};
116 unshift @redirects, $self->_munge_response(@$redirect);
119 $resp->{redirects
} = \
@redirects if @redirects;
121 if (590 <= $code && $code <= 599) {
122 HTTP
::AnyUA
::Util
::internal_exception
($reason, $resp);
138 HTTP::AnyUA::Backend::AnyEvent::HTTP - A unified programming interface for AnyEvent::HTTP
146 This module adds support for the HTTP client L<AnyEvent::HTTP> to be used with the unified
147 programming interface provided by L<HTTP::AnyUA>.
153 $backend->options(\%options);
155 Get and set default arguments to C<http_request>.
163 L<HTTP::AnyUA::Backend>
169 Please report any bugs or feature requests on the bugtracker website
170 L<https://github.com/chazmcgarvey/HTTP-AnyUA/issues>
172 When submitting a bug or request, please include a test-file or a
173 patch to an existing test-file that illustrates the bug or desired
178 Charles McGarvey <chazmcgarvey@brokenzipper.com>
180 =head1 COPYRIGHT AND LICENSE
182 This software is copyright (c) 2017 by Charles McGarvey.
184 This is free software; you can redistribute it and/or modify it under
185 the same terms as the Perl 5 programming language system itself.