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
;
30 use HTTP
::Status
qw(:constants);
35 my $limit_max_headers = 30;
36 my $limit_max_header_size = 8*1024;
37 my $limit_max_post = 16*1024;
47 my $baseuri = "/api2";
52 my ($format, $rel_uri) = $abs_uri =~ m/^\Q$baseuri\E\/+(html
|text
|json
|extjs
|png
|htmljs
|spiceconfig
)(\
/.*)?$/;
53 $rel_uri = '/' if !$rel_uri;
55 return wantarray ?
($rel_uri, $format) : $rel_uri;
59 my ($self, $reqstate) = @_;
61 my $loginfo = $reqstate->{log};
63 # like apache2 common log format
64 # LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-agent}i\""
66 return if $loginfo->{written
}; # avoid duplicate logs
67 $loginfo->{written
} = 1;
69 my $peerip = $reqstate->{peer_host
} || '-';
70 my $userid = $loginfo->{userid
} || '-';
71 my $content_length = defined($loginfo->{content_length
}) ?
$loginfo->{content_length
} : '-';
72 my $code = $loginfo->{code
} || 500;
73 my $requestline = $loginfo->{requestline
} || '-';
74 my $timestr = strftime
("%d/%b/%Y:%H:%M:%S %z", localtime());
76 my $msg = "$peerip - $userid [$timestr] \"$requestline\" $code $content_length\n";
78 $self->write_log($msg);
81 sub log_aborted_request {
82 my ($self, $reqstate, $error) = @_;
84 my $r = $reqstate->{request};
85 return if !$r; # no active request
88 syslog("err
", "problem with client
$reqstate->{peer_host
}; $error");
91 $self->log_request($reqstate);
94 sub cleanup_reqstate {
97 delete $reqstate->{log};
98 delete $reqstate->{request};
99 delete $reqstate->{proto};
100 delete $reqstate->{accept_gzip};
101 delete $reqstate->{starttime};
103 if ($reqstate->{tmpfilename}) {
104 unlink $reqstate->{tmpfilename};
105 delete $reqstate->{tmpfilename};
109 sub client_do_disconnect {
110 my ($self, $reqstate) = @_;
112 cleanup_reqstate($reqstate);
114 my $shutdown_hdl = sub {
117 shutdown($hdl->{fh}, 1);
119 $hdl->on_drain(undef);
120 $hdl->on_read(undef);
124 if (my $proxyhdl = delete $reqstate->{proxyhdl}) {
125 &$shutdown_hdl($proxyhdl);
128 my $hdl = delete $reqstate->{hdl};
131 syslog('err', "detected empty handle
");
135 print "close connection
$hdl\n" if $self->{debug};
137 &$shutdown_hdl($hdl);
139 $self->{conn_count}--;
141 print "$$: CLOSE FH
" . $hdl->{fh}->fileno() . " CONN
$self->{conn_count
}\n" if $self->{debug};
144 sub finish_response {
145 my ($self, $reqstate) = @_;
147 my $hdl = $reqstate->{hdl};
149 cleanup_reqstate($reqstate);
151 if (!$self->{end_loop} && $reqstate->{keep_alive} > 0) {
152 # print "KEEPALIVE
$reqstate->{keep_alive
}\n" if $self->{debug};
154 eval { $self->push_request_header($reqstate); };
158 $hdl->on_drain (sub {
160 $self->client_do_disconnect($reqstate);
168 my ($self, $reqstate, $resp, $mtime, $nocomp, $delay) = @_;
170 #print "$$: send response
: " . Dumper($resp);
173 $reqstate->{hdl}->timeout_reset();
174 $reqstate->{hdl}->timeout($self->{timeout});
176 $nocomp = 1 if !$reqstate->{accept_gzip};
178 my $code = $resp->code;
179 my $msg = $resp->message || HTTP::Status::status_message($code);
180 ($msg) = $msg =~m/^(.*)$/m;
181 my $content = $resp->content;
183 if ($code =~ /^(1\d\d|[23]04)$/) {
184 # make sure content we have no content
188 $reqstate->{keep_alive} = 0 if ($code >= 400) || $self->{end_loop};
190 $reqstate->{log}->{code} = $code;
192 my $proto = $reqstate->{proto} ? $reqstate->{proto}->{str} : 'HTTP/1.0';
193 my $res = "$proto $code $msg\015\012";
196 my $date = HTTP::Date::time2str($ctime);
197 $resp->header('Date' => $date);
199 $resp->header('Last-Modified' => HTTP::Date::time2str($mtime));
201 $resp->header('Expires' => $date);
202 $resp->header('Cache-Control' => "max-age
=0");
203 $resp->header("Pragma
", "no-cache
");
206 $resp->header('Server' => "pve-api-daemon
/3.0");
211 $content_length = length($content);
213 if (!$nocomp && ($content_length > 1024)) {
214 my $comp = Compress::Zlib::memGzip($content);
215 $resp->header('Content-Encoding', 'gzip');
217 $content_length = length($content);
219 $resp->header("Content-Length
" => $content_length);
220 $reqstate->{log}->{content_length} = $content_length;
223 $resp->remove_header("Content-Length
");
226 if ($reqstate->{keep_alive} > 0) {
227 $resp->push_header('Connection' => 'Keep-Alive');
229 $resp->header('Connection' => 'close');
232 $res .= $resp->headers_as_string("\015\012");
233 #print "SEND
(without content
) $res\n" if $self->{debug};
236 $res .= $content if $content;
238 $self->log_request($reqstate, $reqstate->{request});
240 if ($delay && $delay > 0) {
241 my $w; $w = AnyEvent->timer(after => $delay, cb => sub {
242 undef $w; # delete reference
243 $reqstate->{hdl}->push_write($res);
244 $self->finish_response($reqstate);
247 $reqstate->{hdl}->push_write($res);
248 $self->finish_response($reqstate);
253 my ($self, $reqstate, $code, $msg, $hdr, $content) = @_;
256 my $resp = HTTP::Response->new($code, $msg, $hdr, $content);
257 $self->response($reqstate, $resp);
262 sub send_file_start {
263 my ($self, $reqstate, $filename) = @_;
266 # print "SEND FILE
$filename\n";
267 # Note: aio_load() this is not really async unless we use IO::AIO!
270 my $r = $reqstate->{request};
272 my $fh = IO::File->new($filename, '<') ||
274 my $stat = File::stat::stat($fh) ||
277 my $mtime = $stat->mtime;
279 if (my $ifmod = $r->header('if-modified-since')) {
280 my $iftime = HTTP::Date::str2time($ifmod);
281 if ($mtime <= $iftime) {
282 my $resp = HTTP::Response->new(304, "NOT MODIFIED
");
283 $self->response($reqstate, $resp, $mtime);
289 my $len = sysread($fh, $data, $stat->size);
290 die "got short file
\n" if !defined($len) || $len != $stat->size;
294 if ($filename =~ m/\.css$/) {
296 } elsif ($filename =~ m/\.js$/) {
297 $ct = 'application/javascript';
298 } elsif ($filename =~ m/\.png$/) {
301 } elsif ($filename =~ m/\.ico$/) {
302 $ct = 'image/x-icon';
304 } elsif ($filename =~ m/\.gif$/) {
307 } elsif ($filename =~ m/\.jar$/) {
308 $ct = 'application/java-archive';
311 die "unable to detect content type
";
314 my $header = HTTP::Headers->new(Content_Type => $ct);
315 my $resp = HTTP::Response->new(200, "OK
", $header, $data);
316 $self->response($reqstate, $resp, $mtime, $nocomp);
319 $self->error($reqstate, 501, $err);
327 my ($self, $reqstate, $clientip, $host, $method, $uri, $ticket, $token, $params) = @_;
332 if ($host eq 'localhost') {
333 $target = "http
://$host:85$uri";
334 # keep alive for localhost is not worth (connection setup is about 0.2ms)
337 $target = "https
://$host:8006$uri";
341 PVEDisableProxy => 'true',
342 PVEClientIP => $clientip,
345 $headers->{'cookie'} = PVE::REST::create_auth_cookie($ticket) if $ticket;
346 $headers->{'CSRFPreventionToken'} = $token if $token;
347 $headers->{'Accept-Encoding'} = 'gzip' if $reqstate->{accept_gzip};
351 if ($method eq 'POST' || $method eq 'PUT') {
352 $headers->{'Content-Type'} = 'application/x-www-form-urlencoded';
353 # use URI object to format application/x-www-form-urlencoded content.
354 my $url = URI->new('http:');
355 $url->query_form(%$params);
356 $content = $url->query;
357 if (defined($content)) {
358 $headers->{'Content-Length'} = length($content);
362 my $w; $w = http_request(
367 proxy => undef, # avoid use of $ENV{HTTP_PROXY}
368 keepalive => $keep_alive,
370 tls_ctx => $self->{tls_ctx},
372 my ($body, $hdr) = @_;
376 if (!$reqstate->{hdl}) {
377 warn "proxy detected vanished client connection
\n";
382 my $code = delete $hdr->{Status};
383 my $msg = delete $hdr->{Reason};
385 delete $hdr->{HTTPVersion};
386 my $header = HTTP::Headers->new(%$hdr);
387 my $resp = HTTP::Response->new($code, $msg, $header, $body);
388 # Note: disable compression, because body is already compressed
389 $self->response($reqstate, $resp, undef, 1);
397 # return arrays as \0 separated strings (like CGI.pm)
398 sub decode_urlencoded {
403 return $res if !$data;
405 foreach my $kv (split(/[\&\;]/, $data)) {
406 my ($k, $v) = split(/=/, $kv);
408 $k =~ s/%([0-9a-fA-F][0-9a-fA-F])/chr(hex($1))/eg;
410 $v =~ s/%([0-9a-fA-F][0-9a-fA-F])/chr(hex($1))/eg;
412 if (defined(my $old = $res->{$k})) {
413 $res->{$k} = "$old\0$v";
422 my ($r, $method) = @_;
426 if ($method eq 'PUT' || $method eq 'POST') {
427 $params = decode_urlencoded($r->content);
430 my $query_params = decode_urlencoded($r->url->query());
432 foreach my $k (keys %{$query_params}) {
433 $params->{$k} = $query_params->{$k};
436 return PVE::Tools::decode_utf8_parameters($params);
439 sub handle_api2_request {
440 my ($self, $reqstate, $auth, $upload_state) = @_;
443 my $r = $reqstate->{request};
444 my $method = $r->method();
445 my $path = $r->uri->path();
447 my ($rel_uri, $format) = split_abs_uri($path);
449 $self->error($reqstate, HTTP_NOT_IMPLEMENTED, "no such uri
");
453 #print Dumper($upload_state) if $upload_state;
455 my $rpcenv = $self->{rpcenv};
460 $params = $upload_state->{params};
462 $params = extract_params($r, $method);
465 delete $params->{_dc}; # remove disable cache parameter
467 my $clientip = $reqstate->{peer_host};
469 $rpcenv->init_request();
471 my $res = PVE::REST::rest_handler($rpcenv, $clientip, $method, $rel_uri, $auth, $params);
473 AnyEvent->now_update(); # in case somebody called sleep()
475 $rpcenv->set_user(undef); # clear after request
477 if (my $host = $res->{proxy}) {
479 if ($self->{trusted_env}) {
480 $self->error($reqstate, HTTP_INTERNAL_SERVER_ERROR, "proxy
not allowed
");
484 if ($host ne 'localhost' && $r->header('PVEDisableProxy')) {
485 $self->error($reqstate, HTTP_INTERNAL_SERVER_ERROR, "proxy
loop detected
");
489 $res->{proxy_params}->{tmpfilename} = $reqstate->{tmpfilename} if $upload_state;
491 $self->proxy_request($reqstate, $clientip, $host, $method,
492 $r->uri, $auth->{ticket}, $auth->{token}, $res->{proxy_params});
498 if ($res->{status} == HTTP_UNAUTHORIZED) {
499 # always delay unauthorized calls by 3 seconds
500 $delay = 3 - tv_interval($reqstate->{starttime});
501 $delay = 0 if $delay < 0;
504 PVE::REST::prepare_response_data($format, $res);
505 my ($raw, $ct, $nocomp) = PVE::REST::format_response_data($format, $res, $path);
507 my $resp = HTTP::Response->new($res->{status}, $res->{message});
508 $resp->header("Content-Type
" => $ct);
509 $resp->content($raw);
510 $self->response($reqstate, $resp, undef, $nocomp, $delay);
513 $self->error($reqstate, 501, $err);
517 sub handle_spice_proxy_request {
518 my ($self, $reqstate, $connect_str, $vmid, $node, $spiceport) = @_;
522 die "Port
$spiceport is not allowed
" if ($spiceport < 61000 || $spiceport > 61099);
524 my $rpcenv = $self->{rpcenv};
525 $rpcenv->init_request();
527 my $clientip = $reqstate->{peer_host};
528 my $r = $reqstate->{request};
532 if ($node ne 'localhost' && PVE::INotify::nodename() !~ m/^$node$/i) {
533 $remip = PVE::Cluster::remote_node_ip($node);
534 die "unable to get remote IP address
for node
'$node'\n" if !$remip;
535 print "REMOTE CONNECT
$vmid, $remip, $connect_str\n" if $self->{debug};
537 print "$$: CONNECT
$vmid, $node, $spiceport\n" if $self->{debug};
540 if ($remip && $r->header('PVEDisableProxy')) {
541 $self->error($reqstate, HTTP_INTERNAL_SERVER_ERROR, "proxy
loop detected
");
545 $reqstate->{hdl}->timeout(0);
546 $reqstate->{hdl}->wbuf_max(64*10*1024);
548 my $remhost = $remip ? $remip : "127.0.0.1";
549 my $remport = $remip ? 3128 : $spiceport;
551 tcp_connect $remhost, $remport, sub {
553 or die "connect to
'$remhost:$remport' failed
: $!";
555 print "$$: CONNECTed to
'$remhost:$remport'\n" if $self->{debug};
556 $reqstate->{proxyhdl} = AnyEvent::Handle->new(
559 wbuf_max => 64*10*1024,
564 $self->log_aborted_request($reqstate);
565 $self->client_do_disconnect($reqstate);
567 if (my $err = $@) { syslog('err', $err); }
570 my ($hdl, $fatal, $message) = @_;
572 $self->log_aborted_request($reqstate, $message);
573 $self->client_do_disconnect($reqstate);
575 if (my $err = $@) { syslog('err', "$err"); }
579 my $proxyhdlreader = sub {
582 my $len = length($hdl->{rbuf});
583 my $data = substr($hdl->{rbuf}, 0, $len, '');
585 #print "READ1
$len\n";
586 $reqstate->{hdl}->push_write($data) if $reqstate->{hdl};
589 my $hdlreader = sub {
592 my $len = length($hdl->{rbuf});
593 my $data = substr($hdl->{rbuf}, 0, $len, '');
595 #print "READ0
$len\n";
596 $reqstate->{proxyhdl}->push_write($data) if $reqstate->{proxyhdl};
599 my $proto = $reqstate->{proto} ? $reqstate->{proto}->{str} : 'HTTP/1.0';
601 my $startproxy = sub {
602 $reqstate->{proxyhdl}->timeout(0);
603 $reqstate->{proxyhdl}->on_read($proxyhdlreader);
604 $reqstate->{hdl}->on_read($hdlreader);
606 # todo: use stop_read/start_read if write buffer grows to much
608 my $res = "$proto 200 OK
\015\012"; # hope this is the right answer?
609 $reqstate->{hdl}->push_write($res);
612 $reqstate->{log}->{code} = 200;
613 $self->log_request($reqstate);
617 my $header = "CONNECT
${connect_str
} $proto\015\012" .
618 "Host
: ${connect_str
}\015\012" .
619 "Proxy-Connection
: keep-alive
\015\012" .
620 "User-Agent
: spiceproxy
\015\012" .
621 "PVEDisableProxy
: true
\015\012" .
622 "PVEClientIP
: $clientip\015\012" .
625 $reqstate->{proxyhdl}->push_write($header);
626 $reqstate->{proxyhdl}->push_read(line => sub {
627 my ($hdl, $line) = @_;
629 if ($line =~ m!^$proto 200 OK$!) {
632 $reqstate->{hdl}->push_write($line);
633 $self->client_do_disconnect($reqstate);
644 $self->log_aborted_request($reqstate, $err);
645 $self->client_do_disconnect($reqstate);
650 my ($self, $reqstate, $auth) = @_;
653 my $r = $reqstate->{request};
654 my $method = $r->method();
655 my $path = $r->uri->path();
657 # disable timeout on handle (we already have all data we need)
658 # we re-enable timeout in response()
659 $reqstate->{hdl}->timeout(0);
661 if ($path =~ m!$baseuri!) {
662 $self->handle_api2_request($reqstate, $auth);
666 if ($self->{pages} && ($method eq 'GET') && (my $handler = $self->{pages}->{$path})) {
667 if (ref($handler) eq 'CODE') {
668 my $params = decode_urlencoded($r->url->query());
669 my ($resp, $userid) = &$handler($self, $reqstate->{request}, $params);
670 $self->response($reqstate, $resp);
671 } elsif (ref($handler) eq 'HASH') {
672 if (my $filename = $handler->{file}) {
673 my $fh = IO::File->new($filename) ||
674 die "unable to
open file
'$filename' - $!\n";
675 send_file_start($self, $reqstate, $filename);
677 die "internal error
- no handler
";
680 die "internal error
- no handler
";
685 if ($self->{dirs} && ($method eq 'GET')) {
686 # we only allow simple names
687 if ($path =~ m!^(/\S+/)([a-zA-Z0-9\-\_\.]+)$!) {
688 my ($subdir, $file) = ($1, $2);
689 if (my $dir = $self->{dirs}->{$subdir}) {
690 my $filename = "$dir$file";
691 my $fh = IO::File->new($filename) ||
692 die "unable to
open file
'$filename' - $!\n";
693 send_file_start($self, $reqstate, $filename);
699 die "no such file
'$path'";
702 $self->error($reqstate, 501, $err);
706 sub file_upload_multipart {
707 my ($self, $reqstate, $auth, $rstate) = @_;
710 my $boundary = $rstate->{boundary};
711 my $hdl = $reqstate->{hdl};
713 my $startlen = length($hdl->{rbuf});
715 if ($rstate->{phase} == 0) { # skip everything until start
716 if ($hdl->{rbuf} =~ s/^.*?--\Q$boundary\E \015?\012
717 ((?:[^\015]+\015\012)* ) \015?\012//xs) {
719 my ($ct, $disp, $name, $filename);
720 foreach my $line (split(/\015?\012/, $header)) {
721 # assume we have single line headers
722 if ($line =~ m/^Content-Type\s*:\s*(.*)/i) {
723 $ct = parse_content_type($1);
724 } elsif ($line =~ m/^Content-Disposition\s*:\s*(.*)/i) {
725 ($disp, $name, $filename) = parse_content_disposition($1);
729 if (!($disp && $disp eq 'form-data' && $name)) {
730 syslog('err', "wrong content disposition
in multipart
- abort upload
");
731 $rstate->{phase} = -1;
734 $rstate->{fieldname} = $name;
737 if ($name eq 'filename') {
738 # found file upload data
739 $rstate->{phase} = 1;
740 $rstate->{filename} = $filename;
742 syslog('err', "wrong field name
for file upload
- abort upload
");
743 $rstate->{phase} = -1;
746 # found form data for field $name
747 $rstate->{phase} = 2;
751 my $len = length($hdl->{rbuf});
752 substr($hdl->{rbuf}, 0, $len - $rstate->{maxheader}, '')
753 if $len > $rstate->{maxheader}; # skip garbage
755 } elsif ($rstate->{phase} == 1) { # inside file - dump until end marker
756 if ($hdl->{rbuf} =~ s/^(.*?)\015?\012(--\Q$boundary\E(--)? \015?\012(.*))$/$2/xs) {
757 my ($rest, $eof) = ($1, $3);
758 my $len = length($rest);
759 die "write to temporary file failed
- $!"
760 if syswrite($rstate->{outfh}, $rest) != $len;
761 $rstate->{ctx}->add($rest);
762 $rstate->{params}->{filename} = $rstate->{filename};
763 $rstate->{md5sum} = $rstate->{ctx}->hexdigest;
764 $rstate->{bytes} += $len;
765 $rstate->{phase} = $eof ? 100 : 0;
767 my $len = length($hdl->{rbuf});
768 my $wlen = $len - $rstate->{boundlen};
770 my $data = substr($hdl->{rbuf}, 0, $wlen, '');
771 die "write to temporary file failed
- $!"
772 if syswrite($rstate->{outfh}, $data) != $wlen;
773 $rstate->{bytes} += $wlen;
774 $rstate->{ctx}->add($data);
777 } elsif ($rstate->{phase} == 2) { # inside normal field
779 if ($hdl->{rbuf} =~ s/^(.*?)\015?\012(--\Q$boundary\E(--)? \015?\012(.*))$/$2/xs) {
780 my ($rest, $eof) = ($1, $3);
781 my $len = length($rest);
782 $rstate->{post_size} += $len;
783 if ($rstate->{post_size} < $limit_max_post) {
784 $rstate->{params}->{$rstate->{fieldname}} = $rest;
785 $rstate->{phase} = $eof ? 100 : 0;
787 syslog('err', "form data to large
- abort upload
");
788 $rstate->{phase} = -1; # skip
792 my $len = length($hdl->{rbuf});
793 substr($hdl->{rbuf}, 0, $len, ''); # empty rbuf
796 $rstate->{read} += ($startlen - length($hdl->{rbuf}));
798 if (!$rstate->{done} && ($rstate->{read} + length($hdl->{rbuf})) >= $rstate->{size}) {
799 $rstate->{done} = 1; # make sure we dont get called twice
800 if ($rstate->{phase} < 0 || !$rstate->{md5sum}) {
801 die "upload failed
\n";
803 my $elapsed = tv_interval($rstate->{starttime});
805 my $rate = int($rstate->{bytes}/($elapsed*1024*1024));
806 syslog('info', "multipart upload complete
" .
807 "(size
: %d time: %ds rate
: %.2fMiB
/s md5sum
: $rstate->{md5sum
})",
808 $rstate->{bytes}, $elapsed, $rate);
809 $self->handle_api2_request($reqstate, $auth, $rstate);
815 $self->error($reqstate, 501, $err);
819 sub parse_content_type {
822 my ($ct, @params) = split(/\s*[;,]\s*/o, $ctype);
824 foreach my $v (@params) {
825 if ($v =~ m/^\s*boundary\s*=\s*(\S+?)\s*$/o) {
826 return wantarray ? ($ct, $1) : $ct;
830 return wantarray ? ($ct) : $ct;
833 sub parse_content_disposition {
836 my ($disp, @params) = split(/\s*[;,]\s*/o, $line);
840 foreach my $v (@params) {
841 if ($v =~ m/^\s*name\s*=\s*(\S+?)\s*$/o) {
843 $name =~ s/^"(.*)"$/$1/;
844 } elsif ($v =~ m/^\s*filename\s*=\s*(.+?)\s*$/o) {
846 $filename =~ s/^"(.*)"$/$1/;
850 return wantarray ? ($disp, $name, $filename) : $disp;
853 my $tmpfile_seq_no = 0;
855 sub get_upload_filename {
856 # choose unpredictable tmpfile name
859 return "/var/tmp
/pveupload-
" . Digest::MD5::md5_hex($tmpfile_seq_no . time() . $$);
862 sub unshift_read_header {
863 my ($self, $reqstate, $state) = @_;
865 $state = { size => 0, count => 0 } if !$state;
867 $reqstate->{hdl}->unshift_read(line => sub {
868 my ($hdl, $line) = @_;
871 # print "$$: got header
: $line\n" if $self->{debug};
873 die "to many http header lines
\n" if ++$state->{count} >= $limit_max_headers;
874 die "http header too large
\n" if ($state->{size} += length($line)) >= $limit_max_header_size;
876 my $r = $reqstate->{request};
879 my $path = $r->uri->path();
880 my $method = $r->method();
882 $r->push_header($state->{key}, $state->{val})
885 if (!$known_methods->{$method}) {
886 my $resp = HTTP::Response->new(HTTP_NOT_IMPLEMENTED, "method '$method' not available
");
887 $self->response($reqstate, $resp);
891 my $conn = $r->header('Connection');
892 my $accept_enc = $r->header('Accept-Encoding');
893 $reqstate->{accept_gzip} = ($accept_enc && $accept_enc =~ m/gzip/) ? 1 : 0;
896 $reqstate->{keep_alive} = 0 if $conn =~ m/close/oi;
898 if ($reqstate->{proto}->{ver} < 1001) {
899 $reqstate->{keep_alive} = 0;
903 my $te = $r->header('Transfer-Encoding');
904 if ($te && lc($te) eq 'chunked') {
905 # Handle chunked transfer encoding
906 $self->error($reqstate, 501, "chunked transfer encoding
not supported
");
909 $self->error($reqstate, 501, "Unknown transfer encoding
'$te'");
913 my $pveclientip = $r->header('PVEClientIP');
915 # fixme: how can we make PVEClientIP header trusted?
916 if ($self->{trusted_env} && $pveclientip) {
917 $reqstate->{peer_host} = $pveclientip;
919 $r->header('PVEClientIP', $reqstate->{peer_host});
922 my $len = $r->header('Content-Length');
924 # header processing complete - authenticate now
927 if ($self->{spiceproxy}) {
928 my $connect_str = $r->header('Host');
929 my ($vmid, $node, $port) = PVE::AccessControl::verify_spice_connect_url($connect_str);
930 if (!(defined($vmid) && $node && $port)) {
931 $self->error($reqstate, HTTP_UNAUTHORIZED, "invalid ticket
");
934 $self->handle_spice_proxy_request($reqstate, $connect_str, $vmid, $node, $port);
936 } elsif ($path =~ m!$baseuri!) {
937 my $token = $r->header('CSRFPreventionToken');
938 my $cookie = $r->header('Cookie');
939 my $ticket = PVE::REST::extract_auth_cookie($cookie);
941 my ($rel_uri, $format) = split_abs_uri($path);
943 $self->error($reqstate, HTTP_NOT_IMPLEMENTED, "no such uri
");
948 $auth = PVE::REST::auth_handler($self->{rpcenv}, $reqstate->{peer_host}, $method,
949 $rel_uri, $ticket, $token);
952 $self->error($reqstate, HTTP_UNAUTHORIZED, $err);
957 $reqstate->{log}->{userid} = $auth->{userid};
961 if (!($method eq 'PUT' || $method eq 'POST')) {
962 $self->error($reqstate, 501, "Unexpected content
for method '$method'");
966 my $ctype = $r->header('Content-Type');
967 my ($ct, $boundary) = parse_content_type($ctype) if $ctype;
969 if ($auth->{isUpload} && !$self->{trusted_env}) {
970 die "upload
'Content-Type '$ctype' not implemented\n"
971 if !($boundary && $ct && ($ct eq 'multipart
/form-data
'));
973 die "upload without content length header not supported" if !$len;
975 die "upload without content length header not supported" if !$len;
977 print "start upload $path $ct $boundary\n" if $self->{debug};
979 my $tmpfilename = get_upload_filename();
980 my $outfh = IO::File->new($tmpfilename, O_RDWR|O_CREAT|O_EXCL, 0600) ||
981 die "unable to create temporary upload file '$tmpfilename'";
983 $reqstate->{keep_alive} = 0;
985 my $boundlen = length($boundary) + 8; # \015?\012--$boundary--\015?\012
989 boundary => $boundary,
990 ctx => Digest::MD5->new,
991 boundlen => $boundlen,
992 maxheader => 2048 + $boundlen, # should be large enough
993 params => decode_urlencoded($r->url->query()),
997 starttime => [gettimeofday],
1000 $reqstate->{tmpfilename} = $tmpfilename;
1001 $reqstate->{hdl}->on_read(sub { $self->file_upload_multipart($reqstate, $auth, $state); });
1005 if ($len > $limit_max_post) {
1006 $self->error($reqstate, 501, "for data too large");
1010 if (!$ct || $ct eq 'application
/x-www-form-urlencoded
') {
1011 $reqstate->{hdl}->unshift_read(chunk => $len, sub {
1012 my ($hdl, $data) = @_;
1014 $self->handle_request($reqstate, $auth);
1017 $self->error($reqstate, 506, "upload 'Content-Type
'$ctype' not implemented
");
1020 $self->handle_request($reqstate, $auth);
1022 } elsif ($line =~ /^([^:\s]+)\s*:\s*(.*)/) {
1023 $r->push_header($state->{key}, $state->{val}) if $state->{key};
1024 ($state->{key}, $state->{val}) = ($1, $2);
1025 $self->unshift_read_header($reqstate, $state);
1026 } elsif ($line =~ /^\s+(.*)/) {
1027 $state->{val} .= " $1";
1028 $self->unshift_read_header($reqstate, $state);
1030 $self->error($reqstate, 506, "unable to parse request header
");
1037 sub push_request_header {
1038 my ($self, $reqstate) = @_;
1041 $reqstate->{hdl}->push_read(line => sub {
1042 my ($hdl, $line) = @_;
1045 # print "got request header
: $line\n" if $self->{debug};
1047 $reqstate->{keep_alive}--;
1049 if ($line =~ /(\S+)\040(\S+)\040HTTP\/(\d+)\.(\d+)/o) {
1050 my ($method, $url, $maj, $min) = ($1, $2, $3, $4);
1053 $self->error($reqstate, 506, "http protocol version
$maj.$min not supported
");
1057 $self->{request_count}++; # only count valid request headers
1058 if ($self->{request_count} >= $self->{max_requests}) {
1059 $self->{end_loop} = 1;
1061 $reqstate->{log} = { requestline => $line };
1062 $reqstate->{proto}->{str} = "HTTP
/$maj.$min";
1063 $reqstate->{proto}->{maj} = $maj;
1064 $reqstate->{proto}->{min} = $min;
1065 $reqstate->{proto}->{ver} = $maj*1000+$min;
1066 $reqstate->{request} = HTTP::Request->new($method, uri_unescape($url));
1067 $reqstate->{starttime} = [gettimeofday],
1069 $self->unshift_read_header($reqstate);
1070 } elsif ($line eq '') {
1071 # ignore empty lines before requests (browser bugs?)
1072 $self->push_request_header($reqstate);
1074 $self->error($reqstate, 400, 'bad request');
1088 return if $self->{end_loop};
1090 # we need to m make sure that only one process calls accept
1091 while (!flock($self->{lockfh}, Fcntl::LOCK_EX())) {
1092 next if $! == EINTR;
1093 die "could
not get
lock on file
'$self->{lockfile}' - $!\n";
1099 while (!$self->{end_loop} &&
1100 !defined($clientfh = $self->{socket}->accept()) &&
1103 if ($self->{end_loop}) {
1106 $again = ($! == EAGAIN || $! == WSAEWOULDBLOCK);
1107 if (!defined($clientfh)) {
1108 $errmsg = "failed to
accept connection
: $!\n";
1114 flock($self->{lockfh}, Fcntl::LOCK_UN());
1116 if (!defined($clientfh)) {
1118 die $errmsg if $errmsg;
1121 fh_nonblocking $clientfh, 1;
1123 $self->{conn_count}++;
1131 $self->{end_loop} = 1;
1133 undef $self->{socket_watch};
1135 if ($self->{conn_count} <= 0) {
1136 $self->{end_cond}->send(1);
1140 # else we need to wait until all open connections gets closed
1141 my $w; $w = AnyEvent->timer (after => 1, interval => 1, cb => sub {
1143 # todo: test for active connections instead (we can abort idle connections)
1144 if ($self->{conn_count} <= 0) {
1146 $self->{end_cond}->send(1);
1154 sub check_host_access {
1155 my ($self, $clientip) = @_;
1157 my $cip = Net::IP->new($clientip);
1159 my $match_allow = 0;
1162 if ($self->{allow_from}) {
1163 foreach my $t (@{$self->{allow_from}}) {
1164 if ($t->overlaps($cip)) {
1171 if ($self->{deny_from}) {
1172 foreach my $t (@{$self->{deny_from}}) {
1173 if ($t->overlaps($cip)) {
1180 if ($match_allow == $match_deny) {
1181 # match both allow and deny, or no match
1182 return $self->{policy} && $self->{policy} eq 'allow' ? 1 : 0;
1185 return $match_allow;
1188 sub accept_connections {
1193 while (my $clientfh = $self->accept()) {
1195 my $reqstate = { keep_alive => $self->{keep_alive} };
1197 # stop keep-alive when there are many open connections
1198 if ($self->{conn_count} >= $self->{max_conn_soft_limit}) {
1199 $reqstate->{keep_alive} = 0;
1202 if (my $sin = getpeername($clientfh)) {
1203 my ($pport, $phost) = Socket::unpack_sockaddr_in($sin);
1204 ($reqstate->{peer_port}, $reqstate->{peer_host}) = ($pport, Socket::inet_ntoa($phost));
1207 if (!$self->{trusted_env} && !$self->check_host_access($reqstate->{peer_host})) {
1208 print "$$: ABORT request from
$reqstate->{peer_host
} - access denied
\n" if $self->{debug};
1209 $reqstate->{log}->{code} = 403;
1210 $self->log_request($reqstate);
1214 $reqstate->{hdl} = AnyEvent::Handle->new(
1216 rbuf_max => 64*1024,
1217 timeout => $self->{timeout},
1218 linger => 0, # avoid problems with ssh - really needed ?
1222 $self->log_aborted_request($reqstate);
1223 $self->client_do_disconnect($reqstate);
1225 if (my $err = $@) { syslog('err', $err); }
1228 my ($hdl, $fatal, $message) = @_;
1230 $self->log_aborted_request($reqstate, $message);
1231 $self->client_do_disconnect($reqstate);
1233 if (my $err = $@) { syslog('err', "$err"); }
1235 ($self->{tls_ctx} ? (tls => "accept", tls_ctx => $self->{tls_ctx}) : ()));
1237 print "$$: ACCEPT FH
" . $clientfh->fileno() . " CONN
$self->{conn_count
}\n" if $self->{debug};
1239 $self->push_request_header($reqstate);
1244 syslog('err', $err);
1245 $self->{end_loop} = 1;
1248 $self->wait_end_loop() if $self->{end_loop};
1251 # Note: We can't open log file in non-blocking mode and use AnyEvent::Handle,
1252 # because we write from multiple processes, and that would arbitrarily mix output
1254 sub open_access_log {
1255 my ($self, $filename) = @_;
1257 my $old_mask = umask(0137);;
1258 my $logfh = IO::File->new($filename, ">>") ||
1259 die "unable to
open log file
'$filename' - $!\n";
1262 $logfh->autoflush(1);
1264 $self->{logfh} = $logfh;
1268 my ($self, $data) = @_;
1270 return if !defined($self->{logfh}) || !$data;
1272 my $res = $self->{logfh}->print($data);
1275 delete $self->{logfh};
1276 syslog('err', "error writing access
log");
1277 $self->{end_loop} = 1; # terminate asap
1281 sub atfork_handler {
1285 # something else do to ?
1286 close($self->{socket});
1292 my ($this, %args) = @_;
1294 my $class = ref($this) || $this;
1296 foreach my $req (qw(base_handler_class socket lockfh lockfile)) {
1297 die "misssing required argument '$req'" if !defined($args{$req});
1300 my $self = bless { %args }, $class;
1302 PVE
::REST
::set_base_handler_class
($self->{base_handler_class
});
1305 PVE
::INotify
::inotify_init
();
1307 $self->{rpcenv
} = PVE
::RPCEnvironment-
>init(
1308 $self->{trusted_env
} ?
'priv' : 'pub', atfork
=> sub { $self-> atfork_handler
() });
1310 fh_nonblocking
($self->{socket}, 1);
1312 $self->{end_loop
} = 0;
1313 $self->{conn_count
} = 0;
1314 $self->{request_count
} = 0;
1315 $self->{timeout
} = 5 if !$self->{timeout
};
1316 $self->{keep_alive
} = 0 if !defined($self->{keep_alive
});
1317 $self->{max_conn
} = 800 if !$self->{max_conn
};
1318 $self->{max_requests
} = 8000 if !$self->{max_requests
};
1320 $self->{policy
} = 'allow' if !$self->{policy
};
1322 $self->{end_cond
} = AnyEvent-
>condvar;
1325 $self->{tls_ctx
} = AnyEvent
::TLS-
>new(%{$self->{ssl
}});
1326 Net
::SSLeay
::CTX_set_options
($self->{tls_ctx
}->{ctx
}, &Net
::SSLeay
::OP_NO_COMPRESSION
);
1329 if ($self->{spiceproxy
}) {
1330 $known_methods = { CONNECT
=> 1 };
1333 $self->open_access_log($self->{logfile
}) if $self->{logfile
};
1335 $self->{max_conn_soft_limit
} = $self->{max_conn
} > 100 ?
$self->{max_conn
} - 20 : $self->{max_conn
};
1337 $self->{socket_watch
} = AnyEvent-
>io(fh
=> $self->{socket}, poll
=> 'r', cb
=> sub {
1339 if ($self->{conn_count
} >= $self->{max_conn
}) {
1340 my $w; $w = AnyEvent-
>timer (after
=> 1, interval
=> 1, cb
=> sub {
1341 if ($self->{conn_count
} < $self->{max_conn
}) {
1343 $self->accept_connections();
1347 $self->accept_connections();
1353 $self->{term_watch
} = AnyEvent-
>signal(signal
=> "TERM", cb
=> sub {
1354 undef $self->{term_watch
};
1355 $self->wait_end_loop();
1358 $self->{quit_watch
} = AnyEvent-
>signal(signal
=> "QUIT", cb
=> sub {
1359 undef $self->{quit_watch
};
1360 $self->wait_end_loop();
1363 $self->{inotify_poll
} = AnyEvent-
>timer(after
=> 5, interval
=> 5, cb
=> sub {
1364 PVE
::INotify
::poll
(); # read inotify events
1373 $self->{end_cond
}->recv;