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.

268 lines
7.5 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. #include "talk/base/common.h"
  26. #include "talk/base/httpcommon.h"
  27. #include "talk/base/multipart.h"
  28. namespace talk_base {
  29. ///////////////////////////////////////////////////////////////////////////////
  30. // MultipartStream
  31. ///////////////////////////////////////////////////////////////////////////////
  32. MultipartStream::MultipartStream(const std::string& type,
  33. const std::string& boundary)
  34. : type_(type),
  35. boundary_(boundary),
  36. adding_(true),
  37. current_(0),
  38. position_(0) {
  39. // The content type should be multipart/*.
  40. ASSERT(0 == strncmp(type_.c_str(), "multipart/", 10));
  41. }
  42. MultipartStream::~MultipartStream() {
  43. Close();
  44. }
  45. void MultipartStream::GetContentType(std::string* content_type) {
  46. ASSERT(NULL != content_type);
  47. content_type->assign(type_);
  48. content_type->append("; boundary=");
  49. content_type->append(boundary_);
  50. }
  51. bool MultipartStream::AddPart(StreamInterface* data_stream,
  52. const std::string& content_disposition,
  53. const std::string& content_type) {
  54. if (!AddPart("", content_disposition, content_type))
  55. return false;
  56. parts_.push_back(data_stream);
  57. data_stream->SignalEvent.connect(this, &MultipartStream::OnEvent);
  58. return true;
  59. }
  60. bool MultipartStream::AddPart(const std::string& data,
  61. const std::string& content_disposition,
  62. const std::string& content_type) {
  63. ASSERT(adding_);
  64. if (!adding_)
  65. return false;
  66. std::stringstream ss;
  67. if (!parts_.empty()) {
  68. ss << "\r\n";
  69. }
  70. ss << "--" << boundary_ << "\r\n";
  71. if (!content_disposition.empty()) {
  72. ss << ToString(HH_CONTENT_DISPOSITION) << ": "
  73. << content_disposition << "\r\n";
  74. }
  75. if (!content_type.empty()) {
  76. ss << ToString(HH_CONTENT_TYPE) << ": "
  77. << content_type << "\r\n";
  78. }
  79. ss << "\r\n" << data;
  80. parts_.push_back(new MemoryStream(ss.str().data(), ss.str().size()));
  81. return true;
  82. }
  83. void MultipartStream::EndParts() {
  84. ASSERT(adding_);
  85. if (!adding_)
  86. return;
  87. std::stringstream ss;
  88. if (!parts_.empty()) {
  89. ss << "\r\n";
  90. }
  91. ss << "--" << boundary_ << "--" << "\r\n";
  92. parts_.push_back(new MemoryStream(ss.str().data(), ss.str().size()));
  93. ASSERT(0 == current_);
  94. ASSERT(0 == position_);
  95. adding_ = false;
  96. SignalEvent(this, SE_OPEN | SE_READ, 0);
  97. }
  98. size_t MultipartStream::GetPartSize(const std::string& data,
  99. const std::string& content_disposition,
  100. const std::string& content_type) const {
  101. size_t size = 0;
  102. if (!parts_.empty()) {
  103. size += 2; // for "\r\n";
  104. }
  105. size += boundary_.size() + 4; // for "--boundary_\r\n";
  106. if (!content_disposition.empty()) {
  107. // for ToString(HH_CONTENT_DISPOSITION): content_disposition\r\n
  108. size += std::string(ToString(HH_CONTENT_DISPOSITION)).size() + 2 +
  109. content_disposition.size() + 2;
  110. }
  111. if (!content_type.empty()) {
  112. // for ToString(HH_CONTENT_TYPE): content_type\r\n
  113. size += std::string(ToString(HH_CONTENT_TYPE)).size() + 2 +
  114. content_type.size() + 2;
  115. }
  116. size += 2 + data.size(); // for \r\ndata
  117. return size;
  118. }
  119. size_t MultipartStream::GetEndPartSize() const {
  120. size_t size = 0;
  121. if (!parts_.empty()) {
  122. size += 2; // for "\r\n";
  123. }
  124. size += boundary_.size() + 6; // for "--boundary_--\r\n";
  125. return size;
  126. }
  127. //
  128. // StreamInterface
  129. //
  130. StreamState MultipartStream::GetState() const {
  131. if (adding_) {
  132. return SS_OPENING;
  133. }
  134. return (current_ < parts_.size()) ? SS_OPEN : SS_CLOSED;
  135. }
  136. StreamResult MultipartStream::Read(void* buffer, size_t buffer_len,
  137. size_t* read, int* error) {
  138. if (adding_) {
  139. return SR_BLOCK;
  140. }
  141. size_t local_read;
  142. if (!read) read = &local_read;
  143. while (current_ < parts_.size()) {
  144. StreamResult result = parts_[current_]->Read(buffer, buffer_len, read,
  145. error);
  146. if (SR_EOS != result) {
  147. if (SR_SUCCESS == result) {
  148. position_ += *read;
  149. }
  150. return result;
  151. }
  152. ++current_;
  153. }
  154. return SR_EOS;
  155. }
  156. StreamResult MultipartStream::Write(const void* data, size_t data_len,
  157. size_t* written, int* error) {
  158. if (error) {
  159. *error = -1;
  160. }
  161. return SR_ERROR;
  162. }
  163. void MultipartStream::Close() {
  164. for (size_t i = 0; i < parts_.size(); ++i) {
  165. delete parts_[i];
  166. }
  167. parts_.clear();
  168. adding_ = false;
  169. current_ = 0;
  170. position_ = 0;
  171. }
  172. bool MultipartStream::SetPosition(size_t position) {
  173. if (adding_) {
  174. return false;
  175. }
  176. size_t part_size, part_offset = 0;
  177. for (size_t i = 0; i < parts_.size(); ++i) {
  178. if (!parts_[i]->GetSize(&part_size)) {
  179. return false;
  180. }
  181. if (part_offset + part_size > position) {
  182. for (size_t j = i+1; j < _min(parts_.size(), current_+1); ++j) {
  183. if (!parts_[j]->Rewind()) {
  184. return false;
  185. }
  186. }
  187. if (!parts_[i]->SetPosition(position - part_offset)) {
  188. return false;
  189. }
  190. current_ = i;
  191. position_ = position;
  192. return true;
  193. }
  194. part_offset += part_size;
  195. }
  196. return false;
  197. }
  198. bool MultipartStream::GetPosition(size_t* position) const {
  199. if (position) {
  200. *position = position_;
  201. }
  202. return true;
  203. }
  204. bool MultipartStream::GetSize(size_t* size) const {
  205. size_t part_size, total_size = 0;
  206. for (size_t i = 0; i < parts_.size(); ++i) {
  207. if (!parts_[i]->GetSize(&part_size)) {
  208. return false;
  209. }
  210. total_size += part_size;
  211. }
  212. if (size) {
  213. *size = total_size;
  214. }
  215. return true;
  216. }
  217. bool MultipartStream::GetAvailable(size_t* size) const {
  218. if (adding_) {
  219. return false;
  220. }
  221. size_t part_size, total_size = 0;
  222. for (size_t i = current_; i < parts_.size(); ++i) {
  223. if (!parts_[i]->GetAvailable(&part_size)) {
  224. return false;
  225. }
  226. total_size += part_size;
  227. }
  228. if (size) {
  229. *size = total_size;
  230. }
  231. return true;
  232. }
  233. //
  234. // StreamInterface Slots
  235. //
  236. void MultipartStream::OnEvent(StreamInterface* stream, int events, int error) {
  237. if (adding_ || (current_ >= parts_.size()) || (parts_[current_] != stream)) {
  238. return;
  239. }
  240. SignalEvent(this, events, error);
  241. }
  242. } // namespace talk_base