2018-11-30 22:52:05 +03:00
|
|
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
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-08-23 13:39:36 +03:00
|
|
|
#include "nsIAsyncInputStream.h"
|
2018-05-23 08:12:35 +03:00
|
|
|
#include "nsIInputStreamLength.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"
|
2018-04-10 18:33:08 +03:00
|
|
|
#include "mozilla/Mutex.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;
|
2002-02-16 04:19:24 +03:00
|
|
|
|
|
|
|
class nsMIMEInputStream : public nsIMIMEInputStream,
|
2010-10-21 22:36:13 +04:00
|
|
|
public nsISeekableStream,
|
2017-08-23 13:39:36 +03:00
|
|
|
public nsIIPCSerializableInputStream,
|
2018-03-26 20:06:31 +03:00
|
|
|
public nsIAsyncInputStream,
|
2018-05-23 08:12:35 +03:00
|
|
|
public nsIInputStreamCallback,
|
|
|
|
public nsIInputStreamLength,
|
|
|
|
public nsIAsyncInputStreamLength,
|
2018-10-03 23:51:56 +03:00
|
|
|
public nsIInputStreamLengthCallback,
|
|
|
|
public nsICloneableInputStream {
|
2018-04-30 19:46:04 +03:00
|
|
|
virtual ~nsMIMEInputStream() = default;
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2002-02-16 04:19:24 +03:00
|
|
|
public:
|
|
|
|
nsMIMEInputStream();
|
2018-11-30 13:46:48 +03:00
|
|
|
|
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
|
Bug 1496581 - Split nsISeekableStream in 2 classes: nsISeekableStream and nsITellableStream, f=mayhemer, r=froydnj
In the current code there are 3 main issues:
1. nsFileStream is not really thread-safe. There is nothing to protect the
internal members and we see crashes.
2. nsPipeInputStream doesn't implement ::Seek() method and that caused issues
in devtools when a nsHttpChannel sends POST data using a pipe. In order to fix
this, bug 1494176 added a check in nsHttpChannel: if the stream doesn't
implement ::Seek(), let's clone it. This was an hack around nsPipeInputStream,
and it's bad.
3. When nsHttpChannel sends POST data using a file stream, nsFileStream does
I/O on main-thread because of the issue 2. Plus, ::Seek() is called on the
main-thread causing issue 1.
Note that nsPipeInputStream implements only ::Tell(), of the nsISeekableStream
methods. It doesn't implement ::Seek() and it doesn't implement ::SetEOF().
With this patch I want to fix point 2 and point 3 (and consequentially issue 1
- but we need a separate fix for it - follow up). The patch does:
1. it splits nsISeekableStream in 2 interfaces: nsITellableStream and
nsISeekableStream.
2. nsPipeInputStream implements only nsITellableStream. Doing this, we don't
need the ::Seek() check for point 2 in nsHttpChannel: a simple QI check is
enough.
3. Because we don't call ::Seek() in nsHttpChannel, nsFileStream doesn't do I/O
on the main-thread, and we don't crash doing so.
2018-10-18 14:35:35 +03:00
|
|
|
NS_DECL_NSITELLABLESTREAM
|
2012-08-23 06:13:54 +04:00
|
|
|
NS_DECL_NSIIPCSERIALIZABLEINPUTSTREAM
|
2017-08-23 13:39:36 +03:00
|
|
|
NS_DECL_NSIASYNCINPUTSTREAM
|
2018-03-26 20:06:31 +03:00
|
|
|
NS_DECL_NSIINPUTSTREAMCALLBACK
|
2018-05-23 08:12:35 +03:00
|
|
|
NS_DECL_NSIINPUTSTREAMLENGTH
|
|
|
|
NS_DECL_NSIASYNCINPUTSTREAMLENGTH
|
|
|
|
NS_DECL_NSIINPUTSTREAMLENGTHCALLBACK
|
2018-10-03 23:51:56 +03:00
|
|
|
NS_DECL_NSICLONEABLEINPUTSTREAM
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2002-02-16 04:19:24 +03:00
|
|
|
private:
|
|
|
|
void InitStreams();
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2019-01-28 12:48:35 +03:00
|
|
|
template <typename M>
|
|
|
|
void SerializeInternal(InputStreamParams& aParams,
|
|
|
|
FileDescriptorArray& aFileDescriptors,
|
2019-02-05 01:50:51 +03:00
|
|
|
bool aDelayedStart, uint32_t aMaxSize,
|
|
|
|
uint32_t* aSizeUsed, M* aManager);
|
2019-01-28 12:48:35 +03:00
|
|
|
|
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);
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2019-02-05 00:50:47 +03:00
|
|
|
bool IsSeekableInputStream() const;
|
2017-08-23 13:39:36 +03:00
|
|
|
bool IsAsyncInputStream() const;
|
2017-04-07 10:34:13 +03:00
|
|
|
bool IsIPCSerializable() const;
|
2018-05-23 08:12:35 +03:00
|
|
|
bool IsInputStreamLength() const;
|
|
|
|
bool IsAsyncInputStreamLength() const;
|
2018-10-03 23:51:56 +03:00
|
|
|
bool IsCloneableInputStream() const;
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2017-01-21 02:43:07 +03:00
|
|
|
nsTArray<HeaderEntry> mHeaders;
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2017-01-21 02:43:07 +03:00
|
|
|
nsCOMPtr<nsIInputStream> mStream;
|
2011-09-29 10:19:26 +04:00
|
|
|
bool mStartedReading;
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2018-04-10 18:33:08 +03:00
|
|
|
mozilla::Mutex mMutex;
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2018-04-10 18:33:08 +03:00
|
|
|
// This is protected by mutex.
|
2018-03-26 20:06:31 +03:00
|
|
|
nsCOMPtr<nsIInputStreamCallback> mAsyncWaitCallback;
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2018-05-23 08:12:35 +03:00
|
|
|
// This is protected by mutex.
|
|
|
|
nsCOMPtr<nsIInputStreamLengthCallback> mAsyncInputStreamLengthCallback;
|
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)
|
2017-08-23 13:39:36 +03:00
|
|
|
NS_INTERFACE_MAP_ENTRY_AMBIGUOUS(nsIInputStream, nsIMIMEInputStream)
|
Bug 1496581 - Split nsISeekableStream in 2 classes: nsISeekableStream and nsITellableStream, f=mayhemer, r=froydnj
In the current code there are 3 main issues:
1. nsFileStream is not really thread-safe. There is nothing to protect the
internal members and we see crashes.
2. nsPipeInputStream doesn't implement ::Seek() method and that caused issues
in devtools when a nsHttpChannel sends POST data using a pipe. In order to fix
this, bug 1494176 added a check in nsHttpChannel: if the stream doesn't
implement ::Seek(), let's clone it. This was an hack around nsPipeInputStream,
and it's bad.
3. When nsHttpChannel sends POST data using a file stream, nsFileStream does
I/O on main-thread because of the issue 2. Plus, ::Seek() is called on the
main-thread causing issue 1.
Note that nsPipeInputStream implements only ::Tell(), of the nsISeekableStream
methods. It doesn't implement ::Seek() and it doesn't implement ::SetEOF().
With this patch I want to fix point 2 and point 3 (and consequentially issue 1
- but we need a separate fix for it - follow up). The patch does:
1. it splits nsISeekableStream in 2 interfaces: nsITellableStream and
nsISeekableStream.
2. nsPipeInputStream implements only nsITellableStream. Doing this, we don't
need the ::Seek() check for point 2 in nsHttpChannel: a simple QI check is
enough.
3. Because we don't call ::Seek() in nsHttpChannel, nsFileStream doesn't do I/O
on the main-thread, and we don't crash doing so.
2018-10-18 14:35:35 +03:00
|
|
|
NS_INTERFACE_MAP_ENTRY(nsITellableStream)
|
2019-02-05 00:50:47 +03:00
|
|
|
NS_INTERFACE_MAP_ENTRY_CONDITIONAL(nsISeekableStream, IsSeekableInputStream())
|
2017-04-07 10:34:13 +03:00
|
|
|
NS_INTERFACE_MAP_ENTRY_CONDITIONAL(nsIIPCSerializableInputStream,
|
|
|
|
IsIPCSerializable())
|
2017-08-23 13:39:36 +03:00
|
|
|
NS_INTERFACE_MAP_ENTRY_CONDITIONAL(nsIAsyncInputStream, IsAsyncInputStream())
|
2018-03-26 20:06:31 +03:00
|
|
|
NS_INTERFACE_MAP_ENTRY_CONDITIONAL(nsIInputStreamCallback,
|
|
|
|
IsAsyncInputStream())
|
2017-04-07 10:34:13 +03:00
|
|
|
NS_INTERFACE_MAP_ENTRY_AMBIGUOUS(nsISupports, nsIMIMEInputStream)
|
2018-05-23 08:12:35 +03:00
|
|
|
NS_INTERFACE_MAP_ENTRY_CONDITIONAL(nsIInputStreamLength,
|
|
|
|
IsInputStreamLength())
|
|
|
|
NS_INTERFACE_MAP_ENTRY_CONDITIONAL(nsIAsyncInputStreamLength,
|
|
|
|
IsAsyncInputStreamLength())
|
|
|
|
NS_INTERFACE_MAP_ENTRY_CONDITIONAL(nsIInputStreamLengthCallback,
|
|
|
|
IsAsyncInputStreamLength())
|
2018-10-03 23:51:56 +03:00
|
|
|
NS_INTERFACE_MAP_ENTRY_CONDITIONAL(nsICloneableInputStream,
|
|
|
|
IsCloneableInputStream())
|
2017-04-07 10:34:13 +03:00
|
|
|
NS_IMPL_QUERY_CLASSINFO(nsMIMEInputStream)
|
|
|
|
NS_INTERFACE_MAP_END
|
|
|
|
|
2014-04-27 11:06:00 +04:00
|
|
|
NS_IMPL_CI_INTERFACE_GETTER(nsMIMEInputStream, nsIMIMEInputStream,
|
2017-08-23 13:39:36 +03:00
|
|
|
nsIAsyncInputStream, nsIInputStream,
|
Bug 1496581 - Split nsISeekableStream in 2 classes: nsISeekableStream and nsITellableStream, f=mayhemer, r=froydnj
In the current code there are 3 main issues:
1. nsFileStream is not really thread-safe. There is nothing to protect the
internal members and we see crashes.
2. nsPipeInputStream doesn't implement ::Seek() method and that caused issues
in devtools when a nsHttpChannel sends POST data using a pipe. In order to fix
this, bug 1494176 added a check in nsHttpChannel: if the stream doesn't
implement ::Seek(), let's clone it. This was an hack around nsPipeInputStream,
and it's bad.
3. When nsHttpChannel sends POST data using a file stream, nsFileStream does
I/O on main-thread because of the issue 2. Plus, ::Seek() is called on the
main-thread causing issue 1.
Note that nsPipeInputStream implements only ::Tell(), of the nsISeekableStream
methods. It doesn't implement ::Seek() and it doesn't implement ::SetEOF().
With this patch I want to fix point 2 and point 3 (and consequentially issue 1
- but we need a separate fix for it - follow up). The patch does:
1. it splits nsISeekableStream in 2 interfaces: nsITellableStream and
nsISeekableStream.
2. nsPipeInputStream implements only nsITellableStream. Doing this, we don't
need the ::Seek() check for point 2 in nsHttpChannel: a simple QI check is
enough.
3. Because we don't call ::Seek() in nsHttpChannel, nsFileStream doesn't do I/O
on the main-thread, and we don't crash doing so.
2018-10-18 14:35:35 +03:00
|
|
|
nsISeekableStream, nsITellableStream)
|
2002-02-16 04:19:24 +03:00
|
|
|
|
2018-04-10 18:33:08 +03:00
|
|
|
nsMIMEInputStream::nsMIMEInputStream()
|
|
|
|
: mStartedReading(false), mMutex("nsMIMEInputStream::mMutex") {}
|
2002-02-16 04:19:24 +03:00
|
|
|
|
|
|
|
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) {
|
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);
|
2011-10-17 18:59:28 +04:00
|
|
|
if (NS_SUCCEEDED(rv)) 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;
|
2017-08-23 13:39:36 +03:00
|
|
|
// Disambiguate ambiguous nsIInputStream.
|
|
|
|
state.mThisStream =
|
|
|
|
static_cast<nsIInputStream*>(static_cast<nsIMIMEInputStream*>(this));
|
2002-02-16 04:19:24 +03:00
|
|
|
state.mWriter = aWriter;
|
|
|
|
state.mClosure = aClosure;
|
|
|
|
return mStream->ReadSegments(ReadSegCb, &state, aCount, _retval);
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult 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
|
|
|
|
2017-08-23 13:39:36 +03:00
|
|
|
// nsIAsyncInputStream
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsMIMEInputStream::CloseWithStatus(nsresult aStatus) {
|
|
|
|
INITSTREAMS;
|
|
|
|
nsCOMPtr<nsIAsyncInputStream> asyncStream = do_QueryInterface(mStream);
|
|
|
|
return asyncStream->CloseWithStatus(aStatus);
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsMIMEInputStream::AsyncWait(nsIInputStreamCallback* aCallback, uint32_t aFlags,
|
|
|
|
uint32_t aRequestedCount,
|
|
|
|
nsIEventTarget* aEventTarget) {
|
|
|
|
INITSTREAMS;
|
|
|
|
nsCOMPtr<nsIAsyncInputStream> asyncStream = do_QueryInterface(mStream);
|
2018-03-26 20:06:31 +03:00
|
|
|
if (NS_WARN_IF(!asyncStream)) {
|
|
|
|
return NS_ERROR_FAILURE;
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
|
|
|
|
2018-04-10 18:33:08 +03:00
|
|
|
nsCOMPtr<nsIInputStreamCallback> callback = aCallback ? this : nullptr;
|
2018-11-30 13:46:48 +03:00
|
|
|
{
|
2018-04-10 18:33:08 +03:00
|
|
|
MutexAutoLock lock(mMutex);
|
|
|
|
if (mAsyncWaitCallback && aCallback) {
|
2018-03-26 20:06:31 +03:00
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
2018-04-10 18:33:08 +03:00
|
|
|
mAsyncWaitCallback = aCallback;
|
|
|
|
}
|
2018-03-26 20:06:31 +03:00
|
|
|
|
2018-04-10 18:33:08 +03:00
|
|
|
return asyncStream->AsyncWait(callback, aFlags, aRequestedCount,
|
|
|
|
aEventTarget);
|
2018-03-26 20:06:31 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// nsIInputStreamCallback
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsMIMEInputStream::OnInputStreamReady(nsIAsyncInputStream* aStream) {
|
2018-04-10 18:33:08 +03:00
|
|
|
nsCOMPtr<nsIInputStreamCallback> callback;
|
|
|
|
|
|
|
|
{
|
|
|
|
MutexAutoLock lock(mMutex);
|
|
|
|
|
|
|
|
// We have been canceled in the meanwhile.
|
|
|
|
if (!mAsyncWaitCallback) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
callback.swap(mAsyncWaitCallback);
|
2018-03-26 20:06:31 +03:00
|
|
|
}
|
|
|
|
|
2018-04-10 18:33:08 +03:00
|
|
|
MOZ_ASSERT(callback);
|
2018-03-26 20:06:31 +03:00
|
|
|
return callback->OnInputStreamReady(this);
|
2017-08-23 13:39:36 +03:00
|
|
|
}
|
|
|
|
|
Bug 1496581 - Split nsISeekableStream in 2 classes: nsISeekableStream and nsITellableStream, f=mayhemer, r=froydnj
In the current code there are 3 main issues:
1. nsFileStream is not really thread-safe. There is nothing to protect the
internal members and we see crashes.
2. nsPipeInputStream doesn't implement ::Seek() method and that caused issues
in devtools when a nsHttpChannel sends POST data using a pipe. In order to fix
this, bug 1494176 added a check in nsHttpChannel: if the stream doesn't
implement ::Seek(), let's clone it. This was an hack around nsPipeInputStream,
and it's bad.
3. When nsHttpChannel sends POST data using a file stream, nsFileStream does
I/O on main-thread because of the issue 2. Plus, ::Seek() is called on the
main-thread causing issue 1.
Note that nsPipeInputStream implements only ::Tell(), of the nsISeekableStream
methods. It doesn't implement ::Seek() and it doesn't implement ::SetEOF().
With this patch I want to fix point 2 and point 3 (and consequentially issue 1
- but we need a separate fix for it - follow up). The patch does:
1. it splits nsISeekableStream in 2 interfaces: nsITellableStream and
nsISeekableStream.
2. nsPipeInputStream implements only nsITellableStream. Doing this, we don't
need the ::Seek() check for point 2 in nsHttpChannel: a simple QI check is
enough.
3. Because we don't call ::Seek() in nsHttpChannel, nsFileStream doesn't do I/O
on the main-thread, and we don't crash doing so.
2018-10-18 14:35:35 +03:00
|
|
|
// nsITellableStream
|
2012-08-22 19:56:38 +04:00
|
|
|
NS_IMETHODIMP nsMIMEInputStream::Tell(int64_t* _retval) {
|
2002-02-16 04:19:24 +03:00
|
|
|
INITSTREAMS;
|
Bug 1496581 - Split nsISeekableStream in 2 classes: nsISeekableStream and nsITellableStream, f=mayhemer, r=froydnj
In the current code there are 3 main issues:
1. nsFileStream is not really thread-safe. There is nothing to protect the
internal members and we see crashes.
2. nsPipeInputStream doesn't implement ::Seek() method and that caused issues
in devtools when a nsHttpChannel sends POST data using a pipe. In order to fix
this, bug 1494176 added a check in nsHttpChannel: if the stream doesn't
implement ::Seek(), let's clone it. This was an hack around nsPipeInputStream,
and it's bad.
3. When nsHttpChannel sends POST data using a file stream, nsFileStream does
I/O on main-thread because of the issue 2. Plus, ::Seek() is called on the
main-thread causing issue 1.
Note that nsPipeInputStream implements only ::Tell(), of the nsISeekableStream
methods. It doesn't implement ::Seek() and it doesn't implement ::SetEOF().
With this patch I want to fix point 2 and point 3 (and consequentially issue 1
- but we need a separate fix for it - follow up). The patch does:
1. it splits nsISeekableStream in 2 interfaces: nsITellableStream and
nsISeekableStream.
2. nsPipeInputStream implements only nsITellableStream. Doing this, we don't
need the ::Seek() check for point 2 in nsHttpChannel: a simple QI check is
enough.
3. Because we don't call ::Seek() in nsHttpChannel, nsFileStream doesn't do I/O
on the main-thread, and we don't crash doing so.
2018-10-18 14:35:35 +03:00
|
|
|
nsCOMPtr<nsITellableStream> stream = do_QueryInterface(mStream);
|
2002-02-16 04:19:24 +03:00
|
|
|
return stream->Tell(_retval);
|
|
|
|
}
|
Bug 1496581 - Split nsISeekableStream in 2 classes: nsISeekableStream and nsITellableStream, f=mayhemer, r=froydnj
In the current code there are 3 main issues:
1. nsFileStream is not really thread-safe. There is nothing to protect the
internal members and we see crashes.
2. nsPipeInputStream doesn't implement ::Seek() method and that caused issues
in devtools when a nsHttpChannel sends POST data using a pipe. In order to fix
this, bug 1494176 added a check in nsHttpChannel: if the stream doesn't
implement ::Seek(), let's clone it. This was an hack around nsPipeInputStream,
and it's bad.
3. When nsHttpChannel sends POST data using a file stream, nsFileStream does
I/O on main-thread because of the issue 2. Plus, ::Seek() is called on the
main-thread causing issue 1.
Note that nsPipeInputStream implements only ::Tell(), of the nsISeekableStream
methods. It doesn't implement ::Seek() and it doesn't implement ::SetEOF().
With this patch I want to fix point 2 and point 3 (and consequentially issue 1
- but we need a separate fix for it - follow up). The patch does:
1. it splits nsISeekableStream in 2 interfaces: nsITellableStream and
nsISeekableStream.
2. nsPipeInputStream implements only nsITellableStream. Doing this, we don't
need the ::Seek() check for point 2 in nsHttpChannel: a simple QI check is
enough.
3. Because we don't call ::Seek() in nsHttpChannel, nsFileStream doesn't do I/O
on the main-thread, and we don't crash doing so.
2018-10-18 14:35:35 +03:00
|
|
|
|
|
|
|
// nsISeekableStream
|
2002-02-16 04:19:24 +03:00
|
|
|
NS_IMETHODIMP nsMIMEInputStream::SetEOF(void) {
|
|
|
|
INITSTREAMS;
|
|
|
|
nsCOMPtr<nsISeekableStream> stream = do_QueryInterface(mStream);
|
|
|
|
return stream->SetEOF();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Factory method used by do_CreateInstance
|
|
|
|
*/
|
|
|
|
|
|
|
|
nsresult 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
|
|
|
|
2014-03-25 22:37:13 +04:00
|
|
|
void nsMIMEInputStream::Serialize(InputStreamParams& aParams,
|
2019-01-28 12:48:35 +03:00
|
|
|
FileDescriptorArray& aFileDescriptors,
|
2019-02-05 01:50:51 +03:00
|
|
|
bool aDelayedStart, uint32_t aMaxSize,
|
|
|
|
uint32_t* aSizeUsed,
|
2019-02-25 23:04:49 +03:00
|
|
|
mozilla::dom::ContentChild* aManager) {
|
2019-02-05 01:50:51 +03:00
|
|
|
SerializeInternal(aParams, aFileDescriptors, aDelayedStart, aMaxSize,
|
|
|
|
aSizeUsed, aManager);
|
2019-01-28 12:48:35 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void nsMIMEInputStream::Serialize(InputStreamParams& aParams,
|
|
|
|
FileDescriptorArray& aFileDescriptors,
|
2019-02-05 01:50:51 +03:00
|
|
|
bool aDelayedStart, uint32_t aMaxSize,
|
|
|
|
uint32_t* aSizeUsed,
|
2019-01-28 12:48:35 +03:00
|
|
|
PBackgroundChild* aManager) {
|
2019-02-05 01:50:51 +03:00
|
|
|
SerializeInternal(aParams, aFileDescriptors, aDelayedStart, aMaxSize,
|
|
|
|
aSizeUsed, aManager);
|
2019-01-28 12:48:35 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void nsMIMEInputStream::Serialize(InputStreamParams& aParams,
|
|
|
|
FileDescriptorArray& aFileDescriptors,
|
2019-02-05 01:50:51 +03:00
|
|
|
bool aDelayedStart, uint32_t aMaxSize,
|
|
|
|
uint32_t* aSizeUsed,
|
2019-02-25 23:04:47 +03:00
|
|
|
mozilla::dom::ContentParent* aManager) {
|
2019-02-05 01:50:51 +03:00
|
|
|
SerializeInternal(aParams, aFileDescriptors, aDelayedStart, aMaxSize,
|
|
|
|
aSizeUsed, aManager);
|
2019-01-28 12:48:35 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void nsMIMEInputStream::Serialize(InputStreamParams& aParams,
|
|
|
|
FileDescriptorArray& aFileDescriptors,
|
2019-02-05 01:50:51 +03:00
|
|
|
bool aDelayedStart, uint32_t aMaxSize,
|
|
|
|
uint32_t* aSizeUsed,
|
2019-01-28 12:48:35 +03:00
|
|
|
PBackgroundParent* aManager) {
|
2019-02-05 01:50:51 +03:00
|
|
|
SerializeInternal(aParams, aFileDescriptors, aDelayedStart, aMaxSize,
|
|
|
|
aSizeUsed, aManager);
|
2019-01-28 12:48:35 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
template <typename M>
|
|
|
|
void nsMIMEInputStream::SerializeInternal(InputStreamParams& aParams,
|
|
|
|
FileDescriptorArray& aFileDescriptors,
|
2019-02-05 01:50:51 +03:00
|
|
|
bool aDelayedStart, uint32_t aMaxSize,
|
|
|
|
uint32_t* aSizeUsed, M* aManager) {
|
|
|
|
MOZ_ASSERT(aSizeUsed);
|
|
|
|
*aSizeUsed = 0;
|
|
|
|
|
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;
|
2019-02-05 01:50:51 +03:00
|
|
|
InputStreamHelper::SerializeInputStream(mStream, wrappedParams,
|
|
|
|
aFileDescriptors, aDelayedStart,
|
|
|
|
aMaxSize, aSizeUsed, aManager);
|
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
|
|
|
|
2014-03-25 22:37:13 +04:00
|
|
|
bool nsMIMEInputStream::Deserialize(
|
|
|
|
const InputStreamParams& aParams,
|
|
|
|
const FileDescriptorArray& aFileDescriptors) {
|
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();
|
2012-08-23 01:31:06 +04:00
|
|
|
|
2012-08-23 06:13:54 +04:00
|
|
|
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) {
|
|
|
|
nsCOMPtr<nsIInputStream> stream;
|
|
|
|
stream = InputStreamHelper::DeserializeInputStream(
|
|
|
|
wrappedParams.get_InputStreamParams(), aFileDescriptors);
|
|
|
|
if (!stream) {
|
|
|
|
NS_WARNING("Failed to deserialize wrapped stream!");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-01-21 02:43:07 +03:00
|
|
|
mStream = stream;
|
2018-11-30 13:46:48 +03:00
|
|
|
} else {
|
2012-08-23 06:13:54 +04:00
|
|
|
NS_ASSERTION(wrappedParams.type() == OptionalInputStreamParams::Tvoid_t,
|
|
|
|
"Unknown type for OptionalInputStreamParams!");
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
|
|
|
|
2012-08-23 06:13:54 +04:00
|
|
|
return true;
|
2012-08-23 01:31:06 +04:00
|
|
|
}
|
2016-09-29 07:20:00 +03:00
|
|
|
|
2018-05-23 08:12:35 +03:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsMIMEInputStream::Length(int64_t* aLength) {
|
|
|
|
nsCOMPtr<nsIInputStreamLength> stream = do_QueryInterface(mStream);
|
|
|
|
if (NS_WARN_IF(!stream)) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
return stream->Length(aLength);
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsMIMEInputStream::AsyncLengthWait(nsIInputStreamLengthCallback* aCallback,
|
|
|
|
nsIEventTarget* aEventTarget) {
|
|
|
|
nsCOMPtr<nsIAsyncInputStreamLength> stream = do_QueryInterface(mStream);
|
|
|
|
if (NS_WARN_IF(!stream)) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<nsIInputStreamLengthCallback> callback = aCallback ? this : nullptr;
|
|
|
|
{
|
|
|
|
MutexAutoLock lock(mMutex);
|
|
|
|
mAsyncInputStreamLengthCallback = aCallback;
|
|
|
|
}
|
|
|
|
|
|
|
|
return stream->AsyncLengthWait(callback, aEventTarget);
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsMIMEInputStream::OnInputStreamLengthReady(nsIAsyncInputStreamLength* aStream,
|
|
|
|
int64_t aLength) {
|
|
|
|
nsCOMPtr<nsIInputStreamLengthCallback> callback;
|
|
|
|
{
|
|
|
|
MutexAutoLock lock(mMutex);
|
|
|
|
// We have been canceled in the meanwhile.
|
|
|
|
if (!mAsyncInputStreamLengthCallback) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
callback.swap(mAsyncInputStreamLengthCallback);
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
|
|
|
|
2018-05-23 08:12:35 +03:00
|
|
|
MOZ_ASSERT(callback);
|
|
|
|
return callback->OnInputStreamLengthReady(this, aLength);
|
|
|
|
}
|
|
|
|
|
2019-02-05 00:50:47 +03:00
|
|
|
bool nsMIMEInputStream::IsSeekableInputStream() const {
|
|
|
|
nsCOMPtr<nsISeekableStream> seekable = do_QueryInterface(mStream);
|
|
|
|
return !!seekable;
|
|
|
|
}
|
|
|
|
|
2017-08-23 13:39:36 +03:00
|
|
|
bool nsMIMEInputStream::IsAsyncInputStream() const {
|
|
|
|
nsCOMPtr<nsIAsyncInputStream> asyncStream = do_QueryInterface(mStream);
|
|
|
|
return !!asyncStream;
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
}
|
2018-05-23 08:12:35 +03:00
|
|
|
|
|
|
|
bool nsMIMEInputStream::IsInputStreamLength() const {
|
|
|
|
nsCOMPtr<nsIInputStreamLength> stream = do_QueryInterface(mStream);
|
|
|
|
return !!stream;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool nsMIMEInputStream::IsAsyncInputStreamLength() const {
|
|
|
|
nsCOMPtr<nsIAsyncInputStreamLength> stream = do_QueryInterface(mStream);
|
|
|
|
return !!stream;
|
|
|
|
}
|
2018-10-03 23:51:56 +03:00
|
|
|
|
|
|
|
bool nsMIMEInputStream::IsCloneableInputStream() const {
|
|
|
|
nsCOMPtr<nsICloneableInputStream> stream = do_QueryInterface(mStream);
|
|
|
|
return !!stream;
|
|
|
|
}
|
|
|
|
|
|
|
|
// nsICloneableInputStream interface
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsMIMEInputStream::GetCloneable(bool* aCloneable) {
|
|
|
|
nsCOMPtr<nsICloneableInputStream> stream = do_QueryInterface(mStream);
|
|
|
|
if (!mStream) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
return stream->GetCloneable(aCloneable);
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsMIMEInputStream::Clone(nsIInputStream** aResult) {
|
|
|
|
nsCOMPtr<nsICloneableInputStream> stream = do_QueryInterface(mStream);
|
|
|
|
if (!mStream) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<nsIInputStream> clonedStream;
|
|
|
|
nsresult rv = stream->Clone(getter_AddRefs(clonedStream));
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<nsIMIMEInputStream> mimeStream = new nsMIMEInputStream();
|
|
|
|
|
|
|
|
rv = mimeStream->SetData(clonedStream);
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (const HeaderEntry& entry : mHeaders) {
|
|
|
|
rv = mimeStream->AddHeader(entry.name().get(), entry.value().get());
|
|
|
|
MOZ_ASSERT(NS_SUCCEEDED(rv));
|
|
|
|
}
|
|
|
|
|
|
|
|
static_cast<nsMIMEInputStream*>(mimeStream.get())->mStartedReading =
|
|
|
|
mStartedReading;
|
|
|
|
|
|
|
|
mimeStream.forget(aResult);
|
|
|
|
return NS_OK;
|
|
|
|
}
|