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.org code.
17 * The Initial Developer of the Original Code is
18 * Netscape Communications Corporation.
19 * Portions created by the Initial Developer are Copyright (C) 1998
20 * the Initial Developer. All Rights Reserved.
24 * Alternatively, the contents of this file may be used under the terms of
25 * either the GNU General Public License Version 2 or later (the "GPL"), or
26 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
27 * in which case the provisions of the GPL or the LGPL are applicable instead
28 * of those above. If you wish to allow use of your version of this file only
29 * under the terms of either the GPL or the LGPL, and not to allow others to
30 * use your version of this file under the terms of the MPL, indicate your
31 * decision by deleting the provisions above and replace them with the notice
32 * and other provisions required by the GPL or the LGPL. If you do not delete
33 * the provisions above, a recipient may use your version of this file under
34 * the terms of any one of the MPL, the GPL or the LGPL.
36 * ***** END LICENSE BLOCK ***** */
37 #ifndef __nsmultimixedconv__h__
38 #define __nsmultimixedconv__h__
40 #include "nsIStreamConverter.h"
41 #include "nsIChannel.h"
44 #include "nsXPIDLString.h"
47 #include "nsIByteRangeRequest.h"
48 #include "nsIMultiPartChannel.h"
49 #include "nsAutoPtr.h"
51 #define NS_MULTIMIXEDCONVERTER_CID \
52 { /* 7584CE90-5B25-11d3-A175-0050041CAF44 */ \
56 {0xa1, 0x75, 0x0, 0x50, 0x4, 0x1c, 0xaf, 0x44} \
60 // nsPartChannel is a "dummy" channel which represents an individual part of
61 // a multipart/mixed stream...
63 // Instances on this channel are passed out to the consumer through the
64 // nsIStreamListener interface.
66 class nsPartChannel
: public nsIChannel
,
67 public nsIByteRangeRequest
,
68 public nsIMultiPartChannel
71 nsPartChannel(nsIChannel
*aMultipartChannel
, PRUint32 aPartID
);
73 void InitializeByteRange(PRInt64 aStart
, PRInt64 aEnd
);
74 void SetIsLastPart() { mIsLastPart
= PR_TRUE
; }
79 NS_DECL_NSIBYTERANGEREQUEST
80 NS_DECL_NSIMULTIPARTCHANNEL
86 nsCOMPtr
<nsIChannel
> mMultipartChannel
;
89 nsLoadFlags mLoadFlags
;
91 nsCOMPtr
<nsILoadGroup
> mLoadGroup
;
93 nsCString mContentType
;
94 nsCString mContentCharset
;
95 nsCString mContentDisposition
;
96 nsUint64 mContentLength
;
98 PRBool mIsByteRangeRequest
;
99 nsInt64 mByteRangeStart
;
100 nsInt64 mByteRangeEnd
;
102 PRUint32 mPartID
; // unique ID that can be used to identify
103 // this part of the multipart document
107 // The nsMultiMixedConv stream converter converts a stream of type "multipart/x-mixed-replace"
108 // to it's subparts. There was some debate as to whether or not the functionality desired
109 // when HTTP confronted this type required a stream converter. After all, this type really
110 // prompts various viewer related actions rather than stream conversion. There simply needs
111 // to be a piece in place that can strip out the multiple parts of a stream of this type, and
112 // "display" them accordingly.
114 // With that said, this "stream converter" spends more time packaging up the sub parts of the
115 // main stream and sending them off the destination stream listener, than doing any real
116 // stream parsing/converting.
118 // WARNING: This converter requires that it's destination stream listener be able to handle
119 // multiple OnStartRequest(), OnDataAvailable(), and OnStopRequest() call combinations.
120 // Each series represents the beginning, data production, and ending phase of each sub-
121 // part of the original stream.
123 // NOTE: this MIME-type is used by HTTP, *not* SMTP, or IMAP.
125 // NOTE: For reference, a general description of how this MIME type should be handled via
126 // HTTP, see http://home.netscape.com/assist/net_sites/pushpull.html . Note that
127 // real world server content deviates considerably from this overview.
129 // Implementation assumptions:
131 // --BoundaryToken[\r]\n
132 // content-type: foo/bar[\r]\n
133 // ... (other headers if any)
134 // [\r]\n (second line feed to delimit end of headers)
136 // --BoundaryToken-- (end delimited by final "--")
138 // linebreaks can be either CRLF or LFLF. linebreaks preceeding
139 // boundary tokens are considered part of the data. BoundaryToken
140 // is any opaque string.
144 class nsMultiMixedConv
: public nsIStreamConverter
{
147 NS_DECL_NSISTREAMCONVERTER
148 NS_DECL_NSISTREAMLISTENER
149 NS_DECL_NSIREQUESTOBSERVER
152 virtual ~nsMultiMixedConv();
155 nsresult
SendStart(nsIChannel
*aChannel
);
156 nsresult
SendStop(nsresult aStatus
);
157 nsresult
SendData(char *aBuffer
, PRUint32 aLen
);
158 nsresult
ParseHeaders(nsIChannel
*aChannel
, char *&aPtr
,
159 PRUint32
&aLen
, PRBool
*_retval
);
160 PRInt32
PushOverLine(char *&aPtr
, PRUint32
&aLen
);
161 char *FindToken(char *aCursor
, PRUint32 aLen
);
162 nsresult
BufferData(char *aData
, PRUint32 aLen
);
165 PRBool mNewPart
; // Are we processing the beginning of a part?
166 PRBool mProcessingHeaders
;
167 nsCOMPtr
<nsIStreamListener
> mFinalListener
; // this guy gets the converted data via his OnDataAvailable()
172 nsRefPtr
<nsPartChannel
> mPartChannel
; // the channel for the given part we're processing.
173 // one channel per part.
174 nsCOMPtr
<nsISupports
> mContext
;
175 nsCString mContentType
;
176 nsCString mContentDisposition
;
177 nsUint64 mContentLength
;
182 PRBool mFirstOnData
; // used to determine if we're in our first OnData callback.
184 // The following members are for tracking the byte ranges in
185 // multipart/mixed content which specified the 'Content-Range:'
187 nsInt64 mByteRangeStart
;
188 nsInt64 mByteRangeEnd
;
189 PRBool mIsByteRangeRequest
;
191 PRUint32 mCurrentPartID
;
194 #endif /* __nsmultimixedconv__h__ */