Skip to content

Commit

Permalink
Merge pull request #3 from softmoth/master
Browse files Browse the repository at this point in the history
Simplify .setup() method, make it more correct
  • Loading branch information
Tadeusz Sośnierz committed Jul 11, 2011
2 parents 0da92c5 + 41fcbd0 commit f8aec3b
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 46 deletions.
10 changes: 4 additions & 6 deletions examples/02-simple-small.pl6
Expand Up @@ -9,15 +9,13 @@ class Example::Simple::Small is HTTP::Server::Simple {
has %!header;
has $!path;
has $!query_string;
method header ( $key, $value ) {
%!header{$key} = $value;
}
method path ($path) {
method setup ( :$path, :$query_string, *%rest ) {
$!path = $path;
}
method query_string ($query_string) {
$!query_string = $query_string;
}
method header ( $key, $value ) {
%!header{$key} = $value;
}
# override the request handler of the base class
method handle_request () {
print "HTTP/1.0 200 OK\x0D\x0A\x0D\x0A";
Expand Down
13 changes: 1 addition & 12 deletions lib/HTTP/Server/Simple.pm6
Expand Up @@ -48,7 +48,6 @@ role HTTP::Server::Simple {
unless self.valid_http_method($method) { self.bad_request; }
my ( $path, $query-string ) = $uri.split('?',2);
$query-string //= ''; # // confuses P5 syntax highlighters
self.headers( self.parse_headers() );
self.setup(
:method($method), # rakudobug RT
protocol => $protocol || 'HTTP/0.9',
Expand All @@ -60,6 +59,7 @@ role HTTP::Server::Simple {
peername => 'NYI',
peeraddr => 'NYI',
);
self.headers( self.parse_headers() );
self.post_setup_hook;
my $res = self.handler;
$!connection.close();
Expand Down Expand Up @@ -89,17 +89,6 @@ role HTTP::Server::Simple {
}
method setup ( :$method, :$protocol, :$request_uri, :$path,
:$query_string, :$localport, :$peername, :$peeraddr, :$localname ) {
# The following list could probably be rewritten as a loop, but
# when that was tried it was much, much slower than doing it inline.
if self.can('method') { $!method = $method }
if self.can('protocol') { $!protocol = $protocol }
if self.can('request_uri') { $!request_uri = $request_uri }
if self.can('path') { $!path = $path }
if self.can('query_string') { $!query_string = $query_string }
if self.can('localname') { $!localname = $localname }
if self.can('localport') { $!localport = $localport }
if self.can('peername') { $!peername = $peername }
if self.can('peeraddr') { $!peeraddr = $peeraddr }
}
method headers (@headers) {
for @headers -> $key, $value {
Expand Down
51 changes: 23 additions & 28 deletions lib/HTTP/Server/Simple/PSGI.pm6
Expand Up @@ -5,21 +5,35 @@ use HTTP::Server::Simple;
class HTTP::Server::Simple::PSGI does HTTP::Server::Simple {
# The Perl 6 version inherits from H::T::Simple, not H::T::S::CGI
has $!psgi_app;
has $.localname;
has $.localport;
has $.method;
has $.protocol;
has $.request_uri;
has $.path;
has $.query_string;
has $.peeraddr;
has %!env;

method app( $app ) {
$!psgi_app = $app;
}
method setup ( :$localname, :$localport, :$method, :$request_uri,
:$path, :$query_string, :$peername, :$peeraddr, *%rest )
{
%!env = {
'SERVER_NAME' => $localname,
'SERVER_PORT' => $localport,
'REQUEST_METHOD' => $method,
'REQUEST_URI' => $request_uri,
'PATH_INFO' => $path,
'QUERY_STRING' => $query_string,
'REMOTE_NAME' => $peername,
'REMOTE_ADDR' => $peeraddr,
# required PSGI members
'psgi.version' => [1,0],
'psgi.url_scheme' => 'http',
'psgi.multithread' => Bool::False,
'psgi.multiprocess' => Bool::False,
# optional PSGI members
'psgi.runonce' => Bool::False,
'psgi.nonblocking' => Bool::False,
'psgi.streaming' => Bool::False,
};
}
method headers (@headers) {
%!env = Nil;
for @headers -> $key is copy, $value {
$key ~~ s:g /\-/_/;
$key .= uc;
Expand All @@ -37,25 +51,6 @@ class HTTP::Server::Simple::PSGI does HTTP::Server::Simple {
}
method handler { # overrides HTTP::Server::Simple
# $*ERR.say: "in PSGI.handler";
%!env{.key} = .value for (
'SERVER_NAME' => $!localname,
'SERVER_PORT' => $!localport,
'REQUEST_METHOD' => $!method,
'PATH_INFO' => $!path,
'REQUEST_URI' => $!request_uri,
'QUERY_STRING' => $!query_string,
'REMOTE_ADDR' => $!peeraddr,
# required PSGI members
'psgi.version' => [1,0],
'psgi.url_scheme' => 'http',
'psgi.multithread' => Bool::False,
'psgi.multiprocess' => Bool::False,
# optional PSGI members
'psgi.runonce' => Bool::False,
'psgi.nonblocking' => Bool::False,
'psgi.streaming' => Bool::False,
);

# Note: the handler method in HTTP::Server::Simple calls the
# handle_request method but that becomes psgi_app() over here.
# Instead it calls handle_response later on.
Expand Down

0 comments on commit f8aec3b

Please sign in to comment.