Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
tree: 7e7f1d41db
Fetching contributors…

Cannot retrieve contributors at this time

file 59 lines (51 sloc) 1.267 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
## HTTP::Easy::PSGI
## A PSGI application HTTP Server

use HTTP::Easy;

class HTTP::Easy::PSGI does HTTP::Easy;

use HTTP::Status;

constant $CRLF = "\x0D\x0A";

has $!app;

method app ($app)
{
  $!app = $app;
}

method handler
{
  ## First, let's add any necessary PSGI variables.
  %.env<psgi.version> = [1,0];
  %.env<psgi.url_scheme> = 'http'; ## TODO: detect this.
  %.env<psgi.multithread> = False;
  %.env<psgi.multiprocess> = False;
  %.env<psgi.input> = $.body;
  %.env<psgi.errors> = $*ERR; ## TODO: allow override on this.
  %.env<psgi.run_once> = False;
  %.env<psgi.nonblocking> = False; ## Allow when NBIO.
  %.env<psgi.streaming> = False; ## Eventually?
  my $result;
  if $!app ~~ Callable
  {
    $result = $!app(%.env);
  }
  elsif $!app.can('handle')
  {
    $result = $!app.handle(%.env);
  }
  else
  {
    die "Invalid {self.WHAT} application.";
  }
  my $message = get_http_status_msg($result[0]);
  my $output = $.http-protocol ~ ' ' ~ $result[0] ~ " $message$CRLF";
  for @($result[1]) -> $header
  {
    $output ~= $header.key ~ ': ' ~ $header.value ~ $CRLF;
  }
  my $body = $result[2].join;
  $output ~= $CRLF ~ $body;
  return $output;
}

method handle ($app)
{
  self.app($app);
  return self.run;
}
Something went wrong with that request. Please try again.