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
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/
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
15 * The Original Code is Mozilla.
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.
23 * Darin Fisher <darin@netscape.com> (original author)
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.
37 * ***** END LICENSE BLOCK ***** */
39 #ifndef nsHttpHandler_h__
40 #define nsHttpHandler_h__
43 #include "nsHttpAuthCache.h"
44 #include "nsHttpConnection.h"
45 #include "nsHttpConnectionMgr.h"
47 #include "nsXPIDLString.h"
50 #include "nsWeakReference.h"
51 #include "nsVoidArray.h"
53 #include "nsIHttpProtocolHandler.h"
54 #include "nsIProtocolProxyService.h"
55 #include "nsIIOService.h"
56 #include "nsIObserver.h"
57 #include "nsIObserverService.h"
58 #include "nsIProxyObjectManager.h"
59 #include "nsIStreamConverterService.h"
60 #include "nsICacheSession.h"
61 #include "nsICookieService.h"
62 #include "nsIIDNService.h"
65 class nsHttpConnectionInfo
;
66 class nsHttpHeaderArray
;
67 class nsHttpTransaction
;
68 class nsAHttpTransaction
;
72 //-----------------------------------------------------------------------------
73 // nsHttpHandler - protocol handler for HTTP and HTTPS
74 //-----------------------------------------------------------------------------
76 class nsHttpHandler
: public nsIHttpProtocolHandler
78 , public nsSupportsWeakReference
82 NS_DECL_NSIPROTOCOLHANDLER
83 NS_DECL_NSIPROXIEDPROTOCOLHANDLER
84 NS_DECL_NSIHTTPPROTOCOLHANDLER
88 virtual ~nsHttpHandler();
91 nsresult
AddStandardRequestHeaders(nsHttpHeaderArray
*,
94 PRBool
IsAcceptableEncoding(const char *encoding
);
96 const nsAFlatCString
&UserAgent();
98 nsHttpVersion
HttpVersion() { return mHttpVersion
; }
99 nsHttpVersion
ProxyHttpVersion() { return mProxyHttpVersion
; }
100 PRUint8
ReferrerLevel() { return mReferrerLevel
; }
101 PRBool
SendSecureXSiteReferrer() { return mSendSecureXSiteReferrer
; }
102 PRUint8
RedirectionLimit() { return mRedirectionLimit
; }
103 PRUint16
IdleTimeout() { return mIdleTimeout
; }
104 PRUint16
MaxRequestAttempts() { return mMaxRequestAttempts
; }
105 const char *DefaultSocketType() { return mDefaultSocketType
.get(); /* ok to return null */ }
106 nsIIDNService
*IDNConverter() { return mIDNConverter
; }
107 PRUint32
PhishyUserPassLength() { return mPhishyUserPassLength
; }
109 PRBool
CanCacheAllSSLContent() { return mEnablePersistentHttpsCaching
; }
111 nsHttpAuthCache
*AuthCache() { return &mAuthCache
; }
112 nsHttpConnectionMgr
*ConnMgr() { return mConnMgr
; }
115 nsresult
GetCacheSession(nsCacheStoragePolicy
, nsICacheSession
**);
116 PRUint32
GenerateUniqueID() { return ++mLastUniqueID
; }
117 PRUint32
SessionStartTime() { return mSessionStartTime
; }
120 // Connection management methods:
122 // - the handler only owns idle connections; it does not own active
125 // - the handler keeps a count of active connections to enforce the
126 // steady-state max-connections pref.
129 // Called to kick-off a new transaction, by default the transaction
130 // will be put on the pending transaction queue if it cannot be
131 // initiated at this time. Callable from any thread.
132 nsresult
InitiateTransaction(nsHttpTransaction
*trans
, PRInt32 priority
)
134 return mConnMgr
->AddTransaction(trans
, priority
);
137 // Called to change the priority of an existing transaction that has
138 // already been initiated.
139 nsresult
RescheduleTransaction(nsHttpTransaction
*trans
, PRInt32 priority
)
141 return mConnMgr
->RescheduleTransaction(trans
, priority
);
144 // Called to cancel a transaction, which may or may not be assigned to
145 // a connection. Callable from any thread.
146 nsresult
CancelTransaction(nsHttpTransaction
*trans
, nsresult reason
)
148 return mConnMgr
->CancelTransaction(trans
, reason
);
151 // Called when a connection is done processing a transaction. Callable
153 nsresult
ReclaimConnection(nsHttpConnection
*conn
)
155 return mConnMgr
->ReclaimConnection(conn
);
158 nsresult
ProcessPendingQ(nsHttpConnectionInfo
*cinfo
)
160 return mConnMgr
->ProcessPendingQ(cinfo
);
163 nsresult
GetSocketThreadTarget(nsIEventTarget
**target
)
165 return mConnMgr
->GetSocketThreadTarget(target
);
169 // The HTTP handler caches pointers to specific XPCOM services, and
170 // provides the following helper routines for accessing those services:
172 nsresult
GetStreamConverterService(nsIStreamConverterService
**);
173 nsresult
GetIOService(nsIIOService
** service
);
174 nsICookieService
* GetCookieService(); // not addrefed
176 // Called by the channel before writing a request
177 void OnModifyRequest(nsIHttpChannel
*chan
)
179 NotifyObservers(chan
, NS_HTTP_ON_MODIFY_REQUEST_TOPIC
);
182 // Called by the channel once headers are available
183 void OnExamineResponse(nsIHttpChannel
*chan
)
185 NotifyObservers(chan
, NS_HTTP_ON_EXAMINE_RESPONSE_TOPIC
);
188 // Called by the channel once headers have been merged with cached headers
189 void OnExamineMergedResponse(nsIHttpChannel
*chan
)
191 NotifyObservers(chan
, NS_HTTP_ON_EXAMINE_MERGED_RESPONSE_TOPIC
);
194 // Called by channels before a redirect happens. This notifies both the
195 // channel's and the global redirect observers.
196 nsresult
OnChannelRedirect(nsIChannel
* oldChan
, nsIChannel
* newChan
,
201 // Useragent/prefs helper methods
203 void BuildUserAgent();
204 void InitUserAgentComponents();
205 void PrefsChanged(nsIPrefBranch
*prefs
, const char *pref
);
207 nsresult
SetAccept(const char *);
208 nsresult
SetAcceptLanguages(const char *);
209 nsresult
SetAcceptEncodings(const char *);
210 nsresult
SetAcceptCharsets(const char *);
212 nsresult
InitConnectionMgr();
213 void StartPruneDeadConnectionsTimer();
214 void StopPruneDeadConnectionsTimer();
216 void NotifyObservers(nsIHttpChannel
*chan
, const char *event
);
221 nsCOMPtr
<nsIIOService
> mIOService
;
222 nsCOMPtr
<nsIStreamConverterService
> mStreamConvSvc
;
223 nsCOMPtr
<nsIObserverService
> mObserverService
;
224 nsCOMPtr
<nsICookieService
> mCookieService
;
225 nsCOMPtr
<nsIIDNService
> mIDNConverter
;
226 nsCOMPtr
<nsITimer
> mTimer
;
228 // the authentication credentials cache
229 nsHttpAuthCache mAuthCache
;
231 // the connection manager
232 nsHttpConnectionMgr
*mConnMgr
;
238 PRUint8 mHttpVersion
;
239 PRUint8 mProxyHttpVersion
;
240 PRUint8 mCapabilities
;
241 PRUint8 mProxyCapabilities
;
242 PRUint8 mReferrerLevel
;
244 PRUint16 mIdleTimeout
;
245 PRUint16 mMaxRequestAttempts
;
246 PRUint16 mMaxRequestDelay
;
248 PRUint16 mMaxConnections
;
249 PRUint8 mMaxConnectionsPerServer
;
250 PRUint8 mMaxPersistentConnectionsPerServer
;
251 PRUint8 mMaxPersistentConnectionsPerProxy
;
252 PRUint8 mMaxPipelinedRequests
;
254 PRUint8 mRedirectionLimit
;
256 // we'll warn the user if we load an URL containing a userpass field
257 // unless its length is less than this threshold. this warning is
258 // intended to protect the user against spoofing attempts that use
259 // the userpass field of the URL to obscure the actual origin server.
260 PRUint8 mPhishyUserPassLength
;
262 PRPackedBool mPipeliningOverSSL
;
265 nsCString mAcceptLanguages
;
266 nsCString mAcceptEncodings
;
267 nsCString mAcceptCharsets
;
269 nsXPIDLCString mDefaultSocketType
;
272 PRUint32 mLastUniqueID
;
273 PRUint32 mSessionStartTime
;
275 // useragent components
276 nsXPIDLCString mAppName
;
277 nsXPIDLCString mAppVersion
;
280 nsXPIDLCString mSecurity
;
283 nsXPIDLCString mVendor
;
284 nsXPIDLCString mVendorSub
;
285 nsXPIDLCString mVendorComment
;
287 nsXPIDLCString mProductSub
;
288 nsXPIDLCString mProductComment
;
291 nsCString mUserAgent
;
292 nsXPIDLCString mUserAgentOverride
;
293 PRPackedBool mUserAgentIsDirty
; // true if mUserAgent should be rebuilt
295 PRPackedBool mUseCache
;
296 // mSendSecureXSiteReferrer: default is false,
297 // if true allow referrer headers between secure non-matching hosts
298 PRPackedBool mSendSecureXSiteReferrer
;
300 // Persistent HTTPS caching flag
301 PRPackedBool mEnablePersistentHttpsCaching
;
304 //-----------------------------------------------------------------------------
306 extern nsHttpHandler
*gHttpHandler
;
308 //-----------------------------------------------------------------------------
309 // nsHttpsHandler - thin wrapper to distinguish the HTTP handler from the
310 // HTTPS handler (even though they share the same impl).
311 //-----------------------------------------------------------------------------
313 class nsHttpsHandler
: public nsIHttpProtocolHandler
314 , public nsSupportsWeakReference
317 // we basically just want to override GetScheme and GetDefaultPort...
318 // all other methods should be forwarded to the nsHttpHandler instance.
321 NS_DECL_NSIPROTOCOLHANDLER
322 NS_FORWARD_NSIPROXIEDPROTOCOLHANDLER (gHttpHandler
->)
323 NS_FORWARD_NSIHTTPPROTOCOLHANDLER (gHttpHandler
->)
326 virtual ~nsHttpsHandler() { }
331 #endif // nsHttpHandler_h__