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