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 configuration.  The port has
45       also been defaulted to port 80 - but could easily be changed to another
46       through the server configuration.  You can also very easily add ssl by
47       including, proto=>"ssl" and provide a SSL_cert_file and SSL_key_file.
48
49       For example, here is a basic server that will bind to all interfaces,
50       will speak both HTTP on port 8080 as well as HTTPS on 8443, and will
51       speak both IPv4, as well as IPv6 if it is available.
52
53           use base qw(Net::Server::PSGI);
54
55           __PACKAGE__->run(
56               port  => [8080, "8443/ssl"],
57               ipv   => '*', # IPv6 if available
58               SSL_key_file  => '/my/key',
59               SSL_cert_file => '/my/cert',
60           );
61

METHODS

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

OPTIONS

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

AUTHOR

140       Paul T. Seamons paul@seamons.com
141

SEE ALSO

143       Please see also Plack, Starman,
144
145       Net::Server::Fork, Net::Server::INET, Net::Server::PreFork,
146       Net::Server::PreForkSimple, Net::Server::MultiType, Net::Server::Single
147       Net::Server::SIG Net::Server::Daemonize Net::Server::Proto
148       Net::Server::HTTP
149
150
151
152perl v5.36.0                      2023-03-17              Net::Server::PSGI(3)
Impressum