1
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3
* The contents of this file are subject to the Mozilla Public
4
* License Version 1.1 (the "License"); you may not use this file
5
* except in compliance with the License. You may obtain a copy of
6
* the License at http://www.mozilla.org/MPL/
8
* Software distributed under the License is distributed on an "AS
9
* IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
10
* implied. See the License for the specific language governing
11
* rights and limitations under the License.
13
* The Original Code is Mozilla.
15
* The Initial Developer of the Original Code is Netscape
16
* Communications. Portions created by Netscape Communications are
17
* Copyright (C) 2001 by Netscape Communications. All
21
* Darin Fisher <darin@netscape.com> (original author)
24
#ifndef nsHttpHandler_h__
25
#define nsHttpHandler_h__
28
#include "nsHttpAuthCache.h"
29
#include "nsHttpConnection.h"
30
#include "nsHttpConnectionMgr.h"
32
#include "nsXPIDLString.h"
35
#include "nsWeakReference.h"
36
#include "nsVoidArray.h"
38
#include "nsIHttpProtocolHandler.h"
39
#include "nsIProtocolProxyService.h"
40
#include "nsIIOService.h"
41
#include "nsIObserver.h"
42
#include "nsIObserverService.h"
43
#include "nsIProxyObjectManager.h"
45
#include "nsIStreamConverterService.h"
46
#include "nsICacheSession.h"
47
#include "nsIEventQueueService.h"
48
#include "nsICookieService.h"
49
#include "nsIMIMEService.h"
50
#include "nsIIDNService.h"
53
class nsHttpConnectionInfo;
54
class nsHttpHeaderArray;
55
class nsHttpTransaction;
56
class nsAHttpTransaction;
60
//-----------------------------------------------------------------------------
61
// nsHttpHandler - protocol handler for HTTP and HTTPS
62
//-----------------------------------------------------------------------------
64
class nsHttpHandler : public nsIHttpProtocolHandler
66
, public nsSupportsWeakReference
70
NS_DECL_NSIPROTOCOLHANDLER
71
NS_DECL_NSIPROXIEDPROTOCOLHANDLER
72
NS_DECL_NSIHTTPPROTOCOLHANDLER
76
virtual ~nsHttpHandler();
79
nsresult AddStandardRequestHeaders(nsHttpHeaderArray *,
82
PRBool IsAcceptableEncoding(const char *encoding);
84
const nsAFlatCString &UserAgent();
86
nsHttpVersion HttpVersion() { return mHttpVersion; }
87
nsHttpVersion ProxyHttpVersion() { return mProxyHttpVersion; }
88
PRUint8 ReferrerLevel() { return mReferrerLevel; }
89
PRBool SendSecureXSiteReferrer() { return mSendSecureXSiteReferrer; }
90
PRUint8 RedirectionLimit() { return mRedirectionLimit; }
91
PRUint16 IdleTimeout() { return mIdleTimeout; }
92
PRUint16 MaxRequestAttempts() { return mMaxRequestAttempts; }
93
const char *DefaultSocketType() { return mDefaultSocketType.get(); /* ok to return null */ }
94
nsIIDNService *IDNConverter() { return mIDNConverter; }
95
PRUint32 PhishyUserPassLength() { return mPhishyUserPassLength; }
97
PRBool IsPersistentHttpsCachingEnabled() { return mEnablePersistentHttpsCaching; }
99
nsHttpAuthCache *AuthCache() { return &mAuthCache; }
100
nsHttpConnectionMgr *ConnMgr() { return mConnMgr; }
103
nsresult GetCacheSession(nsCacheStoragePolicy, nsICacheSession **);
104
PRUint32 GenerateUniqueID() { return ++mLastUniqueID; }
105
PRUint32 SessionStartTime() { return mSessionStartTime; }
108
// Connection management methods:
110
// - the handler only owns idle connections; it does not own active
113
// - the handler keeps a count of active connections to enforce the
114
// steady-state max-connections pref.
117
// Called to kick-off a new transaction, by default the transaction
118
// will be put on the pending transaction queue if it cannot be
119
// initiated at this time. Callable from any thread.
120
nsresult InitiateTransaction(nsHttpTransaction *trans)
122
return mConnMgr->AddTransaction(trans);
125
// Called to cancel a transaction, which may or may not be assigned to
126
// a connection. Callable from any thread.
127
nsresult CancelTransaction(nsHttpTransaction *trans, nsresult reason)
129
return mConnMgr->CancelTransaction(trans, reason);
132
// Called when a connection is done processing a transaction. Callable
134
nsresult ReclaimConnection(nsHttpConnection *conn)
136
return mConnMgr->ReclaimConnection(conn);
139
nsresult ProcessPendingQ(nsHttpConnectionInfo *cinfo)
141
return mConnMgr->ProcessPendingQ(cinfo);
144
nsresult GetSocketThreadEventTarget(nsIEventTarget **target)
146
return mConnMgr->GetSocketThreadEventTarget(target);
150
// The HTTP handler caches pointers to specific XPCOM services, and
151
// provides the following helper routines for accessing those services:
153
nsresult GetEventQueueService(nsIEventQueueService **);
154
nsresult GetStreamConverterService(nsIStreamConverterService **);
155
nsresult GetMimeService(nsIMIMEService **);
156
nsresult GetIOService(nsIIOService** service);
157
nsICookieService * GetCookieService(); // not addrefed
159
// Called by the channel before writing a request
160
void OnModifyRequest(nsIHttpChannel *chan)
162
NotifyObservers(chan, NS_HTTP_ON_MODIFY_REQUEST_TOPIC);
165
// Called by the channel once headers are available
166
void OnExamineResponse(nsIHttpChannel *chan)
168
NotifyObservers(chan, NS_HTTP_ON_EXAMINE_RESPONSE_TOPIC);
171
// Called by the channel once headers have been merged with cached headers
172
void OnExamineMergedResponse(nsIHttpChannel *chan)
174
NotifyObservers(chan, NS_HTTP_ON_EXAMINE_MERGED_RESPONSE_TOPIC);
180
// Useragent/prefs helper methods
182
void BuildUserAgent();
183
void InitUserAgentComponents();
184
void PrefsChanged(nsIPrefBranch *prefs, const char *pref);
186
nsresult SetAccept(const char *);
187
nsresult SetAcceptLanguages(const char *);
188
nsresult SetAcceptEncodings(const char *);
189
nsresult SetAcceptCharsets(const char *);
191
nsresult InitConnectionMgr();
192
void StartPruneDeadConnectionsTimer();
193
void StopPruneDeadConnectionsTimer();
195
void NotifyObservers(nsIHttpChannel *chan, const char *event);
200
nsCOMPtr<nsIIOService> mIOService;
201
nsCOMPtr<nsIEventQueueService> mEventQueueService;
202
nsCOMPtr<nsIStreamConverterService> mStreamConvSvc;
203
nsCOMPtr<nsIObserverService> mObserverService;
204
nsCOMPtr<nsICookieService> mCookieService;
205
nsCOMPtr<nsIMIMEService> mMimeService;
206
nsCOMPtr<nsIIDNService> mIDNConverter;
207
nsCOMPtr<nsITimer> mTimer;
209
// the authentication credentials cache
210
nsHttpAuthCache mAuthCache;
212
// the connection manager
213
nsHttpConnectionMgr *mConnMgr;
219
PRUint8 mHttpVersion;
220
PRUint8 mProxyHttpVersion;
221
PRUint8 mCapabilities;
222
PRUint8 mProxyCapabilities;
223
PRUint8 mReferrerLevel;
225
PRUint16 mIdleTimeout;
226
PRUint16 mMaxRequestAttempts;
227
PRUint16 mMaxRequestDelay;
229
PRUint16 mMaxConnections;
230
PRUint8 mMaxConnectionsPerServer;
231
PRUint8 mMaxPersistentConnectionsPerServer;
232
PRUint8 mMaxPersistentConnectionsPerProxy;
233
PRUint8 mMaxPipelinedRequests;
235
PRUint8 mRedirectionLimit;
237
// we'll warn the user if we load an URL containing a userpass field
238
// unless its length is less than this threshold. this warning is
239
// intended to protect the user against spoofing attempts that use
240
// the userpass field of the URL to obscure the actual origin server.
241
PRUint8 mPhishyUserPassLength;
244
nsCString mAcceptLanguages;
245
nsCString mAcceptEncodings;
246
nsCString mAcceptCharsets;
248
nsXPIDLCString mDefaultSocketType;
251
nsCOMPtr<nsICacheSession> mCacheSession_ANY;
252
nsCOMPtr<nsICacheSession> mCacheSession_MEM;
253
PRUint32 mLastUniqueID;
254
PRUint32 mSessionStartTime;
256
// useragent components
257
nsXPIDLCString mAppName;
258
nsXPIDLCString mAppVersion;
259
nsXPIDLCString mPlatform;
260
nsXPIDLCString mOscpu;
261
nsXPIDLCString mSecurity;
262
nsXPIDLCString mLanguage;
263
nsXPIDLCString mMisc;
264
nsXPIDLCString mVendor;
265
nsXPIDLCString mVendorSub;
266
nsXPIDLCString mVendorComment;
267
nsXPIDLCString mProduct;
268
nsXPIDLCString mProductSub;
269
nsXPIDLCString mProductComment;
271
nsCString mUserAgent;
272
nsXPIDLCString mUserAgentOverride;
273
PRPackedBool mUserAgentIsDirty; // true if mUserAgent should be rebuilt
275
PRPackedBool mUseCache;
276
// mSendSecureXSiteReferrer: default is false,
277
// if true allow referrer headers between secure non-matching hosts
278
PRPackedBool mSendSecureXSiteReferrer;
280
// Persistent HTTPS caching flag
281
PRPackedBool mEnablePersistentHttpsCaching;
284
//-----------------------------------------------------------------------------
286
extern nsHttpHandler *gHttpHandler;
288
//-----------------------------------------------------------------------------
289
// nsHttpsHandler - thin wrapper to distinguish the HTTP handler from the
290
// HTTPS handler (even though they share the same impl).
291
//-----------------------------------------------------------------------------
293
class nsHttpsHandler : public nsIHttpProtocolHandler
294
, public nsSupportsWeakReference
297
// we basically just want to override GetScheme and GetDefaultPort...
298
// all other methods should be forwarded to the nsHttpHandler instance.
301
NS_DECL_NSIPROTOCOLHANDLER
302
NS_FORWARD_NSIPROXIEDPROTOCOLHANDLER (gHttpHandler->)
303
NS_FORWARD_NSIHTTPPROTOCOLHANDLER (gHttpHandler->)
306
virtual ~nsHttpsHandler() { }
311
#endif // nsHttpHandler_h__