Permalink
Browse files

Merge pull request #2 from softmoth/master

Add HTTP_* headers into %env
  • Loading branch information...
2 parents d2970e5 + 46ea15a commit 0da92c5eaa238520102d5ac4e8e4b5501d1b5ade @tadzik tadzik committed Jul 11, 2011
Showing with 44 additions and 19 deletions.
  1. +2 −2 examples/05-psgi-small.pl6
  2. +5 −9 lib/HTTP/Server/Simple.pm6
  3. +37 −8 lib/HTTP/Server/Simple/PSGI.pm6
@@ -3,11 +3,11 @@ use HTTP::Server::Simple::PSGI;
my $host = 'localhost';
my $port = 8080;
-my $app = sub ($env) {
+my $app = sub (%env) {
return [
'200',
[ 'Content-Type' => 'text/plain' ],
- [ "Hello", "World\n\n", $env.perl ]
+ [ "Hello", "World\n\n", %env.perl ]
];
}
View
@@ -46,15 +46,15 @@ role HTTP::Server::Simple {
$!request = shift @!headers;
my ($method, $uri, $protocol) = self.parse_request;
unless self.valid_http_method($method) { self.bad_request; }
- my ( $file, $query-string ) = $uri.split('?',2);
+ 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',
query_string => $query-string,
request_uri => $uri,
- path => $file,
+ path => $path,
localname => $!host,
localport => $!port,
peername => 'NYI',
@@ -96,10 +96,10 @@ role HTTP::Server::Simple {
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 }
- if self.can('localname') { $!localname = $localname }
}
method headers (@headers) {
for @headers -> $key, $value {
@@ -132,17 +132,13 @@ role HTTP::Server::Simple {
$!request.split( /\s/ );
}
method parse_headers () {
- my $result = [];
- for @!headers -> $line {
+ return gather for @!headers -> $line {
my ( $key, $value ) = $line.split( ': ' );
if defined($key) and defined($value) {
# $*ERR.say: "parse_headers $key => $value";
- # $result.push: $key, $value;
- $result.push: $key;
- $result.push: $value;
+ take $key, $value;
}
}
- return $result;
}
method setup_listener () {
# say "setup listener on port $!port";
@@ -5,18 +5,46 @@ 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 $.request_uri is rw;
+ 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 headers (@headers) {
+ %!env = Nil;
+ for @headers -> $key is copy, $value {
+ $key ~~ s:g /\-/_/;
+ $key .= uc;
+
+ $key = 'HTTP_' ~ $key unless $key eq any(<CONTENT_LENGTH CONTENT_TYPE>);
+ # RAKUDO: :exists doesn't exist yet
+ if %!env.exists($key) {
+ # This is how P5 Plack::HTTPParser::PP handles this
+ %!env{$key} ~= ", $value";
+ }
+ else {
+ %!env{$key} = $value;
+ }
+ }
+ }
method handler { # overrides HTTP::Server::Simple
# $*ERR.say: "in PSGI.handler";
- my $env = {
- 'REQUEST_METHOD' => 'GET',
- 'PATH_INFO' => '',
+ %!env{.key} = .value for (
+ 'SERVER_NAME' => $!localname,
+ 'SERVER_PORT' => $!localport,
+ 'REQUEST_METHOD' => $!method,
+ 'PATH_INFO' => $!path,
'REQUEST_URI' => $!request_uri,
- 'QUERY_STRING' => '',
+ 'QUERY_STRING' => $!query_string,
+ 'REMOTE_ADDR' => $!peeraddr,
# required PSGI members
'psgi.version' => [1,0],
'psgi.url_scheme' => 'http',
@@ -26,12 +54,13 @@ class HTTP::Server::Simple::PSGI does HTTP::Server::Simple {
'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.
my $response_ref = defined($!psgi_app)
- ?? $!psgi_app($env) # app must return [status,[headers],[body]]
+ ?? $!psgi_app(%!env) # app must return [status,[headers],[body]]
!! [500,[Content-Type => 'text/plain'],[self.WHAT,"app missing"]];
my $status = $response_ref[0];
my @headers = $response_ref[1];
@@ -57,7 +86,7 @@ HTTP::Server::Simple::PSGI - Perl Server Gateway Interface web server
my $host = 'localhost';
my port = 80;
- my $app = sub($env) {
+ my $app = sub(%env) {
return [
'200',
[ 'Content-Type' => 'text/plain' ],

0 comments on commit 0da92c5

Please sign in to comment.