Bug 458256. Use LoadLibraryW instead of LoadLibrary (patch by DougT). r+sr=vlad
[wine-gecko.git] / netwerk / protocol / http / src / nsHttpChunkedDecoder.cpp
blobd8f6159fec90ff7bfd0bb85ccda8e14225094de9
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
13 * License.
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.
22 * Contributor(s):
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"
40 #include "nsHttp.h"
42 //-----------------------------------------------------------------------------
43 // nsHttpChunkedDecoder <public>
44 //-----------------------------------------------------------------------------
46 nsresult
47 nsHttpChunkedDecoder::HandleChunkedContent(char *buf,
48 PRUint32 count,
49 PRUint32 *contentRead,
50 PRUint32 *contentRemaining)
52 LOG(("nsHttpChunkedDecoder::HandleChunkedContent [count=%u]\n", count));
54 *contentRead = 0;
56 // from RFC2617 section 3.6.1, the chunked transfer coding is defined as:
58 // Chunked-Body = *chunk
59 // last-chunk
60 // trailer
61 // CRLF
62 // chunk = chunk-size [ chunk-extension ] CRLF
63 // chunk-data CRLF
64 // chunk-size = 1*HEX
65 // last-chunk = 1*("0") [ chunk-extension ] CRLF
66 //
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.
77 while (count) {
78 if (mChunkRemaining) {
79 PRUint32 amt = PR_MIN(mChunkRemaining, count);
81 count -= amt;
82 mChunkRemaining -= amt;
84 *contentRead += amt;
85 buf += amt;
87 else if (mReachedEOF)
88 break; // done
89 else {
90 PRUint32 bytesConsumed = 0;
92 nsresult rv = ParseChunkRemaining(buf, count, &bytesConsumed);
93 if (NS_FAILED(rv)) return rv;
95 count -= bytesConsumed;
97 if (count) {
98 // shift buf by bytesConsumed
99 memmove(buf, buf + bytesConsumed, count);
104 *contentRemaining = count;
105 return NS_OK;
108 //-----------------------------------------------------------------------------
109 // nsHttpChunkedDecoder <private>
110 //-----------------------------------------------------------------------------
112 nsresult
113 nsHttpChunkedDecoder::ParseChunkRemaining(char *buf,
114 PRUint32 count,
115 PRUint32 *bytesConsumed)
117 NS_PRECONDITION(mChunkRemaining == 0, "chunk remaining should be zero");
118 NS_PRECONDITION(count, "unexpected");
120 *bytesConsumed = 0;
122 char *p = static_cast<char *>(memchr(buf, '\n', count));
123 if (p) {
124 *p = 0;
125 if ((p > buf) && (*(p-1) == '\r')) // eliminate a preceding CR
126 *(p-1) = 0;
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();
135 if (mWaitEOF) {
136 if (*buf) {
137 LOG(("got trailer: %s\n", buf));
138 // allocate a header array for the trailers on demand
139 if (!mTrailers) {
140 mTrailers = new nsHttpHeaderArray();
141 if (!mTrailers)
142 return NS_ERROR_OUT_OF_MEMORY;
144 mTrailers->ParseHeaderLine(buf);
146 else {
147 mWaitEOF = PR_FALSE;
148 mReachedEOF = PR_TRUE;
149 LOG(("reached end of chunked-body\n"));
152 else if (*buf) {
153 // ignore any chunk-extensions
154 if ((p = PL_strchr(buf, ';')) != nsnull)
155 *p = 0;
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)
164 mWaitEOF = PR_TRUE;
167 // ensure that the line buffer is clear
168 mLineBuf.Truncate();
170 else {
171 // save the partial line; wait for more data
172 *bytesConsumed = count;
173 // ignore a trailing CR
174 if (buf[count-1] == '\r')
175 count--;
176 mLineBuf.Append(buf, count);
179 return NS_OK;