[email protected] | d9f7662 | 2011-05-19 23:34:09 | [diff] [blame] | 1 | // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
[email protected] | b30a3f5 | 2010-10-16 01:05:46 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | // Derived from: |
| 6 | // mozilla/netwerk/protocol/http/src/nsHttpChunkedDecoder.h |
| 7 | // The license block is: |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 8 | /* ***** BEGIN LICENSE BLOCK ***** |
| 9 | * Version: MPL 1.1/GPL 2.0/LGPL 2.1 |
| 10 | * |
| 11 | * The contents of this file are subject to the Mozilla Public License Version |
| 12 | * 1.1 (the "License"); you may not use this file except in compliance with |
| 13 | * the License. You may obtain a copy of the License at |
| 14 | * http://www.mozilla.org/MPL/ |
| 15 | * |
| 16 | * Software distributed under the License is distributed on an "AS IS" basis, |
| 17 | * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License |
| 18 | * for the specific language governing rights and limitations under the |
| 19 | * License. |
| 20 | * |
| 21 | * The Original Code is Mozilla. |
| 22 | * |
| 23 | * The Initial Developer of the Original Code is |
| 24 | * Netscape Communications. |
| 25 | * Portions created by the Initial Developer are Copyright (C) 2001 |
| 26 | * the Initial Developer. All Rights Reserved. |
| 27 | * |
| 28 | * Contributor(s): |
| 29 | * Darin Fisher <[email protected]> (original author) |
| 30 | * |
| 31 | * Alternatively, the contents of this file may be used under the terms of |
| 32 | * either the GNU General Public License Version 2 or later (the "GPL"), or |
| 33 | * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"), |
| 34 | * in which case the provisions of the GPL or the LGPL are applicable instead |
| 35 | * of those above. If you wish to allow use of your version of this file only |
| 36 | * under the terms of either the GPL or the LGPL, and not to allow others to |
| 37 | * use your version of this file under the terms of the MPL, indicate your |
| 38 | * decision by deleting the provisions above and replace them with the notice |
| 39 | * and other provisions required by the GPL or the LGPL. If you do not delete |
| 40 | * the provisions above, a recipient may use your version of this file under |
| 41 | * the terms of any one of the MPL, the GPL or the LGPL. |
| 42 | * |
| 43 | * ***** END LICENSE BLOCK ***** */ |
| 44 | |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 45 | #ifndef NET_HTTP_HTTP_CHUNKED_DECODER_H_ |
| 46 | #define NET_HTTP_HTTP_CHUNKED_DECODER_H_ |
| 47 | |
Avi Drissman | 13fc893 | 2015-12-20 04:40:46 | [diff] [blame] | 48 | #include <stddef.h> |
mmenke | 3fca2b4 | 2016-07-26 04:01:41 | [diff] [blame] | 49 | #include <stdint.h> |
Avi Drissman | 13fc893 | 2015-12-20 04:40:46 | [diff] [blame] | 50 | |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 51 | #include <string> |
| 52 | |
[email protected] | 172da1b | 2011-08-12 15:52:26 | [diff] [blame] | 53 | #include "net/base/net_export.h" |
[email protected] | d9f7662 | 2011-05-19 23:34:09 | [diff] [blame] | 54 | |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 55 | namespace net { |
| 56 | |
| 57 | // From RFC2617 section 3.6.1, the chunked transfer coding is defined as: |
| 58 | // |
| 59 | // Chunked-Body = *chunk |
| 60 | // last-chunk |
| 61 | // trailer |
| 62 | // CRLF |
| 63 | // chunk = chunk-size [ chunk-extension ] CRLF |
| 64 | // chunk-data CRLF |
| 65 | // chunk-size = 1*HEX |
| 66 | // last-chunk = 1*("0") [ chunk-extension ] CRLF |
| 67 | // |
| 68 | // chunk-extension = *( ";" chunk-ext-name [ "=" chunk-ext-val ] ) |
| 69 | // chunk-ext-name = token |
| 70 | // chunk-ext-val = token | quoted-string |
| 71 | // chunk-data = chunk-size(OCTET) |
| 72 | // trailer = *(entity-header CRLF) |
| 73 | // |
| 74 | // The chunk-size field is a string of hex digits indicating the size of the |
| 75 | // chunk. The chunked encoding is ended by any chunk whose size is zero, |
| 76 | // followed by the trailer, which is terminated by an empty line. |
| 77 | // |
| 78 | // NOTE: This implementation does not bother to parse trailers since they are |
| 79 | // not used on the web. |
| 80 | // |
[email protected] | 172da1b | 2011-08-12 15:52:26 | [diff] [blame] | 81 | class NET_EXPORT_PRIVATE HttpChunkedDecoder { |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 82 | public: |
[email protected] | cbfbbc42 | 2012-10-18 19:18:00 | [diff] [blame] | 83 | // The maximum length of |line_buf_| between calls to FilterBuff(). |
| 84 | // Exposed for tests. |
| 85 | static const size_t kMaxLineBufLen; |
| 86 | |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 87 | HttpChunkedDecoder(); |
| 88 | |
| 89 | // Indicates that a previous call to FilterBuf encountered the final CRLF. |
| 90 | bool reached_eof() const { return reached_eof_; } |
| 91 | |
[email protected] | e1a9602 | 2009-10-13 17:33:09 | [diff] [blame] | 92 | // Returns the number of bytes after the final CRLF. |
| 93 | int bytes_after_eof() const { return bytes_after_eof_; } |
| 94 | |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 95 | // Called to filter out the chunk markers from buf and to check for end-of- |
| 96 | // file. This method modifies |buf| inline if necessary to remove chunk |
| 97 | // markers. The return value indicates the final size of decoded data stored |
| 98 | // in |buf|. Call reached_eof() after this method to check if end-of-file |
| 99 | // was encountered. |
| 100 | int FilterBuf(char* buf, int buf_len); |
| 101 | |
| 102 | private: |
[email protected] | 5368d6f | 2009-06-18 23:22:33 | [diff] [blame] | 103 | // Scans |buf| for the next chunk delimiter. This method returns the number |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 104 | // of bytes consumed from |buf|. If found, |chunk_remaining_| holds the |
| 105 | // value for the next chunk size. |
[email protected] | d89222a | 2008-08-14 02:44:45 | [diff] [blame] | 106 | int ScanForChunkRemaining(const char* buf, int buf_len); |
| 107 | |
[email protected] | 5368d6f | 2009-06-18 23:22:33 | [diff] [blame] | 108 | // Converts string |start| of length |len| to a numeric value. |
[email protected] | d89222a | 2008-08-14 02:44:45 | [diff] [blame] | 109 | // |start| is a string of type "chunk-size" (hex string). |
[email protected] | 5368d6f | 2009-06-18 23:22:33 | [diff] [blame] | 110 | // If the conversion succeeds, returns true and places the result in |out|. |
mmenke | 3fca2b4 | 2016-07-26 04:01:41 | [diff] [blame] | 111 | static bool ParseChunkSize(const char* start, int len, int64_t* out); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 112 | |
| 113 | // Indicates the number of bytes remaining for the current chunk. |
mmenke | 3fca2b4 | 2016-07-26 04:01:41 | [diff] [blame] | 114 | int64_t chunk_remaining_; |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 115 | |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 116 | // A small buffer used to store a partial chunk marker. |
| 117 | std::string line_buf_; |
| 118 | |
[email protected] | b75c5d1 | 2008-09-11 16:55:01 | [diff] [blame] | 119 | // True if waiting for the terminal CRLF of a chunk's data. |
| 120 | bool chunk_terminator_remaining_; |
| 121 | |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 122 | // Set to true when FilterBuf encounters the last-chunk. |
| 123 | bool reached_last_chunk_; |
| 124 | |
| 125 | // Set to true when FilterBuf encounters the final CRLF. |
| 126 | bool reached_eof_; |
[email protected] | e1a9602 | 2009-10-13 17:33:09 | [diff] [blame] | 127 | |
[email protected] | cf4cae3 | 2014-05-27 00:39:10 | [diff] [blame] | 128 | // The number of extraneous unfiltered bytes after the final CRLF. |
[email protected] | e1a9602 | 2009-10-13 17:33:09 | [diff] [blame] | 129 | int bytes_after_eof_; |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 130 | }; |
| 131 | |
| 132 | } // namespace net |
| 133 | |
| 134 | #endif // NET_HTTP_HTTP_CHUNKED_DECODER_H_ |