File: httpd

package info (click to toggle)
libalien-build-perl 2.84-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 3,116 kB
  • sloc: perl: 10,350; ansic: 134; sh: 66; makefile: 2
file content (110 lines) | stat: -rwxr-xr-x 2,605 bytes parent folder | download
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
#!/usr/bin/env perl

use strict;
use warnings;
use Path::Tiny qw( path );
use Getopt::Long qw( GetOptions );
use URI;
use URI::Escape qw( uri_unescape );
use JSON::PP qw( encode_json decode_json );
use HTTP::Server::PSGI;
use Plack::Builder;
use Plack::App::Directory;


my $daemon = 0;
my $kill   = 0;
my $host   = 'localhost';

GetOptions(
  "d"      => \$daemon,
  "k"      => \$kill,
  "host=s" => \$host,
);

my $bindir    = path(__FILE__)->parent->absolute;
my $distdir   = $bindir->parent->parent;

my $config_file = $bindir->child('httpd.json');

if(-r $config_file)
{
  my $config = decode_json($config_file->slurp);
  my $pid = $config->{pid};
  if(defined $pid)
  {
    kill 'KILL', $pid;
  }
}

exit if $kill;

if($daemon)
{
  require Proc::Daemon;
  my $daemon = Proc::Daemon->new(
    child_STDOUT => $bindir->child('httpd.log')->stringify,
    child_STDERR => $bindir->child('httpd.log')->stringify,
  );
  $daemon->Init;
}

my $url = URI->new('http://localhost/corpus/dist/');
$url->host($host);
$url->port(do {
  require IO::Socket::INET;
  IO::Socket::INET->new(Listen => 5, LocalAddr => "127.0.0.1")->sockport;
});

my %config = (
  root => $distdir->child('corpus/dist')->stringify,
  pid  => $$,
  url => $url->as_string,
);
$config_file->spew(encode_json(\%config));

my $app = builder {
  mount '/corpus/dist/test1/' => sub {
    my $env = shift;
    my %headers;
    foreach my $key (keys %$env)
    {
      next unless $key =~ /^HTTP_(.*)$/;
      my $name = join '-', map { ucfirst $_ }map { lc $_ } split /_/, $1;
      $headers{$name} = $env->{$key};
    }
    my $uri = URI->new($env->{'psgi.url_scheme'} . '://' . $env->{SERVER_NAME} . uri_unescape($env->{REQUEST_URI}), $env->{'psgi.url_scheme'});
    $uri->port($env->{SERVER_PORT});

    my %query;
    my @query = $uri->query_form;
    while(@query)
    {
      my $key = shift @query;
      my $value = shift @query;
      push @{ $query{$key} }, $value;
    }

    return [
      '200',
      [ 'Content-Type' => 'text/plain; charset=UTF-8' ],
      [ encode_json( { headers => \%headers, url => { scheme => $uri->scheme, host => $uri->host, port => $uri->port, path => $uri->path, query => \%query } } ) ],
    ];
  };
  mount '/corpus/dist/about.json' => sub {
    my $env = shift;
    return [
      '200',
      [ 'Content-Type' => 'application/json' ],
      [ encode_json( { ident => 'AB Test HTTPd' } ) ],
    ];
  };
  mount '/' => Plack::App::Directory->new({ root => $distdir->stringify })->to_app;
};

my $server = HTTP::Server::PSGI->new(
  host => $url->host,
  port => $url->port,
);

$server->run($app);