1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /* ***** BEGIN LICENSE BLOCK *****
3 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
5 * The contents of this file are subject to the Mozilla Public License Version
6 * 1.1 (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 * http://www.mozilla.org/MPL/
10 * Software distributed under the License is distributed on an "AS IS" basis,
11 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 * for the specific language governing rights and limitations under the
15 * The Original Code is Mozilla.
17 * The Initial Developer of the Original Code is
18 * Netscape Communications.
19 * Portions created by the Initial Developer are Copyright (C) 2001
20 * the Initial Developer. All Rights Reserved.
23 * Darin Fisher <darin@netscape.com> (original author)
25 * Alternatively, the contents of this file may be used under the terms of
26 * either the GNU General Public License Version 2 or later (the "GPL"), or
27 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
28 * in which case the provisions of the GPL or the LGPL are applicable instead
29 * of those above. If you wish to allow use of your version of this file only
30 * under the terms of either the GPL or the LGPL, and not to allow others to
31 * use your version of this file under the terms of the MPL, indicate your
32 * decision by deleting the provisions above and replace them with the notice
33 * and other provisions required by the GPL or the LGPL. If you do not delete
34 * the provisions above, a recipient may use your version of this file under
35 * the terms of any one of the MPL, the GPL or the LGPL.
37 * ***** END LICENSE BLOCK ***** */
39 #include "nsHttpChunkedDecoder.h"
42 //-----------------------------------------------------------------------------
43 // nsHttpChunkedDecoder <public>
44 //-----------------------------------------------------------------------------
47 nsHttpChunkedDecoder::HandleChunkedContent(char *buf
,
49 PRUint32
*contentRead
,
50 PRUint32
*contentRemaining
)
52 LOG(("nsHttpChunkedDecoder::HandleChunkedContent [count=%u]\n", count
));
56 // from RFC2617 section 3.6.1, the chunked transfer coding is defined as:
58 // Chunked-Body = *chunk
62 // chunk = chunk-size [ chunk-extension ] CRLF
65 // last-chunk = 1*("0") [ chunk-extension ] CRLF
67 // chunk-extension = *( ";" chunk-ext-name [ "=" chunk-ext-val ] )
68 // chunk-ext-name = token
69 // chunk-ext-val = token | quoted-string
70 // chunk-data = chunk-size(OCTET)
71 // trailer = *(entity-header CRLF)
73 // the chunk-size field is a string of hex digits indicating the size of the
74 // chunk. the chunked encoding is ended by any chunk whose size is zero,
75 // followed by the trailer, which is terminated by an empty line.
78 if (mChunkRemaining
) {
79 PRUint32 amt
= PR_MIN(mChunkRemaining
, count
);
82 mChunkRemaining
-= amt
;
90 PRUint32 bytesConsumed
= 0;
92 nsresult rv
= ParseChunkRemaining(buf
, count
, &bytesConsumed
);
93 if (NS_FAILED(rv
)) return rv
;
95 count
-= bytesConsumed
;
98 // shift buf by bytesConsumed
99 memmove(buf
, buf
+ bytesConsumed
, count
);
104 *contentRemaining
= count
;
108 //-----------------------------------------------------------------------------
109 // nsHttpChunkedDecoder <private>
110 //-----------------------------------------------------------------------------
113 nsHttpChunkedDecoder::ParseChunkRemaining(char *buf
,
115 PRUint32
*bytesConsumed
)
117 NS_PRECONDITION(mChunkRemaining
== 0, "chunk remaining should be zero");
118 NS_PRECONDITION(count
, "unexpected");
122 char *p
= static_cast<char *>(memchr(buf
, '\n', count
));
125 if ((p
> buf
) && (*(p
-1) == '\r')) // eliminate a preceding CR
127 *bytesConsumed
= p
- buf
+ 1;
129 // make buf point to the full line buffer to parse
130 if (!mLineBuf
.IsEmpty()) {
131 mLineBuf
.Append(buf
);
132 buf
= (char *) mLineBuf
.get();
137 LOG(("got trailer: %s\n", buf
));
138 // allocate a header array for the trailers on demand
140 mTrailers
= new nsHttpHeaderArray();
142 return NS_ERROR_OUT_OF_MEMORY
;
144 mTrailers
->ParseHeaderLine(buf
);
148 mReachedEOF
= PR_TRUE
;
149 LOG(("reached end of chunked-body\n"));
153 // ignore any chunk-extensions
154 if ((p
= PL_strchr(buf
, ';')) != nsnull
)
157 if (!sscanf(buf
, "%x", &mChunkRemaining
)) {
158 LOG(("sscanf failed parsing hex on string [%s]\n", buf
));
159 return NS_ERROR_UNEXPECTED
;
162 // we've discovered the last chunk
163 if (mChunkRemaining
== 0)
167 // ensure that the line buffer is clear
171 // save the partial line; wait for more data
172 *bytesConsumed
= count
;
173 // ignore a trailing CR
174 if (buf
[count
-1] == '\r')
176 mLineBuf
.Append(buf
, count
);