Browse files

Merge pull request #3 from softmoth/master

Simplify .setup() method, make it more correct
  • Loading branch information...
2 parents 0da92c5 + 41fcbd0 commit f8aec3b597faf5d112f9e6079e15945a57595781 @tadzik tadzik committed Jul 11, 2011
Showing with 28 additions and 46 deletions.
  1. +4 −6 examples/02-simple-small.pl6
  2. +1 −12 lib/HTTP/Server/Simple.pm6
  3. +23 −28 lib/HTTP/Server/Simple/PSGI.pm6
View
10 examples/02-simple-small.pl6
@@ -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";
View
13 lib/HTTP/Server/Simple.pm6
@@ -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',
@@ -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();
@@ -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 {
View
51 lib/HTTP/Server/Simple/PSGI.pm6
@@ -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;
@@ -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.

0 comments on commit f8aec3b

Please sign in to comment.