summary refs log tree commit diff
path: root/lib/MooseX/UserAgent/Content.pm
diff options
context:
space:
mode:
Diffstat (limited to 'lib/MooseX/UserAgent/Content.pm')
-rw-r--r--lib/MooseX/UserAgent/Content.pm26
1 files changed, 26 insertions, 0 deletions
diff --git a/lib/MooseX/UserAgent/Content.pm b/lib/MooseX/UserAgent/Content.pm
new file mode 100644
index 0000000..0cae0df
--- /dev/null
+++ b/lib/MooseX/UserAgent/Content.pm
@@ -0,0 +1,26 @@
+package MooseX::UserAgent::Content;
+
+use Encode;
+use Moose::Role;
+use Compress::Zlib;
+use HTML::Encoding 'encoding_from_http_message';
+
+sub get_content {
+    my ( $self, $res ) = @_;
+    my $enc = encoding_from_http_message($res);
+
+    my $content = $res->content;
+    if ( $res->content_encoding && $res->content_encoding eq 'gzip' ) {
+        $content = Compress::Zlib::memGunzip($content);
+    }
+
+    if ( $enc && $enc !~ /utf-8/i ) {
+        $content = $res->decoded_content( raise_error => 1 );
+        if ($@) {
+            $content = Encode::decode( $enc, $content );
+        }
+    }
+    $content;
+}
+
+1;