Changeset 13398

Show
Ignore:
Timestamp:
06/07/08 20:00:14 (5 years ago)
Author:
nothingmuch
Message:

lazify uri, use URI::WithBase?, path/base are handled by uri

Location:
lang/perl/HTTP-Engine/branches/lazy_request/lib/HTTP/Engine
Files:
3 modified

Legend:

Unmodified
Added
Removed
  • lang/perl/HTTP-Engine/branches/lazy_request/lib/HTTP/Engine/Request.pm

    r13397 r13398  
    88use HTTP::Request; 
    99 
     10sub BUILD { 
     11    my ( $self, $param ) = @_; 
     12 
     13    foreach my $field qw(base path) { 
     14        if ( my $val = $param->{$field} ) { 
     15            $self->$field($val); 
     16        } 
     17    } 
     18} 
     19 
    1020has request_builder => ( 
    1121    isa => "HTTP::Engine::RequestBuilder", 
     
    4959    is      => 'rw', 
    5060    isa     => 'HashRef', 
    51     default => sub { {} }, 
    52 ); 
     61    lazy_build => 1, 
     62); 
     63 
     64sub _build_query_parameters { 
     65    my $self = shift; 
     66    $self->uri->query_form_hash; 
     67} 
    5368 
    5469# https or not? 
     
    6378    isa    => Uri, 
    6479    coerce => 1, 
    65 ); 
     80    lazy_build => 1, 
     81    handles => [qw(base path)], 
     82); 
     83 
     84sub _build_uri { 
     85    my $self = shift; 
     86 
     87    if ( my $rb = $self->request_builder ) { 
     88        $rb->_build_uri($self); 
     89    } else { 
     90        URI::WithBase->new; 
     91    } 
     92} 
    6693 
    6794has user => ( is => 'rw', ); 
     
    93120# Contains the URI base. This will always have a trailing slash. 
    94121# If your application was queried with the URI C<http://localhost:3000/some/path> then C<base> is C<http://localhost:3000/>. 
    95 has base => ( 
    96     is      => 'rw', 
    97     isa     => Uri, 
    98     trigger => sub { 
    99         my $self = shift; 
    100  
    101         if ( $self->uri ) { 
    102             $self->path; # clear cache. 
    103         } 
    104     }, 
    105 ); 
    106122 
    107123has hostname => ( 
     
    181197        $self->parameters->{$field} = [@_]; 
    182198    } 
    183 } 
    184  
    185  
    186 sub path { 
    187     my ($self, $params) = @_; 
    188  
    189     if ($params) { 
    190         $self->uri->path($params); 
    191     } else { 
    192         return $self->{path} if $self->{path}; 
    193     } 
    194  
    195     my $path     = $self->uri->path; 
    196     my $location = $self->base->path; 
    197     $path =~ s/^(\Q$location\E)?//; 
    198     $path =~ s/^\///; 
    199     $self->{path} = $path; 
    200  
    201     return $path; 
    202199} 
    203200 
     
    267264 
    268265    unless ($location =~ m!^https?://!) { 
    269         my $base = $self->base; 
    270         my $url = sprintf '%s://%s', $base->scheme, $base->host; 
    271         unless (($base->scheme eq 'http' && $base->port eq '80') || 
    272                ($base->scheme eq 'https' && $base->port eq '443')) { 
    273             $url .= ':' . $base->port; 
    274         } 
    275         $url .= $base->path; 
    276         $location = URI->new_abs($location, $url); 
    277     } 
    278     $location; 
     266        return URI->new( $location )->abs( $self->base ); 
     267    } else { 
     268        return $location; 
     269    } 
    279270} 
    280271 
  • lang/perl/HTTP-Engine/branches/lazy_request/lib/HTTP/Engine/RequestBuilder.pm

    r13397 r13398  
    3737 
    3838    # do build. 
    39     for my $method (qw( connection query_parameters path body parameters uploads )) { 
     39    for my $method (qw( connection body parameters uploads )) { 
    4040        my $method = "_prepare_$method"; 
    4141        $self->$method($context); 
     
    5555    $req->secure(1) if $ENV{HTTPS} && uc $ENV{HTTPS} eq 'ON'; 
    5656    $req->secure(1) if $ENV{SERVER_PORT} == 443; 
    57 } 
    58  
    59 sub _prepare_query_parameters  { 
    60     my($self, $c) = @_; 
    61     my $query_string = $ENV{QUERY_STRING}; 
    62     return unless  
    63         defined $query_string && length($query_string); 
    64  
    65     # replace semi-colons 
    66     $query_string =~ s/;/&/g; 
    67  
    68     my $uri = URI->new('', 'http'); 
    69     $uri->query($query_string); 
    70     for my $key ( $uri->query_param ) { 
    71         my @vals = $uri->query_param($key); 
    72         $c->req->query_parameters->{$key} = @vals > 1 ? [@vals] : $vals[0]; 
    73     } 
    7457} 
    7558 
     
    10386} 
    10487 
    105 sub _prepare_path  { 
    106     my($self, $c) = @_; 
    107  
    108     my $req    = $c->req; 
     88sub _build_uri  { 
     89    my($self, $req) = @_; 
    10990 
    11091    my $scheme = $req->secure ? 'https' : 'http'; 
     
    132113    # sanitize the URI 
    133114    $uri = $uri->canonical; 
    134     $req->uri($uri); 
    135115 
    136116    # set the base URI 
     
    139119    my $base = $uri->clone; 
    140120    $base->path_query($base_path); 
    141     $c->req->base($base); 
     121 
     122    return URI::WithBase->new($uri, $base); 
    142123} 
    143124 
     
    153134    $req->http_body( HTTP::Body->new($type, $self->read_length) ); 
    154135    $req->http_body->{tmpdir} = $self->upload_tmp if $self->upload_tmp; 
     136 
     137    $self->_read_to_end($c); 
     138} 
     139 
     140sub _read_to_end { 
     141    my ( $self, $c ) = @_; 
    155142 
    156143    if ($self->read_length > 0) { 
  • lang/perl/HTTP-Engine/branches/lazy_request/lib/HTTP/Engine/Types/Core.pm

    r13357 r13398  
    88use Class::MOP; 
    99use URI; 
     10use URI::WithBase; 
     11use URI::QueryParam; 
    1012use HTTP::Headers; 
    1113 
     
    2729}; 
    2830 
    29 class_type Uri, { class => "URI" }; 
     31class_type Uri, { class => "URI::WithBase" }; 
    3032 
    31 coerce Uri, from Str => via { URI->new($_) }; 
     33coerce Uri, from Str => via { URI::WithBase->new($_) }; 
    3234 
    3335class_type Header, { class => "HTTP::Headers" };