http_chunked_decoder.cc 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. // Copyright (c) 2011 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. // Derived from:
  5. // mozilla/netwerk/protocol/http/src/nsHttpChunkedDecoder.cpp
  6. // The license block is:
  7. /* ***** BEGIN LICENSE BLOCK *****
  8. * Version: MPL 1.1/GPL 2.0/LGPL 2.1
  9. *
  10. * The contents of this file are subject to the Mozilla Public License Version
  11. * 1.1 (the "License"); you may not use this file except in compliance with
  12. * the License. You may obtain a copy of the License at
  13. * http://www.mozilla.org/MPL/
  14. *
  15. * Software distributed under the License is distributed on an "AS IS" basis,
  16. * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  17. * for the specific language governing rights and limitations under the
  18. * License.
  19. *
  20. * The Original Code is Mozilla.
  21. *
  22. * The Initial Developer of the Original Code is
  23. * Netscape Communications.
  24. * Portions created by the Initial Developer are Copyright (C) 2001
  25. * the Initial Developer. All Rights Reserved.
  26. *
  27. * Contributor(s):
  28. * Darin Fisher <darin@netscape.com> (original author)
  29. *
  30. * Alternatively, the contents of this file may be used under the terms of
  31. * either the GNU General Public License Version 2 or later (the "GPL"), or
  32. * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
  33. * in which case the provisions of the GPL or the LGPL are applicable instead
  34. * of those above. If you wish to allow use of your version of this file only
  35. * under the terms of either the GPL or the LGPL, and not to allow others to
  36. * use your version of this file under the terms of the MPL, indicate your
  37. * decision by deleting the provisions above and replace them with the notice
  38. * and other provisions required by the GPL or the LGPL. If you do not delete
  39. * the provisions above, a recipient may use your version of this file under
  40. * the terms of any one of the MPL, the GPL or the LGPL.
  41. *
  42. * ***** END LICENSE BLOCK ***** */
  43. #include "net/http/http_chunked_decoder.h"
  44. #include <algorithm>
  45. #include "base/logging.h"
  46. #include "base/strings/string_number_conversions.h"
  47. #include "base/strings/string_piece.h"
  48. #include "base/strings/string_util.h"
  49. #include "net/base/net_errors.h"
  50. namespace net {
  51. // Absurdly long size to avoid imposing a constraint on chunked encoding
  52. // extensions.
  53. const size_t HttpChunkedDecoder::kMaxLineBufLen = 16384;
  54. HttpChunkedDecoder::HttpChunkedDecoder() = default;
  55. int HttpChunkedDecoder::FilterBuf(char* buf, int buf_len) {
  56. int result = 0;
  57. while (buf_len > 0) {
  58. if (chunk_remaining_ > 0) {
  59. // Since |chunk_remaining_| is positive and |buf_len| an int, the minimum
  60. // of the two must be an int.
  61. int num = static_cast<int>(
  62. std::min(chunk_remaining_, static_cast<int64_t>(buf_len)));
  63. buf_len -= num;
  64. chunk_remaining_ -= num;
  65. result += num;
  66. buf += num;
  67. // After each chunk's data there should be a CRLF.
  68. if (chunk_remaining_ == 0)
  69. chunk_terminator_remaining_ = true;
  70. continue;
  71. } else if (reached_eof_) {
  72. bytes_after_eof_ += buf_len;
  73. break; // Done!
  74. }
  75. int bytes_consumed = ScanForChunkRemaining(buf, buf_len);
  76. if (bytes_consumed < 0)
  77. return bytes_consumed; // Error
  78. buf_len -= bytes_consumed;
  79. if (buf_len > 0)
  80. memmove(buf, buf + bytes_consumed, buf_len);
  81. }
  82. return result;
  83. }
  84. int HttpChunkedDecoder::ScanForChunkRemaining(const char* buf, int buf_len) {
  85. DCHECK_EQ(0, chunk_remaining_);
  86. DCHECK_GT(buf_len, 0);
  87. int bytes_consumed = 0;
  88. size_t index_of_lf = base::StringPiece(buf, buf_len).find('\n');
  89. if (index_of_lf != base::StringPiece::npos) {
  90. buf_len = static_cast<int>(index_of_lf);
  91. if (buf_len && buf[buf_len - 1] == '\r') // Eliminate a preceding CR.
  92. buf_len--;
  93. bytes_consumed = static_cast<int>(index_of_lf) + 1;
  94. // Make buf point to the full line buffer to parse.
  95. if (!line_buf_.empty()) {
  96. line_buf_.append(buf, buf_len);
  97. buf = line_buf_.data();
  98. buf_len = static_cast<int>(line_buf_.size());
  99. }
  100. if (reached_last_chunk_) {
  101. if (buf_len > 0)
  102. DVLOG(1) << "ignoring http trailer";
  103. else
  104. reached_eof_ = true;
  105. } else if (chunk_terminator_remaining_) {
  106. if (buf_len > 0) {
  107. DLOG(ERROR) << "chunk data not terminated properly";
  108. return ERR_INVALID_CHUNKED_ENCODING;
  109. }
  110. chunk_terminator_remaining_ = false;
  111. } else if (buf_len > 0) {
  112. // Ignore any chunk-extensions.
  113. size_t index_of_semicolon = base::StringPiece(buf, buf_len).find(';');
  114. if (index_of_semicolon != base::StringPiece::npos)
  115. buf_len = static_cast<int>(index_of_semicolon);
  116. if (!ParseChunkSize(buf, buf_len, &chunk_remaining_)) {
  117. DLOG(ERROR) << "Failed parsing HEX from: " <<
  118. std::string(buf, buf_len);
  119. return ERR_INVALID_CHUNKED_ENCODING;
  120. }
  121. if (chunk_remaining_ == 0)
  122. reached_last_chunk_ = true;
  123. } else {
  124. DLOG(ERROR) << "missing chunk-size";
  125. return ERR_INVALID_CHUNKED_ENCODING;
  126. }
  127. line_buf_.clear();
  128. } else {
  129. // Save the partial line; wait for more data.
  130. bytes_consumed = buf_len;
  131. // Ignore a trailing CR
  132. if (buf[buf_len - 1] == '\r')
  133. buf_len--;
  134. if (line_buf_.length() + buf_len > kMaxLineBufLen) {
  135. DLOG(ERROR) << "Chunked line length too long";
  136. return ERR_INVALID_CHUNKED_ENCODING;
  137. }
  138. line_buf_.append(buf, buf_len);
  139. }
  140. return bytes_consumed;
  141. }
  142. // While the HTTP 1.1 specification defines chunk-size as 1*HEX
  143. // some sites rely on more lenient parsing.
  144. // http://www.yahoo.com/, for example, pads chunk-size with trailing spaces
  145. // (0x20) to be 7 characters long, such as "819b ".
  146. //
  147. // A comparison of browsers running on WindowsXP shows that
  148. // they will parse the following inputs (egrep syntax):
  149. //
  150. // Let \X be the character class for a hex digit: [0-9a-fA-F]
  151. //
  152. // RFC 7230: ^\X+$
  153. // IE7: ^\X+[^\X]*$
  154. // Safari 3.1: ^[\t\r ]*\X+[\t ]*$
  155. // Firefox 3: ^[\t\f\v\r ]*[+]?(0x)?\X+[^\X]*$
  156. // Opera 9.51: ^[\t\f\v ]*[+]?(0x)?\X+[^\X]*$
  157. //
  158. // Our strategy is to be as strict as possible, while not breaking
  159. // known sites.
  160. //
  161. // Us: ^\X+[ ]*$
  162. bool HttpChunkedDecoder::ParseChunkSize(const char* start,
  163. int len,
  164. int64_t* out) {
  165. DCHECK_GE(len, 0);
  166. // Strip trailing spaces
  167. while (len > 0 && start[len - 1] == ' ')
  168. len--;
  169. // Be more restrictive than HexStringToInt64;
  170. // don't allow inputs with leading "-", "+", "0x", "0X"
  171. base::StringPiece chunk_size(start, len);
  172. if (chunk_size.find_first_not_of("0123456789abcdefABCDEF")
  173. != base::StringPiece::npos) {
  174. return false;
  175. }
  176. int64_t parsed_number;
  177. bool ok = base::HexStringToInt64(chunk_size, &parsed_number);
  178. if (ok && parsed_number >= 0) {
  179. *out = parsed_number;
  180. return true;
  181. }
  182. return false;
  183. }
  184. } // namespace net