michael@0: /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: #ifndef __nsmultimixedconv__h__ michael@0: #define __nsmultimixedconv__h__ michael@0: michael@0: #include "nsIStreamConverter.h" michael@0: #include "nsIChannel.h" michael@0: #include "nsString.h" michael@0: #include "nsCOMPtr.h" michael@0: #include "nsIByteRangeRequest.h" michael@0: #include "nsIMultiPartChannel.h" michael@0: #include "nsAutoPtr.h" michael@0: #include "mozilla/Attributes.h" michael@0: michael@0: #define NS_MULTIMIXEDCONVERTER_CID \ michael@0: { /* 7584CE90-5B25-11d3-A175-0050041CAF44 */ \ michael@0: 0x7584ce90, \ michael@0: 0x5b25, \ michael@0: 0x11d3, \ michael@0: {0xa1, 0x75, 0x0, 0x50, 0x4, 0x1c, 0xaf, 0x44} \ michael@0: } michael@0: michael@0: // michael@0: // nsPartChannel is a "dummy" channel which represents an individual part of michael@0: // a multipart/mixed stream... michael@0: // michael@0: // Instances on this channel are passed out to the consumer through the michael@0: // nsIStreamListener interface. michael@0: // michael@0: class nsPartChannel MOZ_FINAL : public nsIChannel, michael@0: public nsIByteRangeRequest, michael@0: public nsIMultiPartChannel michael@0: { michael@0: public: michael@0: nsPartChannel(nsIChannel *aMultipartChannel, uint32_t aPartID, michael@0: nsIStreamListener* aListener); michael@0: michael@0: void InitializeByteRange(int64_t aStart, int64_t aEnd); michael@0: void SetIsLastPart() { mIsLastPart = true; } michael@0: nsresult SendOnStartRequest(nsISupports* aContext); michael@0: nsresult SendOnDataAvailable(nsISupports* aContext, nsIInputStream* aStream, michael@0: uint64_t aOffset, uint32_t aLen); michael@0: nsresult SendOnStopRequest(nsISupports* aContext, nsresult aStatus); michael@0: /* SetContentDisposition expects the full value of the Content-Disposition michael@0: * header */ michael@0: void SetContentDisposition(const nsACString& aContentDispositionHeader); michael@0: michael@0: NS_DECL_ISUPPORTS michael@0: NS_DECL_NSIREQUEST michael@0: NS_DECL_NSICHANNEL michael@0: NS_DECL_NSIBYTERANGEREQUEST michael@0: NS_DECL_NSIMULTIPARTCHANNEL michael@0: michael@0: protected: michael@0: ~nsPartChannel(); michael@0: michael@0: protected: michael@0: nsCOMPtr mMultipartChannel; michael@0: nsCOMPtr mListener; michael@0: michael@0: nsresult mStatus; michael@0: nsLoadFlags mLoadFlags; michael@0: michael@0: nsCOMPtr mLoadGroup; michael@0: michael@0: nsCString mContentType; michael@0: nsCString mContentCharset; michael@0: uint32_t mContentDisposition; michael@0: nsString mContentDispositionFilename; michael@0: nsCString mContentDispositionHeader; michael@0: uint64_t mContentLength; michael@0: michael@0: bool mIsByteRangeRequest; michael@0: int64_t mByteRangeStart; michael@0: int64_t mByteRangeEnd; michael@0: michael@0: uint32_t mPartID; // unique ID that can be used to identify michael@0: // this part of the multipart document michael@0: bool mIsLastPart; michael@0: }; michael@0: michael@0: // The nsMultiMixedConv stream converter converts a stream of type "multipart/x-mixed-replace" michael@0: // to it's subparts. There was some debate as to whether or not the functionality desired michael@0: // when HTTP confronted this type required a stream converter. After all, this type really michael@0: // prompts various viewer related actions rather than stream conversion. There simply needs michael@0: // to be a piece in place that can strip out the multiple parts of a stream of this type, and michael@0: // "display" them accordingly. michael@0: // michael@0: // With that said, this "stream converter" spends more time packaging up the sub parts of the michael@0: // main stream and sending them off the destination stream listener, than doing any real michael@0: // stream parsing/converting. michael@0: // michael@0: // WARNING: This converter requires that it's destination stream listener be able to handle michael@0: // multiple OnStartRequest(), OnDataAvailable(), and OnStopRequest() call combinations. michael@0: // Each series represents the beginning, data production, and ending phase of each sub- michael@0: // part of the original stream. michael@0: // michael@0: // NOTE: this MIME-type is used by HTTP, *not* SMTP, or IMAP. michael@0: // michael@0: // NOTE: For reference, a general description of how this MIME type should be handled via michael@0: // HTTP, see http://home.netscape.com/assist/net_sites/pushpull.html . Note that michael@0: // real world server content deviates considerably from this overview. michael@0: // michael@0: // Implementation assumptions: michael@0: // Assumed structue: michael@0: // --BoundaryToken[\r]\n michael@0: // content-type: foo/bar[\r]\n michael@0: // ... (other headers if any) michael@0: // [\r]\n (second line feed to delimit end of headers) michael@0: // data michael@0: // --BoundaryToken-- (end delimited by final "--") michael@0: // michael@0: // linebreaks can be either CRLF or LFLF. linebreaks preceding michael@0: // boundary tokens are considered part of the data. BoundaryToken michael@0: // is any opaque string. michael@0: // michael@0: // michael@0: michael@0: class nsMultiMixedConv : public nsIStreamConverter { michael@0: public: michael@0: NS_DECL_ISUPPORTS michael@0: NS_DECL_NSISTREAMCONVERTER michael@0: NS_DECL_NSISTREAMLISTENER michael@0: NS_DECL_NSIREQUESTOBSERVER michael@0: michael@0: nsMultiMixedConv(); michael@0: virtual ~nsMultiMixedConv(); michael@0: michael@0: protected: michael@0: nsresult SendStart(nsIChannel *aChannel); michael@0: nsresult SendStop(nsresult aStatus); michael@0: nsresult SendData(char *aBuffer, uint32_t aLen); michael@0: nsresult ParseHeaders(nsIChannel *aChannel, char *&aPtr, michael@0: uint32_t &aLen, bool *_retval); michael@0: int32_t PushOverLine(char *&aPtr, uint32_t &aLen); michael@0: char *FindToken(char *aCursor, uint32_t aLen); michael@0: nsresult BufferData(char *aData, uint32_t aLen); michael@0: michael@0: // member data michael@0: bool mNewPart; // Are we processing the beginning of a part? michael@0: bool mProcessingHeaders; michael@0: nsCOMPtr mFinalListener; // this guy gets the converted data via his OnDataAvailable() michael@0: michael@0: nsCString mToken; michael@0: uint32_t mTokenLen; michael@0: michael@0: nsRefPtr mPartChannel; // the channel for the given part we're processing. michael@0: // one channel per part. michael@0: nsCOMPtr mContext; michael@0: nsCString mContentType; michael@0: nsCString mContentDisposition; michael@0: uint64_t mContentLength; michael@0: michael@0: char *mBuffer; michael@0: uint32_t mBufLen; michael@0: uint64_t mTotalSent; michael@0: bool mFirstOnData; // used to determine if we're in our first OnData callback. michael@0: michael@0: // The following members are for tracking the byte ranges in michael@0: // multipart/mixed content which specified the 'Content-Range:' michael@0: // header... michael@0: int64_t mByteRangeStart; michael@0: int64_t mByteRangeEnd; michael@0: bool mIsByteRangeRequest; michael@0: michael@0: uint32_t mCurrentPartID; michael@0: }; michael@0: michael@0: #endif /* __nsmultimixedconv__h__ */