You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

94 lines
3.8 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. // libjingle
  2. // Copyright 2004--2010, Google Inc.
  3. //
  4. // Redistribution and use in source and binary forms, with or without
  5. // modification, are permitted provided that the following conditions are met:
  6. //
  7. // 1. Redistributions of source code must retain the above copyright notice,
  8. // this list of conditions and the following disclaimer.
  9. // 2. Redistributions in binary form must reproduce the above copyright notice,
  10. // this list of conditions and the following disclaimer in the documentation
  11. // and/or other materials provided with the distribution.
  12. // 3. The name of the author may not be used to endorse or promote products
  13. // derived from this software without specific prior written permission.
  14. //
  15. // THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
  16. // WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  17. // MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
  18. // EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  19. // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  20. // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
  21. // OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  22. // WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
  23. // OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  24. // ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. #ifndef TALK_BASE_MULTIPART_H__
  26. #define TALK_BASE_MULTIPART_H__
  27. #include <string>
  28. #include <vector>
  29. #include "talk/base/sigslot.h"
  30. #include "talk/base/stream.h"
  31. namespace talk_base {
  32. ///////////////////////////////////////////////////////////////////////////////
  33. // MultipartStream - Implements an RFC2046 multipart stream by concatenating
  34. // the supplied parts together, and adding the correct boundaries.
  35. ///////////////////////////////////////////////////////////////////////////////
  36. class MultipartStream : public StreamInterface, public sigslot::has_slots<> {
  37. public:
  38. MultipartStream(const std::string& type, const std::string& boundary);
  39. virtual ~MultipartStream();
  40. void GetContentType(std::string* content_type);
  41. // Note: If content_disposition and/or content_type are the empty string,
  42. // they will be omitted.
  43. bool AddPart(StreamInterface* data_stream,
  44. const std::string& content_disposition,
  45. const std::string& content_type);
  46. bool AddPart(const std::string& data,
  47. const std::string& content_disposition,
  48. const std::string& content_type);
  49. void EndParts();
  50. // Calculates the size of a part before actually adding the part.
  51. size_t GetPartSize(const std::string& data,
  52. const std::string& content_disposition,
  53. const std::string& content_type) const;
  54. size_t GetEndPartSize() const;
  55. // StreamInterface
  56. virtual StreamState GetState() const;
  57. virtual StreamResult Read(void* buffer, size_t buffer_len,
  58. size_t* read, int* error);
  59. virtual StreamResult Write(const void* data, size_t data_len,
  60. size_t* written, int* error);
  61. virtual void Close();
  62. virtual bool SetPosition(size_t position);
  63. virtual bool GetPosition(size_t* position) const;
  64. virtual bool GetSize(size_t* size) const;
  65. virtual bool GetAvailable(size_t* size) const;
  66. private:
  67. typedef std::vector<StreamInterface*> PartList;
  68. // StreamInterface Slots
  69. void OnEvent(StreamInterface* stream, int events, int error);
  70. std::string type_, boundary_;
  71. PartList parts_;
  72. bool adding_;
  73. size_t current_; // The index into parts_ of the current read position.
  74. size_t position_; // The current read position in bytes.
  75. DISALLOW_COPY_AND_ASSIGN(MultipartStream);
  76. };
  77. } // namespace talk_base
  78. #endif // TALK_BASE_MULTIPART_H__