File: README

package info (click to toggle)
libpoe-component-server-simplehttp-perl 2.30-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 324 kB
  • sloc: perl: 1,324; makefile: 7
file content (579 lines) | stat: -rw-r--r-- 21,074 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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
NAME

    POE::Component::Server::SimpleHTTP - Perl extension to serve HTTP
    requests in POE.

VERSION

    version 2.30

SYNOPSIS

            use POE;
            use POE::Component::Server::SimpleHTTP;
    
            # Start the server!
            POE::Component::Server::SimpleHTTP->new(
                    'ALIAS'         =>      'HTTPD',
                    'PORT'          =>      11111,
                    'HOSTNAME'      =>      'MySite.com',
                    'HANDLERS'      =>      [
                            {
                                    'DIR'           =>      '^/bar/.*',
                                    'SESSION'       =>      'HTTP_GET',
                                    'EVENT'         =>      'GOT_BAR',
                            },
                            {
                                    'DIR'           =>      '^/$',
                                    'SESSION'       =>      'HTTP_GET',
                                    'EVENT'         =>      'GOT_MAIN',
                            },
                            {
                                    'DIR'           =>      '^/foo/.*',
                                    'SESSION'       =>      'HTTP_GET',
                                    'EVENT'         =>      'GOT_NULL',
                            },
                            {
                                    'DIR'           =>      '.*',
                                    'SESSION'       =>      'HTTP_GET',
                                    'EVENT'         =>      'GOT_ERROR',
                            },
                    ],
    
                    'LOGHANDLER' => { 'SESSION' => 'HTTP_GET',
                                      'EVENT'   => 'GOT_LOG',
                    },
    
                    'LOG2HANDLER' => { 'SESSION' => 'HTTP_GET',
                                      'EVENT'   => 'POSTLOG',
                    },
    
                    # In the testing phase...
                    'SSLKEYCERT'    =>      [ 'private-key.pem', 'public-cert.pem' ],
                    'SSLINTERMEDIATECACERT' =>      'intermediate-ca-cert.pem',
            ) or die 'Unable to create the HTTP Server';
    
            # Create our own session to receive events from SimpleHTTP
            POE::Session->create(
                    inline_states => {
                            '_start'        =>      sub {   $_[KERNEL]->alias_set( 'HTTP_GET' );
                                                            $_[KERNEL]->post( 'HTTPD', 'GETHANDLERS', $_[SESSION], 'GOT_HANDLERS' );
                                                    },
    
                            'GOT_BAR'       =>      \&GOT_REQ,
                            'GOT_MAIN'      =>      \&GOT_REQ,
                            'GOT_ERROR'     =>      \&GOT_ERR,
                            'GOT_NULL'      =>      \&GOT_NULL,
                            'GOT_HANDLERS'  =>      \&GOT_HANDLERS,
                            'GOT_LOG'       =>      \&GOT_LOG,
                    },
            );
    
            # Start POE!
            POE::Kernel->run();
    
            sub GOT_HANDLERS {
                    # ARG0 = HANDLERS array
                    my $handlers = $_[ ARG0 ];
    
                    # Move the first handler to the last one
                    push( @$handlers, shift( @$handlers ) );
    
                    # Send it off!
                    $_[KERNEL]->post( 'HTTPD', 'SETHANDLERS', $handlers );
            }
    
            sub GOT_NULL {
                    # ARG0 = HTTP::Request object, ARG1 = HTTP::Response object, ARG2 = the DIR that matched
                    my( $request, $response, $dirmatch ) = @_[ ARG0 .. ARG2 ];
    
                    # Kill this!
                    $_[KERNEL]->post( 'HTTPD', 'CLOSE', $response );
            }
    
            sub GOT_REQ {
                    # ARG0 = HTTP::Request object, ARG1 = HTTP::Response object, ARG2 = the DIR that matched
                    my( $request, $response, $dirmatch ) = @_[ ARG0 .. ARG2 ];
    
                    # Do our stuff to HTTP::Response
                    $response->code( 200 );
                    $response->content( 'Some funky HTML here' );
    
                    # We are done!
                    # For speed, you could use $_[KERNEL]->call( ... )
                    $_[KERNEL]->post( 'HTTPD', 'DONE', $response );
            }
    
            sub GOT_ERR {
                    # ARG0 = HTTP::Request object, ARG1 = HTTP::Response object, ARG2 = the DIR that matched
                    my( $request, $response, $dirmatch ) = @_[ ARG0 .. ARG2 ];
    
                    # Check for errors
                    if ( ! defined $request ) {
                            $_[KERNEL]->post( 'HTTPD', 'DONE', $response );
                            return;
                    }
    
                    # Do our stuff to HTTP::Response
                    $response->code( 404 );
                    $response->content( "Hi visitor from " . $response->connection->remote_ip . ", Page not found -> '" . $request->uri->path . "'" );
    
                    # We are done!
                    # For speed, you could use $_[KERNEL]->call( ... )
                    $_[KERNEL]->post( 'HTTPD', 'DONE', $response );
            }
    
            sub GOT_LOG {
                    # ARG0 = HTTP::Request object, ARG1 = remote IP
                    my ($request, $remote_ip) = @_[ARG0,ARG1];
    
                    # Do some sort of logging activity.
                    # If the request was malformed, $request = undef
                    # CHECK FOR A REQUEST OBJECT BEFORE USING IT.
            if( $request ) {
            {
                    warn join(' ', time(), $remote_ip, $request->uri ), "\n";
            } else {
                    warn join(' ', time(), $remote_ip, 'Bad request' ), "\n";
            }
    
                    return;
            }

DESCRIPTION

    This module makes serving up HTTP requests a breeze in POE.

    The hardest thing to understand in this module is the HANDLERS. That's
    it!

    The standard way to use this module is to do this:

            use POE;
            use POE::Component::Server::SimpleHTTP;
    
            POE::Component::Server::SimpleHTTP->new( ... );
    
            POE::Session->create( ... );
    
            POE::Kernel->run();

 Starting SimpleHTTP

    To start SimpleHTTP, just call it's new method:

            POE::Component::Server::SimpleHTTP->new(
                    'ALIAS'         =>      'HTTPD',
                    'ADDRESS'       =>      '192.168.1.1',
                    'PORT'          =>      11111,
                    'HOSTNAME'      =>      'MySite.com',
                    'HEADERS'       =>      {},
                    'HANDLERS'      =>      [ ],
            );

    This method will die on error or return success.

    This constructor accepts only 7 options.

    ALIAS

      This will set the alias SimpleHTTP uses in the POE Kernel. This will
      default to "SimpleHTTP"

    ADDRESS

      This value will be passed to POE::Wheel::SocketFactory to bind to,
      will use INADDR_ANY if it is nothing is provided (or IN6ADDR_ANY if
      DOMAIN is AF_INET6). For UNIX domain sockets, it should be a path
      describing the socket's filename.

      If neither DOMAIN nor ADDRESS are specified, it will use IN6ADDR_ANY
      and AF_INET6.

    PORT

      This value will be passed to POE::Wheel::SocketFactory to bind to.

    DOMAIN

      This value will be passed to POE::Wheel::SocketFactory to define the
      socket domain used (AF_INET, AF_INET6, AF_UNIX).

    HOSTNAME

      This value is for the HTTP::Request's URI to point to. If this is not
      supplied, SimpleHTTP will use Sys::Hostname to find it.

    HEADERS

      This should be a hashref, that will become the default headers on all
      HTTP::Response objects. You can override this in individual requests
      by setting it via $request->header( ... )

      For more information, consult the HTTP::Headers module.

    HANDLERS

      This is the hardest part of SimpleHTTP :)

      You supply an array, with each element being a hash. All the hashes
      should contain those 3 keys:

      DIR -> The regexp that will be used, more later.

      SESSION -> The session to send the input

      EVENT -> The event to trigger

      The DIR key should be a valid regexp. This will be matched against
      the current request path. Pseudocode is: if ( $path =~ /$DIR/ )

      NOTE: The path is UNIX style, not MSWIN style ( /blah/foo not
      \blah\foo )

      Now, if you supply 100 handlers, how will SimpleHTTP know what to do?
      Simple! By passing in an array in the first place, you have already
      told SimpleHTTP the order of your handlers. They will be tried in
      order, and if a match is not found, SimpleHTTP will return a 404
      response.

      This allows some cool things like specifying 3 handlers with DIR of:
      '^/foo/.*', '^/$', '.*'

      Now, if the request is not in /foo or not root, your 3rd handler will
      catch it, becoming the "404 not found" handler!

      NOTE: You might get weird Session/Events, make sure your handlers are
      in order, for example: '^/', '^/foo/.*' The 2nd handler will NEVER
      get any requests, as the first one will match ( no $ in the regex )

      Now, here's what a handler receives:

      ARG0 -> HTTP::Request object

      ARG1 -> POE::Component::Server::SimpleHTTP::Response object

      ARG2 -> The exact DIR that matched, so you can see what triggered
      what

      NOTE: If ARG0 is undef, that means POE::Filter::HTTPD encountered an
      error parsing the client request, simply modify the HTTP::Response
      object and send some sort of generic error. SimpleHTTP will set the
      path used in matching the DIR regexes to an empty string, so if there
      is a "catch-all" DIR regex like '.*', it will catch the errors, and
      only that one.

      NOTE: The only way SimpleHTTP will leak memory ( hopefully heh ) is
      if you discard the SimpleHTTP::Response object without sending it
      back to SimpleHTTP via the DONE/CLOSE events, so never do that!

    KEEPALIVE

      Set to true to enable HTTP keep-alive support. Connections will be
      kept alive until the client closes the connection. All HTTP/1.1
      connections are kept-open, unless you set the response Connection
      header to close.

          $response->header( Connection => 'close' );

      If you want more control, use
      POE::Component::Server::HTTP::KeepAlive.

    LOGHANDLER

      Expects a hashref with the following key, values:

      SESSION -> The session to send the input

      EVENT -> The event to trigger

      You will receive an event for each request to the server from
      clients. Malformed client requests will not be passed into the
      handler. Instead undef will be passed. Event is called before ANY
      content handler is called.

      The event will have the following parameters:

      ARG0 -> HTTP::Request object/undef if client request was malformed.

      ARG1 -> the IP address of the client

    LOG2HANDLER

      Expect a hashref with the following key, values:

      SESSION -> The session to send the input

      EVENT -> The event to trigger

      You will receive an event for each response that hit DONE call.
      Malformed client requests will not be passed into the handler. Event
      is after processing all content handlers.

      The event will have the following parameters:

      ARG0 -> HTTP::Request object

      ARG1 -> HTTP::Response object

      That makes possible following code:

              my ($login, $password) = $request->authorization_basic();
              printf STDERR "%s - %s [%s] \"%s %s %s\" %d %d\n",
                      $response->connection->remote_ip, $login||'-', POSIX::strftime("%d/%b/%Y:%T %z",localtime(time())),
                      $request->method(), $request->uri()->path(), $request->protocol(),
                      $response->code(), length($response->content());

      Emulate apache-like logs for PoCo::Server::SimpleHTTP

    SETUPHANDLER

      Expects a hashref with the following key, values:

      SESSION -> The session to send the input

      EVENT -> The event to trigger

      You will receive an event when the listener wheel has been setup.

      Currently there are no parameters returned.

    SSLKEYCERT

      This should be an arrayref of only 2 elements - the private key and
      public certificate locations. Now, this is still in the experimental
      stage, and testing is greatly welcome!

      Again, this will automatically turn every incoming connection into a
      SSL socket. Once enough testing has been done, this option will be
      augmented with more SSL stuff!

    SSLINTERMEDIATECACERT

      This option is needed in case the SSL certificate references an
      intermediate certification authority certificate.

    PROXYMODE

      Set this to a true value to enable the server to act as a proxy
      server, ie. it won't mangle the HTTP::Request URI.

 Events

    SimpleHTTP is so simple, there are only 8 events available.

    DONE

              This event accepts only one argument: the HTTP::Response object we sent to the handler.
      
              Calling this event implies that this particular request is done, and will proceed to close the socket.
      
              NOTE: This method automatically sets those 3 headers if they are not already set:
                      Date            ->      Current date stringified via HTTP::Date->time2str
                      Content-Type    ->      text/html
                      Content-Length  ->      length( $response->content )
      
              To get greater throughput and response time, do not post() to the DONE event, call() it!
              However, this will force your program to block while servicing web requests...

    CLOSE

              This event accepts only one argument: the HTTP::Response object we sent to the handler.
      
              Calling this event will close the socket, not sending any output

    GETHANDLERS

              This event accepts 2 arguments: The session + event to send the response to
      
              This event will send back the current HANDLERS array ( deep-cloned via Storable::dclone )
      
              The resulting array can be played around to your tastes, then once you are done...

    SETHANDLERS

              This event accepts only one argument: pointer to HANDLERS array
      
              BEWARE: if there is an error in the HANDLERS, SimpleHTTP will die!

    SETCLOSEHANDLER

          $_[KERNEL]->call( $_[SENDER], 'SETCLOSEHANDLER', $connection,
                            $event, @args );

      Calls $event in the current session when $connection is closed. You
      could use for persistent connection handling.

      Multiple session may register close handlers.

      Calling SETCLOSEHANDLER without $event to remove the current
      session's handler:

         $_[KERNEL]->call( $_[SENDER], 'SETCLOSEHANDLER', $connection );

      You must make sure that @args doesn't cause a circular reference.
      Ideally, use $connection-ID> or some other unique value associated
      with this $connection.

    STARTLISTEN

              Starts the listening socket, if it was shut down

    STOPLISTEN

              Simply a wrapper for SHUTDOWN GRACEFUL, but will not shutdown SimpleHTTP if there is no more requests

    SHUTDOWN

              Without arguments, SimpleHTTP does this:
                      Close the listening socket
                      Kills all pending requests by closing their sockets
                      Removes it's alias
      
              With an argument of 'GRACEFUL', SimpleHTTP does this:
                      Close the listening socket
                      Waits for all pending requests to come in via DONE/CLOSE, then removes it's alias

    STREAM

              With a $response argument it streams the content and calls back the streaming event
              of the user's session (or with the dont_flush option you're responsible for calling
              back your session's streaming event).
      
              To use the streaming feature see below.

 Streaming with SimpleHTTP

    It's possible to send data as a stream to clients (unbuffered and
    integrated in the POE loop).

    Just create your session to receive events from SimpleHTTP as usually
    and add a streaming event, this event will be triggered over and over
    each time you set the $response to a streaming state and once you
    trigger it:

       # sets the response as streamed within our session which alias is HTTP_GET
       # with the event GOT_STREAM
       $response->stream(
          session     => 'HTTP_GET',
          event       => 'GOT_STREAM',
          dont_flush  => 1
       );
    
       # then you can simply yield your streaming event, once the GOT_STREAM event
       # has reached its end it will be triggered again and again, until you
       # send a CLOSE event to the kernel with the appropriate response as parameter
       $kernel->yield('GOT_STREAM', $response);

    The optional dont_flush option gives the user the ability to control
    the callback to the streaming event, which means once your stream event
    has reached its end it won't be called, you have to call it back.

    You can now send data by chunks and either call yourself back (via POE)
    or shutdown when your streaming is done (EOF for example).

       sub GOT_STREAM {
          my ( $kernel, $heap, $response ) = @_[KERNEL, HEAP, ARG0];
    
          # sets the content of the response
          $response->content("Hello World\n");
    
          # send it to the client
          POE::Kernel->post('HTTPD', 'STREAM', $response);
    
          # if we have previously set the dont_flush option
          # we have to trigger our event back until the end of
          # the stream like this (that can be a yield, of course):
          #
          # $kernel->delay('GOT_STREAM', 1, $stream );
    
          # otherwise the GOT_STREAM event is triggered continuously until
          # we call the CLOSE event on the response like that :
          #
          if ($heap{'streaming_is_done'}) {
             # close the socket and end the stream
             POE::Kernel->post('HTTPD', 'CLOSE', $response );
          }
       }

    The dont_flush option is there to be able to control the frequency of
    flushes to the client.

 SimpleHTTP Notes

    You can enable debugging mode by doing this:

            sub POE::Component::Server::SimpleHTTP::DEBUG () { 1 }
            use POE::Component::Server::SimpleHTTP;

    Also, this module will try to keep the Listening socket alive. if it
    dies, it will open it again for a max of 5 retries.

    You can override this behavior by doing this:

            sub POE::Component::Server::SimpleHTTP::MAX_RETRIES () { 10 }
            use POE::Component::Server::SimpleHTTP;

    For those who are pondering about basic-authentication, here's a tiny
    snippet to put in the Event handler

            # Contributed by Rocco Caputo
            sub Got_Request {
                    # ARG0 = HTTP::Request, ARG1 = HTTP::Response
                    my( $request, $response ) = @_[ ARG0, ARG1 ];
    
                    # Get the login
                    my ( $login, $password ) = $request->authorization_basic();
    
                    # Decide what to do
                    if ( ! defined $login or ! defined $password ) {
                            # Set the authorization
                            $response->header( 'WWW-Authenticate' => 'Basic realm="MyRealm"' );
                            $response->code( 401 );
                            $response->content( 'FORBIDDEN.' );
    
                            # Send it off!
                            $_[KERNEL]->post( 'SimpleHTTP', 'DONE', $response );
                    } else {
                            # Authenticate the user and move on
                    }
            }

 EXPORT

    Nothing.

ABSTRACT

            An easy to use HTTP daemon for POE-enabled programs

SEE ALSO

            L<POE>
    
            L<POE::Filter::HTTPD>
    
            L<HTTP::Request>
    
            L<HTTP::Response>
    
            L<POE::Component::Server::SimpleHTTP::Connection>
    
            L<POE::Component::Server::SimpleHTTP::Response>
    
            L<POE::Component::Server::SimpleHTTP::PreFork>
    
            L<POE::Component::SSLify>

AUTHOR

    Apocalypse <APOCAL@cpan.org>

COPYRIGHT AND LICENSE

    This software is copyright (c) 2023 by Apocalypse, Chris Williams,
    Eriam Schaffter, Marlon Bailey and Philip Gwyn.

    This is free software; you can redistribute it and/or modify it under
    the same terms as the Perl 5 programming language system itself.