2001-05-11 21:04:09 +00:00
|
|
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
|
|
|
|
/*
|
|
|
|
* The contents of this file are subject to the Mozilla Public
|
|
|
|
* License Version 1.1 (the "License"); you may not use this file
|
|
|
|
* except in compliance with the License. You may obtain a copy of
|
|
|
|
* the License at http://www.mozilla.org/MPL/
|
|
|
|
*
|
|
|
|
* Software distributed under the License is distributed on an "AS
|
|
|
|
* IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
|
|
|
|
* implied. See the License for the specific language governing
|
|
|
|
* rights and limitations under the License.
|
|
|
|
*
|
|
|
|
* The Original Code is Mozilla.
|
|
|
|
*
|
|
|
|
* The Initial Developer of the Original Code is Netscape
|
|
|
|
* Communications. Portions created by Netscape Communications are
|
|
|
|
* Copyright (C) 2001 by Netscape Communications. All
|
|
|
|
* Rights Reserved.
|
|
|
|
*
|
|
|
|
* Contributor(s):
|
|
|
|
* Darin Fisher <darin@netscape.com> (original author)
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "nsHttpChunkedDecoder.h"
|
|
|
|
#include "nsHttp.h"
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
// nsHttpChunkedDecoder <public>
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsHttpChunkedDecoder::HandleChunkedContent(char *buf,
|
|
|
|
PRUint32 count,
|
|
|
|
PRUint32 *countRead)
|
|
|
|
{
|
|
|
|
LOG(("nsHttpChunkedDecoder::HandleChunkedContent [count=%u]\n", count));
|
|
|
|
|
|
|
|
*countRead = 0;
|
|
|
|
|
|
|
|
while (count) {
|
|
|
|
if (mChunkRemaining) {
|
|
|
|
PRUint32 amt = PR_MIN(mChunkRemaining, count);
|
|
|
|
|
|
|
|
count -= amt;
|
|
|
|
mChunkRemaining -= amt;
|
|
|
|
|
|
|
|
*countRead += amt;
|
|
|
|
buf += amt;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (count) {
|
|
|
|
PRUint32 bytesConsumed = 0;
|
|
|
|
|
|
|
|
nsresult rv = ParseChunkRemaining(buf, count, &bytesConsumed);
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
|
|
|
count -= bytesConsumed;
|
|
|
|
|
|
|
|
if (count) {
|
|
|
|
// shift buf by bytesConsumed
|
|
|
|
memmove(buf, buf + bytesConsumed, count);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
// nsHttpChunkedDecoder <private>
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsHttpChunkedDecoder::ParseChunkRemaining(char *buf,
|
|
|
|
PRUint32 count,
|
2001-05-14 10:15:06 +00:00
|
|
|
PRUint32 *bytesConsumed)
|
2001-05-11 21:04:09 +00:00
|
|
|
{
|
|
|
|
NS_PRECONDITION(mChunkRemaining == 0, "chunk remaining should be zero");
|
2001-05-14 10:15:06 +00:00
|
|
|
NS_PRECONDITION(count, "unexpected");
|
2001-05-11 21:04:09 +00:00
|
|
|
|
2001-05-14 10:15:06 +00:00
|
|
|
*bytesConsumed = 0;
|
|
|
|
|
|
|
|
char *p = PL_strnchr(buf, '\n', count);
|
|
|
|
if (p) {
|
|
|
|
*p = 0;
|
|
|
|
*bytesConsumed = p - buf + 1;
|
|
|
|
if (mLineBuf.IsEmpty())
|
2001-05-11 21:04:09 +00:00
|
|
|
p = buf;
|
2001-05-14 10:15:06 +00:00
|
|
|
else {
|
2001-05-11 21:04:09 +00:00
|
|
|
// append to the line buf and use that as the buf to parse.
|
|
|
|
mLineBuf.Append(buf);
|
|
|
|
p = (char *) mLineBuf.get();
|
|
|
|
}
|
|
|
|
}
|
2001-05-14 10:15:06 +00:00
|
|
|
else {
|
2001-05-11 21:04:09 +00:00
|
|
|
mLineBuf.Append(buf, count);
|
2001-05-14 10:15:06 +00:00
|
|
|
*bytesConsumed = count;
|
2001-05-11 21:04:09 +00:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2001-05-14 10:15:06 +00:00
|
|
|
if (*p && *p != '\r') {
|
|
|
|
buf = p;
|
2001-05-11 21:04:09 +00:00
|
|
|
|
|
|
|
// ignore any chunk-extensions
|
2001-05-14 10:15:06 +00:00
|
|
|
if ((p = PL_strchr(buf, ';')) != nsnull)
|
|
|
|
*p = 0;
|
2001-05-11 21:04:09 +00:00
|
|
|
|
2001-05-14 10:15:06 +00:00
|
|
|
if (!sscanf(buf, "%x", &mChunkRemaining)) {
|
|
|
|
LOG(("sscanf failed parsing hex on string [%s]\n", buf));
|
|
|
|
return NS_ERROR_UNEXPECTED;
|
|
|
|
}
|
2001-05-11 21:04:09 +00:00
|
|
|
|
|
|
|
// XXX need to add code to consume "trailer" headers
|
2001-05-14 10:15:06 +00:00
|
|
|
if ((mChunkRemaining == 0) && (*buf != 0))
|
|
|
|
mReachedEOF = PR_TRUE;
|
2001-05-11 21:04:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// clear the line buffer if it is not already empty
|
|
|
|
if (!mLineBuf.IsEmpty())
|
|
|
|
mLineBuf.SetLength(0);
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|