~ubuntu-branches/ubuntu/precise/kompozer/precise

« back to all changes in this revision

Viewing changes to mozilla/netwerk/protocol/http/src/nsHttpHandler.h

  • Committer: Bazaar Package Importer
  • Author(s): Anthony Yarusso
  • Date: 2007-08-27 01:11:03 UTC
  • Revision ID: james.westby@ubuntu.com-20070827011103-2jgf4s6532gqu2ka
Tags: upstream-0.7.10
ImportĀ upstreamĀ versionĀ 0.7.10

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
 
2
/*
 
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/
 
7
 * 
 
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.
 
12
 * 
 
13
 * The Original Code is Mozilla.
 
14
 * 
 
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
 
18
 * Rights Reserved.
 
19
 * 
 
20
 * Contributor(s): 
 
21
 *   Darin Fisher <darin@netscape.com> (original author)
 
22
 */
 
23
 
 
24
#ifndef nsHttpHandler_h__
 
25
#define nsHttpHandler_h__
 
26
 
 
27
#include "nsHttp.h"
 
28
#include "nsHttpAuthCache.h"
 
29
#include "nsHttpConnection.h"
 
30
#include "nsHttpConnectionMgr.h"
 
31
 
 
32
#include "nsXPIDLString.h"
 
33
#include "nsString.h"
 
34
#include "nsCOMPtr.h"
 
35
#include "nsWeakReference.h"
 
36
#include "nsVoidArray.h"
 
37
 
 
38
#include "nsIHttpProtocolHandler.h"
 
39
#include "nsIProtocolProxyService.h"
 
40
#include "nsIIOService.h"
 
41
#include "nsIObserver.h"
 
42
#include "nsIObserverService.h"
 
43
#include "nsIProxyObjectManager.h"
 
44
#include "nsIProxy.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"
 
51
#include "nsITimer.h"
 
52
 
 
53
class nsHttpConnectionInfo;
 
54
class nsHttpHeaderArray;
 
55
class nsHttpTransaction;
 
56
class nsAHttpTransaction;
 
57
class nsIHttpChannel;
 
58
class nsIPrefBranch;
 
59
 
 
60
//-----------------------------------------------------------------------------
 
61
// nsHttpHandler - protocol handler for HTTP and HTTPS
 
62
//-----------------------------------------------------------------------------
 
63
 
 
64
class nsHttpHandler : public nsIHttpProtocolHandler
 
65
                    , public nsIObserver
 
66
                    , public nsSupportsWeakReference
 
67
{
 
68
public:
 
69
    NS_DECL_ISUPPORTS
 
70
    NS_DECL_NSIPROTOCOLHANDLER
 
71
    NS_DECL_NSIPROXIEDPROTOCOLHANDLER
 
72
    NS_DECL_NSIHTTPPROTOCOLHANDLER
 
73
    NS_DECL_NSIOBSERVER
 
74
 
 
75
    nsHttpHandler();
 
76
    virtual ~nsHttpHandler();
 
77
 
 
78
    nsresult Init();
 
79
    nsresult AddStandardRequestHeaders(nsHttpHeaderArray *,
 
80
                                       PRUint8 capabilities,
 
81
                                       PRBool useProxy);
 
82
    PRBool   IsAcceptableEncoding(const char *encoding);
 
83
 
 
84
    const nsAFlatCString &UserAgent();
 
85
 
 
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; }
 
96
    
 
97
    PRBool         IsPersistentHttpsCachingEnabled() { return mEnablePersistentHttpsCaching; }
 
98
 
 
99
    nsHttpAuthCache     *AuthCache() { return &mAuthCache; }
 
100
    nsHttpConnectionMgr *ConnMgr()   { return mConnMgr; }
 
101
 
 
102
    // cache support
 
103
    nsresult GetCacheSession(nsCacheStoragePolicy, nsICacheSession **);
 
104
    PRUint32 GenerateUniqueID() { return ++mLastUniqueID; }
 
105
    PRUint32 SessionStartTime() { return mSessionStartTime; }
 
106
 
 
107
    //
 
108
    // Connection management methods:
 
109
    //
 
110
    // - the handler only owns idle connections; it does not own active
 
111
    //   connections.
 
112
    //
 
113
    // - the handler keeps a count of active connections to enforce the
 
114
    //   steady-state max-connections pref.
 
115
    // 
 
116
 
 
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)
 
121
    {
 
122
        return mConnMgr->AddTransaction(trans);
 
123
    }
 
124
 
 
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)
 
128
    {
 
129
        return mConnMgr->CancelTransaction(trans, reason);
 
130
    }
 
131
 
 
132
    // Called when a connection is done processing a transaction.  Callable
 
133
    // from any thread.
 
134
    nsresult ReclaimConnection(nsHttpConnection *conn)
 
135
    {
 
136
        return mConnMgr->ReclaimConnection(conn);
 
137
    }
 
138
 
 
139
    nsresult ProcessPendingQ(nsHttpConnectionInfo *cinfo)
 
140
    {
 
141
        return mConnMgr->ProcessPendingQ(cinfo);
 
142
    }
 
143
 
 
144
    nsresult GetSocketThreadEventTarget(nsIEventTarget **target)
 
145
    {
 
146
        return mConnMgr->GetSocketThreadEventTarget(target);
 
147
    }
 
148
 
 
149
    //
 
150
    // The HTTP handler caches pointers to specific XPCOM services, and
 
151
    // provides the following helper routines for accessing those services:
 
152
    //
 
153
    nsresult GetEventQueueService(nsIEventQueueService **);
 
154
    nsresult GetStreamConverterService(nsIStreamConverterService **);
 
155
    nsresult GetMimeService(nsIMIMEService **);
 
156
    nsresult GetIOService(nsIIOService** service);
 
