X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=lib%2FCatalyst%2FRequest.pm;h=de9f15f51f99a9ba981502ff717faef9722347f9;hb=952ff53094bcabafb62064922bf33f5a0269450f;hp=3d0c03ae3c4919be0c53dca7b96f7633e0b0681e;hpb=d003ff83ac25ab0af3988de66867f73af54ff631;p=catagits%2FCatalyst-Runtime.git diff --git a/lib/Catalyst/Request.pm b/lib/Catalyst/Request.pm index 3d0c03a..de9f15f 100644 --- a/lib/Catalyst/Request.pm +++ b/lib/Catalyst/Request.pm @@ -14,7 +14,7 @@ use namespace::clean -except => 'meta'; with 'MooseX::Emulate::Class::Accessor::Fast'; -has env => (is => 'ro', writer => '_set_env'); +has env => (is => 'ro', writer => '_set_env', predicate => 'has_env'); # XXX Deprecated crap here - warn? has action => (is => 'rw'); # XXX: Deprecated in docs ages ago (2006), deprecated with warning in 5.8000 due @@ -23,13 +23,15 @@ has user => (is => 'rw'); sub snippets { shift->captures(@_) } has _read_position => ( - init_arg => undef, + # FIXME: work around Moose bug RT#75367 + # init_arg => undef, is => 'ro', writer => '_set_read_position', default => 0, ); has _read_length => ( - init_arg => undef, + # FIXME: work around Moose bug RT#75367 + # init_arg => undef, is => 'ro', default => sub { my $self = shift; @@ -89,6 +91,40 @@ has _log => ( required => 1, ); +has io_fh => ( + is=>'ro', + predicate=>'has_io_fh', + lazy=>1, + builder=>'_build_io_fh'); + +sub _build_io_fh { + my $self = shift; + return $self->env->{'psgix.io'} + || die "Your Server does not support psgix.io"; +}; + +has data_handlers => ( is=>'ro', isa=>'HashRef', default=>sub { +{} } ); + +has body_data => ( + is=>'ro', + lazy=>1, + builder=>'_build_body_data'); + +sub _build_body_data { + my ($self) = @_; + my $content_type = $self->content_type; + my ($match) = grep { $content_type =~/$_/i } + keys(%{$self->data_handlers}); + + if($match) { + my $fh = $self->body; + local $_ = $fh; + return $self->data_handlers->{$match}->($fh, $self); + } else { + return undef; + } +} + # Amount of data to read from input on each pass our $CHUNKSIZE = 64 * 1024; @@ -139,7 +175,8 @@ has uploads => ( has parameters => ( is => 'rw', lazy => 1, - builder => 'prepare_parameters', + builder => '_build_parameters', + clearer => '_clear_parameters', ); # TODO: @@ -152,6 +189,12 @@ has parameters => ( sub prepare_parameters { my ( $self ) = @_; + $self->_clear_parameters; + return $self->parameters; +} + +sub _build_parameters { + my ( $self ) = @_; my $parameters = {}; my $body_parameters = $self->body_parameters; my $query_parameters = $self->query_parameters; @@ -181,6 +224,18 @@ has _uploadtmp => ( sub prepare_body { my ( $self ) = @_; + #warn "XXX ${\$self->_uploadtmp}" if $self->_has_uploadtmp; + + if(my $plack_body = $self->env->{'plack.request.http.body'}) { + warn "wtF" x 100; + $self->_body($plack_body); + $self->_body->cleanup(1); # Make extra sure! + $self->_body->tmpdir( $self->_uploadtmp ) + if $self->_has_uploadtmp; + } else { + + } + if ( my $length = $self->_read_length ) { unless ( $self->_body ) { my $type = $self->header('Content-Type'); @@ -208,6 +263,59 @@ sub prepare_body { } } +sub prepare_bodyXXX { + my ( $self ) = @_; + if(my $plack_body = $self->env->{'plack.request.http.body'}) { + + + } else { + + } + + die "XXX ${\$self->_uploadtmp}" x1000; $self->_has_uploadtmp; + + if ( my $length = $self->_read_length ) { + unless ( $self->_body ) { + + ## If something plack middle already ready the body, just use + ## that. + + my $body; + if(my $plack_body = $self->env->{'plack.request.http.body'}) { + $body = $plack_body; + } else { + my $type = $self->header('Content-Type'); + $body = HTTP::Body->new($type, $length); + + ## Play nice with Plak Middleware that looks for a body + $self->env->{'plack.request.http.body'} = $body; + $self->_body($body); + + $body->cleanup(1); # Make extra sure! + $body->tmpdir( $self->_uploadtmp ) + if $self->_has_uploadtmp; + } + } + + # Check for definedness as you could read '0' + while ( defined ( my $buffer = $self->read() ) ) { + $self->prepare_body_chunk($buffer); + } + + # paranoia against wrong Content-Length header + my $remaining = $length - $self->_read_position; + if ( $remaining > 0 ) { + Catalyst::Exception->throw( + "Wrong Content-Length value: $length" ); + } + } + else { + # Defined but will cause all body code to be skipped + $self->_body(0); + } +} + + sub prepare_body_chunk { my ( $self, $chunk ) = @_; @@ -218,7 +326,7 @@ sub prepare_body_parameters { my ( $self ) = @_; $self->prepare_body if ! $self->_has_body; - return unless $self->_body; + return {} unless $self->_body; return $self->_body->param; } @@ -270,7 +378,7 @@ has _body => ( # and provide a custom reader.. sub body { my $self = shift; - $self->prepare_body unless ! $self->_has_body; + $self->prepare_body unless $self->_has_body; croak 'body is a reader' if scalar @_; return blessed $self->_body ? $self->_body->body : $self->_body; } @@ -308,6 +416,7 @@ Catalyst::Request - provides information about the current client request $req->args; $req->base; $req->body; + $req->body_data; $req->body_parameters; $req->content_encoding; $req->content_length; @@ -390,6 +499,14 @@ Returns the message body of the request, as returned by L: a string, unless Content-Type is C, C, or C, in which case a L object is returned. +=head2 $req->body_data + +Returns a Perl representation of POST/PUT body data that is not classic HTML +form data, such as JSON, XML, etc. By default, Catalyst will parse incoming +data of the type 'application/json' and return access to that data via this +method. You may define addition data_handlers via a global configuration +setting. See L for more information. + =head2 $req->body_parameters Returns a reference to a hash containing body (POST) parameters. Values can @@ -629,7 +746,7 @@ defaults to the size of the request if not specified. =head2 $req->read_chunk(\$buff, $max) -Reads a chunk.. +Reads a chunk. You have to set MyApp->config(parse_on_demand => 1) to use this directly. @@ -640,11 +757,12 @@ Shortcut for $req->headers->referer. Returns the referring page. =head2 $req->secure Returns true or false, indicating whether the connection is secure -(https). Note that the URI scheme (e.g., http vs. https) must be determined -through heuristics, and therefore the reliability of $req->secure will depend -on your server configuration. If you are serving secure pages on the standard -SSL port (443) and/or setting the HTTPS environment variable, $req->secure -should be valid. +(https). The reliability of $req->secure may depend on your server +configuration; Catalyst relies on PSGI to determine whether or not a +request is secure (Catalyst looks at psgi.url_scheme), and different +PSGI servers may make this determination in different ways (as by +directly passing along information from the server, interpreting any of +several HTTP headers, or using heuristics of their own). =head2 $req->captures @@ -834,6 +952,12 @@ Returns the value of the C environment variable. Shortcut to $req->headers->user_agent. Returns the user agent (browser) version string. +=head2 $req->io_fh + +Returns a psgix.io bidirectional socket, if your server supports one. Used for +when you want to jailbreak out of PSGI and handle bidirectional client server +communication manually, such as when you are using cometd or websockets. + =head1 SETUP METHODS You should never need to call these yourself in application code, @@ -869,7 +993,8 @@ request method, hostname requested etc. Ensures that the body has been parsed, then builds the parameters, which are combined from those in the request and those in the body. -This method is the builder for the 'parameters' attribute. +If parameters have already been set will clear the parameters and build them again. + =head2 meta