2002-02-16 04:19:24 +03:00
|
|
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
|
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/. */
|
2002-02-16 04:19:24 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The MIME stream separates headers and a datastream. It also allows
|
|
|
|
* automatic creation of the content-length header.
|
|
|
|
*/
|
|
|
|
|
2012-08-28 16:41:04 +04:00
|
|
|
#include "ipc/IPCMessageUtils.h"
|
2010-10-21 22:36:13 +04:00
|
|
|
|
2002-02-16 04:19:24 +03:00
|
|
|
#include "nsCOMPtr.h"
|
|
|
|
#include "nsComponentManagerUtils.h"
|
2017-01-21 02:43:07 +03:00
|
|
|
#include "nsIHttpHeaderVisitor.h"
|
2002-02-16 04:19:24 +03:00
|
|
|
#include "nsIMIMEInputStream.h"
|
|
|
|
#include "nsISeekableStream.h"
|
2006-01-02 05:30:32 +03:00
|
|
|
#include "nsString.h"
|
2010-10-21 22:36:13 +04:00
|
|
|
#include "nsMIMEInputStream.h"
|
|
|
|
#include "nsIClassInfoImpl.h"
|
2012-08-23 06:13:54 +04:00
|
|
|
#include "nsIIPCSerializableInputStream.h"
|
2017-01-21 02:43:07 +03:00
|
|
|
#include "mozilla/Move.h"
|
2012-08-23 06:13:54 +04:00
|
|
|
#include "mozilla/ipc/InputStreamUtils.h"
|
|
|
|
|
|
|
|
using namespace mozilla::ipc;
|
2016-09-29 07:20:00 +03:00
|
|
|
using mozilla::Maybe;
|
2017-01-21 02:43:07 +03:00
|
|
|
using mozilla::Move;
|
2002-02-16 04:19:24 +03:00
|
|
|
|
|
|
|
class nsMIMEInputStream : public nsIMIMEInputStream,
|
2010-10-21 22:36:13 +04:00
|
|
|
public nsISeekableStream,
|
2012-08-23 06:13:54 +04:00
|
|
|
public nsIIPCSerializableInputStream
|
2002-02-16 04:19:24 +03:00
|
|
|
{
|
2014-06-24 20:36:44 +04:00
|
|
|
virtual ~nsMIMEInputStream();
|
|
|
|
|
2002-02-16 04:19:24 +03:00
|
|
|
public:
|
|
|
|
nsMIMEInputStream();
|
|
|
|
|
2013-07-19 06:24:13 +04:00
|
|
|
NS_DECL_THREADSAFE_ISUPPORTS
|
2002-02-16 04:19:24 +03:00
|
|
|
NS_DECL_NSIINPUTSTREAM
|
|
|
|
NS_DECL_NSIMIMEINPUTSTREAM
|
|
|
|
NS_DECL_NSISEEKABLESTREAM
|
2012-08-23 06:13:54 +04:00
|
|
|
NS_DECL_NSIIPCSERIALIZABLEINPUTSTREAM
|
|
|
|
|
2002-02-16 04:19:24 +03:00
|
|
|
private:
|
|
|
|
|
|
|
|
void InitStreams();
|
|
|
|
|
2015-01-18 05:00:34 +03:00
|
|
|
struct MOZ_STACK_CLASS ReadSegmentsState {
|
|
|
|
nsCOMPtr<nsIInputStream> mThisStream;
|
2002-02-16 04:19:24 +03:00
|
|
|
nsWriteSegmentFun mWriter;
|
|
|
|
void* mClosure;
|
|
|
|
};
|
2016-08-12 10:36:22 +03:00
|
|
|
static nsresult ReadSegCb(nsIInputStream* aIn, void* aClosure,
|
|
|
|
const char* aFromRawSegment, uint32_t aToOffset,
|
|
|
|
uint32_t aCount, uint32_t *aWriteCount);
|
2002-02-16 04:19:24 +03:00
|
|
|
|
2017-04-07 10:34:13 +03:00
|
|
|
bool IsIPCSerializable() const;
|
|
|
|
|
2017-01-21 02:43:07 +03:00
|
|
|
nsTArray<HeaderEntry> mHeaders;
|
|
|
|
|
|
|
|
nsCOMPtr<nsIInputStream> mStream;
|
2011-09-29 10:19:26 +04:00
|
|
|
bool mStartedReading;
|
2002-02-16 04:19:24 +03:00
|
|
|
};
|
|
|
|
|
2013-07-19 06:24:13 +04:00
|
|
|
NS_IMPL_ADDREF(nsMIMEInputStream)
|
|
|
|
NS_IMPL_RELEASE(nsMIMEInputStream)
|
2010-10-21 22:36:13 +04:00
|
|
|
|
2013-09-19 23:28:26 +04:00
|
|
|
NS_IMPL_CLASSINFO(nsMIMEInputStream, nullptr, nsIClassInfo::THREADSAFE,
|
2010-10-21 22:36:13 +04:00
|
|
|
NS_MIMEINPUTSTREAM_CID)
|
|
|
|
|
2017-04-07 10:34:13 +03:00
|
|
|
NS_INTERFACE_MAP_BEGIN(nsMIMEInputStream)
|
|
|
|
NS_INTERFACE_MAP_ENTRY(nsIMIMEInputStream)
|
|
|
|
NS_INTERFACE_MAP_ENTRY(nsIInputStream)
|
|
|
|
NS_INTERFACE_MAP_ENTRY(nsISeekableStream)
|
|
|
|
NS_INTERFACE_MAP_ENTRY_CONDITIONAL(nsIIPCSerializableInputStream,
|
|
|
|
IsIPCSerializable())
|
|
|
|
NS_INTERFACE_MAP_ENTRY_AMBIGUOUS(nsISupports, nsIMIMEInputStream)
|
|
|
|
NS_IMPL_QUERY_CLASSINFO(nsMIMEInputStream)
|
|
|
|
NS_INTERFACE_MAP_END
|
|
|
|
|
2014-04-27 11:06:00 +04:00
|
|
|
NS_IMPL_CI_INTERFACE_GETTER(nsMIMEInputStream,
|
2010-10-21 22:36:13 +04:00
|
|
|
nsIMIMEInputStream,
|
|
|
|
nsIInputStream,
|
2014-04-27 11:06:00 +04:00
|
|
|
nsISeekableStream)
|
2002-02-16 04:19:24 +03:00
|
|
|
|
2017-01-21 02:43:07 +03:00
|
|
|
nsMIMEInputStream::nsMIMEInputStream() : mStartedReading(false)
|
2002-02-16 04:19:24 +03:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
nsMIMEInputStream::~nsMIMEInputStream()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2011-09-29 10:19:26 +04:00
|
|
|
nsMIMEInputStream::GetAddContentLength(bool *aAddContentLength)
|
2002-02-16 04:19:24 +03:00
|
|
|
{
|
2017-01-21 02:43:07 +03:00
|
|
|
*aAddContentLength = true;
|
2002-02-16 04:19:24 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
NS_IMETHODIMP
|
2011-09-29 10:19:26 +04:00
|
|
|
nsMIMEInputStream::SetAddContentLength(bool aAddContentLength)
|
2002-02-16 04:19:24 +03:00
|
|
|
{
|
|
|
|
NS_ENSURE_FALSE(mStartedReading, NS_ERROR_FAILURE);
|
2017-01-21 02:43:07 +03:00
|
|
|
if (!aAddContentLength) {
|
|
|
|
// Content-Length is automatically added by the channel when setting the
|
|
|
|
// upload stream, so setting this to false has no practical effect.
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
2002-02-16 04:19:24 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsMIMEInputStream::AddHeader(const char *aName, const char *aValue)
|
|
|
|
{
|
|
|
|
NS_ENSURE_FALSE(mStartedReading, NS_ERROR_FAILURE);
|
|
|
|
|
2017-01-21 02:43:07 +03:00
|
|
|
HeaderEntry* entry = mHeaders.AppendElement();
|
|
|
|
entry->name().Append(aName);
|
|
|
|
entry->value().Append(aValue);
|
2002-02-16 04:19:24 +03:00
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2017-01-21 02:43:07 +03:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsMIMEInputStream::VisitHeaders(nsIHttpHeaderVisitor *visitor)
|
|
|
|
{
|
|
|
|
nsresult rv;
|
|
|
|
|
|
|
|
for (auto& header : mHeaders) {
|
|
|
|
rv = visitor->VisitHeader(header.name(), header.value());
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2002-02-16 04:19:24 +03:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsMIMEInputStream::SetData(nsIInputStream *aStream)
|
|
|
|
{
|
|
|
|
NS_ENSURE_FALSE(mStartedReading, NS_ERROR_FAILURE);
|
|
|
|
|
2017-01-21 02:43:07 +03:00
|
|
|
nsCOMPtr<nsISeekableStream> seekable = do_QueryInterface(aStream);
|
|
|
|
if (!seekable) {
|
|
|
|
return NS_ERROR_INVALID_ARG;
|
|
|
|
}
|
|
|
|
|
|
|
|
mStream = aStream;
|
2002-02-16 04:19:24 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2016-04-20 22:36:43 +03:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsMIMEInputStream::GetData(nsIInputStream **aStream)
|
|
|
|
{
|
|
|
|
NS_ENSURE_ARG_POINTER(aStream);
|
2017-01-21 02:43:07 +03:00
|
|
|
*aStream = mStream;
|
2016-04-20 22:36:43 +03:00
|
|
|
NS_IF_ADDREF(*aStream);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2002-02-16 04:19:24 +03:00
|
|
|
// set up the internal streams
|
|
|
|
void nsMIMEInputStream::InitStreams()
|
|
|
|
{
|
|
|
|
NS_ASSERTION(!mStartedReading,
|
|
|
|
"Don't call initStreams twice without rewinding");
|
|
|
|
|
2011-10-17 18:59:28 +04:00
|
|
|
mStartedReading = true;
|
2002-02-16 04:19:24 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#define INITSTREAMS \
|
|
|
|
if (!mStartedReading) { \
|
2017-01-21 02:43:07 +03:00
|
|
|
NS_ENSURE_TRUE(mStream, NS_ERROR_UNEXPECTED); \
|
2002-02-16 04:19:24 +03:00
|
|
|
InitStreams(); \
|
|
|
|
}
|
|
|
|
|
|
|
|
// Reset mStartedReading when Seek-ing to start
|
|
|
|
NS_IMETHODIMP
|
2012-08-22 19:56:38 +04:00
|
|
|
nsMIMEInputStream::Seek(int32_t whence, int64_t offset)
|
2002-02-16 04:19:24 +03:00
|
|
|
{
|
2017-01-21 02:43:07 +03:00
|
|
|
NS_ENSURE_TRUE(mStream, NS_ERROR_UNEXPECTED);
|
|
|
|
|
2002-02-16 04:19:24 +03:00
|
|
|
nsresult rv;
|
|
|
|
nsCOMPtr<nsISeekableStream> stream = do_QueryInterface(mStream);
|
2017-01-21 02:43:07 +03:00
|
|
|
|
2012-10-26 03:25:59 +04:00
|
|
|
if (whence == NS_SEEK_SET && offset == 0) {
|
2002-02-16 04:19:24 +03:00
|
|
|
rv = stream->Seek(whence, offset);
|
|
|
|
if (NS_SUCCEEDED(rv))
|
2011-10-17 18:59:28 +04:00
|
|
|
mStartedReading = false;
|
2002-02-16 04:19:24 +03:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
INITSTREAMS;
|
|
|
|
rv = stream->Seek(whence, offset);
|
|
|
|
}
|
|
|
|
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Proxy ReadSegments since we need to be a good little nsIInputStream
|
|
|
|
NS_IMETHODIMP nsMIMEInputStream::ReadSegments(nsWriteSegmentFun aWriter,
|
2012-08-22 19:56:38 +04:00
|
|
|
void *aClosure, uint32_t aCount,
|
|
|
|
uint32_t *_retval)
|
2002-02-16 04:19:24 +03:00
|
|
|
{
|
|
|
|
INITSTREAMS;
|
|
|
|
ReadSegmentsState state;
|
|
|
|
state.mThisStream = this;
|
|
|
|
state.mWriter = aWriter;
|
|
|
|
state.mClosure = aClosure;
|
|
|
|
return mStream->ReadSegments(ReadSegCb, &state, aCount, _retval);
|
|
|
|
}
|
|
|
|
|
2016-08-12 10:36:22 +03:00
|
|
|
nsresult
|
2002-02-16 04:19:24 +03:00
|
|
|
nsMIMEInputStream::ReadSegCb(nsIInputStream* aIn, void* aClosure,
|
|
|
|
const char* aFromRawSegment,
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t aToOffset, uint32_t aCount,
|
|
|
|
uint32_t *aWriteCount)
|
2002-02-16 04:19:24 +03:00
|
|
|
{
|
|
|
|
ReadSegmentsState* state = (ReadSegmentsState*)aClosure;
|
|
|
|
return (state->mWriter)(state->mThisStream,
|
|
|
|
state->mClosure,
|
|
|
|
aFromRawSegment,
|
|
|
|
aToOffset,
|
|
|
|
aCount,
|
|
|
|
aWriteCount);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Forward everything else to the mStream after calling InitStreams()
|
|
|
|
*/
|
|
|
|
|
|
|
|
// nsIInputStream
|
|
|
|
NS_IMETHODIMP nsMIMEInputStream::Close(void) { INITSTREAMS; return mStream->Close(); }
|
2012-08-22 19:56:38 +04:00
|
|
|
NS_IMETHODIMP nsMIMEInputStream::Available(uint64_t *_retval) { INITSTREAMS; return mStream->Available(_retval); }
|
|
|
|
NS_IMETHODIMP nsMIMEInputStream::Read(char * buf, uint32_t count, uint32_t *_retval) { INITSTREAMS; return mStream->Read(buf, count, _retval); }
|
2011-09-29 10:19:26 +04:00
|
|
|
NS_IMETHODIMP nsMIMEInputStream::IsNonBlocking(bool *aNonBlocking) { INITSTREAMS; return mStream->IsNonBlocking(aNonBlocking); }
|
2002-02-16 04:19:24 +03:00
|
|
|
|
|
|
|
// nsISeekableStream
|
2012-08-22 19:56:38 +04:00
|
|
|
NS_IMETHODIMP nsMIMEInputStream::Tell(int64_t *_retval)
|
2002-02-16 04:19:24 +03:00
|
|
|
{
|
|
|
|
INITSTREAMS;
|
|
|
|
nsCOMPtr<nsISeekableStream> stream = do_QueryInterface(mStream);
|
|
|
|
return stream->Tell(_retval);
|
|
|
|
}
|
|
|
|
NS_IMETHODIMP nsMIMEInputStream::SetEOF(void) {
|
|
|
|
INITSTREAMS;
|
|
|
|
nsCOMPtr<nsISeekableStream> stream = do_QueryInterface(mStream);
|
|
|
|
return stream->SetEOF();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Factory method used by do_CreateInstance
|
|
|
|
*/
|
|
|
|
|
2010-06-10 22:11:40 +04:00
|
|
|
nsresult
|
2002-02-16 04:19:24 +03:00
|
|
|
nsMIMEInputStreamConstructor(nsISupports *outer, REFNSIID iid, void **result)
|
|
|
|
{
|
2012-07-30 18:20:58 +04:00
|
|
|
*result = nullptr;
|
2002-02-16 04:19:24 +03:00
|
|
|
|
|
|
|
if (outer)
|
|
|
|
return NS_ERROR_NO_AGGREGATION;
|
|
|
|
|
2017-01-21 02:43:07 +03:00
|
|
|
RefPtr<nsMIMEInputStream> inst = new nsMIMEInputStream();
|
2002-02-16 04:19:24 +03:00
|
|
|
if (!inst)
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
|
2017-01-21 02:43:07 +03:00
|
|
|
return inst->QueryInterface(iid, result);
|
2002-02-16 04:19:24 +03:00
|
|
|
}
|
2010-10-21 22:36:13 +04:00
|
|
|
|
2012-08-23 06:13:54 +04:00
|
|
|
void
|
2014-03-25 22:37:13 +04:00
|
|
|
nsMIMEInputStream::Serialize(InputStreamParams& aParams,
|
|
|
|
FileDescriptorArray& aFileDescriptors)
|
2010-10-21 22:36:13 +04:00
|
|
|
{
|
2012-08-23 06:13:54 +04:00
|
|
|
MIMEInputStreamParams params;
|
2012-08-22 23:19:29 +04:00
|
|
|
|
2017-01-21 02:43:07 +03:00
|
|
|
if (mStream) {
|
2012-08-23 06:13:54 +04:00
|
|
|
InputStreamParams wrappedParams;
|
2017-03-21 09:47:25 +03:00
|
|
|
InputStreamHelper::SerializeInputStream(mStream, wrappedParams,
|
|
|
|
aFileDescriptors);
|
2010-10-21 22:36:13 +04:00
|
|
|
|
2012-08-23 06:13:54 +04:00
|
|
|
NS_ASSERTION(wrappedParams.type() != InputStreamParams::T__None,
|
|
|
|
"Wrapped stream failed to serialize!");
|
2010-10-21 22:36:13 +04:00
|
|
|
|
2012-08-23 06:13:54 +04:00
|
|
|
params.optionalStream() = wrappedParams;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
params.optionalStream() = mozilla::void_t();
|
2012-08-22 23:19:29 +04:00
|
|
|
}
|
|
|
|
|
2012-08-23 06:13:54 +04:00
|
|
|
params.headers() = mHeaders;
|
|
|
|
params.startedReading() = mStartedReading;
|
2012-08-22 23:19:29 +04:00
|
|
|
|
2012-08-23 06:13:54 +04:00
|
|
|
aParams = params;
|
2010-10-21 22:36:13 +04:00
|
|
|
}
|
2012-08-23 01:31:06 +04:00
|
|
|
|
2012-08-23 06:13:54 +04:00
|
|
|
bool
|
2014-03-25 22:37:13 +04:00
|
|
|
nsMIMEInputStream::Deserialize(const InputStreamParams& aParams,
|
|
|
|
const FileDescriptorArray& aFileDescriptors)
|
2012-08-23 01:31:06 +04:00
|
|
|
{
|
2012-08-23 06:13:54 +04:00
|
|
|
if (aParams.type() != InputStreamParams::TMIMEInputStreamParams) {
|
|
|
|
NS_ERROR("Received unknown parameters from the other process!");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
const MIMEInputStreamParams& params =
|
|
|
|
aParams.get_MIMEInputStreamParams();
|
|
|
|
const OptionalInputStreamParams& wrappedParams = params.optionalStream();
|
|
|
|
|
|
|
|
mHeaders = params.headers();
|
|
|
|
mStartedReading = params.startedReading();
|
2012-08-23 01:31:06 +04:00
|
|
|
|
2012-08-23 06:13:54 +04:00
|
|
|
if (wrappedParams.type() == OptionalInputStreamParams::TInputStreamParams) {
|
2017-01-21 02:43:07 +03:00
|
|
|
nsCOMPtr<nsIInputStream> stream;
|
2017-03-21 09:47:25 +03:00
|
|
|
stream =
|
|
|
|
InputStreamHelper::DeserializeInputStream(wrappedParams.get_InputStreamParams(),
|
|
|
|
aFileDescriptors);
|
2012-08-23 06:13:54 +04:00
|
|
|
if (!stream) {
|
|
|
|
NS_WARNING("Failed to deserialize wrapped stream!");
|
|
|
|
return false;
|
|
|
|
}
|
2012-08-23 01:31:06 +04:00
|
|
|
|
2017-01-21 02:43:07 +03:00
|
|
|
mStream = stream;
|
2012-08-23 06:13:54 +04:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
NS_ASSERTION(wrappedParams.type() == OptionalInputStreamParams::Tvoid_t,
|
|
|
|
"Unknown type for OptionalInputStreamParams!");
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
2012-08-23 01:31:06 +04:00
|
|
|
}
|
2016-09-29 07:20:00 +03:00
|
|
|
|
|
|
|
Maybe<uint64_t>
|
|
|
|
nsMIMEInputStream::ExpectedSerializedLength()
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsIIPCSerializableInputStream> serializable = do_QueryInterface(mStream);
|
|
|
|
return serializable ? serializable->ExpectedSerializedLength() : Nothing();
|
|
|
|
}
|
|
|
|
|
2017-04-07 10:34:13 +03:00
|
|
|
bool
|
|
|
|
nsMIMEInputStream::IsIPCSerializable() const
|
|
|
|
{
|
|
|
|
// If SetData() or Deserialize() has not be called yet, mStream is null.
|
|
|
|
if (!mStream) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<nsIIPCSerializableInputStream> serializable = do_QueryInterface(mStream);
|
|
|
|
return !!serializable;
|
|
|
|
}
|