Debian Codegraph @

tests/SSLServer.pm

raw
1 package SSLServer;
2
3 # This is only HTTPS server for now.
4 # But it is named SSLServer to easily distinguish from HTTPServer
5
6 use strict;
7 use warnings;
8 use lib '.';
9
10 use HTTP::Daemon;
11 use HTTP::Status;
12 use HTTP::Headers;
13 use HTTP::Response;
14
15 # Skip this test rather than fail it when the module isn't installed
16 if (!eval {require IO::Socket::SSL;1;}) {
17 print STDERR "This test needs the perl module \"IO::Socket::SSL\".\n";
18 print STDERR "Install e.g. on Debian with 'apt-get install libio-socket-ssl-perl'\n";
19 print STDERR " or if using cpanminus 'cpanm IO::Socket::SSL' could be used to install it.\n";
20 exit 77; # skip
21 }
22
23 #use IO::Socket::SSLX; # 'debug4';
24 use HTTPServer;
25
26 our @ISA = qw(IO::Socket::SSL HTTP::Daemon::ClientConn HTTP::Daemon HTTPServer);
27
28 my $VERSION = 0.01;
29
30 my $CRLF = "\015\012"; # "\r\n" is not portable
31
32 # Config options for server
33 my $log = undef;
34 my $DEBUG = undef;
35
36 my %ssl_params;
37
38 my $sslsock;
39 my $plaincon;
40 my %args;
41
42 #$HTTP::Daemon::DEBUG=5;
43 #*DEBUG = \$HTTP::Daemon::DEBUG;
44
45 $args{SSL_error_trap} ||= \&ssl_error;
46
47 my $class = 'SSLServer';
48 my $self = {};
49 $self = bless $self, $class;
50
51 sub init
52 {
53 my $self = shift;
54 my %sargs = @_;
55
56 %ssl_params = %sargs;
57 unless (exists($ssl_params{'lhostname'}) &&
58 exists($ssl_params{'sslport'}) &&
59 exists($ssl_params{'ciphers'}) &&
60 exists($ssl_params{'cafile'}) &&
61 exists($ssl_params{'certfile'}) &&
62 exists($ssl_params{'keyfile'})) {
63 die "Required parameters for SSL tests are missing";
64 }
65 }
66
67 sub ssl_setup_conn
68 {
69 $sslsock = IO::Socket::SSL->new(LocalAddr => $ssl_params{'lhostname'},
70 LocalPort => $ssl_params{'sslport'},
71 Listen => 10,
72 Timeout => 30,
73 ReuseAddr => 1,
74 SSL_cipher_list => $ssl_params{'ciphers'},
75 SSL_verify_mode => 0x00,
76 SSL_ca_file => $ssl_params{'cafile'},
77 SSL_cert_file => $ssl_params{'certfile'},
78 SSL_key_file => $ssl_params{'keyfile'});
79
80 $sslsock || warn $IO::Socket::SSL::ERROR;
81 return $sslsock;
82 }
83
84 sub fileno
85 {
86 my $self = shift;
87 my $fn = ${*$self}{'_SSL_fileno'};
88 return defined($fn) ? $fn : $self->SUPER::fileno();
89 }
90
91 sub accept
92 {
93 my $self = shift;
94 my $pkg = shift || "SSLServer";
95 my ($sock, $peer) = $sslsock->accept($pkg);
96 if ($sock) {
97 ${*$sock}{'httpd_daemon'} = $self;
98 ${*$self}{'httpd_daemon'} = $sock;
99 my $fileno = ${*$self}{'_SSL_fileno'} = &fileno($self);
100 my $f = $sock->fileno;
101 return wantarray ? ($sock, $peer) : $sock;
102 }
103 else {
104 print STDERR "Failed to get socket from SSL\n" if $DEBUG;
105 return;
106 }
107
108 }
109
110 sub _default_port { 443; }
111 sub _default_scheme { "https"; }
112
113 sub url
114 {
115 my $self = shift;
116 my $url = $self->SUPER::url;
117 return $url if ($self->can("HTTP::Daemon::_default_port"));
118
119 # Workaround for old versions of HTTP::Daemon
120 $url =~ s!^http:!https:!;
121 $url =~ s!/$!:80/! unless ($url =~ m!:(?:\d+)/$!);
122 $url =~ s!:443/$!/!;
123 return $url;
124 }
125
126 sub _need_more
127 {
128 my $self = shift;
129 if ($_[1]) {
130 my($timeout, $fdset) = @_[1,2];
131 print STDERR "select(,,,$timeout)\n" if $DEBUG;
132 my $n = select($fdset,undef,undef,$timeout);
133 unless ($n) {
134 $self->reason(defined($n) ? "Timeout" : "select: $!");
135 return;
136 }
137 }
138 my $total = 0;
139 while (1){
140 print STDERR sprintf("sysread() already %d\n",$total) if $DEBUG;
141 my $n = sysread(${*$self}{'httpd_daemon'}, $_[0], 2048, length($_[0]));
142 print STDERR sprintf("sysread() just \$n=%s\n",(defined $n?$n:'undef')) if $DEBUG;
143 $total += $n if defined $n;
144 last if $! =~ 'Resource temporarily unavailable';
145 #SSL_Error because of aggressive reading
146
147 $self->reason(defined($n) ? "Client closed" : "sysread: $!") unless $n;
148 last unless $n;
149 last unless $n == 2048;
150 }
151 $total;
152 }
153
154 sub daemon
155 {
156 my $self = shift;
157 ${*$self}{'httpd_daemon'};
158 }
159
160 sub conn
161 {
162 my $self = shift;
163 ${*$self}{'sslcon'};
164 }
165
166 sub run
167 {
168 my ($self, $urls, $synch_callback) = @_;
169 my $initialized = 0;
170 my $sslsock;
171
172 while (1)
173 {
174 if (!$initialized)
175 {
176 $sslsock = $self->ssl_setup_conn();
177 $sslsock || warn "Failed to get ssl sock";
178
179 $initialized = 1;
180 open (LOGFILE, '>', "/tmp/wgetserver.log");
181 LOGFILE->autoflush(1);
182 print LOGFILE "Starting logging";
183 $synch_callback->() if $synch_callback;
184 }
185
186 my $con = $self->accept();
187 ${*$self}{'sslcon'} = $con;
188
189 while (my $req = $self->get_request)
190 {
191 #my $url_path = $req->url->path;
192 my $url_path = $req->url->as_string;
193 if ($url_path =~ m{/$})
194 { # append 'index.html'
195 $url_path .= 'index.html';
196 }
197
198 #if ($url_path =~ m{^/}) { # remove trailing '/'
199 # $url_path = substr ($url_path, 1);
200 #}
201 if ($log)
202 {
203 print LOGFILE "Method: ", $req->method, "\n";
204 print LOGFILE "Path: ", $url_path, "\n";
205 print LOGFILE "Available URLs: ", "\n";
206 foreach my $key (keys %$urls)
207 {
208 print LOGFILE $key, "\n";
209 }
210 }
211 if (exists($urls->{$url_path}))
212 {
213 print LOGFILE "Serving requested URL: ", $url_path, "\n" if $log;
214 next unless ($req->method eq "HEAD" || $req->method eq "GET");
215
216 my $url_rec = $urls->{$url_path};
217 HTTPServer::send_response($self, $req, $url_rec, $con);
218 last;
219 }
220 else
221 {
222 print LOGFILE "Requested wrong URL: ", $url_path, "\n" if $log;
223 $con->send_error($HTTP::Status::RC_FORBIDDEN);
224 last;
225 }
226 last;
227 }
228 print LOGFILE "Closing connection\n" if $log;
229 close(LOGFILE);
230 $con->close();
231 }
232 }
233
234 1;
235
236 # vim: et ts=4 sw=4