]> Dogcows Code - chaz/p5-HTTP-AnyUA/blobdiff - lib/HTTP/AnyUA/Middleware/ContentLength.pm
add middleware
[chaz/p5-HTTP-AnyUA] / lib / HTTP / AnyUA / Middleware / ContentLength.pm
diff --git a/lib/HTTP/AnyUA/Middleware/ContentLength.pm b/lib/HTTP/AnyUA/Middleware/ContentLength.pm
new file mode 100644 (file)
index 0000000..750316f
--- /dev/null
@@ -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<HTTP::AnyUA::Middleware>
+
+=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;
This page took 0.017631 seconds and 4 git commands to generate.