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