Catalyst-Runtime

 view release on metacpan or  search on metacpan

lib/Catalyst/Request.pm  view on Meta::CPAN


    if ( my $header = $self->header('Cookie') ) {
        return { CGI::Simple::Cookie->parse($header) };
    }
    {};
}

has query_keywords => (is => 'rw');
has match => (is => 'rw');
has method => (is => 'rw');
has protocol => (is => 'rw');
has query_parameters  => (is => 'rw', lazy=>1, default => sub { shift->_use_hash_multivalue ? Hash::MultiValue->new : +{} });
has secure => (is => 'rw', default => 0);
has captures => (is => 'rw', default => sub { [] });
has uri => (is => 'rw', predicate => 'has_uri');
has remote_user => (is => 'rw');
has headers => (
  is      => 'rw',
  isa     => 'HTTP::Headers',
  handles => [qw(content_encoding content_length content_type header referer user_agent)],
  builder => 'prepare_headers',
  lazy => 1,
);

sub prepare_headers {
    my ($self) = @_;

    my $env = $self->env;
    my $headers = HTTP::Headers->new();

    for my $header (keys %{ $env }) {
        next unless $header =~ /^(HTTP|CONTENT|COOKIE)/i;
        (my $field = $header) =~ s/^HTTPS?_//;
        $field =~ tr/_/-/;
        $headers->header($field => $env->{$header});
    }
    return $headers;
}

has _log => (
    is => 'ro',
    weak_ref => 1,
    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'}
      || (
        $self->env->{'net.async.http.server.req'} &&
        $self->env->{'net.async.http.server.req'}->stream)   ## Until I can make ioasync cabal see the value of supportin psgix.io (jnap)
      || 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) = @_;

    # Not sure if these returns should not be exceptions...
    my $content_type = $self->content_type || return;
    return unless ($self->method eq 'POST' || $self->method eq 'PUT' || $self->method eq 'PATCH' || $self->method eq 'DELETE');

    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 {
      Catalyst::Exception->throw(
        sprintf '%s does not have an available data handler. Valid data_handlers are %s.',
          $content_type, join ', ', sort keys %{$self->data_handlers}
      );
    }
}

has _use_hash_multivalue => (
    is=>'ro',
    required=>1,
    default=> sub {0});

# Amount of data to read from input on each pass
our $CHUNKSIZE = 64 * 1024;

sub read {
    my ($self, $maxlength) = @_;
    my $remaining = $self->_read_length - $self->_read_position;
    $maxlength ||= $CHUNKSIZE;

    # Are we done reading?
    if ( $remaining <= 0 ) {
        return;
    }

    my $readlen = ( $remaining > $maxlength ) ? $maxlength : $remaining;
    my $rc = $self->read_chunk( my $buffer, $readlen );
    if ( defined $rc ) {
        if (0 == $rc) { # Nothing more to read even though Content-Length
                        # said there should be.
            return;
        }
        $self->_set_read_position( $self->_read_position + $rc );
        return $buffer;
    }
    else {
        Catalyst::Exception->throw(
            message => "Unknown error reading input: $!" );
    }



( run in 1.727 second using v1.01-cache-2.11-cpan-39bf76dae61 )