webserver 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. #!/usr/bin/perl
  2. # $Id: webserver,v 1.6 2019/11/28 14:45:09 gilles Exp gilles $
  3. package Imapsync;
  4. use base qw(Net::Server::HTTP);
  5. use strict ;
  6. use warnings ;
  7. use Data::Dumper ;
  8. use English qw( -no_match_vars ) ;
  9. my $server = Imapsync->new(
  10. 'port' => [8080],
  11. 'access_log_file' => 'STDERR',
  12. 'log_level' => 4,
  13. 'timeout_header' => 20,
  14. 'timeout_idle' => 60,
  15. ) ;
  16. $server->run() ;
  17. sub default_server_type { 'Fork' }
  18. sub post_configure_hook
  19. {
  20. my $self = shift ;
  21. $self->log( 2, Data::Dumper->Dump( [ $self ], ['self'] ) ) ;
  22. }
  23. sub output_file
  24. {
  25. my ( $self, $file, $type ) = @_ ;
  26. $type ||= 'text/plain' ;
  27. my $string = file_to_string( $file ) ;
  28. my $output ;
  29. my( $status, $msg, $body ) ;
  30. if ( defined $string )
  31. {
  32. $output = "Content-type: $type\r\n\r\n" ;
  33. $output .= $string ;
  34. # body can not be sent by send_status() because then it
  35. # sets Content-type to text/html
  36. $self->send_status( '200', 'OK' ) ;
  37. print $output ;
  38. }
  39. else
  40. {
  41. $self->send_status( '404', 'Not found', "File not found: $file " ) ;
  42. }
  43. return ;
  44. }
  45. sub process_path_info
  46. {
  47. my $self = shift ;
  48. my $path_info = shift ;
  49. my $sitemap =
  50. {
  51. '/imapsync_form_extra.html' => sub {
  52. output_file( $self, './X/imapsync_form_extra.html', 'text/html' )
  53. },
  54. '/imapsync_form.html' => sub {
  55. output_file( $self, './X/imapsync_form.html', 'text/html' )
  56. },
  57. '/imapsync_form.css' => sub {
  58. output_file( $self, './X/imapsync_form.css', 'text/css' )
  59. },
  60. '/imapsync_form.js' => sub {
  61. output_file( $self, './X/imapsync_form.js', 'text/javascript' )
  62. },
  63. '/imapsync_form_new.js' => sub {
  64. output_file( $self, './X/imapsync_form_new.js', 'text/javascript' )
  65. },
  66. '/' => sub {
  67. output_file( $self, './X/imapsync_form_extra.html', 'text/html' )
  68. },
  69. '/vnstat/vnstati.html' => sub {
  70. output_file( $self, './X/vnstati.html', 'text/html' )
  71. },
  72. } ;
  73. if ( defined $sitemap->{ $path_info } )
  74. {
  75. $sitemap->{ $path_info }->() ;
  76. }
  77. else
  78. {
  79. $self->send_status( '404', 'Not found', "Error: $path_info not found!\n" ) ;
  80. }
  81. return ;
  82. }
  83. sub process_http_request
  84. {
  85. my $self = shift ;
  86. $self->log( 2, "In process_http_request PID $$\n" ) ;
  87. local $Data::Dumper::Sortkeys = 1;
  88. #$self->log( 2, Data::Dumper->Dump( [ $self ], ['self'] ) ) ;
  89. $ENV{'SERVER_SOFTWARE'} = $PROGRAM_NAME ;
  90. if ( '/cgi-bin/imapsync' eq $ENV{'PATH_INFO'} ) {
  91. #$self->exec_trusted_perl( './imapsync' ) ;
  92. $self->exec_cgi( './imapsync' ) ;
  93. #$self->exec_cgi( './imapsync_bin_Linux_i686' ) ;
  94. #$self->exec_trusted_perl( '.\imapsync.pl' );
  95. $self->log( 2, "In process_http_request PID $$ after exec_trusted_perl\n" ) ;
  96. #return ;
  97. #return $self->exec_cgi( 'C:\Strawberry\perl\bin\perl.exe .\imapsync.pl' );
  98. #return $self->exec_cgi( 'imapsyncbat' );
  99. #return $self->exec_trusted_perl( 'imapsyncbat' );
  100. }
  101. else
  102. {
  103. process_path_info( $self, $ENV{'PATH_INFO'} ) ;
  104. }
  105. #$self->log( 4, Data::Dumper->Dump( [ $self ], ['self'] ) ) ;
  106. $self->log( 2, "End of process_http_request PID $$\n" ) ;
  107. }
  108. sub file_to_string
  109. {
  110. my $file = shift ;
  111. if ( ! $file ) { warn "Error, no file given\n" ; return ; }
  112. if ( ! -e $file ) { warn "Error, $file does not exist\n" ; return ; }
  113. if ( ! -f $file ) { warn "Error, $file is not a file\n" ; return ; }
  114. if ( ! -r $file ) { warn "Error, $file is not readable\n" ; return ; }
  115. my @string ;
  116. if ( open my $FILE, '<', $file ) {
  117. @string = <$FILE> ;
  118. close $FILE ;
  119. my $string = join q{}, @string ;
  120. return $string ;
  121. }else{
  122. warn "Error reading file $file : $OS_ERROR\n" ;
  123. return ;
  124. }
  125. }