123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156 |
- #!/usr/bin/perl
- # $Id: webserver,v 1.6 2019/11/28 14:45:09 gilles Exp gilles $
- package Imapsync;
- use base qw(Net::Server::HTTP);
- use strict ;
- use warnings ;
- use Data::Dumper ;
- use English qw( -no_match_vars ) ;
- my $server = Imapsync->new(
- 'port' => [8080],
- 'access_log_file' => 'STDERR',
- 'log_level' => 4,
- 'timeout_header' => 20,
- 'timeout_idle' => 60,
- ) ;
- $server->run() ;
- sub default_server_type { 'Fork' }
- sub post_configure_hook
- {
- my $self = shift ;
- $self->log( 2, Data::Dumper->Dump( [ $self ], ['self'] ) ) ;
- }
- sub output_file
- {
- my ( $self, $file, $type ) = @_ ;
-
- $type ||= 'text/plain' ;
-
- my $string = file_to_string( $file ) ;
-
- my $output ;
- my( $status, $msg, $body ) ;
- if ( defined $string )
- {
- $output = "Content-type: $type\r\n\r\n" ;
- $output .= $string ;
- # body can not be sent by send_status() because then it
- # sets Content-type to text/html
- $self->send_status( '200', 'OK' ) ;
- print $output ;
-
- }
- else
- {
- $self->send_status( '404', 'Not found', "File not found: $file " ) ;
- }
-
- return ;
- }
- sub process_path_info
- {
- my $self = shift ;
- my $path_info = shift ;
-
- my $sitemap =
- {
- '/imapsync_form_extra.html' => sub {
- output_file( $self, './X/imapsync_form_extra.html', 'text/html' )
- },
- '/imapsync_form.html' => sub {
- output_file( $self, './X/imapsync_form.html', 'text/html' )
- },
- '/imapsync_form.css' => sub {
- output_file( $self, './X/imapsync_form.css', 'text/css' )
- },
- '/imapsync_form.js' => sub {
- output_file( $self, './X/imapsync_form.js', 'text/javascript' )
- },
- '/imapsync_form_new.js' => sub {
- output_file( $self, './X/imapsync_form_new.js', 'text/javascript' )
- },
- '/' => sub {
- output_file( $self, './X/imapsync_form_extra.html', 'text/html' )
- },
- '/vnstat/vnstati.html' => sub {
- output_file( $self, './X/vnstati.html', 'text/html' )
- },
-
- } ;
-
- if ( defined $sitemap->{ $path_info } )
- {
- $sitemap->{ $path_info }->() ;
- }
- else
- {
- $self->send_status( '404', 'Not found', "Error: $path_info not found!\n" ) ;
- }
- return ;
- }
- sub process_http_request
- {
- my $self = shift ;
-
- $self->log( 2, "In process_http_request PID $$\n" ) ;
- local $Data::Dumper::Sortkeys = 1;
- #$self->log( 2, Data::Dumper->Dump( [ $self ], ['self'] ) ) ;
- $ENV{'SERVER_SOFTWARE'} = $PROGRAM_NAME ;
- if ( '/cgi-bin/imapsync' eq $ENV{'PATH_INFO'} ) {
- #$self->exec_trusted_perl( './imapsync' ) ;
- $self->exec_cgi( './imapsync' ) ;
- #$self->exec_cgi( './imapsync_bin_Linux_i686' ) ;
-
- #$self->exec_trusted_perl( '.\imapsync.pl' );
- $self->log( 2, "In process_http_request PID $$ after exec_trusted_perl\n" ) ;
- #return ;
- #return $self->exec_cgi( 'C:\Strawberry\perl\bin\perl.exe .\imapsync.pl' );
- #return $self->exec_cgi( 'imapsyncbat' );
- #return $self->exec_trusted_perl( 'imapsyncbat' );
-
- }
- else
- {
- process_path_info( $self, $ENV{'PATH_INFO'} ) ;
- }
- #$self->log( 4, Data::Dumper->Dump( [ $self ], ['self'] ) ) ;
- $self->log( 2, "End of process_http_request PID $$\n" ) ;
- }
- sub file_to_string
- {
- my $file = shift ;
- if ( ! $file ) { warn "Error, no file given\n" ; return ; }
- if ( ! -e $file ) { warn "Error, $file does not exist\n" ; return ; }
- if ( ! -f $file ) { warn "Error, $file is not a file\n" ; return ; }
- if ( ! -r $file ) { warn "Error, $file is not readable\n" ; return ; }
- my @string ;
- if ( open my $FILE, '<', $file ) {
- @string = <$FILE> ;
- close $FILE ;
- my $string = join q{}, @string ;
- return $string ;
- }else{
- warn "Error reading file $file : $OS_ERROR\n" ;
- return ;
- }
- }
|