X-Git-Url: https://git.dogcows.com/gitweb?p=chaz%2Fp5-HTTP-AnyUA;a=blobdiff_plain;f=lib%2FHTTP%2FAnyUA%2FMiddleware%2FContentLength.pm;fp=lib%2FHTTP%2FAnyUA%2FMiddleware%2FContentLength.pm;h=750316fb65dd8686dab9f2a1afaa1e98f6fa4746;hp=0000000000000000000000000000000000000000;hb=cde5368e2d23e07a80fa67f670afcb99093d0f77;hpb=8063c6418b41f6e09eb3783f27253502a311f5d8 diff --git a/lib/HTTP/AnyUA/Middleware/ContentLength.pm b/lib/HTTP/AnyUA/Middleware/ContentLength.pm new file mode 100644 index 0000000..750316f --- /dev/null +++ b/lib/HTTP/AnyUA/Middleware/ContentLength.pm @@ -0,0 +1,44 @@ +package HTTP::AnyUA::Middleware::ContentLength; +# ABSTRACT: Middleware to add Content-Length header automatically + +=head1 SYNOPSIS + + $any_ua->apply_middleware('ContentLength'); + +=head1 DESCRIPTION + +This middleware adds a Content-Length header to the request if the content is known (i.e. the +"content" field of the request options is a string instead of a coderef) and if the header is not +already set. + +=head1 SEE ALSO + +=for :list +* L + +=cut + +use warnings; +use strict; + +our $VERSION = '9999.999'; # VERSION + +use parent 'HTTP::AnyUA::Middleware'; + +use HTTP::AnyUA::Util; + + +sub request { + my $self = shift; + my ($method, $url, $args) = @_; + + $args->{headers} = HTTP::AnyUA::Util::normalize_headers($args->{headers}); + + if (!defined $args->{headers}{'content-length'} && $args->{content} && !ref $args->{content}) { + $args->{headers}{'content-length'} = length $args->{content}; + } + + return $self->backend->request($method, $url, $args); +} + +1;