ChunkedDecodingStreamFilter.cc 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  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 "A2STR.h"
  41. namespace aria2 {
  42. const std::string ChunkedDecodingStreamFilter::NAME
  43. ("ChunkedDecodingStreamFilter");
  44. size_t ChunkedDecodingStreamFilter::MAX_BUF_SIZE = 1024*1024;
  45. ChunkedDecodingStreamFilter::ReadChunkSizeStateHandler*
  46. ChunkedDecodingStreamFilter::readChunkSizeStateHandler_ =
  47. new ChunkedDecodingStreamFilter::ReadChunkSizeStateHandler();
  48. ChunkedDecodingStreamFilter::ReadTrailerStateHandler*
  49. ChunkedDecodingStreamFilter::readTrailerStateHandler_ =
  50. new ChunkedDecodingStreamFilter::ReadTrailerStateHandler();
  51. ChunkedDecodingStreamFilter::ReadDataStateHandler*
  52. ChunkedDecodingStreamFilter::readDataStateHandler_ =
  53. new ChunkedDecodingStreamFilter::ReadDataStateHandler();
  54. ChunkedDecodingStreamFilter::ReadDataEndStateHandler*
  55. ChunkedDecodingStreamFilter::readDataEndStateHandler_ =
  56. new ChunkedDecodingStreamFilter::ReadDataEndStateHandler();
  57. ChunkedDecodingStreamFilter::StreamEndStatehandler*
  58. ChunkedDecodingStreamFilter::streamEndStateHandler_ =
  59. new ChunkedDecodingStreamFilter::StreamEndStatehandler();
  60. ChunkedDecodingStreamFilter::ChunkedDecodingStreamFilter
  61. (const SharedHandle<StreamFilter>& delegate):
  62. StreamFilter(delegate),
  63. state_(readChunkSizeStateHandler_),
  64. chunkSize_(0),
  65. bytesProcessed_(0) {}
  66. ChunkedDecodingStreamFilter::~ChunkedDecodingStreamFilter() {}
  67. void ChunkedDecodingStreamFilter::init() {}
  68. bool ChunkedDecodingStreamFilter::readChunkSize
  69. (size_t& inbufOffset, const unsigned char* inbuf, size_t inlen)
  70. {
  71. size_t pbufSize = buf_.size();
  72. buf_.append(&inbuf[inbufOffset], &inbuf[inlen]);
  73. std::string::size_type crlfPos = buf_.find(A2STR::CRLF);
  74. if(crlfPos == std::string::npos) {
  75. if(buf_.size() > MAX_BUF_SIZE) {
  76. throw DL_ABORT_EX("Could not find chunk size before buffer got full.");
  77. }
  78. inbufOffset = inlen;
  79. return false;
  80. }
  81. std::string::size_type extPos = buf_.find(A2STR::SEMICOLON_C);
  82. if(extPos == std::string::npos || crlfPos < extPos) {
  83. extPos = crlfPos;
  84. }
  85. chunkSize_ = util::parseULLInt(buf_.substr(0, extPos), 16);
  86. assert(crlfPos+2 > pbufSize);
  87. inbufOffset += crlfPos+2-pbufSize;
  88. buf_.clear();
  89. if(chunkSize_ == 0) {
  90. state_ = readTrailerStateHandler_;
  91. } else {
  92. state_ = readDataStateHandler_;
  93. }
  94. return true;
  95. }
  96. bool ChunkedDecodingStreamFilter::readTrailer
  97. (size_t& inbufOffset, const unsigned char* inbuf, size_t inlen)
  98. {
  99. size_t pbufSize = buf_.size();
  100. buf_.append(&inbuf[inbufOffset], &inbuf[inlen]);
  101. std::string::size_type crlfcrlfPos = buf_.find("\r\n\r\n");
  102. if(crlfcrlfPos != std::string::npos) {
  103. // TODO crlfcrlfPos == 0 case?
  104. inbufOffset += crlfcrlfPos+4-pbufSize;
  105. inbufOffset = inlen;
  106. buf_.clear();
  107. state_ = streamEndStateHandler_;
  108. return true;
  109. } else {
  110. std::string::size_type crlfPos = buf_.find(A2STR::CRLF);
  111. if(crlfPos == std::string::npos) {
  112. if(buf_.size() > MAX_BUF_SIZE) {
  113. throw DL_ABORT_EX
  114. ("Could not find end of stream before buffer got full.");
  115. }
  116. inbufOffset = inlen;
  117. return false;
  118. } else if(crlfPos == 0) {
  119. inbufOffset += crlfPos+2-pbufSize;
  120. buf_.clear();
  121. state_ = streamEndStateHandler_;
  122. return true;
  123. } else {
  124. if(buf_.size() > MAX_BUF_SIZE) {
  125. throw DL_ABORT_EX
  126. ("Could not find end of stream before buffer got full.");
  127. }
  128. inbufOffset = inlen;
  129. return false;
  130. }
  131. }
  132. }
  133. bool ChunkedDecodingStreamFilter::readData
  134. (ssize_t& outlen,
  135. size_t& inbufOffset,
  136. const unsigned char* inbuf,
  137. size_t inlen,
  138. const SharedHandle<BinaryStream>& out,
  139. const SharedHandle<Segment>& segment)
  140. {
  141. uint64_t readlen =
  142. std::min(chunkSize_, static_cast<uint64_t>(inlen-inbufOffset));
  143. outlen += getDelegate()->transform(out, segment, inbuf+inbufOffset, readlen);
  144. chunkSize_ -= readlen;
  145. inbufOffset += readlen;
  146. if(chunkSize_ == 0) {
  147. state_ = readDataEndStateHandler_;
  148. return true;
  149. } else {
  150. return false;
  151. }
  152. }
  153. bool ChunkedDecodingStreamFilter::readDataEnd
  154. (size_t& inbufOffset, const unsigned char* inbuf, size_t inlen)
  155. {
  156. size_t pbufSize = buf_.size();
  157. buf_.append(&inbuf[inbufOffset], &inbuf[inlen]);
  158. if(buf_.size() >= 2) {
  159. if(util::startsWith(buf_, A2STR::CRLF)) {
  160. inbufOffset += 2-pbufSize;
  161. buf_.clear();
  162. state_ = readChunkSizeStateHandler_;
  163. return true;
  164. } else {
  165. throw DL_ABORT_EX("No CRLF at the end of chunk.");
  166. }
  167. } else {
  168. inbufOffset = inlen;
  169. return false;
  170. }
  171. }
  172. ssize_t ChunkedDecodingStreamFilter::transform
  173. (const SharedHandle<BinaryStream>& out,
  174. const SharedHandle<Segment>& segment,
  175. const unsigned char* inbuf, size_t inlen)
  176. {
  177. size_t inbufOffset = 0;
  178. ssize_t outlen = 0;
  179. while(inbufOffset < inlen) {
  180. ssize_t olen = 0;
  181. bool r = state_->execute
  182. (this, olen, inbufOffset, inbuf, inlen, out, segment);
  183. outlen += olen;
  184. if(!r) {
  185. break;
  186. }
  187. }
  188. bytesProcessed_ = inbufOffset;
  189. return outlen;
  190. }
  191. bool ChunkedDecodingStreamFilter::finished()
  192. {
  193. return state_ == streamEndStateHandler_ && getDelegate()->finished();
  194. }
  195. void ChunkedDecodingStreamFilter::release() {}
  196. const std::string& ChunkedDecodingStreamFilter::getName() const
  197. {
  198. return NAME;
  199. }
  200. } // namespace aria2