2001-05-11 21:04:09 +00:00
|
|
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
|
|
|
|
/*
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
* The Original Code is Mozilla.
|
|
|
|
*
|
|
|
|
* The Initial Developer of the Original Code is Netscape
|
|
|
|
* Communications. Portions created by Netscape Communications are
|
|
|
|
* Copyright (C) 2001 by Netscape Communications. All
|
|
|
|
* Rights Reserved.
|
|
|
|
*
|
|
|
|
* Contributor(s):
|
|
|
|
* Darin Fisher <darin@netscape.com> (original author)
|
|
|
|
*/
|
|
|
|
|
|
|
|
#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"
|
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"
|
2001-06-11 21:20:29 +00:00
|
|
|
#include "nsIEventQueue.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;
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
// 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-01-18 02:15:14 +00:00
|
|
|
, public nsIOutputStreamNotify
|
|
|
|
, public nsISocketEventHandler
|
2001-05-11 21:04:09 +00:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
NS_DECL_ISUPPORTS
|
2003-03-26 05:05:49 +00:00
|
|
|
NS_DECL_NSAHTTPTRANSACTION
|
2003-01-18 02:15:14 +00:00
|
|
|
NS_DECL_NSIOUTPUTSTREAMNOTIFY
|
|
|
|
NS_DECL_NSISOCKETEVENTHANDLER
|
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.
|
|
|
|
// @param eventQ
|
|
|
|
// the event queue were notifications should be sent.
|
|
|
|
// @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,
|
|
|
|
PRBool reqBodyIncludesHeaders,
|
|
|
|
nsIEventQueue *eventQ,
|
|
|
|
nsIInterfaceRequestor *callbacks,
|
|
|
|
nsITransportEventSink *eventsink,
|
|
|
|
nsIAsyncInputStream **responseBody);
|
|
|
|
|
|
|
|
// attributes
|
|
|
|
PRUint8 Caps() { return mCaps; }
|
|
|
|
nsHttpConnectionInfo *ConnectionInfo() { return mConnInfo; }
|
2001-06-11 21:20:29 +00:00
|
|
|
nsHttpRequestHead *RequestHead() { return mRequestHead; }
|
2001-08-02 19:26:24 +00:00
|
|
|
nsHttpResponseHead *ResponseHead() { return mHaveAllHeaders ? mResponseHead : nsnull; }
|
2003-01-18 02:15:14 +00:00
|
|
|
nsISupports *SecurityInfo() { return mSecurityInfo; }
|
|
|
|
nsresult TransportStatus(){ return mTransportStatus; }
|
|
|
|
|
2001-06-11 21:20:29 +00:00
|
|
|
nsIInterfaceRequestor *Callbacks() { return mCallbacks; }
|
|
|
|
nsIEventQueue *ConsumerEventQ() { return mConsumerEventQ; }
|
2003-01-18 02:15:14 +00:00
|
|
|
nsAHttpConnection *Connection() { return mConnection; }
|
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.
|
|
|
|
PRBool ResponseIsComplete() { return mResponseIsComplete; }
|
|
|
|
|
2001-05-11 21:04:09 +00:00
|
|
|
private:
|
2001-09-21 03:59:02 +00:00
|
|
|
nsresult Restart();
|
2001-08-02 19:26:24 +00:00
|
|
|
void ParseLine(char *line);
|
2002-08-27 02:26:43 +00:00
|
|
|
nsresult ParseLineSegment(char *seg, PRUint32 len);
|
2001-05-11 21:04:09 +00:00
|
|
|
nsresult ParseHead(char *, PRUint32 count, PRUint32 *countRead);
|
2001-06-13 22:44:47 +00:00
|
|
|
nsresult HandleContentStart();
|
2002-06-17 21:08:46 +00:00
|
|
|
nsresult HandleContent(char *, PRUint32 count, PRUint32 *contentRead, PRUint32 *contentRemaining);
|
2003-01-18 02:15:14 +00:00
|
|
|
nsresult ProcessData(char *, PRUint32, PRUint32 *);
|
2001-06-11 21:20:29 +00:00
|
|
|
void DeleteSelfOnConsumerThread();
|
|
|
|
|
2003-01-18 02:15:14 +00:00
|
|
|
static void *PR_CALLBACK TransportStatus_Handler(PLEvent *);
|
|
|
|
static void PR_CALLBACK TransportStatus_Cleanup(PLEvent *);
|
|
|
|
static void *PR_CALLBACK DeleteThis_Handler(PLEvent *);
|
|
|
|
static void PR_CALLBACK DeleteThis_Cleanup(PLEvent *);
|
|
|
|
|
|
|
|
static NS_METHOD ReadRequestSegment(nsIInputStream *, void *, const char *,
|
|
|
|
PRUint32, PRUint32, PRUint32 *);
|
|
|
|
static NS_METHOD WritePipeSegment(nsIOutputStream *, void *, char *,
|
|
|
|
PRUint32, PRUint32, PRUint32 *);
|
2001-05-11 21:04:09 +00:00
|
|
|
|
|
|
|
private:
|
|
|
|
nsCOMPtr<nsIInterfaceRequestor> mCallbacks;
|
2003-01-18 02:15:14 +00:00
|
|
|
nsCOMPtr<nsITransportEventSink> mTransportSink;
|
2001-06-11 21:20:29 +00:00
|
|
|
nsCOMPtr<nsIEventQueue> mConsumerEventQ;
|
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;
|
2001-05-11 21:04:09 +00:00
|
|
|
|
|
|
|
nsCString mReqHeaderBuf; // flattened request headers
|
2003-01-18 02:15:14 +00:00
|
|
|
nsCOMPtr<nsIInputStream> mRequestStream;
|
|
|
|
PRUint32 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
|
|
|
|
|
|
|
|
PRInt32 mContentLength; // equals -1 if unknown
|
|
|
|
PRUint32 mContentRead; // count of consumed content bytes
|
|
|
|
|
|
|
|
nsHttpChunkedDecoder *mChunkedDecoder;
|
|
|
|
|
|
|
|
nsresult mStatus;
|
|
|
|
|
2003-01-18 02:15:14 +00:00
|
|
|
// this lock is used to protect access to members which may be accessed
|
|
|
|
// from both the main thread as well as the socket thread.
|
|
|
|
PRLock *mLock;
|
|
|
|
|
|
|
|
// these transport status fields are protected by mLock
|
|
|
|
nsresult mTransportStatus;
|
|
|
|
PRUint32 mTransportProgress;
|
|
|
|
PRUint32 mTransportProgressMax;
|
|
|
|
|
|
|
|
PRUint16 mRestartCount; // the number of times this transaction has been restarted
|
|
|
|
PRUint8 mCaps;
|
2001-08-14 01:03:27 +00:00
|
|
|
|
2003-03-26 05:05:49 +00:00
|
|
|
// state flags
|
|
|
|
PRUint32 mClosed : 1;
|
|
|
|
PRUint32 mDestroying : 1;
|
|
|
|
PRUint32 mConnected : 1;
|
|
|
|
PRUint32 mHaveStatusLine : 1;
|
|
|
|
PRUint32 mHaveAllHeaders : 1;
|
|
|
|
PRUint32 mTransactionDone : 1;
|
|
|
|
PRUint32 mResponseIsComplete : 1;
|
|
|
|
PRUint32 mDidContentStart : 1;
|
|
|
|
PRUint32 mNoContent : 1; // expecting an empty entity body
|
2003-04-10 19:06:24 +00:00
|
|
|
PRUint32 mSentData : 1;
|
2003-03-26 05:05:49 +00:00
|
|
|
PRUint32 mReceivedData : 1;
|
|
|
|
PRUint32 mStatusEventPending : 1;
|
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
|
2001-05-11 21:04:09 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
#endif // nsHttpTransaction_h__
|