2002-04-19 22:25:23 +00:00
|
|
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
|
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.
|
|
|
|
*
|
2002-04-19 22:25:23 +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):
|
2002-04-19 22:25:23 +00:00
|
|
|
* Darin Fisher <darin@netscape.com> (original author)
|
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 ***** */
|
2002-04-19 22:25:23 +00:00
|
|
|
|
2003-01-18 02:15:14 +00:00
|
|
|
#include <stdlib.h>
|
2002-04-19 22:25:23 +00:00
|
|
|
#include "nsHttp.h"
|
|
|
|
#include "nsHttpPipeline.h"
|
2003-01-18 02:15:14 +00:00
|
|
|
#include "nsHttpHandler.h"
|
2003-01-25 02:30:20 +00:00
|
|
|
#include "nsIOService.h"
|
2002-04-19 22:25:23 +00:00
|
|
|
#include "nsIRequest.h"
|
2003-01-18 02:15:14 +00:00
|
|
|
#include "nsISocketTransport.h"
|
2002-04-19 22:25:23 +00:00
|
|
|
#include "nsIStringStream.h"
|
|
|
|
#include "nsIPipe.h"
|
|
|
|
#include "nsCOMPtr.h"
|
|
|
|
#include "nsComponentManagerUtils.h"
|
|
|
|
|
|
|
|
#ifdef DEBUG
|
|
|
|
#include "prthread.h"
|
|
|
|
// defined by the socket transport service while active
|
2003-01-18 02:15:14 +00:00
|
|
|
extern PRThread *gSocketThread;
|
2002-04-19 22:25:23 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
2003-01-18 02:15:14 +00:00
|
|
|
// nsHttpPushBackWriter
|
2002-04-19 22:25:23 +00:00
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
2003-01-18 02:15:14 +00:00
|
|
|
class nsHttpPushBackWriter : public nsAHttpSegmentWriter
|
2002-04-19 22:25:23 +00:00
|
|
|
{
|
2003-01-18 02:15:14 +00:00
|
|
|
public:
|
|
|
|
nsHttpPushBackWriter(const char *buf, PRUint32 bufLen)
|
|
|
|
: mBuf(buf)
|
|
|
|
, mBufLen(bufLen)
|
|
|
|
{ }
|
|
|
|
virtual ~nsHttpPushBackWriter() {}
|
2002-04-19 22:25:23 +00:00
|
|
|
|
2003-01-18 02:15:14 +00:00
|
|
|
nsresult OnWriteSegment(char *buf, PRUint32 count, PRUint32 *countWritten)
|
|
|
|
{
|
|
|
|
if (mBufLen == 0)
|
|
|
|
return NS_BASE_STREAM_CLOSED;
|
2002-04-19 22:25:23 +00:00
|
|
|
|
2003-01-18 02:15:14 +00:00
|
|
|
if (count > mBufLen)
|
|
|
|
count = mBufLen;
|
2002-04-19 22:25:23 +00:00
|
|
|
|
2003-01-18 02:15:14 +00:00
|
|
|
memcpy(buf, mBuf, count);
|
2002-04-19 22:25:23 +00:00
|
|
|
|
2003-01-18 02:15:14 +00:00
|
|
|
mBuf += count;
|
|
|
|
mBufLen -= count;
|
|
|
|
*countWritten = count;
|
2002-04-19 22:25:23 +00:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2003-01-18 02:15:14 +00:00
|
|
|
private:
|
|
|
|
const char *mBuf;
|
|
|
|
PRUint32 mBufLen;
|
|
|
|
};
|
2002-04-19 22:25:23 +00:00
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
// nsHttpPipeline <public>
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
2012-03-22 23:39:31 +00:00
|
|
|
nsHttpPipeline::nsHttpPipeline()
|
|
|
|
: mConnection(nsnull)
|
2002-04-19 22:25:23 +00:00
|
|
|
, mStatus(NS_OK)
|
2011-10-17 14:59:28 +00:00
|
|
|
, mRequestIsPartial(false)
|
|
|
|
, mResponseIsPartial(false)
|
|
|
|
, mClosed(false)
|
2012-03-22 23:39:31 +00:00
|
|
|
, mUtilizedPipeline(false)
|
2003-01-18 02:15:14 +00:00
|
|
|
, mPushBackBuf(nsnull)
|
|
|
|
, mPushBackLen(0)
|
|
|
|
, mPushBackMax(0)
|
2011-12-13 15:55:50 +00:00
|
|
|
, mHttp1xTransactionCount(0)
|
2011-12-01 14:19:41 +00:00
|
|
|
, mReceivingFromProgress(0)
|
|
|
|
, mSendingToProgress(0)
|
|
|
|
, mSuppressSendEvents(true)
|
2002-04-19 22:25:23 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
nsHttpPipeline::~nsHttpPipeline()
|
|
|
|
{
|
2003-01-18 02:15:14 +00:00
|
|
|
// make sure we aren't still holding onto any transactions!
|
|
|
|
Close(NS_ERROR_ABORT);
|
2002-04-19 22:25:23 +00:00
|
|
|
|
2012-01-06 13:46:15 +00:00
|
|
|
NS_IF_RELEASE(mConnection);
|
|
|
|
|
2003-01-18 02:15:14 +00:00
|
|
|
if (mPushBackBuf)
|
|
|
|
free(mPushBackBuf);
|
2002-04-19 22:25:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2003-01-18 02:15:14 +00:00
|
|
|
nsHttpPipeline::AddTransaction(nsAHttpTransaction *trans)
|
2002-04-19 22:25:23 +00:00
|
|
|
{
|
2003-01-18 02:15:14 +00:00
|
|
|
LOG(("nsHttpPipeline::AddTransaction [this=%x trans=%x]\n", this, trans));
|
2002-04-19 22:25:23 +00:00
|
|
|
|
2012-03-22 23:39:31 +00:00
|
|
|
if (mRequestQ.Length() || mResponseQ.Length())
|
|
|
|
mUtilizedPipeline = true;
|
|
|
|
|
2003-01-18 02:15:14 +00:00
|
|
|
NS_ADDREF(trans);
|
|
|
|
mRequestQ.AppendElement(trans);
|
2012-03-22 23:39:31 +00:00
|
|
|
PRUint32 qlen = PipelineDepth();
|
2012-03-22 23:39:31 +00:00
|
|
|
|
|
|
|
if (qlen != 1) {
|
|
|
|
trans->SetPipelinePosition(qlen);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
// do it for this case in case an idempotent cancellation
|
|
|
|
// is being repeated and an old value needs to be cleared
|
|
|
|
trans->SetPipelinePosition(0);
|
|
|
|
}
|
2002-04-19 22:25:23 +00:00
|
|
|
|
2012-03-22 23:39:31 +00:00
|
|
|
// trans->SetConnection() needs to be updated to point back at
|
|
|
|
// the pipeline object.
|
|
|
|
trans->SetConnection(this);
|
|
|
|
|
|
|
|
if (mConnection && !mClosed && mRequestQ.Length() == 1)
|
|
|
|
mConnection->ResumeSend();
|
2002-04-19 22:25:23 +00:00
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2012-03-22 23:39:31 +00:00
|
|
|
PRUint32
|
|
|
|
nsHttpPipeline::PipelineDepth()
|
2012-03-22 23:39:31 +00:00
|
|
|
{
|
2012-03-22 23:39:31 +00:00
|
|
|
return mRequestQ.Length() + mResponseQ.Length();
|
2012-03-22 23:39:31 +00:00
|
|
|
}
|
|
|
|
|
2012-03-22 23:39:31 +00:00
|
|
|
nsresult
|
|
|
|
nsHttpPipeline::SetPipelinePosition(PRInt32 position)
|
|
|
|
{
|
|
|
|
nsAHttpTransaction *trans = Response(0);
|
|
|
|
if (trans)
|
|
|
|
return trans->SetPipelinePosition(position);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
PRInt32
|
|
|
|
nsHttpPipeline::PipelinePosition()
|
|
|
|
{
|
|
|
|
nsAHttpTransaction *trans = Response(0);
|
|
|
|
if (trans)
|
|
|
|
return trans->PipelinePosition();
|
2012-04-03 00:36:11 +00:00
|
|
|
|
|
|
|
// The response queue is empty, so return oldest request
|
|
|
|
if (mRequestQ.Length())
|
|
|
|
return Request(mRequestQ.Length() - 1)->PipelinePosition();
|
|
|
|
|
|
|
|
// No transactions in the pipeline
|
|
|
|
return 0;
|
2012-03-22 23:39:31 +00:00
|
|
|
}
|
|
|
|
|
2012-03-22 23:39:31 +00:00
|
|
|
nsHttpPipeline *
|
|
|
|
nsHttpPipeline::QueryPipeline()
|
|
|
|
{
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
2002-04-19 22:25:23 +00:00
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
// nsHttpPipeline::nsISupports
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
NS_IMPL_THREADSAFE_ADDREF(nsHttpPipeline)
|
|
|
|
NS_IMPL_THREADSAFE_RELEASE(nsHttpPipeline)
|
|
|
|
|
|
|
|
// multiple inheritance fun :-)
|
|
|
|
NS_INTERFACE_MAP_BEGIN(nsHttpPipeline)
|
|
|
|
NS_INTERFACE_MAP_ENTRY_AMBIGUOUS(nsISupports, nsAHttpConnection)
|
|
|
|
NS_INTERFACE_MAP_END
|
|
|
|
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
// nsHttpPipeline::nsAHttpConnection
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsHttpPipeline::OnHeadersAvailable(nsAHttpTransaction *trans,
|
|
|
|
nsHttpRequestHead *requestHead,
|
|
|
|
nsHttpResponseHead *responseHead,
|
2011-09-29 06:19:26 +00:00
|
|
|
bool *reset)
|
2002-04-19 22:25:23 +00:00
|
|
|
{
|
|
|
|
LOG(("nsHttpPipeline::OnHeadersAvailable [this=%x]\n", this));
|
|
|
|
|
2003-01-18 02:15:14 +00:00
|
|
|
NS_ASSERTION(PR_GetCurrentThread() == gSocketThread, "wrong thread");
|
2002-04-19 22:25:23 +00:00
|
|
|
NS_ASSERTION(mConnection, "no connection");
|
2012-03-22 23:39:31 +00:00
|
|
|
|
|
|
|
nsRefPtr<nsHttpConnectionInfo> ci;
|
|
|
|
GetConnectionInfo(getter_AddRefs(ci));
|
2003-01-18 02:15:14 +00:00
|
|
|
|
2012-03-22 23:39:31 +00:00
|
|
|
NS_ABORT_IF_FALSE(ci, "no connection info");
|
|
|
|
|
2012-03-22 23:39:31 +00:00
|
|
|
bool pipeliningBefore = gHttpHandler->ConnMgr()->SupportsPipelining(ci);
|
2012-03-22 23:39:31 +00:00
|
|
|
|
2002-04-19 22:25:23 +00:00
|
|
|
// trans has now received its response headers; forward to the real connection
|
2012-03-22 23:39:31 +00:00
|
|
|
nsresult rv = mConnection->OnHeadersAvailable(trans,
|
|
|
|
requestHead,
|
|
|
|
responseHead,
|
|
|
|
reset);
|
|
|
|
|
2012-03-22 23:39:31 +00:00
|
|
|
if (!pipeliningBefore && gHttpHandler->ConnMgr()->SupportsPipelining(ci))
|
2012-03-22 23:39:31 +00:00
|
|
|
// The received headers have expanded the eligible
|
|
|
|
// pipeline depth for this connection
|
2012-03-22 23:39:31 +00:00
|
|
|
gHttpHandler->ConnMgr()->ProcessPendingQForEntry(ci);
|
2012-03-22 23:39:31 +00:00
|
|
|
|
|
|
|
return rv;
|
2002-04-19 22:25:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2012-01-26 05:15:26 +00:00
|
|
|
nsHttpPipeline::ResumeSend()
|
2002-04-19 22:25:23 +00:00
|
|
|
{
|
2011-11-15 15:00:46 +00:00
|
|
|
if (mConnection)
|
2012-01-26 05:15:26 +00:00
|
|
|
return mConnection->ResumeSend();
|
2003-01-18 02:15:14 +00:00
|
|
|
return NS_ERROR_UNEXPECTED;
|
2002-04-19 22:25:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2012-01-26 05:15:26 +00:00
|
|
|
nsHttpPipeline::ResumeRecv()
|
2002-04-19 22:25:23 +00:00
|
|
|
{
|
2003-01-18 02:15:14 +00:00
|
|
|
NS_ASSERTION(PR_GetCurrentThread() == gSocketThread, "wrong thread");
|
2002-04-19 22:25:23 +00:00
|
|
|
NS_ASSERTION(mConnection, "no connection");
|
2012-01-26 05:15:26 +00:00
|
|
|
return mConnection->ResumeRecv();
|
2002-04-19 22:25:23 +00:00
|
|
|
}
|
|
|
|
|
2003-01-18 02:15:14 +00:00
|
|
|
void
|
|
|
|
nsHttpPipeline::CloseTransaction(nsAHttpTransaction *trans, nsresult reason)
|
2002-04-19 22:25:23 +00:00
|
|
|
{
|
2003-01-18 02:15:14 +00:00
|
|
|
LOG(("nsHttpPipeline::CloseTransaction [this=%x trans=%x reason=%x]\n",
|
|
|
|
this, trans, reason));
|
2002-04-19 22:25:23 +00:00
|
|
|
|
2012-03-22 23:39:31 +00:00
|
|
|
NS_ABORT_IF_FALSE(PR_GetCurrentThread() == gSocketThread, "wrong thread");
|
2003-01-18 02:15:14 +00:00
|
|
|
NS_ASSERTION(NS_FAILED(reason), "expecting failure code");
|
|
|
|
|
|
|
|
// the specified transaction is to be closed with the given "reason"
|
|
|
|
|
|
|
|
PRInt32 index;
|
2011-09-29 06:19:26 +00:00
|
|
|
bool killPipeline = false;
|
2003-01-18 02:15:14 +00:00
|
|
|
|
|
|
|
index = mRequestQ.IndexOf(trans);
|
|
|
|
if (index >= 0) {
|
|
|
|
if (index == 0 && mRequestIsPartial) {
|
|
|
|
// the transaction is in the request queue. check to see if any of
|
|
|
|
// its data has been written out yet.
|
2011-10-17 14:59:28 +00:00
|
|
|
killPipeline = true;
|
2003-01-18 02:15:14 +00:00
|
|
|
}
|
|
|
|
mRequestQ.RemoveElementAt(index);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
index = mResponseQ.IndexOf(trans);
|
|
|
|
if (index >= 0)
|
|
|
|
mResponseQ.RemoveElementAt(index);
|
|
|
|
// while we could avoid killing the pipeline if this transaction is the
|
|
|
|
// last transaction in the pipeline, there doesn't seem to be that much
|
|
|
|
// value in doing so. most likely if this transaction is going away,
|
|
|
|
// the others will be shortly as well.
|
2011-10-17 14:59:28 +00:00
|
|
|
killPipeline = true;
|
2003-01-18 02:15:14 +00:00
|
|
|
}
|
|
|
|
|
2012-03-22 23:39:31 +00:00
|
|
|
// Marking this connection as non-reusable prevents other items from being
|
|
|
|
// added to it and causes it to be torn down soon. Don't tear it down yet
|
|
|
|
// as that would prevent Response(0) from being processed.
|
|
|
|
DontReuse();
|
|
|
|
|
2003-01-18 02:15:14 +00:00
|
|
|
trans->Close(reason);
|
|
|
|
NS_RELEASE(trans);
|
|
|
|
|
2012-04-09 14:21:17 +00:00
|
|
|
if (killPipeline) {
|
2012-03-22 23:39:31 +00:00
|
|
|
// reschedule anything from this pipeline onto a different connection
|
|
|
|
CancelPipeline(reason);
|
2012-04-09 14:21:17 +00:00
|
|
|
}
|
2002-04-19 22:25:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nsHttpPipeline::GetConnectionInfo(nsHttpConnectionInfo **result)
|
|
|
|
{
|
2012-03-22 23:39:31 +00:00
|
|
|
if (!mConnection) {
|
|
|
|
*result = nsnull;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2002-04-19 22:25:23 +00:00
|
|
|
mConnection->GetConnectionInfo(result);
|
|
|
|
}
|
|
|
|
|
2011-05-19 23:43:37 +00:00
|
|
|
nsresult
|
|
|
|
nsHttpPipeline::TakeTransport(nsISocketTransport **aTransport,
|
|
|
|
nsIAsyncInputStream **aInputStream,
|
|
|
|
nsIAsyncOutputStream **aOutputStream)
|
|
|
|
{
|
|
|
|
return mConnection->TakeTransport(aTransport, aInputStream, aOutputStream);
|
|
|
|
}
|
|
|
|
|
2002-04-19 22:25:23 +00:00
|
|
|
void
|
2003-01-18 02:15:14 +00:00
|
|
|
nsHttpPipeline::GetSecurityInfo(nsISupports **result)
|
2002-04-19 22:25:23 +00:00
|
|
|
{
|
|
|
|
NS_ASSERTION(mConnection, "no connection");
|
2003-01-18 02:15:14 +00:00
|
|
|
mConnection->GetSecurityInfo(result);
|
2002-04-19 22:25:23 +00:00
|
|
|
}
|
|
|
|
|
2011-09-29 06:19:26 +00:00
|
|
|
bool
|
2003-03-26 05:05:49 +00:00
|
|
|
nsHttpPipeline::IsPersistent()
|
|
|
|
{
|
2011-10-17 14:59:28 +00:00
|
|
|
return true; // pipelining requires this
|
2003-03-26 05:05:49 +00:00
|
|
|
}
|
|
|
|
|
2011-09-29 06:19:26 +00:00
|
|
|
bool
|
2003-03-26 05:05:49 +00:00
|
|
|
nsHttpPipeline::IsReused()
|
|
|
|
{
|
2012-03-22 23:39:31 +00:00
|
|
|
if (!mUtilizedPipeline && mConnection)
|
|
|
|
return mConnection->IsReused();
|
|
|
|
return true;
|
2003-03-26 05:05:49 +00:00
|
|
|
}
|
|
|
|
|
2012-03-22 23:39:31 +00:00
|
|
|
void
|
|
|
|
nsHttpPipeline::DontReuse()
|
|
|
|
{
|
|
|
|
if (mConnection)
|
|
|
|
mConnection->DontReuse();
|
|
|
|
}
|
|
|
|
|
2002-04-19 22:25:23 +00:00
|
|
|
nsresult
|
|
|
|
nsHttpPipeline::PushBack(const char *data, PRUint32 length)
|
|
|
|
{
|
|
|
|
LOG(("nsHttpPipeline::PushBack [this=%x len=%u]\n", this, length));
|
2003-01-18 02:15:14 +00:00
|
|
|
|
2012-03-22 23:39:31 +00:00
|
|
|
NS_ABORT_IF_FALSE(PR_GetCurrentThread() == gSocketThread, "wrong thread");
|
2003-01-18 02:15:14 +00:00
|
|
|
NS_ASSERTION(mPushBackLen == 0, "push back buffer already has data!");
|
|
|
|
|
2011-05-19 23:43:37 +00:00
|
|
|
// If we have no chance for a pipeline (e.g. due to an Upgrade)
|
|
|
|
// then push this data down to original connection
|
|
|
|
if (!mConnection->IsPersistent())
|
|
|
|
return mConnection->PushBack(data, length);
|
|
|
|
|
2003-01-18 02:15:14 +00:00
|
|
|
// PushBack is called recursively from WriteSegments
|
|
|
|
|
|
|
|
// XXX we have a design decision to make here. either we buffer the data
|
|
|
|
// and process it when we return to WriteSegments, or we attempt to move
|
|
|
|
// onto the next transaction from here. doing so adds complexity with the
|
|
|
|
// benefit of eliminating the extra buffer copy. the buffer is at most
|
|
|
|
// 4096 bytes, so it is really unclear if there is any value in the added
|
|
|
|
// complexity. besides simplicity, buffering this data has the advantage
|
|
|
|
// that we'll call close on the transaction sooner, which will wake up
|
|
|
|
// the HTTP channel sooner to continue with its work.
|
|
|
|
|
|
|
|
if (!mPushBackBuf) {
|
|
|
|
mPushBackMax = length;
|
|
|
|
mPushBackBuf = (char *) malloc(mPushBackMax);
|
|
|
|
if (!mPushBackBuf)
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
}
|
|
|
|
else if (length > mPushBackMax) {
|
|
|
|
// grow push back buffer as necessary.
|
2010-04-20 16:32:28 +00:00
|
|
|
NS_ASSERTION(length <= nsIOService::gDefaultSegmentSize, "too big");
|
2003-01-18 02:15:14 +00:00
|
|
|
mPushBackMax = length;
|
|
|
|
mPushBackBuf = (char *) realloc(mPushBackBuf, mPushBackMax);
|
|
|
|
if (!mPushBackBuf)
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
}
|
|
|
|
|
|
|
|
memcpy(mPushBackBuf, data, length);
|
|
|
|
mPushBackLen = length;
|
2002-04-19 22:25:23 +00:00
|
|
|
|
2003-01-18 02:15:14 +00:00
|
|
|
return NS_OK;
|
2002-04-19 22:25:23 +00:00
|
|
|
}
|
|
|
|
|
2012-03-22 23:39:31 +00:00
|
|
|
bool
|
|
|
|
nsHttpPipeline::IsProxyConnectInProgress()
|
|
|
|
{
|
|
|
|
NS_ABORT_IF_FALSE(mConnection, "no connection");
|
|
|
|
return mConnection->IsProxyConnectInProgress();
|
|
|
|
}
|
|
|
|
|
2011-09-29 06:19:26 +00:00
|
|
|
bool
|
2011-01-27 18:34:39 +00:00
|
|
|
nsHttpPipeline::LastTransactionExpectedNoContent()
|
|
|
|
{
|
|
|
|
NS_ABORT_IF_FALSE(mConnection, "no connection");
|
|
|
|
return mConnection->LastTransactionExpectedNoContent();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2011-09-29 06:19:26 +00:00
|
|
|
nsHttpPipeline::SetLastTransactionExpectedNoContent(bool val)
|
2011-01-27 18:34:39 +00:00
|
|
|
{
|
|
|
|
NS_ABORT_IF_FALSE(mConnection, "no connection");
|
|
|
|
mConnection->SetLastTransactionExpectedNoContent(val);
|
|
|
|
}
|
|
|
|
|
2011-04-08 18:37:16 +00:00
|
|
|
nsHttpConnection *
|
|
|
|
nsHttpPipeline::TakeHttpConnection()
|
|
|
|
{
|
|
|
|
if (mConnection)
|
|
|
|
return mConnection->TakeHttpConnection();
|
|
|
|
return nsnull;
|
|
|
|
}
|
|
|
|
|
2011-12-01 14:19:41 +00:00
|
|
|
nsISocketTransport *
|
|
|
|
nsHttpPipeline::Transport()
|
|
|
|
{
|
|
|
|
if (!mConnection)
|
|
|
|
return nsnull;
|
|
|
|
return mConnection->Transport();
|
|
|
|
}
|
|
|
|
|
2012-03-22 23:39:31 +00:00
|
|
|
nsAHttpTransaction::Classifier
|
|
|
|
nsHttpPipeline::Classification()
|
|
|
|
{
|
|
|
|
if (mConnection)
|
|
|
|
return mConnection->Classification();
|
|
|
|
|
|
|
|
LOG(("nsHttpPipeline::Classification this=%p "
|
|
|
|
"has null mConnection using CLASS_SOLO default", this));
|
|
|
|
return nsAHttpTransaction::CLASS_SOLO;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nsHttpPipeline::Classify(nsAHttpTransaction::Classifier newclass)
|
|
|
|
{
|
|
|
|
if (mConnection)
|
|
|
|
mConnection->Classify(newclass);
|
|
|
|
}
|
|
|
|
|
2011-04-08 18:37:16 +00:00
|
|
|
void
|
|
|
|
nsHttpPipeline::SetSSLConnectFailed()
|
|
|
|
{
|
|
|
|
nsAHttpTransaction *trans = Request(0);
|
|
|
|
|
|
|
|
if (trans)
|
|
|
|
trans->SetSSLConnectFailed();
|
|
|
|
}
|
|
|
|
|
|
|
|
nsHttpRequestHead *
|
|
|
|
nsHttpPipeline::RequestHead()
|
|
|
|
{
|
|
|
|
nsAHttpTransaction *trans = Request(0);
|
|
|
|
|
|
|
|
if (trans)
|
|
|
|
return trans->RequestHead();
|
|
|
|
return nsnull;
|
|
|
|
}
|
|
|
|
|
2011-12-13 15:55:50 +00:00
|
|
|
PRUint32
|
|
|
|
nsHttpPipeline::Http1xTransactionCount()
|
|
|
|
{
|
|
|
|
return mHttp1xTransactionCount;
|
|
|
|
}
|
|
|
|
|
2012-02-15 21:41:18 +00:00
|
|
|
nsresult
|
|
|
|
nsHttpPipeline::TakeSubTransactions(
|
|
|
|
nsTArray<nsRefPtr<nsAHttpTransaction> > &outTransactions)
|
|
|
|
{
|
|
|
|
LOG(("nsHttpPipeline::TakeSubTransactions [this=%p]\n", this));
|
|
|
|
|
|
|
|
if (mResponseQ.Length() || mRequestIsPartial)
|
|
|
|
return NS_ERROR_ALREADY_OPENED;
|
|
|
|
|
|
|
|
PRInt32 i, count = mRequestQ.Length();
|
|
|
|
for (i = 0; i < count; ++i) {
|
|
|
|
nsAHttpTransaction *trans = Request(i);
|
|
|
|
outTransactions.AppendElement(trans);
|
|
|
|
NS_RELEASE(trans);
|
|
|
|
}
|
|
|
|
mRequestQ.Clear();
|
|
|
|
|
|
|
|
LOG((" took %d\n", count));
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2002-04-19 22:25:23 +00:00
|
|
|
//-----------------------------------------------------------------------------
|
2012-03-22 23:39:31 +00:00
|
|
|
// nsHttpPipeline::nsAHttpTransaction
|
2002-04-19 22:25:23 +00:00
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
void
|
|
|
|
nsHttpPipeline::SetConnection(nsAHttpConnection *conn)
|
|
|
|
{
|
|
|
|
LOG(("nsHttpPipeline::SetConnection [this=%x conn=%x]\n", this, conn));
|
|
|
|
|
2003-01-18 02:15:14 +00:00
|
|
|
NS_ASSERTION(PR_GetCurrentThread() == gSocketThread, "wrong thread");
|
2002-04-19 22:25:23 +00:00
|
|
|
NS_ASSERTION(!mConnection, "already have a connection");
|
2003-01-18 02:15:14 +00:00
|
|
|
|
2002-04-19 22:25:23 +00:00
|
|
|
NS_IF_ADDREF(mConnection = conn);
|
|
|
|
}
|
|
|
|
|
2011-12-13 15:55:50 +00:00
|
|
|
nsAHttpConnection *
|
|
|
|
nsHttpPipeline::Connection()
|
|
|
|
{
|
|
|
|
LOG(("nsHttpPipeline::Connection [this=%x conn=%x]\n", this, mConnection));
|
|
|
|
|
|
|
|
NS_ASSERTION(PR_GetCurrentThread() == gSocketThread, "wrong thread");
|
|
|
|
return mConnection;
|
|
|
|
}
|
|
|
|
|
2002-04-19 22:25:23 +00:00
|
|
|
void
|
2011-03-31 19:38:30 +00:00
|
|
|
nsHttpPipeline::GetSecurityCallbacks(nsIInterfaceRequestor **result,
|
|
|
|
nsIEventTarget **target)
|
2002-04-19 22:25:23 +00:00
|
|
|
{
|
2003-01-18 02:15:14 +00:00
|
|
|
NS_ASSERTION(PR_GetCurrentThread() == gSocketThread, "wrong thread");
|
2002-04-19 22:25:23 +00:00
|
|
|
|
2012-03-22 23:39:31 +00:00
|
|
|
// depending on timing this could be either the request or the response
|
|
|
|
// that is needed - but they both go to the same host. A request for these
|
|
|
|
// callbacks directly in nsHttpTransaction would not make a distinction
|
|
|
|
// over whether the the request had been transmitted yet.
|
2003-01-18 02:15:14 +00:00
|
|
|
nsAHttpTransaction *trans = Request(0);
|
2012-03-22 23:39:31 +00:00
|
|
|
if (!trans)
|
|
|
|
trans = Response(0);
|
2003-01-18 02:15:14 +00:00
|
|
|
if (trans)
|
2011-03-31 19:38:30 +00:00
|
|
|
trans->GetSecurityCallbacks(result, target);
|
|
|
|
else {
|
2003-01-18 02:15:14 +00:00
|
|
|
*result = nsnull;
|
2011-03-31 19:38:30 +00:00
|
|
|
if (target)
|
|
|
|
*target = nsnull;
|
|
|
|
}
|
2002-04-19 22:25:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2011-04-10 17:33:08 +00:00
|
|
|
nsHttpPipeline::OnTransportStatus(nsITransport* transport,
|
|
|
|
nsresult status, PRUint64 progress)
|
2002-04-19 22:25:23 +00:00
|
|
|
{
|
2005-01-09 20:43:35 +00:00
|
|
|
LOG(("nsHttpPipeline::OnStatus [this=%x status=%x progress=%llu]\n",
|
2003-01-18 02:15:14 +00:00
|
|
|
this, status, progress));
|
2002-04-19 22:25:23 +00:00
|
|
|
|
2003-01-18 02:15:14 +00:00
|
|
|
NS_ASSERTION(PR_GetCurrentThread() == gSocketThread, "wrong thread");
|
|
|
|
|
|
|
|
nsAHttpTransaction *trans;
|
2011-12-01 14:19:41 +00:00
|
|
|
PRInt32 i, count;
|
|
|
|
|
2003-01-18 02:15:14 +00:00
|
|
|
switch (status) {
|
2011-12-01 14:19:41 +00:00
|
|
|
|
|
|
|
case NS_NET_STATUS_RESOLVING_HOST:
|
|
|
|
case NS_NET_STATUS_RESOLVED_HOST:
|
|
|
|
case NS_NET_STATUS_CONNECTING_TO:
|
|
|
|
case NS_NET_STATUS_CONNECTED_TO:
|
|
|
|
// These should only appear at most once per pipeline.
|
|
|
|
// Deliver to the first transaction.
|
|
|
|
|
|
|
|
trans = Request(0);
|
|
|
|
if (!trans)
|
|
|
|
trans = Response(0);
|
2003-01-18 02:15:14 +00:00
|
|
|
if (trans)
|
2011-04-10 17:33:08 +00:00
|
|
|
trans->OnTransportStatus(transport, status, progress);
|
2011-12-01 14:19:41 +00:00
|
|
|
|
2003-01-18 02:15:14 +00:00
|
|
|
break;
|
2011-12-01 14:19:41 +00:00
|
|
|
|
|
|
|
case NS_NET_STATUS_SENDING_TO:
|
|
|
|
// This is generated by the socket transport when (part) of
|
|
|
|
// a transaction is written out
|
|
|
|
//
|
|
|
|
// In pipelining this is generated out of FillSendBuf(), but it cannot do
|
|
|
|
// so until the connection is confirmed by CONNECTED_TO.
|
|
|
|
// See patch for bug 196827.
|
|
|
|
//
|
|
|
|
|
|
|
|
if (mSuppressSendEvents) {
|
|
|
|
mSuppressSendEvents = false;
|
|
|
|
|
|
|
|
// catch up by sending the event to all the transactions that have
|
|
|
|
// moved from request to response and any that have been partially
|
|
|
|
// sent. Also send WAITING_FOR to those that were completely sent
|
|
|
|
count = mResponseQ.Length();
|
|
|
|
for (i = 0; i < count; ++i) {
|
|
|
|
Response(i)->OnTransportStatus(transport,
|
|
|
|
NS_NET_STATUS_SENDING_TO,
|
|
|
|
progress);
|
|
|
|
Response(i)->OnTransportStatus(transport,
|
|
|
|
NS_NET_STATUS_WAITING_FOR,
|
|
|
|
progress);
|
|
|
|
}
|
|
|
|
if (mRequestIsPartial && Request(0))
|
|
|
|
Request(0)->OnTransportStatus(transport,
|
|
|
|
NS_NET_STATUS_SENDING_TO,
|
|
|
|
progress);
|
|
|
|
mSendingToProgress = progress;
|
2003-01-18 02:15:14 +00:00
|
|
|
}
|
2011-12-01 14:19:41 +00:00
|
|
|
// otherwise ignore it
|
|
|
|
break;
|
|
|
|
|
|
|
|
case NS_NET_STATUS_WAITING_FOR:
|
|
|
|
// Created by nsHttpConnection when request pipeline has been totally
|
|
|
|
// sent. Ignore it here because it is simulated in FillSendBuf() when
|
|
|
|
// a request is moved from request to response.
|
|
|
|
|
|
|
|
// ignore it
|
|
|
|
break;
|
|
|
|
|
|
|
|
case NS_NET_STATUS_RECEIVING_FROM:
|
|
|
|
// Forward this only to the transaction currently recieving data. It is
|
|
|
|
// normally generated by the socket transport, but can also
|
|
|
|
// be repeated by the pushbackwriter if necessary.
|
|
|
|
mReceivingFromProgress = progress;
|
|
|
|
if (Response(0))
|
|
|
|
Response(0)->OnTransportStatus(transport, status, progress);
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
// forward other notifications to all request transactions
|
|
|
|
count = mRequestQ.Length();
|
|
|
|
for (i = 0; i < count; ++i)
|
|
|
|
Request(i)->OnTransportStatus(transport, status, progress);
|
2003-01-18 02:15:14 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-09-29 06:19:26 +00:00
|
|
|
bool
|
2003-01-18 02:15:14 +00:00
|
|
|
nsHttpPipeline::IsDone()
|
|
|
|
{
|
2011-11-15 16:25:56 +00:00
|
|
|
bool done = true;
|
2011-11-15 15:12:43 +00:00
|
|
|
|
|
|
|
PRUint32 i, count = mRequestQ.Length();
|
|
|
|
for (i = 0; done && (i < count); i++)
|
|
|
|
done = Request(i)->IsDone();
|
|
|
|
|
|
|
|
count = mResponseQ.Length();
|
|
|
|
for (i = 0; done && (i < count); i++)
|
|
|
|
done = Response(i)->IsDone();
|
|
|
|
|
|
|
|
return done;
|
2003-01-18 02:15:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsHttpPipeline::Status()
|
|
|
|
{
|
|
|
|
return mStatus;
|
2002-04-19 22:25:23 +00:00
|
|
|
}
|
|
|
|
|
2012-03-22 23:39:31 +00:00
|
|
|
PRUint8
|
|
|
|
nsHttpPipeline::Caps()
|
|
|
|
{
|
|
|
|
nsAHttpTransaction *trans = Request(0);
|
|
|
|
if (!trans)
|
|
|
|
trans = Response(0);
|
|
|
|
|
|
|
|
return trans ? trans->Caps() : 0;
|
|
|
|
}
|
|
|
|
|
2002-04-19 22:25:23 +00:00
|
|
|
PRUint32
|
2003-01-18 02:15:14 +00:00
|
|
|
nsHttpPipeline::Available()
|
2002-04-19 22:25:23 +00:00
|
|
|
{
|
2003-01-18 02:15:14 +00:00
|
|
|
PRUint32 result = 0;
|
2002-04-19 22:25:23 +00:00
|
|
|
|
2009-02-23 01:05:28 +00:00
|
|
|
PRInt32 i, count = mRequestQ.Length();
|
2003-01-18 02:15:14 +00:00
|
|
|
for (i=0; i<count; ++i)
|
|
|
|
result += Request(i)->Available();
|
|
|
|
return result;
|
2002-04-19 22:25:23 +00:00
|
|
|
}
|
|
|
|
|
2003-01-18 02:15:14 +00:00
|
|
|
NS_METHOD
|
|
|
|
nsHttpPipeline::ReadFromPipe(nsIInputStream *stream,
|
|
|
|
void *closure,
|
|
|
|
const char *buf,
|
|
|
|
PRUint32 offset,
|
|
|
|
PRUint32 count,
|
|
|
|
PRUint32 *countRead)
|
2002-04-19 22:25:23 +00:00
|
|
|
{
|
2003-01-18 02:15:14 +00:00
|
|
|
nsHttpPipeline *self = (nsHttpPipeline *) closure;
|
|
|
|
return self->mReader->OnReadSegment(buf, count, countRead);
|
|
|
|
}
|
2002-04-19 22:25:23 +00:00
|
|
|
|
2003-01-18 02:15:14 +00:00
|
|
|
nsresult
|
|
|
|
nsHttpPipeline::ReadSegments(nsAHttpSegmentReader *reader,
|
|
|
|
PRUint32 count,
|
|
|
|
PRUint32 *countRead)
|
|
|
|
{
|
|
|
|
LOG(("nsHttpPipeline::ReadSegments [this=%x count=%u]\n", this, count));
|
2002-04-19 22:25:23 +00:00
|
|
|
|
2003-01-18 02:15:14 +00:00
|
|
|
NS_ASSERTION(PR_GetCurrentThread() == gSocketThread, "wrong thread");
|
2002-04-19 22:25:23 +00:00
|
|
|
|
2003-03-25 02:23:11 +00:00
|
|
|
if (mClosed) {
|
|
|
|
*countRead = 0;
|
|
|
|
return mStatus;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult rv;
|
2003-01-18 02:15:14 +00:00
|
|
|
PRUint32 avail = 0;
|
|
|
|
if (mSendBufIn) {
|
|
|
|
rv = mSendBufIn->Available(&avail);
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
2002-04-19 22:25:23 +00:00
|
|
|
}
|
|
|
|
|
2003-01-18 02:15:14 +00:00
|
|
|
if (avail == 0) {
|
|
|
|
rv = FillSendBuf();
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
2002-04-19 22:25:23 +00:00
|
|
|
|
2003-01-18 02:15:14 +00:00
|
|
|
rv = mSendBufIn->Available(&avail);
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
2003-01-29 06:42:30 +00:00
|
|
|
|
|
|
|
// return EOF if send buffer is empty
|
|
|
|
if (avail == 0) {
|
|
|
|
*countRead = 0;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2003-01-18 02:15:14 +00:00
|
|
|
}
|
2002-04-19 22:25:23 +00:00
|
|
|
|
2003-01-18 02:15:14 +00:00
|
|
|
// read no more than what was requested
|
|
|
|
if (avail > count)
|
|
|
|
avail = count;
|
2002-04-19 22:25:23 +00:00
|
|
|
|
2003-01-18 02:15:14 +00:00
|
|
|
mReader = reader;
|
2002-04-19 22:25:23 +00:00
|
|
|
|
2003-01-18 02:15:14 +00:00
|
|
|
rv = mSendBufIn->ReadSegments(ReadFromPipe, this, avail, countRead);
|
2002-04-19 22:25:23 +00:00
|
|
|
|
2003-01-18 02:15:14 +00:00
|
|
|
mReader = nsnull;
|
|
|
|
return rv;
|
2002-04-19 22:25:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2003-01-18 02:15:14 +00:00
|
|
|
nsHttpPipeline::WriteSegments(nsAHttpSegmentWriter *writer,
|
|
|
|
PRUint32 count,
|
|
|
|
PRUint32 *countWritten)
|
2002-04-19 22:25:23 +00:00
|
|
|
{
|
2003-01-18 02:15:14 +00:00
|
|
|
LOG(("nsHttpPipeline::WriteSegments [this=%x count=%u]\n", this, count));
|
2002-04-19 22:25:23 +00:00
|
|
|
|
2003-01-18 02:15:14 +00:00
|
|
|
NS_ASSERTION(PR_GetCurrentThread() == gSocketThread, "wrong thread");
|
2002-04-19 22:25:23 +00:00
|
|
|
|
2003-03-25 02:23:11 +00:00
|
|
|
if (mClosed)
|
|
|
|
return NS_SUCCEEDED(mStatus) ? NS_BASE_STREAM_CLOSED : mStatus;
|
|
|
|
|
2003-01-18 02:15:14 +00:00
|
|
|
nsAHttpTransaction *trans;
|
|
|
|
nsresult rv;
|
|
|
|
|
|
|
|
trans = Response(0);
|
2012-03-22 23:39:31 +00:00
|
|
|
// This code deals with the establishment of a CONNECT tunnel through
|
|
|
|
// an HTTP proxy. It allows the connection to do the CONNECT/200
|
|
|
|
// HTTP transaction to establish an SSL tunnel as a precursor to the
|
|
|
|
// actual pipeline of regular HTTP transactions.
|
|
|
|
if (!trans && mRequestQ.Length() &&
|
|
|
|
mConnection->IsProxyConnectInProgress()) {
|
|
|
|
LOG(("nsHttpPipeline::WriteSegments [this=%p] Forced Delegation\n",
|
|
|
|
this));
|
|
|
|
trans = Request(0);
|
|
|
|
}
|
|
|
|
|
2003-01-18 02:15:14 +00:00
|
|
|
if (!trans) {
|
2009-02-23 01:05:28 +00:00
|
|
|
if (mRequestQ.Length() > 0)
|
2003-01-18 02:15:14 +00:00
|
|
|
rv = NS_BASE_STREAM_WOULD_BLOCK;
|
|
|
|
else
|
|
|
|
rv = NS_BASE_STREAM_CLOSED;
|
2002-04-19 22:25:23 +00:00
|
|
|
}
|
|
|
|
else {
|
2003-01-18 02:15:14 +00:00
|
|
|
//
|
|
|
|
// ask the transaction to consume data from the connection.
|
|
|
|
// PushBack may be called recursively.
|
|
|
|
//
|
|
|
|
rv = trans->WriteSegments(writer, count, countWritten);
|
2002-04-19 22:25:23 +00:00
|
|
|
|
2003-01-18 02:15:14 +00:00
|
|
|
if (rv == NS_BASE_STREAM_CLOSED || trans->IsDone()) {
|
|
|
|
trans->Close(NS_OK);
|
2012-04-04 13:39:21 +00:00
|
|
|
|
|
|
|
// Release the transaction if it is not IsProxyConnectInProgress()
|
|
|
|
if (trans == Response(0)) {
|
|
|
|
NS_RELEASE(trans);
|
|
|
|
mResponseQ.RemoveElementAt(0);
|
|
|
|
mResponseIsPartial = false;
|
|
|
|
++mHttp1xTransactionCount;
|
|
|
|
}
|
2002-04-19 22:25:23 +00:00
|
|
|
|
2003-01-18 02:15:14 +00:00
|
|
|
// ask the connection manager to add additional transactions
|
|
|
|
// to our pipeline.
|
2012-03-22 23:39:31 +00:00
|
|
|
nsRefPtr<nsHttpConnectionInfo> ci;
|
|
|
|
GetConnectionInfo(getter_AddRefs(ci));
|
|
|
|
if (ci)
|
|
|
|
gHttpHandler->ConnMgr()->ProcessPendingQForEntry(ci);
|
2002-04-19 22:25:23 +00:00
|
|
|
}
|
2003-01-18 02:15:14 +00:00
|
|
|
else
|
2011-10-17 14:59:28 +00:00
|
|
|
mResponseIsPartial = true;
|
2002-04-19 22:25:23 +00:00
|
|
|
}
|
|
|
|
|
2003-01-18 02:15:14 +00:00
|
|
|
if (mPushBackLen) {
|
|
|
|
nsHttpPushBackWriter writer(mPushBackBuf, mPushBackLen);
|
|
|
|
PRUint32 len = mPushBackLen, n;
|
|
|
|
mPushBackLen = 0;
|
2011-12-01 14:19:41 +00:00
|
|
|
|
|
|
|
// This progress notification has previously been sent from
|
|
|
|
// the socket transport code, but it was delivered to the
|
|
|
|
// previous transaction on the pipeline.
|
|
|
|
nsITransport *transport = Transport();
|
|
|
|
if (transport)
|
|
|
|
OnTransportStatus(transport,
|
|
|
|
nsISocketTransport::STATUS_RECEIVING_FROM,
|
|
|
|
mReceivingFromProgress);
|
|
|
|
|
2003-01-18 02:15:14 +00:00
|
|
|
// the push back buffer is never larger than NS_HTTP_SEGMENT_SIZE,
|
|
|
|
// so we are guaranteed that the next response will eat the entire
|
|
|
|
// push back buffer (even though it might again call PushBack).
|
|
|
|
rv = WriteSegments(&writer, len, &n);
|
|
|
|
}
|
2002-04-19 22:25:23 +00:00
|
|
|
|
2003-01-18 02:15:14 +00:00
|
|
|
return rv;
|
2002-04-19 22:25:23 +00:00
|
|
|
}
|
|
|
|
|
2012-03-22 23:39:31 +00:00
|
|
|
PRUint32
|
|
|
|
nsHttpPipeline::CancelPipeline(nsresult originalReason)
|
2002-04-19 22:25:23 +00:00
|
|
|
{
|
2012-03-22 23:39:31 +00:00
|
|
|
PRUint32 i, reqLen, respLen, total;
|
|
|
|
nsAHttpTransaction *trans;
|
2002-04-19 22:25:23 +00:00
|
|
|
|
2012-03-22 23:39:31 +00:00
|
|
|
reqLen = mRequestQ.Length();
|
|
|
|
respLen = mResponseQ.Length();
|
|
|
|
total = reqLen + respLen;
|
2003-03-25 02:23:11 +00:00
|
|
|
|
2012-03-22 23:39:31 +00:00
|
|
|
// don't count the first response, if presnet
|
|
|
|
if (respLen)
|
|
|
|
total--;
|
2002-04-19 22:25:23 +00:00
|
|
|
|
2012-03-22 23:39:31 +00:00
|
|
|
if (!total)
|
|
|
|
return 0;
|
2002-04-19 22:25:23 +00:00
|
|
|
|
2003-01-18 02:15:14 +00:00
|
|
|
// any pending requests can ignore this error and be restarted
|
2012-03-22 23:39:31 +00:00
|
|
|
// unless it is during a CONNECT tunnel request
|
2012-03-22 23:39:31 +00:00
|
|
|
for (i = 0; i < reqLen; ++i) {
|
2003-01-18 02:15:14 +00:00
|
|
|
trans = Request(i);
|
2012-03-22 23:39:31 +00:00
|
|
|
if (mConnection && mConnection->IsProxyConnectInProgress())
|
2012-03-22 23:39:31 +00:00
|
|
|
trans->Close(originalReason);
|
2012-03-22 23:39:31 +00:00
|
|
|
else
|
|
|
|
trans->Close(NS_ERROR_NET_RESET);
|
2003-01-18 02:15:14 +00:00
|
|
|
NS_RELEASE(trans);
|
2002-04-19 22:25:23 +00:00
|
|
|
}
|
2003-01-18 02:15:14 +00:00
|
|
|
mRequestQ.Clear();
|
|
|
|
|
2012-03-22 23:39:31 +00:00
|
|
|
// any pending responses can be restarted except for the first one,
|
2012-04-09 14:21:17 +00:00
|
|
|
// that we might want to finish on this pipeline or cancel individually.
|
|
|
|
// Higher levels of callers ensure that we don't process non-idempotent
|
|
|
|
// tranasction with the NS_HTTP_ALLOW_PIPELINING bit set
|
2012-03-22 23:39:31 +00:00
|
|
|
for (i = 1; i < respLen; ++i) {
|
|
|
|
trans = Response(i);
|
|
|
|
trans->Close(NS_ERROR_NET_RESET);
|
|
|
|
NS_RELEASE(trans);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (respLen > 1)
|
|
|
|
mResponseQ.TruncateLength(1);
|
|
|
|
|
|
|
|
DontReuse();
|
|
|
|
Classify(nsAHttpTransaction::CLASS_SOLO);
|
|
|
|
|
|
|
|
return total;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nsHttpPipeline::Close(nsresult reason)
|
|
|
|
{
|
|
|
|
LOG(("nsHttpPipeline::Close [this=%x reason=%x]\n", this, reason));
|
|
|
|
|
|
|
|
if (mClosed) {
|
|
|
|
LOG((" already closed\n"));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// the connection is going away!
|
|
|
|
mStatus = reason;
|
|
|
|
mClosed = true;
|
|
|
|
|
2012-03-22 23:39:31 +00:00
|
|
|
nsRefPtr<nsHttpConnectionInfo> ci;
|
|
|
|
GetConnectionInfo(getter_AddRefs(ci));
|
2012-03-22 23:39:31 +00:00
|
|
|
PRUint32 numRescheduled = CancelPipeline(reason);
|
|
|
|
|
|
|
|
// numRescheduled can be 0 if there is just a single response in the
|
|
|
|
// pipeline object. That isn't really a meaningful pipeline that
|
|
|
|
// has been forced to be rescheduled so it does not need to generate
|
|
|
|
// negative feedback.
|
|
|
|
if (ci && numRescheduled)
|
2012-03-22 23:39:31 +00:00
|
|
|
gHttpHandler->ConnMgr()->PipelineFeedbackInfo(
|
|
|
|
ci, nsHttpConnectionMgr::RedCanceledPipeline, nsnull, 0);
|
|
|
|
|
2012-03-22 23:39:31 +00:00
|
|
|
nsAHttpTransaction *trans = Response(0);
|
|
|
|
if (!trans)
|
|
|
|
return;
|
|
|
|
|
|
|
|
// The current transaction can be restarted via reset
|
|
|
|
// if the response has not started to arrive and the reason
|
|
|
|
// for failure is innocuous (e.g. not an SSL error)
|
|
|
|
if (!mResponseIsPartial &&
|
|
|
|
(reason == NS_ERROR_NET_RESET ||
|
|
|
|
reason == NS_OK ||
|
|
|
|
reason == NS_BASE_STREAM_CLOSED)) {
|
|
|
|
trans->Close(NS_ERROR_NET_RESET);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
trans->Close(reason);
|
2002-04-19 22:25:23 +00:00
|
|
|
}
|
2010-12-09 01:57:14 +00:00
|
|
|
|
2012-03-22 23:39:31 +00:00
|
|
|
NS_RELEASE(trans);
|
|
|
|
mResponseQ.Clear();
|
2002-04-19 22:25:23 +00:00
|
|
|
}
|
|
|
|
|
2003-01-18 02:15:14 +00:00
|
|
|
nsresult
|
|
|
|
nsHttpPipeline::OnReadSegment(const char *segment,
|
|
|
|
PRUint32 count,
|
|
|
|
PRUint32 *countRead)
|
2002-04-19 22:25:23 +00:00
|
|
|
{
|
2003-01-18 02:15:14 +00:00
|
|
|
return mSendBufOut->Write(segment, count, countRead);
|
2002-04-19 22:25:23 +00:00
|
|
|
}
|
|
|
|
|
2003-01-18 02:15:14 +00:00
|
|
|
nsresult
|
|
|
|
nsHttpPipeline::FillSendBuf()
|
2002-04-19 22:25:23 +00:00
|
|
|
{
|
2003-01-18 02:15:14 +00:00
|
|
|
// reads from request queue, moving transactions to response queue
|
|
|
|
// when they have been completely read.
|
|
|
|
|
|
|
|
nsresult rv;
|
|
|
|
|
|
|
|
if (!mSendBufIn) {
|
|
|
|
// allocate a single-segment pipe
|
|
|
|
rv = NS_NewPipe(getter_AddRefs(mSendBufIn),
|
|
|
|
getter_AddRefs(mSendBufOut),
|
2010-04-20 16:32:28 +00:00
|
|
|
nsIOService::gDefaultSegmentSize, /* segment size */
|
|
|
|
nsIOService::gDefaultSegmentSize, /* max size */
|
2012-01-10 03:43:52 +00:00
|
|
|
true, true);
|
2003-01-18 02:15:14 +00:00
|
|
|
if (NS_FAILED(rv)) return rv;
|
2002-05-15 05:36:04 +00:00
|
|
|
}
|
2003-01-18 02:15:14 +00:00
|
|
|
|
|
|
|
PRUint32 n, avail;
|
|
|
|
nsAHttpTransaction *trans;
|
2011-12-01 14:19:41 +00:00
|
|
|
nsITransport *transport = Transport();
|
|
|
|
|
2003-01-18 02:15:14 +00:00
|
|
|
while ((trans = Request(0)) != nsnull) {
|
|
|
|
avail = trans->Available();
|
|
|
|
if (avail) {
|
2012-03-22 23:39:31 +00:00
|
|
|
// if there is already a response in the responseq then this
|
|
|
|
// new data comprises a pipeline. Update the transaction in the
|
|
|
|
// response queue to reflect that if necessary. We are now sending
|
|
|
|
// out a request while we haven't received all responses.
|
|
|
|
nsAHttpTransaction *response = Response(0);
|
|
|
|
if (response && !response->PipelinePosition())
|
|
|
|
response->SetPipelinePosition(1);
|
2003-01-18 02:15:14 +00:00
|
|
|
rv = trans->ReadSegments(this, avail, &n);
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
2003-01-25 02:30:20 +00:00
|
|
|
if (n == 0) {
|
|
|
|
LOG(("send pipe is full"));
|
|
|
|
break;
|
|
|
|
}
|
2011-12-01 14:19:41 +00:00
|
|
|
|
|
|
|
mSendingToProgress += n;
|
|
|
|
if (!mSuppressSendEvents && transport) {
|
|
|
|
// Simulate a SENDING_TO event
|
|
|
|
trans->OnTransportStatus(transport,
|
|
|
|
NS_NET_STATUS_SENDING_TO,
|
|
|
|
mSendingToProgress);
|
|
|
|
}
|
2003-01-18 02:15:14 +00:00
|
|
|
}
|
2011-12-01 14:19:41 +00:00
|
|
|
|
2003-01-18 02:15:14 +00:00
|
|
|
avail = trans->Available();
|
|
|
|
if (avail == 0) {
|
|
|
|
// move transaction from request queue to response queue
|
|
|
|
mRequestQ.RemoveElementAt(0);
|
|
|
|
mResponseQ.AppendElement(trans);
|
2011-10-17 14:59:28 +00:00
|
|
|
mRequestIsPartial = false;
|
2011-12-01 14:19:41 +00:00
|
|
|
|
|
|
|
if (!mSuppressSendEvents && transport) {
|
|
|
|
// Simulate a WAITING_FOR event
|
|
|
|
trans->OnTransportStatus(transport,
|
|
|
|
NS_NET_STATUS_WAITING_FOR,
|
|
|
|
mSendingToProgress);
|
|
|
|
}
|
2012-03-22 23:39:31 +00:00
|
|
|
|
|
|
|
// It would be good to re-enable data read handlers via ResumeRecv()
|
|
|
|
// except the read handler code can be synchronously dispatched on
|
|
|
|
// the stack.
|
2003-01-18 02:15:14 +00:00
|
|
|
}
|
|
|
|
else
|
2011-10-17 14:59:28 +00:00
|
|
|
mRequestIsPartial = true;
|
2003-01-18 02:15:14 +00:00
|
|
|
}
|
|
|
|
return NS_OK;
|
2002-04-19 22:25:23 +00:00
|
|
|
}
|