1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set sw=2 ts=8 et tw=80 : */
/* ***** BEGIN LICENSE BLOCK *****
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
*
* The contents of this file are subject to the Mozilla Public License Version
* 1.1 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
* http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
*
* The Original Code is mozilla.org code.
*
* The Initial Developer of the Original Code is
* The Mozilla Foundation.
* Portions created by the Initial Developer are Copyright (C) 2010
* the Initial Developer. All Rights Reserved.
*
* Contributor(s):
* Daniel Witte <dwitte@mozilla.com>
* Jason Duell <jduell.mcbugs@gmail.com>
*
* Alternatively, the contents of this file may be used under the terms of
* either the GNU General Public License Version 2 or later (the "GPL"), or
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
* in which case the provisions of the GPL or the LGPL are applicable instead
* of those above. If you wish to allow use of your version of this file only
* under the terms of either the GPL or the LGPL, and not to allow others to
* use your version of this file under the terms of the MPL, indicate your
* decision by deleting the provisions above and replace them with the notice
* and other provisions required by the GPL or the LGPL. If you do not delete
* the provisions above, a recipient may use your version of this file under
* the terms of any one of the MPL, the GPL or the LGPL.
*
* ***** END LICENSE BLOCK ***** */
#ifndef mozilla_net_HttpBaseChannel_h
#define mozilla_net_HttpBaseChannel_h
#include "nsHttp.h"
#include "nsAutoPtr.h"
#include "nsHashPropertyBag.h"
#include "nsProxyInfo.h"
#include "nsHttpRequestHead.h"
#include "nsHttpResponseHead.h"
#include "nsHttpConnectionInfo.h"
#include "nsIEncodedChannel.h"
#include "nsIHttpChannel.h"
#include "nsIHttpChannelInternal.h"
#include "nsIUploadChannel.h"
#include "nsIUploadChannel2.h"
#include "nsIProgressEventSink.h"
#include "nsIURI.h"
#include "nsIStringEnumerator.h"
#include "nsISupportsPriority.h"
#include "nsIApplicationCache.h"
#include "nsIResumableChannel.h"
#include "nsITraceableChannel.h"
#include "mozilla/net/NeckoCommon.h"
#include "nsThreadUtils.h"
namespace mozilla {
namespace net {
/*
* This class is a partial implementation of nsIHttpChannel. It contains code
* shared by nsHttpChannel and HttpChannelChild.
* - Note that this class has nothing to do with nsBaseChannel, which is an
* earlier effort at a base class for channels that somehow never made it all
* the way to the HTTP channel.
*/
class HttpBaseChannel : public nsHashPropertyBag
, public nsIEncodedChannel
, public nsIHttpChannel
, public nsIHttpChannelInternal
, public nsIUploadChannel
, public nsIUploadChannel2
, public nsISupportsPriority
, public nsIResumableChannel
, public nsITraceableChannel
{
public:
NS_DECL_ISUPPORTS_INHERITED
NS_DECL_NSIUPLOADCHANNEL
NS_DECL_NSIUPLOADCHANNEL2
NS_DECL_NSITRACEABLECHANNEL
HttpBaseChannel();
virtual ~HttpBaseChannel();
virtual nsresult Init(nsIURI *aURI, PRUint8 aCaps, nsProxyInfo *aProxyInfo);
// nsIRequest
NS_IMETHOD GetName(nsACString& aName);
NS_IMETHOD IsPending(bool *aIsPending);
NS_IMETHOD GetStatus(nsresult *aStatus);
NS_IMETHOD GetLoadGroup(nsILoadGroup **aLoadGroup);
NS_IMETHOD SetLoadGroup(nsILoadGroup *aLoadGroup);
NS_IMETHOD GetLoadFlags(nsLoadFlags *aLoadFlags);
NS_IMETHOD SetLoadFlags(nsLoadFlags aLoadFlags);
// nsIChannel
NS_IMETHOD GetOriginalURI(nsIURI **aOriginalURI);
NS_IMETHOD SetOriginalURI(nsIURI *aOriginalURI);
NS_IMETHOD GetURI(nsIURI **aURI);
NS_IMETHOD GetOwner(nsISupports **aOwner);
NS_IMETHOD SetOwner(nsISupports *aOwner);
NS_IMETHOD GetNotificationCallbacks(nsIInterfaceRequestor **aCallbacks);
NS_IMETHOD SetNotificationCallbacks(nsIInterfaceRequestor *aCallbacks);
NS_IMETHOD GetContentType(nsACString& aContentType);
NS_IMETHOD SetContentType(const nsACString& aContentType);
NS_IMETHOD GetContentCharset(nsACString& aContentCharset);
NS_IMETHOD SetContentCharset(const nsACString& aContentCharset);
NS_IMETHOD GetContentDisposition(PRUint32 *aContentDisposition);
NS_IMETHOD GetContentDispositionFilename(nsAString& aContentDispositionFilename);
NS_IMETHOD GetContentDispositionHeader(nsACString& aContentDispositionHeader);
NS_IMETHOD GetContentLength(PRInt32 *aContentLength);
NS_IMETHOD SetContentLength(PRInt32 aContentLength);
NS_IMETHOD Open(nsIInputStream **aResult);
// nsIEncodedChannel
NS_IMETHOD GetApplyConversion(bool *value);
NS_IMETHOD SetApplyConversion(bool value);
NS_IMETHOD GetContentEncodings(nsIUTF8StringEnumerator** aEncodings);
// HttpBaseChannel::nsIHttpChannel
NS_IMETHOD GetRequestMethod(nsACString& aMethod);
NS_IMETHOD SetRequestMethod(const nsACString& aMethod);
NS_IMETHOD GetReferrer(nsIURI **referrer);
NS_IMETHOD SetReferrer(nsIURI *referrer);
NS_IMETHOD GetRequestHeader(const nsACString& aHeader, nsACString& aValue);
NS_IMETHOD SetRequestHeader(const nsACString& aHeader,
const nsACString& aValue, bool aMerge);
NS_IMETHOD VisitRequestHeaders(nsIHttpHeaderVisitor *visitor);
NS_IMETHOD GetResponseHeader(const nsACString &header, nsACString &value);
NS_IMETHOD SetResponseHeader(const nsACString& header,
const nsACString& value, bool merge);
NS_IMETHOD VisitResponseHeaders(nsIHttpHeaderVisitor *visitor);
NS_IMETHOD GetAllowPipelining(bool *value);
NS_IMETHOD SetAllowPipelining(bool value);
NS_IMETHOD GetRedirectionLimit(PRUint32 *value);
NS_IMETHOD SetRedirectionLimit(PRUint32 value);
NS_IMETHOD IsNoStoreResponse(bool *value);
NS_IMETHOD IsNoCacheResponse(bool *value);
NS_IMETHOD GetResponseStatus(PRUint32 *aValue);
NS_IMETHOD GetResponseStatusText(nsACString& aValue);
NS_IMETHOD GetRequestSucceeded(bool *aValue);
// nsIHttpChannelInternal
NS_IMETHOD GetDocumentURI(nsIURI **aDocumentURI);
NS_IMETHOD SetDocumentURI(nsIURI *aDocumentURI);
NS_IMETHOD GetRequestVersion(PRUint32 *major, PRUint32 *minor);
NS_IMETHOD GetResponseVersion(PRUint32 *major, PRUint32 *minor);
NS_IMETHOD SetCookie(const char *aCookieHeader);
NS_IMETHOD GetForceAllowThirdPartyCookie(bool *aForce);
NS_IMETHOD SetForceAllowThirdPartyCookie(bool aForce);
NS_IMETHOD GetCanceled(bool *aCanceled);
NS_IMETHOD GetChannelIsForDownload(bool *aChannelIsForDownload);
NS_IMETHOD SetChannelIsForDownload(bool aChannelIsForDownload);
NS_IMETHOD SetCacheKeysRedirectChain(nsTArray<nsCString> *cacheKeys);
NS_IMETHOD GetLocalAddress(nsACString& addr);
NS_IMETHOD GetLocalPort(PRInt32* port);
NS_IMETHOD GetRemoteAddress(nsACString& addr);
NS_IMETHOD GetRemotePort(PRInt32* port);
NS_IMETHOD GetAllowSpdy(bool *aAllowSpdy);
NS_IMETHOD SetAllowSpdy(bool aAllowSpdy);
inline void CleanRedirectCacheChainIfNecessary()
{
if (mRedirectedCachekeys) {
delete mRedirectedCachekeys;
mRedirectedCachekeys = nsnull;
}
}
NS_IMETHOD HTTPUpgrade(const nsACString & aProtocolName,
nsIHttpUpgradeListener *aListener);
// nsISupportsPriority
NS_IMETHOD GetPriority(PRInt32 *value);
NS_IMETHOD AdjustPriority(PRInt32 delta);
// nsIResumableChannel
NS_IMETHOD GetEntityID(nsACString& aEntityID);
class nsContentEncodings : public nsIUTF8StringEnumerator
{
public:
NS_DECL_ISUPPORTS
NS_DECL_NSIUTF8STRINGENUMERATOR
nsContentEncodings(nsIHttpChannel* aChannel, const char* aEncodingHeader);
virtual ~nsContentEncodings();
private:
nsresult PrepareForNext(void);
// We do not own the buffer. The channel owns it.
const char* mEncodingHeader;
const char* mCurStart; // points to start of current header
const char* mCurEnd; // points to end of current header
// Hold a ref to our channel so that it can't go away and take the
// header with it.
nsCOMPtr<nsIHttpChannel> mChannel;
bool mReady;
};
nsHttpResponseHead * GetResponseHead() const { return mResponseHead; }
nsHttpRequestHead * GetRequestHead() { return &mRequestHead; }
const PRNetAddr& GetSelfAddr() { return mSelfAddr; }
const PRNetAddr& GetPeerAddr() { return mPeerAddr; }
public: /* Necko internal use only... */
bool ShouldRewriteRedirectToGET(PRUint32 httpStatus, nsHttpAtom method);
bool IsSafeMethod(nsHttpAtom method);
protected:
// Handle notifying listener, removing from loadgroup if request failed.
void DoNotifyListener();
virtual void DoNotifyListenerCleanup() = 0;
nsresult ApplyContentConversions();
void AddCookiesToRequest();
virtual nsresult SetupReplacementChannel(nsIURI *,
nsIChannel *,
bool preserveMethod,
bool forProxy);
// Helper function to simplify getting notification callbacks.
template <class T>
void GetCallback(nsCOMPtr<T> &aResult)
{
NS_QueryNotificationCallbacks(mCallbacks, mLoadGroup,
NS_GET_TEMPLATE_IID(T),
getter_AddRefs(aResult));
}
nsCOMPtr<nsIURI> mURI;
nsCOMPtr<nsIURI> mOriginalURI;
nsCOMPtr<nsIURI> mDocumentURI;
nsCOMPtr<nsIStreamListener> mListener;
nsCOMPtr<nsISupports> mListenerContext;
nsCOMPtr<nsILoadGroup> mLoadGroup;
nsCOMPtr<nsISupports> mOwner;
nsCOMPtr<nsIInterfaceRequestor> mCallbacks;
nsCOMPtr<nsIProgressEventSink> mProgressSink;
nsCOMPtr<nsIURI> mReferrer;
nsCOMPtr<nsIApplicationCache> mApplicationCache;
nsHttpRequestHead mRequestHead;
nsCOMPtr<nsIInputStream> mUploadStream;
nsAutoPtr<nsHttpResponseHead> mResponseHead;
nsRefPtr<nsHttpConnectionInfo> mConnectionInfo;
nsCString mSpec; // ASCII encoded URL spec
nsCString mContentTypeHint;
nsCString mContentCharsetHint;
nsCString mUserSetCookieHeader;
PRNetAddr mSelfAddr;
PRNetAddr mPeerAddr;
// HTTP Upgrade Data
nsCString mUpgradeProtocol;
nsCOMPtr<nsIHttpUpgradeListener> mUpgradeProtocolCallback;
// Resumable channel specific data
nsCString mEntityID;
PRUint64 mStartPos;
nsresult mStatus;
PRUint32 mLoadFlags;
PRInt16 mPriority;
PRUint8 mCaps;
PRUint8 mRedirectionLimit;
PRUint32 mApplyConversion : 1;
PRUint32 mCanceled : 1;
PRUint32 mIsPending : 1;
PRUint32 mWasOpened : 1;
PRUint32 mResponseHeadersModified : 1;
PRUint32 mAllowPipelining : 1;
PRUint32 mForceAllowThirdPartyCookie : 1;
PRUint32 mUploadStreamHasHeaders : 1;
PRUint32 mInheritApplicationCache : 1;
PRUint32 mChooseApplicationCache : 1;
PRUint32 mLoadedFromApplicationCache : 1;
PRUint32 mChannelIsForDownload : 1;
PRUint32 mTracingEnabled : 1;
// True if timing collection is enabled
PRUint32 mTimingEnabled : 1;
PRUint32 mAllowSpdy : 1;
// Current suspension depth for this channel object
PRUint32 mSuspendCount;
nsTArray<nsCString> *mRedirectedCachekeys;
};
// Share some code while working around C++'s absurd inability to handle casting
// of member functions between base/derived types.
// - We want to store member function pointer to call at resume time, but one
// such function--HandleAsyncAbort--we want to share between the
// nsHttpChannel/HttpChannelChild. Can't define it in base class, because
// then we'd have to cast member function ptr between base/derived class
// types. Sigh...
template <class T>
class HttpAsyncAborter
{
public:
HttpAsyncAborter(T *derived) : mThis(derived), mCallOnResume(0) {}
// Aborts channel: calls OnStart/Stop with provided status, removes channel
// from loadGroup.
nsresult AsyncAbort(nsresult status);
// Does most the actual work.
void HandleAsyncAbort();
// AsyncCall calls a member function asynchronously (via an event).
// retval isn't refcounted and is set only when event was successfully
// posted, the event is returned for the purpose of cancelling when needed
nsresult AsyncCall(void (T::*funcPtr)(),
nsRunnableMethod<T> **retval = nsnull);
private:
T *mThis;
protected:
// Function to be called at resume time
void (T::* mCallOnResume)(void);
};
template <class T>
nsresult HttpAsyncAborter<T>::AsyncAbort(nsresult status)
{
LOG(("HttpAsyncAborter::AsyncAbort [this=%p status=%x]\n", mThis, status));
mThis->mStatus = status;
mThis->mIsPending = false;
// if this fails? Callers ignore our return value anyway....
return AsyncCall(&T::HandleAsyncAbort);
}
// Each subclass needs to define its own version of this (which just calls this
// base version), else we wind up casting base/derived member function ptrs
template <class T>
inline void HttpAsyncAborter<T>::HandleAsyncAbort()
{
NS_PRECONDITION(!mCallOnResume, "How did that happen?");
if (mThis->mSuspendCount) {
LOG(("Waiting until resume to do async notification [this=%p]\n",
mThis));
mCallOnResume = &T::HandleAsyncAbort;
return;
}
mThis->DoNotifyListener();
// finally remove ourselves from the load group.
if (mThis->mLoadGroup)
mThis->mLoadGroup->RemoveRequest(mThis, nsnull, mThis->mStatus);
}
template <class T>
nsresult HttpAsyncAborter<T>::AsyncCall(void (T::*funcPtr)(),
nsRunnableMethod<T> **retval)
{
nsresult rv;
nsRefPtr<nsRunnableMethod<T> > event = NS_NewRunnableMethod(mThis, funcPtr);
rv = NS_DispatchToCurrentThread(event);
if (NS_SUCCEEDED(rv) && retval) {
*retval = event;
}
return rv;
}
} // namespace net
} // namespace mozilla
#endif // mozilla_net_HttpBaseChannel_h
|