ChunkedDecodingStreamFilter.cc 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. /* <!-- copyright */
  2. /*
  3. * aria2 - The high speed download utility
  4. *
  5. * Copyright (C) 2010 Tatsuhiro Tsujikawa
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. *
  21. * In addition, as a special exception, the copyright holders give
  22. * permission to link the code of portions of this program with the
  23. * OpenSSL library under certain conditions as described in each
  24. * individual source file, and distribute linked combinations
  25. * including the two.
  26. * You must obey the GNU General Public License in all respects
  27. * for all of the code used other than OpenSSL. If you modify
  28. * file(s) with this exception, you may extend this exception to your
  29. * version of the file(s), but you are not obligated to do so. If you
  30. * do not wish to do so, delete this exception statement from your
  31. * version. If you delete this exception statement from all source
  32. * files in the program, then also delete it here.
  33. */
  34. /* copyright --> */
  35. #include "ChunkedDecodingStreamFilter.h"
  36. #include <cassert>
  37. #include "util.h"
  38. #include "message.h"
  39. #include "DlAbortEx.h"
  40. #include "StringFormat.h"
  41. #include "A2STR.h"
  42. namespace aria2 {
  43. const std::string ChunkedDecodingStreamFilter::NAME
  44. ("ChunkedDecodingStreamFilter");
  45. size_t ChunkedDecodingStreamFilter::MAX_BUF_SIZE = 1024*1024;
  46. ChunkedDecodingStreamFilter::ReadChunkSizeStateHandler*
  47. ChunkedDecodingStreamFilter::readChunkSizeStateHandler_ =
  48. new ChunkedDecodingStreamFilter::ReadChunkSizeStateHandler();
  49. ChunkedDecodingStreamFilter::ReadTrailerStateHandler*
  50. ChunkedDecodingStreamFilter::readTrailerStateHandler_ =
  51. new ChunkedDecodingStreamFilter::ReadTrailerStateHandler();
  52. ChunkedDecodingStreamFilter::ReadDataStateHandler*
  53. ChunkedDecodingStreamFilter::readDataStateHandler_ =
  54. new ChunkedDecodingStreamFilter::ReadDataStateHandler();
  55. ChunkedDecodingStreamFilter::ReadDataEndStateHandler*
  56. ChunkedDecodingStreamFilter::readDataEndStateHandler_ =
  57. new ChunkedDecodingStreamFilter::ReadDataEndStateHandler();
  58. ChunkedDecodingStreamFilter::StreamEndStatehandler*
  59. ChunkedDecodingStreamFilter::streamEndStateHandler_ =
  60. new ChunkedDecodingStreamFilter::StreamEndStatehandler();
  61. ChunkedDecodingStreamFilter::ChunkedDecodingStreamFilter
  62. (const SharedHandle<StreamFilter>& delegate):
  63. StreamFilter(delegate),
  64. state_(readChunkSizeStateHandler_),
  65. chunkSize_(0),
  66. bytesProcessed_(0) {}
  67. ChunkedDecodingStreamFilter::~ChunkedDecodingStreamFilter() {}
  68. void ChunkedDecodingStreamFilter::init() {}
  69. bool ChunkedDecodingStreamFilter::readChunkSize
  70. (size_t& inbufOffset, const unsigned char* inbuf, size_t inlen)
  71. {
  72. size_t pbufSize = buf_.size();
  73. buf_.append(&inbuf[inbufOffset], &inbuf[inlen]);
  74. std::string::size_type crlfPos = buf_.find(A2STR::CRLF);
  75. if(crlfPos == std::string::npos) {
  76. if(buf_.size() > MAX_BUF_SIZE) {
  77. throw DL_ABORT_EX("Could not find chunk size before buffer got full.");
  78. }
  79. inbufOffset = inlen;
  80. return false;
  81. }
  82. std::string::size_type extPos = buf_.find(A2STR::SEMICOLON_C);
  83. if(extPos == std::string::npos || crlfPos < extPos) {
  84. extPos = crlfPos;
  85. }
  86. chunkSize_ = util::parseULLInt(buf_.substr(0, extPos), 16);
  87. assert(crlfPos+2 > pbufSize);
  88. inbufOffset += crlfPos+2-pbufSize;
  89. buf_.clear();
  90. if(chunkSize_ == 0) {
  91. state_ = readTrailerStateHandler_;
  92. } else {
  93. state_ = readDataStateHandler_;
  94. }
  95. return true;
  96. }
  97. bool ChunkedDecodingStreamFilter::readTrailer
  98. (size_t& inbufOffset, const unsigned char* inbuf, size_t inlen)
  99. {
  100. size_t pbufSize = buf_.size();
  101. buf_.append(&inbuf[inbufOffset], &inbuf[inlen]);
  102. std::string::size_type crlfcrlfPos = buf_.find("\r\n\r\n");
  103. if(crlfcrlfPos != std::string::npos) {
  104. // TODO crlfcrlfPos == 0 case?
  105. inbufOffset += crlfcrlfPos+4-pbufSize;
  106. inbufOffset = inlen;
  107. buf_.clear();
  108. state_ = streamEndStateHandler_;
  109. return true;
  110. } else {
  111. std::string::size_type crlfPos = buf_.find(A2STR::CRLF);
  112. if(crlfPos == std::string::npos) {
  113. if(buf_.size() > MAX_BUF_SIZE) {
  114. throw DL_ABORT_EX
  115. ("Could not find end of stream before buffer got full.");
  116. }
  117. inbufOffset = inlen;
  118. return false;
  119. } else if(crlfPos == 0) {
  120. inbufOffset += crlfPos+2-pbufSize;
  121. buf_.clear();
  122. state_ = streamEndStateHandler_;
  123. return true;
  124. } else {
  125. if(buf_.size() > MAX_BUF_SIZE) {
  126. throw DL_ABORT_EX
  127. ("Could not find end of stream before buffer got full.");
  128. }
  129. inbufOffset = inlen;
  130. return false;
  131. }
  132. }
  133. }
  134. bool ChunkedDecodingStreamFilter::readData
  135. (ssize_t& outlen,
  136. size_t& inbufOffset,
  137. const unsigned char* inbuf,
  138. size_t inlen,
  139. const SharedHandle<BinaryStream>& out,
  140. const SharedHandle<Segment>& segment)
  141. {
  142. uint64_t readlen =
  143. std::min(chunkSize_, static_cast<uint64_t>(inlen-inbufOffset));
  144. outlen += getDelegate()->transform(out, segment, inbuf+inbufOffset, readlen);
  145. chunkSize_ -= readlen;
  146. inbufOffset += readlen;
  147. if(chunkSize_ == 0) {
  148. state_ = readDataEndStateHandler_;
  149. return true;
  150. } else {
  151. return false;
  152. }
  153. }
  154. bool ChunkedDecodingStreamFilter::readDataEnd
  155. (size_t& inbufOffset, const unsigned char* inbuf, size_t inlen)
  156. {
  157. size_t pbufSize = buf_.size();
  158. buf_.append(&inbuf[inbufOffset], &inbuf[inlen]);
  159. if(buf_.size() >= 2) {
  160. if(util::startsWith(buf_, A2STR::CRLF)) {
  161. inbufOffset += 2-pbufSize;
  162. buf_.clear();
  163. state_ = readChunkSizeStateHandler_;
  164. return true;
  165. } else {
  166. throw DL_ABORT_EX("No CRLF at the end of chunk.");
  167. }
  168. } else {
  169. inbufOffset = inlen;
  170. return false;
  171. }
  172. }
  173. ssize_t ChunkedDecodingStreamFilter::transform
  174. (const SharedHandle<BinaryStream>& out,
  175. const SharedHandle<Segment>& segment,
  176. const unsigned char* inbuf, size_t inlen)
  177. {
  178. size_t inbufOffset = 0;
  179. ssize_t outlen = 0;
  180. while(inbufOffset < inlen) {
  181. ssize_t olen = 0;
  182. bool r = state_->execute
  183. (this, olen, inbufOffset, inbuf, inlen, out, segment);
  184. outlen += olen;
  185. if(!r) {
  186. break;
  187. }
  188. }
  189. bytesProcessed_ = inbufOffset;
  190. return outlen;
  191. }
  192. bool ChunkedDecodingStreamFilter::finished()
  193. {
  194. return state_ == streamEndStateHandler_ && getDelegate()->finished();
  195. }
  196. void ChunkedDecodingStreamFilter::release() {}
  197. const std::string& ChunkedDecodingStreamFilter::getName() const
  198. {
  199. return NAME;
  200. }
  201. } // namespace aria2