netwerk/streamconv/converters/nsMultiMixedConv.h

Wed, 31 Dec 2014 06:55:46 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:55:46 +0100
changeset 1
ca08bd8f51b2
permissions
-rw-r--r--

Added tag TORBROWSER_REPLICA for changeset 6474c204b198

michael@0 1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
michael@0 2 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 3 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 5 #ifndef __nsmultimixedconv__h__
michael@0 6 #define __nsmultimixedconv__h__
michael@0 7
michael@0 8 #include "nsIStreamConverter.h"
michael@0 9 #include "nsIChannel.h"
michael@0 10 #include "nsString.h"
michael@0 11 #include "nsCOMPtr.h"
michael@0 12 #include "nsIByteRangeRequest.h"
michael@0 13 #include "nsIMultiPartChannel.h"
michael@0 14 #include "nsAutoPtr.h"
michael@0 15 #include "mozilla/Attributes.h"
michael@0 16
michael@0 17 #define NS_MULTIMIXEDCONVERTER_CID \
michael@0 18 { /* 7584CE90-5B25-11d3-A175-0050041CAF44 */ \
michael@0 19 0x7584ce90, \
michael@0 20 0x5b25, \
michael@0 21 0x11d3, \
michael@0 22 {0xa1, 0x75, 0x0, 0x50, 0x4, 0x1c, 0xaf, 0x44} \
michael@0 23 }
michael@0 24
michael@0 25 //
michael@0 26 // nsPartChannel is a "dummy" channel which represents an individual part of
michael@0 27 // a multipart/mixed stream...
michael@0 28 //
michael@0 29 // Instances on this channel are passed out to the consumer through the
michael@0 30 // nsIStreamListener interface.
michael@0 31 //
michael@0 32 class nsPartChannel MOZ_FINAL : public nsIChannel,
michael@0 33 public nsIByteRangeRequest,
michael@0 34 public nsIMultiPartChannel
michael@0 35 {
michael@0 36 public:
michael@0 37 nsPartChannel(nsIChannel *aMultipartChannel, uint32_t aPartID,
michael@0 38 nsIStreamListener* aListener);
michael@0 39
michael@0 40 void InitializeByteRange(int64_t aStart, int64_t aEnd);
michael@0 41 void SetIsLastPart() { mIsLastPart = true; }
michael@0 42 nsresult SendOnStartRequest(nsISupports* aContext);
michael@0 43 nsresult SendOnDataAvailable(nsISupports* aContext, nsIInputStream* aStream,
michael@0 44 uint64_t aOffset, uint32_t aLen);
michael@0 45 nsresult SendOnStopRequest(nsISupports* aContext, nsresult aStatus);
michael@0 46 /* SetContentDisposition expects the full value of the Content-Disposition
michael@0 47 * header */
michael@0 48 void SetContentDisposition(const nsACString& aContentDispositionHeader);
michael@0 49
michael@0 50 NS_DECL_ISUPPORTS
michael@0 51 NS_DECL_NSIREQUEST
michael@0 52 NS_DECL_NSICHANNEL
michael@0 53 NS_DECL_NSIBYTERANGEREQUEST
michael@0 54 NS_DECL_NSIMULTIPARTCHANNEL
michael@0 55
michael@0 56 protected:
michael@0 57 ~nsPartChannel();
michael@0 58
michael@0 59 protected:
michael@0 60 nsCOMPtr<nsIChannel> mMultipartChannel;
michael@0 61 nsCOMPtr<nsIStreamListener> mListener;
michael@0 62
michael@0 63 nsresult mStatus;
michael@0 64 nsLoadFlags mLoadFlags;
michael@0 65
michael@0 66 nsCOMPtr<nsILoadGroup> mLoadGroup;
michael@0 67
michael@0 68 nsCString mContentType;
michael@0 69 nsCString mContentCharset;
michael@0 70 uint32_t mContentDisposition;
michael@0 71 nsString mContentDispositionFilename;
michael@0 72 nsCString mContentDispositionHeader;
michael@0 73 uint64_t mContentLength;
michael@0 74
michael@0 75 bool mIsByteRangeRequest;
michael@0 76 int64_t mByteRangeStart;
michael@0 77 int64_t mByteRangeEnd;
michael@0 78
michael@0 79 uint32_t mPartID; // unique ID that can be used to identify
michael@0 80 // this part of the multipart document
michael@0 81 bool mIsLastPart;
michael@0 82 };
michael@0 83
michael@0 84 // The nsMultiMixedConv stream converter converts a stream of type "multipart/x-mixed-replace"
michael@0 85 // to it's subparts. There was some debate as to whether or not the functionality desired
michael@0 86 // when HTTP confronted this type required a stream converter. After all, this type really
michael@0 87 // prompts various viewer related actions rather than stream conversion. There simply needs
michael@0 88 // to be a piece in place that can strip out the multiple parts of a stream of this type, and
michael@0 89 // "display" them accordingly.
michael@0 90 //
michael@0 91 // With that said, this "stream converter" spends more time packaging up the sub parts of the
michael@0 92 // main stream and sending them off the destination stream listener, than doing any real
michael@0 93 // stream parsing/converting.
michael@0 94 //
michael@0 95 // WARNING: This converter requires that it's destination stream listener be able to handle
michael@0 96 // multiple OnStartRequest(), OnDataAvailable(), and OnStopRequest() call combinations.
michael@0 97 // Each series represents the beginning, data production, and ending phase of each sub-
michael@0 98 // part of the original stream.
michael@0 99 //
michael@0 100 // NOTE: this MIME-type is used by HTTP, *not* SMTP, or IMAP.
michael@0 101 //
michael@0 102 // NOTE: For reference, a general description of how this MIME type should be handled via
michael@0 103 // HTTP, see http://home.netscape.com/assist/net_sites/pushpull.html . Note that
michael@0 104 // real world server content deviates considerably from this overview.
michael@0 105 //
michael@0 106 // Implementation assumptions:
michael@0 107 // Assumed structue:
michael@0 108 // --BoundaryToken[\r]\n
michael@0 109 // content-type: foo/bar[\r]\n
michael@0 110 // ... (other headers if any)
michael@0 111 // [\r]\n (second line feed to delimit end of headers)
michael@0 112 // data
michael@0 113 // --BoundaryToken-- (end delimited by final "--")
michael@0 114 //
michael@0 115 // linebreaks can be either CRLF or LFLF. linebreaks preceding
michael@0 116 // boundary tokens are considered part of the data. BoundaryToken
michael@0 117 // is any opaque string.
michael@0 118 //
michael@0 119 //
michael@0 120
michael@0 121 class nsMultiMixedConv : public nsIStreamConverter {
michael@0 122 public:
michael@0 123 NS_DECL_ISUPPORTS
michael@0 124 NS_DECL_NSISTREAMCONVERTER
michael@0 125 NS_DECL_NSISTREAMLISTENER
michael@0 126 NS_DECL_NSIREQUESTOBSERVER
michael@0 127
michael@0 128 nsMultiMixedConv();
michael@0 129 virtual ~nsMultiMixedConv();
michael@0 130
michael@0 131 protected:
michael@0 132 nsresult SendStart(nsIChannel *aChannel);
michael@0 133 nsresult SendStop(nsresult aStatus);
michael@0 134 nsresult SendData(char *aBuffer, uint32_t aLen);
michael@0 135 nsresult ParseHeaders(nsIChannel *aChannel, char *&aPtr,
michael@0 136 uint32_t &aLen, bool *_retval);
michael@0 137 int32_t PushOverLine(char *&aPtr, uint32_t &aLen);
michael@0 138 char *FindToken(char *aCursor, uint32_t aLen);
michael@0 139 nsresult BufferData(char *aData, uint32_t aLen);
michael@0 140
michael@0 141 // member data
michael@0 142 bool mNewPart; // Are we processing the beginning of a part?
michael@0 143 bool mProcessingHeaders;
michael@0 144 nsCOMPtr<nsIStreamListener> mFinalListener; // this guy gets the converted data via his OnDataAvailable()
michael@0 145
michael@0 146 nsCString mToken;
michael@0 147 uint32_t mTokenLen;
michael@0 148
michael@0 149 nsRefPtr<nsPartChannel> mPartChannel; // the channel for the given part we're processing.
michael@0 150 // one channel per part.
michael@0 151 nsCOMPtr<nsISupports> mContext;
michael@0 152 nsCString mContentType;
michael@0 153 nsCString mContentDisposition;
michael@0 154 uint64_t mContentLength;
michael@0 155
michael@0 156 char *mBuffer;
michael@0 157 uint32_t mBufLen;
michael@0 158 uint64_t mTotalSent;
michael@0 159 bool mFirstOnData; // used to determine if we're in our first OnData callback.
michael@0 160
michael@0 161 // The following members are for tracking the byte ranges in
michael@0 162 // multipart/mixed content which specified the 'Content-Range:'
michael@0 163 // header...
michael@0 164 int64_t mByteRangeStart;
michael@0 165 int64_t mByteRangeEnd;
michael@0 166 bool mIsByteRangeRequest;
michael@0 167
michael@0 168 uint32_t mCurrentPartID;
michael@0 169 };
michael@0 170
michael@0 171 #endif /* __nsmultimixedconv__h__ */

mercurial