2015-09-18 20:48:50 +03:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim: set sw=2 ts=8 et tw=80 : */
|
2012-05-21 15:12:37 +04:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
2000-03-23 04:09:39 +03:00
|
|
|
|
|
|
|
#include "nsHTTPCompressConv.h"
|
2000-06-03 13:46:12 +04:00
|
|
|
#include "nsMemory.h"
|
2000-03-23 04:09:39 +03:00
|
|
|
#include "plstr.h"
|
|
|
|
#include "nsCOMPtr.h"
|
2012-07-27 18:03:27 +04:00
|
|
|
#include "nsError.h"
|
2006-01-11 04:50:15 +03:00
|
|
|
#include "nsStreamUtils.h"
|
2006-01-02 05:30:32 +03:00
|
|
|
#include "nsStringStream.h"
|
|
|
|
#include "nsComponentManagerUtils.h"
|
2015-02-25 03:16:00 +03:00
|
|
|
#include "nsThreadUtils.h"
|
|
|
|
#include "mozilla/Preferences.h"
|
2016-01-09 04:20:50 +03:00
|
|
|
#include "mozilla/Logging.h"
|
2016-12-16 06:16:31 +03:00
|
|
|
#include "mozilla/SizePrintfMacros.h"
|
2015-09-22 19:55:23 +03:00
|
|
|
#include "nsIForcePendingChannel.h"
|
2016-01-09 04:20:50 +03:00
|
|
|
#include "nsIRequest.h"
|
2015-09-22 19:55:23 +03:00
|
|
|
|
|
|
|
// brotli headers
|
|
|
|
#include "state.h"
|
|
|
|
#include "decode.h"
|
|
|
|
|
2015-09-19 04:24:28 +03:00
|
|
|
namespace mozilla {
|
|
|
|
namespace net {
|
|
|
|
|
2015-11-03 07:35:29 +03:00
|
|
|
extern LazyLogModule gHttpLog;
|
|
|
|
#define LOG(args) MOZ_LOG(mozilla::net::gHttpLog, mozilla::LogLevel::Debug, args)
|
|
|
|
|
2000-03-23 04:09:39 +03:00
|
|
|
// nsISupports implementation
|
2014-04-27 11:06:00 +04:00
|
|
|
NS_IMPL_ISUPPORTS(nsHTTPCompressConv,
|
|
|
|
nsIStreamConverter,
|
|
|
|
nsIStreamListener,
|
2015-07-24 18:49:25 +03:00
|
|
|
nsIRequestObserver,
|
|
|
|
nsICompressConvStats)
|
2000-03-23 04:09:39 +03:00
|
|
|
|
|
|
|
// nsFTPDirListingConv methods
|
2003-04-15 03:51:50 +04:00
|
|
|
nsHTTPCompressConv::nsHTTPCompressConv()
|
2015-09-18 20:48:50 +03:00
|
|
|
: mMode(HTTP_COMPRESS_IDENTITY)
|
|
|
|
, mOutBuffer(nullptr)
|
|
|
|
, mInpBuffer(nullptr)
|
|
|
|
, mOutBufferLen(0)
|
|
|
|
, mInpBufferLen(0)
|
|
|
|
, mCheckHeaderDone(false)
|
|
|
|
, mStreamEnded(false)
|
|
|
|
, mStreamInitialized(false)
|
|
|
|
, mLen(0)
|
|
|
|
, hMode(0)
|
|
|
|
, mSkipCount(0)
|
|
|
|
, mFlags(0)
|
2015-07-24 18:49:25 +03:00
|
|
|
, mDecodedDataLength(0)
|
2000-03-23 04:09:39 +03:00
|
|
|
{
|
2015-09-22 19:55:23 +03:00
|
|
|
LOG(("nsHttpCompresssConv %p ctor\n", this));
|
2015-09-18 20:48:50 +03:00
|
|
|
if (NS_IsMainThread()) {
|
|
|
|
mFailUncleanStops =
|
|
|
|
Preferences::GetBool("network.http.enforce-framing.http", false);
|
|
|
|
} else {
|
|
|
|
mFailUncleanStops = false;
|
|
|
|
}
|
2000-03-23 04:09:39 +03:00
|
|
|
}
|
|
|
|
|
2003-04-15 03:51:50 +04:00
|
|
|
nsHTTPCompressConv::~nsHTTPCompressConv()
|
2000-03-23 04:09:39 +03:00
|
|
|
{
|
2015-09-22 19:55:23 +03:00
|
|
|
LOG(("nsHttpCompresssConv %p dtor\n", this));
|
2015-09-18 20:54:01 +03:00
|
|
|
if (mInpBuffer) {
|
2015-09-18 20:48:50 +03:00
|
|
|
free(mInpBuffer);
|
2015-09-18 20:54:01 +03:00
|
|
|
}
|
2000-03-23 04:09:39 +03:00
|
|
|
|
2015-09-18 20:54:01 +03:00
|
|
|
if (mOutBuffer) {
|
2015-09-18 20:48:50 +03:00
|
|
|
free(mOutBuffer);
|
2015-09-18 20:54:01 +03:00
|
|
|
}
|
2003-05-29 03:45:06 +04:00
|
|
|
|
2015-09-18 20:48:50 +03:00
|
|
|
// For some reason we are not getting Z_STREAM_END. But this was also seen
|
|
|
|
// for mozilla bug 198133. Need to handle this case.
|
2015-09-18 20:54:01 +03:00
|
|
|
if (mStreamInitialized && !mStreamEnded) {
|
2015-09-18 20:48:50 +03:00
|
|
|
inflateEnd (&d_stream);
|
2015-09-18 20:54:01 +03:00
|
|
|
}
|
2000-03-23 04:09:39 +03:00
|
|
|
}
|
|
|
|
|
2015-07-24 18:49:25 +03:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsHTTPCompressConv::GetDecodedDataLength(uint64_t *aDecodedDataLength)
|
|
|
|
{
|
|
|
|
*aDecodedDataLength = mDecodedDataLength;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2000-03-23 04:09:39 +03:00
|
|
|
NS_IMETHODIMP
|
2015-07-01 00:17:10 +03:00
|
|
|
nsHTTPCompressConv::AsyncConvertData(const char *aFromType,
|
|
|
|
const char *aToType,
|
|
|
|
nsIStreamListener *aListener,
|
2003-04-15 03:51:50 +04:00
|
|
|
nsISupports *aCtxt)
|
2000-03-23 04:09:39 +03:00
|
|
|
{
|
2015-09-18 20:48:50 +03:00
|
|
|
if (!PL_strncasecmp(aFromType, HTTP_COMPRESS_TYPE, sizeof(HTTP_COMPRESS_TYPE)-1) ||
|
|
|
|
!PL_strncasecmp(aFromType, HTTP_X_COMPRESS_TYPE, sizeof(HTTP_X_COMPRESS_TYPE)-1)) {
|
|
|
|
mMode = HTTP_COMPRESS_COMPRESS;
|
|
|
|
} else if (!PL_strncasecmp(aFromType, HTTP_GZIP_TYPE, sizeof(HTTP_GZIP_TYPE)-1) ||
|
|
|
|
!PL_strncasecmp(aFromType, HTTP_X_GZIP_TYPE, sizeof(HTTP_X_GZIP_TYPE)-1)) {
|
|
|
|
mMode = HTTP_COMPRESS_GZIP;
|
|
|
|
} else if (!PL_strncasecmp(aFromType, HTTP_DEFLATE_TYPE, sizeof(HTTP_DEFLATE_TYPE)-1)) {
|
|
|
|
mMode = HTTP_COMPRESS_DEFLATE;
|
2015-09-22 19:55:23 +03:00
|
|
|
} else if (!PL_strncasecmp(aFromType, HTTP_BROTLI_TYPE, sizeof(HTTP_BROTLI_TYPE)-1)) {
|
|
|
|
mMode = HTTP_COMPRESS_BROTLI;
|
2015-09-18 20:48:50 +03:00
|
|
|
}
|
2015-09-22 19:55:23 +03:00
|
|
|
LOG(("nsHttpCompresssConv %p AsyncConvertData %s %s mode %d\n",
|
|
|
|
this, aFromType, aToType, mMode));
|
2015-09-18 20:48:50 +03:00
|
|
|
|
|
|
|
// hook ourself up with the receiving listener.
|
|
|
|
mListener = aListener;
|
|
|
|
|
|
|
|
mAsyncConvContext = aCtxt;
|
|
|
|
return NS_OK;
|
2015-07-01 00:17:10 +03:00
|
|
|
}
|
2000-03-23 04:09:39 +03:00
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2003-04-15 03:51:50 +04:00
|
|
|
nsHTTPCompressConv::OnStartRequest(nsIRequest* request, nsISupports *aContext)
|
2000-03-23 04:09:39 +03:00
|
|
|
{
|
2015-09-22 19:55:23 +03:00
|
|
|
LOG(("nsHttpCompresssConv %p onstart\n", this));
|
2015-09-18 20:48:50 +03:00
|
|
|
return mListener->OnStartRequest(request, aContext);
|
2015-07-01 00:17:10 +03:00
|
|
|
}
|
2000-03-23 04:09:39 +03:00
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2015-07-01 00:17:10 +03:00
|
|
|
nsHTTPCompressConv::OnStopRequest(nsIRequest* request, nsISupports *aContext,
|
2001-04-10 10:01:08 +04:00
|
|
|
nsresult aStatus)
|
2000-03-23 04:09:39 +03:00
|
|
|
{
|
2015-09-22 19:55:23 +03:00
|
|
|
nsresult status = aStatus;
|
2016-12-16 06:16:31 +03:00
|
|
|
LOG(("nsHttpCompresssConv %p onstop %" PRIx32 "\n", this, static_cast<uint32_t>(aStatus)));
|
2015-09-22 19:55:23 +03:00
|
|
|
|
2015-09-18 20:48:50 +03:00
|
|
|
// Framing integrity is enforced for content-encoding: gzip, but not for
|
|
|
|
// content-encoding: deflate. Note that gzip vs deflate is NOT determined
|
|
|
|
// by content sniffing but only via header.
|
2015-09-22 19:55:23 +03:00
|
|
|
if (!mStreamEnded && NS_SUCCEEDED(status) &&
|
2015-09-18 20:48:50 +03:00
|
|
|
(mFailUncleanStops && (mMode == HTTP_COMPRESS_GZIP)) ) {
|
|
|
|
// This is not a clean end of gzip stream: the transfer is incomplete.
|
2015-09-22 19:55:23 +03:00
|
|
|
status = NS_ERROR_NET_PARTIAL_TRANSFER;
|
|
|
|
LOG(("nsHttpCompresssConv %p onstop partial gzip\n", this));
|
|
|
|
}
|
|
|
|
if (NS_SUCCEEDED(status) && mMode == HTTP_COMPRESS_BROTLI) {
|
|
|
|
nsCOMPtr<nsIForcePendingChannel> fpChannel = do_QueryInterface(request);
|
|
|
|
bool isPending = false;
|
|
|
|
if (request) {
|
|
|
|
request->IsPending(&isPending);
|
|
|
|
}
|
|
|
|
if (fpChannel && !isPending) {
|
|
|
|
fpChannel->ForcePending(true);
|
|
|
|
}
|
2016-04-01 22:14:22 +03:00
|
|
|
if (mBrotli && (mBrotli->mTotalOut == 0) && !BrotliStateIsStreamEnd(&mBrotli->mState)) {
|
2016-02-08 14:01:21 +03:00
|
|
|
status = NS_ERROR_INVALID_CONTENT_ENCODING;
|
|
|
|
}
|
2016-12-16 06:16:31 +03:00
|
|
|
LOG(("nsHttpCompresssConv %p onstop brotlihandler rv %" PRIx32 "\n",
|
|
|
|
this, static_cast<uint32_t>(status)));
|
2015-09-22 19:55:23 +03:00
|
|
|
if (fpChannel && !isPending) {
|
|
|
|
fpChannel->ForcePending(false);
|
|
|
|
}
|
2015-09-18 20:48:50 +03:00
|
|
|
}
|
2015-09-22 19:55:23 +03:00
|
|
|
return mListener->OnStopRequest(request, aContext, status);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-08-12 10:36:22 +03:00
|
|
|
/* static */ nsresult
|
2015-09-22 19:55:23 +03:00
|
|
|
nsHTTPCompressConv::BrotliHandler(nsIInputStream *stream, void *closure, const char *dataIn,
|
|
|
|
uint32_t, uint32_t aAvail, uint32_t *countRead)
|
|
|
|
{
|
2016-02-08 14:01:21 +03:00
|
|
|
MOZ_ASSERT(stream);
|
2015-09-22 19:55:23 +03:00
|
|
|
nsHTTPCompressConv *self = static_cast<nsHTTPCompressConv *>(closure);
|
|
|
|
*countRead = 0;
|
|
|
|
|
2016-05-29 16:59:24 +03:00
|
|
|
const size_t kOutSize = 128 * 1024; // just a chunk size, we call in a loop
|
|
|
|
uint8_t *outPtr;
|
2015-09-22 19:55:23 +03:00
|
|
|
size_t outSize;
|
|
|
|
size_t avail = aAvail;
|
|
|
|
BrotliResult res;
|
|
|
|
|
2015-10-05 21:11:26 +03:00
|
|
|
if (!self->mBrotli) {
|
|
|
|
*countRead = aAvail;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2016-05-29 16:59:24 +03:00
|
|
|
auto outBuffer = MakeUniqueFallible<uint8_t[]>(kOutSize);
|
|
|
|
if (outBuffer == nullptr) {
|
|
|
|
self->mBrotli->mStatus = NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
return self->mBrotli->mStatus;
|
|
|
|
}
|
|
|
|
|
2015-09-22 19:55:23 +03:00
|
|
|
do {
|
|
|
|
outSize = kOutSize;
|
2016-05-29 16:59:24 +03:00
|
|
|
outPtr = outBuffer.get();
|
2015-09-22 19:55:23 +03:00
|
|
|
|
2016-02-08 14:01:21 +03:00
|
|
|
// brotli api is documented in brotli/dec/decode.h and brotli/dec/decode.c
|
2016-12-16 06:16:31 +03:00
|
|
|
LOG(("nsHttpCompresssConv %p brotlihandler decompress %" PRIuSIZE "\n", self, avail));
|
2016-02-08 14:01:21 +03:00
|
|
|
res = ::BrotliDecompressStream(
|
|
|
|
&avail, reinterpret_cast<const unsigned char **>(&dataIn),
|
2015-09-22 19:55:23 +03:00
|
|
|
&outSize, &outPtr, &self->mBrotli->mTotalOut, &self->mBrotli->mState);
|
|
|
|
outSize = kOutSize - outSize;
|
2016-12-16 06:16:31 +03:00
|
|
|
LOG(("nsHttpCompresssConv %p brotlihandler decompress rv=%" PRIx32 " out=%" PRIuSIZE "\n",
|
|
|
|
self, static_cast<uint32_t>(res), outSize));
|
2015-09-22 19:55:23 +03:00
|
|
|
|
|
|
|
if (res == BROTLI_RESULT_ERROR) {
|
|
|
|
LOG(("nsHttpCompressConv %p marking invalid encoding", self));
|
|
|
|
self->mBrotli->mStatus = NS_ERROR_INVALID_CONTENT_ENCODING;
|
|
|
|
return self->mBrotli->mStatus;
|
|
|
|
}
|
|
|
|
|
2016-02-08 14:01:21 +03:00
|
|
|
// in 'the current implementation' brotli must consume everything before
|
|
|
|
// asking for more input
|
|
|
|
if (res == BROTLI_RESULT_NEEDS_MORE_INPUT) {
|
|
|
|
MOZ_ASSERT(!avail);
|
|
|
|
if (avail) {
|
|
|
|
LOG(("nsHttpCompressConv %p did not consume all input", self));
|
|
|
|
self->mBrotli->mStatus = NS_ERROR_UNEXPECTED;
|
|
|
|
return self->mBrotli->mStatus;
|
|
|
|
}
|
2015-09-22 19:55:23 +03:00
|
|
|
}
|
|
|
|
if (outSize > 0) {
|
|
|
|
nsresult rv = self->do_OnDataAvailable(self->mBrotli->mRequest,
|
|
|
|
self->mBrotli->mContext,
|
|
|
|
self->mBrotli->mSourceOffset,
|
2016-05-29 16:59:24 +03:00
|
|
|
reinterpret_cast<const char *>(outBuffer.get()),
|
2015-09-22 19:55:23 +03:00
|
|
|
outSize);
|
2016-12-16 06:16:31 +03:00
|
|
|
LOG(("nsHttpCompressConv %p BrotliHandler ODA rv=%" PRIx32, self, static_cast<uint32_t>(rv)));
|
2015-09-22 19:55:23 +03:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
self->mBrotli->mStatus = rv;
|
|
|
|
return self->mBrotli->mStatus;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (res == BROTLI_RESULT_SUCCESS ||
|
|
|
|
res == BROTLI_RESULT_NEEDS_MORE_INPUT) {
|
|
|
|
*countRead = aAvail;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
MOZ_ASSERT (res == BROTLI_RESULT_NEEDS_MORE_OUTPUT);
|
|
|
|
} while (res == BROTLI_RESULT_NEEDS_MORE_OUTPUT);
|
|
|
|
|
|
|
|
self->mBrotli->mStatus = NS_ERROR_UNEXPECTED;
|
|
|
|
return self->mBrotli->mStatus;
|
2015-07-01 00:17:10 +03:00
|
|
|
}
|
2000-03-23 04:09:39 +03:00
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2015-07-01 00:17:10 +03:00
|
|
|
nsHTTPCompressConv::OnDataAvailable(nsIRequest* request,
|
|
|
|
nsISupports *aContext,
|
|
|
|
nsIInputStream *iStr,
|
|
|
|
uint64_t aSourceOffset,
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t aCount)
|
2000-03-23 04:09:39 +03:00
|
|
|
{
|
2015-09-18 20:48:50 +03:00
|
|
|
nsresult rv = NS_ERROR_INVALID_CONTENT_ENCODING;
|
|
|
|
uint32_t streamLen = aCount;
|
2015-09-22 19:55:23 +03:00
|
|
|
LOG(("nsHttpCompressConv %p OnDataAvailable %d", this, aCount));
|
2015-09-18 20:48:50 +03:00
|
|
|
|
2015-09-18 20:54:01 +03:00
|
|
|
if (streamLen == 0) {
|
2015-09-18 20:48:50 +03:00
|
|
|
NS_ERROR("count of zero passed to OnDataAvailable");
|
|
|
|
return NS_ERROR_UNEXPECTED;
|
|
|
|
}
|
|
|
|
|
2015-09-18 20:54:01 +03:00
|
|
|
if (mStreamEnded) {
|
2015-09-18 20:48:50 +03:00
|
|
|
// Hmm... this may just indicate that the data stream is done and that
|
|
|
|
// what's left is either metadata or padding of some sort.... throwing
|
|
|
|
// it out is probably the safe thing to do.
|
|
|
|
uint32_t n;
|
|
|
|
return iStr->ReadSegments(NS_DiscardSegment, nullptr, streamLen, &n);
|
|
|
|
}
|
|
|
|
|
2015-09-18 20:54:01 +03:00
|
|
|
switch (mMode) {
|
2015-09-18 20:48:50 +03:00
|
|
|
case HTTP_COMPRESS_GZIP:
|
|
|
|
streamLen = check_header(iStr, streamLen, &rv);
|
|
|
|
|
2015-09-18 20:54:01 +03:00
|
|
|
if (rv != NS_OK) {
|
2015-09-18 20:48:50 +03:00
|
|
|
return rv;
|
2015-09-18 20:54:01 +03:00
|
|
|
}
|
2000-03-23 04:09:39 +03:00
|
|
|
|
2015-09-18 20:54:01 +03:00
|
|
|
if (streamLen == 0) {
|
2015-09-18 20:48:50 +03:00
|
|
|
return NS_OK;
|
2015-09-18 20:54:01 +03:00
|
|
|
}
|
2004-06-16 20:28:30 +04:00
|
|
|
|
2015-11-23 08:58:33 +03:00
|
|
|
MOZ_FALLTHROUGH;
|
2015-09-18 20:48:50 +03:00
|
|
|
|
|
|
|
case HTTP_COMPRESS_DEFLATE:
|
|
|
|
|
2015-09-18 20:54:01 +03:00
|
|
|
if (mInpBuffer != nullptr && streamLen > mInpBufferLen) {
|
2015-09-18 20:48:50 +03:00
|
|
|
mInpBuffer = (unsigned char *) realloc(mInpBuffer, mInpBufferLen = streamLen);
|
|
|
|
|
2015-09-18 20:54:01 +03:00
|
|
|
if (mOutBufferLen < streamLen * 2) {
|
2015-09-18 20:48:50 +03:00
|
|
|
mOutBuffer = (unsigned char *) realloc(mOutBuffer, mOutBufferLen = streamLen * 3);
|
2015-09-18 20:54:01 +03:00
|
|
|
}
|
2015-09-18 20:48:50 +03:00
|
|
|
|
2015-09-18 20:54:01 +03:00
|
|
|
if (mInpBuffer == nullptr || mOutBuffer == nullptr) {
|
2015-09-18 20:48:50 +03:00
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
2015-09-18 20:54:01 +03:00
|
|
|
}
|
2004-06-16 20:28:30 +04:00
|
|
|
}
|
2000-03-23 04:09:39 +03:00
|
|
|
|
2015-09-18 20:54:01 +03:00
|
|
|
if (mInpBuffer == nullptr) {
|
2015-09-18 20:48:50 +03:00
|
|
|
mInpBuffer = (unsigned char *) malloc(mInpBufferLen = streamLen);
|
2015-09-18 20:54:01 +03:00
|
|
|
}
|
2015-09-18 20:48:50 +03:00
|
|
|
|
2015-09-18 20:54:01 +03:00
|
|
|
if (mOutBuffer == nullptr) {
|
2015-09-18 20:48:50 +03:00
|
|
|
mOutBuffer = (unsigned char *) malloc(mOutBufferLen = streamLen * 3);
|
2015-09-18 20:54:01 +03:00
|
|
|
}
|
2015-09-18 20:48:50 +03:00
|
|
|
|
2015-09-18 20:54:01 +03:00
|
|
|
if (mInpBuffer == nullptr || mOutBuffer == nullptr) {
|
2015-09-18 20:48:50 +03:00
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
2015-09-18 20:54:01 +03:00
|
|
|
}
|
2015-09-18 20:48:50 +03:00
|
|
|
|
|
|
|
uint32_t unused;
|
|
|
|
iStr->Read((char *)mInpBuffer, streamLen, &unused);
|
|
|
|
|
2015-09-18 20:54:01 +03:00
|
|
|
if (mMode == HTTP_COMPRESS_DEFLATE) {
|
|
|
|
if (!mStreamInitialized) {
|
2015-09-18 20:48:50 +03:00
|
|
|
memset(&d_stream, 0, sizeof (d_stream));
|
2000-03-24 01:23:14 +03:00
|
|
|
|
2015-09-18 20:54:01 +03:00
|
|
|
if (inflateInit(&d_stream) != Z_OK) {
|
2015-09-18 20:48:50 +03:00
|
|
|
return NS_ERROR_FAILURE;
|
2015-09-18 20:54:01 +03:00
|
|
|
}
|
2000-03-24 01:23:14 +03:00
|
|
|
|
2015-09-18 20:48:50 +03:00
|
|
|
mStreamInitialized = true;
|
|
|
|
}
|
|
|
|
d_stream.next_in = mInpBuffer;
|
|
|
|
d_stream.avail_in = (uInt)streamLen;
|
2000-03-24 01:23:14 +03:00
|
|
|
|
2015-09-18 20:48:50 +03:00
|
|
|
mDummyStreamInitialised = false;
|
2015-09-18 20:54:01 +03:00
|
|
|
for (;;) {
|
2015-09-18 20:48:50 +03:00
|
|
|
d_stream.next_out = mOutBuffer;
|
|
|
|
d_stream.avail_out = (uInt)mOutBufferLen;
|
2011-03-24 06:30:04 +03:00
|
|
|
|
2015-09-18 20:48:50 +03:00
|
|
|
int code = inflate(&d_stream, Z_NO_FLUSH);
|
|
|
|
unsigned bytesWritten = (uInt)mOutBufferLen - d_stream.avail_out;
|
2000-03-23 04:09:39 +03:00
|
|
|
|
2015-09-18 20:54:01 +03:00
|
|
|
if (code == Z_STREAM_END) {
|
|
|
|
if (bytesWritten) {
|
2015-09-18 20:48:50 +03:00
|
|
|
rv = do_OnDataAvailable(request, aContext, aSourceOffset, (char *)mOutBuffer, bytesWritten);
|
2015-09-18 20:54:01 +03:00
|
|
|
if (NS_FAILED (rv)) {
|
2015-09-18 20:48:50 +03:00
|
|
|
return rv;
|
2015-09-18 20:54:01 +03:00
|
|
|
}
|
2015-09-18 20:48:50 +03:00
|
|
|
}
|
2015-07-01 00:17:10 +03:00
|
|
|
|
2015-09-18 20:48:50 +03:00
|
|
|
inflateEnd(&d_stream);
|
|
|
|
mStreamEnded = true;
|
|
|
|
break;
|
2015-09-18 20:54:01 +03:00
|
|
|
} else if (code == Z_OK) {
|
|
|
|
if (bytesWritten) {
|
2015-09-18 20:48:50 +03:00
|
|
|
rv = do_OnDataAvailable(request, aContext, aSourceOffset, (char *)mOutBuffer, bytesWritten);
|
2015-09-18 20:54:01 +03:00
|
|
|
if (NS_FAILED (rv)) {
|
2015-09-18 20:48:50 +03:00
|
|
|
return rv;
|
2015-09-18 20:54:01 +03:00
|
|
|
}
|
2015-09-18 20:48:50 +03:00
|
|
|
}
|
2015-09-18 20:54:01 +03:00
|
|
|
} else if (code == Z_BUF_ERROR) {
|
|
|
|
if (bytesWritten) {
|
2015-09-18 20:48:50 +03:00
|
|
|
rv = do_OnDataAvailable(request, aContext, aSourceOffset, (char *)mOutBuffer, bytesWritten);
|
2015-09-18 20:54:01 +03:00
|
|
|
if (NS_FAILED (rv)) {
|
2015-09-18 20:48:50 +03:00
|
|
|
return rv;
|
2015-09-18 20:54:01 +03:00
|
|
|
}
|
2015-09-18 20:48:50 +03:00
|
|
|
}
|
|
|
|
break;
|
2015-09-18 20:54:01 +03:00
|
|
|
} else if (code == Z_DATA_ERROR) {
|
2015-09-18 20:48:50 +03:00
|
|
|
// some servers (notably Apache with mod_deflate) don't generate zlib headers
|
|
|
|
// insert a dummy header and try again
|
|
|
|
static char dummy_head[2] =
|
|
|
|
{
|
|
|
|
0x8 + 0x7 * 0x10,
|
|
|
|
(((0x8 + 0x7 * 0x10) * 0x100 + 30) / 31 * 31) & 0xFF,
|
|
|
|
};
|
|
|
|
inflateReset(&d_stream);
|
|
|
|
d_stream.next_in = (Bytef*) dummy_head;
|
|
|
|
d_stream.avail_in = sizeof(dummy_head);
|
|
|
|
|
|
|
|
code = inflate(&d_stream, Z_NO_FLUSH);
|
2015-09-18 20:54:01 +03:00
|
|
|
if (code != Z_OK) {
|
2015-09-18 20:48:50 +03:00
|
|
|
return NS_ERROR_FAILURE;
|
2015-09-18 20:54:01 +03:00
|
|
|
}
|
2015-09-18 20:48:50 +03:00
|
|
|
|
|
|
|
// stop an endless loop caused by non-deflate data being labelled as deflate
|
|
|
|
if (mDummyStreamInitialised) {
|
|
|
|
NS_WARNING("endless loop detected"
|
|
|
|
" - invalid deflate");
|
|
|
|
return NS_ERROR_INVALID_CONTENT_ENCODING;
|
|
|
|
}
|
|
|
|
mDummyStreamInitialised = true;
|
|
|
|
// reset stream pointers to our original data
|
|
|
|
d_stream.next_in = mInpBuffer;
|
|
|
|
d_stream.avail_in = (uInt)streamLen;
|
|
|
|
} else {
|
|
|
|
return NS_ERROR_INVALID_CONTENT_ENCODING;
|
|
|
|
}
|
|
|
|
} /* for */
|
2015-09-18 20:54:01 +03:00
|
|
|
} else {
|
|
|
|
if (!mStreamInitialized) {
|
2015-09-18 20:48:50 +03:00
|
|
|
memset(&d_stream, 0, sizeof (d_stream));
|
2000-03-23 04:09:39 +03:00
|
|
|
|
2015-09-18 20:54:01 +03:00
|
|
|
if (inflateInit2(&d_stream, -MAX_WBITS) != Z_OK) {
|
2015-09-18 20:48:50 +03:00
|
|
|
return NS_ERROR_FAILURE;
|
2015-09-18 20:54:01 +03:00
|
|
|
}
|
2000-03-23 04:09:39 +03:00
|
|
|
|
2015-09-18 20:48:50 +03:00
|
|
|
mStreamInitialized = true;
|
|
|
|
}
|
2000-03-23 04:09:39 +03:00
|
|
|
|
2015-09-18 20:48:50 +03:00
|
|
|
d_stream.next_in = mInpBuffer;
|
|
|
|
d_stream.avail_in = (uInt)streamLen;
|
2000-03-23 04:09:39 +03:00
|
|
|
|
2015-09-18 20:54:01 +03:00
|
|
|
for (;;) {
|
2015-09-18 20:48:50 +03:00
|
|
|
d_stream.next_out = mOutBuffer;
|
|
|
|
d_stream.avail_out = (uInt)mOutBufferLen;
|
2000-03-23 04:09:39 +03:00
|
|
|
|
2015-09-18 20:48:50 +03:00
|
|
|
int code = inflate (&d_stream, Z_NO_FLUSH);
|
|
|
|
unsigned bytesWritten = (uInt)mOutBufferLen - d_stream.avail_out;
|
2000-03-23 04:09:39 +03:00
|
|
|
|
2015-09-18 20:54:01 +03:00
|
|
|
if (code == Z_STREAM_END) {
|
|
|
|
if (bytesWritten) {
|
2015-09-18 20:48:50 +03:00
|
|
|
rv = do_OnDataAvailable(request, aContext, aSourceOffset, (char *)mOutBuffer, bytesWritten);
|
2015-09-18 20:54:01 +03:00
|
|
|
if (NS_FAILED (rv)) {
|
2015-09-18 20:48:50 +03:00
|
|
|
return rv;
|
2015-09-18 20:54:01 +03:00
|
|
|
}
|
2015-09-18 20:48:50 +03:00
|
|
|
}
|
2000-03-23 04:09:39 +03:00
|
|
|
|
2015-09-18 20:48:50 +03:00
|
|
|
inflateEnd(&d_stream);
|
|
|
|
mStreamEnded = true;
|
|
|
|
break;
|
2015-09-18 20:54:01 +03:00
|
|
|
} else if (code == Z_OK) {
|
|
|
|
if (bytesWritten) {
|
2015-09-18 20:48:50 +03:00
|
|
|
rv = do_OnDataAvailable(request, aContext, aSourceOffset, (char *)mOutBuffer, bytesWritten);
|
2015-09-18 20:54:01 +03:00
|
|
|
if (NS_FAILED (rv)) {
|
|
|
|
return rv;
|
|
|
|
}
|
2015-09-18 20:48:50 +03:00
|
|
|
}
|
2015-09-18 20:54:01 +03:00
|
|
|
} else if (code == Z_BUF_ERROR) {
|
|
|
|
if (bytesWritten) {
|
2015-09-18 20:48:50 +03:00
|
|
|
rv = do_OnDataAvailable(request, aContext, aSourceOffset, (char *)mOutBuffer, bytesWritten);
|
2015-09-18 20:54:01 +03:00
|
|
|
if (NS_FAILED (rv)) {
|
2015-09-18 20:48:50 +03:00
|
|
|
return rv;
|
2015-09-18 20:54:01 +03:00
|
|
|
}
|
2015-09-18 20:48:50 +03:00
|
|
|
}
|
|
|
|
break;
|
2015-09-18 20:54:01 +03:00
|
|
|
} else {
|
2015-09-18 20:48:50 +03:00
|
|
|
return NS_ERROR_INVALID_CONTENT_ENCODING;
|
2015-09-18 20:54:01 +03:00
|
|
|
}
|
2015-09-18 20:48:50 +03:00
|
|
|
} /* for */
|
|
|
|
} /* gzip */
|
|
|
|
break;
|
|
|
|
|
2015-09-22 19:55:23 +03:00
|
|
|
case HTTP_COMPRESS_BROTLI:
|
|
|
|
{
|
|
|
|
if (!mBrotli) {
|
|
|
|
mBrotli = new BrotliWrapper();
|
|
|
|
}
|
|
|
|
|
|
|
|
mBrotli->mRequest = request;
|
|
|
|
mBrotli->mContext = aContext;
|
|
|
|
mBrotli->mSourceOffset = aSourceOffset;
|
|
|
|
|
|
|
|
uint32_t countRead;
|
|
|
|
rv = iStr->ReadSegments(BrotliHandler, this, streamLen, &countRead);
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
|
|
rv = mBrotli->mStatus;
|
|
|
|
}
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2015-09-18 20:48:50 +03:00
|
|
|
default:
|
|
|
|
rv = mListener->OnDataAvailable(request, aContext, iStr, aSourceOffset, aCount);
|
2015-09-18 20:54:01 +03:00
|
|
|
if (NS_FAILED (rv)) {
|
2015-09-18 20:48:50 +03:00
|
|
|
return rv;
|
2015-09-18 20:54:01 +03:00
|
|
|
}
|
2015-09-18 20:48:50 +03:00
|
|
|
} /* switch */
|
|
|
|
|
|
|
|
return NS_OK;
|
2000-03-23 04:09:39 +03:00
|
|
|
} /* OnDataAvailable */
|
|
|
|
|
|
|
|
// XXX/ruslan: need to implement this too
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2015-07-01 00:17:10 +03:00
|
|
|
nsHTTPCompressConv::Convert(nsIInputStream *aFromStream,
|
|
|
|
const char *aFromType,
|
|
|
|
const char *aToType,
|
|
|
|
nsISupports *aCtxt,
|
2003-04-15 03:51:50 +04:00
|
|
|
nsIInputStream **_retval)
|
2015-07-01 00:17:10 +03:00
|
|
|
{
|
2015-09-18 20:48:50 +03:00
|
|
|
return NS_ERROR_NOT_IMPLEMENTED;
|
2015-07-01 00:17:10 +03:00
|
|
|
}
|
2000-03-23 04:09:39 +03:00
|
|
|
|
|
|
|
nsresult
|
2006-01-02 05:30:32 +03:00
|
|
|
nsHTTPCompressConv::do_OnDataAvailable(nsIRequest* request,
|
2012-09-06 06:41:02 +04:00
|
|
|
nsISupports *context, uint64_t offset,
|
2012-08-22 19:56:38 +04:00
|
|
|
const char *buffer, uint32_t count)
|
2000-03-23 04:09:39 +03:00
|
|
|
{
|
2015-09-18 20:48:50 +03:00
|
|
|
if (!mStream) {
|
|
|
|
mStream = do_CreateInstance(NS_STRINGINPUTSTREAM_CONTRACTID);
|
|
|
|
NS_ENSURE_STATE(mStream);
|
|
|
|
}
|
2000-04-05 02:09:47 +04:00
|
|
|
|
2015-09-18 20:48:50 +03:00
|
|
|
mStream->ShareData(buffer, count);
|
2000-03-23 04:09:39 +03:00
|
|
|
|
2015-09-18 20:48:50 +03:00
|
|
|
nsresult rv = mListener->OnDataAvailable(request, context, mStream,
|
|
|
|
offset, count);
|
2000-03-23 04:09:39 +03:00
|
|
|
|
2015-09-18 20:48:50 +03:00
|
|
|
// Make sure the stream no longer references |buffer| in case our listener
|
|
|
|
// is crazy enough to try to read from |mStream| after ODA.
|
|
|
|
mStream->ShareData("", 0);
|
2015-07-24 18:49:25 +03:00
|
|
|
mDecodedDataLength += count;
|
2000-03-23 04:09:39 +03:00
|
|
|
|
2015-09-18 20:48:50 +03:00
|
|
|
return rv;
|
2000-03-23 04:09:39 +03:00
|
|
|
}
|
2000-03-24 01:23:14 +03:00
|
|
|
|
|
|
|
#define ASCII_FLAG 0x01 /* bit 0 set: file probably ascii text */
|
|
|
|
#define HEAD_CRC 0x02 /* bit 1 set: header CRC present */
|
|
|
|
#define EXTRA_FIELD 0x04 /* bit 2 set: extra field present */
|
|
|
|
#define ORIG_NAME 0x08 /* bit 3 set: original file name present */
|
|
|
|
#define COMMENT 0x10 /* bit 4 set: file comment present */
|
|
|
|
#define RESERVED 0xE0 /* bits 5..7: reserved */
|
|
|
|
|
|
|
|
static unsigned gz_magic[2] = {0x1f, 0x8b}; /* gzip magic header */
|
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t
|
|
|
|
nsHTTPCompressConv::check_header(nsIInputStream *iStr, uint32_t streamLen, nsresult *rs)
|
2000-03-24 01:23:14 +03:00
|
|
|
{
|
2015-09-18 20:48:50 +03:00
|
|
|
enum { GZIP_INIT = 0, GZIP_OS, GZIP_EXTRA0, GZIP_EXTRA1, GZIP_EXTRA2, GZIP_ORIG, GZIP_COMMENT, GZIP_CRC };
|
|
|
|
char c;
|
2000-03-24 01:23:14 +03:00
|
|
|
|
2015-09-18 20:48:50 +03:00
|
|
|
*rs = NS_OK;
|
2000-03-24 01:23:14 +03:00
|
|
|
|
2015-09-18 20:54:01 +03:00
|
|
|
if (mCheckHeaderDone) {
|
2015-09-18 20:48:50 +03:00
|
|
|
return streamLen;
|
2015-09-18 20:54:01 +03:00
|
|
|
}
|
2000-03-24 01:23:14 +03:00
|
|
|
|
2015-09-18 20:54:01 +03:00
|
|
|
while (streamLen) {
|
|
|
|
switch (hMode) {
|
2015-09-18 20:48:50 +03:00
|
|
|
case GZIP_INIT:
|
|
|
|
uint32_t unused;
|
|
|
|
iStr->Read(&c, 1, &unused);
|
|
|
|
streamLen--;
|
|
|
|
|
2015-09-18 20:54:01 +03:00
|
|
|
if (mSkipCount == 0 && ((unsigned)c & 0377) != gz_magic[0]) {
|
2015-09-18 20:48:50 +03:00
|
|
|
*rs = NS_ERROR_INVALID_CONTENT_ENCODING;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-09-18 20:54:01 +03:00
|
|
|
if (mSkipCount == 1 && ((unsigned)c & 0377) != gz_magic[1]) {
|
2015-09-18 20:48:50 +03:00
|
|
|
*rs = NS_ERROR_INVALID_CONTENT_ENCODING;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-09-18 20:54:01 +03:00
|
|
|
if (mSkipCount == 2 && ((unsigned)c & 0377) != Z_DEFLATED) {
|
2015-09-18 20:48:50 +03:00
|
|
|
*rs = NS_ERROR_INVALID_CONTENT_ENCODING;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
mSkipCount++;
|
2015-09-18 20:54:01 +03:00
|
|
|
if (mSkipCount == 4) {
|
2015-09-18 20:48:50 +03:00
|
|
|
mFlags = (unsigned) c & 0377;
|
2015-09-18 20:54:01 +03:00
|
|
|
if (mFlags & RESERVED) {
|
2015-09-18 20:48:50 +03:00
|
|
|
*rs = NS_ERROR_INVALID_CONTENT_ENCODING;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
hMode = GZIP_OS;
|
|
|
|
mSkipCount = 0;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case GZIP_OS:
|
|
|
|
iStr->Read(&c, 1, &unused);
|
|
|
|
streamLen--;
|
|
|
|
mSkipCount++;
|
|
|
|
|
2015-09-18 20:54:01 +03:00
|
|
|
if (mSkipCount == 6) {
|
2015-09-18 20:48:50 +03:00
|
|
|
hMode = GZIP_EXTRA0;
|
2015-09-18 20:54:01 +03:00
|
|
|
}
|
2015-09-18 20:48:50 +03:00
|
|
|
break;
|
|
|
|
|
|
|
|
case GZIP_EXTRA0:
|
2015-09-18 20:54:01 +03:00
|
|
|
if (mFlags & EXTRA_FIELD) {
|
2015-09-18 20:48:50 +03:00
|
|
|
iStr->Read(&c, 1, &unused);
|
|
|
|
streamLen--;
|
|
|
|
mLen = (uInt) c & 0377;
|
|
|
|
hMode = GZIP_EXTRA1;
|
2015-09-18 20:54:01 +03:00
|
|
|
} else {
|
2015-09-18 20:48:50 +03:00
|
|
|
hMode = GZIP_ORIG;
|
2015-09-18 20:54:01 +03:00
|
|
|
}
|
2015-09-18 20:48:50 +03:00
|
|
|
break;
|
|
|
|
|
|
|
|
case GZIP_EXTRA1:
|
|
|
|
iStr->Read(&c, 1, &unused);
|
|
|
|
streamLen--;
|
2015-12-17 00:33:40 +03:00
|
|
|
mLen |= ((uInt) c & 0377) << 8;
|
2015-09-18 20:48:50 +03:00
|
|
|
mSkipCount = 0;
|
|
|
|
hMode = GZIP_EXTRA2;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case GZIP_EXTRA2:
|
2015-09-18 20:54:01 +03:00
|
|
|
if (mSkipCount == mLen) {
|
2015-09-18 20:48:50 +03:00
|
|
|
hMode = GZIP_ORIG;
|
2015-09-18 20:54:01 +03:00
|
|
|
} else {
|
2015-09-18 20:48:50 +03:00
|
|
|
iStr->Read(&c, 1, &unused);
|
|
|
|
streamLen--;
|
|
|
|
mSkipCount++;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case GZIP_ORIG:
|
2015-09-18 20:54:01 +03:00
|
|
|
if (mFlags & ORIG_NAME) {
|
2015-09-18 20:48:50 +03:00
|
|
|
iStr->Read(&c, 1, &unused);
|
|
|
|
streamLen--;
|
|
|
|
if (c == 0)
|
|
|
|
hMode = GZIP_COMMENT;
|
2015-09-18 20:54:01 +03:00
|
|
|
} else {
|
2015-09-18 20:48:50 +03:00
|
|
|
hMode = GZIP_COMMENT;
|
2015-09-18 20:54:01 +03:00
|
|
|
}
|
2015-09-18 20:48:50 +03:00
|
|
|
break;
|
|
|
|
|
|
|
|
case GZIP_COMMENT:
|
2015-09-18 20:54:01 +03:00
|
|
|
if (mFlags & COMMENT) {
|
2015-09-18 20:48:50 +03:00
|
|
|
iStr->Read(&c, 1, &unused);
|
|
|
|
streamLen--;
|
2015-09-18 20:54:01 +03:00
|
|
|
if (c == 0) {
|
2015-09-18 20:48:50 +03:00
|
|
|
hMode = GZIP_CRC;
|
|
|
|
mSkipCount = 0;
|
|
|
|
}
|
2015-09-18 20:54:01 +03:00
|
|
|
} else {
|
2015-09-18 20:48:50 +03:00
|
|
|
hMode = GZIP_CRC;
|
|
|
|
mSkipCount = 0;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case GZIP_CRC:
|
2015-09-18 20:54:01 +03:00
|
|
|
if (mFlags & HEAD_CRC) {
|
2015-09-18 20:48:50 +03:00
|
|
|
iStr->Read(&c, 1, &unused);
|
|
|
|
streamLen--;
|
|
|
|
mSkipCount++;
|
2015-09-18 20:54:01 +03:00
|
|
|
if (mSkipCount == 2) {
|
2015-09-18 20:48:50 +03:00
|
|
|
mCheckHeaderDone = true;
|
|
|
|
return streamLen;
|
2000-03-24 01:23:14 +03:00
|
|
|
}
|
2015-09-18 20:54:01 +03:00
|
|
|
} else {
|
2015-09-18 20:48:50 +03:00
|
|
|
mCheckHeaderDone = true;
|
|
|
|
return streamLen;
|
|
|
|
}
|
|
|
|
break;
|
2000-03-24 01:23:14 +03:00
|
|
|
}
|
2015-09-18 20:48:50 +03:00
|
|
|
}
|
|
|
|
return streamLen;
|
2000-03-24 01:23:14 +03:00
|
|
|
}
|
2001-02-21 23:38:08 +03:00
|
|
|
|
2015-09-19 04:24:28 +03:00
|
|
|
} // namespace net
|
|
|
|
} // namespace mozilla
|
|
|
|
|
2001-02-21 23:38:08 +03:00
|
|
|
nsresult
|
2015-09-19 04:24:28 +03:00
|
|
|
NS_NewHTTPCompressConv(mozilla::net::nsHTTPCompressConv **aHTTPCompressConv)
|
2001-02-21 23:38:08 +03:00
|
|
|
{
|
2015-09-18 20:48:50 +03:00
|
|
|
NS_PRECONDITION(aHTTPCompressConv != nullptr, "null ptr");
|
2015-09-18 20:54:01 +03:00
|
|
|
if (!aHTTPCompressConv) {
|
2015-09-18 20:48:50 +03:00
|
|
|
return NS_ERROR_NULL_POINTER;
|
2015-09-18 20:54:01 +03:00
|
|
|
}
|
2001-02-21 23:38:08 +03:00
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<mozilla::net::nsHTTPCompressConv> outVal =
|
2015-09-18 20:58:14 +03:00
|
|
|
new mozilla::net::nsHTTPCompressConv();
|
|
|
|
if (!outVal) {
|
2015-09-18 20:48:50 +03:00
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
2015-09-18 20:54:01 +03:00
|
|
|
}
|
2015-09-18 20:58:14 +03:00
|
|
|
outVal.forget(aHTTPCompressConv);
|
2015-09-18 20:48:50 +03:00
|
|
|
return NS_OK;
|
2001-02-21 23:38:08 +03:00
|
|
|
}
|