Backed out changeset 2450366cf7ca (bug 1891629) for causing win msix mochitest failures
[gecko.git] / netwerk / streamconv / converters / nsMultiMixedConv.h
blob4f773a69eb67a33dff3516c61881ae08bf92fb33
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
5 #ifndef __nsmultimixedconv__h__
6 #define __nsmultimixedconv__h__
8 #include "nsIStreamConverter.h"
9 #include "nsIChannel.h"
10 #include "nsString.h"
11 #include "nsCOMPtr.h"
12 #include "nsIByteRangeRequest.h"
13 #include "nsIMultiPartChannel.h"
14 #include "mozilla/Attributes.h"
15 #include "mozilla/IncrementalTokenizer.h"
16 #include "nsHttpResponseHead.h"
17 #include "mozilla/UniquePtr.h"
19 #define NS_MULTIMIXEDCONVERTER_CID \
20 { /* 7584CE90-5B25-11d3-A175-0050041CAF44 */ \
21 0x7584ce90, 0x5b25, 0x11d3, { \
22 0xa1, 0x75, 0x0, 0x50, 0x4, 0x1c, 0xaf, 0x44 \
23 } \
27 // nsPartChannel is a "dummy" channel which represents an individual part of
28 // a multipart/mixed stream...
30 // Instances on this channel are passed out to the consumer through the
31 // nsIStreamListener interface.
33 class nsPartChannel final : public nsIChannel,
34 public nsIByteRangeRequest,
35 public nsIMultiPartChannel {
36 public:
37 nsPartChannel(nsIChannel* aMultipartChannel, uint32_t aPartID,
38 bool aIsFirstPart, nsIStreamListener* aListener);
40 void InitializeByteRange(int64_t aStart, int64_t aEnd);
41 void SetIsLastPart() { mIsLastPart = true; }
42 nsresult SendOnStartRequest(nsISupports* aContext);
43 nsresult SendOnDataAvailable(nsISupports* aContext, nsIInputStream* aStream,
44 uint64_t aOffset, uint32_t aLen);
45 nsresult SendOnStopRequest(nsISupports* aContext, nsresult aStatus);
46 /* SetContentDisposition expects the full value of the Content-Disposition
47 * header */
48 void SetContentDisposition(const nsACString& aContentDispositionHeader);
49 // TODO(ER): This appears to be dead code
50 void SetResponseHead(mozilla::net::nsHttpResponseHead* head) {
51 mResponseHead.reset(head);
54 NS_DECL_ISUPPORTS
55 NS_DECL_NSIREQUEST
56 NS_DECL_NSICHANNEL
57 NS_DECL_NSIBYTERANGEREQUEST
58 NS_DECL_NSIMULTIPARTCHANNEL
60 protected:
61 ~nsPartChannel() = default;
63 protected:
64 nsCOMPtr<nsIChannel> mMultipartChannel;
65 nsCOMPtr<nsIStreamListener> mListener;
66 mozilla::UniquePtr<mozilla::net::nsHttpResponseHead> mResponseHead;
68 nsresult mStatus{NS_OK};
69 nsLoadFlags mLoadFlags{0};
71 nsCOMPtr<nsILoadGroup> mLoadGroup;
73 nsCString mContentType;
74 nsCString mContentCharset;
75 uint32_t mContentDisposition{0};
76 nsString mContentDispositionFilename;
77 nsCString mContentDispositionHeader;
78 uint64_t mContentLength{UINT64_MAX};
80 bool mIsByteRangeRequest{false};
81 int64_t mByteRangeStart{0};
82 int64_t mByteRangeEnd{0};
84 uint32_t mPartID; // unique ID that can be used to identify
85 // this part of the multipart document
86 bool mIsFirstPart;
87 bool mIsLastPart{false};
90 // The nsMultiMixedConv stream converter converts a stream of type
91 // "multipart/x-mixed-replace" to it's subparts. There was some debate as to
92 // whether or not the functionality desired when HTTP confronted this type
93 // required a stream converter. After all, this type really prompts various
94 // viewer related actions rather than stream conversion. There simply needs to
95 // be a piece in place that can strip out the multiple parts of a stream of this
96 // type, and "display" them accordingly.
98 // With that said, this "stream converter" spends more time packaging up the sub
99 // parts of the main stream and sending them off the destination stream
100 // listener, than doing any real stream parsing/converting.
102 // WARNING: This converter requires that it's destination stream listener be
103 // able to handle multiple OnStartRequest(), OnDataAvailable(), and
104 // OnStopRequest() call combinations. Each series represents the beginning,
105 // data production, and ending phase of each sub- part of the original
106 // stream.
108 // NOTE: this MIME-type is used by HTTP, *not* SMTP, or IMAP.
110 // NOTE: For reference, a general description of how this MIME type should be
111 // handled via HTTP, see
112 // http://home.netscape.com/assist/net_sites/pushpull.html . Note that real
113 // world server content deviates considerably from this overview.
115 // Implementation assumptions:
116 // Assumed structue:
117 // --BoundaryToken[\r]\n
118 // content-type: foo/bar[\r]\n
119 // ... (other headers if any)
120 // [\r]\n (second line feed to delimit end of headers)
121 // data
122 // --BoundaryToken-- (end delimited by final "--")
124 // linebreaks can be either CRLF or LFLF. linebreaks preceding
125 // boundary tokens are NOT considered part of the data. BoundaryToken
126 // is any opaque string.
130 class nsMultiMixedConv : public nsIStreamConverter {
131 public:
132 NS_DECL_ISUPPORTS
133 NS_DECL_NSISTREAMCONVERTER
134 NS_DECL_NSISTREAMLISTENER
135 NS_DECL_NSITHREADRETARGETABLESTREAMLISTENER
136 NS_DECL_NSIREQUESTOBSERVER
138 explicit nsMultiMixedConv();
140 protected:
141 using Token = mozilla::IncrementalTokenizer::Token;
143 virtual ~nsMultiMixedConv() = default;
145 nsresult SendStart();
146 void AccumulateData(Token const& aToken);
147 nsresult SendData();
148 nsresult SendStop(nsresult aStatus);
150 // member data
151 nsCOMPtr<nsIStreamListener> mFinalListener; // this guy gets the converted
152 // data via his OnDataAvailable()
154 nsCOMPtr<nsIChannel>
155 mChannel; // The channel as we get in in OnStartRequest call
156 RefPtr<nsPartChannel> mPartChannel; // the channel for the given part we're
157 // processing. one channel per part.
158 nsCOMPtr<nsISupports> mContext;
159 nsCString mContentType;
160 nsCString mContentDisposition;
161 nsCString mContentSecurityPolicy;
162 nsCString mRootContentSecurityPolicy;
163 uint64_t mContentLength{UINT64_MAX};
164 uint64_t mTotalSent{0};
166 // The following members are for tracking the byte ranges in
167 // multipart/mixed content which specified the 'Content-Range:'
168 // header...
169 int64_t mByteRangeStart{0};
170 int64_t mByteRangeEnd{0};
171 bool mIsByteRangeRequest{false};
172 // This flag is set first time we create a part channel.
173 // We use it to prevent duplicated OnStopRequest call on the listener
174 // when we fail from some reason to ever create a part channel that
175 // ensures correct notifications.
176 bool mRequestListenerNotified{false};
178 uint32_t mCurrentPartID{0};
180 // Flag preventing reenter of OnDataAvailable in case the target listener
181 // ends up spinning the event loop.
182 bool mInOnDataAvailable{false};
184 // Current state of the incremental parser
185 enum EParserState {
186 PREAMBLE,
187 BOUNDARY_CRLF,
188 HEADER_NAME,
189 HEADER_SEP,
190 HEADER_VALUE,
191 BODY_INIT,
192 BODY,
193 TRAIL_DASH1,
194 TRAIL_DASH2,
195 EPILOGUE,
197 INIT = PREAMBLE
198 } mParserState{INIT};
200 // Response part header value, valid when we find a header name
201 // we recognize.
202 enum EHeader : uint32_t {
203 HEADER_FIRST,
204 HEADER_CONTENT_TYPE = HEADER_FIRST,
205 HEADER_CONTENT_LENGTH,
206 HEADER_CONTENT_DISPOSITION,
207 HEADER_SET_COOKIE,
208 HEADER_CONTENT_RANGE,
209 HEADER_RANGE,
210 HEADER_CONTENT_SECURITY_POLICY,
211 HEADER_UNKNOWN
212 } mResponseHeader{HEADER_UNKNOWN};
213 // Cumulated value of a response header.
214 nsCString mResponseHeaderValue;
216 nsCString mBoundary;
217 mozilla::IncrementalTokenizer mTokenizer;
219 // When in the "body parsing" mode, see below, we cumulate raw data
220 // incrementally to mainly avoid any unnecessary granularity.
221 // mRawData points to the first byte in the tokenizer buffer where part
222 // body data begins or continues. mRawDataLength is a cumulated length
223 // of that data during a single tokenizer input feed. This is always
224 // flushed right after we fed the tokenizer.
225 nsACString::const_char_iterator mRawData{nullptr};
226 nsACString::size_type mRawDataLength{0};
228 // At the start we don't know if the server will be sending boundary with
229 // or without the leading dashes.
230 Token mBoundaryToken;
231 Token mBoundaryTokenWithDashes;
232 // We need these custom tokens to allow finding CRLF when in the binary mode.
233 // CRLF before boundary is considered part of the boundary and not part of
234 // the data.
235 Token mLFToken;
236 Token mCRLFToken;
237 // Custom tokens for each of the response headers we recognize.
238 Token mHeaderTokens[HEADER_UNKNOWN];
240 // Resets values driven by part headers, like content type, to their defaults,
241 // called at the start of every part processing.
242 void HeadersToDefault();
243 // Processes captured value of mResponseHeader header.
244 nsresult ProcessHeader();
245 // Switches the parser and tokenizer state to "binary mode" which only
246 // searches for the 'CRLF boundary' delimiter.
247 void SwitchToBodyParsing();
248 // Switches to the default mode, we are in this mode when parsing headers and
249 // control data around the boundary delimiters.
250 void SwitchToControlParsing();
251 // Turns on or off recognition of the headers we recognize in part heads.
252 void SetHeaderTokensEnabled(bool aEnable);
254 // The main parser callback called by the IncrementalTokenizer
255 // instance from OnDataAvailable or OnStopRequest.
256 nsresult ConsumeToken(Token const& token);
259 #endif /* __nsmultimixedconv__h__ */