1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /* ***** BEGIN LICENSE BLOCK *****
3 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
5 * The contents of this file are subject to the Mozilla Public License Version
6 * 1.1 (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 * http://www.mozilla.org/MPL/
10 * Software distributed under the License is distributed on an "AS IS" basis,
11 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 * for the specific language governing rights and limitations under the
15 * The Original Code is Mozilla.
17 * The Initial Developer of the Original Code is
18 * Netscape Communications.
19 * Portions created by the Initial Developer are Copyright (C) 2001
20 * the Initial Developer. All Rights Reserved.
23 * Darin Fisher <darin@netscape.com> (original author)
25 * Alternatively, the contents of this file may be used under the terms of
26 * either the GNU General Public License Version 2 or later (the "GPL"), or
27 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
28 * in which case the provisions of the GPL or the LGPL are applicable instead
29 * of those above. If you wish to allow use of your version of this file only
30 * under the terms of either the GPL or the LGPL, and not to allow others to
31 * use your version of this file under the terms of the MPL, indicate your
32 * decision by deleting the provisions above and replace them with the notice
33 * and other provisions required by the GPL or the LGPL. If you do not delete
34 * the provisions above, a recipient may use your version of this file under
35 * the terms of any one of the MPL, the GPL or the LGPL.
37 * ***** END LICENSE BLOCK ***** */
39 #ifndef nsHttpTransaction_h__
40 #define nsHttpTransaction_h__
43 #include "nsHttpHeaderArray.h"
44 #include "nsAHttpTransaction.h"
45 #include "nsAHttpConnection.h"
50 #include "nsIInputStream.h"
51 #include "nsIOutputStream.h"
52 #include "nsIInterfaceRequestor.h"
53 #include "nsISocketTransportService.h"
54 #include "nsITransport.h"
55 #include "nsIEventTarget.h"
57 //-----------------------------------------------------------------------------
59 class nsHttpTransaction
;
60 class nsHttpRequestHead
;
61 class nsHttpResponseHead
;
62 class nsHttpChunkedDecoder
;
63 class nsIHttpActivityObserver
;
65 //-----------------------------------------------------------------------------
66 // nsHttpTransaction represents a single HTTP transaction. It is thread-safe,
67 // intended to run on the socket thread.
68 //-----------------------------------------------------------------------------
70 class nsHttpTransaction
: public nsAHttpTransaction
71 , public nsIInputStreamCallback
72 , public nsIOutputStreamCallback
76 NS_DECL_NSAHTTPTRANSACTION
77 NS_DECL_NSIINPUTSTREAMCALLBACK
78 NS_DECL_NSIOUTPUTSTREAMCALLBACK
81 virtual ~nsHttpTransaction();
84 // called to initialize the transaction
87 // the transaction capabilities (see nsHttp.h)
89 // the connection type for this transaction.
91 // the request header struct
93 // the request body (POST or PUT data stream)
94 // @param reqBodyIncludesHeaders
95 // fun stuff to support NPAPI plugins.
97 // the dispatch target were notifications should be sent.
99 // the notification callbacks to be given to PSM.
100 // @param responseBody
101 // the input stream that will contain the response data. async
102 // wait on this input stream for data. on first notification,
103 // headers should be available (check transaction status).
105 nsresult
Init(PRUint8 caps
,
106 nsHttpConnectionInfo
*connInfo
,
107 nsHttpRequestHead
*reqHeaders
,
108 nsIInputStream
*reqBody
,
109 PRBool reqBodyIncludesHeaders
,
110 nsIEventTarget
*consumerTarget
,
111 nsIInterfaceRequestor
*callbacks
,
112 nsITransportEventSink
*eventsink
,
113 nsIAsyncInputStream
**responseBody
);
116 PRUint8
Caps() { return mCaps
; }
117 nsHttpConnectionInfo
*ConnectionInfo() { return mConnInfo
; }
118 nsHttpRequestHead
*RequestHead() { return mRequestHead
; }
119 nsHttpResponseHead
*ResponseHead() { return mHaveAllHeaders
? mResponseHead
: nsnull
; }
120 nsISupports
*SecurityInfo() { return mSecurityInfo
; }
122 nsIInterfaceRequestor
*Callbacks() { return mCallbacks
; }
123 nsIEventTarget
*ConsumerTarget() { return mConsumerTarget
; }
124 nsAHttpConnection
*Connection() { return mConnection
; }
126 // Called to take ownership of the response headers; the transaction
127 // will drop any reference to the response headers after this call.
128 nsHttpResponseHead
*TakeResponseHead();
130 // Called to find out if the transaction generated a complete response.
131 PRBool
ResponseIsComplete() { return mResponseIsComplete
; }
133 void SetSSLConnectFailed() { mSSLConnectFailed
= PR_TRUE
; }
134 PRBool
SSLConnectFailed() { return mSSLConnectFailed
; }
136 // These methods may only be used by the connection manager.
137 void SetPriority(PRInt32 priority
) { mPriority
= priority
; }
138 PRInt32
Priority() { return mPriority
; }
142 void ParseLine(char *line
);
143 nsresult
ParseLineSegment(char *seg
, PRUint32 len
);
144 nsresult
ParseHead(char *, PRUint32 count
, PRUint32
*countRead
);
145 nsresult
HandleContentStart();
146 nsresult
HandleContent(char *, PRUint32 count
, PRUint32
*contentRead
, PRUint32
*contentRemaining
);
147 nsresult
ProcessData(char *, PRUint32
, PRUint32
*);
148 void DeleteSelfOnConsumerThread();
150 static NS_METHOD
ReadRequestSegment(nsIInputStream
*, void *, const char *,
151 PRUint32
, PRUint32
, PRUint32
*);
152 static NS_METHOD
WritePipeSegment(nsIOutputStream
*, void *, char *,
153 PRUint32
, PRUint32
, PRUint32
*);
156 nsCOMPtr
<nsIInterfaceRequestor
> mCallbacks
;
157 nsCOMPtr
<nsITransportEventSink
> mTransportSink
;
158 nsCOMPtr
<nsIEventTarget
> mConsumerTarget
;
159 nsCOMPtr
<nsISupports
> mSecurityInfo
;
160 nsCOMPtr
<nsIAsyncInputStream
> mPipeIn
;
161 nsCOMPtr
<nsIAsyncOutputStream
> mPipeOut
;
163 nsCOMPtr
<nsISupports
> mChannel
;
164 nsCOMPtr
<nsIHttpActivityObserver
> mActivityDistributor
;
166 nsCString mReqHeaderBuf
; // flattened request headers
167 nsCOMPtr
<nsIInputStream
> mRequestStream
;
168 PRUint32 mRequestSize
;
170 nsAHttpConnection
*mConnection
; // hard ref
171 nsHttpConnectionInfo
*mConnInfo
; // hard ref
172 nsHttpRequestHead
*mRequestHead
; // weak ref
173 nsHttpResponseHead
*mResponseHead
; // hard ref
175 nsAHttpSegmentReader
*mReader
;
176 nsAHttpSegmentWriter
*mWriter
;
178 nsCString mLineBuf
; // may contain a partial line
180 nsInt64 mContentLength
; // equals -1 if unknown
181 nsInt64 mContentRead
; // count of consumed content bytes
183 nsHttpChunkedDecoder
*mChunkedDecoder
;
189 PRUint16 mRestartCount
; // the number of times this transaction has been restarted
193 PRUint32 mClosed
: 1;
194 PRUint32 mConnected
: 1;
195 PRUint32 mHaveStatusLine
: 1;
196 PRUint32 mHaveAllHeaders
: 1;
197 PRUint32 mTransactionDone
: 1;
198 PRUint32 mResponseIsComplete
: 1;
199 PRUint32 mDidContentStart
: 1;
200 PRUint32 mNoContent
: 1; // expecting an empty entity body
201 PRUint32 mSentData
: 1;
202 PRUint32 mReceivedData
: 1;
203 PRUint32 mStatusEventPending
: 1;
204 PRUint32 mHasRequestBody
: 1;
205 PRUint32 mSSLConnectFailed
: 1;
207 // mClosed := transaction has been explicitly closed
208 // mTransactionDone := transaction ran to completion or was interrupted
209 // mResponseComplete := transaction ran to completion
212 #endif // nsHttpTransaction_h__