2001-05-11 21:04:09 +00:00
|
|
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
|
2005-05-10 06:09:45 +00:00
|
|
|
/* vim:set ts=4 sw=4 sts=4 et cin: */
|
2004-04-18 22:01:16 +00:00
|
|
|
/* ***** 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.
|
|
|
|
*
|
2001-05-11 21:04:09 +00:00
|
|
|
* The Original Code is Mozilla.
|
2004-04-18 22:01:16 +00:00
|
|
|
*
|
|
|
|
* The Initial Developer of the Original Code is
|
|
|
|
* Netscape Communications.
|
|
|
|
* Portions created by the Initial Developer are Copyright (C) 2001
|
|
|
|
* the Initial Developer. All Rights Reserved.
|
|
|
|
*
|
|
|
|
* Contributor(s):
|
2001-05-11 21:04:09 +00:00
|
|
|
* Darin Fisher <darin@netscape.com> (original author)
|
2001-05-25 23:07:44 +00:00
|
|
|
* Andreas M. Schneider <clarence@clarence.de>
|
2004-04-18 22:01:16 +00:00
|
|
|
*
|
|
|
|
* 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 ***** */
|
2001-05-11 21:04:09 +00:00
|
|
|
|
2010-04-21 11:26:01 +00:00
|
|
|
#include "base/basictypes.h"
|
|
|
|
|
2010-04-20 16:32:28 +00:00
|
|
|
#include "nsIOService.h"
|
2001-05-11 21:04:09 +00:00
|
|
|
#include "nsHttpHandler.h"
|
|
|
|
#include "nsHttpTransaction.h"
|
|
|
|
#include "nsHttpConnection.h"
|
|
|
|
#include "nsHttpRequestHead.h"
|
|
|
|
#include "nsHttpResponseHead.h"
|
|
|
|
#include "nsHttpChunkedDecoder.h"
|
2003-10-06 01:46:31 +00:00
|
|
|
#include "nsTransportUtils.h"
|
2007-08-24 01:45:23 +00:00
|
|
|
#include "nsNetUtil.h"
|
2006-05-10 17:30:15 +00:00
|
|
|
#include "nsProxyRelease.h"
|
2003-01-18 02:15:14 +00:00
|
|
|
#include "nsIOService.h"
|
2011-03-28 19:58:49 +00:00
|
|
|
#include "nsAtomicRefcnt.h"
|
2001-05-25 23:18:50 +00:00
|
|
|
|
2003-01-18 02:15:14 +00:00
|
|
|
#include "nsISeekableStream.h"
|
|
|
|
#include "nsISocketTransport.h"
|
|
|
|
#include "nsMultiplexInputStream.h"
|
2006-01-02 03:20:04 +00:00
|
|
|
#include "nsStringStream.h"
|
2003-01-18 02:15:14 +00:00
|
|
|
|
2004-05-08 18:02:10 +00:00
|
|
|
#include "nsComponentManagerUtils.h" // do_CreateInstance
|
2006-05-19 22:50:02 +00:00
|
|
|
#include "nsServiceManagerUtils.h" // do_GetService
|
|
|
|
#include "nsIHttpActivityObserver.h"
|
2004-05-08 18:02:10 +00:00
|
|
|
|
2010-05-19 23:22:19 +00:00
|
|
|
#include "mozilla/FunctionTimer.h"
|
|
|
|
|
2003-01-18 02:15:14 +00: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 18:34:39 +00: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)
|
|
|
|
|
2001-05-11 21:04:09 +00:00
|
|
|
//-----------------------------------------------------------------------------
|
2001-09-04 23:11:46 +00:00
|
|
|
// helpers
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
2001-09-28 22:23:26 +00:00
|
|
|
#if defined(PR_LOGGING)
|
|
|
|
static void
|
|
|
|
LogHeaders(const char *lines)
|
|
|
|
{
|
|
|
|
nsCAutoString buf;
|
|
|
|
char *p;
|
|
|
|
while ((p = PL_strstr(lines, "\r\n")) != nsnull) {
|
|
|
|
buf.Assign(lines, p - lines);
|
|
|
|
if (PL_strcasestr(buf.get(), "authorization: ") != nsnull) {
|
|
|
|
char *p = PL_strchr(PL_strchr(buf.get(), ' ')+1, ' ');
|
|
|
|
while (*++p) *p = '*';
|
|
|
|
}
|
2003-07-11 22:43:28 +00:00
|
|
|
LOG3((" %s\n", buf.get()));
|
2001-09-28 22:23:26 +00:00
|
|
|
lines = p + 2;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2001-09-04 23:11:46 +00:00
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
// nsHttpTransaction <public>
|
2001-05-11 21:04:09 +00:00
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
2003-01-18 02:15:14 +00:00
|
|
|
nsHttpTransaction::nsHttpTransaction()
|
|
|
|
: mRequestSize(0)
|
2001-05-11 21:04:09 +00:00
|
|
|
, mConnection(nsnull)
|
2003-01-18 02:15:14 +00:00
|
|
|
, mConnInfo(nsnull)
|
|
|
|
, mRequestHead(nsnull)
|
2001-05-11 21:04:09 +00:00
|
|
|
, mResponseHead(nsnull)
|
|
|
|
, mContentLength(-1)
|
|
|
|
, mContentRead(0)
|
2011-01-27 18:34:39 +00:00
|
|
|
, mInvalidResponseBytesRead(0)
|
2001-05-11 21:04:09 +00:00
|
|
|
, mChunkedDecoder(nsnull)
|
|
|
|
, mStatus(NS_OK)
|
2005-03-25 03:41:33 +00:00
|
|
|
, mPriority(0)
|
2001-08-14 01:03:27 +00:00
|
|
|
, mRestartCount(0)
|
2003-01-18 02:15:14 +00:00
|
|
|
, mCaps(0)
|
2011-10-17 14:59:28 +00: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)
|
|
|
|
, mSSLConnectFailed(false)
|
|
|
|
, mHttpResponseMatched(false)
|
|
|
|
, mPreserveStream(false)
|
2001-05-11 21:04:09 +00:00
|
|
|
{
|
|
|
|
LOG(("Creating nsHttpTransaction @%x\n", this));
|
|
|
|
}
|
|
|
|
|
|
|
|
nsHttpTransaction::~nsHttpTransaction()
|
|
|
|
{
|
|
|
|
LOG(("Destroying nsHttpTransaction @%x\n", this));
|
|
|
|
|
2001-06-11 21:20:29 +00:00
|
|
|
NS_IF_RELEASE(mConnection);
|
2003-01-18 02:15:14 +00:00
|
|
|
NS_IF_RELEASE(mConnInfo);
|
2001-05-11 21:04:09 +00:00
|
|
|
|
2001-08-02 19:26:24 +00:00
|
|
|
delete mResponseHead;
|
2003-01-18 02:15:14 +00:00
|
|
|
delete mChunkedDecoder;
|
2001-05-11 21:04:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2003-01-18 02:15:14 +00:00
|
|
|
nsHttpTransaction::Init(PRUint8 caps,
|
|
|
|
nsHttpConnectionInfo *cinfo,
|
|
|
|
nsHttpRequestHead *requestHead,
|
|
|
|
nsIInputStream *requestBody,
|
2011-09-29 06:19:26 +00:00
|
|
|
bool requestBodyHasHeaders,
|
2006-05-10 17:30:15 +00:00
|
|
|
nsIEventTarget *target,
|
2003-01-18 02:15:14 +00:00
|
|
|
nsIInterfaceRequestor *callbacks,
|
|
|
|
nsITransportEventSink *eventsink,
|
|
|
|
nsIAsyncInputStream **responseBody)
|
2001-05-11 21:04:09 +00:00
|
|
|
{
|
2010-05-19 23:22:19 +00:00
|
|
|
NS_TIME_FUNCTION;
|
|
|
|
|
2001-05-11 21:04:09 +00:00
|
|
|
nsresult rv;
|
|
|
|
|
2003-01-18 02:15:14 +00:00
|
|
|
LOG(("nsHttpTransaction::Init [this=%x caps=%x]\n", this, caps));
|
2001-05-11 21:04:09 +00:00
|
|
|
|
2003-01-18 02:15:14 +00:00
|
|
|
NS_ASSERTION(cinfo, "ouch");
|
|
|
|
NS_ASSERTION(requestHead, "ouch");
|
2006-05-10 17:30:15 +00:00
|
|
|
NS_ASSERTION(target, "ouch");
|
2003-10-06 01:46:31 +00:00
|
|
|
|
2006-05-19 22:50:02 +00:00
|
|
|
mActivityDistributor = do_GetService(NS_HTTPACTIVITYDISTRIBUTOR_CONTRACTID, &rv);
|
2009-09-01 20:55:50 +00:00
|
|
|
if (NS_FAILED(rv)) return rv;
|
2006-05-19 22:50:02 +00:00
|
|
|
|
2011-09-29 06:19:26 +00:00
|
|
|
bool activityDistributorActive;
|
2010-01-26 13:33:03 +00:00
|
|
|
rv = mActivityDistributor->GetIsActive(&activityDistributorActive);
|
|
|
|
if (NS_SUCCEEDED(rv) && activityDistributorActive) {
|
2009-09-01 20:55:50 +00: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 " \
|
|
|
|
"this=%x", this));
|
2010-01-26 13:33:03 +00:00
|
|
|
} else {
|
2009-09-01 20:55:50 +00:00
|
|
|
// there is no observer, so don't use it
|
2011-10-17 14:59:28 +00:00
|
|
|
activityDistributorActive = false;
|
2009-09-01 20:55:50 +00:00
|
|
|
mActivityDistributor = nsnull;
|
2010-01-26 13:33:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// create transport event sink proxy. it coalesces all events if and only
|
|
|
|
// 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;
|
2006-05-19 22:50:02 +00:00
|
|
|
|
2003-01-18 02:15:14 +00:00
|
|
|
NS_ADDREF(mConnInfo = cinfo);
|
|
|
|
mCallbacks = callbacks;
|
2006-05-10 17:30:15 +00:00
|
|
|
mConsumerTarget = target;
|
2003-01-18 02:15:14 +00:00
|
|
|
mCaps = caps;
|
2001-09-14 21:08:58 +00:00
|
|
|
|
2001-05-11 21:04:09 +00:00
|
|
|
if (requestHead->Method() == nsHttp::Head)
|
2011-10-17 14:59:28 +00:00
|
|
|
mNoContent = true;
|
2001-05-11 21:04:09 +00:00
|
|
|
|
2008-10-01 05:31:24 +00: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.
|
|
|
|
if ((requestHead->Method() == nsHttp::Post || requestHead->Method() == nsHttp::Put) &&
|
|
|
|
!requestBody && !requestHead->PeekHeader(nsHttp::Transfer_Encoding)) {
|
|
|
|
requestHead->SetHeader(nsHttp::Content_Length, NS_LITERAL_CSTRING("0"));
|
|
|
|
}
|
|
|
|
|
2001-05-30 00:40:50 +00:00
|
|
|
// grab a weak reference to the request head
|
|
|
|
mRequestHead = requestHead;
|
|
|
|
|
2003-01-18 02:15:14 +00:00
|
|
|
// make sure we eliminate any proxy specific headers from
|
|
|
|
// the request if we are talking HTTPS via a SSL tunnel.
|
2011-09-29 06:19:26 +00:00
|
|
|
bool pruneProxyHeaders =
|
2011-05-13 17:53:27 +00:00
|
|
|
cinfo->ShouldForceConnectMethod() ||
|
|
|
|
(cinfo->UsingSSL() && cinfo->UsingHttpProxy());
|
|
|
|
|
2003-01-18 02:15:14 +00:00
|
|
|
mReqHeaderBuf.Truncate();
|
2002-03-13 07:34:51 +00:00
|
|
|
requestHead->Flatten(mReqHeaderBuf, pruneProxyHeaders);
|
2001-05-11 21:04:09 +00:00
|
|
|
|
2001-09-28 22:23:26 +00:00
|
|
|
#if defined(PR_LOGGING)
|
2003-07-11 22:43:28 +00:00
|
|
|
if (LOG3_ENABLED()) {
|
|
|
|
LOG3(("http request [\n"));
|
2001-09-28 22:23:26 +00:00
|
|
|
LogHeaders(mReqHeaderBuf.get());
|
2003-07-11 22:43:28 +00:00
|
|
|
LOG3(("]\n"));
|
2001-09-28 22:23:26 +00:00
|
|
|
}
|
|
|
|
#endif
|
2001-05-11 21:04:09 +00:00
|
|
|
|
2002-01-11 22:44:26 +00:00
|
|
|
// 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)
|
2004-12-04 10:19:29 +00:00
|
|
|
mReqHeaderBuf.AppendLiteral("\r\n");
|
2001-05-11 21:04:09 +00:00
|
|
|
|
2006-05-19 22:50:02 +00:00
|
|
|
// report the request header
|
|
|
|
if (mActivityDistributor)
|
|
|
|
mActivityDistributor->ObserveActivity(
|
|
|
|
mChannel,
|
|
|
|
NS_HTTP_ACTIVITY_TYPE_HTTP_TRANSACTION,
|
|
|
|
NS_HTTP_ACTIVITY_SUBTYPE_REQUEST_HEADER,
|
2009-09-01 20:55:50 +00:00
|
|
|
PR_Now(), LL_ZERO,
|
2006-05-19 22:50:02 +00:00
|
|
|
mReqHeaderBuf);
|
|
|
|
|
2003-01-18 02:15:14 +00:00
|
|
|
// 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).
|
2003-07-11 23:10:27 +00:00
|
|
|
nsCOMPtr<nsIInputStream> headers;
|
|
|
|
rv = NS_NewByteInputStream(getter_AddRefs(headers),
|
2001-11-04 05:50:37 +00:00
|
|
|
mReqHeaderBuf.get(),
|
|
|
|
mReqHeaderBuf.Length());
|
2001-05-11 21:04:09 +00:00
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
2003-01-18 02:15:14 +00:00
|
|
|
if (requestBody) {
|
2011-10-17 14:59:28 +00:00
|
|
|
mHasRequestBody = true;
|
2004-04-10 20:03:03 +00:00
|
|
|
|
2003-01-18 02:15:14 +00: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-24 01:45:23 +00: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 16:32:28 +00:00
|
|
|
nsIOService::gDefaultSegmentSize);
|
2007-08-24 01:45:23 +00:00
|
|
|
if (NS_FAILED(rv)) return rv;
|
2003-01-18 02:15:14 +00: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 14:59:28 +00:00
|
|
|
true, true,
|
2010-04-20 16:32:28 +00:00
|
|
|
nsIOService::gDefaultSegmentSize,
|
2012-01-10 03:43:52 +00:00
|
|
|
nsIOService::gDefaultSegmentCount);
|
2003-01-18 02:15:14 +00:00
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
|
|
|
NS_ADDREF(*responseBody = mPipeIn);
|
|
|
|
return NS_OK;
|
2001-05-11 21:04:09 +00:00
|
|
|
}
|
|
|
|
|
2011-12-13 15:55:50 +00:00
|
|
|
nsAHttpConnection *
|
|
|
|
nsHttpTransaction::Connection()
|
|
|
|
{
|
|
|
|
return mConnection;
|
|
|
|
}
|
|
|
|
|
2001-05-11 21:04:09 +00:00
|
|
|
nsHttpResponseHead *
|
|
|
|
nsHttpTransaction::TakeResponseHead()
|
|
|
|
{
|
2004-09-30 14:20:27 +00:00
|
|
|
if (!mHaveAllHeaders) {
|
|
|
|
NS_WARNING("response headers not available or incomplete");
|
2001-08-02 19:26:24 +00:00
|
|
|
return nsnull;
|
2004-09-30 14:20:27 +00:00
|
|
|
}
|
2001-08-02 19:26:24 +00:00
|
|
|
|
2001-05-11 21:04:09 +00:00
|
|
|
nsHttpResponseHead *head = mResponseHead;
|
|
|
|
mResponseHead = nsnull;
|
|
|
|
return head;
|
|
|
|
}
|
|
|
|
|
2011-04-08 18:37:16 +00:00
|
|
|
void
|
|
|
|
nsHttpTransaction::SetSSLConnectFailed()
|
|
|
|
{
|
2011-10-17 14:59:28 +00:00
|
|
|
mSSLConnectFailed = true;
|
2011-04-08 18:37:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
nsHttpRequestHead *
|
|
|
|
nsHttpTransaction::RequestHead()
|
|
|
|
{
|
|
|
|
return mRequestHead;
|
|
|
|
}
|
|
|
|
|
2011-12-13 15:55:50 +00:00
|
|
|
PRUint32
|
|
|
|
nsHttpTransaction::Http1xTransactionCount()
|
|
|
|
{
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2012-02-15 21:41:18 +00:00
|
|
|
nsresult
|
|
|
|
nsHttpTransaction::TakeSubTransactions(
|
|
|
|
nsTArray<nsRefPtr<nsAHttpTransaction> > &outTransactions)
|
|
|
|
{
|
|
|
|
return NS_ERROR_NOT_IMPLEMENTED;
|
|
|
|
}
|
|
|
|
|
2001-10-02 00:31:30 +00:00
|
|
|
//----------------------------------------------------------------------------
|
|
|
|
// nsHttpTransaction::nsAHttpTransaction
|
|
|
|
//----------------------------------------------------------------------------
|
|
|
|
|
2003-03-26 05:05:49 +00:00
|
|
|
void
|
|
|
|
nsHttpTransaction::SetConnection(nsAHttpConnection *conn)
|
|
|
|
{
|
|
|
|
NS_IF_RELEASE(mConnection);
|
|
|
|
NS_IF_ADDREF(mConnection = conn);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2011-03-31 19:38:30 +00:00
|
|
|
nsHttpTransaction::GetSecurityCallbacks(nsIInterfaceRequestor **cb,
|
|
|
|
nsIEventTarget **target)
|
2003-03-26 05:05:49 +00:00
|
|
|
{
|
|
|
|
NS_IF_ADDREF(*cb = mCallbacks);
|
2011-03-31 19:38:30 +00:00
|
|
|
if (target)
|
|
|
|
NS_IF_ADDREF(*target = mConsumerTarget);
|
2003-03-26 05:05:49 +00:00
|
|
|
}
|
|
|
|
|
2003-01-18 02:15:14 +00:00
|
|
|
void
|
2011-04-10 17:33:08 +00:00
|
|
|
nsHttpTransaction::OnTransportStatus(nsITransport* transport,
|
|
|
|
nsresult status, PRUint64 progress)
|
2003-01-18 02:15:14 +00:00
|
|
|
{
|
2005-01-09 20:43:35 +00:00
|
|
|
LOG(("nsHttpTransaction::OnSocketStatus [this=%x status=%x progress=%llu]\n",
|
2003-01-18 02:15:14 +00:00
|
|
|
this, status, progress));
|
2003-10-06 01:46:31 +00:00
|
|
|
|
2011-05-21 10:03:36 +00:00
|
|
|
if (TimingEnabled()) {
|
|
|
|
if (status == nsISocketTransport::STATUS_RESOLVING) {
|
|
|
|
mTimings.domainLookupStart = mozilla::TimeStamp::Now();
|
|
|
|
} else if (status == nsISocketTransport::STATUS_RESOLVED) {
|
|
|
|
mTimings.domainLookupEnd = mozilla::TimeStamp::Now();
|
|
|
|
} else if (status == nsISocketTransport::STATUS_CONNECTING_TO) {
|
|
|
|
mTimings.connectStart = mozilla::TimeStamp::Now();
|
|
|
|
} else if (status == nsISocketTransport::STATUS_CONNECTED_TO) {
|
|
|
|
mTimings.connectEnd = mozilla::TimeStamp::Now();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-10-06 01:46:31 +00:00
|
|
|
if (!mTransportSink)
|
|
|
|
return;
|
2011-04-10 17:33:08 +00:00
|
|
|
|
2003-01-18 02:15:14 +00:00
|
|
|
NS_ASSERTION(PR_GetCurrentThread() == gSocketThread, "wrong thread");
|
|
|
|
|
2009-03-05 18:47:01 +00: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.
|
2006-05-19 22:50:02 +00:00
|
|
|
if (mActivityDistributor) {
|
|
|
|
// upon STATUS_WAITING_FOR; report request body sent
|
|
|
|
if ((mHasRequestBody) &&
|
|
|
|
(status == nsISocketTransport::STATUS_WAITING_FOR))
|
|
|
|
mActivityDistributor->ObserveActivity(
|
|
|
|
mChannel,
|
|
|
|
NS_HTTP_ACTIVITY_TYPE_HTTP_TRANSACTION,
|
|
|
|
NS_HTTP_ACTIVITY_SUBTYPE_REQUEST_BODY_SENT,
|
2009-09-01 20:55:50 +00:00
|
|
|
PR_Now(), LL_ZERO, EmptyCString());
|
2006-05-19 22:50:02 +00:00
|
|
|
|
|
|
|
// report the status and progress
|
|
|
|
mActivityDistributor->ObserveActivity(
|
|
|
|
mChannel,
|
|
|
|
NS_HTTP_ACTIVITY_TYPE_SOCKET_TRANSPORT,
|
2007-07-08 07:08:04 +00:00
|
|
|
static_cast<PRUint32>(status),
|
2009-09-01 20:55:50 +00:00
|
|
|
PR_Now(),
|
2006-05-19 22:50:02 +00:00
|
|
|
progress,
|
|
|
|
EmptyCString());
|
|
|
|
}
|
|
|
|
|
2009-03-05 18:47:01 +00:00
|
|
|
// nsHttpChannel synthesizes progress events in OnDataAvailable
|
|
|
|
if (status == nsISocketTransport::STATUS_RECEIVING_FROM)
|
|
|
|
return;
|
|
|
|
|
2009-08-12 08:51:46 +00:00
|
|
|
PRUint64 progressMax;
|
2003-01-18 02:15:14 +00:00
|
|
|
|
2004-04-10 20:03:03 +00:00
|
|
|
if (status == nsISocketTransport::STATUS_SENDING_TO) {
|
|
|
|
// suppress progress when only writing request headers
|
|
|
|
if (!mHasRequestBody)
|
|
|
|
return;
|
|
|
|
|
2008-09-10 10:06:06 +00:00
|
|
|
nsCOMPtr<nsISeekableStream> seekable = do_QueryInterface(mRequestStream);
|
|
|
|
NS_ASSERTION(seekable, "Request stream isn't seekable?!?");
|
|
|
|
|
|
|
|
PRInt64 prog = 0;
|
|
|
|
seekable->Tell(&prog);
|
|
|
|
progress = prog;
|
|
|
|
|
2003-10-06 01:46:31 +00:00
|
|
|
// when uploading, we include the request headers in the progress
|
|
|
|
// notifications.
|
2005-01-09 20:43:35 +00:00
|
|
|
progressMax = mRequestSize; // XXX mRequestSize is 32-bit!
|
2003-01-18 02:15:14 +00:00
|
|
|
}
|
2003-10-06 01:46:31 +00:00
|
|
|
else {
|
2005-01-09 20:43:35 +00:00
|
|
|
progress = LL_ZERO;
|
2003-10-06 01:46:31 +00:00
|
|
|
progressMax = 0;
|
|
|
|
}
|
|
|
|
|
2011-04-10 17:33:08 +00:00
|
|
|
mTransportSink->OnTransportStatus(transport, status, progress, progressMax);
|
2003-01-18 02:15:14 +00:00
|
|
|
}
|
|
|
|
|
2011-09-29 06:19:26 +00:00
|
|
|
bool
|
2003-03-26 05:05:49 +00:00
|
|
|
nsHttpTransaction::IsDone()
|
|
|
|
{
|
|
|
|
return mTransactionDone;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsHttpTransaction::Status()
|
|
|
|
{
|
|
|
|
return mStatus;
|
|
|
|
}
|
|
|
|
|
2002-04-19 22:25:23 +00:00
|
|
|
PRUint32
|
2003-01-18 02:15:14 +00:00
|
|
|
nsHttpTransaction::Available()
|
2002-04-19 22:25:23 +00:00
|
|
|
{
|
2003-01-18 02:15:14 +00:00
|
|
|
PRUint32 size;
|
|
|
|
if (NS_FAILED(mRequestStream->Available(&size)))
|
|
|
|
size = 0;
|
|
|
|
return size;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_METHOD
|
|
|
|
nsHttpTransaction::ReadRequestSegment(nsIInputStream *stream,
|
|
|
|
void *closure,
|
|
|
|
const char *buf,
|
|
|
|
PRUint32 offset,
|
|
|
|
PRUint32 count,
|
|
|
|
PRUint32 *countRead)
|
|
|
|
{
|
|
|
|
nsHttpTransaction *trans = (nsHttpTransaction *) closure;
|
2003-04-10 19:06:24 +00:00
|
|
|
nsresult rv = trans->mReader->OnReadSegment(buf, count, countRead);
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
2011-05-21 10:03:36 +00:00
|
|
|
if (trans->TimingEnabled() && trans->mTimings.requestStart.IsNull()) {
|
|
|
|
// First data we're sending -> this is requestStart
|
|
|
|
trans->mTimings.requestStart = mozilla::TimeStamp::Now();
|
|
|
|
}
|
2011-10-17 14:59:28 +00:00
|
|
|
trans->mSentData = true;
|
2003-04-10 19:06:24 +00:00
|
|
|
return NS_OK;
|
2002-04-19 22:25:23 +00:00
|
|
|
}
|
|
|
|
|
2001-05-11 21:04:09 +00:00
|
|
|
nsresult
|
2003-01-18 02:15:14 +00:00
|
|
|
nsHttpTransaction::ReadSegments(nsAHttpSegmentReader *reader,
|
|
|
|
PRUint32 count, PRUint32 *countRead)
|
2001-05-11 21:04:09 +00:00
|
|
|
{
|
2004-09-21 16:00:50 +00:00
|
|
|
NS_ASSERTION(PR_GetCurrentThread() == gSocketThread, "wrong thread");
|
2001-05-11 21:04:09 +00:00
|
|
|
|
2003-03-25 02:23:11 +00:00
|
|
|
if (mTransactionDone) {
|
|
|
|
*countRead = 0;
|
|
|
|
return mStatus;
|
|
|
|
}
|
|
|
|
|
2003-01-18 02:15:14 +00:00
|
|
|
if (!mConnected) {
|
2011-10-17 14:59:28 +00:00
|
|
|
mConnected = true;
|
2003-01-18 02:15:14 +00:00
|
|
|
mConnection->GetSecurityInfo(getter_AddRefs(mSecurityInfo));
|
|
|
|
}
|
2001-05-11 21:04:09 +00:00
|
|
|
|
2003-01-18 02:15:14 +00:00
|
|
|
mReader = reader;
|
2001-05-11 21:04:09 +00:00
|
|
|
|
2003-01-18 02:15:14 +00:00
|
|
|
nsresult rv = mRequestStream->ReadSegments(ReadRequestSegment, this, count, countRead);
|
2001-05-11 21:04:09 +00:00
|
|
|
|
2003-01-18 02:15:14 +00:00
|
|
|
mReader = nsnull;
|
2003-10-08 06:10:47 +00: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;
|
2006-05-10 17:30:15 +00:00
|
|
|
gHttpHandler->GetSocketThreadTarget(getter_AddRefs(target));
|
2003-10-08 06:10:47 +00:00
|
|
|
if (target)
|
|
|
|
asyncIn->AsyncWait(this, 0, 0, target);
|
|
|
|
else {
|
|
|
|
NS_ERROR("no socket thread event target");
|
|
|
|
rv = NS_ERROR_UNEXPECTED;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-01-18 02:15:14 +00:00
|
|
|
return rv;
|
2001-05-11 21:04:09 +00:00
|
|
|
}
|
|
|
|
|
2003-01-18 02:15:14 +00:00
|
|
|
NS_METHOD
|
|
|
|
nsHttpTransaction::WritePipeSegment(nsIOutputStream *stream,
|
|
|
|
void *closure,
|
|
|
|
char *buf,
|
|
|
|
PRUint32 offset,
|
|
|
|
PRUint32 count,
|
|
|
|
PRUint32 *countWritten)
|
2001-05-11 21:04:09 +00:00
|
|
|
{
|
2003-01-18 02:15:14 +00:00
|
|
|
nsHttpTransaction *trans = (nsHttpTransaction *) closure;
|
|
|
|
|
|
|
|
if (trans->mTransactionDone)
|
2003-03-25 02:23:11 +00:00
|
|
|
return NS_BASE_STREAM_CLOSED; // stop iterating
|
2003-01-18 02:15:14 +00:00
|
|
|
|
2011-05-21 10:03:36 +00:00
|
|
|
if (trans->TimingEnabled() && trans->mTimings.responseStart.IsNull()) {
|
|
|
|
trans->mTimings.responseStart = mozilla::TimeStamp::Now();
|
|
|
|
}
|
|
|
|
|
2001-05-11 21:04:09 +00:00
|
|
|
nsresult rv;
|
2003-01-18 02:15:14 +00:00
|
|
|
//
|
|
|
|
// 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
|
2001-05-11 21:04:09 +00:00
|
|
|
|
2003-01-18 02:15:14 +00:00
|
|
|
NS_ASSERTION(*countWritten > 0, "bad writer");
|
2011-10-17 14:59:28 +00:00
|
|
|
trans->mReceivedData = true;
|
2001-05-11 21:04:09 +00:00
|
|
|
|
2011-06-23 21:41:45 +00:00
|
|
|
// Let the transaction "play" with the buffer. It is free to modify
|
2003-01-18 02:15:14 +00:00
|
|
|
// the contents of the buffer and/or modify countWritten.
|
2011-06-23 21:41:45 +00: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.
|
|
|
|
//
|
2003-01-18 02:15:14 +00:00
|
|
|
rv = trans->ProcessData(buf, *countWritten, countWritten);
|
|
|
|
if (NS_FAILED(rv))
|
|
|
|
trans->Close(rv);
|
2001-05-11 21:04:09 +00:00
|
|
|
|
2006-11-01 23:02:18 +00:00
|
|
|
return rv; // failure code only stops WriteSegments; it is not propagated.
|
2003-01-18 02:15:14 +00:00
|
|
|
}
|
2001-05-11 21:04:09 +00:00
|
|
|
|
2003-01-18 02:15:14 +00:00
|
|
|
nsresult
|
|
|
|
nsHttpTransaction::WriteSegments(nsAHttpSegmentWriter *writer,
|
|
|
|
PRUint32 count, PRUint32 *countWritten)
|
|
|
|
{
|
2004-09-21 16:00:50 +00:00
|
|
|
NS_ASSERTION(PR_GetCurrentThread() == gSocketThread, "wrong thread");
|
2001-05-11 21:04:09 +00:00
|
|
|
|
2003-01-18 02:15:14 +00:00
|
|
|
if (mTransactionDone)
|
2003-03-25 02:23:11 +00:00
|
|
|
return NS_SUCCEEDED(mStatus) ? NS_BASE_STREAM_CLOSED : mStatus;
|
2001-05-11 21:04:09 +00:00
|
|
|
|
2003-01-18 02:15:14 +00:00
|
|
|
mWriter = writer;
|
2001-05-25 23:18:50 +00:00
|
|
|
|
2003-01-18 02:15:14 +00:00
|
|
|
nsresult rv = mPipeOut->WriteSegments(WritePipeSegment, this, count, countWritten);
|
|
|
|
|
|
|
|
mWriter = nsnull;
|
|
|
|
|
2003-10-06 01:46:31 +00: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;
|
2006-05-10 17:30:15 +00:00
|
|
|
gHttpHandler->GetSocketThreadTarget(getter_AddRefs(target));
|
2003-10-06 01:46:31 +00:00
|
|
|
if (target)
|
|
|
|
mPipeOut->AsyncWait(this, 0, 0, target);
|
|
|
|
else {
|
|
|
|
NS_ERROR("no socket thread event target");
|
|
|
|
rv = NS_ERROR_UNEXPECTED;
|
|
|
|
}
|
|
|
|
}
|
2001-05-25 23:18:50 +00:00
|
|
|
|
2001-05-11 21:04:09 +00:00
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2003-01-18 02:15:14 +00:00
|
|
|
void
|
|
|
|
nsHttpTransaction::Close(nsresult reason)
|
2001-05-11 21:04:09 +00:00
|
|
|
{
|
2003-01-18 02:15:14 +00:00
|
|
|
LOG(("nsHttpTransaction::Close [this=%x reason=%x]\n", this, reason));
|
|
|
|
|
2004-09-21 16:00:50 +00:00
|
|
|
NS_ASSERTION(PR_GetCurrentThread() == gSocketThread, "wrong thread");
|
2003-01-18 02:15:14 +00:00
|
|
|
|
2003-03-25 02:23:11 +00:00
|
|
|
if (mClosed) {
|
2003-01-18 02:15:14 +00:00
|
|
|
LOG((" already closed\n"));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2006-05-19 22:50:02 +00: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 20:55:50 +00:00
|
|
|
PR_Now(),
|
2011-04-01 00:15:04 +00:00
|
|
|
static_cast<PRUint64>(mContentRead),
|
2006-05-19 22:50:02 +00:00
|
|
|
EmptyCString());
|
|
|
|
|
|
|
|
// report that this transaction is closing
|
|
|
|
mActivityDistributor->ObserveActivity(
|
|
|
|
mChannel,
|
|
|
|
NS_HTTP_ACTIVITY_TYPE_HTTP_TRANSACTION,
|
|
|
|
NS_HTTP_ACTIVITY_SUBTYPE_TRANSACTION_CLOSE,
|
2009-09-01 20:55:50 +00:00
|
|
|
PR_Now(), LL_ZERO, EmptyCString());
|
2006-05-19 22:50:02 +00:00
|
|
|
}
|
|
|
|
|
2003-02-09 20:13:46 +00:00
|
|
|
// we must no longer reference the connection! find out if the
|
|
|
|
// connection was being reused before letting it go.
|
2011-09-29 06:19:26 +00:00
|
|
|
bool connReused = false;
|
2003-03-26 05:05:49 +00:00
|
|
|
if (mConnection)
|
2003-02-09 20:13:46 +00:00
|
|
|
connReused = mConnection->IsReused();
|
2011-10-17 14:59:28 +00:00
|
|
|
mConnected = false;
|
2001-05-11 21:04:09 +00:00
|
|
|
|
2003-02-09 20:13:46 +00:00
|
|
|
//
|
2003-04-10 19:06:24 +00: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.
|
2003-02-09 20:13:46 +00:00
|
|
|
//
|
2003-04-10 19:06:24 +00:00
|
|
|
// 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.
|
|
|
|
//
|
2003-05-05 21:41:30 +00:00
|
|
|
// 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.)
|
|
|
|
//
|
2003-04-10 19:06:24 +00:00
|
|
|
if (reason == NS_ERROR_NET_RESET || reason == NS_OK) {
|
2003-05-05 21:41:30 +00:00
|
|
|
if (!mReceivedData && (!mSentData || connReused)) {
|
2003-04-10 19:06:24 +00:00
|
|
|
// if restarting fails, then we must proceed to close the pipe,
|
|
|
|
// which will notify the channel that the transaction failed.
|
|
|
|
if (NS_SUCCEEDED(Restart()))
|
|
|
|
return;
|
|
|
|
}
|
2001-09-21 03:59:02 +00:00
|
|
|
}
|
|
|
|
|
2011-09-29 06:19:26 +00:00
|
|
|
bool relConn = true;
|
2003-03-26 05:05:49 +00:00
|
|
|
if (NS_SUCCEEDED(reason)) {
|
2005-10-21 01:56:55 +00: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 12:42:06 +00:00
|
|
|
// response will be usable (see bug 88792).
|
2005-10-21 01:56:55 +00:00
|
|
|
if (!mHaveAllHeaders) {
|
|
|
|
char data = '\n';
|
|
|
|
PRUint32 unused;
|
|
|
|
ParseHead(&data, 1, &unused);
|
2011-09-03 12:42:06 +00: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;
|
|
|
|
}
|
2005-10-21 01:56:55 +00:00
|
|
|
}
|
2003-03-26 05:05:49 +00:00
|
|
|
|
|
|
|
// honor the sticky connection flag...
|
|
|
|
if (mCaps & NS_HTTP_STICKY_CONNECTION)
|
2011-10-17 14:59:28 +00:00
|
|
|
relConn = false;
|
2003-01-18 02:15:14 +00:00
|
|
|
}
|
2003-03-26 05:05:49 +00:00
|
|
|
if (relConn && mConnection)
|
|
|
|
NS_RELEASE(mConnection);
|
2001-05-30 00:40:50 +00:00
|
|
|
|
2003-01-18 02:15:14 +00:00
|
|
|
mStatus = reason;
|
2011-10-17 14:59:28 +00:00
|
|
|
mTransactionDone = true; // forcibly flag the transaction as complete
|
|
|
|
mClosed = true;
|
2001-05-11 21:04:09 +00:00
|
|
|
|
2003-03-26 05:05:49 +00:00
|
|
|
// release some resources that we no longer need
|
|
|
|
mRequestStream = nsnull;
|
|
|
|
mReqHeaderBuf.Truncate();
|
|
|
|
mLineBuf.Truncate();
|
|
|
|
if (mChunkedDecoder) {
|
|
|
|
delete mChunkedDecoder;
|
|
|
|
mChunkedDecoder = nsnull;
|
|
|
|
}
|
|
|
|
|
2004-10-15 05:59:21 +00:00
|
|
|
// closing this pipe triggers the channel's OnStopRequest method.
|
2003-10-06 01:46:31 +00:00
|
|
|
mPipeOut->CloseWithStatus(reason);
|
2001-09-14 21:08:58 +00:00
|
|
|
}
|
|
|
|
|
2001-09-04 23:11:46 +00:00
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
// nsHttpTransaction <private>
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
2001-09-21 03:59:02 +00:00
|
|
|
nsresult
|
|
|
|
nsHttpTransaction::Restart()
|
|
|
|
{
|
2003-01-18 02:15:14 +00:00
|
|
|
NS_ASSERTION(PR_GetCurrentThread() == gSocketThread, "wrong thread");
|
2001-09-21 03:59:02 +00:00
|
|
|
|
|
|
|
// limit the number of restart attempts - bug 92224
|
2003-01-18 02:15:14 +00:00
|
|
|
if (++mRestartCount >= gHttpHandler->MaxRequestAttempts()) {
|
2001-09-21 03:59:02 +00:00
|
|
|
LOG(("reached max request attempts, failing transaction @%x\n", this));
|
2002-03-22 21:30:41 +00:00
|
|
|
return NS_ERROR_NET_RESET;
|
2001-09-21 03:59:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
LOG(("restarting transaction @%x\n", this));
|
|
|
|
|
|
|
|
// rewind streams in case we already wrote out the request
|
2003-01-18 02:15:14 +00:00
|
|
|
nsCOMPtr<nsISeekableStream> seekable = do_QueryInterface(mRequestStream);
|
2001-11-14 06:45:27 +00:00
|
|
|
if (seekable)
|
|
|
|
seekable->Seek(nsISeekableStream::NS_SEEK_SET, 0);
|
2001-09-21 03:59:02 +00:00
|
|
|
|
2003-03-26 05:05:49 +00:00
|
|
|
// clear old connection state...
|
2003-01-18 02:15:14 +00:00
|
|
|
mSecurityInfo = 0;
|
2003-03-26 05:05:49 +00:00
|
|
|
NS_IF_RELEASE(mConnection);
|
2001-09-21 03:59:02 +00:00
|
|
|
|
2005-03-16 20:50:37 +00: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;
|
|
|
|
|
2005-01-15 07:01:20 +00:00
|
|
|
return gHttpHandler->InitiateTransaction(this, mPriority);
|
2001-09-21 03:59:02 +00:00
|
|
|
}
|
|
|
|
|
2010-12-01 00:02:52 +00:00
|
|
|
char *
|
|
|
|
nsHttpTransaction::LocateHttpStart(char *buf, PRUint32 len,
|
2011-09-29 06:19:26 +00:00
|
|
|
bool aAllowPartialMatch)
|
2010-12-01 00:02:52 +00:00
|
|
|
{
|
|
|
|
NS_ASSERTION(!aAllowPartialMatch || mLineBuf.IsEmpty(), "ouch");
|
|
|
|
|
|
|
|
static const char HTTPHeader[] = "HTTP/1.";
|
2011-03-24 03:48:52 +00:00
|
|
|
static const PRUint32 HTTPHeaderLen = sizeof(HTTPHeader) - 1;
|
2011-02-23 02:02:19 +00:00
|
|
|
static const char HTTP2Header[] = "HTTP/2.0";
|
|
|
|
static const PRUint32 HTTP2HeaderLen = sizeof(HTTP2Header) - 1;
|
2011-02-08 23:26:33 +00:00
|
|
|
|
|
|
|
if (aAllowPartialMatch && (len < HTTPHeaderLen))
|
|
|
|
return (PL_strncasecmp(buf, HTTPHeader, len) == 0) ? buf : nsnull;
|
2010-12-01 00:02:52 +00:00
|
|
|
|
|
|
|
// mLineBuf can contain partial match from previous search
|
|
|
|
if (!mLineBuf.IsEmpty()) {
|
|
|
|
NS_ASSERTION(mLineBuf.Length() < HTTPHeaderLen, "ouch");
|
2011-06-02 12:56:50 +00:00
|
|
|
PRInt32 checkChars = NS_MIN(len, HTTPHeaderLen - mLineBuf.Length());
|
2010-12-01 00:02:52 +00: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-24 03:31:28 +00:00
|
|
|
// Response matches pattern but is still incomplete.
|
|
|
|
return 0;
|
2010-12-01 00:02:52 +00:00
|
|
|
}
|
|
|
|
// Previous partial match together with new data doesn't match the
|
|
|
|
// pattern. Start the search again.
|
|
|
|
mLineBuf.Truncate();
|
|
|
|
}
|
|
|
|
|
2011-09-29 06:19:26 +00:00
|
|
|
bool firstByte = true;
|
2010-12-01 00:02:52 +00:00
|
|
|
while (len > 0) {
|
2011-06-02 12:56:50 +00:00
|
|
|
if (PL_strncasecmp(buf, HTTPHeader, NS_MIN<PRUint32>(len, HTTPHeaderLen)) == 0) {
|
2010-12-01 00:02:52 +00:00
|
|
|
if (len < HTTPHeaderLen) {
|
|
|
|
// partial HTTPHeader sequence found
|
2011-02-08 23:26:33 +00:00
|
|
|
// save partial match to mLineBuf
|
|
|
|
mLineBuf.Assign(buf, len);
|
|
|
|
return 0;
|
2010-12-01 00:02:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// whole HTTPHeader sequence found
|
|
|
|
return buf;
|
|
|
|
}
|
2011-02-23 02:02:19 +00: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;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!nsCRT::IsAsciiSpace(*buf))
|
2011-10-17 14:59:28 +00:00
|
|
|
firstByte = false;
|
2010-12-01 00:02:52 +00:00
|
|
|
buf++;
|
|
|
|
len--;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2011-05-31 23:51:51 +00:00
|
|
|
nsresult
|
2001-05-11 21:04:09 +00:00
|
|
|
nsHttpTransaction::ParseLine(char *line)
|
|
|
|
{
|
|
|
|
LOG(("nsHttpTransaction::ParseLine [%s]\n", line));
|
2011-05-31 23:51:51 +00:00
|
|
|
nsresult rv = NS_OK;
|
|
|
|
|
2001-05-11 21:04:09 +00:00
|
|
|
if (!mHaveStatusLine) {
|
|
|
|
mResponseHead->ParseStatusLine(line);
|
2011-10-17 14:59:28 +00:00
|
|
|
mHaveStatusLine = true;
|
2001-05-25 23:07:44 +00:00
|
|
|
// XXX this should probably never happen
|
2001-05-18 02:03:08 +00:00
|
|
|
if (mResponseHead->Version() == NS_HTTP_VERSION_0_9)
|
2011-10-17 14:59:28 +00:00
|
|
|
mHaveAllHeaders = true;
|
2001-05-11 21:04:09 +00:00
|
|
|
}
|
2011-05-31 23:51:51 +00:00
|
|
|
else {
|
|
|
|
rv = mResponseHead->ParseHeaderLine(line);
|
|
|
|
}
|
|
|
|
return rv;
|
2001-05-25 23:07:44 +00:00
|
|
|
}
|
|
|
|
|
2002-08-27 02:26:43 +00:00
|
|
|
nsresult
|
2001-05-25 23:07:44 +00:00
|
|
|
nsHttpTransaction::ParseLineSegment(char *segment, PRUint32 len)
|
|
|
|
{
|
|
|
|
NS_PRECONDITION(!mHaveAllHeaders, "already have all headers");
|
|
|
|
|
|
|
|
if (!mLineBuf.IsEmpty() && mLineBuf.Last() == '\n') {
|
2002-08-27 02:26:43 +00:00
|
|
|
// 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 23:51:51 +00:00
|
|
|
nsresult rv = ParseLine(mLineBuf.BeginWriting());
|
2002-08-27 02:26:43 +00:00
|
|
|
mLineBuf.Truncate();
|
2011-05-31 23:51:51 +00:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return rv;
|
|
|
|
}
|
2001-05-25 23:07:44 +00:00
|
|
|
}
|
|
|
|
}
|
2002-08-27 02:26:43 +00:00
|
|
|
|
|
|
|
// append segment to mLineBuf...
|
|
|
|
mLineBuf.Append(segment, len);
|
2001-08-01 22:56:50 +00:00
|
|
|
|
2001-05-25 23:07:44 +00:00
|
|
|
// a line buf with only a new line char signifies the end of headers.
|
|
|
|
if (mLineBuf.First() == '\n') {
|
|
|
|
mLineBuf.Truncate();
|
2003-11-04 02:20:54 +00:00
|
|
|
// discard this response if it is a 100 continue or other 1xx status.
|
2011-05-19 23:43:37 +00:00
|
|
|
PRUint16 status = mResponseHead->Status();
|
|
|
|
if ((status != 101) && (status / 100 == 1)) {
|
2003-11-04 02:20:54 +00:00
|
|
|
LOG(("ignoring 1xx response\n"));
|
2011-10-17 14:59:28 +00:00
|
|
|
mHaveStatusLine = false;
|
|
|
|
mHttpResponseMatched = false;
|
|
|
|
mConnection->SetLastTransactionExpectedNoContent(true);
|
2001-08-02 19:26:24 +00:00
|
|
|
mResponseHead->Reset();
|
2002-08-27 02:26:43 +00:00
|
|
|
return NS_OK;
|
2001-08-02 19:26:24 +00:00
|
|
|
}
|
2011-10-17 14:59:28 +00:00
|
|
|
mHaveAllHeaders = true;
|
2001-05-25 23:07:44 +00:00
|
|
|
}
|
2002-08-27 02:26:43 +00:00
|
|
|
return NS_OK;
|
2001-05-11 21:04:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsHttpTransaction::ParseHead(char *buf,
|
|
|
|
PRUint32 count,
|
|
|
|
PRUint32 *countRead)
|
|
|
|
{
|
2002-08-27 02:26:43 +00:00
|
|
|
nsresult rv;
|
2001-05-25 23:07:44 +00:00
|
|
|
PRUint32 len;
|
2001-05-11 21:04:09 +00:00
|
|
|
char *eol;
|
|
|
|
|
|
|
|
LOG(("nsHttpTransaction::ParseHead [count=%u]\n", count));
|
|
|
|
|
|
|
|
*countRead = 0;
|
|
|
|
|
|
|
|
NS_PRECONDITION(!mHaveAllHeaders, "oops");
|
2001-08-02 19:26:24 +00:00
|
|
|
|
|
|
|
// allocate the response head object if necessary
|
|
|
|
if (!mResponseHead) {
|
|
|
|
mResponseHead = new nsHttpResponseHead();
|
|
|
|
if (!mResponseHead)
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
2006-05-19 22:50:02 +00:00
|
|
|
|
|
|
|
// report that we have a least some of the response
|
|
|
|
if (mActivityDistributor)
|
|
|
|
mActivityDistributor->ObserveActivity(
|
|
|
|
mChannel,
|
|
|
|
NS_HTTP_ACTIVITY_TYPE_HTTP_TRANSACTION,
|
|
|
|
NS_HTTP_ACTIVITY_SUBTYPE_RESPONSE_START,
|
2009-09-01 20:55:50 +00:00
|
|
|
PR_Now(), LL_ZERO, EmptyCString());
|
2001-08-02 19:26:24 +00:00
|
|
|
}
|
2001-05-25 23:07:44 +00:00
|
|
|
|
2010-12-01 00:02:52 +00:00
|
|
|
if (!mHttpResponseMatched) {
|
2011-01-27 18:34:39 +00: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 14:59:28 +00:00
|
|
|
mHttpResponseMatched = true;
|
|
|
|
char *p = LocateHttpStart(buf, NS_MIN<PRUint32>(count, 11), true);
|
2010-12-01 00:02:52 +00:00
|
|
|
if (!p) {
|
|
|
|
// Treat any 0.9 style response of a put as a failure.
|
|
|
|
if (mRequestHead->Method() == nsHttp::Put)
|
|
|
|
return NS_ERROR_ABORT;
|
|
|
|
|
|
|
|
mResponseHead->ParseStatusLine("");
|
2011-10-17 14:59:28 +00:00
|
|
|
mHaveStatusLine = true;
|
|
|
|
mHaveAllHeaders = true;
|
2010-12-01 00:02:52 +00:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
if (p > buf) {
|
|
|
|
// skip over the junk
|
2011-01-27 18:34:39 +00:00
|
|
|
mInvalidResponseBytesRead += p - buf;
|
2010-12-01 00:02:52 +00:00
|
|
|
*countRead = p - buf;
|
|
|
|
buf = p;
|
|
|
|
}
|
2001-09-04 23:11:46 +00:00
|
|
|
}
|
2010-12-01 00:02:52 +00:00
|
|
|
else {
|
2011-10-17 14:59:28 +00:00
|
|
|
char *p = LocateHttpStart(buf, count, false);
|
2010-12-01 00:02:52 +00:00
|
|
|
if (p) {
|
2011-01-27 18:34:39 +00:00
|
|
|
mInvalidResponseBytesRead += p - buf;
|
2010-12-01 00:02:52 +00:00
|
|
|
*countRead = p - buf;
|
|
|
|
buf = p;
|
2011-10-17 14:59:28 +00:00
|
|
|
mHttpResponseMatched = true;
|
2010-12-01 00:02:52 +00:00
|
|
|
} else {
|
2011-01-27 18:34:39 +00:00
|
|
|
mInvalidResponseBytesRead += count;
|
2010-12-01 00:02:52 +00:00
|
|
|
*countRead = count;
|
2011-01-27 18:34:39 +00: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-12-01 00:02:52 +00:00
|
|
|
return NS_OK;
|
|
|
|
}
|
2001-09-04 23:11:46 +00:00
|
|
|
}
|
2001-05-25 23:07:44 +00:00
|
|
|
}
|
|
|
|
// otherwise we can assume that we don't have a HTTP/0.9 response.
|
2001-05-11 21:04:09 +00:00
|
|
|
|
2011-03-23 14:59:58 +00:00
|
|
|
NS_ABORT_IF_FALSE (mHttpResponseMatched, "inconsistent");
|
2007-07-08 07:08:04 +00:00
|
|
|
while ((eol = static_cast<char *>(memchr(buf, '\n', count - *countRead))) != nsnull) {
|
2001-05-11 21:04:09 +00:00
|
|
|
// found line in range [buf:eol]
|
2001-05-25 23:07:44 +00:00
|
|
|
len = eol - buf + 1;
|
2001-05-11 21:04:09 +00:00
|
|
|
|
2001-05-25 23:07:44 +00:00
|
|
|
*countRead += len;
|
|
|
|
|
|
|
|
// actually, the line is in the range [buf:eol-1]
|
2001-05-11 21:04:09 +00:00
|
|
|
if ((eol > buf) && (*(eol-1) == '\r'))
|
2001-05-25 23:07:44 +00:00
|
|
|
len--;
|
2001-05-11 21:04:09 +00:00
|
|
|
|
2001-05-25 23:07:44 +00:00
|
|
|
buf[len-1] = '\n';
|
2002-08-27 02:26:43 +00:00
|
|
|
rv = ParseLineSegment(buf, len);
|
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return rv;
|
2001-05-11 21:04:09 +00:00
|
|
|
|
2001-05-25 23:07:44 +00:00
|
|
|
if (mHaveAllHeaders)
|
|
|
|
return NS_OK;
|
2001-05-11 21:04:09 +00:00
|
|
|
|
|
|
|
// skip over line
|
|
|
|
buf = eol + 1;
|
2011-03-23 14:59:58 +00: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;
|
|
|
|
}
|
2001-05-11 21:04:09 +00:00
|
|
|
}
|
|
|
|
|
2001-05-25 23:07:44 +00:00
|
|
|
// do something about a partial header line
|
|
|
|
if (!mHaveAllHeaders && (len = count - *countRead)) {
|
2001-05-11 21:04:09 +00:00
|
|
|
*countRead = count;
|
2001-05-25 23:07:44 +00:00
|
|
|
// 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;
|
2002-08-27 02:26:43 +00:00
|
|
|
rv = ParseLineSegment(buf, len);
|
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return rv;
|
2001-05-11 21:04:09 +00:00
|
|
|
}
|
2001-08-02 19:26:24 +00:00
|
|
|
return NS_OK;
|
2001-05-11 21:04:09 +00:00
|
|
|
}
|
|
|
|
|
2001-06-14 20:52:03 +00:00
|
|
|
// called on the socket thread
|
2001-06-13 22:44:47 +00:00
|
|
|
nsresult
|
|
|
|
nsHttpTransaction::HandleContentStart()
|
|
|
|
{
|
2003-01-18 02:15:14 +00:00
|
|
|
LOG(("nsHttpTransaction::HandleContentStart [this=%x]\n", this));
|
2001-06-13 22:44:47 +00:00
|
|
|
|
|
|
|
if (mResponseHead) {
|
|
|
|
#if defined(PR_LOGGING)
|
2003-07-11 22:43:28 +00:00
|
|
|
if (LOG3_ENABLED()) {
|
|
|
|
LOG3(("http response [\n"));
|
2001-09-28 22:23:26 +00:00
|
|
|
nsCAutoString headers;
|
2011-10-17 14:59:28 +00:00
|
|
|
mResponseHead->Flatten(headers, false);
|
2001-09-28 22:23:26 +00:00
|
|
|
LogHeaders(headers.get());
|
2003-07-11 22:43:28 +00:00
|
|
|
LOG3(("]\n"));
|
2001-09-28 22:23:26 +00:00
|
|
|
}
|
2001-06-13 22:44:47 +00:00
|
|
|
#endif
|
|
|
|
// notify the connection, give it a chance to cause a reset.
|
2011-09-29 06:19:26 +00:00
|
|
|
bool reset = false;
|
2002-04-18 22:36:39 +00:00
|
|
|
mConnection->OnHeadersAvailable(this, mRequestHead, mResponseHead, &reset);
|
2001-06-13 22:44:47 +00:00
|
|
|
|
|
|
|
// looks like we should ignore this response, resetting...
|
|
|
|
if (reset) {
|
|
|
|
LOG(("resetting transaction's response head\n"));
|
2011-10-17 14:59:28 +00:00
|
|
|
mHaveAllHeaders = false;
|
|
|
|
mHaveStatusLine = false;
|
|
|
|
mReceivedData = false;
|
|
|
|
mSentData = false;
|
|
|
|
mHttpResponseMatched = false;
|
2001-06-13 22:44:47 +00:00
|
|
|
mResponseHead->Reset();
|
|
|
|
// wait to be called again...
|
2003-01-18 02:15:14 +00:00
|
|
|
return NS_OK;
|
2001-06-13 22:44:47 +00:00
|
|
|
}
|
|
|
|
|
2001-08-10 20:42:42 +00:00
|
|
|
// check if this is a no-content response
|
|
|
|
switch (mResponseHead->Status()) {
|
2011-05-19 23:43:37 +00:00
|
|
|
case 101:
|
2011-10-17 14:59:28 +00:00
|
|
|
mPreserveStream = true; // fall through to other no content
|
2001-08-10 20:42:42 +00:00
|
|
|
case 204:
|
|
|
|
case 205:
|
|
|
|
case 304:
|
2011-10-17 14:59:28 +00:00
|
|
|
mNoContent = true;
|
2001-08-10 20:42:42 +00:00
|
|
|
LOG(("this response should not contain a body.\n"));
|
|
|
|
break;
|
2001-06-13 22:44:47 +00:00
|
|
|
}
|
2011-01-27 18:34:39 +00:00
|
|
|
mConnection->SetLastTransactionExpectedNoContent(mNoContent);
|
2001-07-17 01:19:19 +00:00
|
|
|
|
2001-08-08 08:16:39 +00:00
|
|
|
if (mNoContent)
|
|
|
|
mContentLength = 0;
|
2001-08-10 20:42:42 +00:00
|
|
|
else {
|
|
|
|
// grab the content-length from the response headers
|
|
|
|
mContentLength = mResponseHead->ContentLength();
|
|
|
|
|
|
|
|
// 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.
|
2006-04-05 02:09:41 +00:00
|
|
|
// ignore chunked responses from HTTP/1.0 servers and proxies.
|
|
|
|
if (mResponseHead->Version() >= NS_HTTP_VERSION_1_1 &&
|
|
|
|
mResponseHead->HasHeaderValue(nsHttp::Transfer_Encoding, "chunked")) {
|
2001-08-10 20:42:42 +00:00
|
|
|
// 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"));
|
2001-12-20 21:13:25 +00:00
|
|
|
// Ignore server specified Content-Length.
|
|
|
|
mContentLength = -1;
|
2001-08-10 20:42:42 +00:00
|
|
|
}
|
|
|
|
#if defined(PR_LOGGING)
|
2011-04-01 00:15:27 +00:00
|
|
|
else if (mContentLength == PRInt64(-1))
|
2001-08-10 20:42:42 +00:00
|
|
|
LOG(("waiting for the server to close the connection.\n"));
|
|
|
|
#endif
|
|
|
|
}
|
2001-06-13 22:44:47 +00:00
|
|
|
}
|
|
|
|
|
2011-10-17 14:59:28 +00:00
|
|
|
mDidContentStart = true;
|
2003-01-18 02:15:14 +00:00
|
|
|
return NS_OK;
|
2001-06-13 22:44:47 +00:00
|
|
|
}
|
|
|
|
|
2001-06-14 20:52:03 +00:00
|
|
|
// called on the socket thread
|
2001-05-11 21:04:09 +00:00
|
|
|
nsresult
|
|
|
|
nsHttpTransaction::HandleContent(char *buf,
|
|
|
|
PRUint32 count,
|
2002-04-23 07:30:28 +00:00
|
|
|
PRUint32 *contentRead,
|
2002-06-17 21:08:46 +00:00
|
|
|
PRUint32 *contentRemaining)
|
2001-05-11 21:04:09 +00:00
|
|
|
{
|
|
|
|
nsresult rv;
|
|
|
|
|
2003-01-18 02:15:14 +00:00
|
|
|
LOG(("nsHttpTransaction::HandleContent [this=%x count=%u]\n", this, count));
|
2001-05-11 21:04:09 +00:00
|
|
|
|
2002-04-23 07:30:28 +00:00
|
|
|
*contentRead = 0;
|
2002-06-17 21:08:46 +00:00
|
|
|
*contentRemaining = 0;
|
2001-05-11 21:04:09 +00:00
|
|
|
|
2002-04-23 07:30:28 +00:00
|
|
|
NS_ASSERTION(mConnection, "no connection");
|
2001-05-11 21:04:09 +00:00
|
|
|
|
2003-01-18 02:15:14 +00:00
|
|
|
if (!mDidContentStart) {
|
2001-06-13 22:44:47 +00:00
|
|
|
rv = HandleContentStart();
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
2004-09-30 14:20:27 +00:00
|
|
|
// Do not write content to the pipe if we haven't started streaming yet
|
|
|
|
if (!mDidContentStart)
|
2005-05-10 06:09:45 +00:00
|
|
|
return NS_OK;
|
2001-05-11 21:04:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (mChunkedDecoder) {
|
|
|
|
// give the buf over to the chunked decoder so it can reformat the
|
|
|
|
// data and tell us how much is really there.
|
2002-06-17 21:08:46 +00:00
|
|
|
rv = mChunkedDecoder->HandleChunkedContent(buf, count, contentRead, contentRemaining);
|
2001-05-11 21:04:09 +00:00
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
}
|
2011-04-01 00:15:27 +00:00
|
|
|
else if (mContentLength >= PRInt64(0)) {
|
2001-08-10 20:42:42 +00:00
|
|
|
// HTTP/1.0 servers have been known to send erroneous Content-Length
|
2001-10-02 00:31:30 +00:00
|
|
|
// headers. So, unless the connection is persistent, we must make
|
2001-08-10 20:42:42 +00:00
|
|
|
// allowances for a possibly invalid Content-Length header. Thus, if
|
2001-10-02 00:31:30 +00:00
|
|
|
// NOT persistent, we simply accept everything in |buf|.
|
2011-05-19 23:43:37 +00:00
|
|
|
if (mConnection->IsPersistent() || mPreserveStream) {
|
2011-04-01 00:15:27 +00:00
|
|
|
PRInt64 remaining = mContentLength - mContentRead;
|
2011-06-25 14:06:02 +00:00
|
|
|
*contentRead = PRUint32(NS_MIN<PRInt64>(count, remaining));
|
2005-04-28 21:34:55 +00:00
|
|
|
*contentRemaining = count - *contentRead;
|
2001-08-14 01:03:27 +00:00
|
|
|
}
|
2001-08-10 20:42:42 +00:00
|
|
|
else {
|
2002-06-17 21:08:46 +00:00
|
|
|
*contentRead = count;
|
2001-08-10 20:42:42 +00:00
|
|
|
// mContentLength might need to be increased...
|
2011-04-01 00:15:27 +00:00
|
|
|
PRInt64 position = mContentRead + PRInt64(count);
|
2005-04-28 21:34:55 +00:00
|
|
|
if (position > mContentLength) {
|
|
|
|
mContentLength = position;
|
2001-08-11 20:01:53 +00:00
|
|
|
//mResponseHead->SetContentLength(mContentLength);
|
2001-08-10 20:42:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2002-04-23 07:30:28 +00:00
|
|
|
else {
|
2001-05-11 21:04:09 +00:00
|
|
|
// when we are just waiting for the server to close the connection...
|
2004-09-30 14:20:27 +00:00
|
|
|
// (no explicit content-length given)
|
2002-06-17 21:08:46 +00:00
|
|
|
*contentRead = count;
|
2002-04-23 07:30:28 +00:00
|
|
|
}
|
2001-05-11 21:04:09 +00:00
|
|
|
|
2002-04-23 07:30:28 +00:00
|
|
|
if (*contentRead) {
|
2001-06-14 20:52:03 +00:00
|
|
|
// update count of content bytes read and report progress...
|
2002-04-23 07:30:28 +00:00
|
|
|
mContentRead += *contentRead;
|
2011-06-25 14:06:02 +00:00
|
|
|
/* when uncommenting, take care of 64-bit integers w/ NS_MAX...
|
2001-09-14 21:08:58 +00:00
|
|
|
if (mProgressSink)
|
2011-06-02 12:56:50 +00:00
|
|
|
mProgressSink->OnProgress(nsnull, nsnull, mContentRead, NS_MAX(0, mContentLength));
|
2003-01-18 02:15:14 +00:00
|
|
|
*/
|
2001-05-11 21:04:09 +00:00
|
|
|
}
|
|
|
|
|
2004-09-30 14:20:27 +00:00
|
|
|
LOG(("nsHttpTransaction::HandleContent [this=%x count=%u read=%u mContentRead=%lld mContentLength=%lld]\n",
|
2011-04-01 00:15:04 +00:00
|
|
|
this, count, *contentRead, mContentRead, mContentLength));
|
2001-05-11 21:04:09 +00:00
|
|
|
|
|
|
|
// check for end-of-file
|
2004-06-16 19:51:21 +00:00
|
|
|
if ((mContentRead == mContentLength) ||
|
2001-05-11 21:04:09 +00:00
|
|
|
(mChunkedDecoder && mChunkedDecoder->ReachedEOF())) {
|
2003-01-18 02:15:14 +00:00
|
|
|
// the transaction is done with a complete response.
|
2011-10-17 14:59:28 +00:00
|
|
|
mTransactionDone = true;
|
|
|
|
mResponseIsComplete = true;
|
2006-05-19 22:50:02 +00:00
|
|
|
|
2011-08-21 08:27:29 +00:00
|
|
|
if (TimingEnabled())
|
|
|
|
mTimings.responseEnd = mozilla::TimeStamp::Now();
|
|
|
|
|
2006-05-19 22:50:02 +00: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 20:55:50 +00:00
|
|
|
PR_Now(),
|
2011-04-01 00:15:04 +00:00
|
|
|
static_cast<PRUint64>(mContentRead),
|
2006-05-19 22:50:02 +00:00
|
|
|
EmptyCString());
|
2003-01-18 02:15:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsHttpTransaction::ProcessData(char *buf, PRUint32 count, PRUint32 *countRead)
|
|
|
|
{
|
|
|
|
nsresult rv;
|
|
|
|
|
|
|
|
LOG(("nsHttpTransaction::ProcessData [this=%x count=%u]\n", this, count));
|
|
|
|
|
|
|
|
*countRead = 0;
|
|
|
|
|
|
|
|
// we may not have read all of the headers yet...
|
|
|
|
if (!mHaveAllHeaders) {
|
|
|
|
PRUint32 bytesConsumed = 0;
|
|
|
|
|
2011-03-23 14:59:58 +00:00
|
|
|
do {
|
|
|
|
PRUint32 localBytesConsumed = 0;
|
|
|
|
char *localBuf = buf + bytesConsumed;
|
|
|
|
PRUint32 localCount = count - bytesConsumed;
|
|
|
|
|
|
|
|
rv = ParseHead(localBuf, localCount, &localBytesConsumed);
|
|
|
|
if (NS_FAILED(rv) && rv != NS_ERROR_NET_INTERRUPT)
|
|
|
|
return rv;
|
|
|
|
bytesConsumed += localBytesConsumed;
|
|
|
|
} while (rv == NS_ERROR_NET_INTERRUPT);
|
|
|
|
|
2003-01-18 02:15:14 +00:00
|
|
|
count -= bytesConsumed;
|
|
|
|
|
|
|
|
// if buf has some content in it, shift bytes to top of buf.
|
|
|
|
if (count && bytesConsumed)
|
|
|
|
memmove(buf, buf + bytesConsumed, count);
|
2006-05-19 22:50:02 +00:00
|
|
|
|
|
|
|
// report the completed response header
|
|
|
|
if (mActivityDistributor && mResponseHead && mHaveAllHeaders) {
|
|
|
|
nsCAutoString completeResponseHeaders;
|
2011-10-17 14:59:28 +00:00
|
|
|
mResponseHead->Flatten(completeResponseHeaders, false);
|
2006-05-19 22:50:02 +00:00
|
|
|
completeResponseHeaders.AppendLiteral("\r\n");
|
|
|
|
mActivityDistributor->ObserveActivity(
|
|
|
|
mChannel,
|
|
|
|
NS_HTTP_ACTIVITY_TYPE_HTTP_TRANSACTION,
|
|
|
|
NS_HTTP_ACTIVITY_SUBTYPE_RESPONSE_HEADER,
|
2009-09-01 20:55:50 +00:00
|
|
|
PR_Now(), LL_ZERO,
|
2006-05-19 22:50:02 +00:00
|
|
|
completeResponseHeaders);
|
|
|
|
}
|
2003-01-18 02:15:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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) {
|
|
|
|
PRUint32 countRemaining = 0;
|
|
|
|
//
|
|
|
|
// buf layout:
|
|
|
|
//
|
|
|
|
// +--------------------------------------+----------------+-----+
|
|
|
|
// | 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) {
|
|
|
|
NS_ASSERTION(mConnection, "no connection");
|
|
|
|
mConnection->PushBack(buf + *countRead, countRemaining);
|
2001-05-11 21:04:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-01-18 02:15:14 +00:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
2003-10-06 01:46:31 +00:00
|
|
|
// nsHttpTransaction deletion event
|
2003-01-18 02:15:14 +00:00
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
2006-05-16 20:11:22 +00:00
|
|
|
class nsDeleteHttpTransaction : public nsRunnable {
|
|
|
|
public:
|
|
|
|
nsDeleteHttpTransaction(nsHttpTransaction *trans)
|
|
|
|
: mTrans(trans)
|
|
|
|
{}
|
|
|
|
|
|
|
|
NS_IMETHOD Run()
|
|
|
|
{
|
|
|
|
delete mTrans;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
private:
|
|
|
|
nsHttpTransaction *mTrans;
|
|
|
|
};
|
|
|
|
|
2001-06-11 21:20:29 +00:00
|
|
|
void
|
|
|
|
nsHttpTransaction::DeleteSelfOnConsumerThread()
|
2001-05-25 23:18:50 +00:00
|
|
|
{
|
2001-06-11 21:20:29 +00:00
|
|
|
LOG(("nsHttpTransaction::DeleteSelfOnConsumerThread [this=%x]\n", this));
|
2003-01-18 02:15:14 +00:00
|
|
|
|
2011-09-29 06:19:26 +00:00
|
|
|
bool val;
|
2011-06-11 14:56:36 +00:00
|
|
|
if (!mConsumerTarget ||
|
|
|
|
(NS_SUCCEEDED(mConsumerTarget->IsOnCurrentThread(&val)) && val)) {
|
2001-06-11 21:20:29 +00:00
|
|
|
delete this;
|
2011-06-11 14:56:36 +00:00
|
|
|
} else {
|
2001-06-11 21:20:29 +00:00
|
|
|
LOG(("proxying delete to consumer thread...\n"));
|
2006-05-16 20:11:22 +00:00
|
|
|
nsCOMPtr<nsIRunnable> event = new nsDeleteHttpTransaction(this);
|
|
|
|
if (NS_FAILED(mConsumerTarget->Dispatch(event, NS_DISPATCH_NORMAL)))
|
|
|
|
NS_WARNING("failed to dispatch nsHttpDeleteTransaction event");
|
2001-06-11 21:20:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2001-05-11 21:04:09 +00:00
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
// nsHttpTransaction::nsISupports
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
2001-06-11 21:20:29 +00:00
|
|
|
NS_IMPL_THREADSAFE_ADDREF(nsHttpTransaction)
|
|
|
|
|
|
|
|
NS_IMETHODIMP_(nsrefcnt)
|
|
|
|
nsHttpTransaction::Release()
|
|
|
|
{
|
|
|
|
nsrefcnt count;
|
|
|
|
NS_PRECONDITION(0 != mRefCnt, "dup release");
|
2011-03-28 19:58:49 +00:00
|
|
|
count = NS_AtomicDecrementRefcnt(mRefCnt);
|
2001-06-11 21:20:29 +00:00
|
|
|
NS_LOG_RELEASE(this, count, "nsHttpTransaction");
|
|
|
|
if (0 == count) {
|
|
|
|
mRefCnt = 1; /* stablize */
|
|
|
|
// it is essential that the transaction be destroyed on the consumer
|
|
|
|
// thread (we could be holding the last reference to our consumer).
|
|
|
|
DeleteSelfOnConsumerThread();
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return count;
|
|
|
|
}
|
|
|
|
|
2003-10-08 06:10:47 +00:00
|
|
|
NS_IMPL_THREADSAFE_QUERY_INTERFACE2(nsHttpTransaction,
|
|
|
|
nsIInputStreamCallback,
|
|
|
|
nsIOutputStreamCallback)
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
// nsHttpTransaction::nsIInputStreamCallback
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
// called on the socket thread
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsHttpTransaction::OnInputStreamReady(nsIAsyncInputStream *out)
|
|
|
|
{
|
|
|
|
if (mConnection) {
|
2012-01-26 05:15:26 +00:00
|
|
|
mConnection->TransactionHasDataToWrite(this);
|
|
|
|
nsresult rv = mConnection->ResumeSend();
|
2003-10-08 06:10:47 +00:00
|
|
|
if (NS_FAILED(rv))
|
|
|
|
NS_ERROR("ResumeSend failed");
|
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2001-05-11 21:04:09 +00:00
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
2003-10-06 01:46:31 +00:00
|
|
|
// nsHttpTransaction::nsIOutputStreamCallback
|
2001-05-11 21:04:09 +00:00
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
2003-10-06 01:46:31 +00:00
|
|
|
// called on the socket thread
|
2001-05-11 21:04:09 +00:00
|
|
|
NS_IMETHODIMP
|
2003-01-18 02:15:14 +00:00
|
|
|
nsHttpTransaction::OnOutputStreamReady(nsIAsyncOutputStream *out)
|
2001-05-11 21:04:09 +00:00
|
|
|
{
|
2003-01-18 02:15:14 +00:00
|
|
|
if (mConnection) {
|
2012-01-26 05:15:26 +00:00
|
|
|
nsresult rv = mConnection->ResumeRecv();
|
2003-10-08 06:10:47 +00:00
|
|
|
if (NS_FAILED(rv))
|
|
|
|
NS_ERROR("ResumeRecv failed");
|
2001-05-11 21:04:09 +00:00
|
|
|
}
|
2003-01-18 02:15:14 +00:00
|
|
|
return NS_OK;
|
2001-05-11 21:04:09 +00:00
|
|
|
}
|