From: <jgr...@us...> - 2003-03-25 05:25:04
|
Update of /cvsroot/popfile/engine/UI In directory sc8-pr-cvs1:/tmp/cvs-serv20897/UI Modified Files: HTML.pm Added Files: HTTP.pm XMLRPC.pm Log Message: Make the UI pluggable; make SMTP, NNTP and POP3 modules register and handle their own configuration UI; factor the HTTP code out of the UI and into own class; implement XML-RPC interface; make test suite allow selection of tests to run; change the way POPFile reports its startup; make the version string work --- NEW FILE: HTTP.pm --- #---------------------------------------------------------------------------- # # This package contains an HTTP server used as a base class for other # modules that service requests over HTTP (e.g. the UI) # # Copyright (c) 2001-2003 John Graham-Cumming # #---------------------------------------------------------------------------- package UI::HTTP; use POPFile::Module; @ISA = ("POPFile::Module"); use strict; use warnings; use locale; use IO::Socket; use IO::Select; # A handy variable containing the value of an EOL for the network my $eol = "\015\012"; #---------------------------------------------------------------------------- # new # # Class new() function #---------------------------------------------------------------------------- sub new { my $type = shift; my $self = POPFile::Module->new(); return $self; } # --------------------------------------------------------------------------------------------- # # start # # Called to start the HTTP interface running # # --------------------------------------------------------------------------------------------- sub start { my ( $self ) = @_; $self->{server_} = IO::Socket::INET->new( Proto => 'tcp', $self->config_( 'local' ) == 1 ? (LocalAddr => 'localhost') : (), LocalPort => $self->config_( 'port' ), Listen => SOMAXCONN, Reuse => 1 ); if ( !defined( $self->{server_} ) ) { my $port = $self->config_( 'port' ); my $name = $self->name(); print <<EOM; \nCouldn't start the $name HTTP interface because POPFile could not bind to the HTTP port $port. This could be because there is another service using that port or because you do not have the right privileges on your system (On Unix systems this can happen if you are not root and the port you specified is less than 1024). EOM return 0; } $self->{selector_} = new IO::Select( $self->{server_} ); return 1; } # --------------------------------------------------------------------------------------------- # # stop # # Called when the interface must shutdown # # --------------------------------------------------------------------------------------------- sub stop { my ( $self ) = @_; close $self->{server_} if ( defined( $self->{server_} ) ); } # --------------------------------------------------------------------------------------------- # # service # # Called to handle interface requests # # --------------------------------------------------------------------------------------------- sub service { my ( $self ) = @_; my $code = 1; # See if there's a connection waiting for us, if there is we accept it handle a single # request and then exit my ( $ready ) = $self->{selector_}->can_read(0); # Handle HTTP requests for the UI if ( ( defined( $ready ) ) && ( $ready == $self->{server_} ) ) { if ( my $client = $self->{server_}->accept() ) { # Check that this is a connection from the local machine, if it's not then we drop it immediately # without any further processing. We don't want to allow remote users to admin POPFile my ( $remote_port, $remote_host ) = sockaddr_in( $client->peername() ); if ( ( $self->config_( 'local' ) == 0 ) || ( $remote_host eq inet_aton( "127.0.0.1" ) ) ) { # Read the request line (GET or POST) from the client and if we manage to do that # then read the rest of the HTTP headers grabbing the Content-Length and using # it to read any form POST content into $content if ( ( defined( $client ) ) && ( my $request = <$client> ) ) { my $content_length = 0; my $content; while ( <$client> ) { $content_length = $1 if ( /Content-Length: (\d+)/i ); last if ( !/[A-Z]/i ); } if ( $content_length > 0 ) { $content = ''; $client->read( $content, $content_length, length( $content ) ); } if ( $request =~ /^(GET|POST) (.*) HTTP\/1\./i ) { $client->autoflush(1); $code = $self->handle_url( $client, $2, $1, $content ); } else { http_error_( $self, $client, 500 ); } } } close $client; } } return $code; } # --------------------------------------------------------------------------------------------- # # forked # # Called when someone forks POPFile # # --------------------------------------------------------------------------------------------- sub forked { my ( $self ) = @_; close $self->{server_}; } # --------------------------------------------------------------------------------------------- # # handle_url - Handle a URL request # # $client The web browser to send the results to # $url URL to process # $command The HTTP command used (GET or POST) # $content Any non-header data in the HTTP command # # Takes a URL and splits it into the URL portion and form arguments specified in the command # filling out the %form hash with the form elements and their values. Checks the session # key and refuses access unless it matches. Serves up a small set of specific urls that are # the main UI pages and then any GIF file in the POPFile directory and CSS files in the skins # subdirectory # # --------------------------------------------------------------------------------------------- sub handle_url { my ( $self, $client, $url, $command, $content ) = @_; return $self->{url_handler_}( $self, $client, $url, $command, $content ); } # --------------------------------------------------------------------------------------------- # # parse_form__ - parse form data and fill in $self->{form_} # # $arguments The text of the form arguments (e.g. foo=bar&baz=fou) # # --------------------------------------------------------------------------------------------- sub parse_form__ { my ( $self, $arguments ) = @_; # Normally the browser should have done & to & translation on # URIs being passed onto us, but there was a report that someone # was having a problem with form arguments coming through with # something like http://127.0.0.1/history?session=foo&filter=bar # which would mess things up in the argument splitter so this code # just changes & to & for safety $arguments =~ s/&/&/g; while ( $arguments =~ m/\G(.*?)=(.*?)(&|\r|\n|$)/g ) { my $arg = $1; $self->{form_}{$arg} = $2; # Expand %7E (hex) escapes in the form data $self->{form_}{$arg} =~ s/%([0-9A-F][0-9A-F])/chr hex $1/gie; $self->{form_}{$arg} =~ s/\+/ /g; # Push the value onto an array to allow for multiple values of the same name push( @{ $self->{form_}{$arg . "_array"} }, $self->{form_}{$arg} ); } } # --------------------------------------------------------------------------------------------- # # url_encode_ # # $text Text to encode for URL safety # # Encode a URL so that it can be safely passed in a URL as per RFC2396 # # --------------------------------------------------------------------------------------------- sub url_encode_ { my ( $self, $text ) = @_; $text =~ s/ /\+/; $text =~ s/([^a-zA-Z0-9_\-.+])/sprintf("%%%02x",ord($1))/eg; return $text; } # --------------------------------------------------------------------------------------------- # # http_redirect_ - tell the browser to redirect to a url # # $client The web browser to send redirect to # $url Where to go # # Return a valid HTTP/1.0 header containing a 302 redirect message to the passed in URL # # --------------------------------------------------------------------------------------------- sub http_redirect_ { my ( $self, $client, $url ) = @_; my $header = "HTTP/1.0 302 Found\r\nLocation: "; $header .= $url; $header .= "$eol$eol"; print $client $header; } # --------------------------------------------------------------------------------------------- # # http_error_ - Output a standard HTTP error message # # $client The web browser to send the results to # $error The error number # # Return a simple HTTP error message in HTTP 1/0 format # # --------------------------------------------------------------------------------------------- sub http_error_ { my ( $self, $client, $error ) = @_; print $client "HTTP/1.0 $error Error$eol$eol"; } # --------------------------------------------------------------------------------------------- # # http_file_ - Read a file from disk and send it to the other end # # $client The web browser to send the results to # $file The file to read (always assumed to be a GIF right now) # $type Set this to the HTTP return type (e.g. text/html or image/gif) # # Returns the contents of a file formatted into an HTTP 200 message or an HTTP 404 if the # file does not exist # # --------------------------------------------------------------------------------------------- sub http_file_ { my ( $self, $client, $file, $type ) = @_; my $contents = ''; if ( open FILE, "<$file" ) { binmode FILE; while (<FILE>) { $contents .= $_; } close FILE; my $header = "HTTP/1.0 200 OK\r\nContent-Type: $type\r\nContent-Length: "; $header .= length($contents); $header .= "$eol$eol"; print $client $header . $contents; } else { http_error_( $self, $client, 404 ); } } --- NEW FILE: XMLRPC.pm --- # POPFILE LOADABLE MODULE package UI::XMLRPC; #---------------------------------------------------------------------------- # # This package contains the XML-RPC interface for POPFile, all the methods # in Classifier::Bayes can be accessed through the XMLRPC interface and # a typical method would be accessed as follows # # Classifier/Bayes.get_buckets # # Copyright (c) 2001-2003 John Graham-Cumming # #---------------------------------------------------------------------------- use POPFile::Module; @ISA = ("POPFile::Module"); use strict; use warnings; use locale; use IO::Socket; use IO::Select; require XMLRPC::Transport::HTTP; #---------------------------------------------------------------------------- # new # # Class new() function #---------------------------------------------------------------------------- sub new { my $type = shift; my $self = UI::HTTP->new(); bless $self, $type;; $self->name( 'xmlrpc' ); return $self; } # --------------------------------------------------------------------------------------------- # # initialize # # Called to initialize the interface # # --------------------------------------------------------------------------------------------- sub initialize { my ( $self ) = @_; # XML-RPC is available on port 8081 initially $self->config_( 'port', 8081 ); # Only accept connections from the local machine $self->config_( 'local', 1 ); # Tell the user interface module that we having a configuration # item that needs a UI component $self->{ui__}->register_configuration_item( 'configuration', 'xmlrpc_port', $self ); $self->{ui__}->register_configuration_item( 'security', 'xmlrpc_local', $self ); return 1; } # --------------------------------------------------------------------------------------------- # # start # # Called to start the HTTP interface running # # --------------------------------------------------------------------------------------------- sub start { my ( $self ) = @_; # We use a single XMLRPC::Lite object to handle requests for access to the # Classifier::Bayes object $self->{server__} = XMLRPC::Transport::HTTP::Daemon->new( Proto => 'tcp', $self->config_( 'local' ) == 1 ? (LocalAddr => 'localhost') : (), LocalPort => $self->config_( 'port' ), Listen => SOMAXCONN, Reuse => 1 ); if ( !defined( $self->{server__} ) ) { my $port = $self->config_( 'port' ); my $name = $self->name(); print <<EOM; \nCouldn't start the $name HTTP interface because POPFile could not bind to the HTTP port $port. This could be because there is another service using that port or because you do not have the right privileges on your system (On Unix systems this can happen if you are not root and the port you specified is less than 1024). EOM return 0; } # All requests will get dispatched to the main Classifier::Bayes object, for example # the get_bucket_color interface is accessed with the method name # # Classifier/Bayes.get_bucket_color $self->{server__}->dispatch_to( $self->{classifier__} ); # DANGER WILL ROBINSON! In order to make a polling XML-RPC server I am using # the XMLRPC::Transport::HTTP::Daemon class which uses blocking I/O. This would # be all very well but it seems to be totally ignorning signals on Windows and so # POPFile is unstoppable when the handle() method is called. Forking with this # blocking doesn't help much because then we get an unstoppable child. # # So the solution relies on knowing the internals of XMLRPC::Transport::HTTP::Daemon # which is actuall a SOAP::Transport::HTTP::Daemon which has a HTTP::Daemon (stored # in a private variable called _daemon. HTTP::Daemon is an IO::Socket::INET which means # we can create a selector on it, so here we access a PRIVATE variable on the XMLRPC # object. This is very bad behaviour, but it works until someone changes XMLRPC. $self->{selector__} = new IO::Select( $self->{server__}->{_daemon} ); return 1; } # --------------------------------------------------------------------------------------------- # # service # # Called to handle interface requests # # --------------------------------------------------------------------------------------------- sub service { my ( $self ) = @_; # See if there's a connection pending on the XMLRPC socket and handle # single request my ( $ready ) = $self->{selector__}->can_read(0); if ( defined( $ready ) ) { if ( my $client = $self->{server__}->accept() ) { # Check that this is a connection from the local machine, if it's not then we drop it immediately # without any further processing. We don't want to allow remote users to admin POPFile my ( $remote_port, $remote_host ) = sockaddr_in( $client->peername() ); if ( ( $self->config_( 'local' ) == 0 ) || ( $remote_host eq inet_aton( "127.0.0.1" ) ) ) { my $request = $client->get_request(); $self->{server__}->request( $request ); # Note the direct call to SOAP::Transport::HTTP::Server::handle() here, this is # because we have taken the code from XMLRPC::Transport::HTTP::Server::handle() # and reproduced a modification of it here, accepting a single request and handling # it. This call to the parent of XMLRPC::Transport::HTTP::Server will actually # deal with the request $self->{server__}->SOAP::Transport::HTTP::Server::handle(); $client->send_response( $self->{server__}->response ); $client->close(); } } } return 1; } # --------------------------------------------------------------------------------------------- # # configure_item # # $name The name of the item being configured, was passed in by the call # to register_configuration_item # $language Reference to the hash holding the current language # $session_key The current session key # # Must return the HTML for this item # --------------------------------------------------------------------------------------------- sub configure_item { my ( $self, $name, $language, $session_key ) = @_; my $body; if ( $name eq 'xmlrpc_port' ) { $body .= "<form action=\"/configuration\">\n"; $body .= "<label class=\"configurationLabel\" for=\"configPopPort\">". $$language{Configuration_XMLRPCPort} . ":</label><br />\n"; $body .= "<input name=\"xmlrpc_port\" type=\"text\" id=\"configPopPort\" value=\"" . $self->config_( 'port' ) . "\" />\n"; $body .= "<input type=\"submit\" class=\"submit\" name=\"update_xmlrpc_port\" value=\"" . $$language{Apply} . "\" />\n"; $body .= "<input type=\"hidden\" name=\"session\" value=\"$session_key\" />\n</form>\n"; } if ( $name eq 'xmlrpc_local' ) { $body .= "<span class=\"securityLabel\">$$language{Security_XMLRPC}:</span><br />\n"; $body .= "<table border=\"0\" cellpadding=\"0\" cellspacing=\"0\" summary=\"\"><tr><td nowrap=\"nowrap\">\n"; if ( $self->config_( 'local' ) == 1 ) { $body .= "<form class=\"securitySwitch\" action=\"/security\">\n"; $body .= "<span class=\"securityWidgetStateOff\">$$language{Security_NoStealthMode}</span>\n"; $body .= "<input type=\"submit\" class=\"toggleOn\" id=\"securityAcceptPOP3On\" name=\"toggle\" value=\"$$language{ChangeToYes}\" />\n"; $body .= "<input type=\"hidden\" name=\"xmlrpc_local\" value=\"1\" />\n"; $body .= "<input type=\"hidden\" name=\"session\" value=\"$session_key\" />\n</form>\n"; } else { $body .= "<form class=\"securitySwitch\" action=\"/security\">\n"; $body .= "<span class=\"securityWidgetStateOn\">$$language{Yes}</span>\n"; $body .= "<input type=\"submit\" class=\"toggleOff\" id=\"securityAcceptPOP3Off\" name=\"toggle\" value=\"$$language{ChangeToNo} (Stealth Mode)\" />\n"; $body .= "<input type=\"hidden\" name=\"xmlrpc_local\" value=\"2\" />\n"; $body .= "<input type=\"hidden\" name=\"session\" value=\"$session_key\" />\n</form>\n"; } $body .= "</td></tr></table>\n"; } return $body; } # --------------------------------------------------------------------------------------------- # # validate_item # # $name The name of the item being configured, was passed in by the call # to register_configuration_item # $language Reference to the hash holding the current language # $form Hash containing all form items # # Must return the HTML for this item # --------------------------------------------------------------------------------------------- sub validate_item { my ( $self, $name, $language, $form ) = @_; # Just check to see if the XML rpc port was change and check its value if ( $name eq 'xmlrpc_port' ) { if ( defined($$form{xmlrpc_port}) ) { if ( ( $$form{xmlrpc_port} >= 1 ) && ( $$form{xmlrpc_port} < 65536 ) ) { $self->config_( 'port', $$form{xmlrpc_port} ); return '<blockquote>' . sprintf( $$language{Configuration_XMLRPCUpdate} . '</blockquote>' , $self->config_( 'port' ) ); } else { return "<blockquote><div class=\"error01\">$$language{Configuration_Error7}</div></blockquote>"; } } } if ( $name eq 'xmlrpc_local' ) { $self->config_( 'local', $$form{xmlrpc_local}-1 ) if ( defined($$form{xmlrpc_local}) ); } return ''; } # GETTERS/SETTERS sub classifier { my ( $self, $value ) = @_; if ( defined( $value ) ) { $self->{classifier__} = $value; } return $self->{classifier__}; } sub ui { my ( $self, $value ) = @_; if ( defined( $value ) ) { $self->{ui__} = $value; } return $self->{ui__}; } 1; Index: HTML.pm =================================================================== RCS file: /cvsroot/popfile/engine/UI/HTML.pm,v retrieving revision 1.118 retrieving revision 1.119 diff -C2 -d -r1.118 -r1.119 *** HTML.pm 21 Mar 2003 23:10:32 -0000 1.118 --- HTML.pm 25 Mar 2003 05:24:58 -0000 1.119 *************** *** 2,8 **** package UI::HTML; - use POPFile::Module; - @ISA = ("POPFile::Module"); - #---------------------------------------------------------------------------- # --- 2,5 ---- *************** *** 12,16 **** [...2157 lines suppressed...] + # + # and needs to return the HTML for the foo_bar item. Then it will may receive a call to its + # + # validate_item( 'foo_bar', language hash, form hash ) + # + # and needs to check the form for information from any form it created and returned from the + # call to configure_item and update its own state. It can optionally return HTML that + # will be displayed at the top of the page + # + # --------------------------------------------------------------------------------------------- + sub register_configuration_item + { + my ( $self, $type, $name, $object ) = @_; + + $self->{dynamic_ui__}{$type}{$name} = $object; + } + + # GETTERS/SETTERS sub classifier |