18 #ifndef PAGESPEED_SYSTEM_SERF_URL_ASYNC_FETCHER_H_
19 #define PAGESPEED_SYSTEM_SERF_URL_ASYNC_FETCHER_H_
24 #include "apr_network_io.h"
32 #include "pagespeed/kernel/base/thread_annotations.h"
36 #include "third_party/serf/src/serf.h"
48 #ifndef SERF_HTTPS_FETCHING
49 #define SERF_HTTPS_FETCHING 1
54 struct serf_context_t;
56 namespace net_instaweb {
62 class SerfThreadedFetcher;
68 static const char kSerfFetchRequestCount[];
69 static const char kSerfFetchByteCount[];
70 static const char kSerfFetchTimeDurationMs[];
71 static const char kSerfFetchCancelCount[];
72 static const char kSerfFetchActiveCount[];
73 static const char kSerfFetchTimeoutCount[];
74 static const char kSerfFetchFailureCount[];
75 static const char kSerfFetchCertErrors[];
76 static const char kSerfFetchReadCalls[];
91 enum class SerfCompletionResult {
101 #define SERF_HTTPS_KEYWORDS \
102 "enable,disable,allow_self_signed," \
103 "allow_unknown_certificate_authority,allow_certificate_not_yet_valid"
126 static void InitStats(
Statistics* statistics);
138 int Poll(int64 max_wait_ms);
140 bool WaitForActiveFetches(int64 max_milliseconds,
142 WaitChoice wait_choice);
157 apr_pool_t* pool()
const {
return pool_; }
161 ThreadSystem* thread_system() {
return thread_system_; }
170 return track_original_content_length_;
172 void set_track_original_content_length(
bool x);
190 return ParseHttpsOptions(directive, &options, error_message);
193 void SetSslCertificatesDir(StringPiece dir);
195 return ssl_certificates_dir_;
198 void SetSslCertificatesFile(StringPiece file);
200 return ssl_certificates_file_;
204 typedef Pool<SerfFetch> SerfFetchPool;
208 inline bool allow_self_signed()
const;
209 inline bool allow_unknown_certificate_authority()
const;
210 inline bool allow_certificate_not_yet_valid()
const;
212 void set_https_options(uint32 https_options) {
213 https_options_ = https_options;
216 void Init(apr_pool_t* parent_pool,
const char* proxy)
217 EXCLUSIVE_LOCKS_REQUIRED(
mutex_);
218 bool SetupProxy(
const char* proxy) EXCLUSIVE_LOCKS_REQUIRED(
mutex_);
233 void CancelActiveFetches();
234 void CancelActiveFetchesMutexHeld() EXCLUSIVE_LOCKS_REQUIRED(
mutex_);
235 bool WaitForActiveFetchesHelper(int64 max_ms,
236 MessageHandler* message_handler);
242 bool shutdown() const EXCLUSIVE_LOCKS_REQUIRED(mutex_) {
return shutdown_; }
243 void set_shutdown(
bool s) EXCLUSIVE_LOCKS_REQUIRED(
mutex_) { shutdown_ = s; }
246 ThreadSystem* thread_system_;
253 typedef std::vector<SerfFetch*> FetchVector;
255 SerfThreadedFetcher* threaded_fetcher_;
265 static const char* ExtractHostHeader(
const apr_uri_t& uri,
267 FRIEND_TEST(SerfUrlAsyncFetcherTest, TestHostConstruction);
272 FRIEND_TEST(SerfUrlAsyncFetcherTest, TestPortRemoval);
274 static bool ParseHttpsOptions(StringPiece directive, uint32* options,
277 serf_context_t* serf_context_ GUARDED_BY(
mutex_);
291 const int64 timeout_ms_;
292 bool shutdown_ GUARDED_BY(
mutex_);
293 bool list_outstanding_urls_on_error_;
294 bool track_original_content_length_;
295 uint32 https_options_;
306 enum class CancelCause {
326 void Cancel(CancelCause cause);
337 void CallbackDone(SerfCompletionResult result);
347 const char** host_header,
348 const char** sni_host);
352 int64 TimeDuration()
const;
354 int64 fetch_start_ms()
const {
return fetch_start_ms_; }
356 size_t bytes_received()
const {
return bytes_received_; }
367 #if SERF_HTTPS_FETCHING
368 static apr_status_t SSLCertValidate(
void *data,
int failures,
369 const serf_ssl_certificate_t *cert);
371 static apr_status_t SSLCertChainValidate(
372 void *data,
int failures,
int error_depth,
373 const serf_ssl_certificate_t *
const *certs,
374 apr_size_t certs_count);
377 static apr_status_t ConnectionSetup(
378 apr_socket_t* socket, serf_bucket_t **read_bkt, serf_bucket_t **write_bkt,
379 void* setup_baton, apr_pool_t* pool);
380 static void ClosedConnection(serf_connection_t* conn,
384 static serf_bucket_t* AcceptResponse(serf_request_t* request,
385 serf_bucket_t* stream,
386 void* acceptor_baton,
388 static apr_status_t HandleResponse(serf_request_t* request,
389 serf_bucket_t* response,
397 static bool StatusIndicatesDataPossible(apr_status_t status);
399 #if SERF_HTTPS_FETCHING
400 apr_status_t HandleSSLCertValidation(
411 int errors,
int failure_depth,
const serf_ssl_certificate_t *cert);
414 apr_status_t HandleResponse(serf_bucket_t* response);
416 apr_status_t ReadStatusLine(serf_bucket_t* response);
421 apr_status_t ReadHeaders(serf_bucket_t* response);
426 apr_status_t ReadBody(serf_bucket_t* response);
431 static apr_status_t SetupRequest(serf_request_t* request,
433 serf_bucket_t** req_bkt,
434 serf_response_acceptor_t* acceptor,
435 void** acceptor_baton,
436 serf_response_handler_t* handler,
437 void** handler_baton,
446 bool status_line_read_;
450 serf_bucket_alloc_t* bucket_alloc_;
452 const char* host_header_;
453 const char* sni_host_;
454 serf_connection_t* connection_;
455 size_t bytes_received_;
456 int64 fetch_start_ms_;
461 serf_ssl_context_t* ssl_context_;
462 const char* ssl_error_message_;
bool Start(SerfUrlAsyncFetcher *fetcher, serf_context_t *context)
Definition: serf_url_async_fetcher.h:304
int Poll(int64 max_wait_ms)
void Cancel(CancelCause cause)
This must be called while holding SerfUrlAsyncFetcher's mutex_.
bool track_original_content_length() const
Definition: serf_url_async_fetcher.h:169
Definition: statistics.h:43
virtual void Fetch(const GoogleString &url, MessageHandler *message_handler, AsyncFetch *callback)
void CallCallback(SerfCompletionResult result)
Base class for implementations of monitoring statistics.
Definition: statistics.h:342
SerfFetch(const GoogleString &url, AsyncFetch *async_fetch, MessageHandler *message_handler, Timer *timer)
Definition: serf_url_async_fetcher.h:111
static const char kSerfFetchUltimateFailure[]
Definition: serf_url_async_fetcher.h:84
bool SetHttpsOptions(StringPiece directive)
void ParseUrlForTesting(bool *status, apr_uri_t **url, const char **host_header, const char **sni_host)
Definition: thread_system.h:46
void ReportFetchSuccessStats(SerfCompletionResult result, const ResponseHeaders *headers, const SerfFetch *fetch)
Updates states used for success/failure monitoring.
static bool ValidateHttpsOptions(StringPiece directive, GoogleString *error_message)
Definition: serf_url_async_fetcher.h:187
void CleanupFetchesWithErrors() EXCLUSIVE_LOCKS_REQUIRED(mutex_)
std::string GoogleString
PAGESPEED_KERNEL_BASE_STRING_H_.
Definition: string.h:24
int ApproximateNumActiveFetches()
bool allow_https() const
Determines whether https is allowed in the current configuration.
void ReportCompletedFetchStats(const SerfFetch *fetch)
Update the statistics object with results of the (completed) fetch.
void FetchComplete(SerfFetch *fetch)
Definition: async_fetch.h:53
virtual bool AnyPendingFetches()
UpDownCounter * active_count_
Definition: serf_url_async_fetcher.h:259
Definition: thread_system.h:40
Definition: statistics.h:73
Definition: message_handler.h:39
static const char kSerfFetchUltimateSuccess[]
Definition: serf_url_async_fetcher.h:80
virtual bool SupportsHttps() const
friend class SerfFetch
To access stats variables below.
Definition: serf_url_async_fetcher.h:262
ThreadSystem::CondvarCapableMutex * mutex_
Definition: serf_url_async_fetcher.h:251
Definition: serf_url_async_fetcher.h:67
virtual int64 timeout_ms()
Definition: serf_url_async_fetcher.h:160
void set_list_outstanding_urls_on_error(bool x)
Timer interface, made virtual so it can be mocked for tests.
Definition: timer.h:27
bool StartFetch(SerfFetch *fetch) EXCLUSIVE_LOCKS_REQUIRED(mutex_)
Definition: url_async_fetcher.h:33
Definition: pool_element.h:33
static const char kSerfFetchLastCheckTimestampMs[]
Definition: serf_url_async_fetcher.h:88