2001-05-12 01:04:09 +04:00
|
|
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
|
2012-05-21 15:12:37 +04:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
2001-05-12 01:04:09 +04:00
|
|
|
|
|
|
|
#ifndef nsHttpTransaction_h__
|
|
|
|
#define nsHttpTransaction_h__
|
|
|
|
|
|
|
|
#include "nsHttp.h"
|
|
|
|
#include "nsHttpHeaderArray.h"
|
2001-10-02 04:31:30 +04:00
|
|
|
#include "nsAHttpTransaction.h"
|
|
|
|
#include "nsAHttpConnection.h"
|
2003-01-18 05:15:14 +03:00
|
|
|
#include "nsCOMPtr.h"
|
|
|
|
|
|
|
|
#include "nsIPipe.h"
|
2001-05-12 01:04:09 +04:00
|
|
|
#include "nsIInputStream.h"
|
2003-01-18 05:15:14 +03:00
|
|
|
#include "nsIOutputStream.h"
|
2001-05-12 01:04:09 +04:00
|
|
|
#include "nsIInterfaceRequestor.h"
|
2003-01-18 05:15:14 +03:00
|
|
|
#include "nsISocketTransportService.h"
|
|
|
|
#include "nsITransport.h"
|
2006-05-10 21:30:15 +04:00
|
|
|
#include "nsIEventTarget.h"
|
2011-05-21 14:03:36 +04:00
|
|
|
#include "TimingStruct.h"
|
2001-05-12 01:04:09 +04:00
|
|
|
|
2003-01-18 05:15:14 +03:00
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
class nsHttpTransaction;
|
2001-05-12 01:04:09 +04:00
|
|
|
class nsHttpRequestHead;
|
|
|
|
class nsHttpResponseHead;
|
|
|
|
class nsHttpChunkedDecoder;
|
2006-05-20 02:50:02 +04:00
|
|
|
class nsIHttpActivityObserver;
|
2001-05-12 01:04:09 +04:00
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
// nsHttpTransaction represents a single HTTP transaction. It is thread-safe,
|
|
|
|
// intended to run on the socket thread.
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
2001-10-02 04:31:30 +04:00
|
|
|
class nsHttpTransaction : public nsAHttpTransaction
|
2003-10-08 10:10:47 +04:00
|
|
|
, public nsIInputStreamCallback
|
2003-10-06 05:46:31 +04:00
|
|
|
, public nsIOutputStreamCallback
|
2001-05-12 01:04:09 +04:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
NS_DECL_ISUPPORTS
|
2003-03-26 08:05:49 +03:00
|
|
|
NS_DECL_NSAHTTPTRANSACTION
|
2003-10-08 10:10:47 +04:00
|
|
|
NS_DECL_NSIINPUTSTREAMCALLBACK
|
2003-10-06 05:46:31 +04:00
|
|
|
NS_DECL_NSIOUTPUTSTREAMCALLBACK
|
2001-05-12 01:04:09 +04:00
|
|
|
|
2003-01-18 05:15:14 +03:00
|
|
|
nsHttpTransaction();
|
2001-05-12 01:04:09 +04:00
|
|
|
virtual ~nsHttpTransaction();
|
|
|
|
|
2003-01-18 05:15:14 +03:00
|
|
|
//
|
|
|
|
// called to initialize the transaction
|
|
|
|
//
|
|
|
|
// @param caps
|
|
|
|
// the transaction capabilities (see nsHttp.h)
|
|
|
|
// @param connInfo
|
|
|
|
// the connection type for this transaction.
|
|
|
|
// @param reqHeaders
|
|
|
|
// the request header struct
|
|
|
|
// @param reqBody
|
|
|
|
// the request body (POST or PUT data stream)
|
|
|
|
// @param reqBodyIncludesHeaders
|
|
|
|
// fun stuff to support NPAPI plugins.
|
2006-05-10 21:30:15 +04:00
|
|
|
// @param target
|
|
|
|
// the dispatch target were notifications should be sent.
|
2003-01-18 05:15:14 +03:00
|
|
|
// @param callbacks
|
|
|
|
// the notification callbacks to be given to PSM.
|
|
|
|
// @param responseBody
|
|
|
|
// the input stream that will contain the response data. async
|
|
|
|
// wait on this input stream for data. on first notification,
|
|
|
|
// headers should be available (check transaction status).
|
|
|
|
//
|
|
|
|
nsresult Init(PRUint8 caps,
|
|
|
|
nsHttpConnectionInfo *connInfo,
|
|
|
|
nsHttpRequestHead *reqHeaders,
|
|
|
|
nsIInputStream *reqBody,
|
2011-09-29 10:19:26 +04:00
|
|
|
bool reqBodyIncludesHeaders,
|
2006-05-10 21:30:15 +04:00
|
|
|
nsIEventTarget *consumerTarget,
|
2003-01-18 05:15:14 +03:00
|
|
|
nsIInterfaceRequestor *callbacks,
|
|
|
|
nsITransportEventSink *eventsink,
|
|
|
|
nsIAsyncInputStream **responseBody);
|
|
|
|
|
|
|
|
// attributes
|
|
|
|
nsHttpConnectionInfo *ConnectionInfo() { return mConnInfo; }
|
2001-08-02 23:26:24 +04:00
|
|
|
nsHttpResponseHead *ResponseHead() { return mHaveAllHeaders ? mResponseHead : nsnull; }
|
2003-01-18 05:15:14 +03:00
|
|
|
nsISupports *SecurityInfo() { return mSecurityInfo; }
|
|
|
|
|
2001-06-12 01:20:29 +04:00
|
|
|
nsIInterfaceRequestor *Callbacks() { return mCallbacks; }
|
2006-05-10 21:30:15 +04:00
|
|
|
nsIEventTarget *ConsumerTarget() { return mConsumerTarget; }
|
2001-05-12 01:04:09 +04:00
|
|
|
|
|
|
|
// Called to take ownership of the response headers; the transaction
|
|
|
|
// will drop any reference to the response headers after this call.
|
|
|
|
nsHttpResponseHead *TakeResponseHead();
|
|
|
|
|
2002-03-27 02:33:19 +03:00
|
|
|
// Called to find out if the transaction generated a complete response.
|
2011-09-29 10:19:26 +04:00
|
|
|
bool ResponseIsComplete() { return mResponseIsComplete; }
|
2002-03-27 02:33:19 +03:00
|
|
|
|
2012-06-26 00:38:10 +04:00
|
|
|
bool SSLConnectFailed() { return mSSLConnectFailed; }
|
2004-11-06 04:35:01 +03:00
|
|
|
|
2011-12-13 19:55:50 +04:00
|
|
|
// SetPriority() may only be used by the connection manager.
|
2005-01-15 10:01:20 +03:00
|
|
|
void SetPriority(PRInt32 priority) { mPriority = priority; }
|
|
|
|
PRInt32 Priority() { return mPriority; }
|
|
|
|
|
2011-05-21 14:03:36 +04:00
|
|
|
const TimingStruct& Timings() const { return mTimings; }
|
2012-03-23 03:39:31 +04:00
|
|
|
enum Classifier Classification() { return mClassification; }
|
2011-05-21 14:03:36 +04:00
|
|
|
|
2012-07-10 02:07:57 +04:00
|
|
|
void PrintDiagnostics(nsCString &log);
|
|
|
|
|
2001-05-12 01:04:09 +04:00
|
|
|
private:
|
2001-09-21 07:59:02 +04:00
|
|
|
nsresult Restart();
|
2012-03-23 03:39:31 +04:00
|
|
|
nsresult RestartInProgress();
|
2010-12-01 03:02:52 +03:00
|
|
|
char *LocateHttpStart(char *buf, PRUint32 len,
|
2011-09-29 10:19:26 +04:00
|
|
|
bool aAllowPartialMatch);
|
2011-06-01 03:51:51 +04:00
|
|
|
nsresult ParseLine(char *line);
|
2002-08-27 06:26:43 +04:00
|
|
|
nsresult ParseLineSegment(char *seg, PRUint32 len);
|
2001-05-12 01:04:09 +04:00
|
|
|
nsresult ParseHead(char *, PRUint32 count, PRUint32 *countRead);
|
2001-06-14 02:44:47 +04:00
|
|
|
nsresult HandleContentStart();
|
2002-06-18 01:08:46 +04:00
|
|
|
nsresult HandleContent(char *, PRUint32 count, PRUint32 *contentRead, PRUint32 *contentRemaining);
|
2003-01-18 05:15:14 +03:00
|
|
|
nsresult ProcessData(char *, PRUint32, PRUint32 *);
|
2001-06-12 01:20:29 +04:00
|
|
|
void DeleteSelfOnConsumerThread();
|
|
|
|
|
2012-03-23 03:39:31 +04:00
|
|
|
Classifier Classify();
|
2012-03-23 03:39:31 +04:00
|
|
|
void CancelPipeline(PRUint32 reason);
|
2012-03-23 03:39:31 +04:00
|
|
|
|
2003-01-18 05:15:14 +03:00
|
|
|
static NS_METHOD ReadRequestSegment(nsIInputStream *, void *, const char *,
|
|
|
|
PRUint32, PRUint32, PRUint32 *);
|
|
|
|
static NS_METHOD WritePipeSegment(nsIOutputStream *, void *, char *,
|
|
|
|
PRUint32, PRUint32, PRUint32 *);
|
2001-05-12 01:04:09 +04:00
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
bool TimingEnabled() const { return mCaps & NS_HTTP_TIMING_ENABLED; }
|
2011-05-21 14:03:36 +04:00
|
|
|
|
2001-05-12 01:04:09 +04:00
|
|
|
private:
|
|
|
|
nsCOMPtr<nsIInterfaceRequestor> mCallbacks;
|
2003-01-18 05:15:14 +03:00
|
|
|
nsCOMPtr<nsITransportEventSink> mTransportSink;
|
2006-05-10 21:30:15 +04:00
|
|
|
nsCOMPtr<nsIEventTarget> mConsumerTarget;
|
2001-06-15 00:52:03 +04:00
|
|
|
nsCOMPtr<nsISupports> mSecurityInfo;
|
2003-01-18 05:15:14 +03:00
|
|
|
nsCOMPtr<nsIAsyncInputStream> mPipeIn;
|
|
|
|
nsCOMPtr<nsIAsyncOutputStream> mPipeOut;
|
2001-05-12 01:04:09 +04:00
|
|
|
|
2006-05-20 02:50:02 +04:00
|
|
|
nsCOMPtr<nsISupports> mChannel;
|
|
|
|
nsCOMPtr<nsIHttpActivityObserver> mActivityDistributor;
|
|
|
|
|
2001-05-12 01:04:09 +04:00
|
|
|
nsCString mReqHeaderBuf; // flattened request headers
|
2003-01-18 05:15:14 +03:00
|
|
|
nsCOMPtr<nsIInputStream> mRequestStream;
|
|
|
|
PRUint32 mRequestSize;
|
2001-05-12 01:04:09 +04:00
|
|
|
|
2003-01-18 05:15:14 +03:00
|
|
|
nsAHttpConnection *mConnection; // hard ref
|
|
|
|
nsHttpConnectionInfo *mConnInfo; // hard ref
|
2001-05-30 04:40:50 +04:00
|
|
|
nsHttpRequestHead *mRequestHead; // weak ref
|
|
|
|
nsHttpResponseHead *mResponseHead; // hard ref
|
2001-05-12 01:04:09 +04:00
|
|
|
|
2003-01-18 05:15:14 +03:00
|
|
|
nsAHttpSegmentReader *mReader;
|
|
|
|
nsAHttpSegmentWriter *mWriter;
|
|
|
|
|
2001-05-12 01:04:09 +04:00
|
|
|
nsCString mLineBuf; // may contain a partial line
|
|
|
|
|
2011-04-01 04:15:27 +04:00
|
|
|
PRInt64 mContentLength; // equals -1 if unknown
|
|
|
|
PRInt64 mContentRead; // count of consumed content bytes
|
2001-05-12 01:04:09 +04:00
|
|
|
|
2011-01-27 21:34:39 +03:00
|
|
|
// After a 304/204 or other "no-content" style response we will skip over
|
|
|
|
// up to MAX_INVALID_RESPONSE_BODY_SZ bytes when looking for the next
|
|
|
|
// response header to deal with servers that actually sent a response
|
|
|
|
// body where they should not have. This member tracks how many bytes have
|
|
|
|
// so far been skipped.
|
|
|
|
PRUint32 mInvalidResponseBytesRead;
|
|
|
|
|
2001-05-12 01:04:09 +04:00
|
|
|
nsHttpChunkedDecoder *mChunkedDecoder;
|
|
|
|
|
2011-05-21 14:03:36 +04:00
|
|
|
TimingStruct mTimings;
|
|
|
|
|
2001-05-12 01:04:09 +04:00
|
|
|
nsresult mStatus;
|
|
|
|
|
2005-01-15 10:01:20 +03:00
|
|
|
PRInt16 mPriority;
|
|
|
|
|
2003-01-18 05:15:14 +03:00
|
|
|
PRUint16 mRestartCount; // the number of times this transaction has been restarted
|
|
|
|
PRUint8 mCaps;
|
2012-03-23 03:39:31 +04:00
|
|
|
enum Classifier mClassification;
|
2012-03-23 03:39:31 +04:00
|
|
|
PRInt32 mPipelinePosition;
|
2012-03-23 03:39:31 +04:00
|
|
|
PRInt64 mMaxPipelineObjectSize;
|
2001-08-14 05:03:27 +04:00
|
|
|
|
2011-03-24 22:51:00 +03:00
|
|
|
// state flags, all logically boolean, but not packed together into a
|
|
|
|
// bitfield so as to avoid bitfield-induced races. See bug 560579.
|
2011-09-29 10:19:26 +04:00
|
|
|
bool mClosed;
|
|
|
|
bool mConnected;
|
|
|
|
bool mHaveStatusLine;
|
|
|
|
bool mHaveAllHeaders;
|
|
|
|
bool mTransactionDone;
|
|
|
|
bool mResponseIsComplete;
|
|
|
|
bool mDidContentStart;
|
|
|
|
bool mNoContent; // expecting an empty entity body
|
|
|
|
bool mSentData;
|
|
|
|
bool mReceivedData;
|
|
|
|
bool mStatusEventPending;
|
|
|
|
bool mHasRequestBody;
|
2012-06-26 00:38:10 +04:00
|
|
|
bool mSSLConnectFailed;
|
2011-09-29 10:19:26 +04:00
|
|
|
bool mHttpResponseMatched;
|
|
|
|
bool mPreserveStream;
|
2003-03-25 05:23:11 +03:00
|
|
|
|
|
|
|
// mClosed := transaction has been explicitly closed
|
|
|
|
// mTransactionDone := transaction ran to completion or was interrupted
|
|
|
|
// mResponseComplete := transaction ran to completion
|
2012-03-23 03:39:31 +04:00
|
|
|
|
|
|
|
// For Restart-In-Progress Functionality
|
|
|
|
bool mReportedStart;
|
|
|
|
bool mReportedResponseHeader;
|
|
|
|
|
|
|
|
// protected by nsHttp::GetLock()
|
|
|
|
nsHttpResponseHead *mForTakeResponseHead;
|
2012-04-06 02:37:57 +04:00
|
|
|
bool mResponseHeadTaken;
|
2012-03-23 03:39:31 +04:00
|
|
|
|
|
|
|
class RestartVerifier
|
|
|
|
{
|
|
|
|
|
|
|
|
// When a idemptotent transaction has received part of its response body
|
|
|
|
// and incurs an error it can be restarted. To do this we mark the place
|
|
|
|
// where we stopped feeding the body to the consumer and start the
|
|
|
|
// network call over again. If everything we track (headers, length, etc..)
|
|
|
|
// matches up to the place where we left off then the consumer starts being
|
|
|
|
// fed data again with the new information. This can be done N times up
|
|
|
|
// to the normal restart (i.e. with no response info) limit.
|
|
|
|
|
|
|
|
public:
|
|
|
|
RestartVerifier()
|
|
|
|
: mContentLength(-1)
|
|
|
|
, mAlreadyProcessed(0)
|
2012-04-06 02:37:57 +04:00
|
|
|
, mToReadBeforeRestart(0)
|
2012-03-23 03:39:31 +04:00
|
|
|
, mSetup(false)
|
|
|
|
{}
|
|
|
|
~RestartVerifier() {}
|
|
|
|
|
|
|
|
void Set(PRInt64 contentLength, nsHttpResponseHead *head);
|
|
|
|
bool Verify(PRInt64 contentLength, nsHttpResponseHead *head);
|
2012-04-06 02:37:57 +04:00
|
|
|
bool IsDiscardingContent() { return mToReadBeforeRestart != 0; }
|
2012-03-23 03:39:31 +04:00
|
|
|
bool IsSetup() { return mSetup; }
|
|
|
|
PRInt64 AlreadyProcessed() { return mAlreadyProcessed; }
|
2012-04-06 02:37:57 +04:00
|
|
|
void SetAlreadyProcessed(PRInt64 val) {
|
|
|
|
mAlreadyProcessed = val;
|
|
|
|
mToReadBeforeRestart = val;
|
|
|
|
}
|
|
|
|
PRInt64 ToReadBeforeRestart() { return mToReadBeforeRestart; }
|
|
|
|
void HaveReadBeforeRestart(PRUint32 amt)
|
|
|
|
{
|
|
|
|
NS_ABORT_IF_FALSE(amt <= mToReadBeforeRestart,
|
|
|
|
"too large of a HaveReadBeforeRestart deduction");
|
|
|
|
mToReadBeforeRestart -= amt;
|
|
|
|
}
|
2012-03-23 03:39:31 +04:00
|
|
|
|
|
|
|
private:
|
|
|
|
// This is the data from the first complete response header
|
|
|
|
// used to make sure that all subsequent response headers match
|
|
|
|
|
|
|
|
PRInt64 mContentLength;
|
|
|
|
nsCString mETag;
|
|
|
|
nsCString mLastModified;
|
|
|
|
nsCString mContentRange;
|
|
|
|
nsCString mContentEncoding;
|
|
|
|
nsCString mTransferEncoding;
|
|
|
|
|
|
|
|
// This is the amount of data that has been passed to the channel
|
|
|
|
// from previous iterations of the transaction and must therefore
|
|
|
|
// be skipped in the new one.
|
|
|
|
PRInt64 mAlreadyProcessed;
|
|
|
|
|
2012-04-06 02:37:57 +04:00
|
|
|
// The amount of data that must be discarded in the current iteration
|
|
|
|
// (where iteration > 0) to reach the mAlreadyProcessed high water
|
|
|
|
// mark.
|
|
|
|
PRInt64 mToReadBeforeRestart;
|
2012-03-23 03:39:31 +04:00
|
|
|
|
|
|
|
// true when ::Set has been called with a response header
|
|
|
|
bool mSetup;
|
|
|
|
} mRestartInProgressVerifier;
|
2001-05-12 01:04:09 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
#endif // nsHttpTransaction_h__
|