forked from imapsync/imapsync
-
Notifications
You must be signed in to change notification settings - Fork 0
/
webserver
executable file
·156 lines (125 loc) · 4.61 KB
/
webserver
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
#!/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 ;
}
}