2007-03-22 10:30:00 -07:00
|
|
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
|
|
|
|
/* vim:set ts=4 sw=4 sts=4 et cin: */
|
2012-05-21 04:12:37 -07:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2013-06-22 09:01:06 -07:00
|
|
|
// HttpLog.h should generally be included first
|
|
|
|
#include "HttpLog.h"
|
|
|
|
|
2010-04-21 04:26:01 -07:00
|
|
|
#include "base/basictypes.h"
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
#include "nsHttpHandler.h"
|
|
|
|
#include "nsHttpTransaction.h"
|
|
|
|
#include "nsHttpRequestHead.h"
|
|
|
|
#include "nsHttpResponseHead.h"
|
|
|
|
#include "nsHttpChunkedDecoder.h"
|
|
|
|
#include "nsTransportUtils.h"
|
2007-08-23 18:45:23 -07:00
|
|
|
#include "nsNetUtil.h"
|
2013-09-22 20:35:52 -07:00
|
|
|
#include "nsCRT.h"
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
#include "nsISeekableStream.h"
|
|
|
|
#include "nsMultiplexInputStream.h"
|
|
|
|
#include "nsStringStream.h"
|
2013-03-28 10:38:04 -07:00
|
|
|
#include "mozilla/VisualEventTracer.h"
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
#include "nsComponentManagerUtils.h" // do_CreateInstance
|
|
|
|
#include "nsServiceManagerUtils.h" // do_GetService
|
|
|
|
#include "nsIHttpActivityObserver.h"
|
2012-12-05 14:10:19 -08:00
|
|
|
#include "nsSocketTransportService2.h"
|
2013-09-21 20:04:57 -07:00
|
|
|
#include "nsICancelable.h"
|
2013-09-22 20:35:52 -07:00
|
|
|
#include "nsIEventTarget.h"
|
2013-12-18 15:03:48 -08:00
|
|
|
#include "nsIHttpChannelInternal.h"
|
2013-09-22 20:35:52 -07:00
|
|
|
#include "nsIInputStream.h"
|
|
|
|
#include "nsITransport.h"
|
|
|
|
#include "nsIOService.h"
|
2013-01-15 04:22:03 -08:00
|
|
|
#include <algorithm>
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2013-06-27 20:35:46 -07:00
|
|
|
#ifdef MOZ_WIDGET_GONK
|
2014-01-14 01:55:29 -08:00
|
|
|
#include "NetStatistics.h"
|
2013-06-27 20:35:46 -07:00
|
|
|
#endif
|
2010-05-19 16:22:19 -07:00
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
#ifdef DEBUG
|
|
|
|
// defined by the socket transport service while active
|
|
|
|
extern PRThread *gSocketThread;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
static NS_DEFINE_CID(kMultiplexInputStream, NS_MULTIPLEXINPUTSTREAM_CID);
|
|
|
|
|
2011-01-27 10:34:39 -08:00
|
|
|
// Place a limit on how much non-compliant HTTP can be skipped while
|
|
|
|
// looking for a response header
|
|
|
|
#define MAX_INVALID_RESPONSE_BODY_SIZE (1024 * 128)
|
|
|
|
|
2014-01-14 01:55:29 -08:00
|
|
|
using namespace mozilla::net;
|
|
|
|
|
2014-01-07 14:05:56 -08:00
|
|
|
namespace mozilla {
|
|
|
|
namespace net {
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
// helpers
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
#if defined(PR_LOGGING)
|
|
|
|
static void
|
2012-09-05 18:31:35 -07:00
|
|
|
LogHeaders(const char *lineStart)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2012-09-01 19:35:17 -07:00
|
|
|
nsAutoCString buf;
|
2012-09-05 18:31:35 -07:00
|
|
|
char *endOfLine;
|
|
|
|
while ((endOfLine = PL_strstr(lineStart, "\r\n"))) {
|
|
|
|
buf.Assign(lineStart, endOfLine - lineStart);
|
|
|
|
if (PL_strcasestr(buf.get(), "authorization: ") ||
|
|
|
|
PL_strcasestr(buf.get(), "proxy-authorization: ")) {
|
|
|
|
char *p = PL_strchr(PL_strchr(buf.get(), ' ') + 1, ' ');
|
|
|
|
while (p && *++p)
|
|
|
|
*p = '*';
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
LOG3((" %s\n", buf.get()));
|
2012-09-05 18:31:35 -07:00
|
|
|
lineStart = endOfLine + 2;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
// nsHttpTransaction <public>
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
nsHttpTransaction::nsHttpTransaction()
|
2012-11-14 08:00:43 -08:00
|
|
|
: mCallbacksLock("transaction mCallbacks lock")
|
|
|
|
, mRequestSize(0)
|
2014-02-17 10:53:25 -08:00
|
|
|
, mConnection(nullptr)
|
|
|
|
, mConnInfo(nullptr)
|
2012-07-30 07:20:58 -07:00
|
|
|
, mRequestHead(nullptr)
|
|
|
|
, mResponseHead(nullptr)
|
2007-03-22 10:30:00 -07:00
|
|
|
, mContentLength(-1)
|
|
|
|
, mContentRead(0)
|
2011-01-27 10:34:39 -08:00
|
|
|
, mInvalidResponseBytesRead(0)
|
2012-07-30 07:20:58 -07:00
|
|
|
, mChunkedDecoder(nullptr)
|
2007-03-22 10:30:00 -07:00
|
|
|
, mStatus(NS_OK)
|
|
|
|
, mPriority(0)
|
|
|
|
, mRestartCount(0)
|
|
|
|
, mCaps(0)
|
2013-11-27 10:19:14 -08:00
|
|
|
, mCapsToClear(0)
|
2012-03-22 16:39:31 -07:00
|
|
|
, mClassification(CLASS_GENERAL)
|
2012-03-22 16:39:31 -07:00
|
|
|
, mPipelinePosition(0)
|
2012-08-16 03:37:52 -07:00
|
|
|
, mHttpVersion(NS_HTTP_VERSION_UNKNOWN)
|
2011-10-17 07:59:28 -07:00
|
|
|
, mClosed(false)
|
|
|
|
, mConnected(false)
|
|
|
|
, mHaveStatusLine(false)
|
|
|
|
, mHaveAllHeaders(false)
|
|
|
|
, mTransactionDone(false)
|
|
|
|
, mResponseIsComplete(false)
|
|
|
|
, mDidContentStart(false)
|
|
|
|
, mNoContent(false)
|
|
|
|
, mSentData(false)
|
|
|
|
, mReceivedData(false)
|
|
|
|
, mStatusEventPending(false)
|
|
|
|
, mHasRequestBody(false)
|
2012-06-22 12:06:00 -07:00
|
|
|
, mProxyConnectFailed(false)
|
2011-10-17 07:59:28 -07:00
|
|
|
, mHttpResponseMatched(false)
|
|
|
|
, mPreserveStream(false)
|
2012-12-04 15:06:29 -08:00
|
|
|
, mDispatchedAsBlocking(false)
|
2013-12-18 15:03:48 -08:00
|
|
|
, mResponseTimeoutEnabled(true)
|
2014-04-16 06:52:43 -07:00
|
|
|
, mDontRouteViaWildCard(false)
|
2012-03-22 16:39:31 -07:00
|
|
|
, mReportedStart(false)
|
|
|
|
, mReportedResponseHeader(false)
|
2012-07-30 07:20:58 -07:00
|
|
|
, mForTakeResponseHead(nullptr)
|
2012-04-05 15:37:57 -07:00
|
|
|
, mResponseHeadTaken(false)
|
2013-04-15 05:50:35 -07:00
|
|
|
, mSubmittedRatePacing(false)
|
|
|
|
, mPassedRatePacing(false)
|
|
|
|
, mSynchronousRatePaceRequest(false)
|
2013-06-27 20:35:46 -07:00
|
|
|
, mCountRecv(0)
|
|
|
|
, mCountSent(0)
|
|
|
|
, mAppId(NECKO_NO_APP_ID)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2013-10-01 08:48:47 -07:00
|
|
|
LOG(("Creating nsHttpTransaction @%p\n", this));
|
2012-04-09 07:21:17 -07:00
|
|
|
gHttpHandler->GetMaxPipelineObjectSize(&mMaxPipelineObjectSize);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
nsHttpTransaction::~nsHttpTransaction()
|
|
|
|
{
|
2013-10-01 08:48:47 -07:00
|
|
|
LOG(("Destroying nsHttpTransaction @%p\n", this));
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2013-04-15 05:50:35 -07:00
|
|
|
if (mTokenBucketCancel) {
|
|
|
|
mTokenBucketCancel->Cancel(NS_ERROR_ABORT);
|
|
|
|
mTokenBucketCancel = nullptr;
|
|
|
|
}
|
|
|
|
|
2012-11-14 08:00:44 -08:00
|
|
|
// Force the callbacks to be released right now
|
|
|
|
mCallbacks = nullptr;
|
|
|
|
|
2014-02-17 10:53:25 -08:00
|
|
|
NS_IF_RELEASE(mConnection);
|
|
|
|
NS_IF_RELEASE(mConnInfo);
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
delete mResponseHead;
|
2012-03-22 16:39:31 -07:00
|
|
|
delete mForTakeResponseHead;
|
2007-03-22 10:30:00 -07:00
|
|
|
delete mChunkedDecoder;
|
2012-12-04 15:06:29 -08:00
|
|
|
ReleaseBlockingTransaction();
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2012-03-22 16:39:31 -07:00
|
|
|
nsHttpTransaction::Classifier
|
|
|
|
nsHttpTransaction::Classify()
|
|
|
|
{
|
|
|
|
if (!(mCaps & NS_HTTP_ALLOW_PIPELINING))
|
|
|
|
return (mClassification = CLASS_SOLO);
|
|
|
|
|
|
|
|
if (mRequestHead->PeekHeader(nsHttp::If_Modified_Since) ||
|
|
|
|
mRequestHead->PeekHeader(nsHttp::If_None_Match))
|
|
|
|
return (mClassification = CLASS_REVALIDATION);
|
|
|
|
|
|
|
|
const char *accept = mRequestHead->PeekHeader(nsHttp::Accept);
|
|
|
|
if (accept && !PL_strncmp(accept, "image/", 6))
|
|
|
|
return (mClassification = CLASS_IMAGE);
|
|
|
|
|
|
|
|
if (accept && !PL_strncmp(accept, "text/css", 8))
|
|
|
|
return (mClassification = CLASS_SCRIPT);
|
|
|
|
|
|
|
|
mClassification = CLASS_GENERAL;
|
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
int32_t queryPos = mRequestHead->RequestURI().FindChar('?');
|
2012-03-22 16:39:31 -07:00
|
|
|
if (queryPos == kNotFound) {
|
|
|
|
if (StringEndsWith(mRequestHead->RequestURI(),
|
|
|
|
NS_LITERAL_CSTRING(".js")))
|
|
|
|
mClassification = CLASS_SCRIPT;
|
|
|
|
}
|
|
|
|
else if (queryPos >= 3 &&
|
|
|
|
Substring(mRequestHead->RequestURI(), queryPos - 3, 3).
|
|
|
|
EqualsLiteral(".js")) {
|
|
|
|
mClassification = CLASS_SCRIPT;
|
|
|
|
}
|
|
|
|
|
|
|
|
return mClassification;
|
|
|
|
}
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
nsresult
|
2012-11-29 17:34:35 -08:00
|
|
|
nsHttpTransaction::Init(uint32_t caps,
|
2007-03-22 10:30:00 -07:00
|
|
|
nsHttpConnectionInfo *cinfo,
|
|
|
|
nsHttpRequestHead *requestHead,
|
|
|
|
nsIInputStream *requestBody,
|
2011-09-28 23:19:26 -07:00
|
|
|
bool requestBodyHasHeaders,
|
2007-03-22 10:30:00 -07:00
|
|
|
nsIEventTarget *target,
|
|
|
|
nsIInterfaceRequestor *callbacks,
|
|
|
|
nsITransportEventSink *eventsink,
|
|
|
|
nsIAsyncInputStream **responseBody)
|
|
|
|
{
|
2013-03-28 10:38:04 -07:00
|
|
|
MOZ_EVENT_TRACER_COMPOUND_NAME(static_cast<nsAHttpTransaction*>(this),
|
|
|
|
requestHead->PeekHeader(nsHttp::Host),
|
|
|
|
requestHead->RequestURI().BeginReading());
|
|
|
|
|
|
|
|
MOZ_EVENT_TRACER_WAIT(static_cast<nsAHttpTransaction*>(this),
|
|
|
|
"net::http::transaction");
|
2007-03-22 10:30:00 -07:00
|
|
|
nsresult rv;
|
|
|
|
|
2013-06-04 16:32:31 -07:00
|
|
|
LOG(("nsHttpTransaction::Init [this=%p caps=%x]\n", this, caps));
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2013-05-16 06:30:41 -07:00
|
|
|
MOZ_ASSERT(cinfo);
|
|
|
|
MOZ_ASSERT(requestHead);
|
|
|
|
MOZ_ASSERT(target);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
mActivityDistributor = do_GetService(NS_HTTPACTIVITYDISTRIBUTOR_CONTRACTID, &rv);
|
2009-09-01 13:55:50 -07:00
|
|
|
if (NS_FAILED(rv)) return rv;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
bool activityDistributorActive;
|
2010-01-26 05:33:03 -08:00
|
|
|
rv = mActivityDistributor->GetIsActive(&activityDistributorActive);
|
|
|
|
if (NS_SUCCEEDED(rv) && activityDistributorActive) {
|
2009-09-01 13:55:50 -07:00
|
|
|
// there are some observers registered at activity distributor, gather
|
|
|
|
// nsISupports for the channel that called Init()
|
|
|
|
mChannel = do_QueryInterface(eventsink);
|
|
|
|
LOG(("nsHttpTransaction::Init() " \
|
|
|
|
"mActivityDistributor is active " \
|
2013-06-04 16:32:31 -07:00
|
|
|
"this=%p", this));
|
2010-01-26 05:33:03 -08:00
|
|
|
} else {
|
2009-09-01 13:55:50 -07:00
|
|
|
// there is no observer, so don't use it
|
2011-10-17 07:59:28 -07:00
|
|
|
activityDistributorActive = false;
|
2012-07-30 07:20:58 -07:00
|
|
|
mActivityDistributor = nullptr;
|
2010-01-26 05:33:03 -08:00
|
|
|
}
|
|
|
|
|
2013-06-27 20:35:46 -07:00
|
|
|
nsCOMPtr<nsIChannel> channel = do_QueryInterface(eventsink);
|
|
|
|
if (channel) {
|
2013-10-30 03:00:17 -07:00
|
|
|
bool isInBrowser;
|
2013-06-27 20:35:46 -07:00
|
|
|
NS_GetAppInfo(channel, &mAppId, &isInBrowser);
|
|
|
|
}
|
|
|
|
|
2013-10-30 03:00:17 -07:00
|
|
|
#ifdef MOZ_WIDGET_GONK
|
2013-06-27 20:35:46 -07:00
|
|
|
if (mAppId != NECKO_NO_APP_ID) {
|
2013-10-30 03:00:17 -07:00
|
|
|
nsCOMPtr<nsINetworkInterface> activeNetwork;
|
2014-01-14 01:55:29 -08:00
|
|
|
GetActiveNetworkInterface(activeNetwork);
|
2013-10-30 03:00:17 -07:00
|
|
|
mActiveNetwork =
|
|
|
|
new nsMainThreadPtrHolder<nsINetworkInterface>(activeNetwork);
|
2013-06-27 20:35:46 -07:00
|
|
|
}
|
2013-10-30 03:00:17 -07:00
|
|
|
#endif
|
2013-06-27 20:35:46 -07:00
|
|
|
|
2013-12-18 15:03:48 -08:00
|
|
|
nsCOMPtr<nsIHttpChannelInternal> httpChannelInternal =
|
|
|
|
do_QueryInterface(eventsink);
|
|
|
|
if (httpChannelInternal) {
|
|
|
|
rv = httpChannelInternal->GetResponseTimeoutEnabled(
|
|
|
|
&mResponseTimeoutEnabled);
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-05-16 06:30:42 -07:00
|
|
|
// create transport event sink proxy. it coalesces all events if and only
|
2010-01-26 05:33:03 -08:00
|
|
|
// if the activity observer is not active. when the observer is active
|
|
|
|
// we need not to coalesce any events to get all expected notifications
|
|
|
|
// of the transaction state, necessary for correct debugging and logging.
|
|
|
|
rv = net_NewTransportEventSinkProxy(getter_AddRefs(mTransportSink),
|
|
|
|
eventsink, target,
|
|
|
|
!activityDistributorActive);
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-02-17 10:53:25 -08:00
|
|
|
NS_ADDREF(mConnInfo = cinfo);
|
2007-03-22 10:30:00 -07:00
|
|
|
mCallbacks = callbacks;
|
|
|
|
mConsumerTarget = target;
|
|
|
|
mCaps = caps;
|
|
|
|
|
2014-03-18 09:36:18 -07:00
|
|
|
if (requestHead->IsHead()) {
|
2011-10-17 07:59:28 -07:00
|
|
|
mNoContent = true;
|
2014-03-18 09:36:18 -07:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2008-09-30 22:31:24 -07:00
|
|
|
// Make sure that there is "Content-Length: 0" header in the requestHead
|
|
|
|
// in case of POST and PUT methods when there is no requestBody and
|
|
|
|
// requestHead doesn't contain "Transfer-Encoding" header.
|
|
|
|
//
|
|
|
|
// RFC1945 section 7.2.2:
|
|
|
|
// HTTP/1.0 requests containing an entity body must include a valid
|
|
|
|
// Content-Length header field.
|
|
|
|
//
|
|
|
|
// RFC2616 section 4.4:
|
|
|
|
// For compatibility with HTTP/1.0 applications, HTTP/1.1 requests
|
|
|
|
// containing a message-body MUST include a valid Content-Length header
|
|
|
|
// field unless the server is known to be HTTP/1.1 compliant.
|
2014-03-18 09:36:18 -07:00
|
|
|
if ((requestHead->IsPost() || requestHead->IsPut()) &&
|
2008-09-30 22:31:24 -07:00
|
|
|
!requestBody && !requestHead->PeekHeader(nsHttp::Transfer_Encoding)) {
|
|
|
|
requestHead->SetHeader(nsHttp::Content_Length, NS_LITERAL_CSTRING("0"));
|
|
|
|
}
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
// grab a weak reference to the request head
|
|
|
|
mRequestHead = requestHead;
|
|
|
|
|
2013-05-16 06:30:42 -07:00
|
|
|
// make sure we eliminate any proxy specific headers from
|
2012-06-18 22:50:20 -07:00
|
|
|
// the request if we are using CONNECT
|
|
|
|
bool pruneProxyHeaders = cinfo->UsingConnect();
|
2013-05-16 06:30:42 -07:00
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
mReqHeaderBuf.Truncate();
|
|
|
|
requestHead->Flatten(mReqHeaderBuf, pruneProxyHeaders);
|
|
|
|
|
|
|
|
#if defined(PR_LOGGING)
|
|
|
|
if (LOG3_ENABLED()) {
|
|
|
|
LOG3(("http request [\n"));
|
|
|
|
LogHeaders(mReqHeaderBuf.get());
|
|
|
|
LOG3(("]\n"));
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
// If the request body does not include headers or if there is no request
|
|
|
|
// body, then we must add the header/body separator manually.
|
|
|
|
if (!requestBodyHasHeaders || !requestBody)
|
|
|
|
mReqHeaderBuf.AppendLiteral("\r\n");
|
|
|
|
|
|
|
|
// report the request header
|
|
|
|
if (mActivityDistributor)
|
|
|
|
mActivityDistributor->ObserveActivity(
|
|
|
|
mChannel,
|
|
|
|
NS_HTTP_ACTIVITY_TYPE_HTTP_TRANSACTION,
|
|
|
|
NS_HTTP_ACTIVITY_SUBTYPE_REQUEST_HEADER,
|
2012-10-25 16:25:59 -07:00
|
|
|
PR_Now(), 0,
|
2007-03-22 10:30:00 -07:00
|
|
|
mReqHeaderBuf);
|
|
|
|
|
|
|
|
// Create a string stream for the request header buf (the stream holds
|
|
|
|
// a non-owning reference to the request header data, so we MUST keep
|
|
|
|
// mReqHeaderBuf around).
|
|
|
|
nsCOMPtr<nsIInputStream> headers;
|
|
|
|
rv = NS_NewByteInputStream(getter_AddRefs(headers),
|
|
|
|
mReqHeaderBuf.get(),
|
|
|
|
mReqHeaderBuf.Length());
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
|
|
|
if (requestBody) {
|
2011-10-17 07:59:28 -07:00
|
|
|
mHasRequestBody = true;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
// wrap the headers and request body in a multiplexed input stream.
|
|
|
|
nsCOMPtr<nsIMultiplexInputStream> multi =
|
|
|
|
do_CreateInstance(kMultiplexInputStream, &rv);
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
|
|
|
rv = multi->AppendStream(headers);
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
|
|
|
rv = multi->AppendStream(requestBody);
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
2007-08-23 18:45:23 -07:00
|
|
|
// wrap the multiplexed input stream with a buffered input stream, so
|
|
|
|
// that we write data in the largest chunks possible. this is actually
|
|
|
|
// necessary to workaround some common server bugs (see bug 137155).
|
|
|
|
rv = NS_NewBufferedInputStream(getter_AddRefs(mRequestStream), multi,
|
2010-04-20 09:32:28 -07:00
|
|
|
nsIOService::gDefaultSegmentSize);
|
2007-08-23 18:45:23 -07:00
|
|
|
if (NS_FAILED(rv)) return rv;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
else
|
|
|
|
mRequestStream = headers;
|
|
|
|
|
|
|
|
rv = mRequestStream->Available(&mRequestSize);
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
|
|
|
// create pipe for response stream
|
|
|
|
rv = NS_NewPipe2(getter_AddRefs(mPipeIn),
|
|
|
|
getter_AddRefs(mPipeOut),
|
2011-10-17 07:59:28 -07:00
|
|
|
true, true,
|
2010-04-20 09:32:28 -07:00
|
|
|
nsIOService::gDefaultSegmentSize,
|
2012-01-09 19:43:52 -08:00
|
|
|
nsIOService::gDefaultSegmentCount);
|
2007-03-22 10:30:00 -07:00
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
2012-03-22 16:39:31 -07:00
|
|
|
Classify();
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
NS_ADDREF(*responseBody = mPipeIn);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2011-12-13 07:55:50 -08:00
|
|
|
nsAHttpConnection *
|
|
|
|
nsHttpTransaction::Connection()
|
|
|
|
{
|
|
|
|
return mConnection;
|
|
|
|
}
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
nsHttpResponseHead *
|
|
|
|
nsHttpTransaction::TakeResponseHead()
|
|
|
|
{
|
2013-05-16 06:30:41 -07:00
|
|
|
MOZ_ASSERT(!mResponseHeadTaken, "TakeResponseHead called 2x");
|
2012-03-22 16:39:31 -07:00
|
|
|
|
|
|
|
// Lock RestartInProgress() and TakeResponseHead() against main thread
|
|
|
|
MutexAutoLock lock(*nsHttp::GetLock());
|
|
|
|
|
2012-04-05 15:37:57 -07:00
|
|
|
mResponseHeadTaken = true;
|
2012-03-22 16:39:31 -07:00
|
|
|
|
2012-04-04 18:19:20 -07:00
|
|
|
// Prefer mForTakeResponseHead over mResponseHead. It is always a complete
|
|
|
|
// set of headers.
|
|
|
|
nsHttpResponseHead *head;
|
|
|
|
if (mForTakeResponseHead) {
|
|
|
|
head = mForTakeResponseHead;
|
2012-07-30 07:20:58 -07:00
|
|
|
mForTakeResponseHead = nullptr;
|
2012-04-04 18:19:20 -07:00
|
|
|
return head;
|
|
|
|
}
|
2013-05-16 06:30:42 -07:00
|
|
|
|
2012-03-22 16:39:31 -07:00
|
|
|
// Even in OnStartRequest() the headers won't be available if we were
|
|
|
|
// canceled
|
2007-03-22 10:30:00 -07:00
|
|
|
if (!mHaveAllHeaders) {
|
|
|
|
NS_WARNING("response headers not available or incomplete");
|
2012-07-30 07:20:58 -07:00
|
|
|
return nullptr;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2012-04-04 18:19:20 -07:00
|
|
|
head = mResponseHead;
|
2012-07-30 07:20:58 -07:00
|
|
|
mResponseHead = nullptr;
|
2007-03-22 10:30:00 -07:00
|
|
|
return head;
|
|
|
|
}
|
|
|
|
|
2011-04-08 11:37:16 -07:00
|
|
|
void
|
2012-06-22 12:06:00 -07:00
|
|
|
nsHttpTransaction::SetProxyConnectFailed()
|
2011-04-08 11:37:16 -07:00
|
|
|
{
|
2012-06-22 12:06:00 -07:00
|
|
|
mProxyConnectFailed = true;
|
2011-04-08 11:37:16 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
nsHttpRequestHead *
|
|
|
|
nsHttpTransaction::RequestHead()
|
|
|
|
{
|
|
|
|
return mRequestHead;
|
|
|
|
}
|
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t
|
2011-12-13 07:55:50 -08:00
|
|
|
nsHttpTransaction::Http1xTransactionCount()
|
|
|
|
{
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2012-02-15 13:41:18 -08:00
|
|
|
nsresult
|
|
|
|
nsHttpTransaction::TakeSubTransactions(
|
|
|
|
nsTArray<nsRefPtr<nsAHttpTransaction> > &outTransactions)
|
|
|
|
{
|
|
|
|
return NS_ERROR_NOT_IMPLEMENTED;
|
|
|
|
}
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
//----------------------------------------------------------------------------
|
|
|
|
// nsHttpTransaction::nsAHttpTransaction
|
|
|
|
//----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
void
|
|
|
|
nsHttpTransaction::SetConnection(nsAHttpConnection *conn)
|
|
|
|
{
|
2014-02-17 10:53:25 -08:00
|
|
|
NS_IF_RELEASE(mConnection);
|
|
|
|
NS_IF_ADDREF(mConnection = conn);
|
2013-03-28 10:38:04 -07:00
|
|
|
|
|
|
|
if (conn) {
|
|
|
|
MOZ_EVENT_TRACER_EXEC(static_cast<nsAHttpTransaction*>(this),
|
|
|
|
"net::http::transaction");
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2012-11-14 08:00:44 -08:00
|
|
|
nsHttpTransaction::GetSecurityCallbacks(nsIInterfaceRequestor **cb)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2012-11-14 08:00:43 -08:00
|
|
|
MutexAutoLock lock(mCallbacksLock);
|
2007-03-22 10:30:00 -07:00
|
|
|
NS_IF_ADDREF(*cb = mCallbacks);
|
|
|
|
}
|
|
|
|
|
2012-11-14 08:00:43 -08:00
|
|
|
void
|
2012-11-14 08:00:44 -08:00
|
|
|
nsHttpTransaction::SetSecurityCallbacks(nsIInterfaceRequestor* aCallbacks)
|
2012-11-14 08:00:43 -08:00
|
|
|
{
|
|
|
|
{
|
|
|
|
MutexAutoLock lock(mCallbacksLock);
|
|
|
|
mCallbacks = aCallbacks;
|
|
|
|
}
|
2012-12-05 14:10:19 -08:00
|
|
|
|
|
|
|
if (gSocketTransportService) {
|
|
|
|
nsRefPtr<UpdateSecurityCallbacks> event = new UpdateSecurityCallbacks(this, aCallbacks);
|
|
|
|
gSocketTransportService->Dispatch(event, nsIEventTarget::DISPATCH_NORMAL);
|
2012-11-14 08:00:43 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
void
|
2011-04-10 10:33:08 -07:00
|
|
|
nsHttpTransaction::OnTransportStatus(nsITransport* transport,
|
2012-08-22 08:56:38 -07:00
|
|
|
nsresult status, uint64_t progress)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2013-06-04 16:32:31 -07:00
|
|
|
LOG(("nsHttpTransaction::OnSocketStatus [this=%p status=%x progress=%llu]\n",
|
2007-03-22 10:30:00 -07:00
|
|
|
this, status, progress));
|
|
|
|
|
2011-05-21 03:03:36 -07:00
|
|
|
if (TimingEnabled()) {
|
2012-08-01 01:17:09 -07:00
|
|
|
if (status == NS_NET_STATUS_RESOLVING_HOST) {
|
2012-10-24 18:00:27 -07:00
|
|
|
mTimings.domainLookupStart = TimeStamp::Now();
|
2012-08-01 01:17:09 -07:00
|
|
|
} else if (status == NS_NET_STATUS_RESOLVED_HOST) {
|
2012-10-24 18:00:27 -07:00
|
|
|
mTimings.domainLookupEnd = TimeStamp::Now();
|
2012-08-01 01:17:09 -07:00
|
|
|
} else if (status == NS_NET_STATUS_CONNECTING_TO) {
|
2012-10-24 18:00:27 -07:00
|
|
|
mTimings.connectStart = TimeStamp::Now();
|
2012-08-01 01:17:09 -07:00
|
|
|
} else if (status == NS_NET_STATUS_CONNECTED_TO) {
|
2012-10-24 18:00:27 -07:00
|
|
|
mTimings.connectEnd = TimeStamp::Now();
|
2011-05-21 03:03:36 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
if (!mTransportSink)
|
|
|
|
return;
|
2011-04-10 10:33:08 -07:00
|
|
|
|
2013-05-16 06:30:41 -07:00
|
|
|
MOZ_ASSERT(PR_GetCurrentThread() == gSocketThread);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2009-03-05 10:47:01 -08:00
|
|
|
// Need to do this before the STATUS_RECEIVING_FROM check below, to make
|
|
|
|
// sure that the activity distributor gets told about all status events.
|
2007-03-22 10:30:00 -07:00
|
|
|
if (mActivityDistributor) {
|
|
|
|
// upon STATUS_WAITING_FOR; report request body sent
|
|
|
|
if ((mHasRequestBody) &&
|
2012-08-01 01:17:09 -07:00
|
|
|
(status == NS_NET_STATUS_WAITING_FOR))
|
2007-03-22 10:30:00 -07:00
|
|
|
mActivityDistributor->ObserveActivity(
|
|
|
|
mChannel,
|
|
|
|
NS_HTTP_ACTIVITY_TYPE_HTTP_TRANSACTION,
|
|
|
|
NS_HTTP_ACTIVITY_SUBTYPE_REQUEST_BODY_SENT,
|
2012-10-25 16:25:59 -07:00
|
|
|
PR_Now(), 0, EmptyCString());
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
// report the status and progress
|
2012-04-05 15:37:57 -07:00
|
|
|
if (!mRestartInProgressVerifier.IsDiscardingContent())
|
2012-03-22 16:39:31 -07:00
|
|
|
mActivityDistributor->ObserveActivity(
|
|
|
|
mChannel,
|
|
|
|
NS_HTTP_ACTIVITY_TYPE_SOCKET_TRANSPORT,
|
2012-08-22 08:56:38 -07:00
|
|
|
static_cast<uint32_t>(status),
|
2012-03-22 16:39:31 -07:00
|
|
|
PR_Now(),
|
|
|
|
progress,
|
|
|
|
EmptyCString());
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2009-03-05 10:47:01 -08:00
|
|
|
// nsHttpChannel synthesizes progress events in OnDataAvailable
|
2012-08-01 01:17:09 -07:00
|
|
|
if (status == NS_NET_STATUS_RECEIVING_FROM)
|
2009-03-05 10:47:01 -08:00
|
|
|
return;
|
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
uint64_t progressMax;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2012-08-01 01:17:09 -07:00
|
|
|
if (status == NS_NET_STATUS_SENDING_TO) {
|
2007-03-22 10:30:00 -07:00
|
|
|
// suppress progress when only writing request headers
|
|
|
|
if (!mHasRequestBody)
|
|
|
|
return;
|
|
|
|
|
2008-09-10 03:06:06 -07:00
|
|
|
nsCOMPtr<nsISeekableStream> seekable = do_QueryInterface(mRequestStream);
|
2013-05-16 06:30:41 -07:00
|
|
|
MOZ_ASSERT(seekable, "Request stream isn't seekable?!?");
|
2008-09-10 03:06:06 -07:00
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
int64_t prog = 0;
|
2008-09-10 03:06:06 -07:00
|
|
|
seekable->Tell(&prog);
|
|
|
|
progress = prog;
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
// when uploading, we include the request headers in the progress
|
|
|
|
// notifications.
|
|
|
|
progressMax = mRequestSize; // XXX mRequestSize is 32-bit!
|
|
|
|
}
|
|
|
|
else {
|
2012-10-25 16:25:59 -07:00
|
|
|
progress = 0;
|
2007-03-22 10:30:00 -07:00
|
|
|
progressMax = 0;
|
|
|
|
}
|
|
|
|
|
2011-04-10 10:33:08 -07:00
|
|
|
mTransportSink->OnTransportStatus(transport, status, progress, progressMax);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
bool
|
2007-03-22 10:30:00 -07:00
|
|
|
nsHttpTransaction::IsDone()
|
|
|
|
{
|
|
|
|
return mTransactionDone;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsHttpTransaction::Status()
|
|
|
|
{
|
|
|
|
return mStatus;
|
|
|
|
}
|
|
|
|
|
2012-11-29 17:34:35 -08:00
|
|
|
uint32_t
|
2012-03-22 16:39:31 -07:00
|
|
|
nsHttpTransaction::Caps()
|
2013-05-16 06:30:42 -07:00
|
|
|
{
|
2013-11-27 10:19:14 -08:00
|
|
|
return mCaps & ~mCapsToClear;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nsHttpTransaction::SetDNSWasRefreshed()
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread(), "SetDNSWasRefreshed on main thread only!");
|
|
|
|
mCapsToClear |= NS_HTTP_REFRESH_DNS;
|
2012-03-22 16:39:31 -07:00
|
|
|
}
|
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
uint64_t
|
2007-03-22 10:30:00 -07:00
|
|
|
nsHttpTransaction::Available()
|
|
|
|
{
|
2012-08-22 08:56:38 -07:00
|
|
|
uint64_t size;
|
2007-03-22 10:30:00 -07:00
|
|
|
if (NS_FAILED(mRequestStream->Available(&size)))
|
|
|
|
size = 0;
|
|
|
|
return size;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_METHOD
|
|
|
|
nsHttpTransaction::ReadRequestSegment(nsIInputStream *stream,
|
|
|
|
void *closure,
|
|
|
|
const char *buf,
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t offset,
|
|
|
|
uint32_t count,
|
|
|
|
uint32_t *countRead)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
nsHttpTransaction *trans = (nsHttpTransaction *) closure;
|
|
|
|
nsresult rv = trans->mReader->OnReadSegment(buf, count, countRead);
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
2011-05-21 03:03:36 -07:00
|
|
|
if (trans->TimingEnabled() && trans->mTimings.requestStart.IsNull()) {
|
|
|
|
// First data we're sending -> this is requestStart
|
2012-10-24 18:00:27 -07:00
|
|
|
trans->mTimings.requestStart = TimeStamp::Now();
|
2011-05-21 03:03:36 -07:00
|
|
|
}
|
2013-03-28 10:38:04 -07:00
|
|
|
|
|
|
|
if (!trans->mSentData) {
|
|
|
|
MOZ_EVENT_TRACER_MARK(static_cast<nsAHttpTransaction*>(trans),
|
|
|
|
"net::http::first-write");
|
|
|
|
}
|
|
|
|
|
2013-06-27 20:35:46 -07:00
|
|
|
trans->CountSentBytes(*countRead);
|
2011-10-17 07:59:28 -07:00
|
|
|
trans->mSentData = true;
|
2007-03-22 10:30:00 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsHttpTransaction::ReadSegments(nsAHttpSegmentReader *reader,
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t count, uint32_t *countRead)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2013-05-16 06:30:41 -07:00
|
|
|
MOZ_ASSERT(PR_GetCurrentThread() == gSocketThread);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
if (mTransactionDone) {
|
|
|
|
*countRead = 0;
|
|
|
|
return mStatus;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!mConnected) {
|
2011-10-17 07:59:28 -07:00
|
|
|
mConnected = true;
|
2007-03-22 10:30:00 -07:00
|
|
|
mConnection->GetSecurityInfo(getter_AddRefs(mSecurityInfo));
|
|
|
|
}
|
|
|
|
|
|
|
|
mReader = reader;
|
|
|
|
|
|
|
|
nsresult rv = mRequestStream->ReadSegments(ReadRequestSegment, this, count, countRead);
|
|
|
|
|
2012-07-30 07:20:58 -07:00
|
|
|
mReader = nullptr;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
// if read would block then we need to AsyncWait on the request stream.
|
|
|
|
// have callback occur on socket thread so we stay synchronized.
|
|
|
|
if (rv == NS_BASE_STREAM_WOULD_BLOCK) {
|
|
|
|
nsCOMPtr<nsIAsyncInputStream> asyncIn =
|
|
|
|
do_QueryInterface(mRequestStream);
|
|
|
|
if (asyncIn) {
|
|
|
|
nsCOMPtr<nsIEventTarget> target;
|
|
|
|
gHttpHandler->GetSocketThreadTarget(getter_AddRefs(target));
|
|
|
|
if (target)
|
|
|
|
asyncIn->AsyncWait(this, 0, 0, target);
|
|
|
|
else {
|
|
|
|
NS_ERROR("no socket thread event target");
|
|
|
|
rv = NS_ERROR_UNEXPECTED;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_METHOD
|
|
|
|
nsHttpTransaction::WritePipeSegment(nsIOutputStream *stream,
|
|
|
|
void *closure,
|
|
|
|
char *buf,
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t offset,
|
|
|
|
uint32_t count,
|
|
|
|
uint32_t *countWritten)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
nsHttpTransaction *trans = (nsHttpTransaction *) closure;
|
|
|
|
|
|
|
|
if (trans->mTransactionDone)
|
|
|
|
return NS_BASE_STREAM_CLOSED; // stop iterating
|
|
|
|
|
2011-05-21 03:03:36 -07:00
|
|
|
if (trans->TimingEnabled() && trans->mTimings.responseStart.IsNull()) {
|
2012-10-24 18:00:27 -07:00
|
|
|
trans->mTimings.responseStart = TimeStamp::Now();
|
2011-05-21 03:03:36 -07:00
|
|
|
}
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
nsresult rv;
|
|
|
|
//
|
|
|
|
// OK, now let the caller fill this segment with data.
|
|
|
|
//
|
|
|
|
rv = trans->mWriter->OnWriteSegment(buf, count, countWritten);
|
|
|
|
if (NS_FAILED(rv)) return rv; // caller didn't want to write anything
|
|
|
|
|
2013-03-28 10:38:04 -07:00
|
|
|
if (!trans->mReceivedData) {
|
|
|
|
MOZ_EVENT_TRACER_MARK(static_cast<nsAHttpTransaction*>(trans),
|
|
|
|
"net::http::first-read");
|
|
|
|
}
|
|
|
|
|
2013-05-16 06:30:41 -07:00
|
|
|
MOZ_ASSERT(*countWritten > 0, "bad writer");
|
2013-06-27 20:35:46 -07:00
|
|
|
trans->CountRecvBytes(*countWritten);
|
2011-10-17 07:59:28 -07:00
|
|
|
trans->mReceivedData = true;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2011-06-23 14:41:45 -07:00
|
|
|
// Let the transaction "play" with the buffer. It is free to modify
|
2007-03-22 10:30:00 -07:00
|
|
|
// the contents of the buffer and/or modify countWritten.
|
2011-06-23 14:41:45 -07:00
|
|
|
// - Bytes in HTTP headers don't count towards countWritten, so the input
|
|
|
|
// side of pipe (aka nsHttpChannel's mTransactionPump) won't hit
|
|
|
|
// OnInputStreamReady until all headers have been parsed.
|
2013-05-16 06:30:42 -07:00
|
|
|
//
|
2007-03-22 10:30:00 -07:00
|
|
|
rv = trans->ProcessData(buf, *countWritten, countWritten);
|
|
|
|
if (NS_FAILED(rv))
|
|
|
|
trans->Close(rv);
|
|
|
|
|
|
|
|
return rv; // failure code only stops WriteSegments; it is not propagated.
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsHttpTransaction::WriteSegments(nsAHttpSegmentWriter *writer,
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t count, uint32_t *countWritten)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2013-05-16 06:30:41 -07:00
|
|
|
MOZ_ASSERT(PR_GetCurrentThread() == gSocketThread);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
if (mTransactionDone)
|
|
|
|
return NS_SUCCEEDED(mStatus) ? NS_BASE_STREAM_CLOSED : mStatus;
|
|
|
|
|
|
|
|
mWriter = writer;
|
|
|
|
|
|
|
|
nsresult rv = mPipeOut->WriteSegments(WritePipeSegment, this, count, countWritten);
|
|
|
|
|
2012-07-30 07:20:58 -07:00
|
|
|
mWriter = nullptr;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
// if pipe would block then we need to AsyncWait on it. have callback
|
|
|
|
// occur on socket thread so we stay synchronized.
|
|
|
|
if (rv == NS_BASE_STREAM_WOULD_BLOCK) {
|
|
|
|
nsCOMPtr<nsIEventTarget> target;
|
|
|
|
gHttpHandler->GetSocketThreadTarget(getter_AddRefs(target));
|
|
|
|
if (target)
|
|
|
|
mPipeOut->AsyncWait(this, 0, 0, target);
|
|
|
|
else {
|
|
|
|
NS_ERROR("no socket thread event target");
|
|
|
|
rv = NS_ERROR_UNEXPECTED;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2013-06-27 20:35:46 -07:00
|
|
|
nsresult
|
|
|
|
nsHttpTransaction::SaveNetworkStats(bool enforce)
|
|
|
|
{
|
|
|
|
#ifdef MOZ_WIDGET_GONK
|
|
|
|
// Check if active network and appid are valid.
|
|
|
|
if (!mActiveNetwork || mAppId == NECKO_NO_APP_ID) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mCountRecv <= 0 && mCountSent <= 0) {
|
|
|
|
// There is no traffic, no need to save.
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If |enforce| is false, the traffic amount is saved
|
|
|
|
// only when the total amount exceeds the predefined
|
|
|
|
// threshold.
|
|
|
|
uint64_t totalBytes = mCountRecv + mCountSent;
|
|
|
|
if (!enforce && totalBytes < NETWORK_STATS_THRESHOLD) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create the event to save the network statistics.
|
|
|
|
// the event is then dispathed to the main thread.
|
|
|
|
nsRefPtr<nsRunnable> event =
|
|
|
|
new SaveNetworkStatsEvent(mAppId, mActiveNetwork,
|
2014-01-14 01:55:29 -08:00
|
|
|
mCountRecv, mCountSent, false);
|
2013-06-27 20:35:46 -07:00
|
|
|
NS_DispatchToMainThread(event);
|
|
|
|
|
|
|
|
// Reset the counters after saving.
|
|
|
|
mCountSent = 0;
|
|
|
|
mCountRecv = 0;
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
#else
|
|
|
|
return NS_ERROR_NOT_IMPLEMENTED;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
void
|
|
|
|
nsHttpTransaction::Close(nsresult reason)
|
|
|
|
{
|
2013-06-04 16:32:31 -07:00
|
|
|
LOG(("nsHttpTransaction::Close [this=%p reason=%x]\n", this, reason));
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2013-05-16 06:30:41 -07:00
|
|
|
MOZ_ASSERT(PR_GetCurrentThread() == gSocketThread);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
if (mClosed) {
|
|
|
|
LOG((" already closed\n"));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-04-15 05:50:35 -07:00
|
|
|
if (mTokenBucketCancel) {
|
|
|
|
mTokenBucketCancel->Cancel(reason);
|
|
|
|
mTokenBucketCancel = nullptr;
|
|
|
|
}
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
if (mActivityDistributor) {
|
|
|
|
// report the reponse is complete if not already reported
|
|
|
|
if (!mResponseIsComplete)
|
|
|
|
mActivityDistributor->ObserveActivity(
|
|
|
|
mChannel,
|
|
|
|
NS_HTTP_ACTIVITY_TYPE_HTTP_TRANSACTION,
|
|
|
|
NS_HTTP_ACTIVITY_SUBTYPE_RESPONSE_COMPLETE,
|
2009-09-01 13:55:50 -07:00
|
|
|
PR_Now(),
|
2012-08-22 08:56:38 -07:00
|
|
|
static_cast<uint64_t>(mContentRead),
|
2007-03-22 10:30:00 -07:00
|
|
|
EmptyCString());
|
|
|
|
|
|
|
|
// report that this transaction is closing
|
|
|
|
mActivityDistributor->ObserveActivity(
|
|
|
|
mChannel,
|
|
|
|
NS_HTTP_ACTIVITY_TYPE_HTTP_TRANSACTION,
|
|
|
|
NS_HTTP_ACTIVITY_SUBTYPE_TRANSACTION_CLOSE,
|
2012-10-25 16:25:59 -07:00
|
|
|
PR_Now(), 0, EmptyCString());
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2013-05-16 06:30:42 -07:00
|
|
|
// we must no longer reference the connection! find out if the
|
2007-03-22 10:30:00 -07:00
|
|
|
// connection was being reused before letting it go.
|
2011-09-28 23:19:26 -07:00
|
|
|
bool connReused = false;
|
2007-03-22 10:30:00 -07:00
|
|
|
if (mConnection)
|
|
|
|
connReused = mConnection->IsReused();
|
2011-10-17 07:59:28 -07:00
|
|
|
mConnected = false;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
//
|
|
|
|
// if the connection was reset or closed before we wrote any part of the
|
|
|
|
// request or if we wrote the request but didn't receive any part of the
|
|
|
|
// response and the connection was being reused, then we can (and really
|
|
|
|
// should) assume that we wrote to a stale connection and we must therefore
|
|
|
|
// repeat the request over a new connection.
|
|
|
|
//
|
|
|
|
// NOTE: the conditions under which we will automatically retry the HTTP
|
|
|
|
// request have to be carefully selected to avoid duplication of the
|
|
|
|
// request from the point-of-view of the server. such duplication could
|
|
|
|
// have dire consequences including repeated purchases, etc.
|
|
|
|
//
|
|
|
|
// NOTE: because of the way SSL proxy CONNECT is implemented, it is
|
|
|
|
// possible that the transaction may have received data without having
|
|
|
|
// sent any data. for this reason, mSendData == FALSE does not imply
|
|
|
|
// mReceivedData == FALSE. (see bug 203057 for more info.)
|
|
|
|
//
|
|
|
|
if (reason == NS_ERROR_NET_RESET || reason == NS_OK) {
|
2012-05-14 10:25:28 -07:00
|
|
|
|
|
|
|
// reallySentData is meant to separate the instances where data has
|
|
|
|
// been sent by this transaction but buffered at a higher level while
|
|
|
|
// a TLS session (perhaps via a tunnel) is setup.
|
|
|
|
bool reallySentData =
|
|
|
|
mSentData && (!mConnection || mConnection->BytesWritten());
|
2013-05-16 06:30:42 -07:00
|
|
|
|
2012-05-14 10:25:28 -07:00
|
|
|
if (!mReceivedData &&
|
|
|
|
(!reallySentData || connReused || mPipelinePosition)) {
|
2007-03-22 10:30:00 -07:00
|
|
|
// if restarting fails, then we must proceed to close the pipe,
|
|
|
|
// which will notify the channel that the transaction failed.
|
2013-05-16 06:30:42 -07:00
|
|
|
|
2012-03-22 16:39:31 -07:00
|
|
|
if (mPipelinePosition) {
|
|
|
|
gHttpHandler->ConnMgr()->PipelineFeedbackInfo(
|
|
|
|
mConnInfo, nsHttpConnectionMgr::RedCanceledPipeline,
|
2012-07-30 07:20:58 -07:00
|
|
|
nullptr, 0);
|
2012-03-22 16:39:31 -07:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
if (NS_SUCCEEDED(Restart()))
|
|
|
|
return;
|
|
|
|
}
|
2012-03-22 16:39:31 -07:00
|
|
|
else if (!mResponseIsComplete && mPipelinePosition &&
|
|
|
|
reason == NS_ERROR_NET_RESET) {
|
|
|
|
// due to unhandled rst on a pipeline - safe to
|
|
|
|
// restart as only idempotent is found there
|
|
|
|
|
|
|
|
gHttpHandler->ConnMgr()->PipelineFeedbackInfo(
|
2012-07-30 07:20:58 -07:00
|
|
|
mConnInfo, nsHttpConnectionMgr::RedCorruptedContent, nullptr, 0);
|
2012-03-22 16:39:31 -07:00
|
|
|
if (NS_SUCCEEDED(RestartInProgress()))
|
|
|
|
return;
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2014-06-08 15:15:00 -07:00
|
|
|
if ((mChunkedDecoder || (mContentLength >= int64_t(0))) &&
|
|
|
|
(mHttpVersion >= NS_HTTP_VERSION_1_1)) {
|
|
|
|
|
|
|
|
if (NS_SUCCEEDED(reason) && !mResponseIsComplete) {
|
|
|
|
reason = NS_ERROR_NET_PARTIAL_TRANSFER;
|
|
|
|
LOG(("Partial transfer, incomplete HTTP responese received: %s",
|
|
|
|
mChunkedDecoder ? "broken chunk" : "c-l underrun"));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
bool relConn = true;
|
2007-03-22 10:30:00 -07:00
|
|
|
if (NS_SUCCEEDED(reason)) {
|
2012-03-22 16:39:31 -07:00
|
|
|
if (!mResponseIsComplete) {
|
|
|
|
// The response has not been delimited with a high-confidence
|
|
|
|
// algorithm like Content-Length or Chunked Encoding. We
|
|
|
|
// need to use a strong framing mechanism to pipeline.
|
|
|
|
gHttpHandler->ConnMgr()->PipelineFeedbackInfo(
|
|
|
|
mConnInfo, nsHttpConnectionMgr::BadInsufficientFraming,
|
2012-07-30 07:20:58 -07:00
|
|
|
nullptr, mClassification);
|
2012-03-22 16:39:31 -07:00
|
|
|
}
|
|
|
|
else if (mPipelinePosition) {
|
|
|
|
// report this success as feedback
|
|
|
|
gHttpHandler->ConnMgr()->PipelineFeedbackInfo(
|
|
|
|
mConnInfo, nsHttpConnectionMgr::GoodCompletedOK,
|
2012-07-30 07:20:58 -07:00
|
|
|
nullptr, mPipelinePosition);
|
2012-03-22 16:39:31 -07:00
|
|
|
}
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
// the server has not sent the final \r\n terminating the header
|
|
|
|
// section, and there may still be a header line unparsed. let's make
|
|
|
|
// sure we parse the remaining header line, and then hopefully, the
|
2011-09-03 05:42:06 -07:00
|
|
|
// response will be usable (see bug 88792).
|
2007-03-22 10:30:00 -07:00
|
|
|
if (!mHaveAllHeaders) {
|
|
|
|
char data = '\n';
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t unused;
|
2007-03-22 10:30:00 -07:00
|
|
|
ParseHead(&data, 1, &unused);
|
2011-09-03 05:42:06 -07:00
|
|
|
|
|
|
|
if (mResponseHead->Version() == NS_HTTP_VERSION_0_9) {
|
|
|
|
// Reject 0 byte HTTP/0.9 Responses - bug 423506
|
|
|
|
LOG(("nsHttpTransaction::Close %p 0 Byte 0.9 Response", this));
|
|
|
|
reason = NS_ERROR_NET_RESET;
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// honor the sticky connection flag...
|
|
|
|
if (mCaps & NS_HTTP_STICKY_CONNECTION)
|
2011-10-17 07:59:28 -07:00
|
|
|
relConn = false;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
2012-06-26 07:47:46 -07:00
|
|
|
|
|
|
|
// mTimings.responseEnd is normally recorded based on the end of a
|
|
|
|
// HTTP delimiter such as chunked-encodings or content-length. However,
|
|
|
|
// EOF or an error still require an end time be recorded.
|
|
|
|
if (TimingEnabled() &&
|
|
|
|
mTimings.responseEnd.IsNull() && !mTimings.responseStart.IsNull())
|
2012-10-24 18:00:27 -07:00
|
|
|
mTimings.responseEnd = TimeStamp::Now();
|
2012-06-26 07:47:46 -07:00
|
|
|
|
2014-02-17 10:53:25 -08:00
|
|
|
if (relConn && mConnection)
|
|
|
|
NS_RELEASE(mConnection);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2013-06-27 20:35:46 -07:00
|
|
|
// save network statistics in the end of transaction
|
|
|
|
SaveNetworkStats(true);
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
mStatus = reason;
|
2011-10-17 07:59:28 -07:00
|
|
|
mTransactionDone = true; // forcibly flag the transaction as complete
|
|
|
|
mClosed = true;
|
2012-12-04 15:06:29 -08:00
|
|
|
ReleaseBlockingTransaction();
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
// release some resources that we no longer need
|
2012-07-30 07:20:58 -07:00
|
|
|
mRequestStream = nullptr;
|
2007-03-22 10:30:00 -07:00
|
|
|
mReqHeaderBuf.Truncate();
|
|
|
|
mLineBuf.Truncate();
|
|
|
|
if (mChunkedDecoder) {
|
|
|
|
delete mChunkedDecoder;
|
2012-07-30 07:20:58 -07:00
|
|
|
mChunkedDecoder = nullptr;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// closing this pipe triggers the channel's OnStopRequest method.
|
|
|
|
mPipeOut->CloseWithStatus(reason);
|
2013-03-28 10:38:04 -07:00
|
|
|
|
|
|
|
MOZ_EVENT_TRACER_DONE(static_cast<nsAHttpTransaction*>(this),
|
|
|
|
"net::http::transaction");
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2014-04-20 09:01:35 -07:00
|
|
|
nsHttpConnectionInfo *
|
|
|
|
nsHttpTransaction::ConnectionInfo()
|
|
|
|
{
|
|
|
|
return mConnInfo;
|
|
|
|
}
|
|
|
|
|
2012-03-22 16:39:31 -07:00
|
|
|
nsresult
|
|
|
|
nsHttpTransaction::AddTransaction(nsAHttpTransaction *trans)
|
|
|
|
{
|
|
|
|
return NS_ERROR_NOT_IMPLEMENTED;
|
|
|
|
}
|
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t
|
2012-03-22 16:39:31 -07:00
|
|
|
nsHttpTransaction::PipelineDepth()
|
2012-03-22 16:39:31 -07:00
|
|
|
{
|
2012-03-22 16:39:31 -07:00
|
|
|
return IsDone() ? 0 : 1;
|
2012-03-22 16:39:31 -07:00
|
|
|
}
|
|
|
|
|
2012-03-22 16:39:31 -07:00
|
|
|
nsresult
|
2012-08-22 08:56:38 -07:00
|
|
|
nsHttpTransaction::SetPipelinePosition(int32_t position)
|
2012-03-22 16:39:31 -07:00
|
|
|
{
|
|
|
|
mPipelinePosition = position;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2013-05-16 06:30:42 -07:00
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
int32_t
|
2012-03-22 16:39:31 -07:00
|
|
|
nsHttpTransaction::PipelinePosition()
|
|
|
|
{
|
|
|
|
return mPipelinePosition;
|
|
|
|
}
|
|
|
|
|
2014-02-07 14:30:36 -08:00
|
|
|
bool // NOTE BASE CLASS
|
|
|
|
nsAHttpTransaction::ResponseTimeoutEnabled() const
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
PRIntervalTime // NOTE BASE CLASS
|
|
|
|
nsAHttpTransaction::ResponseTimeout()
|
|
|
|
{
|
|
|
|
return gHttpHandler->ResponseTimeout();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
nsHttpTransaction::ResponseTimeoutEnabled() const
|
|
|
|
{
|
|
|
|
return mResponseTimeoutEnabled;
|
|
|
|
}
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
// nsHttpTransaction <private>
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
2012-03-22 16:39:31 -07:00
|
|
|
nsresult
|
|
|
|
nsHttpTransaction::RestartInProgress()
|
|
|
|
{
|
2013-05-16 06:30:41 -07:00
|
|
|
MOZ_ASSERT(PR_GetCurrentThread() == gSocketThread);
|
2013-05-16 06:30:42 -07:00
|
|
|
|
2012-04-05 15:37:57 -07:00
|
|
|
if ((mRestartCount + 1) >= gHttpHandler->MaxRequestAttempts()) {
|
|
|
|
LOG(("nsHttpTransaction::RestartInProgress() "
|
|
|
|
"reached max request attempts, failing transaction %p\n", this));
|
|
|
|
return NS_ERROR_NET_RESET;
|
|
|
|
}
|
|
|
|
|
2012-03-22 16:39:31 -07:00
|
|
|
// Lock RestartInProgress() and TakeResponseHead() against main thread
|
|
|
|
MutexAutoLock lock(*nsHttp::GetLock());
|
|
|
|
|
2012-04-05 15:37:57 -07:00
|
|
|
// Don't try and RestartInProgress() things that haven't gotten a response
|
|
|
|
// header yet. Those should be handled under the normal restart() path if
|
|
|
|
// they are eligible.
|
|
|
|
if (!mHaveAllHeaders)
|
|
|
|
return NS_ERROR_NET_RESET;
|
|
|
|
|
2012-04-04 18:19:20 -07:00
|
|
|
// don't try and restart 0.9 or non 200/Get HTTP/1
|
2012-04-05 15:37:57 -07:00
|
|
|
if (!mRestartInProgressVerifier.IsSetup())
|
2012-03-22 16:39:31 -07:00
|
|
|
return NS_ERROR_NET_RESET;
|
|
|
|
|
|
|
|
LOG(("Will restart transaction %p and skip first %lld bytes, "
|
|
|
|
"old Content-Length %lld",
|
|
|
|
this, mContentRead, mContentLength));
|
|
|
|
|
2012-04-05 15:37:57 -07:00
|
|
|
mRestartInProgressVerifier.SetAlreadyProcessed(
|
2013-01-15 04:22:03 -08:00
|
|
|
std::max(mRestartInProgressVerifier.AlreadyProcessed(), mContentRead));
|
2012-04-05 15:37:57 -07:00
|
|
|
|
|
|
|
if (!mResponseHeadTaken && !mForTakeResponseHead) {
|
|
|
|
// TakeResponseHeader() has not been called yet and this
|
|
|
|
// is the first restart. Store the resp headers exclusively
|
|
|
|
// for TakeResponseHead() which is called from the main thread and
|
|
|
|
// could happen at any time - so we can't continue to modify those
|
|
|
|
// headers (which restarting will effectively do)
|
|
|
|
mForTakeResponseHead = mResponseHead;
|
2012-07-30 07:20:58 -07:00
|
|
|
mResponseHead = nullptr;
|
2012-03-22 16:39:31 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
if (mResponseHead) {
|
|
|
|
mResponseHead->Reset();
|
|
|
|
}
|
|
|
|
|
|
|
|
mContentRead = 0;
|
|
|
|
mContentLength = -1;
|
|
|
|
delete mChunkedDecoder;
|
2012-07-30 07:20:58 -07:00
|
|
|
mChunkedDecoder = nullptr;
|
2012-03-22 16:39:31 -07:00
|
|
|
mHaveStatusLine = false;
|
|
|
|
mHaveAllHeaders = false;
|
|
|
|
mHttpResponseMatched = false;
|
|
|
|
mResponseIsComplete = false;
|
|
|
|
mDidContentStart = false;
|
|
|
|
mNoContent = false;
|
|
|
|
mSentData = false;
|
|
|
|
mReceivedData = false;
|
|
|
|
|
|
|
|
return Restart();
|
|
|
|
}
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
nsresult
|
|
|
|
nsHttpTransaction::Restart()
|
|
|
|
{
|
2013-05-16 06:30:41 -07:00
|
|
|
MOZ_ASSERT(PR_GetCurrentThread() == gSocketThread);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
// limit the number of restart attempts - bug 92224
|
|
|
|
if (++mRestartCount >= gHttpHandler->MaxRequestAttempts()) {
|
2013-10-01 08:48:47 -07:00
|
|
|
LOG(("reached max request attempts, failing transaction @%p\n", this));
|
2007-03-22 10:30:00 -07:00
|
|
|
return NS_ERROR_NET_RESET;
|
|
|
|
}
|
|
|
|
|
2013-10-01 08:48:47 -07:00
|
|
|
LOG(("restarting transaction @%p\n", this));
|
2014-04-16 06:52:43 -07:00
|
|
|
SetDontRouteViaWildCard(false);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
// rewind streams in case we already wrote out the request
|
|
|
|
nsCOMPtr<nsISeekableStream> seekable = do_QueryInterface(mRequestStream);
|
|
|
|
if (seekable)
|
|
|
|
seekable->Seek(nsISeekableStream::NS_SEEK_SET, 0);
|
|
|
|
|
|
|
|
// clear old connection state...
|
|
|
|
mSecurityInfo = 0;
|
2014-04-30 10:15:46 -07:00
|
|
|
if (mConnection) {
|
|
|
|
mConnection->DontReuse();
|
|
|
|
NS_RELEASE(mConnection);
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
// disable pipelining for the next attempt in case pipelining caused the
|
|
|
|
// reset. this is being overly cautious since we don't know if pipelining
|
|
|
|
// was the problem here.
|
|
|
|
mCaps &= ~NS_HTTP_ALLOW_PIPELINING;
|
2012-06-29 11:24:29 -07:00
|
|
|
SetPipelinePosition(0);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
return gHttpHandler->InitiateTransaction(this, mPriority);
|
|
|
|
}
|
|
|
|
|
2010-11-30 16:02:52 -08:00
|
|
|
char *
|
2012-08-22 08:56:38 -07:00
|
|
|
nsHttpTransaction::LocateHttpStart(char *buf, uint32_t len,
|
2011-09-28 23:19:26 -07:00
|
|
|
bool aAllowPartialMatch)
|
2010-11-30 16:02:52 -08:00
|
|
|
{
|
2013-05-16 06:30:41 -07:00
|
|
|
MOZ_ASSERT(!aAllowPartialMatch || mLineBuf.IsEmpty());
|
2010-11-30 16:02:52 -08:00
|
|
|
|
|
|
|
static const char HTTPHeader[] = "HTTP/1.";
|
2012-08-22 08:56:38 -07:00
|
|
|
static const uint32_t HTTPHeaderLen = sizeof(HTTPHeader) - 1;
|
2011-02-22 18:02:19 -08:00
|
|
|
static const char HTTP2Header[] = "HTTP/2.0";
|
2012-08-22 08:56:38 -07:00
|
|
|
static const uint32_t HTTP2HeaderLen = sizeof(HTTP2Header) - 1;
|
2013-05-16 12:22:06 -07:00
|
|
|
// ShoutCast ICY is treated as HTTP/1.0
|
|
|
|
static const char ICYHeader[] = "ICY ";
|
|
|
|
static const uint32_t ICYHeaderLen = sizeof(ICYHeader) - 1;
|
2013-05-16 06:30:42 -07:00
|
|
|
|
2011-02-08 15:26:33 -08:00
|
|
|
if (aAllowPartialMatch && (len < HTTPHeaderLen))
|
2012-07-30 07:20:58 -07:00
|
|
|
return (PL_strncasecmp(buf, HTTPHeader, len) == 0) ? buf : nullptr;
|
2010-11-30 16:02:52 -08:00
|
|
|
|
|
|
|
// mLineBuf can contain partial match from previous search
|
|
|
|
if (!mLineBuf.IsEmpty()) {
|
2013-05-16 06:30:41 -07:00
|
|
|
MOZ_ASSERT(mLineBuf.Length() < HTTPHeaderLen);
|
2013-01-15 04:22:03 -08:00
|
|
|
int32_t checkChars = std::min(len, HTTPHeaderLen - mLineBuf.Length());
|
2010-11-30 16:02:52 -08:00
|
|
|
if (PL_strncasecmp(buf, HTTPHeader + mLineBuf.Length(),
|
|
|
|
checkChars) == 0) {
|
|
|
|
mLineBuf.Append(buf, checkChars);
|
|
|
|
if (mLineBuf.Length() == HTTPHeaderLen) {
|
|
|
|
// We've found whole HTTPHeader sequence. Return pointer at the
|
|
|
|
// end of matched sequence since it is stored in mLineBuf.
|
|
|
|
return (buf + checkChars);
|
|
|
|
}
|
2011-03-23 20:31:28 -07:00
|
|
|
// Response matches pattern but is still incomplete.
|
|
|
|
return 0;
|
2010-11-30 16:02:52 -08:00
|
|
|
}
|
|
|
|
// Previous partial match together with new data doesn't match the
|
|
|
|
// pattern. Start the search again.
|
|
|
|
mLineBuf.Truncate();
|
|
|
|
}
|
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
bool firstByte = true;
|
2010-11-30 16:02:52 -08:00
|
|
|
while (len > 0) {
|
2013-01-15 04:22:03 -08:00
|
|
|
if (PL_strncasecmp(buf, HTTPHeader, std::min<uint32_t>(len, HTTPHeaderLen)) == 0) {
|
2010-11-30 16:02:52 -08:00
|
|
|
if (len < HTTPHeaderLen) {
|
|
|
|
// partial HTTPHeader sequence found
|
2011-02-08 15:26:33 -08:00
|
|
|
// save partial match to mLineBuf
|
|
|
|
mLineBuf.Assign(buf, len);
|
|
|
|
return 0;
|
2010-11-30 16:02:52 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
// whole HTTPHeader sequence found
|
|
|
|
return buf;
|
|
|
|
}
|
2011-02-22 18:02:19 -08:00
|
|
|
|
|
|
|
// At least "SmarterTools/2.0.3974.16813" generates nonsensical
|
|
|
|
// HTTP/2.0 responses to our HTTP/1 requests. Treat the minimal case of
|
|
|
|
// it as HTTP/1.1 to be compatible with old versions of ourselves and
|
|
|
|
// other browsers
|
|
|
|
|
|
|
|
if (firstByte && !mInvalidResponseBytesRead && len >= HTTP2HeaderLen &&
|
|
|
|
(PL_strncasecmp(buf, HTTP2Header, HTTP2HeaderLen) == 0)) {
|
|
|
|
LOG(("nsHttpTransaction:: Identified HTTP/2.0 treating as 1.x\n"));
|
|
|
|
return buf;
|
|
|
|
}
|
|
|
|
|
2013-05-16 12:22:06 -07:00
|
|
|
// Treat ICY (AOL/Nullsoft ShoutCast) non-standard header in same fashion
|
|
|
|
// as HTTP/2.0 is treated above. This will allow "ICY " to be interpretted
|
|
|
|
// as HTTP/1.0 in nsHttpResponseHead::ParseVersion
|
|
|
|
|
|
|
|
if (firstByte && !mInvalidResponseBytesRead && len >= ICYHeaderLen &&
|
|
|
|
(PL_strncasecmp(buf, ICYHeader, ICYHeaderLen) == 0)) {
|
|
|
|
LOG(("nsHttpTransaction:: Identified ICY treating as HTTP/1.0\n"));
|
|
|
|
return buf;
|
|
|
|
}
|
|
|
|
|
2011-02-22 18:02:19 -08:00
|
|
|
if (!nsCRT::IsAsciiSpace(*buf))
|
2011-10-17 07:59:28 -07:00
|
|
|
firstByte = false;
|
2010-11-30 16:02:52 -08:00
|
|
|
buf++;
|
|
|
|
len--;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2011-05-31 16:51:51 -07:00
|
|
|
nsresult
|
2007-03-22 10:30:00 -07:00
|
|
|
nsHttpTransaction::ParseLine(char *line)
|
|
|
|
{
|
|
|
|
LOG(("nsHttpTransaction::ParseLine [%s]\n", line));
|
2011-05-31 16:51:51 -07:00
|
|
|
nsresult rv = NS_OK;
|
2013-05-16 06:30:42 -07:00
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
if (!mHaveStatusLine) {
|
|
|
|
mResponseHead->ParseStatusLine(line);
|
2011-10-17 07:59:28 -07:00
|
|
|
mHaveStatusLine = true;
|
2007-03-22 10:30:00 -07:00
|
|
|
// XXX this should probably never happen
|
|
|
|
if (mResponseHead->Version() == NS_HTTP_VERSION_0_9)
|
2011-10-17 07:59:28 -07:00
|
|
|
mHaveAllHeaders = true;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
2011-05-31 16:51:51 -07:00
|
|
|
else {
|
|
|
|
rv = mResponseHead->ParseHeaderLine(line);
|
|
|
|
}
|
|
|
|
return rv;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2012-08-22 08:56:38 -07:00
|
|
|
nsHttpTransaction::ParseLineSegment(char *segment, uint32_t len)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
NS_PRECONDITION(!mHaveAllHeaders, "already have all headers");
|
|
|
|
|
|
|
|
if (!mLineBuf.IsEmpty() && mLineBuf.Last() == '\n') {
|
|
|
|
// trim off the new line char, and if this segment is
|
|
|
|
// not a continuation of the previous or if we haven't
|
|
|
|
// parsed the status line yet, then parse the contents
|
|
|
|
// of mLineBuf.
|
|
|
|
mLineBuf.Truncate(mLineBuf.Length() - 1);
|
|
|
|
if (!mHaveStatusLine || (*segment != ' ' && *segment != '\t')) {
|
2011-05-31 16:51:51 -07:00
|
|
|
nsresult rv = ParseLine(mLineBuf.BeginWriting());
|
2007-03-22 10:30:00 -07:00
|
|
|
mLineBuf.Truncate();
|
2011-05-31 16:51:51 -07:00
|
|
|
if (NS_FAILED(rv)) {
|
2012-03-22 16:39:31 -07:00
|
|
|
gHttpHandler->ConnMgr()->PipelineFeedbackInfo(
|
|
|
|
mConnInfo, nsHttpConnectionMgr::RedCorruptedContent,
|
2012-07-30 07:20:58 -07:00
|
|
|
nullptr, 0);
|
2011-05-31 16:51:51 -07:00
|
|
|
return rv;
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// append segment to mLineBuf...
|
|
|
|
mLineBuf.Append(segment, len);
|
2013-05-16 06:30:42 -07:00
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
// a line buf with only a new line char signifies the end of headers.
|
|
|
|
if (mLineBuf.First() == '\n') {
|
|
|
|
mLineBuf.Truncate();
|
|
|
|
// discard this response if it is a 100 continue or other 1xx status.
|
2012-08-22 08:56:38 -07:00
|
|
|
uint16_t status = mResponseHead->Status();
|
2011-05-19 16:43:37 -07:00
|
|
|
if ((status != 101) && (status / 100 == 1)) {
|
2007-03-22 10:30:00 -07:00
|
|
|
LOG(("ignoring 1xx response\n"));
|
2011-10-17 07:59:28 -07:00
|
|
|
mHaveStatusLine = false;
|
|
|
|
mHttpResponseMatched = false;
|
|
|
|
mConnection->SetLastTransactionExpectedNoContent(true);
|
2007-03-22 10:30:00 -07:00
|
|
|
mResponseHead->Reset();
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2011-10-17 07:59:28 -07:00
|
|
|
mHaveAllHeaders = true;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsHttpTransaction::ParseHead(char *buf,
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t count,
|
|
|
|
uint32_t *countRead)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
nsresult rv;
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t len;
|
2007-03-22 10:30:00 -07:00
|
|
|
char *eol;
|
|
|
|
|
|
|
|
LOG(("nsHttpTransaction::ParseHead [count=%u]\n", count));
|
|
|
|
|
|
|
|
*countRead = 0;
|
|
|
|
|
|
|
|
NS_PRECONDITION(!mHaveAllHeaders, "oops");
|
2013-05-16 06:30:42 -07:00
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
// allocate the response head object if necessary
|
|
|
|
if (!mResponseHead) {
|
|
|
|
mResponseHead = new nsHttpResponseHead();
|
|
|
|
if (!mResponseHead)
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
|
|
|
|
// report that we have a least some of the response
|
2012-03-22 16:39:31 -07:00
|
|
|
if (mActivityDistributor && !mReportedStart) {
|
|
|
|
mReportedStart = true;
|
2007-03-22 10:30:00 -07:00
|
|
|
mActivityDistributor->ObserveActivity(
|
|
|
|
mChannel,
|
|
|
|
NS_HTTP_ACTIVITY_TYPE_HTTP_TRANSACTION,
|
|
|
|
NS_HTTP_ACTIVITY_SUBTYPE_RESPONSE_START,
|
2012-10-25 16:25:59 -07:00
|
|
|
PR_Now(), 0, EmptyCString());
|
2012-03-22 16:39:31 -07:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2010-11-30 16:02:52 -08:00
|
|
|
if (!mHttpResponseMatched) {
|
2011-01-27 10:34:39 -08:00
|
|
|
// Normally we insist on seeing HTTP/1.x in the first few bytes,
|
|
|
|
// but if we are on a persistent connection and the previous transaction
|
|
|
|
// was not supposed to have any content then we need to be prepared
|
|
|
|
// to skip over a response body that the server may have sent even
|
|
|
|
// though it wasn't allowed.
|
|
|
|
if (!mConnection || !mConnection->LastTransactionExpectedNoContent()) {
|
|
|
|
// tolerate only minor junk before the status line
|
2011-10-17 07:59:28 -07:00
|
|
|
mHttpResponseMatched = true;
|
2013-01-15 04:22:03 -08:00
|
|
|
char *p = LocateHttpStart(buf, std::min<uint32_t>(count, 11), true);
|
2010-11-30 16:02:52 -08:00
|
|
|
if (!p) {
|
|
|
|
// Treat any 0.9 style response of a put as a failure.
|
2014-03-18 09:36:18 -07:00
|
|
|
if (mRequestHead->IsPut())
|
2010-11-30 16:02:52 -08:00
|
|
|
return NS_ERROR_ABORT;
|
|
|
|
|
|
|
|
mResponseHead->ParseStatusLine("");
|
2011-10-17 07:59:28 -07:00
|
|
|
mHaveStatusLine = true;
|
|
|
|
mHaveAllHeaders = true;
|
2010-11-30 16:02:52 -08:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
if (p > buf) {
|
|
|
|
// skip over the junk
|
2011-01-27 10:34:39 -08:00
|
|
|
mInvalidResponseBytesRead += p - buf;
|
2010-11-30 16:02:52 -08:00
|
|
|
*countRead = p - buf;
|
|
|
|
buf = p;
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
2010-11-30 16:02:52 -08:00
|
|
|
else {
|
2011-10-17 07:59:28 -07:00
|
|
|
char *p = LocateHttpStart(buf, count, false);
|
2010-11-30 16:02:52 -08:00
|
|
|
if (p) {
|
2011-01-27 10:34:39 -08:00
|
|
|
mInvalidResponseBytesRead += p - buf;
|
2010-11-30 16:02:52 -08:00
|
|
|
*countRead = p - buf;
|
|
|
|
buf = p;
|
2011-10-17 07:59:28 -07:00
|
|
|
mHttpResponseMatched = true;
|
2010-11-30 16:02:52 -08:00
|
|
|
} else {
|
2011-01-27 10:34:39 -08:00
|
|
|
mInvalidResponseBytesRead += count;
|
2010-11-30 16:02:52 -08:00
|
|
|
*countRead = count;
|
2011-01-27 10:34:39 -08:00
|
|
|
if (mInvalidResponseBytesRead > MAX_INVALID_RESPONSE_BODY_SIZE) {
|
|
|
|
LOG(("nsHttpTransaction::ParseHead() "
|
|
|
|
"Cannot find Response Header\n"));
|
|
|
|
// cannot go back and call this 0.9 anymore as we
|
|
|
|
// have thrown away a lot of the leading junk
|
|
|
|
return NS_ERROR_ABORT;
|
|
|
|
}
|
2010-11-30 16:02:52 -08:00
|
|
|
return NS_OK;
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
// otherwise we can assume that we don't have a HTTP/0.9 response.
|
|
|
|
|
2013-05-16 06:30:41 -07:00
|
|
|
MOZ_ASSERT (mHttpResponseMatched);
|
2012-07-30 07:20:58 -07:00
|
|
|
while ((eol = static_cast<char *>(memchr(buf, '\n', count - *countRead))) != nullptr) {
|
2007-03-22 10:30:00 -07:00
|
|
|
// found line in range [buf:eol]
|
|
|
|
len = eol - buf + 1;
|
|
|
|
|
|
|
|
*countRead += len;
|
|
|
|
|
|
|
|
// actually, the line is in the range [buf:eol-1]
|
|
|
|
if ((eol > buf) && (*(eol-1) == '\r'))
|
|
|
|
len--;
|
|
|
|
|
|
|
|
buf[len-1] = '\n';
|
|
|
|
rv = ParseLineSegment(buf, len);
|
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return rv;
|
|
|
|
|
|
|
|
if (mHaveAllHeaders)
|
|
|
|
return NS_OK;
|
|
|
|
|
|
|
|
// skip over line
|
|
|
|
buf = eol + 1;
|
2011-03-23 07:59:58 -07:00
|
|
|
|
|
|
|
if (!mHttpResponseMatched) {
|
|
|
|
// a 100 class response has caused us to throw away that set of
|
|
|
|
// response headers and look for the next response
|
|
|
|
return NS_ERROR_NET_INTERRUPT;
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// do something about a partial header line
|
|
|
|
if (!mHaveAllHeaders && (len = count - *countRead)) {
|
|
|
|
*countRead = count;
|
|
|
|
// ignore a trailing carriage return, and don't bother calling
|
|
|
|
// ParseLineSegment if buf only contains a carriage return.
|
|
|
|
if ((buf[len-1] == '\r') && (--len == 0))
|
|
|
|
return NS_OK;
|
|
|
|
rv = ParseLineSegment(buf, len);
|
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
// called on the socket thread
|
|
|
|
nsresult
|
|
|
|
nsHttpTransaction::HandleContentStart()
|
|
|
|
{
|
2013-06-04 16:32:31 -07:00
|
|
|
LOG(("nsHttpTransaction::HandleContentStart [this=%p]\n", this));
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
if (mResponseHead) {
|
|
|
|
#if defined(PR_LOGGING)
|
|
|
|
if (LOG3_ENABLED()) {
|
|
|
|
LOG3(("http response [\n"));
|
2012-09-01 19:35:17 -07:00
|
|
|
nsAutoCString headers;
|
2011-10-17 07:59:28 -07:00
|
|
|
mResponseHead->Flatten(headers, false);
|
2007-03-22 10:30:00 -07:00
|
|
|
LogHeaders(headers.get());
|
|
|
|
LOG3(("]\n"));
|
|
|
|
}
|
|
|
|
#endif
|
2012-08-16 03:37:52 -07:00
|
|
|
// Save http version, mResponseHead isn't available anymore after
|
|
|
|
// TakeResponseHead() is called
|
|
|
|
mHttpVersion = mResponseHead->Version();
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
// notify the connection, give it a chance to cause a reset.
|
2011-09-28 23:19:26 -07:00
|
|
|
bool reset = false;
|
2012-03-22 16:39:31 -07:00
|
|
|
if (!mRestartInProgressVerifier.IsSetup())
|
|
|
|
mConnection->OnHeadersAvailable(this, mRequestHead, mResponseHead, &reset);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
// looks like we should ignore this response, resetting...
|
|
|
|
if (reset) {
|
|
|
|
LOG(("resetting transaction's response head\n"));
|
2011-10-17 07:59:28 -07:00
|
|
|
mHaveAllHeaders = false;
|
|
|
|
mHaveStatusLine = false;
|
|
|
|
mReceivedData = false;
|
|
|
|
mSentData = false;
|
|
|
|
mHttpResponseMatched = false;
|
2007-03-22 10:30:00 -07:00
|
|
|
mResponseHead->Reset();
|
|
|
|
// wait to be called again...
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
// check if this is a no-content response
|
|
|
|
switch (mResponseHead->Status()) {
|
2011-05-19 16:43:37 -07:00
|
|
|
case 101:
|
2011-10-17 07:59:28 -07:00
|
|
|
mPreserveStream = true; // fall through to other no content
|
2007-03-22 10:30:00 -07:00
|
|
|
case 204:
|
|
|
|
case 205:
|
|
|
|
case 304:
|
2011-10-17 07:59:28 -07:00
|
|
|
mNoContent = true;
|
2007-03-22 10:30:00 -07:00
|
|
|
LOG(("this response should not contain a body.\n"));
|
|
|
|
break;
|
|
|
|
}
|
2013-05-16 06:30:42 -07:00
|
|
|
|
2012-05-07 07:27:25 -07:00
|
|
|
if (mResponseHead->Status() == 200 &&
|
|
|
|
mConnection->IsProxyConnectInProgress()) {
|
|
|
|
// successful CONNECTs do not have response bodies
|
|
|
|
mNoContent = true;
|
|
|
|
}
|
2011-01-27 10:34:39 -08:00
|
|
|
mConnection->SetLastTransactionExpectedNoContent(mNoContent);
|
2012-03-22 16:39:31 -07:00
|
|
|
if (mInvalidResponseBytesRead)
|
|
|
|
gHttpHandler->ConnMgr()->PipelineFeedbackInfo(
|
|
|
|
mConnInfo, nsHttpConnectionMgr::BadInsufficientFraming,
|
2012-07-30 07:20:58 -07:00
|
|
|
nullptr, mClassification);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
if (mNoContent)
|
|
|
|
mContentLength = 0;
|
|
|
|
else {
|
|
|
|
// grab the content-length from the response headers
|
|
|
|
mContentLength = mResponseHead->ContentLength();
|
|
|
|
|
2012-03-22 16:39:31 -07:00
|
|
|
if ((mClassification != CLASS_SOLO) &&
|
|
|
|
(mContentLength > mMaxPipelineObjectSize))
|
|
|
|
CancelPipeline(nsHttpConnectionMgr::BadUnexpectedLarge);
|
2013-05-16 06:30:42 -07:00
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
// handle chunked encoding here, so we'll know immediately when
|
|
|
|
// we're done with the socket. please note that _all_ other
|
|
|
|
// decoding is done when the channel receives the content data
|
|
|
|
// so as not to block the socket transport thread too much.
|
|
|
|
// ignore chunked responses from HTTP/1.0 servers and proxies.
|
|
|
|
if (mResponseHead->Version() >= NS_HTTP_VERSION_1_1 &&
|
|
|
|
mResponseHead->HasHeaderValue(nsHttp::Transfer_Encoding, "chunked")) {
|
|
|
|
// we only support the "chunked" transfer encoding right now.
|
|
|
|
mChunkedDecoder = new nsHttpChunkedDecoder();
|
|
|
|
if (!mChunkedDecoder)
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
LOG(("chunked decoder created\n"));
|
|
|
|
// Ignore server specified Content-Length.
|
|
|
|
mContentLength = -1;
|
|
|
|
}
|
|
|
|
#if defined(PR_LOGGING)
|
2012-08-22 08:56:38 -07:00
|
|
|
else if (mContentLength == int64_t(-1))
|
2007-03-22 10:30:00 -07:00
|
|
|
LOG(("waiting for the server to close the connection.\n"));
|
|
|
|
#endif
|
|
|
|
}
|
2012-04-05 15:37:57 -07:00
|
|
|
if (mRestartInProgressVerifier.IsSetup() &&
|
2012-03-22 16:39:31 -07:00
|
|
|
!mRestartInProgressVerifier.Verify(mContentLength, mResponseHead)) {
|
|
|
|
LOG(("Restart in progress subsequent transaction failed to match"));
|
|
|
|
return NS_ERROR_ABORT;
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2011-10-17 07:59:28 -07:00
|
|
|
mDidContentStart = true;
|
2012-04-05 15:37:57 -07:00
|
|
|
|
|
|
|
// The verifier only initializes itself once (from the first iteration of
|
|
|
|
// a transaction that gets far enough to have response headers)
|
2014-03-18 09:36:18 -07:00
|
|
|
if (mRequestHead->IsGet())
|
2012-04-04 18:19:20 -07:00
|
|
|
mRestartInProgressVerifier.Set(mContentLength, mResponseHead);
|
2012-04-05 15:37:57 -07:00
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
// called on the socket thread
|
|
|
|
nsresult
|
|
|
|
nsHttpTransaction::HandleContent(char *buf,
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t count,
|
|
|
|
uint32_t *contentRead,
|
|
|
|
uint32_t *contentRemaining)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
nsresult rv;
|
|
|
|
|
2013-06-04 16:32:31 -07:00
|
|
|
LOG(("nsHttpTransaction::HandleContent [this=%p count=%u]\n", this, count));
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
*contentRead = 0;
|
|
|
|
*contentRemaining = 0;
|
|
|
|
|
2013-05-16 06:30:41 -07:00
|
|
|
MOZ_ASSERT(mConnection);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
if (!mDidContentStart) {
|
|
|
|
rv = HandleContentStart();
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
// Do not write content to the pipe if we haven't started streaming yet
|
|
|
|
if (!mDidContentStart)
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mChunkedDecoder) {
|
|
|
|
// give the buf over to the chunked decoder so it can reformat the
|
|
|
|
// data and tell us how much is really there.
|
|
|
|
rv = mChunkedDecoder->HandleChunkedContent(buf, count, contentRead, contentRemaining);
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
}
|
2012-08-22 08:56:38 -07:00
|
|
|
else if (mContentLength >= int64_t(0)) {
|
2007-03-22 10:30:00 -07:00
|
|
|
// HTTP/1.0 servers have been known to send erroneous Content-Length
|
|
|
|
// headers. So, unless the connection is persistent, we must make
|
|
|
|
// allowances for a possibly invalid Content-Length header. Thus, if
|
|
|
|
// NOT persistent, we simply accept everything in |buf|.
|
2012-08-16 03:37:52 -07:00
|
|
|
if (mConnection->IsPersistent() || mPreserveStream ||
|
|
|
|
mHttpVersion >= NS_HTTP_VERSION_1_1) {
|
2012-08-22 08:56:38 -07:00
|
|
|
int64_t remaining = mContentLength - mContentRead;
|
2013-01-15 04:22:03 -08:00
|
|
|
*contentRead = uint32_t(std::min<int64_t>(count, remaining));
|
2007-03-22 10:30:00 -07:00
|
|
|
*contentRemaining = count - *contentRead;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
*contentRead = count;
|
|
|
|
// mContentLength might need to be increased...
|
2012-08-22 08:56:38 -07:00
|
|
|
int64_t position = mContentRead + int64_t(count);
|
2007-03-22 10:30:00 -07:00
|
|
|
if (position > mContentLength) {
|
|
|
|
mContentLength = position;
|
|
|
|
//mResponseHead->SetContentLength(mContentLength);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
// when we are just waiting for the server to close the connection...
|
|
|
|
// (no explicit content-length given)
|
|
|
|
*contentRead = count;
|
|
|
|
}
|
2013-05-16 06:30:42 -07:00
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
int64_t toReadBeforeRestart =
|
2012-04-05 15:37:57 -07:00
|
|
|
mRestartInProgressVerifier.ToReadBeforeRestart();
|
|
|
|
|
|
|
|
if (toReadBeforeRestart && *contentRead) {
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t ignore =
|
2013-01-15 04:22:03 -08:00
|
|
|
static_cast<uint32_t>(std::min<int64_t>(toReadBeforeRestart, UINT32_MAX));
|
|
|
|
ignore = std::min(*contentRead, ignore);
|
2012-03-22 16:39:31 -07:00
|
|
|
LOG(("Due To Restart ignoring %d of remaining %ld",
|
2012-04-05 15:37:57 -07:00
|
|
|
ignore, toReadBeforeRestart));
|
2012-03-22 16:39:31 -07:00
|
|
|
*contentRead -= ignore;
|
|
|
|
mContentRead += ignore;
|
2012-04-05 15:37:57 -07:00
|
|
|
mRestartInProgressVerifier.HaveReadBeforeRestart(ignore);
|
2012-03-22 16:39:31 -07:00
|
|
|
memmove(buf, buf + ignore, *contentRead + *contentRemaining);
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
if (*contentRead) {
|
|
|
|
// update count of content bytes read and report progress...
|
|
|
|
mContentRead += *contentRead;
|
2013-01-15 04:22:03 -08:00
|
|
|
/* when uncommenting, take care of 64-bit integers w/ std::max...
|
2007-03-22 10:30:00 -07:00
|
|
|
if (mProgressSink)
|
2013-01-15 04:22:03 -08:00
|
|
|
mProgressSink->OnProgress(nullptr, nullptr, mContentRead, std::max(0, mContentLength));
|
2007-03-22 10:30:00 -07:00
|
|
|
*/
|
|
|
|
}
|
|
|
|
|
2013-06-04 16:32:31 -07:00
|
|
|
LOG(("nsHttpTransaction::HandleContent [this=%p count=%u read=%u mContentRead=%lld mContentLength=%lld]\n",
|
2011-03-31 17:15:04 -07:00
|
|
|
this, count, *contentRead, mContentRead, mContentLength));
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2012-03-22 16:39:31 -07:00
|
|
|
// Check the size of chunked responses. If we exceed the max pipeline size
|
|
|
|
// for this response reschedule the pipeline
|
|
|
|
if ((mClassification != CLASS_SOLO) &&
|
|
|
|
mChunkedDecoder &&
|
2012-04-09 07:21:17 -07:00
|
|
|
((mContentRead + mChunkedDecoder->GetChunkRemaining()) >
|
|
|
|
mMaxPipelineObjectSize)) {
|
2012-03-22 16:39:31 -07:00
|
|
|
CancelPipeline(nsHttpConnectionMgr::BadUnexpectedLarge);
|
2012-04-09 07:21:17 -07:00
|
|
|
}
|
2012-03-22 16:39:31 -07:00
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
// check for end-of-file
|
|
|
|
if ((mContentRead == mContentLength) ||
|
|
|
|
(mChunkedDecoder && mChunkedDecoder->ReachedEOF())) {
|
|
|
|
// the transaction is done with a complete response.
|
2011-10-17 07:59:28 -07:00
|
|
|
mTransactionDone = true;
|
|
|
|
mResponseIsComplete = true;
|
2012-12-04 15:06:29 -08:00
|
|
|
ReleaseBlockingTransaction();
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2011-08-21 01:27:29 -07:00
|
|
|
if (TimingEnabled())
|
2012-10-24 18:00:27 -07:00
|
|
|
mTimings.responseEnd = TimeStamp::Now();
|
2011-08-21 01:27:29 -07:00
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
// report the entire response has arrived
|
|
|
|
if (mActivityDistributor)
|
|
|
|
mActivityDistributor->ObserveActivity(
|
|
|
|
mChannel,
|
|
|
|
NS_HTTP_ACTIVITY_TYPE_HTTP_TRANSACTION,
|
|
|
|
NS_HTTP_ACTIVITY_SUBTYPE_RESPONSE_COMPLETE,
|
2009-09-01 13:55:50 -07:00
|
|
|
PR_Now(),
|
2012-08-22 08:56:38 -07:00
|
|
|
static_cast<uint64_t>(mContentRead),
|
2007-03-22 10:30:00 -07:00
|
|
|
EmptyCString());
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2012-08-22 08:56:38 -07:00
|
|
|
nsHttpTransaction::ProcessData(char *buf, uint32_t count, uint32_t *countRead)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
nsresult rv;
|
|
|
|
|
2013-06-04 16:32:31 -07:00
|
|
|
LOG(("nsHttpTransaction::ProcessData [this=%p count=%u]\n", this, count));
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
*countRead = 0;
|
|
|
|
|
|
|
|
// we may not have read all of the headers yet...
|
|
|
|
if (!mHaveAllHeaders) {
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t bytesConsumed = 0;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2011-03-23 07:59:58 -07:00
|
|
|
do {
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t localBytesConsumed = 0;
|
2011-03-23 07:59:58 -07:00
|
|
|
char *localBuf = buf + bytesConsumed;
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t localCount = count - bytesConsumed;
|
2013-05-16 06:30:42 -07:00
|
|
|
|
2011-03-23 07:59:58 -07:00
|
|
|
rv = ParseHead(localBuf, localCount, &localBytesConsumed);
|
|
|
|
if (NS_FAILED(rv) && rv != NS_ERROR_NET_INTERRUPT)
|
|
|
|
return rv;
|
|
|
|
bytesConsumed += localBytesConsumed;
|
|
|
|
} while (rv == NS_ERROR_NET_INTERRUPT);
|
2013-05-16 06:30:42 -07:00
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
count -= bytesConsumed;
|
|
|
|
|
|
|
|
// if buf has some content in it, shift bytes to top of buf.
|
|
|
|
if (count && bytesConsumed)
|
|
|
|
memmove(buf, buf + bytesConsumed, count);
|
|
|
|
|
|
|
|
// report the completed response header
|
2012-03-22 16:39:31 -07:00
|
|
|
if (mActivityDistributor && mResponseHead && mHaveAllHeaders &&
|
|
|
|
!mReportedResponseHeader) {
|
|
|
|
mReportedResponseHeader = true;
|
2012-09-01 19:35:17 -07:00
|
|
|
nsAutoCString completeResponseHeaders;
|
2011-10-17 07:59:28 -07:00
|
|
|
mResponseHead->Flatten(completeResponseHeaders, false);
|
2007-03-22 10:30:00 -07:00
|
|
|
completeResponseHeaders.AppendLiteral("\r\n");
|
|
|
|
mActivityDistributor->ObserveActivity(
|
|
|
|
mChannel,
|
|
|
|
NS_HTTP_ACTIVITY_TYPE_HTTP_TRANSACTION,
|
|
|
|
NS_HTTP_ACTIVITY_SUBTYPE_RESPONSE_HEADER,
|
2012-10-25 16:25:59 -07:00
|
|
|
PR_Now(), 0,
|
2007-03-22 10:30:00 -07:00
|
|
|
completeResponseHeaders);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// even though count may be 0, we still want to call HandleContent
|
|
|
|
// so it can complete the transaction if this is a "no-content" response.
|
|
|
|
if (mHaveAllHeaders) {
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t countRemaining = 0;
|
2007-03-22 10:30:00 -07:00
|
|
|
//
|
|
|
|
// buf layout:
|
2013-05-16 06:30:42 -07:00
|
|
|
//
|
2007-03-22 10:30:00 -07:00
|
|
|
// +--------------------------------------+----------------+-----+
|
|
|
|
// | countRead | countRemaining | |
|
|
|
|
// +--------------------------------------+----------------+-----+
|
|
|
|
//
|
|
|
|
// count : bytes read from the socket
|
|
|
|
// countRead : bytes corresponding to this transaction
|
|
|
|
// countRemaining : bytes corresponding to next pipelined transaction
|
|
|
|
//
|
|
|
|
// NOTE:
|
|
|
|
// count > countRead + countRemaining <==> chunked transfer encoding
|
|
|
|
//
|
|
|
|
rv = HandleContent(buf, count, countRead, &countRemaining);
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
// we may have read more than our share, in which case we must give
|
|
|
|
// the excess bytes back to the connection
|
|
|
|
if (mResponseIsComplete && countRemaining) {
|
2013-05-16 06:30:41 -07:00
|
|
|
MOZ_ASSERT(mConnection);
|
2007-03-22 10:30:00 -07:00
|
|
|
mConnection->PushBack(buf + *countRead, countRemaining);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2012-03-22 16:39:31 -07:00
|
|
|
void
|
2012-08-22 08:56:38 -07:00
|
|
|
nsHttpTransaction::CancelPipeline(uint32_t reason)
|
2012-03-22 16:39:31 -07:00
|
|
|
{
|
|
|
|
// reason is casted through a uint to avoid compiler header deps
|
|
|
|
gHttpHandler->ConnMgr()->PipelineFeedbackInfo(
|
|
|
|
mConnInfo,
|
|
|
|
static_cast<nsHttpConnectionMgr::PipelineFeedbackInfoType>(reason),
|
2012-07-30 07:20:58 -07:00
|
|
|
nullptr, mClassification);
|
2012-03-22 16:39:31 -07:00
|
|
|
|
2012-04-09 07:21:17 -07:00
|
|
|
mConnection->CancelPipeline(NS_ERROR_ABORT);
|
2012-03-22 16:39:31 -07:00
|
|
|
|
|
|
|
// Avoid pipelining this transaction on restart by classifying it as solo.
|
|
|
|
// This also prevents BadUnexpectedLarge from being reported more
|
|
|
|
// than one time per transaction.
|
|
|
|
mClassification = CLASS_SOLO;
|
|
|
|
}
|
|
|
|
|
2012-12-04 15:06:29 -08:00
|
|
|
// Called when the transaction marked for blocking is associated with a connection
|
|
|
|
// (i.e. added to a spdy session, an idle http connection, or placed into
|
|
|
|
// a http pipeline). It is safe to call this multiple times with it only
|
|
|
|
// having an effect once.
|
|
|
|
void
|
|
|
|
nsHttpTransaction::DispatchedAsBlocking()
|
|
|
|
{
|
|
|
|
if (mDispatchedAsBlocking)
|
|
|
|
return;
|
|
|
|
|
|
|
|
LOG(("nsHttpTransaction %p dispatched as blocking\n", this));
|
2013-05-16 06:30:42 -07:00
|
|
|
|
2012-12-04 15:06:29 -08:00
|
|
|
if (!mLoadGroupCI)
|
|
|
|
return;
|
|
|
|
|
|
|
|
LOG(("nsHttpTransaction adding blocking channel %p from "
|
|
|
|
"loadgroup %p\n", this, mLoadGroupCI.get()));
|
2013-05-16 06:30:42 -07:00
|
|
|
|
2012-12-04 15:06:29 -08:00
|
|
|
mLoadGroupCI->AddBlockingTransaction();
|
|
|
|
mDispatchedAsBlocking = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nsHttpTransaction::RemoveDispatchedAsBlocking()
|
|
|
|
{
|
|
|
|
if (!mLoadGroupCI || !mDispatchedAsBlocking)
|
|
|
|
return;
|
2013-05-16 06:30:42 -07:00
|
|
|
|
2012-12-04 15:06:29 -08:00
|
|
|
uint32_t blockers = 0;
|
|
|
|
nsresult rv = mLoadGroupCI->RemoveBlockingTransaction(&blockers);
|
|
|
|
|
|
|
|
LOG(("nsHttpTransaction removing blocking channel %p from "
|
|
|
|
"loadgroup %p. %d blockers remain.\n", this,
|
|
|
|
mLoadGroupCI.get(), blockers));
|
|
|
|
|
|
|
|
if (NS_SUCCEEDED(rv) && !blockers) {
|
|
|
|
LOG(("nsHttpTransaction %p triggering release of blocked channels.\n",
|
|
|
|
this));
|
|
|
|
gHttpHandler->ConnMgr()->ProcessPendingQ();
|
|
|
|
}
|
2013-05-16 06:30:42 -07:00
|
|
|
|
2012-12-04 15:06:29 -08:00
|
|
|
mDispatchedAsBlocking = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nsHttpTransaction::ReleaseBlockingTransaction()
|
|
|
|
{
|
|
|
|
RemoveDispatchedAsBlocking();
|
|
|
|
mLoadGroupCI = nullptr;
|
|
|
|
}
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
// nsHttpTransaction deletion event
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
2014-07-08 09:43:04 -07:00
|
|
|
class DeleteHttpTransaction : public nsRunnable {
|
2007-03-22 10:30:00 -07:00
|
|
|
public:
|
2014-08-05 06:20:24 -07:00
|
|
|
explicit DeleteHttpTransaction(nsHttpTransaction *trans)
|
2007-03-22 10:30:00 -07:00
|
|
|
: mTrans(trans)
|
|
|
|
{}
|
|
|
|
|
|
|
|
NS_IMETHOD Run()
|
|
|
|
{
|
|
|
|
delete mTrans;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
private:
|
|
|
|
nsHttpTransaction *mTrans;
|
|
|
|
};
|
|
|
|
|
|
|
|
void
|
|
|
|
nsHttpTransaction::DeleteSelfOnConsumerThread()
|
|
|
|
{
|
2013-06-04 16:32:31 -07:00
|
|
|
LOG(("nsHttpTransaction::DeleteSelfOnConsumerThread [this=%p]\n", this));
|
2013-05-16 06:30:42 -07:00
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
bool val;
|
2011-06-11 07:56:36 -07:00
|
|
|
if (!mConsumerTarget ||
|
|
|
|
(NS_SUCCEEDED(mConsumerTarget->IsOnCurrentThread(&val)) && val)) {
|
2007-03-22 10:30:00 -07:00
|
|
|
delete this;
|
2011-06-11 07:56:36 -07:00
|
|
|
} else {
|
2007-03-22 10:30:00 -07:00
|
|
|
LOG(("proxying delete to consumer thread...\n"));
|
2014-07-08 09:43:04 -07:00
|
|
|
nsCOMPtr<nsIRunnable> event = new DeleteHttpTransaction(this);
|
2007-03-22 10:30:00 -07:00
|
|
|
if (NS_FAILED(mConsumerTarget->Dispatch(event, NS_DISPATCH_NORMAL)))
|
|
|
|
NS_WARNING("failed to dispatch nsHttpDeleteTransaction event");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-04-15 05:50:35 -07:00
|
|
|
bool
|
|
|
|
nsHttpTransaction::TryToRunPacedRequest()
|
|
|
|
{
|
|
|
|
if (mSubmittedRatePacing)
|
|
|
|
return mPassedRatePacing;
|
|
|
|
|
|
|
|
mSubmittedRatePacing = true;
|
|
|
|
mSynchronousRatePaceRequest = true;
|
|
|
|
gHttpHandler->SubmitPacedRequest(this, getter_AddRefs(mTokenBucketCancel));
|
|
|
|
mSynchronousRatePaceRequest = false;
|
|
|
|
return mPassedRatePacing;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nsHttpTransaction::OnTokenBucketAdmitted()
|
|
|
|
{
|
|
|
|
mPassedRatePacing = true;
|
|
|
|
mTokenBucketCancel = nullptr;
|
|
|
|
|
|
|
|
if (!mSynchronousRatePaceRequest)
|
|
|
|
gHttpHandler->ConnMgr()->ProcessPendingQ(mConnInfo);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nsHttpTransaction::CancelPacing(nsresult reason)
|
|
|
|
{
|
|
|
|
if (mTokenBucketCancel) {
|
|
|
|
mTokenBucketCancel->Cancel(reason);
|
|
|
|
mTokenBucketCancel = nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
// nsHttpTransaction::nsISupports
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
2013-07-18 19:24:13 -07:00
|
|
|
NS_IMPL_ADDREF(nsHttpTransaction)
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-03-27 13:38:33 -07:00
|
|
|
NS_IMETHODIMP_(MozExternalRefCountType)
|
2007-03-22 10:30:00 -07:00
|
|
|
nsHttpTransaction::Release()
|
|
|
|
{
|
|
|
|
nsrefcnt count;
|
|
|
|
NS_PRECONDITION(0 != mRefCnt, "dup release");
|
2013-07-18 19:24:13 -07:00
|
|
|
count = --mRefCnt;
|
2007-03-22 10:30:00 -07:00
|
|
|
NS_LOG_RELEASE(this, count, "nsHttpTransaction");
|
|
|
|
if (0 == count) {
|
|
|
|
mRefCnt = 1; /* stablize */
|
2013-05-16 06:30:42 -07:00
|
|
|
// it is essential that the transaction be destroyed on the consumer
|
2007-03-22 10:30:00 -07:00
|
|
|
// thread (we could be holding the last reference to our consumer).
|
|
|
|
DeleteSelfOnConsumerThread();
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return count;
|
|
|
|
}
|
|
|
|
|
2014-04-27 00:06:00 -07:00
|
|
|
NS_IMPL_QUERY_INTERFACE(nsHttpTransaction,
|
|
|
|
nsIInputStreamCallback,
|
|
|
|
nsIOutputStreamCallback)
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
// nsHttpTransaction::nsIInputStreamCallback
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
// called on the socket thread
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsHttpTransaction::OnInputStreamReady(nsIAsyncInputStream *out)
|
|
|
|
{
|
|
|
|
if (mConnection) {
|
2012-01-25 21:15:26 -08:00
|
|
|
mConnection->TransactionHasDataToWrite(this);
|
|
|
|
nsresult rv = mConnection->ResumeSend();
|
2007-03-22 10:30:00 -07:00
|
|
|
if (NS_FAILED(rv))
|
|
|
|
NS_ERROR("ResumeSend failed");
|
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
// nsHttpTransaction::nsIOutputStreamCallback
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
// called on the socket thread
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsHttpTransaction::OnOutputStreamReady(nsIAsyncOutputStream *out)
|
|
|
|
{
|
|
|
|
if (mConnection) {
|
2012-01-25 21:15:26 -08:00
|
|
|
nsresult rv = mConnection->ResumeRecv();
|
2007-03-22 10:30:00 -07:00
|
|
|
if (NS_FAILED(rv))
|
|
|
|
NS_ERROR("ResumeRecv failed");
|
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2012-03-22 16:39:31 -07:00
|
|
|
|
|
|
|
// nsHttpTransaction::RestartVerifier
|
|
|
|
|
|
|
|
static bool
|
|
|
|
matchOld(nsHttpResponseHead *newHead, nsCString &old,
|
|
|
|
nsHttpAtom headerAtom)
|
|
|
|
{
|
|
|
|
const char *val;
|
2013-05-16 06:30:42 -07:00
|
|
|
|
2012-03-22 16:39:31 -07:00
|
|
|
val = newHead->PeekHeader(headerAtom);
|
|
|
|
if (val && old.IsEmpty())
|
|
|
|
return false;
|
|
|
|
if (!val && !old.IsEmpty())
|
|
|
|
return false;
|
|
|
|
if (val && !old.Equals(val))
|
|
|
|
return false;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2012-08-22 08:56:38 -07:00
|
|
|
nsHttpTransaction::RestartVerifier::Verify(int64_t contentLength,
|
2012-03-22 16:39:31 -07:00
|
|
|
nsHttpResponseHead *newHead)
|
|
|
|
{
|
|
|
|
if (mContentLength != contentLength)
|
|
|
|
return false;
|
|
|
|
|
2012-04-04 18:19:20 -07:00
|
|
|
if (newHead->Status() != 200)
|
|
|
|
return false;
|
|
|
|
|
2012-03-22 16:39:31 -07:00
|
|
|
if (!matchOld(newHead, mContentRange, nsHttp::Content_Range))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (!matchOld(newHead, mLastModified, nsHttp::Last_Modified))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (!matchOld(newHead, mETag, nsHttp::ETag))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (!matchOld(newHead, mContentEncoding, nsHttp::Content_Encoding))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (!matchOld(newHead, mTransferEncoding, nsHttp::Transfer_Encoding))
|
|
|
|
return false;
|
2013-05-16 06:30:42 -07:00
|
|
|
|
2012-03-22 16:39:31 -07:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2012-08-22 08:56:38 -07:00
|
|
|
nsHttpTransaction::RestartVerifier::Set(int64_t contentLength,
|
2012-03-22 16:39:31 -07:00
|
|
|
nsHttpResponseHead *head)
|
|
|
|
{
|
|
|
|
if (mSetup)
|
|
|
|
return;
|
|
|
|
|
2012-04-04 18:19:20 -07:00
|
|
|
// If mSetup does not transition to true RestartInPogress() is later
|
|
|
|
// forbidden
|
|
|
|
|
|
|
|
// Only RestartInProgress with 200 response code
|
|
|
|
if (head->Status() != 200)
|
|
|
|
return;
|
|
|
|
|
2012-03-22 16:39:31 -07:00
|
|
|
mContentLength = contentLength;
|
2013-05-16 06:30:42 -07:00
|
|
|
|
2012-03-22 16:39:31 -07:00
|
|
|
if (head) {
|
|
|
|
const char *val;
|
|
|
|
val = head->PeekHeader(nsHttp::ETag);
|
|
|
|
if (val)
|
|
|
|
mETag.Assign(val);
|
|
|
|
val = head->PeekHeader(nsHttp::Last_Modified);
|
|
|
|
if (val)
|
|
|
|
mLastModified.Assign(val);
|
|
|
|
val = head->PeekHeader(nsHttp::Content_Range);
|
|
|
|
if (val)
|
|
|
|
mContentRange.Assign(val);
|
|
|
|
val = head->PeekHeader(nsHttp::Content_Encoding);
|
|
|
|
if (val)
|
|
|
|
mContentEncoding.Assign(val);
|
|
|
|
val = head->PeekHeader(nsHttp::Transfer_Encoding);
|
|
|
|
if (val)
|
|
|
|
mTransferEncoding.Assign(val);
|
2012-04-05 15:37:57 -07:00
|
|
|
|
|
|
|
// We can only restart with any confidence if we have a stored etag or
|
|
|
|
// last-modified header
|
|
|
|
if (mETag.IsEmpty() && mLastModified.IsEmpty())
|
|
|
|
return;
|
|
|
|
|
2012-03-22 16:39:31 -07:00
|
|
|
mSetup = true;
|
|
|
|
}
|
|
|
|
}
|
2014-01-07 14:05:56 -08:00
|
|
|
|
|
|
|
} // namespace mozilla::net
|
|
|
|
} // namespace mozilla
|