1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /* ***** BEGIN LICENSE BLOCK *****
3 : * Version: MPL 1.1/GPL 2.0/LGPL 2.1
4 : *
5 : * The contents of this file are subject to the Mozilla Public License Version
6 : * 1.1 (the "License"); you may not use this file except in compliance with
7 : * the License. You may obtain a copy of the License at
8 : * http://www.mozilla.org/MPL/
9 : *
10 : * Software distributed under the License is distributed on an "AS IS" basis,
11 : * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 : * for the specific language governing rights and limitations under the
13 : * License.
14 : *
15 : * The Original Code is Mozilla.
16 : *
17 : * The Initial Developer of the Original Code is
18 : * Netscape Communications.
19 : * Portions created by the Initial Developer are Copyright (C) 2001
20 : * the Initial Developer. All Rights Reserved.
21 : *
22 : * Contributor(s):
23 : * Darin Fisher <darin@netscape.com> (original author)
24 : *
25 : * Alternatively, the contents of this file may be used under the terms of
26 : * either the GNU General Public License Version 2 or later (the "GPL"), or
27 : * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
28 : * in which case the provisions of the GPL or the LGPL are applicable instead
29 : * of those above. If you wish to allow use of your version of this file only
30 : * under the terms of either the GPL or the LGPL, and not to allow others to
31 : * use your version of this file under the terms of the MPL, indicate your
32 : * decision by deleting the provisions above and replace them with the notice
33 : * and other provisions required by the GPL or the LGPL. If you do not delete
34 : * the provisions above, a recipient may use your version of this file under
35 : * the terms of any one of the MPL, the GPL or the LGPL.
36 : *
37 : * ***** END LICENSE BLOCK ***** */
38 :
39 : #ifndef nsHttpHandler_h__
40 : #define nsHttpHandler_h__
41 :
42 : #include "nsHttp.h"
43 : #include "nsHttpAuthCache.h"
44 : #include "nsHttpConnection.h"
45 : #include "nsHttpConnectionMgr.h"
46 :
47 : #include "nsXPIDLString.h"
48 : #include "nsString.h"
49 : #include "nsCOMPtr.h"
50 : #include "nsWeakReference.h"
51 :
52 : #include "nsIHttpProtocolHandler.h"
53 : #include "nsIProtocolProxyService.h"
54 : #include "nsIIOService.h"
55 : #include "nsIObserver.h"
56 : #include "nsIObserverService.h"
57 : #include "nsIPrivateBrowsingService.h"
58 : #include "nsIStreamConverterService.h"
59 : #include "nsICacheSession.h"
60 : #include "nsICookieService.h"
61 : #include "nsIIDNService.h"
62 : #include "nsITimer.h"
63 : #include "nsIStrictTransportSecurityService.h"
64 :
65 : class nsHttpConnectionInfo;
66 : class nsHttpHeaderArray;
67 : class nsHttpTransaction;
68 : class nsAHttpTransaction;
69 : class nsIHttpChannel;
70 : class nsIPrefBranch;
71 :
72 : //-----------------------------------------------------------------------------
73 : // nsHttpHandler - protocol handler for HTTP and HTTPS
74 : //-----------------------------------------------------------------------------
75 :
76 : class nsHttpHandler : public nsIHttpProtocolHandler
77 : , public nsIObserver
78 : , public nsSupportsWeakReference
79 : {
80 : public:
81 : NS_DECL_ISUPPORTS
82 : NS_DECL_NSIPROTOCOLHANDLER
83 : NS_DECL_NSIPROXIEDPROTOCOLHANDLER
84 : NS_DECL_NSIHTTPPROTOCOLHANDLER
85 : NS_DECL_NSIOBSERVER
86 :
87 : nsHttpHandler();
88 : virtual ~nsHttpHandler();
89 :
90 : nsresult Init();
91 : nsresult AddStandardRequestHeaders(nsHttpHeaderArray *,
92 : PRUint8 capabilities,
93 : bool useProxy);
94 : bool IsAcceptableEncoding(const char *encoding);
95 :
96 : const nsAFlatCString &UserAgent();
97 :
98 2962 : nsHttpVersion HttpVersion() { return mHttpVersion; }
99 27 : nsHttpVersion ProxyHttpVersion() { return mProxyHttpVersion; }
100 34 : PRUint8 ReferrerLevel() { return mReferrerLevel; }
101 0 : bool SendSecureXSiteReferrer() { return mSendSecureXSiteReferrer; }
102 3514 : PRUint8 RedirectionLimit() { return mRedirectionLimit; }
103 5 : PRIntervalTime IdleTimeout() { return mIdleTimeout; }
104 0 : PRIntervalTime SpdyTimeout() { return mSpdyTimeout; }
105 0 : PRUint16 MaxRequestAttempts() { return mMaxRequestAttempts; }
106 2973 : const char *DefaultSocketType() { return mDefaultSocketType.get(); /* ok to return null */ }
107 : nsIIDNService *IDNConverter() { return mIDNConverter; }
108 0 : PRUint32 PhishyUserPassLength() { return mPhishyUserPassLength; }
109 2976 : PRUint8 GetQoSBits() { return mQoSBits; }
110 2969 : PRUint16 GetIdleSynTimeout() { return mIdleSynTimeout; }
111 0 : bool FastFallbackToIPv4() { return mFastFallbackToIPv4; }
112 : PRUint32 MaxSocketCount();
113 :
114 2894 : bool IsPersistentHttpsCachingEnabled() { return mEnablePersistentHttpsCaching; }
115 : bool IsTelemetryEnabled() { return mTelemetryEnabled; }
116 0 : bool AllowExperiments() { return mTelemetryEnabled && mAllowExperiments; }
117 :
118 31968 : bool IsSpdyEnabled() { return mEnableSpdy; }
119 15084 : bool CoalesceSpdy() { return mCoalesceSpdy; }
120 5047 : bool UseAlternateProtocol() { return mUseAlternateProtocol; }
121 0 : PRUint32 SpdySendingChunkSize() { return mSpdySendingChunkSize; }
122 0 : PRIntervalTime SpdyPingThreshold() { return mSpdyPingThreshold; }
123 0 : PRIntervalTime SpdyPingTimeout() { return mSpdyPingTimeout; }
124 :
125 16 : bool PromptTempRedirect() { return mPromptTempRedirect; }
126 :
127 3598 : nsHttpAuthCache *AuthCache() { return &mAuthCache; }
128 22907 : nsHttpConnectionMgr *ConnMgr() { return mConnMgr; }
129 :
130 : // cache support
131 : nsresult GetCacheSession(nsCacheStoragePolicy, nsICacheSession **);
132 176 : PRUint32 GenerateUniqueID() { return ++mLastUniqueID; }
133 567 : PRUint32 SessionStartTime() { return mSessionStartTime; }
134 :
135 : //
136 : // Connection management methods:
137 : //
138 : // - the handler only owns idle connections; it does not own active
139 : // connections.
140 : //
141 : // - the handler keeps a count of active connections to enforce the
142 : // steady-state max-connections pref.
143 : //
144 :
145 : // Called to kick-off a new transaction, by default the transaction
146 : // will be put on the pending transaction queue if it cannot be
147 : // initiated at this time. Callable from any thread.
148 2989 : nsresult InitiateTransaction(nsHttpTransaction *trans, PRInt32 priority)
149 : {
150 2989 : return mConnMgr->AddTransaction(trans, priority);
151 : }
152 :
153 : // Called to change the priority of an existing transaction that has
154 : // already been initiated.
155 0 : nsresult RescheduleTransaction(nsHttpTransaction *trans, PRInt32 priority)
156 : {
157 0 : return mConnMgr->RescheduleTransaction(trans, priority);
158 : }
159 :
160 : // Called to cancel a transaction, which may or may not be assigned to
161 : // a connection. Callable from any thread.
162 162 : nsresult CancelTransaction(nsHttpTransaction *trans, nsresult reason)
163 : {
164 162 : return mConnMgr->CancelTransaction(trans, reason);
165 : }
166 :
167 : // Called when a connection is done processing a transaction. Callable
168 : // from any thread.
169 2975 : nsresult ReclaimConnection(nsHttpConnection *conn)
170 : {
171 2975 : return mConnMgr->ReclaimConnection(conn);
172 : }
173 :
174 0 : nsresult ProcessPendingQ(nsHttpConnectionInfo *cinfo)
175 : {
176 0 : return mConnMgr->ProcessPendingQ(cinfo);
177 : }
178 :
179 0 : nsresult GetSocketThreadTarget(nsIEventTarget **target)
180 : {
181 0 : return mConnMgr->GetSocketThreadTarget(target);
182 : }
183 :
184 : // for anything that wants to know if we're in private browsing mode.
185 : bool InPrivateBrowsingMode();
186 :
187 : //
188 : // The HTTP handler caches pointers to specific XPCOM services, and
189 : // provides the following helper routines for accessing those services:
190 : //
191 : nsresult GetStreamConverterService(nsIStreamConverterService **);
192 : nsresult GetIOService(nsIIOService** service);
193 : nsICookieService * GetCookieService(); // not addrefed
194 : nsIStrictTransportSecurityService * GetSTSService();
195 :
196 : // callable from socket thread only
197 : PRUint32 Get32BitsOfPseudoRandom();
198 :
199 : // Called by the channel before writing a request
200 3506 : void OnModifyRequest(nsIHttpChannel *chan)
201 : {
202 3506 : NotifyObservers(chan, NS_HTTP_ON_MODIFY_REQUEST_TOPIC);
203 3506 : }
204 :
205 : // Called by the channel once headers are available
206 2819 : void OnExamineResponse(nsIHttpChannel *chan)
207 : {
208 2819 : NotifyObservers(chan, NS_HTTP_ON_EXAMINE_RESPONSE_TOPIC);
209 2819 : }
210 :
211 : // Called by the channel once headers have been merged with cached headers
212 8 : void OnExamineMergedResponse(nsIHttpChannel *chan)
213 : {
214 8 : NotifyObservers(chan, NS_HTTP_ON_EXAMINE_MERGED_RESPONSE_TOPIC);
215 8 : }
216 :
217 : // Called by channels before a redirect happens. This notifies both the
218 : // channel's and the global redirect observers.
219 : nsresult AsyncOnChannelRedirect(nsIChannel* oldChan, nsIChannel* newChan,
220 : PRUint32 flags);
221 :
222 : // Called by the channel when the response is read from the cache without
223 : // communicating with the server.
224 220 : void OnExamineCachedResponse(nsIHttpChannel *chan)
225 : {
226 220 : NotifyObservers(chan, NS_HTTP_ON_EXAMINE_CACHED_RESPONSE_TOPIC);
227 220 : }
228 :
229 : // Generates the host:port string for use in the Host: header as well as the
230 : // CONNECT line for proxies. This handles IPv6 literals correctly.
231 : static nsresult GenerateHostPort(const nsCString& host, PRInt32 port,
232 : nsCString& hostLine);
233 :
234 : private:
235 :
236 : //
237 : // Useragent/prefs helper methods
238 : //
239 : void BuildUserAgent();
240 : void InitUserAgentComponents();
241 : void PrefsChanged(nsIPrefBranch *prefs, const char *pref);
242 :
243 : nsresult SetAccept(const char *);
244 : nsresult SetAcceptLanguages(const char *);
245 : nsresult SetAcceptEncodings(const char *);
246 :
247 : nsresult InitConnectionMgr();
248 :
249 : void NotifyObservers(nsIHttpChannel *chan, const char *event);
250 :
251 : private:
252 :
253 : // cached services
254 : nsCOMPtr<nsIIOService> mIOService;
255 : nsCOMPtr<nsIStreamConverterService> mStreamConvSvc;
256 : nsCOMPtr<nsIObserverService> mObserverService;
257 : nsCOMPtr<nsICookieService> mCookieService;
258 : nsCOMPtr<nsIIDNService> mIDNConverter;
259 : nsCOMPtr<nsIStrictTransportSecurityService> mSTSService;
260 :
261 : // the authentication credentials cache
262 : nsHttpAuthCache mAuthCache;
263 :
264 : // the connection manager
265 : nsHttpConnectionMgr *mConnMgr;
266 :
267 : //
268 : // prefs
269 : //
270 :
271 : PRUint8 mHttpVersion;
272 : PRUint8 mProxyHttpVersion;
273 : PRUint8 mCapabilities;
274 : PRUint8 mProxyCapabilities;
275 : PRUint8 mReferrerLevel;
276 :
277 : bool mFastFallbackToIPv4;
278 :
279 : PRIntervalTime mIdleTimeout;
280 : PRIntervalTime mSpdyTimeout;
281 :
282 : PRUint16 mMaxRequestAttempts;
283 : PRUint16 mMaxRequestDelay;
284 : PRUint16 mIdleSynTimeout;
285 :
286 : PRUint16 mMaxConnections;
287 : PRUint8 mMaxConnectionsPerServer;
288 : PRUint8 mMaxPersistentConnectionsPerServer;
289 : PRUint8 mMaxPersistentConnectionsPerProxy;
290 : PRUint8 mMaxPipelinedRequests;
291 :
292 : PRUint8 mRedirectionLimit;
293 :
294 : // we'll warn the user if we load an URL containing a userpass field
295 : // unless its length is less than this threshold. this warning is
296 : // intended to protect the user against spoofing attempts that use
297 : // the userpass field of the URL to obscure the actual origin server.
298 : PRUint8 mPhishyUserPassLength;
299 :
300 : PRUint8 mQoSBits;
301 :
302 : bool mPipeliningOverSSL;
303 :
304 : // cached value of whether or not the browser is in private browsing mode.
305 : enum {
306 : PRIVATE_BROWSING_OFF = false,
307 : PRIVATE_BROWSING_ON = true,
308 : PRIVATE_BROWSING_UNKNOWN = 2
309 : } mInPrivateBrowsingMode;
310 :
311 : nsCString mAccept;
312 : nsCString mAcceptLanguages;
313 : nsCString mAcceptEncodings;
314 :
315 : nsXPIDLCString mDefaultSocketType;
316 :
317 : // cache support
318 : PRUint32 mLastUniqueID;
319 : PRUint32 mSessionStartTime;
320 :
321 : // useragent components
322 : nsCString mLegacyAppName;
323 : nsCString mLegacyAppVersion;
324 : nsCString mPlatform;
325 : nsCString mOscpu;
326 : nsCString mMisc;
327 : nsCString mProduct;
328 : nsXPIDLCString mProductSub;
329 : nsXPIDLCString mAppName;
330 : nsXPIDLCString mAppVersion;
331 : nsCString mCompatFirefox;
332 : nsXPIDLCString mCompatDevice;
333 :
334 : nsCString mUserAgent;
335 : nsXPIDLCString mUserAgentOverride;
336 : bool mUserAgentIsDirty; // true if mUserAgent should be rebuilt
337 :
338 : bool mUseCache;
339 :
340 : bool mPromptTempRedirect;
341 : // mSendSecureXSiteReferrer: default is false,
342 : // if true allow referrer headers between secure non-matching hosts
343 : bool mSendSecureXSiteReferrer;
344 :
345 : // Persistent HTTPS caching flag
346 : bool mEnablePersistentHttpsCaching;
347 :
348 : // For broadcasting the preference to not be tracked
349 : bool mDoNotTrackEnabled;
350 :
351 : // Whether telemetry is reported or not
352 : bool mTelemetryEnabled;
353 :
354 : // The value of network.allow-experiments
355 : bool mAllowExperiments;
356 :
357 : // Try to use SPDY features instead of HTTP/1.1 over SSL
358 : bool mEnableSpdy;
359 : bool mCoalesceSpdy;
360 : bool mUseAlternateProtocol;
361 : PRUint32 mSpdySendingChunkSize;
362 : PRIntervalTime mSpdyPingThreshold;
363 : PRIntervalTime mSpdyPingTimeout;
364 : };
365 :
366 : //-----------------------------------------------------------------------------
367 :
368 : extern nsHttpHandler *gHttpHandler;
369 :
370 : //-----------------------------------------------------------------------------
371 : // nsHttpsHandler - thin wrapper to distinguish the HTTP handler from the
372 : // HTTPS handler (even though they share the same impl).
373 : //-----------------------------------------------------------------------------
374 :
375 : class nsHttpsHandler : public nsIHttpProtocolHandler
376 : , public nsSupportsWeakReference
377 : {
378 : public:
379 : // we basically just want to override GetScheme and GetDefaultPort...
380 : // all other methods should be forwarded to the nsHttpHandler instance.
381 :
382 : NS_DECL_ISUPPORTS
383 : NS_DECL_NSIPROTOCOLHANDLER
384 0 : NS_FORWARD_NSIPROXIEDPROTOCOLHANDLER (gHttpHandler->)
385 0 : NS_FORWARD_NSIHTTPPROTOCOLHANDLER (gHttpHandler->)
386 :
387 39 : nsHttpsHandler() { }
388 156 : virtual ~nsHttpsHandler() { }
389 :
390 : nsresult Init();
391 : };
392 :
393 : #endif // nsHttpHandler_h__
|