root/trunk/lib/Perlbal/ClientProxy.pm @ 618

Revision 618, 42.9 kB (checked in by bradfitz, 3 years ago)

was done

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
Line 
1######################################################################
2# HTTP Connection from a reverse proxy client
3#
4# Copyright 2004, Danga Interactice, Inc.
5# Copyright 2005-2006, Six Apart, Ltd.
6#
7package Perlbal::ClientProxy;
8use strict;
9use warnings;
10use base "Perlbal::ClientHTTPBase";
11no  warnings qw(deprecated);
12
13use Perlbal::ChunkedUploadState;
14
15use fields (
16            'backend',             # Perlbal::BackendHTTP object (or undef if disconnected)
17            'backend_requested',   # true if we've requested a backend for this request
18            'reconnect_count',     # number of times we've tried to reconnect to backend
19            'high_priority',       # boolean; 1 if we are or were in the high priority queue
20            'low_priority',        # boolean; 1 if we are or were in the low priority queue
21            'reproxy_uris',        # arrayref; URIs to reproxy to, in order
22            'reproxy_expected_size', # int: size of response we expect to get back for reproxy
23            'currently_reproxying',  # arrayref; the host info and URI we're reproxying right now
24            'content_length_remain', # int: amount of data we're still waiting for
25            'responded',           # bool: whether we've already sent a response to the user or not
26            'last_request_time',   # int: time that we last received a request
27            'primary_res_hdrs',  # if defined, we are doing a transparent reproxy-URI
28                                 # and the headers we get back aren't necessarily
29                                 # the ones we want.  instead, get most headers
30                                 # from the provided res headers object here.
31            'is_buffering',        # bool; if we're buffering some/all of a request to memory/disk
32            'is_writing',          # bool; if on, we currently have an aio_write out
33            'start_time',          # hi-res time when we started getting data to upload
34            'bufh',                # buffered upload filehandle object
35            'bufilename',          # string; buffered upload filename
36            'bureason',            # string; if defined, the reason we're buffering to disk
37            'buoutpos',            # int; buffered output position
38            'backend_stalled',   # boolean:  if backend has shut off its reads because we're too slow.
39            'unread_data_waiting',  # boolean:  if we shut off reads while we know data is yet to be read from client
40            'chunked_upload_state', # bool/obj:  if processing a chunked upload, Perlbal::ChunkedUploadState object, else undef
41            'request_body_length',  # integer:  request's body length, either as-declared,
42                                    #           or calculated after chunked upload is complete
43
44            # for perlbal sending out UDP packets related to upload status (for xmlhttprequest upload bar)
45            'last_upload_packet',  # unixtime we last sent a UDP upload packet
46            'upload_session',      # client's self-generated upload session
47
48            # error-retrying stuff
49            'retry_count',         # number of times we've retried this request so far after getting 500 errors
50            );
51
52use constant READ_SIZE         => 131072;    # 128k, ~common TCP window size?
53use constant READ_AHEAD_SIZE   =>  32768;    # kinda arbitrary.  sum of these two is max stored per connection while waiting for backend.
54use Errno qw( EPIPE ENOENT ECONNRESET EAGAIN );
55use POSIX qw( O_CREAT O_TRUNC O_RDWR O_RDONLY );
56use Time::HiRes qw( gettimeofday tv_interval );
57
58my $udp_sock;
59
60# ClientProxy
61sub new {
62    my ($class, $service, $sock) = @_;
63
64    my $self = $class;
65    $self = fields::new($class) unless ref $self;
66    $self->SUPER::new($service, $sock);       # init base fields
67
68    Perlbal::objctor($self);
69    bless $self, ref $class || $class;
70
71    $self->init;
72    $self->watch_read(1);
73    return $self;
74}
75
76sub new_from_base {
77    my $class = shift;
78    my Perlbal::ClientHTTPBase $cb = shift;
79    bless $cb, $class;
80    $cb->init;
81    $cb->watch_read(1);
82    $cb->handle_request;
83    return $cb;
84}
85
86sub init {
87    my Perlbal::ClientProxy $self = $_[0];
88
89    $self->{last_request_time} = 0;
90
91    $self->{backend} = undef;
92    $self->{high_priority} = 0;
93
94    $self->{responded} = 0;
95    $self->{unread_data_waiting} = 0;
96    $self->{content_length_remain} = undef;
97    $self->{backend_requested} = 0;
98
99    $self->{is_buffering} = 0;
100    $self->{is_writing} = 0;
101    $self->{start_time} = undef;
102    $self->{bufh} = undef;
103    $self->{bufilename} = undef;
104    $self->{buoutpos} = 0;
105    $self->{bureason} = undef;
106    $self->{chunked_upload_state} = undef;
107    $self->{request_body_length} = undef;
108
109    $self->{reproxy_uris} = undef;
110    $self->{reproxy_expected_size} = undef;
111    $self->{currently_reproxying} = undef;
112
113    $self->{retry_count} = 0;
114}
115
116# given a service name, re-request (GET/HEAD only) to that service, even though
117# you've already done a request to your original service
118sub start_reproxy_service {
119    my Perlbal::ClientProxy $self = $_[0];
120    my Perlbal::HTTPHeaders $primary_res_hdrs = $_[1];
121    my $svc_name = $_[2];
122
123    my $svc = $svc_name ? Perlbal->service($svc_name) : undef;
124    unless ($svc) {
125        $self->_simple_response(404, "Vhost twiddling not configured for requested pair.");
126        return 1;
127    }
128
129    $self->{backend_requested} = 0;
130    $self->{backend} = undef;
131    $self->{res_headers} = $primary_res_hdrs;
132
133    $svc->adopt_base_client($self);
134}
135
136# call this with a string of space separated URIs to start a process
137# that will fetch the item at the first and return it to the user,
138# on failure it will try the second, then third, etc
139sub start_reproxy_uri {
140    my Perlbal::ClientProxy $self = $_[0];
141    my Perlbal::HTTPHeaders $primary_res_hdrs = $_[1];
142    my $urls = $_[2];
143
144    # at this point we need to disconnect from our backend
145    $self->{backend} = undef;
146
147    # failure if we have no primary response headers
148    return unless $self->{primary_res_hdrs} ||= $primary_res_hdrs;
149
150    # construct reproxy_uri list
151    if (defined $urls) {
152        my @uris = split /\s+/, $urls;
153        $self->{currently_reproxying} = undef;
154        $self->{reproxy_uris} = [];
155        foreach my $uri (@uris) {
156            next unless $uri =~ m!^http://(.+?)(?::(\d+))?(/.*)?$!;
157            push @{$self->{reproxy_uris}}, [ $1, $2 || 80, $3 || '/' ];
158        }
159    }
160
161    # if we get in here and we have currently_reproxying defined, then something
162    # happened and we want to retry that one
163    if ($self->{currently_reproxying}) {
164        unshift @{$self->{reproxy_uris}}, $self->{currently_reproxying};
165        $self->{currently_reproxying} = undef;
166    }
167
168    # if we have no uris in our list now, tell the user 404
169    return $self->_simple_response(503)
170        unless @{$self->{reproxy_uris} || []};
171
172    # set the expected size if we got a content length in our headers
173    if ($primary_res_hdrs && (my $expected_size = $primary_res_hdrs->header('X-REPROXY-EXPECTED-SIZE'))) {
174        $self->{reproxy_expected_size} = $expected_size;
175    }
176
177    # pass ourselves off to the reproxy manager
178    $self->state('wait_backend');
179    Perlbal::ReproxyManager::do_reproxy($self);
180}
181
182# called by the reproxy manager when we can't get to our requested backend
183sub try_next_uri {
184    my Perlbal::ClientProxy $self = $_[0];
185
186    shift @{$self->{reproxy_uris}};
187    $self->{currently_reproxying} = undef;
188    $self->start_reproxy_uri();
189}
190
191# returns true if this ClientProxy is too many bytes behind the backend
192sub too_far_behind_backend {
193    my Perlbal::ClientProxy $self    = $_[0];
194    my Perlbal::BackendHTTP $backend = $self->{backend}   or return 0;
195
196    # if a backend doesn't have a service, it's a
197    # ReproxyManager-created backend, and thus it should use the
198    # 'buffer_size_reproxy_url' parameter for acceptable buffer
199    # widths, and not the regular 'buffer_size'.  this lets people
200    # tune buffers depending on the types of webservers.  (assumption
201    # being that reproxied-to webservers are event-based and it's okay
202    # to tie the up longer in favor of using less buffer memory in
203    # perlbal)
204    my $max_buffer = defined $backend->{service} ?
205        $self->{service}->{buffer_size} :
206        $self->{service}->{buffer_size_reproxy_url};
207
208    return $self->{write_buf_size} > $max_buffer;
209}
210
211# this is a callback for when a backend has been created and is
212# ready for us to do something with it
213sub use_reproxy_backend {
214    my Perlbal::ClientProxy $self = $_[0];
215    my Perlbal::BackendHTTP $be = $_[1];
216
217    # get a URI
218    my $datref = $self->{currently_reproxying} = shift @{$self->{reproxy_uris}};
219    unless (defined $datref) {
220        # return error and close the backend
221        $be->close('invalid_uris');
222        return $self->_simple_response(503);
223    }
224
225    # now send request
226    $self->{backend} = $be;
227    $be->{client} = $self;
228
229    my $extra_hdr = "";
230    if (my $range = $self->{req_headers}->header("Range")) {
231        $extra_hdr .= "Range: $range\r\n";
232    }
233    if (my $host = $self->{req_headers}->header("Host")) {
234        $extra_hdr .= "Host: $host\r\n";
235    }
236
237    my $headers = "GET $datref->[2] HTTP/1.0\r\nConnection: keep-alive\r\n${extra_hdr}\r\n";
238
239    $be->{req_headers} = Perlbal::HTTPHeaders->new(\$headers);
240    $be->state('sending_req');
241    $self->state('backend_req_sent');
242    $be->write($be->{req_headers}->to_string_ref);
243    $be->watch_read(1);
244    $be->watch_write(1);
245}
246
247# this is called when a transient backend getting a reproxied URI has received
248# a response from the server and is ready for us to deal with it
249sub backend_response_received {
250    my Perlbal::ClientProxy $self = $_[0];
251    my Perlbal::BackendHTTP $be = $_[1];
252
253    # a response means that we are no longer currently waiting on a reproxy, and
254    # don't want to retry this URI
255    $self->{currently_reproxying} = undef;
256
257    # we fail if we got something that's NOT a 2xx code, OR, if we expected
258    # a certain size and got back something different
259    my $code = $be->{res_headers}->response_code + 0;
260
261    my $bad_code = sub {
262        return 0 if $code >= 200 && $code <= 299;
263        return 0 if $code == 416;
264        return 1;
265    };
266
267    my $bad_size = sub {
268        return 0 unless defined $self->{reproxy_expected_size};
269        return $self->{reproxy_expected_size} != $be->{res_headers}->header('Content-length');
270    };
271
272    if ($bad_code->() || $bad_size->()) {
273        # fall back to an alternate URL
274        $be->{client} = undef;
275        $be->close('non_200_reproxy');
276        $self->try_next_uri;
277        return 1;
278    }
279    return 0;
280}
281
282sub start_reproxy_file {
283    my Perlbal::ClientProxy $self = shift;
284    my $file = shift;                      # filename to reproxy
285    my Perlbal::HTTPHeaders $hd = shift;   # headers from backend, in need of cleanup
286
287    # at this point we need to disconnect from our backend
288    $self->{backend} = undef;
289
290    # call hook for pre-reproxy
291    return if $self->{service}->run_hook("start_file_reproxy", $self, \$file);
292
293    # set our expected size
294    if (my $expected_size = $hd->header('X-REPROXY-EXPECTED-SIZE')) {
295        $self->{reproxy_expected_size} = $expected_size;
296    }
297
298    # start an async stat on the file
299    $self->state('wait_stat');
300    Perlbal::AIO::aio_stat($file, sub {
301
302        # if the client's since disconnected by the time we get the stat,
303        # just bail.
304        return if $self->{closed};
305
306        my $size = -s _;
307
308        unless ($size) {
309            # FIXME: POLICY: 404 or retry request to backend w/o reproxy-file capability?
310            return $self->_simple_response(404);
311        }
312        if (defined $self->{reproxy_expected_size} && $self->{reproxy_expected_size} != $size) {
313            # 404; the file size doesn't match what we expected
314            return $self->_simple_response(404);
315        }
316
317        # if the thing we're reproxying is indeed a file, advertise that
318        # we support byteranges on it
319        if (-f _) {
320            $hd->header("Accept-Ranges", "bytes");
321        }
322
323        my ($status, $range_start, $range_end) = $self->{req_headers}->range($size);
324        my $not_satisfiable = 0;
325
326        if ($status == 416) {
327            $hd = Perlbal::HTTPHeaders->new_response(416);
328            $hd->header("Content-Range", $size ? "bytes */$size" : "*");
329            $not_satisfiable = 1;
330        }
331
332        # change the status code to 200 if the backend gave us 204 No Content
333        $hd->code(200) if $hd->response_code == 204;
334
335        # fixup the Content-Length header with the correct size (application
336        # doesn't need to provide a correct value if it doesn't want to stat())
337        if ($status == 200) {
338            $hd->header("Content-Length", $size);
339        } elsif ($status == 206) {
340            $hd->header("Content-Range", "bytes $range_start-$range_end/$size");
341            $hd->header("Content-Length", $range_end - $range_start + 1);
342            $hd->code(206);
343        }
344
345        # don't send this internal header to the client:
346        $hd->header('X-REPROXY-FILE', undef);
347
348        # rewrite some other parts of the header
349        $self->setup_keepalive($hd);
350
351        # just send the header, now that we cleaned it.
352        $self->{res_headers} = $hd;
353        $self->write($hd->to_string_ref);
354
355        if ($self->{req_headers}->request_method eq 'HEAD' || $not_satisfiable) {
356            $self->write(sub { $self->http_response_sent; });
357            return;
358        }
359
360        $self->state('wait_open');
361        Perlbal::AIO::aio_open($file, 0, 0 , sub {
362            my $fh = shift;
363
364            # if client's gone, just close filehandle and abort
365            if ($self->{closed}) {
366                CORE::close($fh) if $fh;
367                return;
368            }
369
370            # handle errors
371            if (! $fh) {
372                # FIXME: do 500 vs. 404 vs whatever based on $! ?
373                return $self->_simple_response(500);
374            }
375
376            # seek if partial content
377            if ($status == 206) {
378                sysseek($fh, $range_start, &POSIX::SEEK_SET);
379                $size = $range_end - $range_start + 1;
380            }
381
382            $self->reproxy_fh($fh, $size);
383            $self->watch_write(1);
384        });
385    });
386}
387
388# Client
389# get/set backend proxy connection
390sub backend {
391    my Perlbal::ClientProxy $self = shift;
392    return $self->{backend} unless @_;
393
394    my $backend = shift;
395    $self->state('draining_res') unless $backend;
396    return $self->{backend} = $backend;
397}
398
399# invoked by backend when it wants us to start watching for reads again
400# and feeding it data (if we have any)
401sub backend_ready {
402    my Perlbal::ClientProxy $self = $_[0];
403    my Perlbal::BackendHTTP $be = $_[1];
404
405    # if we'd turned ourselves off while we waited for a backend, turn
406    # ourselves back on, because the backend is ready for data now.
407    if ($self->{unread_data_waiting}) {
408        $self->watch_read(1);
409    }
410
411    # normal, not-buffered-to-disk case:
412    return $self->drain_read_buf_to($be) unless $self->{bureason};
413
414    # buffered-to-disk case.
415
416    # tell the backend it has to go into buffered_upload_mode,
417    # which makes it inform us of its writable availability
418    $be->invoke_buffered_upload_mode;
419}
420
421# our backend enqueues a call to this method in our write buffer, so this is called
422# right after we've finished sending all of the results to the user.  at this point,
423# if we were doing keep-alive, we don't close and setup for the next request.
424sub backend_finished {
425    my Perlbal::ClientProxy $self = shift;
426    print "ClientProxy::backend_finished\n" if Perlbal::DEBUG >= 3;
427
428    # mark ourselves as having responded (presumeably if we're here,
429    # the backend has responded already)
430    $self->{responded} = 1;
431
432    # our backend is done with us, so we disconnect ourselves from it
433    $self->{backend} = undef;
434
435    # backend is done sending data to us, so we can recycle this clientproxy
436    # if we don't have any data yet to read
437    return $self->http_response_sent unless $self->{unread_data_waiting};
438
439    # if we get here (and we do, rarely, in practice) then that means
440    # the backend read was empty/disconected (or otherwise messed up),
441    # and the only thing we can really do is close the client down.
442    $self->close("backend_finished_while_unread_data");
443}
444
445# called when we've sent a response to a user fully and we need to reset state
446sub http_response_sent {
447    my Perlbal::ClientProxy $self = $_[0];
448
449    # persistence logic is in ClientHTTPBase
450    return 0 unless $self->SUPER::http_response_sent;
451
452    print "ClientProxy::http_response_sent -- resetting state\n" if Perlbal::DEBUG >= 3;
453
454    if (my $be = $self->{backend}) {
455        $self->{backend} = undef;
456        $be->forget_client;
457    }
458
459    # if we get here we're being persistent, reset our state
460    $self->{backend_requested} = 0;
461    $self->{high_priority} = 0;
462    $self->{reproxy_uris} = undef;
463    $self->{reproxy_expected_size} = undef;
464    $self->{currently_reproxying} = undef;
465    $self->{content_length_remain} = undef;
466    $self->{primary_res_hdrs} = undef;
467    $self->{responded} = 0;
468    $self->{is_buffering} = 0;
469    $self->{is_writing} = 0;
470    $self->{start_time} = undef;
471    $self->{bufh} = undef;
472    $self->{bufilename} = undef;
473    $self->{buoutpos} = 0;
474    $self->{bureason} = undef;
475    $self->{upload_session} = undef;
476    $self->{chunked_upload_state} = undef;
477    $self->{request_body_length} = undef;
478    return 1;
479}
480
481# to request a backend connection AFTER you've already done so, if you
482# didn't like the results from the first one.  (like after a 500 error)
483sub rerequest_backend {
484    my Perlbal::ClientProxy $self = shift;
485
486    $self->{backend_requested} = 0;
487    $self->{backend} = undef;
488    $self->request_backend;
489}
490
491sub request_backend {
492    my Perlbal::ClientProxy $self = shift;
493    return if $self->{backend_requested};
494    $self->{backend_requested} = 1;
495
496    $self->state('wait_backend');
497    $self->{service}->request_backend_connection($self);
498    $self->tcp_cork(1);  # cork writes to self
499}
500
501# Client (overrides and calls super)
502sub close {
503    my Perlbal::ClientProxy $self = shift;
504    my $reason = shift;
505
506    # don't close twice
507    return if $self->{closed};
508
509    # signal that we're done
510    $self->{service}->run_hooks('end_proxy_request', $self);
511
512    # kill our backend if we still have one
513    if (my $backend = $self->{backend}) {
514        print "Client ($self) closing backend ($backend)\n" if Perlbal::DEBUG >= 1;
515        $self->backend(undef);
516        $backend->close($reason ? "proxied_from_client_close:$reason" : "proxied_from_client_close");
517    } else {
518        # if no backend, tell our service that we don't care for one anymore
519        $self->{service}->note_client_close($self);
520    }
521
522    # call ClientHTTPBase's close
523    $self->SUPER::close($reason);
524}
525
526sub client_disconnected { # : void
527    my Perlbal::ClientProxy $self = shift;
528    print "ClientProxy::client_disconnected\n" if Perlbal::DEBUG >= 2;
529
530    # if client disconnected, then we need to turn off watching for
531    # further reads and purge the existing upload if any. also, we
532    # should just return and do nothing else.
533
534    $self->watch_read(0);
535    $self->purge_buffered_upload if $self->{bureason};
536    return $self->close('user_disconnected');
537}
538
539# Client
540sub event_write {
541    my Perlbal::ClientProxy $self = shift;
542    print "ClientProxy::event_write\n" if Perlbal::DEBUG >= 3;
543
544    $self->SUPER::event_write;
545
546    # obviously if we're writing the backend has processed our request
547    # and we are responding/have responded to the user, so mark it so
548    $self->{responded} = 1;
549
550    # trigger our backend to keep reading, if it's still connected
551    if ($self->{backend_stalled} && (my $backend = $self->{backend})) {
552        print "  unstalling backend\n" if Perlbal::DEBUG >= 3;
553
554        $self->{backend_stalled} = 0;
555        $backend->watch_read(1);
556    }
557}
558
559# ClientProxy
560sub event_read {
561    my Perlbal::ClientProxy $self = shift;
562    print "ClientProxy::event_read\n" if Perlbal::DEBUG >= 3;
563
564    # mark alive so we don't get killed for being idle
565    $self->{alive_time} = time;
566
567    # if we have no headers, the only thing we can do is try to get some
568    if (! $self->{req_headers}) {
569        print "  no headers.  reading.\n" if Perlbal::DEBUG >= 3;
570        $self->handle_request if $self->read_request_headers;
571        return;
572    }
573
574    # if we're buffering to disk or haven't read too much from this client, keep reading,
575    # otherwise shut off read notifications
576    unless ($self->{is_buffering} || $self->{read_ahead} < READ_AHEAD_SIZE) {
577        # our buffer is full, so turn off reads for now
578        print "  disabling reads.\n" if Perlbal::DEBUG >= 3;
579        $self->watch_read(0);
580        return;
581    }
582
583    # deal with chunked uploads
584    if (my $cus = $self->{chunked_upload_state}) {
585        $cus->on_readable($self);
586
587        # if we got more than 1MB not flushed to disk,
588        # stop reading for a bit until disk catches up
589        if ($self->{read_ahead} > 1024*1024) {
590            $self->watch_read(0);
591        }
592        return;
593    }
594
595    # read more data if we're still buffering or if our current read buffer
596    # is not full to the max READ_AHEAD_SIZE which is how much data we will
597    # buffer in from the user before passing on to the backend
598
599    # read the MIN(READ_SIZE, content_length_remain)
600    my $read_size = READ_SIZE;
601    my $remain = $self->{content_length_remain};
602
603    $read_size = $remain if $remain && $remain < $read_size;
604    print "  reading $read_size bytes (", (defined $remain ? $remain : "(undef)"), " bytes remain)\n" if Perlbal::DEBUG >= 3;
605
606    my $bref = $self->read($read_size);
607
608    # if the read returned undef, that means the connection was closed
609    # (see: Danga::Socket::read)
610    return $self->client_disconnected unless defined $bref;
611
612    # if they didn't declare a content body length and we just got a
613    # readable event that's not a disconnect, something's messed up.
614    # they're overflowing us.  disconnect!
615    if (! $remain) {
616        $self->_simple_response(400, "Can't pipeline to HTTP/1.0");
617        $self->close("pipelining_to_http10");
618        return;
619    }
620
621    # now that we know we have a defined value, determine how long it is, and do
622    # housekeeping to keep our tracking numbers up to date.
623    my $len = length($$bref);
624    print "  read $len bytes\n" if Perlbal::DEBUG >= 3;
625
626    # when run under the program "trickle", epoll speaks the truth to
627    # us, but then trickle interferes and steals our reads/writes, so
628    # this fails.  normally this check isn't needed.
629    return unless $len;
630
631    $self->{read_size} += $len;
632    $self->{content_length_remain} -= $len if $remain;
633
634    my $done_reading = defined $self->{content_length_remain} && $self->{content_length_remain} <= 0;
635    my $backend = $self->backend;
636    print("  done_reading = $done_reading, backend = ", ($backend || "<undef>"), "\n") if Perlbal::DEBUG >= 3;
637
638    # upload tracking
639    if (my $session = $self->{upload_session}) {
640        my $cl = $self->{req_headers}->content_length;
641        my $remain = $self->{content_length_remain};
642        my $now = time();  # FIXME: more efficient?
643        if ($cl && $remain && ($self->{last_upload_packet} || 0) != $now) {
644            my $done = $cl - $remain;
645            $self->{last_upload_packet} = $now;
646            $udp_sock ||= IO::Socket::INET->new(Proto => 'udp');
647            my $since = $self->{last_request_time};
648            my $send = "UPLOAD:$session:$done:$cl:$since:$now";
649            if ($udp_sock) {
650                foreach my $ep (@{ $self->{service}{upload_status_listeners_sockaddr} }) {
651                    my $rv = $udp_sock->send($send, 0, $ep);
652                }
653            }
654        }
655    }
656
657    # just dump the read into the nether if we're dangling. that is
658    # the case when we send the headers to the backend and it responds
659    # before we're done reading from the client; therefore further
660    # reads from the client just need to be sent nowhere, because the
661    # RFC2616 section 8.2.3 says: "the server SHOULD NOT close the
662    # transport connection until it has read the entire request"
663    if ($self->{responded}) {
664        print "  already responded.\n" if Perlbal::DEBUG >= 3;
665        # in addition, if we're now out of data (clr == 0), then we should
666        # either close ourselves or get ready for another request
667        return $self->http_response_sent if $done_reading;
668
669        print "  already responded [2].\n" if Perlbal::DEBUG >= 3;
670        # at this point, if the backend has responded then we just return
671        # as we don't want to send it on to them or buffer it up, which is
672        # what the code below does
673        return;
674    }
675
676    # if we have no data left to read, stop reading.  all that can
677    # come later is an extra \r\n which we handle later when parsing
678    # new request headers.  and if it's something else, we'll bail on
679    # the next request, not this one.
680    if ($done_reading) {
681        Carp::confess("content_length_remain less than zero: self->{content_length_remain}")
682            if $self->{content_length_remain} < 0;
683        $self->{unread_data_waiting} = 0;
684        $self->watch_read(0);
685    }
686
687    # now, if we have a backend, then we should be writing it to the backend
688    # and not doing anything else
689    if ($backend) {
690        print "  got a backend.  sending write to it.\n" if Perlbal::DEBUG >= 3;
691        $backend->write($bref);
692        # TODO: monitor the backend's write buffer depth?
693        return;
694    }
695
696    # now, we know we don't have a backend, so we have to push this data onto our
697    # read buffer... it's not going anywhere yet
698    push @{$self->{read_buf}}, $bref;
699    $self->{read_ahead} += $len;
700    print "  no backend.  read_ahead = $self->{read_ahead}.\n" if Perlbal::DEBUG >= 3;
701
702    # if we know we've already started spooling a file to disk, then continue
703    # to do that.
704    print "  bureason = $self->{bureason}\n" if Perlbal::DEBUG >= 3 && $self->{bureason};
705    return $self->buffered_upload_update if $self->{bureason};
706
707    # if we are under our buffer-to-memory size, just continue buffering here and
708    # don't fall through to the backend request call below
709    return if
710        ! $done_reading &&
711        $self->{read_ahead} < $self->{service}->{buffer_backend_connect};
712
713    # over the buffer-to-memory size, see if we should start spooling to disk.
714    return if $self->{service}->{buffer_uploads} && $self->decide_to_buffer_to_disk;
715
716    # give plugins a chance to act on the request before we request a backend
717    # (added by Chris Hondl <chris@imvu.com>, March 2006)
718    my $svc = $self->{service};
719    return if $svc->run_hook('proxy_read_request', $self);
720
721    # if we fall through to here, we need to ensure that a backend is on the
722    # way, because no specialized handling took over above
723    print "  finally requesting a backend\n" if Perlbal::DEBUG >= 3;
724    return $self->request_backend;
725}
726
727sub handle_request {
728    my Perlbal::ClientProxy $self = shift;
729    my $req_hd = $self->{req_headers};
730
731    my $svc = $self->{service};
732    # give plugins a chance to force us to bail
733    return if $svc->run_hook('start_proxy_request', $self);
734    return if $svc->run_hook('start_http_request',  $self);
735
736    if ($self->handle_chunked_upload) {
737        # handled in method.
738    } else {
739        # if defined we're waiting on some amount of data.  also, we have to
740        # subtract out read_size, which is the amount of data that was
741        # extra in the packet with the header that's part of the body.
742        $self->{request_body_length} =
743            $self->{content_length_remain} =
744            $req_hd->content_length;
745        $self->{unread_data_waiting} = 1 if $self->{content_length_remain};
746    }
747
748    # upload-tracking stuff.  both starting a new upload track session,
749    # and checking on status of ongoing one
750    return if $svc->{upload_status_listeners} && $self->handle_upload_tracking;
751
752    # note that we've gotten a request
753    $self->{requests}++;
754    $self->{last_request_time} = $self->{alive_time};
755
756    # either start buffering some of the request to memory, or
757    # immediately request a backend connection.
758    if ($self->{chunked_upload_state}) {
759        $self->{request_body_length} = 0;
760        $self->{is_buffering} = 1;
761        $self->{bureason} = 'chunked';
762        $self->buffered_upload_update;
763    } elsif ($self->{content_length_remain} && $self->{service}->{buffer_backend_connect}) {
764        # the deeper path
765        $self->start_buffering_request;
766    } else {
767        # get the backend request process moving, since we aren't buffering
768        $self->{is_buffering} = 0;
769
770        # if reproxy-caching is enabled, we can often bypass needing to allocate a BackendHTTP connection:
771        return if $svc->{reproxy_cache} && $self->satisfy_request_from_cache;
772
773        $self->request_backend;
774    }
775}
776
777sub handle_chunked_upload {
778    my Perlbal::ClientProxy $self = shift;
779    my $req_hd = $self->{req_headers};
780    my $te = $req_hd->header("Transfer-Encoding");
781    return unless $te && $te eq "chunked";
782    return unless $self->{service}->{buffer_uploads};
783
784    $req_hd->header("Transfer-Encoding", undef); # remove it (won't go to backend)
785
786    my $eh = $req_hd->header("Expect");
787    if ($eh && $eh =~ /\b100-continue\b/) {
788        $self->write(\ "HTTP/1.1 100 Continue\r\n\r\n");
789        $req_hd->header("Expect", undef); # remove it (won't go to backend)
790    }
791
792    my $args = {
793        on_new_chunk => sub {
794            my $cref = shift;
795            my $len = length($$cref);
796            push @{$self->{read_buf}}, $cref;
797            $self->{read_ahead}          += $len;
798            $self->{request_body_length} += $len;
799            # TODO: if too large, disconnect?
800            $self->buffered_upload_update;
801        },
802        on_disconnect => sub {
803            $self->client_disconnected;
804        },
805        on_zero_chunk => sub {
806            $self->send_buffered_upload;
807        },
808    };
809
810    $self->{chunked_upload_state} = Perlbal::ChunkedUploadState->new(%$args);
811    return 1;
812}
813
814sub satisfy_request_from_cache {
815    my Perlbal::ClientProxy $self = shift;
816
817    my $req_hd = $self->{req_headers};
818    my $svc    = $self->{service};
819    my $cache  = $svc->{reproxy_cache};
820    $svc->{_stat_requests}++;
821
822    my $requri   = $req_hd->request_uri    || '';
823    my $hostname = $req_hd->header("Host") || '';
824
825    my $key      = "$hostname|$requri";
826
827    my $reproxy  = $cache->get($key) or
828        return 0;
829
830    my ($timeout, $headers, $urls) = @$reproxy;
831    return 0 if time() > $timeout;
832
833    $svc->{_stat_cache_hits}++;
834    my %headers = map { ref $_ eq 'SCALAR' ? $$_ : $_ } @{$headers || []};
835
836    if (my $ims = $req_hd->header("If-Modified-Since")) {
837        my ($lm_key) = grep { uc($_) eq "LAST-MODIFIED" } keys %headers;
838        my $lm = $headers{$lm_key} || "";
839
840        # remove the IE length suffix
841        $ims =~ s/; length=(\d+)//;
842
843        # If 'Last-Modified' is same as 'If-Modified-Since', send a 304
844        if ($ims eq $lm) {
845            my $res_hd = Perlbal::HTTPHeaders->new_response(304);
846            $res_hd->header("Content-Length", "0");
847            $self->tcp_cork(1);
848            $self->state('xfer_resp');
849            $self->write($res_hd->to_string_ref);
850            $self->write(sub { $self->http_response_sent; });
851            return 1;
852        }
853    }
854
855    my $res_hd = Perlbal::HTTPHeaders->new_response(200);
856    $res_hd->header("Date", HTTP::Date::time2str(time()));
857    while (my ($key, $value) = each %headers) {
858        $res_hd->header($key, $value);
859    }
860
861    $self->start_reproxy_uri($res_hd, $urls);
862    return 1;
863}
864
865# return 1 to steal this connection (when they're asking status of an
866# upload session), return 0 to return it to handle_request's control.
867sub handle_upload_tracking {
868    my Perlbal::ClientProxy $self = shift;
869    my $req_hd = $self->{req_headers};
870
871    return 0 unless
872        $req_hd->request_uri =~ /[\?&]client_up_sess=(\w{5,50})\b/;
873
874    my $sess = $1;
875
876    # getting status?
877    if ($req_hd->request_uri =~ m!^/__upload_status\?!) {
878        my $status = Perlbal::UploadListener::get_status($sess);
879        my $now = time();
880        my $body = $status ?
881            "{done:$status->{done},total:$status->{total},starttime:$status->{starttime},nowtime:$now}" :
882            "{}";
883
884        my $res = $self->{res_headers} = Perlbal::HTTPHeaders->new_response(200);
885        $res->header("Content-Type", "text/plain");
886        $res->header('Content-Length', length $body);
887        $self->setup_keepalive($res);
888        $self->tcp_cork(1);  # cork writes to self
889        $self->write($res->to_string_ref);
890        $self->write(\ $body);
891        $self->write(sub { $self->http_response_sent; });
892        return 1;
893    }
894
895    # otherwise just tagging this upload as a new upload session
896    $self->{upload_session} = $sess;
897    return 0;
898}
899
900# continuation of handle_request, in the case where we need to start buffering
901# a bit of the request body to memory, either hoping that's all of it, or to
902# make a determination of whether or not we should save it all to disk first
903sub start_buffering_request {
904    my Perlbal::ClientProxy $self = shift;
905
906    # buffering case:
907    $self->{is_buffering} = 1;
908
909    # shortcut: if we know that we're buffering by size, and the size
910    # of this upload is bigger than that value, we can just turn on spool
911    # to disk right now...
912    if ($self->{service}->{buffer_uploads} && $self->{service}->{buffer_upload_threshold_size}) {
913        my $req_hd = $self->{req_headers};
914        if ($req_hd->content_length >= $self->{service}->{buffer_upload_threshold_size}) {
915            $self->{bureason} = 'size';
916            if ($ENV{PERLBAL_DEBUG_BUFFERED_UPLOADS}) {
917                $self->{req_headers}->header('X-PERLBAL-BUFFERED-UPLOAD-REASON', 'size');
918            }
919            $self->state('buffering_upload');
920            $self->buffered_upload_update;
921            return;
922        }
923    }
924
925    # well, we're buffering, but we're not going to disk just yet (but still might)
926    $self->state('buffering_request');
927
928    # only need time if we are using the buffer to disk functionality
929    $self->{start_time} = [ gettimeofday() ]
930        if $self->{service}->{buffer_uploads};
931}
932
933# looks at our states and decides if we should start writing to disk
934# or should just go ahead and blast this to the backend.  returns 1
935# if the decision was made to buffer to disk
936sub decide_to_buffer_to_disk {
937    my Perlbal::ClientProxy $self = shift;
938    return unless $self->{is_buffering};
939    return $self->{bureason} if defined $self->{bureason};
940
941    # this is called when we have enough data to determine whether or not to
942    # start buffering to disk
943    my $dur = tv_interval($self->{start_time}) || 1;
944    my $rate = $self->{read_ahead} / $dur;
945    my $etime = $self->{content_length_remain} / $rate;
946
947    # see if we have enough data to make the determination
948    my $reason = undef;
949
950    # see if we blow the rate away
951    if ($self->{service}->{buffer_upload_threshold_rate} > 0 &&
952            $rate < $self->{service}->{buffer_upload_threshold_rate}) {
953        # they are slower than the minimum rate
954        $reason = 'rate';
955    }
956
957    # and finally check estimated time exceeding
958    if ($self->{service}->{buffer_upload_threshold_time} > 0 &&
959            $etime > $self->{service}->{buffer_upload_threshold_time}) {
960        # exceeds
961        $reason = 'time';
962    }
963
964    unless ($reason) {
965        $self->{is_buffering} = 0;
966        return 0;
967    }
968
969    # start saving it to disk
970    $self->state('buffering_upload');
971    $self->buffered_upload_update;
972    $self->{bureason} = $reason;
973
974    if ($ENV{PERLBAL_DEBUG_BUFFERED_UPLOADS}) {
975        $self->{req_headers}->header('X-PERLBAL-BUFFERED-UPLOAD-REASON', $reason);
976    }
977
978    return 1;
979}
980
981# take ourselves and send along our buffered data to the backend
982sub send_buffered_upload {
983    my Perlbal::ClientProxy $self = shift;
984
985    # make sure our buoutpos is the same as the content length...
986    return if $self->{is_writing};
987
988    # set the content-length that goes to the backend...
989    if ($self->{chunked_upload_state}) {
990        $self->{req_headers}->header("Content-Length", $self->{request_body_length});
991    }
992
993    my $clen = $self->{req_headers}->content_length;
994    if ($clen != $self->{buoutpos}) {
995        Perlbal::log('critical', "Content length of $clen declared but $self->{buoutpos} bytes written to disk");
996        return $self->_simple_response(500);
997    }
998
999    # reset our position so we start reading from the right spot
1000    $self->{buoutpos} = 0;
1001    sysseek($self->{bufh}, 0, 0);
1002
1003    # notify that we want the backend so we get the ball rolling
1004    $self->request_backend;
1005}
1006
1007sub continue_buffered_upload {
1008    my Perlbal::ClientProxy $self = shift;
1009    my Perlbal::BackendHTTP $be = shift;
1010    return unless $self && $be;
1011
1012    # now send the data
1013    my $clen = $self->{request_body_length};
1014
1015    my $sent = Perlbal::Socket::sendfile($be->{fd}, fileno($self->{bufh}), $clen - $self->{buoutpos});
1016    if ($sent < 0) {
1017        return $self->close("epipe") if $! == EPIPE;
1018        return $self->close("connreset") if $! == ECONNRESET;
1019        print STDERR "Error w/ sendfile: $!\n";
1020        return $self->close('sendfile_error');
1021    }
1022    $self->{buoutpos} += $sent;
1023
1024    # if we're done, purge the file and move on
1025    if ($self->{buoutpos} >= $clen) {
1026        $be->{buffered_upload_mode} = 0;
1027        $self->purge_buffered_upload;
1028        return;
1029    }
1030
1031    # we will be called again by the backend since buffered_upload_mode is on
1032}
1033
1034# write data to disk
1035sub buffered_upload_update {
1036    my Perlbal::ClientProxy $self = shift;
1037    return if $self->{is_writing};
1038    return unless $self->{is_buffering} && $self->{read_ahead};
1039
1040    # so we're not writing now and we have data to write...
1041    unless ($self->{bufilename}) {
1042        # create a filename and see if it exists or not
1043        $self->{is_writing} = 1;
1044        my $fn = join('-', $self->{service}->name, $self->{service}->listenaddr, "client", $self->{fd}, int(rand(0xffffffff)));
1045        $fn = $self->{service}->{buffer_uploads_path} . '/' . $fn;
1046
1047        # good, now we need to create the file
1048        Perlbal::AIO::aio_open($fn, O_CREAT | O_TRUNC | O_RDWR, 0644, sub {
1049            $self->{is_writing} = 0;
1050            $self->{bufh} = shift;
1051
1052            # throw errors back to the user
1053            if (! $self->{bufh}) {
1054                Perlbal::log('critical', "Failure to open $fn for buffered upload output");
1055                return $self->_simple_response(500);
1056            }
1057
1058            # save state and info and bounce it back to write data
1059            $self->{bufilename} = $fn;
1060            $self->buffered_upload_update;
1061        });
1062
1063        return;
1064    }
1065
1066    # at this point, we want to do some writing
1067    my $bref = shift(@{$self->{read_buf}});
1068    my $len = length $$bref;
1069    $self->{read_ahead} -= $len;
1070
1071    # so at this point we have a valid filename and file handle and should write out
1072    # the buffer that we have
1073    $self->{is_writing} = 1;
1074    Perlbal::AIO::aio_write($self->{bufh}, $self->{buoutpos}, $len, $$bref, sub {
1075        my $bytes = shift;
1076        $self->{is_writing} = 0;
1077
1078        # check for error
1079        unless ($bytes) {
1080            Perlbal::log('critical', "Error writing buffered upload: $!.  Tried to do $len bytes at $self->{buoutpos}.");
1081            return $self->_simple_response(500);
1082        }
1083
1084        # update our count of data written
1085        $self->{buoutpos} += $bytes;
1086
1087        # now check if we wrote less than we had in this chunk of buffer.  if that's
1088        # the case then we need to reenqueue the part of the chunk that wasn't
1089        # written out and update as appropriate.
1090        if ($bytes < $len) {
1091            my $diff = $len - $bytes;
1092            unshift @{$self->{read_buf}}, \ substr($$bref, $bytes, $diff);
1093            $self->{read_ahead} += $diff;
1094        }
1095
1096        # if we're processing a chunked upload, ...
1097        if ($self->{chunked_upload_state}) {
1098            # turn reads back on, if we haven't hit the end yet.
1099            if ($self->{unread_data_waiting} && $self->{read_ahead} < 1024*1024) {
1100                $self->watch_read(1);
1101                $self->{unread_data_waiting} = 0;
1102            }
1103
1104            if ($self->{read_ahead} == 0 && $self->{chunked_upload_state}->hit_zero_chunk) {
1105                $self->watch_read(0);
1106                $self->send_buffered_upload;
1107                return;
1108            }
1109        }
1110
1111        # if we're done (no clr and no read ahead!) then send it
1112        elsif ($self->{read_ahead} <= 0 && $self->{content_length_remain} <= 0) {
1113            $self->send_buffered_upload;
1114            return;
1115        }
1116
1117        # spawn another writer!
1118        $self->buffered_upload_update;
1119    });
1120}
1121
1122# destroy any files we've created
1123sub purge_buffered_upload {
1124    my Perlbal::ClientProxy $self = shift;
1125
1126    # FIXME: it's reported that sometimes the two now-in-eval blocks
1127    # fail, hence the eval blocks and warnings.  the FIXME is to
1128    # figure this out, why it happens sometimes.
1129
1130    # first close our filehandle... not async
1131    eval {
1132        CORE::close($self->{bufh});
1133    };
1134    if ($@) { warn "Error closing file in ClientProxy::purge_buffered_upload: $@\n"; }
1135
1136    $self->{bufh} = undef;
1137
1138    eval {
1139        # now asyncronously unlink the file
1140        Perlbal::AIO::aio_unlink($self->{bufilename}, sub {
1141            if ($!) {
1142                # note an error, but whatever, we'll either overwrite the file later (O_TRUNC | O_CREAT)
1143                # or a cleaner will come through and do it for us someday (if the user runs one)
1144                Perlbal::log('warning', "Unable to link $self->{bufilename}: $!");
1145              }
1146        });
1147    };
1148    if ($@) { warn "Error unlinking file in ClientProxy::purge_buffered_upload: $@\n"; }
1149}
1150
1151# returns bool; whether backend should hide the 500 error from the client
1152#   and have us try a new backend.  return true to retry, false to get a 500 error.
1153sub should_retry_after_500 {
1154    my Perlbal::ClientProxy $self = shift;
1155    my Perlbal::BackendHTTP $be   = shift;
1156    my $svc = $be->{service};
1157    return 0 unless $svc->{enable_error_retries};
1158    my @sched = split(/\s*,\s*/, $svc->{error_retry_schedule});
1159    return 0 if ++$self->{retry_count} > @sched;
1160    return 1;
1161}
1162
1163# called by Backend to tell us it got a 500 error and we should retry another backend.
1164sub retry_after_500 {
1165    my Perlbal::ClientProxy $self = shift;
1166    my Perlbal::Service     $svc  = shift;
1167
1168    my @sched = split(/\s*,\s*/, $svc->{error_retry_schedule});
1169    my $delay = $sched[$self->{retry_count} - 1];
1170
1171    if ($delay) {
1172        Danga::Socket->AddTimer($delay, sub {
1173            return if $self->{closed};
1174            $self->rerequest_backend;
1175        });
1176    } else {
1177        $self->rerequest_backend;
1178    }
1179
1180}
1181
1182sub as_string {
1183    my Perlbal::ClientProxy $self = shift;
1184
1185    my $ret = $self->SUPER::as_string;
1186    if ($self->{backend}) {
1187        my $ipport = $self->{backend}->{ipport};
1188        $ret .= "; backend=$ipport";
1189    } else {
1190        $ret .= "; write_buf_size=$self->{write_buf_size}"
1191            if $self->{write_buf_size} > 0;
1192    }
1193    $ret .= "; highpri" if $self->{high_priority};
1194    $ret .= "; lowpri" if $self->{low_priority};
1195    $ret .= "; responded" if $self->{responded};
1196    $ret .= "; waiting_for=" . $self->{content_length_remain}
1197        if defined $self->{content_length_remain};
1198    $ret .= "; reproxying" if $self->{currently_reproxying};
1199
1200    return $ret;
1201}
1202
1203sub DESTROY {
1204    Perlbal::objdtor($_[0]);
1205    $_[0]->SUPER::DESTROY;
1206}
1207
12081;
1209
1210
1211# Local Variables:
1212# mode: perl
1213# c-basic-indent: 4
1214# indent-tabs-mode: nil
1215# End:
Note: See TracBrowser for help on using the browser.