The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.

Name

Web::ComposableRequest::Base - Request class core attributes and methods

Synopsis

   package Web::ComposableRequest;

   use Web::ComposableRequest::Util qw( merge_attributes );
   use Unexpected::Types            qw( NonEmptySimpleStr );

   my $_build_request_class = sub {
      my $self  = shift;
      my $base  = __PACKAGE__.'::Base';
      my $conf  = $self->config_attr or return $base;
      my $attr  = {};

      merge_attributes $attr, $conf, [ 'request_class', 'request_roles' ];

      my $class = $attr->{request_class} // $base;
      my @roles = $attr->{request_roles} // [];

      @roles > 0 or return $class;

      @roles = map { (substr $_, 0, 1 eq '+')
                   ?  substr $_, 1 : __PACKAGE__."::Role::${_}" } @roles;

      return Moo::Role->create_class_with_roles( $class, @roles );
   };

   has 'request_class' => is => 'lazy', isa => NonEmptySimpleStr,
      builder          => $_build_request_class;

Description

Request class core attributes and methods

Configuration and Environment

Defines the following attributes;

address

A simple string the REMOTE_ADDR attribute from the Plack environment

base

A URI object reference that points to the application base

body

An HTTP::Body object constructed from the current request

content_length

Length in bytes of the not yet decoded body content

content_type

Mime type of the body content

host

A non empty simple string which is the hostname in the request. The value of "hostport" but without the port number

hostport

The hostname and port number in the request

method

The HTTP request method. Lower cased

path

Taken from the request path, this should be the same as the mount_point configuration attribute

port

A non zero positive integer that default to 80. The default server port

protocol

A non empty simple string. The protocol used by the request e.g. HTTP/2.0

query

The query parameters from the current request. A simple string beginning with ?

referer

The HTTP_REFERER attribute from the Plack environment

remote_host

The REMOTE_HOST attribute from the Plack environment

scheme

The HTTP protocol used in the request. Defaults to http

script

The request path

tunnel_method

The _method attribute from the body of a post or from the query parameters in the event of a get request

upload

The upload object if one was supplied in the request. Undefined otherwise

uri

The URI of the current request. Does not include the query parameters

_args

An array reference of the arguments supplied with the URI

_base

A non empty simple string which is the base of the requested URI

_config

The configuration object reference. Required

_content

A decoded string of characters representing the body of the request

_env

A hash reference, the Plack request environment

_log

The logger code reference. Defaults to the one supplied by the Plack environment

_params

A hash reference of query parameters supplied with the request URI

Subroutines/Methods

BUILD

Decodes the URI and query parameters

body_params

   $code_ref = $req->body_params; $value = $code_ref->( 'key', $opts );

Returns a code reference which when called with a body parameter name returns the body parameter value after first scrubbing it of "dodgy" characters. Throws if the value is undefined or tainted

has_upload

   $bool = $req->has_upload;

Return true if the request contains an upload, false otherwise

query_params

   $code_ref = $req->query_params; $value = $code_ref->( 'key', $opts );

Returns a code reference which when called with a query parameter name returns the query parameter value after first scrubbing it of "dodgy" characters. Throws if the value is undefined or tainted

uri_for

   $uri_obj = $req->uri_for( $partial_uri_path, $args, $query_params );

Prefixes $partial_uri_path with the base of the current request. Returns an absolute URI

uri_params

   $code_ref = $req->uri_params; $value = $code_ref->( $index, $opts );

Returns a code reference which when called with an integer index returns the uri parameter value after first scrubbing it of "dodgy" characters. Throws if the value is undefined or tainted

If the index is -1 and the option multiple is true, returns an array reference of all the uri parameters

Diagnostics

None

Dependencies

HTTP::Body
HTTP::Status

Incompatibilities

There are no known incompatibilities in this module

Bugs and Limitations

There are no known bugs in this module. Please report problems to http://rt.cpan.org/NoAuth/Bugs.html?Dist=Web-ComposableRequest. Patches are welcome

Acknowledgements

Larry Wall - For the Perl programming language

Author

Peter Flanigan, <pjfl@cpan.org>

License and Copyright

Copyright (c) 2017 Peter Flanigan. All rights reserved

This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself. See perlartistic

This program is distributed in the hope that it will be useful, but WITHOUT WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE