This repository has been archived by the owner on Apr 21, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 158
/
serf_url_async_fetcher.cc
1551 lines (1401 loc) · 57.2 KB
/
serf_url_async_fetcher.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright 2010 Google Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// Author: jmarantz@google.com (Joshua Marantz)
// lsong@google.com (Libo Song)
// TODO(jmarantz): Avoid initiating fetches for resources already in flight.
// The challenge is that we would want to call all the callbacks that indicated
// interest in a particular URL once the callback completed. Alternatively,
// this could be done in a level above the URL fetcher.
#include "pagespeed/system/serf_url_async_fetcher.h"
#include <cstddef>
#include <list>
#include <vector>
#include "apr.h"
#include "apr_strings.h"
#include "apr_pools.h"
#include "apr_thread_proc.h"
#include "base/logging.h"
#include "net/instaweb/http/public/async_fetch.h"
#include "net/instaweb/public/global_constants.h"
#include "net/instaweb/public/version.h"
#include "pagespeed/system/apr_thread_compatible_pool.h"
#include "pagespeed/kernel/base/abstract_mutex.h"
#include "pagespeed/kernel/base/basictypes.h"
#include "pagespeed/kernel/base/condvar.h"
#include "pagespeed/kernel/base/message_handler.h"
#include "pagespeed/kernel/base/pool.h"
#include "pagespeed/kernel/base/scoped_ptr.h"
#include "pagespeed/kernel/base/statistics.h"
#include "pagespeed/kernel/base/string_util.h"
#include "pagespeed/kernel/base/thread_system.h"
#include "pagespeed/kernel/base/timer.h"
#include "pagespeed/kernel/http/google_url.h"
#include "pagespeed/kernel/http/http_names.h"
#include "pagespeed/kernel/http/request_headers.h"
#include "pagespeed/kernel/http/response_headers.h"
// This is an easy way to turn on lots of debug messages. Note that this
// is somewhat verbose.
#define SERF_DEBUG(x)
namespace {
enum HttpsOptions {
kEnableHttps = 1 << 0,
kAllowSelfSigned = 1 << 1,
kAllowUnknownCertificateAuthority = 1 << 2,
kAllowCertificateNotYetValid = 1 << 3,
};
} // namespace
extern "C" {
// Declares new functions added to
// src/third_party/serf/instaweb_context.c
serf_bucket_t* serf_request_bucket_request_create_for_host(
serf_request_t *request,
const char *method,
const char *uri,
serf_bucket_t *body,
serf_bucket_alloc_t *allocator, const char* host);
int serf_connection_is_in_error_state(serf_connection_t* connection);
// Declares new functions added in instaweb_ssl_buckets.c
apr_status_t serf_ssl_set_certificates_directory(serf_ssl_context_t *ssl_ctx,
const char* path);
apr_status_t serf_ssl_set_certificates_file(serf_ssl_context_t *ssl_ctx,
const char* file);
int serf_ssl_check_host(const serf_ssl_certificate_t *cert,
const char* hostname);
} // extern "C"
namespace net_instaweb {
const char SerfStats::kSerfFetchRequestCount[] = "serf_fetch_request_count";
const char SerfStats::kSerfFetchByteCount[] = "serf_fetch_bytes_count";
const char SerfStats::kSerfFetchTimeDurationMs[] =
"serf_fetch_time_duration_ms";
const char SerfStats::kSerfFetchCancelCount[] = "serf_fetch_cancel_count";
const char SerfStats::kSerfFetchActiveCount[] =
"serf_fetch_active_count";
const char SerfStats::kSerfFetchTimeoutCount[] = "serf_fetch_timeout_count";
const char SerfStats::kSerfFetchFailureCount[] = "serf_fetch_failure_count";
const char SerfStats::kSerfFetchCertErrors[] = "serf_fetch_cert_errors";
GoogleString GetAprErrorString(apr_status_t status) {
char error_str[1024];
apr_strerror(status, error_str, sizeof(error_str));
return error_str;
}
SerfFetch::SerfFetch(const GoogleString& url,
AsyncFetch* async_fetch,
MessageHandler* message_handler,
Timer* timer)
: fetcher_(NULL),
timer_(timer),
str_url_(url),
async_fetch_(async_fetch),
parser_(async_fetch->response_headers()),
status_line_read_(false),
one_byte_read_(false),
has_saved_byte_(false),
saved_byte_('\0'),
message_handler_(message_handler),
pool_(NULL), // filled in once assigned to a thread, to use its pool.
bucket_alloc_(NULL),
host_header_(NULL),
sni_host_(NULL),
connection_(NULL),
bytes_received_(0),
fetch_start_ms_(0),
fetch_end_ms_(0),
using_https_(false),
ssl_context_(NULL),
ssl_error_message_(NULL) {
memset(&url_, 0, sizeof(url_));
}
SerfFetch::~SerfFetch() {
DCHECK(async_fetch_ == NULL);
if (connection_ != NULL) {
serf_connection_close(connection_);
}
if (pool_ != NULL) {
apr_pool_destroy(pool_);
}
}
GoogleString SerfFetch::DebugInfo() {
if (host_header_ != NULL &&
url_.scheme != NULL &&
url_.hostinfo != NULL) {
GoogleUrl base(StrCat(url_.scheme, "://", host_header_));
if (base.IsWebValid()) {
const char* url_path = apr_uri_unparse(pool_, &url_,
APR_URI_UNP_OMITSITEPART);
GoogleUrl abs_url(base, url_path);
if (abs_url.IsWebValid()) {
GoogleString debug_info;
abs_url.Spec().CopyToString(&debug_info);
if (StringPiece(url_.hostinfo) != host_header_) {
StrAppend(&debug_info, " (connecting to:", url_.hostinfo, ")");
}
return debug_info;
}
}
}
return str_url_;
}
void SerfFetch::Cancel() {
if (connection_ != NULL) {
// We can get here either because we're canceling the connection ourselves
// or because Serf detected an error.
//
// If we canceled/timed out, we want to close the serf connection so it
// doesn't call us back, as we will detach from the async_fetch_ shortly.
//
// If Serf detected an error we also want to clean up as otherwise it will
// keep re-detecting it, which will interfere with other jobs getting
// handled (until we finally cleanup the old fetch and close things in
// ~SerfFetch).
serf_connection_close(connection_);
connection_ = NULL;
}
CallCallback(false);
}
void SerfFetch::CallCallback(bool success) {
if (ssl_error_message_ != NULL) {
success = false;
}
if (async_fetch_ != NULL) {
fetch_end_ms_ = timer_->NowMs();
fetcher_->ReportCompletedFetchStats(this);
CallbackDone(success);
fetcher_->FetchComplete(this);
} else if (ssl_error_message_ == NULL) {
LOG(FATAL) << "BUG: Serf callback called more than once on same fetch "
<< DebugInfo() << " (" << this << "). Please report this "
<< "at http://code.google.com/p/modpagespeed/issues/";
}
}
void SerfFetch::CallbackDone(bool success) {
// fetcher_==NULL if Start is called during shutdown.
if (fetcher_ != NULL) {
if (!success) {
fetcher_->failure_count_->Add(1);
}
if (fetcher_->track_original_content_length() &&
!async_fetch_->response_headers()->Has(
HttpAttributes::kXOriginalContentLength)) {
async_fetch_->extra_response_headers()->SetOriginalContentLength(
bytes_received_);
}
}
async_fetch_->Done(success);
// We should always NULL the async_fetch_ out after calling otherwise we
// could get weird double calling errors.
async_fetch_ = NULL;
}
void SerfFetch::CleanupIfError() {
if ((connection_ != NULL) &&
serf_connection_is_in_error_state(connection_)) {
message_handler_->Message(
kInfo, "Serf cleanup for error'd fetch of: %s", DebugInfo().c_str());
Cancel();
}
}
int64 SerfFetch::TimeDuration() const {
if ((fetch_start_ms_ != 0) && (fetch_end_ms_ != 0)) {
return fetch_end_ms_ - fetch_start_ms_;
} else {
return 0;
}
}
#if SERF_HTTPS_FETCHING
// static
apr_status_t SerfFetch::SSLCertValidate(void *data, int failures,
const serf_ssl_certificate_t *cert) {
return static_cast<SerfFetch*>(data)->HandleSSLCertValidation(
failures, 0, cert);
}
// static
apr_status_t SerfFetch::SSLCertChainValidate(
void *data, int failures, int error_depth,
const serf_ssl_certificate_t * const *certs,
apr_size_t certs_count) {
return static_cast<SerfFetch*>(data)->HandleSSLCertValidation(
failures, error_depth, NULL);
}
#endif
// static
apr_status_t SerfFetch::ConnectionSetup(
apr_socket_t* socket, serf_bucket_t **read_bkt, serf_bucket_t **write_bkt,
void* setup_baton, apr_pool_t* pool) {
SerfFetch* fetch = static_cast<SerfFetch*>(setup_baton);
*read_bkt = serf_bucket_socket_create(socket, fetch->bucket_alloc_);
#if SERF_HTTPS_FETCHING
apr_status_t status = APR_SUCCESS;
if (fetch->using_https_) {
*read_bkt = serf_bucket_ssl_decrypt_create(*read_bkt,
fetch->ssl_context_,
fetch->bucket_alloc_);
if (fetch->ssl_context_ == NULL) {
fetch->ssl_context_ = serf_bucket_ssl_decrypt_context_get(*read_bkt);
if (fetch->ssl_context_ == NULL) {
status = APR_EGENERAL;
} else {
SerfUrlAsyncFetcher* fetcher = fetch->fetcher_;
const GoogleString& certs_dir = fetcher->ssl_certificates_dir();
const GoogleString& certs_file = fetcher->ssl_certificates_file();
if (!certs_file.empty()) {
status = serf_ssl_set_certificates_file(
fetch->ssl_context_, certs_file.c_str());
}
if ((status == APR_SUCCESS) && !certs_dir.empty()) {
status = serf_ssl_set_certificates_directory(fetch->ssl_context_,
certs_dir.c_str());
}
// If no explicit file or directory is specified, then use the
// compiled-in default.
if (certs_dir.empty() && certs_file.empty()) {
status = serf_ssl_use_default_certificates(fetch->ssl_context_);
}
}
if (status != APR_SUCCESS) {
return status;
}
}
serf_ssl_server_cert_callback_set(
fetch->ssl_context_, SSLCertValidate, fetch);
serf_ssl_server_cert_chain_callback_set(
fetch->ssl_context_, SSLCertValidate, SSLCertChainValidate, fetch);
status = serf_ssl_set_hostname(fetch->ssl_context_, fetch->sni_host_);
if (status != APR_SUCCESS) {
LOG(INFO) << "Unable to set hostname from serf fetcher. Connection "
"setup failed";
return status;
}
*write_bkt = serf_bucket_ssl_encrypt_create(*write_bkt,
fetch->ssl_context_,
fetch->bucket_alloc_);
}
#endif
return APR_SUCCESS;
}
// static
void SerfFetch::ClosedConnection(serf_connection_t* conn,
void* closed_baton,
apr_status_t why,
apr_pool_t* pool) {
SerfFetch* fetch = static_cast<SerfFetch*>(closed_baton);
if (why != APR_SUCCESS) {
fetch->message_handler_->Warning(
fetch->DebugInfo().c_str(), 0, "Connection close (code=%d %s).",
why, GetAprErrorString(why).c_str());
}
// Connection is closed.
fetch->connection_ = NULL;
}
// static
serf_bucket_t* SerfFetch::AcceptResponse(serf_request_t* request,
serf_bucket_t* stream,
void* acceptor_baton,
apr_pool_t* pool) {
// Get the per-request bucket allocator.
serf_bucket_alloc_t* bucket_alloc = serf_request_get_alloc(request);
// Create a barrier so the response doesn't eat us!
// From the comment in Serf:
// ### the stream does not have a barrier, this callback should generally
// ### add a barrier around the stream before incorporating it into a
// ### response bucket stack.
// ... i.e. the passed bucket becomes owned rather than
// ### borrowed.
serf_bucket_t* bucket = serf_bucket_barrier_create(stream, bucket_alloc);
return serf_bucket_response_create(bucket, bucket_alloc);
}
// The handler MUST process data from the response bucket until the
// bucket's read function states it would block (APR_STATUS_IS_EAGAIN).
// The handler is invoked only when new data arrives. If no further data
// arrives, and the handler does not process all available data, then the
// system can result in a deadlock around the unprocessed, but read, data.
//
// static
apr_status_t SerfFetch::HandleResponse(serf_request_t* request,
serf_bucket_t* response,
void* handler_baton,
apr_pool_t* pool) {
SerfFetch* fetch = static_cast<SerfFetch*>(handler_baton);
return fetch->HandleResponse(response);
}
// static
bool SerfFetch::MoreDataAvailable(apr_status_t status) {
// This OR is structured like this to make debugging easier, as it's
// not obvious when looking at the status mask which of these conditions
// is hit.
if (APR_STATUS_IS_EAGAIN(status)) {
return true;
}
return APR_STATUS_IS_EINTR(status);
}
// static
bool SerfFetch::IsStatusOk(apr_status_t status) {
return ((status == APR_SUCCESS) ||
APR_STATUS_IS_EOF(status) ||
MoreDataAvailable(status));
}
#if SERF_HTTPS_FETCHING
apr_status_t SerfFetch::HandleSSLCertValidation(
int errors, int failure_depth, const serf_ssl_certificate_t *cert) {
// TODO(jmarantz): is there value in logging the errors and failure_depth
// formals here?
// Note that HandleSSLCertValidation can be called multiple times for a single
// request. As far as I can tell, there is value in recording only one of
// these. For now, I have set up the logic so only the last error will be
// printed lazilly, in ReadHeaders.
if (((errors & SERF_SSL_CERT_SELF_SIGNED) != 0) &&
!fetcher_->allow_self_signed()) {
ssl_error_message_ = "SSL certificate is self-signed";
} else if (((errors & SERF_SSL_CERT_UNKNOWNCA) != 0) &&
!fetcher_->allow_unknown_certificate_authority()) {
ssl_error_message_ =
"SSL certificate has an unknown certificate authority";
} else if (((errors & SERF_SSL_CERT_NOTYETVALID) != 0) &&
!fetcher_->allow_certificate_not_yet_valid()) {
ssl_error_message_ = "SSL certificate is not yet valid";
} else if (errors & SERF_SSL_CERT_EXPIRED) {
ssl_error_message_ = "SSL certificate is expired";
} else if (errors & SERF_SSL_CERT_UNKNOWN_FAILURE) {
ssl_error_message_ = "SSL certificate has an unknown error";
}
if (ssl_error_message_ == NULL && async_fetch_ != NULL) {
if (// If cert is null that means we're being called via SSLCertChainError.
// We only need to check the host name matches when being called via
// SSLCertError, in which case cert won't be null.
cert != NULL &&
// No point in checking the host if we're allowing self-signed or a made
// up CA, since people can forge whatever they want and often don't
// bother to make the name match.
!fetcher_->allow_self_signed() &&
!fetcher_->allow_unknown_certificate_authority()) {
DCHECK(serf_ssl_cert_depth(cert) == 0) <<
"Serf should be filtering out intermediate certs before hitting us.";
// You would think we could do whatever serf_get.c does, but it turns
// out that does no checking at all. There's x509_check_host, added in
// 1.0.2, but when svn uses serf it rolls its own check because it wants
// to support older versions. We generally build with boringssl, which
// forked from 1.0.2 and has always had this function, but when we build
// with openssl we now require 1.0.2.
if (serf_ssl_check_host(cert, sni_host_) != 1) {
ssl_error_message_ = "Failed to match host.";
}
}
}
// Immediately call the fetch callback on a cert error. Note that
// HandleSSLCertValidation is called multiple times when there is an error, so
// check async_fetch before CallCallback.
if ((ssl_error_message_ != NULL) && (async_fetch_ != NULL)) {
fetcher_->cert_errors_->Add(1);
CallCallback(false); // sets async_fetch_ to null.
}
// TODO(jmarantz): I think the design of this system indicates
// that we should be returning APR_EGENERAL on failure. However I
// have found that doesn't work properly, at least for
// SERF_SSL_CERT_SELF_SIGNED. The request does not terminate
// quickly but instead times out. Thus we return APR_SUCCESS
// but change the status_code to 404, report an error, and suppress
// the output.
//
// TODO(jmarantz): consider aiding diagnosability with by changing the
// 404 to a 401 (Unauthorized) or 418 (I'm a teapot), or 459 (nginx
// internal cert error code).
return APR_SUCCESS;
}
#endif
apr_status_t SerfFetch::HandleResponse(serf_bucket_t* response) {
if (response == NULL) {
message_handler_->Message(
kInfo, "serf HandlerReponse called with NULL response for %s",
DebugInfo().c_str());
CallCallback(false);
return APR_EGENERAL;
}
// The response-handling code must be robust to packets coming in all at once,
// one byte at a time, or anything in between. If we get EAGAIN we need to
// return it to our caller so it can do more work and call us again. See the
// serf example code in serf_get.c.
apr_status_t status = APR_EAGAIN;
while (MoreDataAvailable(status) && (async_fetch_ != NULL) &&
!parser_.headers_complete()) {
if (!status_line_read_) {
status = ReadStatusLine(response);
}
if (status_line_read_ && !one_byte_read_) {
status = ReadOneByteFromBody(response);
}
if (one_byte_read_ && !parser_.headers_complete()) {
status = ReadHeaders(response);
}
if (APR_STATUS_IS_EAGAIN(status)) {
return status;
}
}
if (parser_.headers_complete()) {
status = ReadBody(response);
}
if ((async_fetch_ != NULL) &&
((APR_STATUS_IS_EOF(status) && parser_.headers_complete()) ||
(status == APR_EGENERAL))) {
bool success = (IsStatusOk(status) && parser_.headers_complete());
if (!parser_.headers_complete() && (async_fetch_ != NULL)) {
// Be careful not to leave headers in inconsistent state in some error
// conditions.
async_fetch_->response_headers()->Clear();
}
CallCallback(success);
}
return status;
}
apr_status_t SerfFetch::ReadStatusLine(serf_bucket_t* response) {
serf_status_line status_line;
apr_status_t status = serf_bucket_response_status(response, &status_line);
if (status == APR_SUCCESS) {
ResponseHeaders* response_headers = async_fetch_->response_headers();
response_headers->SetStatusAndReason(
static_cast<HttpStatus::Code>(status_line.code));
response_headers->set_major_version(status_line.version / 1000);
response_headers->set_minor_version(status_line.version % 1000);
status_line_read_ = true;
}
return status;
}
apr_status_t SerfFetch::ReadOneByteFromBody(serf_bucket_t* response) {
apr_size_t len = 0;
const char* data = NULL;
apr_status_t status = serf_bucket_read(response, 1, &data, &len);
if (!APR_STATUS_IS_EINTR(status) && IsStatusOk(status)) {
one_byte_read_ = true;
if (len == 1) {
has_saved_byte_ = true;
saved_byte_ = data[0];
}
}
return status;
}
apr_status_t SerfFetch::ReadHeaders(serf_bucket_t* response) {
serf_bucket_t* headers = serf_bucket_response_get_headers(response);
const char* data = NULL;
apr_size_t len = 0;
apr_status_t status = serf_bucket_read(headers, SERF_READ_ALL_AVAIL,
&data, &len);
// Feed valid chunks to the header parser --- but skip empty ones,
// which can occur for value-less headers, since otherwise they'd
// look like parse errors.
if (IsStatusOk(status) && (len > 0)) {
if (parser_.ParseChunk(StringPiece(data, len), message_handler_)) {
if (parser_.headers_complete()) {
ResponseHeaders* response_headers = async_fetch_->response_headers();
if (ssl_error_message_ != NULL) {
response_headers->set_status_code(HttpStatus::kNotFound);
message_handler_->Message(kInfo, "%s: %s", DebugInfo().c_str(),
ssl_error_message_);
has_saved_byte_ = false;
}
if (fetcher_->track_original_content_length()) {
// Set X-Original-Content-Length, if Content-Length is available.
int64 content_length;
if (response_headers->FindContentLength(&content_length)) {
response_headers->SetOriginalContentLength(content_length);
}
}
// Stream the one byte read from ReadOneByteFromBody to writer.
if (has_saved_byte_) {
++bytes_received_;
if (!async_fetch_->Write(StringPiece(&saved_byte_, 1),
message_handler_)) {
status = APR_EGENERAL;
}
}
}
} else {
status = APR_EGENERAL;
}
}
return status;
}
apr_status_t SerfFetch::ReadBody(serf_bucket_t* response) {
apr_status_t status = APR_EAGAIN;
const char* data = NULL;
apr_size_t len = 0;
apr_size_t bytes_to_flush = 0;
while (MoreDataAvailable(status) && (async_fetch_ != NULL)) {
status = serf_bucket_read(response, SERF_READ_ALL_AVAIL, &data, &len);
bytes_received_ += len;
bytes_to_flush += len;
if (IsStatusOk(status) && (len != 0) &&
!async_fetch_->Write(StringPiece(data, len), message_handler_)) {
status = APR_EGENERAL;
}
}
if ((bytes_to_flush != 0) && !async_fetch_->Flush(message_handler_)) {
status = APR_EGENERAL;
}
return status;
}
void SerfFetch::FixUserAgent() {
// Supply a default user-agent if none is present, and in any case
// append on a 'serf' suffix.
GoogleString user_agent;
ConstStringStarVector v;
RequestHeaders* request_headers = async_fetch_->request_headers();
if (request_headers->Lookup(HttpAttributes::kUserAgent, &v)) {
for (int i = 0, n = v.size(); i < n; ++i) {
if (i != 0) {
user_agent += " ";
}
if (v[i] != NULL) {
user_agent += *(v[i]);
}
}
request_headers->RemoveAll(HttpAttributes::kUserAgent);
}
if (user_agent.empty()) {
user_agent += "Serf/" SERF_VERSION_STRING;
}
GoogleString version = StrCat(
" (", kModPagespeedSubrequestUserAgent,
"/" MOD_PAGESPEED_VERSION_STRING "-" LASTCHANGE_STRING ")");
if (!StringPiece(user_agent).ends_with(version)) {
user_agent += version;
}
request_headers->Add(HttpAttributes::kUserAgent, user_agent);
}
// static
apr_status_t SerfFetch::SetupRequest(serf_request_t* request,
void* setup_baton,
serf_bucket_t** req_bkt,
serf_response_acceptor_t* acceptor,
void** acceptor_baton,
serf_response_handler_t* handler,
void** handler_baton,
apr_pool_t* pool) {
SerfFetch* fetch = static_cast<SerfFetch*>(setup_baton);
const char* url_path = apr_uri_unparse(pool, &fetch->url_,
APR_URI_UNP_OMITSITEPART);
// If there is an explicit Host header, then override the
// host field in the Serf structure, as we will not be able
// to override it after it is created; only append to it.
//
// Serf automatically populates the Host field based on the
// URL, and provides no mechanism to override it, except
// by hacking source. We hacked source.
//
// See src/third_party/serf/src/instaweb_context.c
fetch->FixUserAgent();
RequestHeaders* request_headers = fetch->async_fetch_->request_headers();
// Don't want to forward hop-by-hop stuff.
StringPieceVector names_to_sanitize =
HttpAttributes::SortedHopByHopHeaders();
request_headers->RemoveAllFromSortedArray(&names_to_sanitize[0],
names_to_sanitize.size());
// Also leave Content-Length to serf.
request_headers->RemoveAll(HttpAttributes::kContentLength);
serf_bucket_t* body_bkt = NULL;
const GoogleString& message_body = request_headers->message_body();
bool post_payload =
!message_body.empty() &&
(request_headers->method() == RequestHeaders::kPost);
if (post_payload) {
body_bkt = serf_bucket_simple_create(
message_body.data(), message_body.length(),
NULL /* no free function */, NULL /* no free baton*/,
serf_request_get_alloc(request));
}
*req_bkt = serf_request_bucket_request_create_for_host(
request, request_headers->method_string(),
url_path, body_bkt,
serf_request_get_alloc(request), fetch->host_header_);
serf_bucket_t* hdrs_bkt = serf_bucket_request_get_headers(*req_bkt);
// Add other headers from the caller's request. Skip the "Host:" header
// because it's set above.
for (int i = 0; i < request_headers->NumAttributes(); ++i) {
const GoogleString& name = request_headers->Name(i);
const GoogleString& value = request_headers->Value(i);
if (!(StringCaseEqual(name, HttpAttributes::kHost))) {
// Note: *_setn() stores a pointer to name and value instead of a
// copy of those values. So name and value must have long lifetimes.
// In this case, we depend on request_headers being unchanged for
// the lifetime of hdrs_bkt, which is a documented requirement of
// the UrlAsyncFetcher interface.
serf_bucket_headers_setn(hdrs_bkt, name.c_str(), value.c_str());
}
}
*acceptor = SerfFetch::AcceptResponse;
*acceptor_baton = fetch;
*handler = SerfFetch::HandleResponse;
*handler_baton = fetch;
return APR_SUCCESS;
}
bool SerfFetch::ParseUrl() {
apr_status_t status = 0;
status = apr_uri_parse(pool_, str_url_.c_str(), &url_);
if (status != APR_SUCCESS || url_.scheme == NULL) {
return false; // Failed to parse URL.
}
bool is_https = StringCaseEqual(url_.scheme, "https");
if (is_https && !fetcher_->allow_https()) {
return false;
}
if (!url_.port) {
url_.port = apr_uri_port_of_scheme(url_.scheme);
}
if (!url_.path) {
url_.path = apr_pstrdup(pool_, "/");
}
// Compute our host header. First see if there is an explicit specified
// Host: in the fetch object.
RequestHeaders* request_headers = async_fetch_->request_headers();
const char* host = request_headers->Lookup1(HttpAttributes::kHost);
if (host == NULL) {
host = SerfUrlAsyncFetcher::ExtractHostHeader(url_, pool_);
}
host_header_ = apr_pstrdup(pool_, host);
if (is_https) {
// SNI hosts, unlike Host: do not have a port number.
GoogleString sni_host =
SerfUrlAsyncFetcher::RemovePortFromHostHeader(host_header_);
sni_host_ = apr_pstrdup(pool_, sni_host.c_str());
}
return true;
}
class SerfThreadedFetcher : public SerfUrlAsyncFetcher {
public:
SerfThreadedFetcher(SerfUrlAsyncFetcher* parent, const char* proxy) :
SerfUrlAsyncFetcher(parent, proxy),
thread_id_(NULL),
initiate_mutex_(parent->thread_system()->NewMutex()),
initiate_fetches_(new SerfFetchPool()),
initiate_fetches_nonempty_(initiate_mutex_->NewCondvar()),
thread_finish_(false),
thread_started_(false) {
}
~SerfThreadedFetcher() {
// Let the thread terminate naturally by telling it to unblock,
// then waiting for it to finish its next active Poll operation.
{
// Indicate termination and unblock the worker thread so it can clean up.
ScopedMutex lock(initiate_mutex_.get());
if (thread_started_) {
thread_finish_ = true;
initiate_fetches_nonempty_->Signal();
} else {
LOG(INFO) << "Serf threaded not actually started, quick shutdown.";
return;
}
}
LOG(INFO) << "Waiting for threaded serf fetcher to terminate";
apr_status_t ignored_retval;
apr_thread_join(&ignored_retval, thread_id_);
// Under normal circumstances there shouldn't be any active fetches at
// this point. However, in practice we may have some lingering fetches that
// have timed out, and we need to clean those up properly before we can
// exit. We try to do this gracefully, but fall back to graceless cleanup
// if that fails.
// Before we can clean up, we must make sure we haven't initiated any
// fetches that haven't moved to the active pool yet. This should not
// happen, but we're exercising undue caution here. We do this by just
// moving them across. From this point, calls to InitiateFetch(...) are
// illegal, but we should be invoking this destructor from the only thread
// that could have called InitiateFetch anyhow.
TransferFetchesAndCheckDone(false);
// Although Cancel will be called in the base class destructor, we
// want to call it here as well, as it will make it easier for the
// thread to terminate.
CancelActiveFetches();
completed_fetches_.DeleteAll();
initiate_fetches_->DeleteAll();
}
void StartThread() {
CHECK_EQ(APR_SUCCESS,
apr_thread_create(&thread_id_, NULL, SerfThreadFn, this, pool_));
thread_started_ = true;
}
// Called from mainline to queue up a fetch for the thread. If the
// thread is idle then we can unlock it.
void InitiateFetch(SerfFetch* fetch) {
ScopedMutex lock(initiate_mutex_.get());
// We delay thread startup until we actually want to fetch something
// to avoid problems with ITK.
if (!thread_started_) {
StartThread();
}
// TODO(jmaessen): Consider adding an awaiting_nonempty_ flag to avoid
// spurious calls to Signal().
bool signal = initiate_fetches_->empty();
initiate_fetches_->Add(fetch);
if (signal) {
initiate_fetches_nonempty_->Signal();
}
}
void ShutDown() {
// See comments in the destructor above.. The big difference is that
// because we set shutdown_ to true new jobs can't actually come in.
{
// Acquisition order is initiate before hold, see e.g. AnyPendingFetches()
ScopedMutex hold_initiate(initiate_mutex_.get());
ScopedMutex hold(mutex_);
set_shutdown(true);
if (!thread_started_) {
return;
}
}
TransferFetchesAndCheckDone(false);
CancelActiveFetches();
}
protected:
bool AnyPendingFetches() {
ScopedMutex lock(initiate_mutex_.get());
// NOTE: We must hold both mutexes to avoid the case where we miss a fetch
// in transit.
return !initiate_fetches_->empty() ||
SerfUrlAsyncFetcher::AnyPendingFetches();
}
private:
static void* APR_THREAD_FUNC SerfThreadFn(apr_thread_t* thread_id,
void* context) {
SerfThreadedFetcher* stc = static_cast<SerfThreadedFetcher*>(context);
CHECK_EQ(thread_id, stc->thread_id_);
stc->SerfThread();
return NULL;
}
// Transfer fetches from initiate_fetches_ to active_fetches_. If there's no
// new fetches to initiate, check whether the webserver thread is trying to
// shut down the worker thread, and return true to indicate "done". Doesn't
// do any work if initiate_fetches_ is empty, but in that case if
// block_on_empty is true it will perform a bounded wait for
// initiate_fetches_nonempty_. Called by worker thread and during thread
// cleanup.
bool TransferFetchesAndCheckDone(bool block_on_empty) {
// Use a temp to minimize the amount of time we hold the
// initiate_mutex_ lock, so that the parent thread doesn't get
// blocked trying to initiate fetches.
scoped_ptr<SerfFetchPool> xfer_fetches;
{
ScopedMutex lock(initiate_mutex_.get());
// We must do this checking under the initiate_mutex_ lock.
if (initiate_fetches_->empty()) {
// No new work to do now.
if (!block_on_empty || thread_finish_) {
return thread_finish_;
} else {
// Wait until some work shows up. Note that after the wait we still
// must actually check that there's some work to be done.
initiate_fetches_nonempty_->TimedWait(Timer::kSecondMs);
if (initiate_fetches_->empty()) {
// On timeout / false wakeup, return control to caller; we might be
// finished or have other things to attend to.
return thread_finish_;
}
}
}
xfer_fetches.reset(new SerfFetchPool());
// Take mutex_ before relinquishing initiate_mutex_. This guarantees that
// AnyPendingFetches cannot see us in the time between emptying
// initiate_fetches_ and inserting into active_fetches_. At that time, it
// can look as though no fetch work is occurring. Note that we obtain
// mutex_ before performing the swap (but after creating the new pool)
// because additional fetches might arrive in the mean time. This was
// causing problems with timeout in TestThreeThreaded under valgrind,
// because we'd block waiting for mutex_ after a single fetch had been
// initiated, but not obtain mutex_ until after several more fetches
// arrived (at which point we'd go into the poll loop without initiating
// all available fetches).
mutex_->Lock();
xfer_fetches.swap(initiate_fetches_);
}
// Now that we've unblocked the parent thread, we can leisurely
// queue up the fetches, employing the proper lock for the active_fetches_
// set. Actually we expect we wll never have contention on this mutex
// from the thread.
while (!xfer_fetches->empty()) {
SerfFetch* fetch = xfer_fetches->RemoveOldest();
if (StartFetch(fetch)) {
SERF_DEBUG(LOG(INFO) << "Adding threaded fetch to url "
<< fetch->DebugInfo()
<< " (" << active_fetches_.size() << ")");
}
}
mutex_->Unlock();
return false;
}
void SerfThread() {
// Make sure we don't get yet-another copy of signals used by the webserver
// to shutdown here, to avoid double-free.
// TODO(morlovich): Port this to use ThreadSystem stuff, and have
// SystemThreadSystem take care of this automatically.
apr_setup_signal_thread();
// Initially there's no active fetch work to be done.
int num_active_fetches = 0;
while (!TransferFetchesAndCheckDone(num_active_fetches == 0)) {
// If initiate_fetches is empty, and there's no current active fetch
// work to do, we'll block in the above call. Otherwise the call will
// start initiated fetches (if any) without blocking.
// We set the poll interval to try to start new fetches promptly from the
// observer's perspective (ie .1s is perceptible, so we try to make sure
// new fetches are started after at most half that time). The downside is
// that we don't hand off control to serf / the OS for long periods when
// fetches are active but no data is arriving. We trust that doesn't
// happen often.
// TODO(jmaessen): Break out of Poll before timeout if work becomes
// available, so that we initiate new fetches as promptly as possible
// while continuing to serve the old ones. This would let us dial the
// poll interval up high (to multiple seconds). The classic trick here is
// to set up a pipe/FIFO/socket and add it to the set of things being
// read, then use a write to force wakeup. But will serf support this
// kind of thing?
const int64 kPollIntervalMs = Timer::kSecondMs / 20;
// If active_fetches_ is empty, we will not do any work and won't block
// here. num_active_fetches will be 0, and we'll block in the next
// call to TransferFetches above.
num_active_fetches = Poll(kPollIntervalMs);
SERF_DEBUG(LOG(INFO) << "Finished polling from serf thread ("
<< this << ")");
}
}
apr_thread_t* thread_id_;
// protects initiate_fetches_, initiate_fetches_nonempty_, thread_finish_
// and thread_started_.
scoped_ptr<ThreadSystem::CondvarCapableMutex> initiate_mutex_;
// pushed in the main thread; popped by TransferFetches().
scoped_ptr<SerfFetchPool> initiate_fetches_;
// condvar that indicates that initiate_fetches_ has become nonempty. During
// normal operation, only the serf worker thread consumes initiated fetches
// (this can change during thread shutdown), but the usual condition variable
// caveats apply: Just because the condition variable indicates
// initiate_fetches_nonempty_ doesn't mean it's true, and a waiting thread
// must check initiate_fetches_ explicitly while holding initiate_mutex_.
scoped_ptr<ThreadSystem::Condvar> initiate_fetches_nonempty_;
// Flag to signal worker to finish working and terminate.
bool thread_finish_;
// True if we actually started the worker thread. Protected by initiate_mutex_
bool thread_started_;
DISALLOW_COPY_AND_ASSIGN(SerfThreadedFetcher);
};
bool SerfFetch::Start(SerfUrlAsyncFetcher* fetcher) {
// Note: this is called in the thread's context, so this is when we do
// the pool ops.
fetcher_ = fetcher;
apr_pool_create(&pool_, fetcher_->pool());
bucket_alloc_ = serf_bucket_allocator_create(pool_, NULL, NULL);
fetch_start_ms_ = timer_->NowMs();
// Parse and validate the URL.
if (!ParseUrl()) {
return false;
}
using_https_ = StringCaseEqual("https", url_.scheme);
DCHECK(fetcher->allow_https() || !using_https_);
apr_status_t status = serf_connection_create2(&connection_,
fetcher_->serf_context(),