1Net::Server::PSGI(3)  User Contributed Perl Documentation Net::Server::PSGI(3)
2
3
4

NAME

6       Net::Server::PSGI - basic Net::Server based PSGI HTTP server class
7

TEST ONE LINER

9           perl -e 'use base qw(Net::Server::PSGI); main->run(port => 8080, ipv => "*")'
10           # runs a default echo server
11

SYNOPSIS

13           use base qw(Net::Server::PSGI);
14           __PACKAGE__->run(app => \&my_echo_handler); # will bind IPv4 port 80
15
16           sub my_echo_handler {
17               my $env = shift;
18               my $txt = qq{<form method="post" action="/bam"><input type="text" name="foo"><input type="submit"></form>\n};
19
20               require Data::Dumper;
21               local $Data::Dumper::Sortkeys = 1;
22
23               require CGI::PSGI;
24               my $form = {};
25               my $q = CGI::PSGI->new($env);
26               $form->{$_} = $q->param($_) for $q->param;
27
28               $txt .= "<pre>".Data::Dumper->Dump([$env, $form], ['env', 'form'])."</pre>";
29
30               return [200, ['Content-type', 'text/html'], [$txt]];
31           }
32

DESCRIPTION

34       If you want a more fully featured PSGI experience, it would be wise to
35       look at the Plack and Starman set of modules.  Net::Server::PSGI is
36       intended as an easy gateway into PSGI.  But to get the most out of all
37       that PSGI has to offer, you should review the Plack and
38       Plack::Middleware.  If you only need something a little more
39       rudimentary, then Net::Server::PSGI may be good for you.
40
41       Net::Server::PSGI takes Net::Server::HTTP one level farther.  It begins
42       with base type MultiType defaulting to Net::Server::Fork.  It is easy
43       to change it to any of the other Net::Server flavors by passing
44       server_type => $other_flavor in the server configurtation.  The port
45       has also been defaulted to port 80 - but could easily be changed to
46       another through the server configuration.  You can also very easily add
47       ssl by including, proto=>"ssl" and provide a SSL_cert_file and
48       SSL_key_file.
49
50       For example, here is a basic server that will bind to all interfaces,
51       will speak both HTTP on port 8080 as well as HTTPS on 8443, and will
52       speak both IPv4, as well as IPv6 if it is available.
53
54           use base qw(Net::Server::PSGI);
55
56           __PACKAGE__->run(
57               port  => [8080, "8443/ssl"],
58               ipv   => '*', # IPv6 if available
59               SSL_key_file  => '/my/key',
60               SSL_cert_file => '/my/cert',
61           );
62

METHODS

64       "process_request"
65           This method has been overridden in Net::Server::PSGI - you should
66           not use it while using Net::Server::PSGI.  This overridden method
67           parses the environment and sets up request alarms and handles dying
68           failures.  It calls process_psgi_request once the request is ready
69           and headers have been parsed.
70
71       "process_psgi_request"
72           Used when psgi_enabled is true.  During this method,
73           find_psgi_handler will be called to return the appropriate psgi
74           response handler.  Once finished, print_psgi_headers and
75           print_psgi_body are used to print out the response.  See PSGI.
76
77           Typically this method should not be overridden.  Instead, an
78           appropriate method for finding the app should be given to
79           find_psgi_handler or app.
80
81       "find_psgi_handler"
82           Used to lookup the appropriate PSGI handler.  A reference to the
83           already parsed $env hashref is passed.  PATH_INFO will be
84           initialized to the full path portion of the URI.  SCRIPT_NAME will
85           be initialized to the empty string.  This handler should set the
86           appropriate values for SCRIPT_NAME and PATH_INFO depending upon the
87           path matched.  A code reference for the handler should be returned.
88           The default find_psgi_handler will call the "app" method.  If that
89           fails a reference to the psgi_echo_handler is returned as the
90           default application.
91
92               sub find_psgi_handler {
93                   my ($self, $env) = @_;
94
95                   if ($env->{'PATH_INFO'} && $env->{'PATH_INFO'} =~ s{^ (/foo) (?= $ | /) }{}x) {
96                       $env->{'SCRIPT_NAME'} = $1;
97                       return \&foo_app;
98                   }
99
100                   return $self->SUPER::find_psgi_handler($env);
101               }
102
103       "app"
104           Return a reference to the application being served.  This should be
105           a valid PSGI application.  See PSGI.  By default it will look at
106           the value of the "app" configuration option.  The "app" method may
107           also be used to set the "app" configuration option.
108
109               package MyApp;
110               use base qw(Net::Server::PSGI);
111
112               sub default_server_type { 'Prefork' }
113
114               sub my_app {
115                   my $env = shift;
116                   return [200, ['Content-type', 'text/html'], ["Hello world"]];
117               }
118
119
120               MyApp->run(app => \&my_app);
121
122
123               # OR
124               sub app { \&my_app }
125               MyApp->run;
126
127
128               # OR
129               my $server = MyApp->new;
130               $server->app(\&my_app);
131               $server->run;
132

OPTIONS

134       In addition to the command line arguments of the Net::Server::HTTP base
135       classes you can also set the following options.
136
137       app Should return a coderef of the PSGI application.  Is returned by
138           the app method.
139

AUTHOR

141       Paul T. Seamons paul@seamons.com
142

SEE ALSO

144       Please see also Plack, Starman,
145
146       Net::Server::Fork, Net::Server::INET, Net::Server::PreFork,
147       Net::Server::PreForkSimple, Net::Server::MultiType, Net::Server::Single
148       Net::Server::SIG Net::Server::Daemonize Net::Server::Proto
149       Net::Server::HTTP
150
151
152
153perl v5.26.3                      2017-08-10              Net::Server::PSGI(3)
Impressum