The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.

NAME

CGI::Portable::AdapterSocket - Run under IO::Socket-based Perl server

DEPENDENCIES

Perl Version

        5.004

Standard Modules

        IO::Socket  -- IO::Socket::INET built in

Nonstandard Modules

        CGI::Portable 0.50

SYNOPSIS

Content of thin shell "startup_socket.pl" for IO::Socket::INET:

        #!/usr/bin/perl
        use strict;
        use warnings;

        print "[Server $0 starting up]\n";

        require CGI::Portable;
        my $globals = CGI::Portable->new();

        use Cwd;
        $globals->file_path_root( cwd() );  # let us default to current working directory
        $globals->file_path_delimiter( $^O=~/Mac/i ? ":" : $^O=~/Win/i ? "\\" : "/" );

        $globals->set_prefs( 'config.pl' );
        $globals->current_user_path_level( 1 );

        require CGI::Portable::AdapterSocket;
        my $io = CGI::Portable::AdapterSocket->new();

        use IO::Socket;
        my $server = IO::Socket::INET->new(
                Listen    => SOMAXCONN,
                LocalAddr => '127.0.0.1',
                LocalPort => 1984,
                Proto     => 'tcp'
        );
        die "[Error: can't setup server $0]" unless $server;

        print "[Server $0 accepting clients]\n";

        while( my $client = $server->accept() ) {
                printf "%s: [Connect from %s]\n", scalar localtime, $client->peerhost;

                my $content = $globals->make_new_context();

                $io->fetch_user_input( $content, $client );
                $content->call_component( 'DemoAardvark' );
                $io->send_user_output( $content, $client );

                close $client;

                printf "%s http://%s:%s%s %s\n", $content->request_method, 
                        $content->server_domain, $content->server_port, 
                        $content->user_path_string, $content->http_status_code;
        }

        1;

DESCRIPTION

This Perl 5 object class is an adapter for CGI::Portable that takes care of the details for gathering user input and sending user output when this Perl script is the HTTP server itself, and IO::Socket (IO::Socket::INET) is being used for networking with the HTTP client.

SYNTAX

This class does not export any functions or methods, so you need to call them using object notation. This means using Class->function() for functions and $object->method() for methods. If you are inheriting this class for your own modules, then that often means something like $self->method().

FUNCTIONS AND METHODS

new()

This function creates a new CGI::Portable::AdapterSocket object and returns it. The new object has no properties, but only methods.

fetch_user_input( GLOBALS, CLIENT )

This method takes a CGI::Portable object as its first argument, GLOBALS, and feeds it all of the HTTP request and user input details that it can gather. The second argument, CLIENT, is an IO::Socket::INET object which is the client we will be getting our input from. The user_path() is always initialized from the requested uri by this method; if you want it to be from a query param then you can update it yourself later. For debugging purposes, this method returns two strings containing the un-parsed HTTP request headers and body respectively, should you want to inspect them for yourself later.

send_user_output( GLOBALS, CLIENT )

This method takes a CGI::Portable object as its first argument, GLOBALS, and sends to the user as much of the HTTP response and user output details that it can get from the object. The second argument, CLIENT, is an IO::Socket::INET object which is the client we will be sending our output to.

send_quick_html_response( CONTENT, CLIENT )

This method takes a string containing an HTML document as its first argument, CONTENT, and sends an http response appropriate for an HTML document which includes CONTENT as the http body. The second argument, CLIENT, is an IO::Socket::INET object which is the client we will be sending our output to.

send_quick_redirect_response( URL, CLIENT )

This method takes a string containing an url as its first argument, URL, and sends an http redirection header to send the client browser to that url. The second argument, CLIENT, is an IO::Socket::INET object which is the client we will be sending our output to.

AUTHOR

Copyright (c) 1999-2004, Darren R. Duncan. All rights reserved. This module is free software; you can redistribute it and/or modify it under the same terms as Perl itself. However, I do request that this copyright information and credits remain attached to the file. If you modify this module and redistribute a changed version then please attach a note listing the modifications. This module is available "as-is" and the author can not be held accountable for any problems resulting from its use.

I am always interested in knowing how my work helps others, so if you put this module to use in any of your own products or services then I would appreciate (but not require) it if you send me the website url for said product or service, so I know who you are. Also, if you make non-proprietary changes to the module because it doesn't work the way you need, and you are willing to make these freely available, then please send me a copy so that I can roll desirable changes into the main release.

Address comments, suggestions, and bug reports to perl@DarrenDuncan.net.

SEE ALSO

perl(1), CGI::Portable, IO::Socket, IO::Socket::INET.