157
    nsICookieService * GetCookieService(); // not addrefed
 
158
 
 
159
    // Called by the channel before writing a request
 
160
    void OnModifyRequest(nsIHttpChannel *chan)
 
161
    {
 
162
        NotifyObservers(chan, NS_HTTP_ON_MODIFY_REQUEST_TOPIC);
 
163
    }
 
164
 
 
165
    // Called by the channel once headers are available
 
166
    void OnExamineResponse(nsIHttpChannel *chan)
 
167
    {
 
168
        NotifyObservers(chan, NS_HTTP_ON_EXAMINE_RESPONSE_TOPIC);
 
169
    }
 
170
 
 
171
    // Called by the channel once headers have been merged with cached headers
 
172
    void OnExamineMergedResponse(nsIHttpChannel *chan)
 
173
    {
 
174
        NotifyObservers(chan, NS_HTTP_ON_EXAMINE_MERGED_RESPONSE_TOPIC);
 
175
    }
 
176
 
 
177
private:
 
178
 
 
179
    //
 
180
    // Useragent/prefs helper methods
 
181
    //
 
182
    void     BuildUserAgent();
 
183
    void     InitUserAgentComponents();
 
184
    void     PrefsChanged(nsIPrefBranch *prefs, const char *pref);
 
185
 
 
186
    nsresult SetAccept(const char *);
 
187
    nsresult SetAcceptLanguages(const char *);
 
188
    nsresult SetAcceptEncodings(const char *);
 
189
    nsresult SetAcceptCharsets(const char *);
 
190
 
 
191
    nsresult InitConnectionMgr();
 
192
    void     StartPruneDeadConnectionsTimer();
 
193
    void     StopPruneDeadConnectionsTimer();
 
194
 
 
195
    void     NotifyObservers(nsIHttpChannel *chan, const char *event);
 
196
 
 
197
private:
 
198
 
 
199
    // cached services
 
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;
 
208
 
 
209
    // the authentication credentials cache
 
210
    nsHttpAuthCache mAuthCache;
 
211
 
 
212
    // the connection manager
 
213
    nsHttpConnectionMgr *mConnMgr;
 
214
 
 
215
    //
 
216
    // prefs
 
217
    //
 
218
 
 
219
    PRUint8  mHttpVersion;
 
220
    PRUint8  mProxyHttpVersion;
 
221
    PRUint8  mCapabilities;
 
222
    PRUint8  mProxyCapabilities;
 
223
    PRUint8  mReferrerLevel;
 
224
 
 
225
    PRUint16 mIdleTimeout;
 
226
    PRUint16 mMaxRequestAttempts;
 
227
    PRUint16 mMaxRequestDelay;
 
228
 
 
229
    PRUint16 mMaxConnections;
 
230
    PRUint8  mMaxConnectionsPerServer;
 
231
    PRUint8  mMaxPersistentConnectionsPerServer;
 
232
    PRUint8  mMaxPersistentConnectionsPerProxy;
 
233
    PRUint8  mMaxPipelinedRequests;
 
234
 
 
235
    PRUint8  mRedirectionLimit;
 
236
 
 
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;
 
242
 
 
243
    nsCString mAccept;
 
244
    nsCString mAcceptLanguages;
 
245
    nsCString mAcceptEncodings;
 
246
    nsCString mAcceptCharsets;
 
247
 
 
248
    nsXPIDLCString mDefaultSocketType;
 
249
 
 
250
    // cache support
 
251
    nsCOMPtr<nsICacheSession> mCacheSession_ANY;
 
252
    nsCOMPtr<nsICacheSession> mCacheSession_MEM;
 
253
    PRUint32                  mLastUniqueID;
 
254
    PRUint32                  mSessionStartTime;
 
255
 
 
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;
 
270
 
 
271
    nsCString      mUserAgent;
 
272
    nsXPIDLCString mUserAgentOverride;
 
273
    PRPackedBool   mUserAgentIsDirty; // true if mUserAgent should be rebuilt
 
274
 
 
275
    PRPackedBool   mUseCache;
 
276
    // mSendSecureXSiteReferrer: default is false, 
 
277
    // if true allow referrer headers between secure non-matching hosts
 
278
    PRPackedBool   mSendSecureXSiteReferrer;
 
279
 
 
280
    // Persistent HTTPS caching flag
 
281
    PRPackedBool   mEnablePersistentHttpsCaching;
 
282
};
 
283
 
 
284
//-----------------------------------------------------------------------------
 
285
 
 
286
extern nsHttpHandler *gHttpHandler;
 
287
 
 
288
//-----------------------------------------------------------------------------
 
289
// nsHttpsHandler - thin wrapper to distinguish the HTTP handler from the
 
290
//                  HTTPS handler (even though they share the same impl).
 
291
//-----------------------------------------------------------------------------
 
292
 
 
293
class nsHttpsHandler : public nsIHttpProtocolHandler
 
294
                     , public nsSupportsWeakReference
 
295
{
 
296
public:
 
297
    // we basically just want to override GetScheme and GetDefaultPort...
 
298
    // all other methods should be forwarded to the nsHttpHandler instance.
 
299
    
 
300
    NS_DECL_ISUPPORTS
 
301
    NS_DECL_NSIPROTOCOLHANDLER
 
302
    NS_FORWARD_NSIPROXIEDPROTOCOLHANDLER (gHttpHandler->)
 
303
    NS_FORWARD_NSIHTTPPROTOCOLHANDLER    (gHttpHandler->)
 
304
 
 
305
    nsHttpsHandler() { }
 
306
    virtual ~nsHttpsHandler() { }
 
307
 
 
308
    nsresult Init();
 
309
};
 
310
 
 
311
#endif // nsHttpHandler_h__