diff options
author | franck cuny <franck@lumberjaph.net> | 2010-09-15 16:44:57 +0200 |
---|---|---|
committer | franck cuny <franck@lumberjaph.net> | 2010-09-15 16:44:57 +0200 |
commit | 1278125ebaeb8f4f94685176d641f53dbe7855ff (patch) | |
tree | a935921b9ef5143261b98b0c53ae41bdbd3e1f95 | |
parent | update tests (diff) | |
download | net-http-spore-1278125ebaeb8f4f94685176d641f53dbe7855ff.tar.gz |
POD
-rw-r--r-- | lib/Net/HTTP/Spore/Request.pm | 119 | ||||
-rw-r--r-- | lib/Net/HTTP/Spore/Response.pm | 144 |
2 files changed, 250 insertions, 13 deletions
diff --git a/lib/Net/HTTP/Spore/Request.pm b/lib/Net/HTTP/Spore/Request.pm index 908cce7..267ec0b 100644 --- a/lib/Net/HTTP/Spore/Request.pm +++ b/lib/Net/HTTP/Spore/Request.pm @@ -1,8 +1,11 @@ package Net::HTTP::Spore::Request; +# ABSTRACT: Net::HTTP::Spore::Request - Portable HTTP request object from SPORE env hash + use strict; use warnings; +use Carp (); use URI; use HTTP::Headers; use HTTP::Request; @@ -13,18 +16,23 @@ use Net::HTTP::Spore::Response; sub new { my ( $class, $env ) = @_; + + Carp::croak('$env is required') unless defined $env && ref($env) eq 'HASH'; bless { env => $env }, $class; } -sub env { $_[0]->{env}; } +sub env { $_[0]->{env} } sub method { $_[0]->{env}->{REQUEST_METHOD} } sub port { $_[0]->{env}->{SERVER_PORT} } sub script_name { $_[0]->{env}->{SCRIPT_NAME} } -sub path { $_[0]->{env}->{PATH_INFO} || '/' } +sub path { $_[0]->path_info } sub request_uri { $_[0]->{env}->{REQUEST_URI} } -sub protocol { $_[0]->{env}->{SERVER_PROTOCOL} } -sub content { $_[0]->{env}->{'spore.payload'} } sub scheme { $_[0]->{env}->{'spore.scheme'} } +sub logger { $_[0]->{env}->{'sporex.logger'} } +sub secure { $_[0]->scheme eq 'https' } +sub content { $_[0]->{env}->{'spore.payload'} } +sub body { $_[0]->{env}->{'spore.payload'} } +sub input { $_[0]->{env}->{'spore.payload'} } sub path_info { my $self = shift; @@ -97,7 +105,7 @@ sub uri { my $path = URI::Escape::uri_escape($path_info || '', $path_escape_class); - if ($query_string) { + if (defined $query_string) { $path .= '?' . $query_string; } @@ -105,6 +113,7 @@ sub uri { return URI->new( $base . $path )->canonical; } +# retourner les query parameters ? vu qu'on a pas encore peuple l'url, on gere comment ? sub query_parameters { my $self = shift; } @@ -153,3 +162,103 @@ sub finalize { } 1; + +__END__ + +=head1 SYNOPSIS + + use Net::HTTP::Spore::Request; + + my $request = Net::HTTP::Spore::Request->new($env); + +=head1 DESCRIPTION + +Net::HTTP::Spore::Request create a HTTP request + +=head1 METHODS + +=over 4 + +=item new + + my $req = Net::HTTP::Spore::Request->new(); + +Creates a new Net::HTTP::Spore::Request object. + +=item env + + my $env = $request->env; + +Get the environment for the given request + +=item method + + my $method = $request->method; + +Get the HTTP method for the given request + +=item port + + my $port = $request->port; + +Get the HTTP port from the URL + +=item script_name + + my $script_name = $request->script_name; + +Get the script name part from the URL + +=item path + +=item path_info + + my $path = $request->path_info; + +Get the path info part from the URL + +=item request_uri + + my $request_uri = $request->request_uri; + +Get the request uri from the URL + +=item scheme + + my $scheme = $request->scheme; + +Get the scheme from the URL + +=item secure + + my $secure = $request->secure; + +Return true if the URL is HTTPS + +=item content + +=item body + +=item input + + my $input = $request->input; + +Get the content that will be posted + +=item query_string + +=item headers + +=item header + +=item uri + +=item query_parameters + +=item base + +=item new_response + +=item finalize + +=back diff --git a/lib/Net/HTTP/Spore/Response.pm b/lib/Net/HTTP/Spore/Response.pm index d695dfa..4667e08 100644 --- a/lib/Net/HTTP/Spore/Response.pm +++ b/lib/Net/HTTP/Spore/Response.pm @@ -1,5 +1,7 @@ package Net::HTTP::Spore::Response; +# ABSTRACT: Portable HTTP Response object for SPORE response + use strict; use warnings; @@ -11,20 +13,19 @@ sub new { my ( $class, $rc, $headers, $body ) = @_; my $self = bless {}, $class; + $self->status($rc) if defined $rc; - if (defined $body) { - $self->body($body); - $self->raw_body($body); - } - $self->headers($headers || []); + $self->body($body) if defined $body; + $self->headers( $headers || [] ); $self; } -sub code { shift->status(@_) } -sub content { shift->body(@_) } - +sub code { shift->status(@_) } +sub content { shift->body(@_) } +sub env { shift->request->env } sub content_type { shift->headers->content_type(@_) } sub content_length { shift->headers->content_length(@_) } +sub location { shift->header->header( 'Location' => @_ ) } sub status { my $self = shift; @@ -40,6 +41,9 @@ sub body { my $self = shift; if (@_) { $self->{body} = shift; + if ( !defined $self->{raw_body} ) { + $self->{raw_body} = $self->{body}; + } } else { return $self->{body}; @@ -101,3 +105,127 @@ sub finalize { } 1; +__END__ + +=head1 SYNOPSIS + + use Net:HTTP::Spore::Response; + + my $response = Net::HTTP::Spore::Response->new( + 200, ['Content-Type', 'application/json'], '{"foo":1}'; + ); + $response->request($request); + +=head1 DESCRIPTION + +Net::HTTP::Spore::Response create a HTTP response + +=head1 METHODS + +=over 4 + +=item new + + my $res = Net::HTTP::Spore::Response->new; + my $res = Net::HTTP::Spore::Response->new($status); + my $res = Net::HTTP::Spore::Response->new($status, $headers); + my $res = Net::HTTP::Spore::Response->new($status, $headers, $body); + +Creates a new Net::HTTP::Spore::Response object. + +=item code + +=item status + + $res->status(200); + my $status = $res->status; + +Gets or sets the HTTP status of the response + +=item env + + $res->env($env); + my $env = $res->env; + +Gets or sets the environment for the response. Shortcut to C<< $res->request->env >> + +=item content + +=item body + + $res->body($body); + my $body = $res->body; + +Gets or sets the body for the response + +=item raw_body + + my $raw_body = $res->raw_body + +The raw_body value is the same as body when the body is sets for the first time. + +=item content_type + + $res->content_type('application/json'); + my $ct = $res->content_type; + +Gets or sets the content type of the response body + +=item content_length + + $res->content_length(length($body)); + my $cl = $res->content_length; + +Gets or sets the content type of the response body + +=item location + + $res->location('http://example.com'); + my $location = $res->location; + +Gets or sets the location header for the response + +=item request + + $res->request($request); + $request = $res->request; + +Gets or sets the HTTP request that created the current HTTP response. + +=item headers + + $headers = $res->headers; + $res->headers(['Content-Type' => 'application/json']); + +Gets or sets HTTP response headers. + +=item header + + my $cl = $res->header('Content-Length'); + $res->header('Content-Type' => 'application/json'); + +Shortcut for C<< $res->headers->header >>. + +=item finalise + + my $res = Net::HTTP::Response->new($status, $headers, $body); + say "http status is ".$res->[0]; + +Return an arrayref: + +=over 2 + +=item status + +The first element of the array ref is the HTTP status + +=item headers + +The second element is an arrayref containing the list of HTTP headers + +=item body + +The third and final element is the body + +=back + |