nsMultiMixedConv.h 6.6 KB

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