2
* Copyright 2011 Google Inc.
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
* you may not use this file except in compliance with the License.
6
* You may obtain a copy of the License at
8
* http://www.apache.org/licenses/LICENSE-2.0
10
* Unless required by applicable law or agreed to in writing, software
11
* distributed under the License is distributed on an "AS IS" BASIS,
12
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
* See the License for the specific language governing permissions and
14
* limitations under the License.
17
// Author: sligocki@google.com (Shawn Ligocki)
19
// Simple interface for running Page Speed Automatic as a proxy.
21
// When implementing a Page Speed Automatic proxy, simply construct a
22
// ProxyInterface at start up time and call Fetch for every
23
// requested resource. Fetch decides how to deal with requests
24
// (pagespeed resources will be computed, HTML pages will be proxied
25
// and rewritten, and other resources will just be proxied).
27
#ifndef NET_INSTAWEB_AUTOMATIC_PUBLIC_PROXY_INTERFACE_H_
28
#define NET_INSTAWEB_AUTOMATIC_PUBLIC_PROXY_INTERFACE_H_
30
#include "net/instaweb/http/public/url_async_fetcher.h"
31
#include "net/instaweb/util/public/basictypes.h"
32
#include "net/instaweb/util/public/scoped_ptr.h"
33
#include "net/instaweb/util/public/string.h"
34
#include "net/instaweb/util/public/string_util.h"
36
namespace net_instaweb {
41
class ProxyFetchPropertyCallbackCollector;
42
class ProxyFetchFactory;
48
// TODO(sligocki): Rename as per style-guide.
49
class ProxyInterface : public UrlAsyncFetcher {
51
ProxyInterface(const StringPiece& hostname, int port,
52
ServerContext* server_context, Statistics* stats);
53
virtual ~ProxyInterface();
55
// Initializes statistics variables associated with this class.
56
static void InitStats(Statistics* statistics);
58
// All requests use this interface. We decide internally whether the
59
// request is a pagespeed resource, HTML page to be rewritten or another
60
// resource to be proxied directly.
61
virtual void Fetch(const GoogleString& requested_url,
62
MessageHandler* handler,
63
AsyncFetch* async_fetch);
65
// Is this url_string well-formed enough to proxy through?
66
bool IsWellFormedUrl(const GoogleUrl& url);
68
static const char kCacheHtmlRequestCount[];
70
// Initiates the PropertyCache look up.
71
virtual ProxyFetchPropertyCallbackCollector* InitiatePropertyCacheLookup(
72
bool is_resource_fetch,
73
const GoogleUrl& request_url,
74
RewriteOptions* options,
75
AsyncFetch* async_fetch,
76
const bool requires_blink_cohort,
77
bool* added_page_property_callback);
80
// Needed by subclasses when overriding InitiatePropertyCacheLookup.
81
ServerContext* server_context_; // thread-safe, unowned
84
friend class ProxyInterfaceTest;
86
// Handle requests that are being proxied.
87
// * HTML requests are rewritten.
88
// * Resource requests are proxied verbatim.
89
void ProxyRequest(bool is_resource_fetch,
90
const GoogleUrl& requested_url,
91
AsyncFetch* async_fetch,
92
MessageHandler* handler);
94
// Callback function which runs once we have rewrite_options for requests that
97
void GetRewriteOptionsDone(RequestData* request_data,
98
RewriteOptions* query_options);
100
// If the URL and port are for this server, don't proxy those (to avoid
101
// infinite fetching loops). This might be the favicon or something...
102
bool UrlAndPortMatchThisServer(const GoogleUrl& url);
104
// This server's hostname and port (to avoid making circular requests).
105
// TODO(sligocki): This assumes we will only be called as one hostname,
106
// there could be multiple DNS entries pointing at us.
107
const GoogleString hostname_;
112
TimedVariable* all_requests_;
113
// Total Pagespeed requests.
114
TimedVariable* pagespeed_requests_;
115
// Cache Html requests.
116
TimedVariable* cache_html_flow_requests_;
117
// Rejected requests counter.
118
TimedVariable* rejected_requests_;
120
scoped_ptr<ProxyFetchFactory> proxy_fetch_factory_;
122
DISALLOW_COPY_AND_ASSIGN(ProxyInterface);
125
} // namespace net_instaweb
127
#endif // NET_INSTAWEB_AUTOMATIC_PUBLIC_PROXY_INTERFACE_H_