]> Dogcows Code - chaz/p5-HTTP-AnyUA/blob - lib/HTTP/AnyUA/Middleware/ContentLength.pm
add middleware
[chaz/p5-HTTP-AnyUA] / lib / HTTP / AnyUA / Middleware / ContentLength.pm
1 package HTTP::AnyUA::Middleware::ContentLength;
2 # ABSTRACT: Middleware to add Content-Length header automatically
3
4 =head1 SYNOPSIS
5
6 $any_ua->apply_middleware('ContentLength');
7
8 =head1 DESCRIPTION
9
10 This middleware adds a Content-Length header to the request if the content is known (i.e. the
11 "content" field of the request options is a string instead of a coderef) and if the header is not
12 already set.
13
14 =head1 SEE ALSO
15
16 =for :list
17 * L<HTTP::AnyUA::Middleware>
18
19 =cut
20
21 use warnings;
22 use strict;
23
24 our $VERSION = '9999.999'; # VERSION
25
26 use parent 'HTTP::AnyUA::Middleware';
27
28 use HTTP::AnyUA::Util;
29
30
31 sub request {
32 my $self = shift;
33 my ($method, $url, $args) = @_;
34
35 $args->{headers} = HTTP::AnyUA::Util::normalize_headers($args->{headers});
36
37 if (!defined $args->{headers}{'content-length'} && $args->{content} && !ref $args->{content}) {
38 $args->{headers}{'content-length'} = length $args->{content};
39 }
40
41 return $self->backend->request($method, $url, $args);
42 }
43
44 1;
This page took 0.032281 seconds and 4 git commands to generate.