| 1 | # | ||
| 2 | # $Id$ | ||
| 3 | # | ||
| 4 | # string::http Brik | ||
| 5 | # | ||
| 6 | package Metabrik::String::Http; | ||
| 7 | 1 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 8 | 1 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 9 | |||
| 10 | 1 | ▶ | use base qw(Metabrik); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 11 | |||
| 12 | sub brik_properties { | ||
| 13 | return { | ||
| 14 | revision => '$Revision$', | ||
| 15 | tags => [ qw(unstable) ], | ||
| 16 | author => 'GomoR <GomoR[at]metabrik.org>', | ||
| 17 | license => 'http://opensource.org/licenses/BSD-3-Clause', | ||
| 18 | attributes => { | ||
| 19 | }, | ||
| 20 | attributes_default => { | ||
| 21 | }, | ||
| 22 | commands => { | ||
| 23 | decode => [ qw(string) ], | ||
| 24 | encode => [ qw(hash) ], | ||
| 25 | }, | ||
| 26 | require_modules => { | ||
| 27 | 'HTTP::Headers' => [ ], | ||
| 28 | 'HTTP::Request' => [ ], | ||
| 29 | 'HTTP::Response' => [ ], | ||
| 30 | }, | ||
| 31 | }; | ||
| 32 | } | ||
| 33 | |||
| 34 | sub encode { | ||
| 35 | my $self = shift; | ||
| 36 | my ($hash) = @_; | ||
| 37 | |||
| 38 | $self->brik_help_run_undef_arg('encode', $hash) or return; | ||
| 39 | |||
| 40 | my $protocol = $hash->{protocol}; | ||
| 41 | my $headers = HTTP::Headers->new(%{$hash->{headers}})->as_string; | ||
| 42 | my $content = $hash->{content}; | ||
| 43 | |||
| 44 | my @lines = (); | ||
| 45 | |||
| 46 | # Probably a response | ||
| 47 | if (exists($hash->{code})) { | ||
| 48 | my $code = $hash->{code}; | ||
| 49 | my $message = $hash->{message}; | ||
| 50 | |||
| 51 | @lines = ( "$protocol $code $message" ); | ||
| 52 | } | ||
| 53 | # Probably a request | ||
| 54 | else { | ||
| 55 | my $method = $hash->{method}; | ||
| 56 | my $uri = $hash->{uri}; | ||
| 57 | |||
| 58 | @lines = ( "$method $uri $protocol" ); | ||
| 59 | } | ||
| 60 | |||
| 61 | push @lines, $headers; | ||
| 62 | push @lines, $content; | ||
| 63 | |||
| 64 | return join("\n", @lines); | ||
| 65 | } | ||
| 66 | |||
| 67 | sub decode { | ||
| 68 | my $self = shift; | ||
| 69 | my ($string) = @_; | ||
| 70 | |||
| 71 | $self->brik_help_run_undef_arg('decode', $string) or return; | ||
| 72 | |||
| 73 | my $r; | ||
| 74 | my $h; | ||
| 75 | # Probably a response | ||
| 76 | if ($string =~ m{^HTTP/}) { | ||
| 77 | $r = HTTP::Response->parse($string); | ||
| 78 | $h = { | ||
| 79 | code => $r->code, | ||
| 80 | message => $r->message, | ||
| 81 | }; | ||
| 82 | } | ||
| 83 | # Probably a request | ||
| 84 | else { | ||
| 85 | $r = HTTP::Request->parse($string); | ||
| 86 | $h = { | ||
| 87 | method => $r->method, | ||
| 88 | uri => $r->uri->as_string, | ||
| 89 | }; | ||
| 90 | } | ||
| 91 | |||
| 92 | my @list = $r->headers->flatten; | ||
| 93 | |||
| 94 | $h->{headers} = { @list }; | ||
| 95 | $h->{protocol} = $r->protocol; | ||
| 96 | $h->{content} = $r->content; | ||
| 97 | $h->{decoded_content} = $r->decoded_content; | ||
| 98 | |||
| 99 | return $h; | ||
| 100 | } | ||
| 101 | |||
| 102 | 1; | ||
| 103 | |||
| 104 | __END__ | ||