2001-05-11 21:04:09 +00:00
|
|
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
|
2012-05-21 11:12:37 +00: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-11 21:04:09 +00:00
|
|
|
|
|
|
|
#ifndef nsHttpTransaction_h__
|
|
|
|
#define nsHttpTransaction_h__
|
|
|
|
|
|
|
|
#include "nsHttp.h"
|
|
|
|
#include "nsHttpHeaderArray.h"
|
2001-10-02 00:31:30 +00:00
|
|
|
#include "nsAHttpTransaction.h"
|
|
|
|
#include "nsAHttpConnection.h"
|
2003-01-18 02:15:14 +00:00
|
|
|
#include "nsCOMPtr.h"
|
|
|
|
|
|
|
|
#include "nsIPipe.h"
|
2001-05-11 21:04:09 +00:00
|
|
|
#include "nsIInputStream.h"
|
2012-12-04 23:06:29 +00:00
|
|
|
#include "nsILoadGroup.h"
|
2003-01-18 02:15:14 +00:00
|
|
|
#include "nsIOutputStream.h"
|
2001-05-11 21:04:09 +00:00
|
|
|
#include "nsIInterfaceRequestor.h"
|
2003-01-18 02:15:14 +00:00
|
|
|
#include "nsISocketTransportService.h"
|
|
|
|
#include "nsITransport.h"
|
2006-05-10 17:30:15 +00:00
|
|
|
#include "nsIEventTarget.h"
|
2011-05-21 10:03:36 +00:00
|
|
|
#include "TimingStruct.h"
|
2001-05-11 21:04:09 +00:00
|
|
|
|
2003-01-18 02:15:14 +00:00
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
class nsHttpTransaction;
|
2001-05-11 21:04:09 +00:00
|
|
|
class nsHttpRequestHead;
|
|
|
|
class nsHttpResponseHead;
|
|
|
|
class nsHttpChunkedDecoder;
|
2006-05-19 22:50:02 +00:00
|
|
|
class nsIHttpActivityObserver;
|
2012-12-05 22:10:19 +00:00
|
|
|
class UpdateSecurityCallbacks;
|
2001-05-11 21:04:09 +00:00
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
// nsHttpTransaction represents a single HTTP transaction. It is thread-safe,
|
|
|
|
// intended to run on the socket thread.
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
2001-10-02 00:31:30 +00:00
|
|
|
class nsHttpTransaction : public nsAHttpTransaction
|
2003-10-08 06:10:47 +00:00
|
|
|
, public nsIInputStreamCallback
|
2003-10-06 01:46:31 +00:00
|
|
|
, public nsIOutputStreamCallback
|
2001-05-11 21:04:09 +00:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
NS_DECL_ISUPPORTS
|
2003-03-26 05:05:49 +00:00
|
|
|
NS_DECL_NSAHTTPTRANSACTION
|
2003-10-08 06:10:47 +00:00
|
|
|
NS_DECL_NSIINPUTSTREAMCALLBACK
|
2003-10-06 01:46:31 +00:00
|
|
|
NS_DECL_NSIOUTPUTSTREAMCALLBACK
|
2001-05-11 21:04:09 +00:00
|
|
|
|
2003-01-18 02:15:14 +00:00
|
|
|
nsHttpTransaction();
|
2001-05-11 21:04:09 +00:00
|
|
|
virtual ~nsHttpTransaction();
|
|
|
|
|
2003-01-18 02:15:14 +00: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 17:30:15 +00:00
|
|
|
// @param target
|
|
|
|
// the dispatch target were notifications should be sent.
|
2003-01-18 02:15:14 +00: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).
|
|
|
|
//
|
2012-11-30 01:34:35 +00:00
|
|
|
nsresult Init(uint32_t caps,
|
2003-01-18 02:15:14 +00:00
|
|
|
nsHttpConnectionInfo *connInfo,
|
|
|
|
nsHttpRequestHead *reqHeaders,
|
|
|
|
nsIInputStream *reqBody,
|
2011-09-29 06:19:26 +00:00
|
|
|
bool reqBodyIncludesHeaders,
|
2006-05-10 17:30:15 +00:00
|
|
|
nsIEventTarget *consumerTarget,
|
2003-01-18 02:15:14 +00:00
|
|
|
nsIInterfaceRequestor *callbacks,
|
|
|
|
nsITransportEventSink *eventsink,
|
|
|
|
nsIAsyncInputStream **responseBody);
|
|
|
|
|
|
|
|
// attributes
|
|
|
|
nsHttpConnectionInfo *ConnectionInfo() { return mConnInfo; }
|
2012-07-30 14:20:58 +00:00
|
|
|
nsHttpResponseHead *ResponseHead() { return mHaveAllHeaders ? mResponseHead : nullptr; }
|
2003-01-18 02:15:14 +00:00
|
|
|
nsISupports *SecurityInfo() { return mSecurityInfo; }
|
|
|
|
|
2006-05-10 17:30:15 +00:00
|
|
|
nsIEventTarget *ConsumerTarget() { return mConsumerTarget; }
|
2001-05-11 21:04:09 +00:00
|
|
|
|
2012-11-14 16:00:44 +00:00
|
|
|
void SetSecurityCallbacks(nsIInterfaceRequestor* aCallbacks);
|
2012-11-14 16:00:43 +00:00
|
|
|
|
2001-05-11 21:04:09 +00: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-26 23:33:19 +00:00
|
|
|
// Called to find out if the transaction generated a complete response.
|
2011-09-29 06:19:26 +00:00
|
|
|
bool ResponseIsComplete() { return mResponseIsComplete; }
|
2002-03-26 23:33:19 +00:00
|
|
|
|
2012-06-22 19:06:00 +00:00
|
|
|
bool ProxyConnectFailed() { return mProxyConnectFailed; }
|
2004-11-06 01:35:01 +00:00
|
|
|
|
2011-12-13 15:55:50 +00:00
|
|
|
// SetPriority() may only be used by the connection manager.
|
2012-08-22 15:56:38 +00:00
|
|
|
void SetPriority(int32_t priority) { mPriority = priority; }
|
|
|
|
int32_t Priority() { return mPriority; }
|
2005-01-15 07:01:20 +00:00
|
|
|
|
2011-05-21 10:03:36 +00:00
|
|
|
const TimingStruct& Timings() const { return mTimings; }
|
2012-03-22 23:39:31 +00:00
|
|
|
enum Classifier Classification() { return mClassification; }
|
2011-05-21 10:03:36 +00:00
|
|
|
|
2012-07-09 22:07:57 +00:00
|
|
|
void PrintDiagnostics(nsCString &log);
|
|
|
|
|
2012-09-20 00:24:58 +00:00
|
|
|
// Sets mPendingTime to the current time stamp or to a null time stamp (if now is false)
|
|
|
|
void SetPendingTime(bool now = true) { mPendingTime = now ? mozilla::TimeStamp::Now() : mozilla::TimeStamp(); }
|
|
|
|
const mozilla::TimeStamp GetPendingTime() { return mPendingTime; }
|
|
|
|
bool UsesPipelining() const { return mCaps & NS_HTTP_ALLOW_PIPELINING; }
|
|
|
|
|
2012-12-04 23:06:29 +00:00
|
|
|
void SetLoadGroupConnectionInfo(nsILoadGroupConnectionInfo *aLoadGroupCI) { mLoadGroupCI = aLoadGroupCI; }
|
|
|
|
nsILoadGroupConnectionInfo *LoadGroupConnectionInfo() { return mLoadGroupCI.get(); }
|
|
|
|
void DispatchedAsBlocking();
|
|
|
|
void RemoveDispatchedAsBlocking();
|
|
|
|
|
2001-05-11 21:04:09 +00:00
|
|
|
private:
|
2001-09-21 03:59:02 +00:00
|
|
|
nsresult Restart();
|
2012-03-22 23:39:31 +00:00
|
|
|
nsresult RestartInProgress();
|
2012-08-22 15:56:38 +00:00
|
|
|
char *LocateHttpStart(char *buf, uint32_t len,
|
2011-09-29 06:19:26 +00:00
|
|
|
bool aAllowPartialMatch);
|
2011-05-31 23:51:51 +00:00
|
|
|
nsresult ParseLine(char *line);
|
2012-08-22 15:56:38 +00:00
|
|
|
nsresult ParseLineSegment(char *seg, uint32_t len);
|
|
|
|
nsresult ParseHead(char *, uint32_t count, uint32_t *countRead);
|
2001-06-13 22:44:47 +00:00
|
|
|
nsresult HandleContentStart();
|
2012-08-22 15:56:38 +00:00
|
|
|
nsresult HandleContent(char *, uint32_t count, uint32_t *contentRead, uint32_t *contentRemaining);
|
|
|
|
nsresult ProcessData(char *, uint32_t, uint32_t *);
|
2001-06-11 21:20:29 +00:00
|
|
|
void DeleteSelfOnConsumerThread();
|
2012-12-04 23:06:29 +00:00
|
|
|
void ReleaseBlockingTransaction();
|
2001-06-11 21:20:29 +00:00
|
|
|
|
2012-03-22 23:39:31 +00:00
|
|
|
Classifier Classify();
|
2012-08-22 15:56:38 +00:00
|
|
|
void CancelPipeline(uint32_t reason);
|
2012-03-22 23:39:31 +00:00
|
|
|
|
2003-01-18 02:15:14 +00:00
|
|
|
static NS_METHOD ReadRequestSegment(nsIInputStream *, void *, const char *,
|
2012-08-22 15:56:38 +00:00
|
|
|
uint32_t, uint32_t, uint32_t *);
|
2003-01-18 02:15:14 +00:00
|
|
|
static NS_METHOD WritePipeSegment(nsIOutputStream *, void *, char *,
|
2012-08-22 15:56:38 +00:00
|
|
|
uint32_t, uint32_t, uint32_t *);
|
2001-05-11 21:04:09 +00:00
|
|
|
|
2011-09-29 06:19:26 +00:00
|
|
|
bool TimingEnabled() const { return mCaps & NS_HTTP_TIMING_ENABLED; }
|
2011-05-21 10:03:36 +00:00
|
|
|
|
2001-05-11 21:04:09 +00:00
|
|
|
private:
|
2012-12-05 22:10:19 +00:00
|
|
|
class UpdateSecurityCallbacks : public nsRunnable
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
UpdateSecurityCallbacks(nsHttpTransaction* aTrans,
|
|
|
|
nsIInterfaceRequestor* aCallbacks)
|
|
|
|
: mTrans(aTrans), mCallbacks(aCallbacks) {}
|
|
|
|
|
|
|
|
NS_IMETHOD Run()
|
|
|
|
{
|
|
|
|
if (mTrans->mConnection)
|
|
|
|
mTrans->mConnection->SetSecurityCallbacks(mCallbacks);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
private:
|
|
|
|
nsRefPtr<nsHttpTransaction> mTrans;
|
|
|
|
nsCOMPtr<nsIInterfaceRequestor> mCallbacks;
|
|
|
|
};
|
|
|
|
|
2012-11-14 16:00:43 +00:00
|
|
|
mozilla::Mutex mCallbacksLock;
|
|
|
|
|
2001-05-11 21:04:09 +00:00
|
|
|
nsCOMPtr<nsIInterfaceRequestor> mCallbacks;
|
2003-01-18 02:15:14 +00:00
|
|
|
nsCOMPtr<nsITransportEventSink> mTransportSink;
|
2006-05-10 17:30:15 +00:00
|
|
|
nsCOMPtr<nsIEventTarget> mConsumerTarget;
|
2001-06-14 20:52:03 +00:00
|
|
|
nsCOMPtr<nsISupports> mSecurityInfo;
|
2003-01-18 02:15:14 +00:00
|
|
|
nsCOMPtr<nsIAsyncInputStream> mPipeIn;
|
|
|
|
nsCOMPtr<nsIAsyncOutputStream> mPipeOut;
|
2012-12-04 23:06:29 +00:00
|
|
|
nsCOMPtr<nsILoadGroupConnectionInfo> mLoadGroupCI;
|
2001-05-11 21:04:09 +00:00
|
|
|
|
2006-05-19 22:50:02 +00:00
|
|
|
nsCOMPtr<nsISupports> mChannel;
|
|
|
|
nsCOMPtr<nsIHttpActivityObserver> mActivityDistributor;
|
|
|
|
|
2001-05-11 21:04:09 +00:00
|
|
|
nsCString mReqHeaderBuf; // flattened request headers
|
2003-01-18 02:15:14 +00:00
|
|
|
nsCOMPtr<nsIInputStream> mRequestStream;
|
2012-08-22 15:56:38 +00:00
|
|
|
uint64_t mRequestSize;
|
2001-05-11 21:04:09 +00:00
|
|
|
|
2003-01-18 02:15:14 +00:00
|
|
|
nsAHttpConnection *mConnection; // hard ref
|
|
|
|
nsHttpConnectionInfo *mConnInfo; // hard ref
|
2001-05-30 00:40:50 +00:00
|
|
|
nsHttpRequestHead *mRequestHead; // weak ref
|
|
|
|
nsHttpResponseHead *mResponseHead; // hard ref
|
2001-05-11 21:04:09 +00:00
|
|
|
|
2003-01-18 02:15:14 +00:00
|
|
|
nsAHttpSegmentReader *mReader;
|
|
|
|
nsAHttpSegmentWriter *mWriter;
|
|
|
|
|
2001-05-11 21:04:09 +00:00
|
|
|
nsCString mLineBuf; // may contain a partial line
|
|
|
|
|
2012-08-22 15:56:38 +00:00
|
|
|
int64_t mContentLength; // equals -1 if unknown
|
|
|
|
int64_t mContentRead; // count of consumed content bytes
|
2001-05-11 21:04:09 +00:00
|
|
|
|
2011-01-27 18:34:39 +00: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.
|
2012-08-22 15:56:38 +00:00
|
|
|
uint32_t mInvalidResponseBytesRead;
|
2011-01-27 18:34:39 +00:00
|
|
|
|
2001-05-11 21:04:09 +00:00
|
|
|
nsHttpChunkedDecoder *mChunkedDecoder;
|
|
|
|
|
2011-05-21 10:03:36 +00:00
|
|
|
TimingStruct mTimings;
|
|
|
|
|
2001-05-11 21:04:09 +00:00
|
|
|
nsresult mStatus;
|
|
|
|
|
2012-08-22 15:56:38 +00:00
|
|
|
int16_t mPriority;
|
2005-01-15 07:01:20 +00:00
|
|
|
|
2012-08-22 15:56:38 +00:00
|
|
|
uint16_t mRestartCount; // the number of times this transaction has been restarted
|
2012-11-30 01:34:35 +00:00
|
|
|
uint32_t mCaps;
|
2012-03-22 23:39:31 +00:00
|
|
|
enum Classifier mClassification;
|
2012-08-22 15:56:38 +00:00
|
|
|
int32_t mPipelinePosition;
|
|
|
|
int64_t mMaxPipelineObjectSize;
|
2001-08-14 01:03:27 +00:00
|
|
|
|
2012-08-16 10:37:52 +00:00
|
|
|
nsHttpVersion mHttpVersion;
|
|
|
|
|
2011-03-24 19:51:00 +00: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 06:19:26 +00: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-22 19:06:00 +00:00
|
|
|
bool mProxyConnectFailed;
|
2011-09-29 06:19:26 +00:00
|
|
|
bool mHttpResponseMatched;
|
|
|
|
bool mPreserveStream;
|
2012-12-04 23:06:29 +00:00
|
|
|
bool mDispatchedAsBlocking;
|
2003-03-25 02:23:11 +00:00
|
|
|
|
|
|
|
// mClosed := transaction has been explicitly closed
|
|
|
|
// mTransactionDone := transaction ran to completion or was interrupted
|
|
|
|
// mResponseComplete := transaction ran to completion
|
2012-03-22 23:39:31 +00:00
|
|
|
|
|
|
|
// For Restart-In-Progress Functionality
|
|
|
|
bool mReportedStart;
|
|
|
|
bool mReportedResponseHeader;
|
|
|
|
|
|
|
|
// protected by nsHttp::GetLock()
|
|
|
|
nsHttpResponseHead *mForTakeResponseHead;
|
2012-04-05 22:37:57 +00:00
|
|
|
bool mResponseHeadTaken;
|
2012-03-22 23:39:31 +00:00
|
|
|
|
2012-09-20 00:24:58 +00:00
|
|
|
// The time when the transaction was submitted to the Connection Manager
|
|
|
|
mozilla::TimeStamp mPendingTime;
|
|
|
|
|
2012-03-22 23:39:31 +00: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-05 22:37:57 +00:00
|
|
|
, mToReadBeforeRestart(0)
|
2012-03-22 23:39:31 +00:00
|
|
|
, mSetup(false)
|
|
|
|
{}
|
|
|
|
~RestartVerifier() {}
|
|
|
|
|
2012-08-22 15:56:38 +00:00
|
|
|
void Set(int64_t contentLength, nsHttpResponseHead *head);
|
|
|
|
bool Verify(int64_t contentLength, nsHttpResponseHead *head);
|
2012-04-05 22:37:57 +00:00
|
|
|
bool IsDiscardingContent() { return mToReadBeforeRestart != 0; }
|
2012-03-22 23:39:31 +00:00
|
|
|
bool IsSetup() { return mSetup; }
|
2012-08-22 15:56:38 +00:00
|
|
|
int64_t AlreadyProcessed() { return mAlreadyProcessed; }
|
|
|
|
void SetAlreadyProcessed(int64_t val) {
|
2012-04-05 22:37:57 +00:00
|
|
|
mAlreadyProcessed = val;
|
|
|
|
mToReadBeforeRestart = val;
|
|
|
|
}
|
2012-08-22 15:56:38 +00:00
|
|
|
int64_t ToReadBeforeRestart() { return mToReadBeforeRestart; }
|
|
|
|
void HaveReadBeforeRestart(uint32_t amt)
|
2012-04-05 22:37:57 +00:00
|
|
|
{
|
|
|
|
NS_ABORT_IF_FALSE(amt <= mToReadBeforeRestart,
|
|
|
|
"too large of a HaveReadBeforeRestart deduction");
|
|
|
|
mToReadBeforeRestart -= amt;
|
|
|
|
}
|
2012-03-22 23:39:31 +00:00
|
|
|
|
|
|
|
private:
|
|
|
|
// This is the data from the first complete response header
|
|
|
|
// used to make sure that all subsequent response headers match
|
|
|
|
|
2012-08-22 15:56:38 +00:00
|
|
|
int64_t mContentLength;
|
2012-03-22 23:39:31 +00:00
|
|
|
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.
|
2012-08-22 15:56:38 +00:00
|
|
|
int64_t mAlreadyProcessed;
|
2012-03-22 23:39:31 +00:00
|
|
|
|
2012-04-05 22:37:57 +00:00
|
|
|
// The amount of data that must be discarded in the current iteration
|
|
|
|
// (where iteration > 0) to reach the mAlreadyProcessed high water
|
|
|
|
// mark.
|
2012-08-22 15:56:38 +00:00
|
|
|
int64_t mToReadBeforeRestart;
|
2012-03-22 23:39:31 +00:00
|
|
|
|
|
|
|
// true when ::Set has been called with a response header
|
|
|
|
bool mSetup;
|
|
|
|
} mRestartInProgressVerifier;
|
2001-05-11 21:04:09 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
#endif // nsHttpTransaction_h__
|