From ce39d07593b33d11ce990144e2c6155e4f302988 Mon Sep 17 00:00:00 2001 From: Jesse Luehrs Date: Tue, 17 Jul 2012 16:09:10 -0500 Subject: sketch out the api here --- lib/Web/Request.pm | 191 ++++++++++++++++++++++++++++++++++++++++++++++ lib/Web/Request/Upload.pm | 34 +++++++++ lib/Web/Response.pm | 51 +++++++++++++ 3 files changed, 276 insertions(+) create mode 100644 lib/Web/Request/Upload.pm create mode 100644 lib/Web/Response.pm diff --git a/lib/Web/Request.pm b/lib/Web/Request.pm index e69de29..c4237db 100644 --- a/lib/Web/Request.pm +++ b/lib/Web/Request.pm @@ -0,0 +1,191 @@ +package Web::Request; +use Moose; + +use HTTP::Headers; +use URI; + +has env => ( + traits => ['Hash'], + is => 'ro', + isa => 'HashRef', + lazy => 1, + default => sub { + confess "Can't get the env if it wasn't provided during construction"; + }, + handles => { + address => 'REMOTE_ADDR', + remote_host => 'REMOTE_HOST', + protocol => 'SERVER_PROTOCOL', + method => 'REQUEST_METHOD', + port => 'SERVER_PORT', + user => 'REMOTE_USER', + request_uri => 'REQUEST_URI', + path_info => 'PATH_INFO', + script_name => 'SCRIPT_NAME', + scheme => 'psgi.url_scheme', + _input => 'psgi.input', + content_length => 'CONTENT_LENGTH', + content_type => 'CONTENT_TYPE', + session => 'psgix.session', + session_options => 'psgix.session.options', + logger => 'psgix.logger', + }, +); + +has uri => ( + is => 'ro', + isa => 'URI', + lazy => 1, + default => sub { + ... + }, +); + +has headers => ( + is => 'ro', + isa => 'HTTP::Headers', + lazy => 1, + default => sub { + ... + }, + handles => ['header', 'content_encoding', 'referer', 'user_agent'], +); + +has cookies => ( + is => 'ro', + isa => 'HashRef', + lazy => 1, + default => sub { + ... + }, +); + +has content => ( + is => 'ro', + isa => 'Str', + lazy => 1, + default => sub { + ... + }, +); + +has query_parameters => ( + is => 'ro', + isa => 'HashRef[Str]', + lazy => 1, + default => sub { +{ shift->uri->query_form } }, +); + +has all_query_parameters => ( + is => 'ro', + isa => 'HashRef[ArrayRef[Str]]', + lazy => 1, + default => sub { + my $self = shift; + + my @params = $self->uri->query_form; + my $it = natatime 2, @params; + my $ret = {}; + + while (my ($k, $v) = $it->()) { + push @{ $ret->{$k} ||= [] }, $v; + } + + return $ret; + }, +); + +has body_parameters => ( + is => 'ro', + isa => 'HashRef[Str]', + lazy => 1, + default => sub { + ... + }, +); + +has all_body_parameters => ( + is => 'ro', + isa => 'HashRef[ArrayRef[Str]]', + lazy => 1, + default => sub { + ... + }, +); + +has uploads => ( + is => 'ro', + isa => 'ArrayRef[Web::Request::Upload]', + lazy => 1, + default => sub { + ... + }, +); + +sub new_from_env { + my $class = shift; + my ($env) = @_; + + return $class->new(env => $env); +} + +sub new_from_request { + my $class = shift; + my ($req) = @_; + + return $class->new_from_env(req_to_psgi($req)); +} + +sub response_class { 'Web::Response' } + +sub path { + my $self = shift; + + my $path = $self->path_info; + return $path if length($path); + return '/'; +} + +sub uri_base { + ... +} + +sub new_response { + my $self = shift; + + $self->response_class->new(@_); +} + +sub parameters { + my $self = shift; + + return { + %{ $self->query_parameters }, + %{ $self->body_parameters }, + }; +} + +sub all_parameters { + my $self = shift; + + my $ret = { %{ $self->all_query_parameters } }; + my $body_parameters = $self->all_body_parameters; + + for my $key (keys %$body_parameters) { + push @{ $ret->{$key} ||= [] }, @{ $body_parameters->{key} }; + } + + return $ret; +} + +sub param { + my $self = shift; + my ($key) = @_; + + $self->parameters->{$key}; +} + +__PACKAGE__->meta->make_immutable; +no Moose; + +1; diff --git a/lib/Web/Request/Upload.pm b/lib/Web/Request/Upload.pm new file mode 100644 index 0000000..ff3592c --- /dev/null +++ b/lib/Web/Request/Upload.pm @@ -0,0 +1,34 @@ +package Web::Request::Upload; +use Moose; + +use HTTP::Headers; + +has headers => ( + is => 'ro', + isa => 'HTTP::Headers', + handles => ['content_type'], +); + +has tempname => ( + is => 'ro', + isa => 'Str', +); + +has size => ( + is => 'ro', + isa => 'Int', +); + +has filename => ( + is => 'ro', + isa => 'Str', +); + +sub basename { + ... +} + +__PACKAGE__->meta->make_immutable; +no Moose; + +1; diff --git a/lib/Web/Response.pm b/lib/Web/Response.pm new file mode 100644 index 0000000..e7b96c9 --- /dev/null +++ b/lib/Web/Response.pm @@ -0,0 +1,51 @@ +package Web::Response; +use Moose; + +use HTTP::Headers; + +has status => ( + is => 'rw', + isa => 'Int', # XXX restrict to /^[1-5][0-9][0-9]$/ + lazy => 1, + default => sub { confess "Status was not supplied" }, +); + +has headers => ( + is => 'rw', + isa => 'HTTP::Headers', # XXX coerce from array/hashref + lazy => 1, + default => sub { HTTP::Headers->new }, + handles => { + header => 'header', + content_length => 'content_length', + content_type => 'content_type', + content_encoding => 'content_encoding', + location => [ header => 'Location' ], + }, +); + +has body => ( + is => 'rw', + lazy => 1, + default => sub { [] }, +); + +has cookies => ( + is => 'ro', + isa => 'HashRef', + lazy => 1, + default => sub { +{} }, +); + +sub redirect { + ... +} + +sub finalize { + ... +} + +__PACKAGE__->meta->make_immutable; +no Moose; + +1; -- cgit v1.2.3-54-g00ecf