1 package PVE
::HTTPServer
;
5 use Time
::HiRes
qw(usleep ualarm gettimeofday tv_interval);
6 use Socket
qw(IPPROTO_TCP TCP_NODELAY SOMAXCONN);
7 use POSIX
qw(strftime EINTR EAGAIN);
12 # use AnyEvent::Strict; # only use this for debugging
13 use AnyEvent
::Util
qw(guard fh_nonblocking WSAEWOULDBLOCK WSAEINPROGRESS);
24 use PVE
::RPCEnvironment
;
29 use HTTP
::Status
qw(:constants);
34 my $limit_max_headers = 30;
35 my $limit_max_header_size = 8*1024;
36 my $limit_max_post = 16*1024;
46 my $baseuri = "/api2";
51 my ($format, $rel_uri) = $abs_uri =~ m/^\Q$baseuri\E\/+(html
|text
|json
|extjs
|png
|htmljs
|spiceconfig
)(\
/.*)?$/;
52 $rel_uri = '/' if !$rel_uri;
54 return wantarray ?
($rel_uri, $format) : $rel_uri;
58 my ($self, $reqstate) = @_;
60 my $loginfo = $reqstate->{log};
62 # like apache2 common log format
63 # LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-agent}i\""
65 return if $loginfo->{written
}; # avoid duplicate logs
66 $loginfo->{written
} = 1;
68 my $peerip = $reqstate->{peer_host
} || '-';
69 my $userid = $loginfo->{userid
} || '-';
70 my $content_length = defined($loginfo->{content_length
}) ?
$loginfo->{content_length
} : '-';
71 my $code = $loginfo->{code
} || 500;
72 my $requestline = $loginfo->{requestline
} || '-';
73 my $timestr = strftime
("%d/%b/%Y:%H:%M:%S %z", localtime());
75 my $msg = "$peerip - $userid [$timestr] \"$requestline\" $code $content_length\n";
77 $self->write_log($msg);
80 sub log_aborted_request {
81 my ($self, $reqstate, $error) = @_;
83 my $r = $reqstate->{request};
84 return if !$r; # no active request
87 syslog("err
", "problem with client
$reqstate->{peer_host
}; $error");
90 $self->log_request($reqstate);
93 sub cleanup_reqstate {
96 delete $reqstate->{log};
97 delete $reqstate->{request};
98 delete $reqstate->{proto};
99 delete $reqstate->{accept_gzip};
100 delete $reqstate->{starttime};
102 if ($reqstate->{tmpfilename}) {
103 unlink $reqstate->{tmpfilename};
104 delete $reqstate->{tmpfilename};
108 sub client_do_disconnect {
109 my ($self, $reqstate) = @_;
111 cleanup_reqstate($reqstate);
113 my $shutdown_hdl = sub {
116 shutdown($hdl->{fh}, 1);
118 $hdl->on_drain(undef);
119 $hdl->on_read(undef);
123 if (my $proxyhdl = delete $reqstate->{proxyhdl}) {
124 &$shutdown_hdl($proxyhdl);
127 my $hdl = delete $reqstate->{hdl};
130 syslog('err', "detected empty handle
");
134 print "close connection
$hdl\n" if $self->{debug};
136 &$shutdown_hdl($hdl);
138 $self->{conn_count}--;
140 print "$$: CLOSE FH
" . $hdl->{fh}->fileno() . " CONN
$self->{conn_count
}\n" if $self->{debug};
143 sub finish_response {
144 my ($self, $reqstate) = @_;
146 my $hdl = $reqstate->{hdl};
148 cleanup_reqstate($reqstate);
150 if (!$self->{end_loop} && $reqstate->{keep_alive} > 0) {
151 # print "KEEPALIVE
$reqstate->{keep_alive
}\n" if $self->{debug};
153 eval { $self->push_request_header($reqstate); };
157 $hdl->on_drain (sub {
159 $self->client_do_disconnect($reqstate);
167 my ($self, $reqstate, $resp, $mtime, $nocomp, $delay) = @_;
169 #print "$$: send response
: " . Dumper($resp);
172 $reqstate->{hdl}->timeout_reset();
173 $reqstate->{hdl}->timeout($self->{timeout});
175 $nocomp = 1 if !$reqstate->{accept_gzip};
177 my $code = $resp->code;
178 my $msg = $resp->message || HTTP::Status::status_message($code);
179 ($msg) = $msg =~m/^(.*)$/m;
180 my $content = $resp->content;
182 if ($code =~ /^(1\d\d|[23]04)$/) {
183 # make sure content we have no content
187 $reqstate->{keep_alive} = 0 if ($code >= 400) || $self->{end_loop};
189 $reqstate->{log}->{code} = $code;
191 my $proto = $reqstate->{proto} ? $reqstate->{proto}->{str} : 'HTTP/1.0';
192 my $res = "$proto $code $msg\015\012";
195 my $date = HTTP::Date::time2str($ctime);
196 $resp->header('Date' => $date);
198 $resp->header('Last-Modified' => HTTP::Date::time2str($mtime));
200 $resp->header('Expires' => $date);
201 $resp->header('Cache-Control' => "max-age
=0");
202 $resp->header("Pragma
", "no-cache
");
205 $resp->header('Server' => "pve-api-daemon
/3.0");
210 $content_length = length($content);
212 if (!$nocomp && ($content_length > 1024)) {
213 my $comp = Compress::Zlib::memGzip($content);
214 $resp->header('Content-Encoding', 'gzip');
216 $content_length = length($content);
218 $resp->header("Content-Length
" => $content_length);
219 $reqstate->{log}->{content_length} = $content_length;
222 $resp->remove_header("Content-Length
");
225 if ($reqstate->{keep_alive} > 0) {
226 $resp->push_header('Connection' => 'Keep-Alive');
228 $resp->header('Connection' => 'close');
231 $res .= $resp->headers_as_string("\015\012");
232 #print "SEND
(without content
) $res\n" if $self->{debug};
235 $res .= $content if $content;
237 $self->log_request($reqstate, $reqstate->{request});
239 if ($delay && $delay > 0) {
240 my $w; $w = AnyEvent->timer(after => $delay, cb => sub {
241 undef $w; # delete reference
242 $reqstate->{hdl}->push_write($res);
243 $self->finish_response($reqstate);
246 $reqstate->{hdl}->push_write($res);
247 $self->finish_response($reqstate);
252 my ($self, $reqstate, $code, $msg, $hdr, $content) = @_;
255 my $resp = HTTP::Response->new($code, $msg, $hdr, $content);
256 $self->response($reqstate, $resp);
261 sub send_file_start {
262 my ($self, $reqstate, $filename) = @_;
265 # print "SEND FILE
$filename\n";
266 # Note: aio_load() this is not really async unless we use IO::AIO!
269 my $r = $reqstate->{request};
271 my $fh = IO::File->new($filename, '<') ||
273 my $stat = File::stat::stat($fh) ||
276 my $mtime = $stat->mtime;
278 if (my $ifmod = $r->header('if-modified-since')) {
279 my $iftime = HTTP::Date::str2time($ifmod);
280 if ($mtime <= $iftime) {
281 my $resp = HTTP::Response->new(304, "NOT MODIFIED
");
282 $self->response($reqstate, $resp, $mtime);
288 my $len = sysread($fh, $data, $stat->size);
289 die "got short file
\n" if !defined($len) || $len != $stat->size;
293 if ($filename =~ m/\.css$/) {
295 } elsif ($filename =~ m/\.js$/) {
296 $ct = 'application/javascript';
297 } elsif ($filename =~ m/\.png$/) {
300 } elsif ($filename =~ m/\.ico$/) {
301 $ct = 'image/x-icon';
303 } elsif ($filename =~ m/\.gif$/) {
306 } elsif ($filename =~ m/\.jar$/) {
307 $ct = 'application/java-archive';
310 die "unable to detect content type
";
313 my $header = HTTP::Headers->new(Content_Type => $ct);
314 my $resp = HTTP::Response->new(200, "OK
", $header, $data);
315 $self->response($reqstate, $resp, $mtime, $nocomp);
318 $self->error($reqstate, 501, $err);
326 my ($self, $reqstate, $clientip, $host, $method, $uri, $ticket, $token, $params) = @_;
331 if ($host eq 'localhost') {
332 $target = "http
://$host:85$uri";
333 # keep alive for localhost is not worth (connection setup is about 0.2ms)
336 $target = "https
://$host:8006$uri";
340 PVEDisableProxy => 'true',
341 PVEClientIP => $clientip,
344 my $cookie_name = 'PVEAuthCookie';
346 $headers->{'cookie'} = PVE::REST::create_auth_cookie($ticket) if $ticket;
347 $headers->{'CSRFPreventionToken'} = $token if $token;
348 $headers->{'Accept-Encoding'} = 'gzip' if $reqstate->{accept_gzip};
352 if ($method eq 'POST' || $method eq 'PUT') {
353 $headers->{'Content-Type'} = 'application/x-www-form-urlencoded';
354 # use URI object to format application/x-www-form-urlencoded content.
355 my $url = URI->new('http:');
356 $url->query_form(%$params);
357 $content = $url->query;
358 if (defined($content)) {
359 $headers->{'Content-Length'} = length($content);
363 my $w; $w = http_request(
368 proxy => undef, # avoid use of $ENV{HTTP_PROXY}
369 keepalive => $keep_alive,
371 tls_ctx => $self->{tls_ctx},
373 my ($body, $hdr) = @_;
377 if (!$reqstate->{hdl}) {
378 warn "proxy detected vanished client connection
\n";
383 my $code = delete $hdr->{Status};
384 my $msg = delete $hdr->{Reason};
386 delete $hdr->{HTTPVersion};
387 my $header = HTTP::Headers->new(%$hdr);
388 my $resp = HTTP::Response->new($code, $msg, $header, $body);
389 # Note: disable compression, because body is already compressed
390 $self->response($reqstate, $resp, undef, 1);
398 # return arrays as \0 separated strings (like CGI.pm)
399 sub decode_urlencoded {
404 return $res if !$data;
406 foreach my $kv (split(/[\&\;]/, $data)) {
407 my ($k, $v) = split(/=/, $kv);
409 $k =~ s/%([0-9a-fA-F][0-9a-fA-F])/chr(hex($1))/eg;
411 $v =~ s/%([0-9a-fA-F][0-9a-fA-F])/chr(hex($1))/eg;
413 if (defined(my $old = $res->{$k})) {
414 $res->{$k} = "$old\0$v";
423 my ($r, $method) = @_;
427 if ($method eq 'PUT' || $method eq 'POST') {
428 $params = decode_urlencoded($r->content);
431 my $query_params = decode_urlencoded($r->url->query());
433 foreach my $k (keys %{$query_params}) {
434 $params->{$k} = $query_params->{$k};
437 return PVE::Tools::decode_utf8_parameters($params);
440 sub handle_api2_request {
441 my ($self, $reqstate, $auth, $upload_state) = @_;
444 my $r = $reqstate->{request};
445 my $method = $r->method();
446 my $path = $r->uri->path();
448 my ($rel_uri, $format) = split_abs_uri($path);
450 $self->error($reqstate, HTTP_NOT_IMPLEMENTED, "no such uri
");
454 #print Dumper($upload_state) if $upload_state;
456 my $rpcenv = $self->{rpcenv};
461 $params = $upload_state->{params};
463 $params = extract_params($r, $method);
466 delete $params->{_dc}; # remove disable cache parameter
468 my $clientip = $reqstate->{peer_host};
470 $rpcenv->init_request();
472 my $res = PVE::REST::rest_handler($rpcenv, $clientip, $method, $rel_uri, $auth, $params);
474 AnyEvent->now_update(); # in case somebody called sleep()
476 $rpcenv->set_user(undef); # clear after request
478 if (my $host = $res->{proxy}) {
480 if ($self->{trusted_env}) {
481 $self->error($reqstate, HTTP_INTERNAL_SERVER_ERROR, "proxy
not allowed
");
485 if ($host ne 'localhost' && $r->header('PVEDisableProxy')) {
486 $self->error($reqstate, HTTP_INTERNAL_SERVER_ERROR, "proxy
loop detected
");
490 $res->{proxy_params}->{tmpfilename} = $reqstate->{tmpfilename} if $upload_state;
492 $self->proxy_request($reqstate, $clientip, $host, $method,
493 $r->uri, $auth->{ticket}, $auth->{token}, $res->{proxy_params});
499 if ($res->{status} == HTTP_UNAUTHORIZED) {
500 # always delay unauthorized calls by 3 seconds
501 $delay = 3 - tv_interval($reqstate->{starttime});
502 $delay = 0 if $delay < 0;
505 PVE::REST::prepare_response_data($format, $res);
506 my ($raw, $ct, $nocomp) = PVE::REST::format_response_data($format, $res, $path);
508 my $resp = HTTP::Response->new($res->{status}, $res->{message});
509 $resp->header("Content-Type
" => $ct);
510 $resp->content($raw);
511 $self->response($reqstate, $resp, undef, $nocomp, $delay);
514 $self->error($reqstate, 501, $err);
518 sub handle_spice_proxy_request {
519 my ($self, $reqstate, $connect_str, $vmid, $node, $spiceport) = @_;
523 die "Port
$spiceport is not allowed
" if ($spiceport < 61000 || $spiceport > 61099);
525 my $rpcenv = $self->{rpcenv};
526 $rpcenv->init_request();
528 my $clientip = $reqstate->{peer_host};
529 my $r = $reqstate->{request};
533 if ($node ne 'localhost' && PVE::INotify::nodename() !~ m/^$node$/i) {
534 $remip = PVE::Cluster::remote_node_ip($node);
535 die "unable to get remote IP address
for node
'$node'\n" if !$remip;
536 print "REMOTE CONNECT
$vmid, $remip, $connect_str\n" if $self->{debug};
538 print "$$: CONNECT
$vmid, $node, $spiceport\n" if $self->{debug};
541 if ($remip && $r->header('PVEDisableProxy')) {
542 $self->error($reqstate, HTTP_INTERNAL_SERVER_ERROR, "proxy
loop detected
");
546 $reqstate->{hdl}->timeout(0);
547 $reqstate->{hdl}->wbuf_max(64*10*1024);
549 my $remhost = $remip ? $remip : "127.0.0.1";
550 my $remport = $remip ? 3128 : $spiceport;
552 tcp_connect $remhost, $remport, sub {
554 or die "connect to
'$remhost:$remport' failed
: $!";
556 print "$$: CONNECTed to
'$remhost:$remport'\n" if $self->{debug};
557 $reqstate->{proxyhdl} = AnyEvent::Handle->new(
560 wbuf_max => 64*10*1024,
565 $self->log_aborted_request($reqstate);
566 $self->client_do_disconnect($reqstate);
568 if (my $err = $@) { syslog('err', $err); }
571 my ($hdl, $fatal, $message) = @_;
573 $self->log_aborted_request($reqstate, $message);
574 $self->client_do_disconnect($reqstate);
576 if (my $err = $@) { syslog('err', "$err"); }
580 my $proxyhdlreader = sub {
583 my $len = length($hdl->{rbuf});
584 my $data = substr($hdl->{rbuf}, 0, $len, '');
586 #print "READ1
$len\n";
587 $reqstate->{hdl}->push_write($data) if $reqstate->{hdl};
590 my $hdlreader = sub {
593 my $len = length($hdl->{rbuf});
594 my $data = substr($hdl->{rbuf}, 0, $len, '');
596 #print "READ0
$len\n";
597 $reqstate->{proxyhdl}->push_write($data) if $reqstate->{proxyhdl};
600 my $proto = $reqstate->{proto} ? $reqstate->{proto}->{str} : 'HTTP/1.0';
602 my $startproxy = sub {
603 $reqstate->{proxyhdl}->timeout(0);
604 $reqstate->{proxyhdl}->on_read($proxyhdlreader);
605 $reqstate->{hdl}->on_read($hdlreader);
607 # todo: use stop_read/start_read if write buffer grows to much
609 my $res = "$proto 200 OK
\015\012"; # hope this is the right answer?
610 $reqstate->{hdl}->push_write($res);
613 $reqstate->{log}->{code} = 200;
614 $self->log_request($reqstate);
618 my $header = "CONNECT
${connect_str
} $proto\015\012" .
619 "Host
: ${connect_str
}\015\012" .
620 "Proxy-Connection
: keep-alive
\015\012" .
621 "User-Agent
: spiceproxy
\015\012" .
622 "PVEDisableProxy
: true
\015\012" .
623 "PVEClientIP
: $clientip\015\012" .
626 $reqstate->{proxyhdl}->push_write($header);
627 $reqstate->{proxyhdl}->push_read(line => sub {
628 my ($hdl, $line) = @_;
630 if ($line =~ m!^$proto 200 OK$!) {
633 $reqstate->{hdl}->push_write($line);
634 $self->client_do_disconnect($reqstate);
645 $self->log_aborted_request($reqstate, $err);
646 $self->client_do_disconnect($reqstate);
651 my ($self, $reqstate, $auth) = @_;
654 my $r = $reqstate->{request};
655 my $method = $r->method();
656 my $path = $r->uri->path();
658 # disable timeout on handle (we already have all data we need)
659 # we re-enable timeout in response()
660 $reqstate->{hdl}->timeout(0);
662 if ($path =~ m!$baseuri!) {
663 $self->handle_api2_request($reqstate, $auth);
667 if ($self->{pages} && ($method eq 'GET') && (my $handler = $self->{pages}->{$path})) {
668 if (ref($handler) eq 'CODE') {
669 my $params = decode_urlencoded($r->url->query());
670 my ($resp, $userid) = &$handler($self, $reqstate->{request}, $params);
671 $self->response($reqstate, $resp);
672 } elsif (ref($handler) eq 'HASH') {
673 if (my $filename = $handler->{file}) {
674 my $fh = IO::File->new($filename) ||
675 die "unable to
open file
'$filename' - $!\n";
676 send_file_start($self, $reqstate, $filename);
678 die "internal error
- no handler
";
681 die "internal error
- no handler
";
686 if ($self->{dirs} && ($method eq 'GET')) {
687 # we only allow simple names
688 if ($path =~ m!^(/\S+/)([a-zA-Z0-9\-\_\.]+)$!) {
689 my ($subdir, $file) = ($1, $2);
690 if (my $dir = $self->{dirs}->{$subdir}) {
691 my $filename = "$dir$file";
692 my $fh = IO::File->new($filename) ||
693 die "unable to
open file
'$filename' - $!\n";
694 send_file_start($self, $reqstate, $filename);
700 die "no such file
'$path'";
703 $self->error($reqstate, 501, $err);
707 sub file_upload_multipart {
708 my ($self, $reqstate, $auth, $rstate) = @_;
711 my $boundary = $rstate->{boundary};
712 my $hdl = $reqstate->{hdl};
714 my $startlen = length($hdl->{rbuf});
716 if ($rstate->{phase} == 0) { # skip everything until start
717 if ($hdl->{rbuf} =~ s/^.*?--\Q$boundary\E \015?\012
718 ((?:[^\015]+\015\012)* ) \015?\012//xs) {
720 my ($ct, $disp, $name, $filename);
721 foreach my $line (split(/\015?\012/, $header)) {
722 # assume we have single line headers
723 if ($line =~ m/^Content-Type\s*:\s*(.*)/i) {
724 $ct = parse_content_type($1);
725 } elsif ($line =~ m/^Content-Disposition\s*:\s*(.*)/i) {
726 ($disp, $name, $filename) = parse_content_disposition($1);
730 if (!($disp && $disp eq 'form-data' && $name)) {
731 syslog('err', "wrong content disposition
in multipart
- abort upload
");
732 $rstate->{phase} = -1;
735 $rstate->{fieldname} = $name;
738 if ($name eq 'filename') {
739 # found file upload data
740 $rstate->{phase} = 1;
741 $rstate->{filename} = $filename;
743 syslog('err', "wrong field name
for file upload
- abort upload
");
744 $rstate->{phase} = -1;
747 # found form data for field $name
748 $rstate->{phase} = 2;
752 my $len = length($hdl->{rbuf});
753 substr($hdl->{rbuf}, 0, $len - $rstate->{maxheader}, '')
754 if $len > $rstate->{maxheader}; # skip garbage
756 } elsif ($rstate->{phase} == 1) { # inside file - dump until end marker
757 if ($hdl->{rbuf} =~ s/^(.*?)\015?\012(--\Q$boundary\E(--)? \015?\012(.*))$/$2/xs) {
758 my ($rest, $eof) = ($1, $3);
759 my $len = length($rest);
760 die "write to temporary file failed
- $!"
761 if syswrite($rstate->{outfh}, $rest) != $len;
762 $rstate->{ctx}->add($rest);
763 $rstate->{params}->{filename} = $rstate->{filename};
764 $rstate->{md5sum} = $rstate->{ctx}->hexdigest;
765 $rstate->{bytes} += $len;
766 $rstate->{phase} = $eof ? 100 : 0;
768 my $len = length($hdl->{rbuf});
769 my $wlen = $len - $rstate->{boundlen};
771 my $data = substr($hdl->{rbuf}, 0, $wlen, '');
772 die "write to temporary file failed
- $!"
773 if syswrite($rstate->{outfh}, $data) != $wlen;
774 $rstate->{bytes} += $wlen;
775 $rstate->{ctx}->add($data);
778 } elsif ($rstate->{phase} == 2) { # inside normal field
780 if ($hdl->{rbuf} =~ s/^(.*?)\015?\012(--\Q$boundary\E(--)? \015?\012(.*))$/$2/xs) {
781 my ($rest, $eof) = ($1, $3);
782 my $len = length($rest);
783 $rstate->{post_size} += $len;
784 if ($rstate->{post_size} < $limit_max_post) {
785 $rstate->{params}->{$rstate->{fieldname}} = $rest;
786 $rstate->{phase} = $eof ? 100 : 0;
788 syslog('err', "form data to large
- abort upload
");
789 $rstate->{phase} = -1; # skip
793 my $len = length($hdl->{rbuf});
794 substr($hdl->{rbuf}, 0, $len, ''); # empty rbuf
797 $rstate->{read} += ($startlen - length($hdl->{rbuf}));
799 if (!$rstate->{done} && ($rstate->{read} + length($hdl->{rbuf})) >= $rstate->{size}) {
800 $rstate->{done} = 1; # make sure we dont get called twice
801 if ($rstate->{phase} < 0 || !$rstate->{md5sum}) {
802 die "upload failed
\n";
804 my $elapsed = tv_interval($rstate->{starttime});
806 my $rate = int($rstate->{bytes}/($elapsed*1024*1024));
807 syslog('info', "multipart upload complete
" .
808 "(size
: %d time: %ds rate
: %.2fMiB
/s md5sum
: $rstate->{md5sum
})",
809 $rstate->{bytes}, $elapsed, $rate);
810 $self->handle_api2_request($reqstate, $auth, $rstate);
816 $self->error($reqstate, 501, $err);
820 sub parse_content_type {
823 my ($ct, @params) = split(/\s*[;,]\s*/o, $ctype);
825 foreach my $v (@params) {
826 if ($v =~ m/^\s*boundary\s*=\s*(\S+?)\s*$/o) {
827 return wantarray ? ($ct, $1) : $ct;
831 return wantarray ? ($ct) : $ct;
834 sub parse_content_disposition {
837 my ($disp, @params) = split(/\s*[;,]\s*/o, $line);
841 foreach my $v (@params) {
842 if ($v =~ m/^\s*name\s*=\s*(\S+?)\s*$/o) {
844 $name =~ s/^"(.*)"$/$1/;
845 } elsif ($v =~ m/^\s*filename\s*=\s*(.+?)\s*$/o) {
847 $filename =~ s/^"(.*)"$/$1/;
851 return wantarray ? ($disp, $name, $filename) : $disp;
854 my $tmpfile_seq_no = 0;
856 sub get_upload_filename {
857 # choose unpredictable tmpfile name
860 return "/var/tmp
/pveupload-
" . Digest::MD5::md5_hex($tmpfile_seq_no . time() . $$);
863 sub unshift_read_header {
864 my ($self, $reqstate, $state) = @_;
866 $state = { size => 0, count => 0 } if !$state;
868 $reqstate->{hdl}->unshift_read(line => sub {
869 my ($hdl, $line) = @_;
872 # print "$$: got header
: $line\n" if $self->{debug};
874 die "to many http header lines
\n" if ++$state->{count} >= $limit_max_headers;
875 die "http header too large
\n" if ($state->{size} += length($line)) >= $limit_max_header_size;
877 my $r = $reqstate->{request};
880 my $path = $r->uri->path();
881 my $method = $r->method();
883 $r->push_header($state->{key}, $state->{val})
886 if (!$known_methods->{$method}) {
887 my $resp = HTTP::Response->new(HTTP_NOT_IMPLEMENTED, "method '$method' not available
");
888 $self->response($reqstate, $resp);
892 my $conn = $r->header('Connection');
893 my $accept_enc = $r->header('Accept-Encoding');
894 $reqstate->{accept_gzip} = ($accept_enc && $accept_enc =~ m/gzip/) ? 1 : 0;
897 $reqstate->{keep_alive} = 0 if $conn =~ m/close/oi;
899 if ($reqstate->{proto}->{ver} < 1001) {
900 $reqstate->{keep_alive} = 0;
904 my $te = $r->header('Transfer-Encoding');
905 if ($te && lc($te) eq 'chunked') {
906 # Handle chunked transfer encoding
907 $self->error($reqstate, 501, "chunked transfer encoding
not supported
");
910 $self->error($reqstate, 501, "Unknown transfer encoding
'$te'");
914 my $pveclientip = $r->header('PVEClientIP');
916 # fixme: how can we make PVEClientIP header trusted?
917 if ($self->{trusted_env} && $pveclientip) {
918 $reqstate->{peer_host} = $pveclientip;
920 $r->header('PVEClientIP', $reqstate->{peer_host});
923 my $len = $r->header('Content-Length');
925 # header processing complete - authenticate now
928 if ($self->{spiceproxy}) {
929 my $connect_str = $r->header('Host');
930 my ($vmid, $node, $port) = PVE::AccessControl::verify_spice_connect_url($connect_str);
931 if (!(defined($vmid) && $node && $port)) {
932 $self->error($reqstate, HTTP_UNAUTHORIZED, "invalid ticket
");
935 $self->handle_spice_proxy_request($reqstate, $connect_str, $vmid, $node, $port);
937 } elsif ($path =~ m!$baseuri!) {
938 my $token = $r->header('CSRFPreventionToken');
939 my $cookie = $r->header('Cookie');
940 my $ticket = PVE::REST::extract_auth_cookie($cookie);
942 my ($rel_uri, $format) = split_abs_uri($path);
944 $self->error($reqstate, HTTP_NOT_IMPLEMENTED, "no such uri
");
949 $auth = PVE::REST::auth_handler($self->{rpcenv}, $reqstate->{peer_host}, $method,
950 $rel_uri, $ticket, $token);
953 $self->error($reqstate, HTTP_UNAUTHORIZED, $err);
958 $reqstate->{log}->{userid} = $auth->{userid};
962 if (!($method eq 'PUT' || $method eq 'POST')) {
963 $self->error($reqstate, 501, "Unexpected content
for method '$method'");
967 my $ctype = $r->header('Content-Type');
968 my ($ct, $boundary) = parse_content_type($ctype) if $ctype;
970 if ($auth->{isUpload} && !$self->{trusted_env}) {
971 die "upload
'Content-Type '$ctype' not implemented\n"
972 if !($boundary && $ct && ($ct eq 'multipart
/form-data
'));
974 die "upload without content length header not supported" if !$len;
976 die "upload without content length header not supported" if !$len;
978 print "start upload $path $ct $boundary\n" if $self->{debug};
980 my $tmpfilename = get_upload_filename();
981 my $outfh = IO::File->new($tmpfilename, O_RDWR|O_CREAT|O_EXCL, 0600) ||
982 die "unable to create temporary upload file '$tmpfilename'";
984 $reqstate->{keep_alive} = 0;
986 my $boundlen = length($boundary) + 8; # \015?\012--$boundary--\015?\012
990 boundary => $boundary,
991 ctx => Digest::MD5->new,
992 boundlen => $boundlen,
993 maxheader => 2048 + $boundlen, # should be large enough
994 params => decode_urlencoded($r->url->query()),
998 starttime => [gettimeofday],
1001 $reqstate->{tmpfilename} = $tmpfilename;
1002 $reqstate->{hdl}->on_read(sub { $self->file_upload_multipart($reqstate, $auth, $state); });
1006 if ($len > $limit_max_post) {
1007 $self->error($reqstate, 501, "for data too large");
1011 if (!$ct || $ct eq 'application
/x-www-form-urlencoded
') {
1012 $reqstate->{hdl}->unshift_read(chunk => $len, sub {
1013 my ($hdl, $data) = @_;
1015 $self->handle_request($reqstate, $auth);
1018 $self->error($reqstate, 506, "upload 'Content-Type
'$ctype' not implemented
");
1021 $self->handle_request($reqstate, $auth);
1023 } elsif ($line =~ /^([^:\s]+)\s*:\s*(.*)/) {
1024 $r->push_header($state->{key}, $state->{val}) if $state->{key};
1025 ($state->{key}, $state->{val}) = ($1, $2);
1026 $self->unshift_read_header($reqstate, $state);
1027 } elsif ($line =~ /^\s+(.*)/) {
1028 $state->{val} .= " $1";
1029 $self->unshift_read_header($reqstate, $state);
1031 $self->error($reqstate, 506, "unable to parse request header
");
1038 sub push_request_header {
1039 my ($self, $reqstate) = @_;
1042 $reqstate->{hdl}->push_read(line => sub {
1043 my ($hdl, $line) = @_;
1046 # print "got request header
: $line\n" if $self->{debug};
1048 $reqstate->{keep_alive}--;
1050 if ($line =~ /(\S+)\040(\S+)\040HTTP\/(\d+)\.(\d+)/o) {
1051 my ($method, $uri, $maj, $min) = ($1, $2, $3, $4);
1054 $self->error($reqstate, 506, "http protocol version
$maj.$min not supported
");
1058 $self->{request_count}++; # only count valid request headers
1059 if ($self->{request_count} >= $self->{max_requests}) {
1060 $self->{end_loop} = 1;
1062 $reqstate->{log} = { requestline => $line };
1063 $reqstate->{proto}->{str} = "HTTP
/$maj.$min";
1064 $reqstate->{proto}->{maj} = $maj;
1065 $reqstate->{proto}->{min} = $min;
1066 $reqstate->{proto}->{ver} = $maj*1000+$min;
1067 $reqstate->{request} = HTTP::Request->new($method, $uri);
1068 $reqstate->{starttime} = [gettimeofday],
1070 $self->unshift_read_header($reqstate);
1071 } elsif ($line eq '') {
1072 # ignore empty lines before requests (browser bugs?)
1073 $self->push_request_header($reqstate);
1075 $self->error($reqstate, 400, 'bad request');
1089 return if $self->{end_loop};
1091 # we need to m make sure that only one process calls accept
1092 while (!flock($self->{lockfh}, Fcntl::LOCK_EX())) {
1093 next if $! == EINTR;
1094 die "could
not get
lock on file
'$self->{lockfile}' - $!\n";
1100 while (!$self->{end_loop} &&
1101 !defined($clientfh = $self->{socket}->accept()) &&
1104 if ($self->{end_loop}) {
1107 $again = ($! == EAGAIN || $! == WSAEWOULDBLOCK);
1108 if (!defined($clientfh)) {
1109 $errmsg = "failed to
accept connection
: $!\n";
1115 flock($self->{lockfh}, Fcntl::LOCK_UN());
1117 if (!defined($clientfh)) {
1119 die $errmsg if $errmsg;
1122 fh_nonblocking $clientfh, 1;
1124 $self->{conn_count}++;
1132 $self->{end_loop} = 1;
1134 undef $self->{socket_watch};
1136 if ($self->{conn_count} <= 0) {
1137 $self->{end_cond}->send(1);
1141 # else we need to wait until all open connections gets closed
1142 my $w; $w = AnyEvent->timer (after => 1, interval => 1, cb => sub {
1144 # todo: test for active connections instead (we can abort idle connections)
1145 if ($self->{conn_count} <= 0) {
1147 $self->{end_cond}->send(1);
1155 sub check_host_access {
1156 my ($self, $clientip) = @_;
1158 my $cip = Net::IP->new($clientip);
1160 my $match_allow = 0;
1163 if ($self->{allow_from}) {
1164 foreach my $t (@{$self->{allow_from}}) {
1165 if ($t->overlaps($cip)) {
1172 if ($self->{deny_from}) {
1173 foreach my $t (@{$self->{deny_from}}) {
1174 if ($t->overlaps($cip)) {
1181 if ($match_allow == $match_deny) {
1182 # match both allow and deny, or no match
1183 return $self->{policy} && $self->{policy} eq 'allow' ? 1 : 0;
1186 return $match_allow;
1189 sub accept_connections {
1194 while (my $clientfh = $self->accept()) {
1196 my $reqstate = { keep_alive => $self->{keep_alive} };
1198 # stop keep-alive when there are many open connections
1199 if ($self->{conn_count} >= $self->{max_conn_soft_limit}) {
1200 $reqstate->{keep_alive} = 0;
1203 if (my $sin = getpeername($clientfh)) {
1204 my ($pport, $phost) = Socket::unpack_sockaddr_in($sin);
1205 ($reqstate->{peer_port}, $reqstate->{peer_host}) = ($pport, Socket::inet_ntoa($phost));
1208 if (!$self->{trusted_env} && !$self->check_host_access($reqstate->{peer_host})) {
1209 print "$$: ABORT request from
$reqstate->{peer_host
} - access denied
\n" if $self->{debug};
1210 $reqstate->{log}->{code} = 403;
1211 $self->log_request($reqstate);
1215 $reqstate->{hdl} = AnyEvent::Handle->new(
1217 rbuf_max => 64*1024,
1218 timeout => $self->{timeout},
1219 linger => 0, # avoid problems with ssh - really needed ?
1223 $self->log_aborted_request($reqstate);
1224 $self->client_do_disconnect($reqstate);
1226 if (my $err = $@) { syslog('err', $err); }
1229 my ($hdl, $fatal, $message) = @_;
1231 $self->log_aborted_request($reqstate, $message);
1232 $self->client_do_disconnect($reqstate);
1234 if (my $err = $@) { syslog('err', "$err"); }
1236 ($self->{tls_ctx} ? (tls => "accept", tls_ctx => $self->{tls_ctx}) : ()));
1238 print "$$: ACCEPT FH
" . $clientfh->fileno() . " CONN
$self->{conn_count
}\n" if $self->{debug};
1240 $self->push_request_header($reqstate);
1245 syslog('err', $err);
1246 $self->{end_loop} = 1;
1249 $self->wait_end_loop() if $self->{end_loop};
1252 # Note: We can't open log file in non-blocking mode and use AnyEvent::Handle,
1253 # because we write from multiple processes, and that would arbitrarily mix output
1255 sub open_access_log {
1256 my ($self, $filename) = @_;
1258 my $old_mask = umask(0137);;
1259 my $logfh = IO::File->new($filename, ">>") ||
1260 die "unable to
open log file
'$filename' - $!\n";
1263 $logfh->autoflush(1);
1265 $self->{logfh} = $logfh;
1269 my ($self, $data) = @_;
1271 return if !defined($self->{logfh}) || !$data;
1273 my $res = $self->{logfh}->print($data);
1276 delete $self->{logfh};
1277 syslog('err', "error writing access
log");
1278 $self->{end_loop} = 1; # terminate asap
1282 sub atfork_handler {
1286 # something else do to ?
1287 close($self->{socket});
1293 my ($this, %args) = @_;
1295 my $class = ref($this) || $this;
1297 foreach my $req (qw(socket lockfh lockfile)) {
1298 die "misssing required argument '$req'" if !defined($args{$req});
1301 my $self = bless { %args }, $class;
1304 PVE
::INotify
::inotify_init
();
1306 $self->{rpcenv
} = PVE
::RPCEnvironment-
>init(
1307 $self->{trusted_env
} ?
'priv' : 'pub', atfork
=> sub { $self-> atfork_handler
() });
1309 fh_nonblocking
($self->{socket}, 1);
1311 $self->{end_loop
} = 0;
1312 $self->{conn_count
} = 0;
1313 $self->{request_count
} = 0;
1314 $self->{timeout
} = 5 if !$self->{timeout
};
1315 $self->{keep_alive
} = 0 if !defined($self->{keep_alive
});
1316 $self->{max_conn
} = 800 if !$self->{max_conn
};
1317 $self->{max_requests
} = 8000 if !$self->{max_requests
};
1319 $self->{policy
} = 'allow' if !$self->{policy
};
1321 $self->{end_cond
} = AnyEvent-
>condvar;
1324 $self->{tls_ctx
} = AnyEvent
::TLS-
>new(%{$self->{ssl
}});
1325 Net
::SSLeay
::CTX_set_options
($self->{tls_ctx
}->{ctx
}, &Net
::SSLeay
::OP_NO_COMPRESSION
);
1328 if ($self->{spiceproxy
}) {
1329 $known_methods = { CONNECT
=> 1 };
1332 $self->open_access_log($self->{logfile
}) if $self->{logfile
};
1334 $self->{max_conn_soft_limit
} = $self->{max_conn
} > 100 ?
$self->{max_conn
} - 20 : $self->{max_conn
};
1336 $self->{socket_watch
} = AnyEvent-
>io(fh
=> $self->{socket}, poll
=> 'r', cb
=> sub {
1338 if ($self->{conn_count
} >= $self->{max_conn
}) {
1339 my $w; $w = AnyEvent-
>timer (after
=> 1, interval
=> 1, cb
=> sub {
1340 if ($self->{conn_count
} < $self->{max_conn
}) {
1342 $self->accept_connections();
1346 $self->accept_connections();
1352 $self->{term_watch
} = AnyEvent-
>signal(signal
=> "TERM", cb
=> sub {
1353 undef $self->{term_watch
};
1354 $self->wait_end_loop();
1357 $self->{quit_watch
} = AnyEvent-
>signal(signal
=> "QUIT", cb
=> sub {
1358 undef $self->{quit_watch
};
1359 $self->wait_end_loop();
1362 $self->{inotify_poll
} = AnyEvent-
>timer(after
=> 5, interval
=> 5, cb
=> sub {
1363 PVE
::INotify
::poll
(); # read inotify events
1372 $self->{end_cond
}->recv;