ChunkedDecodingStreamFilter.cc 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. /* <!-- copyright */
  2. /*
  3. * aria2 - The high speed download utility
  4. *
  5. * Copyright (C) 2012 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
  43. ChunkedDecodingStreamFilter::NAME("ChunkedDecodingStreamFilter");
  44. namespace {
  45. enum {
  46. PREV_CHUNK_SIZE,
  47. CHUNK_SIZE,
  48. CHUNK_EXTENSION,
  49. PREV_CHUNK_SIZE_LF,
  50. CHUNK,
  51. PREV_CHUNK_CR,
  52. PREV_CHUNK_LF,
  53. PREV_TRAILER,
  54. TRAILER,
  55. PREV_TRAILER_LF,
  56. PREV_END_CR,
  57. PREV_END_LF,
  58. CHUNKS_COMPLETE
  59. };
  60. } // namespace
  61. ChunkedDecodingStreamFilter::ChunkedDecodingStreamFilter(
  62. std::unique_ptr<StreamFilter> delegate)
  63. : StreamFilter{std::move(delegate)},
  64. state_{PREV_CHUNK_SIZE},
  65. chunkSize_{0},
  66. chunkRemaining_{0},
  67. bytesProcessed_{0}
  68. {
  69. }
  70. ChunkedDecodingStreamFilter::~ChunkedDecodingStreamFilter() {}
  71. void ChunkedDecodingStreamFilter::init() {}
  72. ssize_t
  73. ChunkedDecodingStreamFilter::transform(const std::shared_ptr<BinaryStream>& out,
  74. const std::shared_ptr<Segment>& segment,
  75. const unsigned char* inbuf, size_t inlen)
  76. {
  77. ssize_t outlen = 0;
  78. size_t i;
  79. bytesProcessed_ = 0;
  80. for (i = 0; i < inlen; ++i) {
  81. unsigned char c = inbuf[i];
  82. switch (state_) {
  83. case PREV_CHUNK_SIZE:
  84. if (util::isHexDigit(c)) {
  85. chunkSize_ = util::hexCharToUInt(c);
  86. state_ = CHUNK_SIZE;
  87. }
  88. else {
  89. throw DL_ABORT_EX("Bad chunk size: not hex string");
  90. }
  91. break;
  92. case CHUNK_SIZE:
  93. if (util::isHexDigit(c)) {
  94. if (chunkSize_ & 0x7800000000000000LL) {
  95. throw DL_ABORT_EX("Too big chunk size");
  96. }
  97. chunkSize_ <<= 4;
  98. chunkSize_ += util::hexCharToUInt(c);
  99. }
  100. else if (c == ';') {
  101. state_ = CHUNK_EXTENSION;
  102. }
  103. else if (c == '\r') {
  104. state_ = PREV_CHUNK_SIZE_LF;
  105. }
  106. else {
  107. throw DL_ABORT_EX("Bad chunk size: not hex string");
  108. }
  109. break;
  110. case CHUNK_EXTENSION:
  111. if (c == '\r') {
  112. state_ = PREV_CHUNK_SIZE_LF;
  113. }
  114. break;
  115. case PREV_CHUNK_SIZE_LF:
  116. if (c == '\n') {
  117. chunkRemaining_ = chunkSize_;
  118. if (chunkSize_ == 0) {
  119. state_ = PREV_TRAILER;
  120. }
  121. else {
  122. state_ = CHUNK;
  123. }
  124. }
  125. else {
  126. throw DL_ABORT_EX("Bad chunk encoding: "
  127. "missing LF at the end of chunk size");
  128. }
  129. break;
  130. case CHUNK: {
  131. int64_t readlen =
  132. std::min(chunkRemaining_, static_cast<int64_t>(inlen - i));
  133. outlen += getDelegate()->transform(out, segment, inbuf + i, readlen);
  134. chunkRemaining_ -= readlen;
  135. i += readlen - 1;
  136. if (chunkRemaining_ == 0) {
  137. state_ = PREV_CHUNK_CR;
  138. }
  139. break;
  140. }
  141. case PREV_CHUNK_CR:
  142. if (c == '\r') {
  143. state_ = PREV_CHUNK_LF;
  144. }
  145. else {
  146. throw DL_ABORT_EX("Bad chunk encoding: "
  147. "missing CR at the end of chunk");
  148. }
  149. break;
  150. case PREV_CHUNK_LF:
  151. if (c == '\n') {
  152. if (chunkSize_ == 0) {
  153. state_ = PREV_TRAILER;
  154. }
  155. else {
  156. chunkSize_ = chunkRemaining_ = 0;
  157. state_ = PREV_CHUNK_SIZE;
  158. }
  159. }
  160. else {
  161. throw DL_ABORT_EX("Bad chunk encoding: "
  162. "missing LF at the end of chunk");
  163. }
  164. break;
  165. case PREV_TRAILER:
  166. if (c == '\r') {
  167. // No trailer
  168. state_ = PREV_END_LF;
  169. }
  170. else {
  171. state_ = TRAILER;
  172. }
  173. break;
  174. case TRAILER:
  175. if (c == '\r') {
  176. state_ = PREV_TRAILER_LF;
  177. }
  178. break;
  179. case PREV_TRAILER_LF:
  180. if (c == '\n') {
  181. state_ = PREV_TRAILER;
  182. }
  183. else {
  184. throw DL_ABORT_EX("Bad chunk encoding: "
  185. "missing LF at the end of trailer");
  186. }
  187. break;
  188. case PREV_END_LF:
  189. if (c == '\n') {
  190. state_ = CHUNKS_COMPLETE;
  191. }
  192. else {
  193. throw DL_ABORT_EX("Bad chunk encoding: "
  194. "missing LF at the end of chunks");
  195. }
  196. break;
  197. case CHUNKS_COMPLETE:
  198. goto fin;
  199. default:
  200. // unreachable
  201. assert(0);
  202. }
  203. }
  204. fin:
  205. bytesProcessed_ += i;
  206. return outlen;
  207. }
  208. bool ChunkedDecodingStreamFilter::finished()
  209. {
  210. return state_ == CHUNKS_COMPLETE && getDelegate()->finished();
  211. }
  212. void ChunkedDecodingStreamFilter::release() {}
  213. const std::string& ChunkedDecodingStreamFilter::getName() const { return NAME; }
  214. } // namespace aria2