2005-11-12 21:17:19 +03:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim:set ts=2 sw=2 sts=2 et cin: */
|
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/. */
|
1999-06-11 05:37:24 +04:00
|
|
|
|
2010-04-20 20:32:28 +04:00
|
|
|
#include "nsIOService.h"
|
1999-06-11 05:37:24 +04:00
|
|
|
#include "nsFileChannel.h"
|
2005-11-12 21:17:19 +03:00
|
|
|
#include "nsBaseContentStream.h"
|
2003-01-18 05:15:14 +03:00
|
|
|
#include "nsDirectoryIndexStream.h"
|
2006-05-10 21:30:15 +04:00
|
|
|
#include "nsThreadUtils.h"
|
2005-11-12 21:17:19 +03:00
|
|
|
#include "nsTransportUtils.h"
|
|
|
|
#include "nsStreamUtils.h"
|
2003-01-18 05:15:14 +03:00
|
|
|
#include "nsMimeTypes.h"
|
|
|
|
#include "nsNetUtil.h"
|
2015-07-07 05:17:00 +03:00
|
|
|
#include "nsNetCID.h"
|
|
|
|
#include "nsIOutputStream.h"
|
|
|
|
#include "nsIFileStreams.h"
|
|
|
|
#include "nsFileProtocolHandler.h"
|
2006-05-11 02:49:04 +04:00
|
|
|
#include "nsProxyRelease.h"
|
2014-09-21 20:40:12 +04:00
|
|
|
#include "nsIContentPolicy.h"
|
|
|
|
#include "nsContentUtils.h"
|
2003-01-18 05:15:14 +03:00
|
|
|
|
2002-03-06 10:48:55 +03:00
|
|
|
#include "nsIFileURL.h"
|
2018-02-26 22:43:46 +03:00
|
|
|
#include "nsIURIMutator.h"
|
2015-07-07 05:17:00 +03:00
|
|
|
#include "nsIFile.h"
|
1999-07-03 04:59:51 +04:00
|
|
|
#include "nsIMIMEService.h"
|
2015-07-07 05:17:00 +03:00
|
|
|
#include "prio.h"
|
2013-01-15 16:22:03 +04:00
|
|
|
#include <algorithm>
|
1999-06-11 05:37:24 +04:00
|
|
|
|
2019-03-25 22:31:06 +03:00
|
|
|
#include "mozilla/TaskQueue.h"
|
2018-02-26 22:43:46 +03:00
|
|
|
#include "mozilla/Unused.h"
|
|
|
|
|
2015-01-08 22:48:52 +03:00
|
|
|
using namespace mozilla;
|
|
|
|
using namespace mozilla::net;
|
|
|
|
|
2003-01-18 05:15:14 +03:00
|
|
|
//-----------------------------------------------------------------------------
|
1999-06-11 05:37:24 +04:00
|
|
|
|
2016-04-26 03:23:21 +03:00
|
|
|
class nsFileCopyEvent : public Runnable {
|
2005-11-12 21:17:19 +03:00
|
|
|
public:
|
2017-06-12 22:34:10 +03:00
|
|
|
nsFileCopyEvent(nsIOutputStream* dest, nsIInputStream* source, int64_t len)
|
|
|
|
: mozilla::Runnable("nsFileCopyEvent"),
|
|
|
|
mDest(dest),
|
2005-11-12 21:17:19 +03:00
|
|
|
mSource(source),
|
|
|
|
mLen(len),
|
2003-01-18 05:15:14 +03:00
|
|
|
mStatus(NS_OK),
|
2017-06-12 22:34:10 +03:00
|
|
|
mInterruptStatus(NS_OK) {}
|
2005-11-12 21:17:19 +03:00
|
|
|
|
|
|
|
// Read the current status of the file copy operation.
|
|
|
|
nsresult Status() { return mStatus; }
|
2017-07-06 15:00:35 +03:00
|
|
|
|
2005-11-12 21:17:19 +03:00
|
|
|
// Call this method to perform the file copy synchronously.
|
|
|
|
void DoCopy();
|
|
|
|
|
|
|
|
// Call this method to perform the file copy on a background thread. The
|
|
|
|
// callback is dispatched when the file copy completes.
|
2006-05-11 02:49:04 +04:00
|
|
|
nsresult Dispatch(nsIRunnable* callback, nsITransportEventSink* sink,
|
2005-11-12 21:17:19 +03:00
|
|
|
nsIEventTarget* target);
|
|
|
|
|
|
|
|
// Call this method to interrupt a file copy operation that is occuring on
|
|
|
|
// a background thread. The status parameter passed to this function must
|
|
|
|
// be a failure code and is set as the status of this file copy operation.
|
|
|
|
void Interrupt(nsresult status) {
|
|
|
|
NS_ASSERTION(NS_FAILED(status), "must be a failure code");
|
|
|
|
mInterruptStatus = status;
|
|
|
|
}
|
|
|
|
|
2016-08-08 05:18:10 +03:00
|
|
|
NS_IMETHOD Run() override {
|
2006-05-10 21:30:15 +04:00
|
|
|
DoCopy();
|
|
|
|
return NS_OK;
|
2005-11-12 21:17:19 +03:00
|
|
|
}
|
|
|
|
|
2006-05-10 21:30:15 +04:00
|
|
|
private:
|
2006-05-11 02:49:04 +04:00
|
|
|
nsCOMPtr<nsIEventTarget> mCallbackTarget;
|
|
|
|
nsCOMPtr<nsIRunnable> mCallback;
|
2005-11-12 21:17:19 +03:00
|
|
|
nsCOMPtr<nsITransportEventSink> mSink;
|
|
|
|
nsCOMPtr<nsIOutputStream> mDest;
|
|
|
|
nsCOMPtr<nsIInputStream> mSource;
|
2012-08-22 19:56:38 +04:00
|
|
|
int64_t mLen;
|
2005-11-12 21:17:19 +03:00
|
|
|
nsresult mStatus; // modified on i/o thread only
|
|
|
|
nsresult mInterruptStatus; // modified on main thread only
|
|
|
|
};
|
|
|
|
|
|
|
|
void nsFileCopyEvent::DoCopy() {
|
|
|
|
// We'll copy in chunks this large by default. This size affects how
|
|
|
|
// frequently we'll check for interrupts.
|
2012-08-22 19:56:38 +04:00
|
|
|
const int32_t chunk =
|
|
|
|
nsIOService::gDefaultSegmentSize * nsIOService::gDefaultSegmentCount;
|
2005-11-12 21:17:19 +03:00
|
|
|
|
|
|
|
nsresult rv = NS_OK;
|
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
int64_t len = mLen, progress = 0;
|
2005-11-12 21:17:19 +03:00
|
|
|
while (len) {
|
|
|
|
// If we've been interrupted, then stop copying.
|
|
|
|
rv = mInterruptStatus;
|
2005-04-05 21:40:32 +04:00
|
|
|
if (NS_FAILED(rv)) break;
|
2002-05-01 00:48:15 +04:00
|
|
|
|
2013-01-15 16:22:03 +04:00
|
|
|
int32_t num = std::min((int32_t)len, chunk);
|
2002-05-01 00:48:15 +04:00
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t result;
|
2005-11-12 21:17:19 +03:00
|
|
|
rv = mSource->ReadSegments(NS_CopySegmentToStream, mDest, num, &result);
|
|
|
|
if (NS_FAILED(rv)) break;
|
2012-08-22 19:56:38 +04:00
|
|
|
if (result != (uint32_t)num) {
|
2005-11-12 21:17:19 +03:00
|
|
|
rv = NS_ERROR_FILE_DISK_FULL; // stopped prematurely (out of disk space)
|
|
|
|
break;
|
2003-01-18 05:15:14 +03:00
|
|
|
}
|
2002-05-01 00:48:15 +04:00
|
|
|
|
2005-11-12 21:17:19 +03:00
|
|
|
// Dispatch progress notification
|
|
|
|
if (mSink) {
|
|
|
|
progress += num;
|
2012-08-01 12:17:09 +04:00
|
|
|
mSink->OnTransportStatus(nullptr, NS_NET_STATUS_WRITING, progress, mLen);
|
2005-11-12 21:17:19 +03:00
|
|
|
}
|
2017-07-06 15:00:35 +03:00
|
|
|
|
2005-11-12 21:17:19 +03:00
|
|
|
len -= num;
|
|
|
|
}
|
2003-02-22 22:09:04 +03:00
|
|
|
|
2005-11-12 21:17:19 +03:00
|
|
|
if (NS_FAILED(rv)) mStatus = rv;
|
2003-01-18 05:15:14 +03:00
|
|
|
|
2005-11-12 21:17:19 +03:00
|
|
|
// Close the output stream before notifying our callback so that others may
|
|
|
|
// freely "play" with the file.
|
|
|
|
mDest->Close();
|
2003-01-18 05:15:14 +03:00
|
|
|
|
2005-11-12 21:17:19 +03:00
|
|
|
// Notify completion
|
2006-05-10 21:30:15 +04:00
|
|
|
if (mCallback) {
|
2006-05-11 02:49:04 +04:00
|
|
|
mCallbackTarget->Dispatch(mCallback, NS_DISPATCH_NORMAL);
|
|
|
|
|
|
|
|
// Release the callback on the target thread to avoid destroying stuff on
|
|
|
|
// the wrong thread.
|
2017-06-14 04:27:17 +03:00
|
|
|
NS_ProxyRelease("nsFileCopyEvent::mCallback", mCallbackTarget,
|
|
|
|
mCallback.forget());
|
2006-05-10 21:30:15 +04:00
|
|
|
}
|
1999-06-11 05:37:24 +04:00
|
|
|
}
|
|
|
|
|
2006-05-11 02:49:04 +04:00
|
|
|
nsresult nsFileCopyEvent::Dispatch(nsIRunnable* callback,
|
2005-11-12 21:17:19 +03:00
|
|
|
nsITransportEventSink* sink,
|
|
|
|
nsIEventTarget* target) {
|
|
|
|
// Use the supplied event target for all asynchronous operations.
|
1999-06-12 06:53:21 +04:00
|
|
|
|
2006-05-11 02:49:04 +04:00
|
|
|
mCallback = callback;
|
|
|
|
mCallbackTarget = target;
|
1999-06-12 06:53:21 +04:00
|
|
|
|
2005-11-12 21:17:19 +03:00
|
|
|
// Build a coalescing proxy for progress events
|
2015-09-16 01:12:37 +03:00
|
|
|
nsresult rv =
|
|
|
|
net_NewTransportEventSinkProxy(getter_AddRefs(mSink), sink, target);
|
|
|
|
|
2005-11-12 21:17:19 +03:00
|
|
|
if (NS_FAILED(rv)) return rv;
|
1999-06-12 06:53:21 +04:00
|
|
|
|
2006-05-10 21:30:15 +04:00
|
|
|
// Dispatch ourselves to I/O thread pool...
|
2005-11-12 21:17:19 +03:00
|
|
|
nsCOMPtr<nsIEventTarget> pool =
|
2006-05-10 21:30:15 +04:00
|
|
|
do_GetService(NS_STREAMTRANSPORTSERVICE_CONTRACTID, &rv);
|
2005-11-12 21:17:19 +03:00
|
|
|
if (NS_FAILED(rv)) return rv;
|
1999-10-26 13:16:24 +04:00
|
|
|
|
2006-05-10 21:30:15 +04:00
|
|
|
return pool->Dispatch(this, NS_DISPATCH_NORMAL);
|
2000-03-29 07:58:50 +04:00
|
|
|
}
|
|
|
|
|
2005-11-12 21:17:19 +03:00
|
|
|
//-----------------------------------------------------------------------------
|
1999-06-11 05:37:24 +04:00
|
|
|
|
2005-11-12 21:17:19 +03:00
|
|
|
// This is a dummy input stream that when read, performs the file copy. The
|
|
|
|
// copy happens on a background thread via mCopyEvent.
|
1999-11-17 11:14:52 +03:00
|
|
|
|
2006-05-11 02:49:04 +04:00
|
|
|
class nsFileUploadContentStream : public nsBaseContentStream {
|
2005-11-12 21:17:19 +03:00
|
|
|
public:
|
2018-02-12 23:44:40 +03:00
|
|
|
NS_INLINE_DECL_REFCOUNTING_INHERITED(nsFileUploadContentStream,
|
|
|
|
nsBaseContentStream)
|
2000-03-29 07:58:50 +04:00
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
nsFileUploadContentStream(bool nonBlocking, nsIOutputStream* dest,
|
2005-11-12 21:17:19 +03:00
|
|
|
nsIInputStream* source, int64_t len,
|
|
|
|
nsITransportEventSink* sink)
|
|
|
|
: nsBaseContentStream(nonBlocking),
|
2006-05-10 21:30:15 +04:00
|
|
|
mCopyEvent(new nsFileCopyEvent(dest, source, len)),
|
2005-11-12 21:17:19 +03:00
|
|
|
mSink(sink) {}
|
1999-06-15 09:18:40 +04:00
|
|
|
|
2012-07-30 18:20:58 +04:00
|
|
|
bool IsInitialized() { return mCopyEvent != nullptr; }
|
2006-05-10 21:30:15 +04:00
|
|
|
|
2016-08-08 03:54:47 +03:00
|
|
|
NS_IMETHOD ReadSegments(nsWriteSegmentFun fun, void* closure, uint32_t count,
|
|
|
|
uint32_t* result) override;
|
|
|
|
NS_IMETHOD AsyncWait(nsIInputStreamCallback* callback, uint32_t flags,
|
|
|
|
uint32_t count, nsIEventTarget* target) override;
|
1999-06-11 05:37:24 +04:00
|
|
|
|
2005-11-12 21:17:19 +03:00
|
|
|
private:
|
2018-04-30 19:46:04 +03:00
|
|
|
virtual ~nsFileUploadContentStream() = default;
|
2014-07-09 22:02:06 +04:00
|
|
|
|
2006-05-11 02:49:04 +04:00
|
|
|
void OnCopyComplete();
|
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<nsFileCopyEvent> mCopyEvent;
|
2005-11-12 21:17:19 +03:00
|
|
|
nsCOMPtr<nsITransportEventSink> mSink;
|
|
|
|
};
|
1999-06-22 04:19:58 +04:00
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2005-11-12 21:17:19 +03:00
|
|
|
nsFileUploadContentStream::ReadSegments(nsWriteSegmentFun fun, void* closure,
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t count, uint32_t* result) {
|
2005-11-12 21:17:19 +03:00
|
|
|
*result = 0; // nothing is ever actually read from this stream
|
1999-07-03 04:59:51 +04:00
|
|
|
|
2005-11-12 21:17:19 +03:00
|
|
|
if (IsClosed()) return NS_OK;
|
2002-03-21 01:50:33 +03:00
|
|
|
|
2005-11-12 21:17:19 +03:00
|
|
|
if (IsNonBlocking()) {
|
|
|
|
// Inform the caller that they will have to wait for the copy operation to
|
|
|
|
// complete asynchronously. We'll kick of the copy operation once they
|
|
|
|
// call AsyncWait.
|
|
|
|
return NS_BASE_STREAM_WOULD_BLOCK;
|
|
|
|
}
|
1999-06-22 04:19:58 +04:00
|
|
|
|
2005-11-12 21:17:19 +03:00
|
|
|
// Perform copy synchronously, and then close out the stream.
|
2006-05-10 21:30:15 +04:00
|
|
|
mCopyEvent->DoCopy();
|
|
|
|
nsresult status = mCopyEvent->Status();
|
2005-11-12 21:17:19 +03:00
|
|
|
CloseWithStatus(NS_FAILED(status) ? status : NS_BASE_STREAM_CLOSED);
|
|
|
|
return status;
|
2000-01-08 09:26:04 +03:00
|
|
|
}
|
|
|
|
|
1999-09-09 09:07:30 +04:00
|
|
|
NS_IMETHODIMP
|
2005-11-12 21:17:19 +03:00
|
|
|
nsFileUploadContentStream::AsyncWait(nsIInputStreamCallback* callback,
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t flags, uint32_t count,
|
2005-11-12 21:17:19 +03:00
|
|
|
nsIEventTarget* target) {
|
|
|
|
nsresult rv = nsBaseContentStream::AsyncWait(callback, flags, count, target);
|
|
|
|
if (NS_FAILED(rv) || IsClosed()) return rv;
|
1999-09-09 09:07:30 +04:00
|
|
|
|
2006-05-11 02:49:04 +04:00
|
|
|
if (IsNonBlocking()) {
|
|
|
|
nsCOMPtr<nsIRunnable> callback =
|
2017-06-12 22:34:10 +03:00
|
|
|
NewRunnableMethod("nsFileUploadContentStream::OnCopyComplete", this,
|
|
|
|
&nsFileUploadContentStream::OnCopyComplete);
|
2006-05-11 02:49:04 +04:00
|
|
|
mCopyEvent->Dispatch(callback, mSink, target);
|
|
|
|
}
|
2003-06-24 01:00:46 +04:00
|
|
|
|
2005-11-12 21:17:19 +03:00
|
|
|
return NS_OK;
|
2000-03-29 07:58:50 +04:00
|
|
|
}
|
|
|
|
|
2006-05-11 02:49:04 +04:00
|
|
|
void nsFileUploadContentStream::OnCopyComplete() {
|
2005-11-12 21:17:19 +03:00
|
|
|
// This method is being called to indicate that we are done copying.
|
2006-05-10 21:30:15 +04:00
|
|
|
nsresult status = mCopyEvent->Status();
|
1999-07-31 10:53:12 +04:00
|
|
|
|
2005-11-12 21:17:19 +03:00
|
|
|
CloseWithStatus(NS_FAILED(status) ? status : NS_BASE_STREAM_CLOSED);
|
1999-11-17 11:14:52 +03:00
|
|
|
}
|
|
|
|
|
2005-11-12 21:17:19 +03:00
|
|
|
//-----------------------------------------------------------------------------
|
1999-08-30 01:58:42 +04:00
|
|
|
|
2018-04-13 16:01:28 +03:00
|
|
|
nsFileChannel::nsFileChannel(nsIURI* uri) : mUploadLength(0), mFileURI(uri) {}
|
2017-05-30 19:07:59 +03:00
|
|
|
|
|
|
|
nsresult nsFileChannel::Init() {
|
|
|
|
NS_ENSURE_STATE(mLoadInfo);
|
|
|
|
|
2012-05-03 23:23:28 +04:00
|
|
|
// If we have a link file, we should resolve its target right away.
|
|
|
|
// This is to protect against a same origin attack where the same link file
|
|
|
|
// can point to different resources right after the first resource is loaded.
|
|
|
|
nsCOMPtr<nsIFile> file;
|
|
|
|
nsCOMPtr<nsIURI> targetURI;
|
2017-01-21 09:37:27 +03:00
|
|
|
#ifdef XP_WIN
|
|
|
|
nsAutoString fileTarget;
|
|
|
|
#else
|
2012-09-02 06:35:17 +04:00
|
|
|
nsAutoCString fileTarget;
|
2017-01-21 09:37:27 +03:00
|
|
|
#endif
|
2012-06-06 06:08:30 +04:00
|
|
|
nsCOMPtr<nsIFile> resolvedFile;
|
2012-05-03 23:23:28 +04:00
|
|
|
bool symLink;
|
2017-05-30 19:07:59 +03:00
|
|
|
nsCOMPtr<nsIFileURL> fileURL = do_QueryInterface(mFileURI);
|
2012-05-03 23:23:28 +04:00
|
|
|
if (fileURL && NS_SUCCEEDED(fileURL->GetFile(getter_AddRefs(file))) &&
|
2017-05-30 19:07:59 +03:00
|
|
|
NS_SUCCEEDED(file->IsSymlink(&symLink)) && symLink &&
|
2017-01-21 09:37:27 +03:00
|
|
|
#ifdef XP_WIN
|
|
|
|
NS_SUCCEEDED(file->GetTarget(fileTarget)) &&
|
2017-05-30 19:07:59 +03:00
|
|
|
NS_SUCCEEDED(
|
|
|
|
NS_NewLocalFile(fileTarget, true, getter_AddRefs(resolvedFile))) &&
|
2017-01-21 09:37:27 +03:00
|
|
|
#else
|
2012-05-03 23:23:28 +04:00
|
|
|
NS_SUCCEEDED(file->GetNativeTarget(fileTarget)) &&
|
2017-05-30 19:07:59 +03:00
|
|
|
NS_SUCCEEDED(NS_NewNativeLocalFile(fileTarget, true,
|
2012-05-03 23:23:28 +04:00
|
|
|
getter_AddRefs(resolvedFile))) &&
|
2017-01-21 09:37:27 +03:00
|
|
|
#endif
|
2017-05-30 19:07:59 +03:00
|
|
|
NS_SUCCEEDED(
|
|
|
|
NS_NewFileURI(getter_AddRefs(targetURI), resolvedFile, nullptr))) {
|
2015-10-30 00:13:23 +03:00
|
|
|
// Make an effort to match up the query strings.
|
2017-05-30 19:07:59 +03:00
|
|
|
nsCOMPtr<nsIURL> origURL = do_QueryInterface(mFileURI);
|
2015-10-30 00:13:23 +03:00
|
|
|
nsCOMPtr<nsIURL> targetURL = do_QueryInterface(targetURI);
|
|
|
|
nsAutoCString queryString;
|
|
|
|
if (origURL && targetURL && NS_SUCCEEDED(origURL->GetQuery(queryString))) {
|
2018-02-26 22:43:46 +03:00
|
|
|
Unused
|
|
|
|
<< NS_MutateURI(targetURI).SetQuery(queryString).Finalize(targetURI);
|
2015-10-30 00:13:23 +03:00
|
|
|
}
|
|
|
|
|
2012-05-03 23:23:28 +04:00
|
|
|
SetURI(targetURI);
|
2017-05-30 19:07:59 +03:00
|
|
|
SetOriginalURI(mFileURI);
|
|
|
|
mLoadInfo->SetResultPrincipalURI(targetURI);
|
2012-05-03 23:23:28 +04:00
|
|
|
} else {
|
2017-05-30 19:07:59 +03:00
|
|
|
SetURI(mFileURI);
|
2012-05-03 23:23:28 +04:00
|
|
|
}
|
2017-05-30 19:07:59 +03:00
|
|
|
|
|
|
|
return NS_OK;
|
2012-05-03 23:23:28 +04:00
|
|
|
}
|
|
|
|
|
2006-02-14 21:46:12 +03:00
|
|
|
nsresult nsFileChannel::MakeFileInputStream(nsIFile* file,
|
|
|
|
nsCOMPtr<nsIInputStream>& stream,
|
2012-11-02 03:23:13 +04:00
|
|
|
nsCString& contentType,
|
|
|
|
bool async) {
|
2005-11-12 21:17:19 +03:00
|
|
|
// we accept that this might result in a disk hit to stat the file
|
2011-09-29 10:19:26 +04:00
|
|
|
bool isDir;
|
2005-11-12 21:17:19 +03:00
|
|
|
nsresult rv = file->IsDirectory(&isDir);
|
|
|
|
if (NS_FAILED(rv)) {
|
2012-05-10 04:02:40 +04:00
|
|
|
// canonicalize error message
|
|
|
|
if (rv == NS_ERROR_FILE_TARGET_DOES_NOT_EXIST) rv = NS_ERROR_FILE_NOT_FOUND;
|
2012-11-02 03:23:13 +04:00
|
|
|
|
|
|
|
if (async && (NS_ERROR_FILE_NOT_FOUND == rv)) {
|
|
|
|
// We don't return "Not Found" errors here. Since we could not find
|
|
|
|
// the file, it's not a directory anyway.
|
|
|
|
isDir = false;
|
|
|
|
} else {
|
|
|
|
return rv;
|
|
|
|
}
|
2005-11-12 21:17:19 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if (isDir) {
|
|
|
|
rv = nsDirectoryIndexStream::Create(file, getter_AddRefs(stream));
|
2006-02-14 21:46:12 +03:00
|
|
|
if (NS_SUCCEEDED(rv) && !HasContentTypeHint())
|
2005-11-12 21:17:19 +03:00
|
|
|
contentType.AssignLiteral(APPLICATION_HTTP_INDEX_FORMAT);
|
|
|
|
} else {
|
2012-11-02 03:23:13 +04:00
|
|
|
rv = NS_NewLocalFileInputStream(getter_AddRefs(stream), file, -1, -1,
|
|
|
|
async ? nsIFileInputStream::DEFER_OPEN : 0);
|
2006-02-14 21:46:12 +03:00
|
|
|
if (NS_SUCCEEDED(rv) && !HasContentTypeHint()) {
|
2005-11-12 21:17:19 +03:00
|
|
|
// Use file extension to infer content type
|
|
|
|
nsCOMPtr<nsIMIMEService> mime = do_GetService("@mozilla.org/mime;1", &rv);
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
|
|
mime->GetTypeFromFile(file, contentType);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return rv;
|
1999-08-30 01:58:42 +04:00
|
|
|
}
|
1999-07-31 10:53:12 +04:00
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
nsresult nsFileChannel::OpenContentStream(bool async, nsIInputStream** result,
|
2008-09-30 01:02:44 +04:00
|
|
|
nsIChannel** channel) {
|
2005-11-12 21:17:19 +03:00
|
|
|
// NOTE: the resulting file is a clone, so it is safe to pass it to the
|
|
|
|
// file input stream which will be read on a background thread.
|
|
|
|
nsCOMPtr<nsIFile> file;
|
|
|
|
nsresult rv = GetFile(getter_AddRefs(file));
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
1999-11-17 11:14:52 +03:00
|
|
|
|
2008-09-30 01:02:44 +04:00
|
|
|
nsCOMPtr<nsIFileProtocolHandler> fileHandler;
|
|
|
|
rv = NS_GetFileProtocolHandler(getter_AddRefs(fileHandler));
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
2017-07-06 15:00:35 +03:00
|
|
|
|
2008-09-30 01:02:44 +04:00
|
|
|
nsCOMPtr<nsIURI> newURI;
|
2020-09-22 10:26:33 +03:00
|
|
|
if (NS_SUCCEEDED(fileHandler->ReadURLFile(file, getter_AddRefs(newURI))) ||
|
|
|
|
NS_SUCCEEDED(fileHandler->ReadShellLink(file, getter_AddRefs(newURI)))) {
|
2008-09-30 01:02:44 +04:00
|
|
|
nsCOMPtr<nsIChannel> newChannel;
|
2014-09-21 20:40:12 +04:00
|
|
|
rv = NS_NewChannel(getter_AddRefs(newChannel), newURI,
|
|
|
|
nsContentUtils::GetSystemPrincipal(),
|
2020-07-15 14:20:45 +03:00
|
|
|
nsILoadInfo::SEC_ALLOW_CROSS_ORIGIN_SEC_CONTEXT_IS_NULL,
|
2014-09-21 20:40:12 +04:00
|
|
|
nsIContentPolicy::TYPE_OTHER);
|
|
|
|
|
2008-09-30 01:02:44 +04:00
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
2012-07-30 18:20:58 +04:00
|
|
|
*result = nullptr;
|
2008-09-30 01:02:44 +04:00
|
|
|
newChannel.forget(channel);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2005-11-12 21:17:19 +03:00
|
|
|
nsCOMPtr<nsIInputStream> stream;
|
2000-06-02 09:19:16 +04:00
|
|
|
|
2005-11-12 21:17:19 +03:00
|
|
|
if (mUploadStream) {
|
|
|
|
// Pass back a nsFileUploadContentStream instance that knows how to perform
|
|
|
|
// the file copy when "read" (the resulting stream in this case does not
|
|
|
|
// actually return any data).
|
2000-06-02 09:19:16 +04:00
|
|
|
|
2005-11-12 21:17:19 +03:00
|
|
|
nsCOMPtr<nsIOutputStream> fileStream;
|
|
|
|
rv = NS_NewLocalFileOutputStream(getter_AddRefs(fileStream), file,
|
|
|
|
PR_WRONLY | PR_CREATE_FILE | PR_TRUNCATE,
|
|
|
|
PR_IRUSR | PR_IWUSR);
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<nsFileUploadContentStream> uploadStream =
|
2006-05-10 21:30:15 +04:00
|
|
|
new nsFileUploadContentStream(async, fileStream, mUploadStream,
|
|
|
|
mUploadLength, this);
|
2006-10-25 20:00:02 +04:00
|
|
|
if (!uploadStream || !uploadStream->IsInitialized()) {
|
2005-11-12 21:17:19 +03:00
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
2006-10-25 20:00:02 +04:00
|
|
|
}
|
2020-02-13 17:38:48 +03:00
|
|
|
stream = std::move(uploadStream);
|
2005-11-12 21:17:19 +03:00
|
|
|
|
2012-10-22 21:51:07 +04:00
|
|
|
mContentLength = 0;
|
2005-11-12 21:17:19 +03:00
|
|
|
|
|
|
|
// Since there isn't any content to speak of we just set the content-type
|
|
|
|
// to something other than "unknown" to avoid triggering the content-type
|
|
|
|
// sniffer code in nsBaseChannel.
|
2006-02-14 21:46:12 +03:00
|
|
|
// However, don't override explicitly set types.
|
|
|
|
if (!HasContentTypeHint())
|
|
|
|
SetContentType(nsLiteralCString(APPLICATION_OCTET_STREAM));
|
2005-11-12 21:17:19 +03:00
|
|
|
} else {
|
2012-09-02 06:35:17 +04:00
|
|
|
nsAutoCString contentType;
|
2012-11-02 03:23:13 +04:00
|
|
|
rv = MakeFileInputStream(file, stream, contentType, async);
|
2005-11-12 21:17:19 +03:00
|
|
|
if (NS_FAILED(rv)) return rv;
|
2003-01-18 05:15:14 +03:00
|
|
|
|
2011-10-17 18:59:28 +04:00
|
|
|
EnableSynthesizedProgressEvents(true);
|
1999-08-27 02:45:55 +04:00
|
|
|
|
2005-11-12 21:17:19 +03:00
|
|
|
// fixup content length and type
|
2019-03-25 22:31:06 +03:00
|
|
|
|
|
|
|
// when we are called from asyncOpen, the content length fixup will be
|
|
|
|
// performed on a background thread and block the listener invocation via
|
|
|
|
// ListenerBlockingPromise method
|
|
|
|
if (!async && mContentLength < 0) {
|
|
|
|
rv = FixupContentLength(false);
|
2012-11-02 03:23:13 +04:00
|
|
|
if (NS_FAILED(rv)) {
|
2019-03-25 22:31:06 +03:00
|
|
|
return rv;
|
2012-11-02 03:23:13 +04:00
|
|
|
}
|
2000-06-02 09:19:16 +04:00
|
|
|
}
|
2019-03-25 22:31:06 +03:00
|
|
|
|
|
|
|
if (!contentType.IsEmpty()) {
|
|
|
|
SetContentType(contentType);
|
|
|
|
}
|
2005-11-12 21:17:19 +03:00
|
|
|
}
|
2000-06-02 09:19:16 +04:00
|
|
|
|
2012-07-30 18:20:58 +04:00
|
|
|
*result = nullptr;
|
2005-11-12 21:17:19 +03:00
|
|
|
stream.swap(*result);
|
|
|
|
return NS_OK;
|
2000-06-02 09:19:16 +04:00
|
|
|
}
|
|
|
|
|
2019-03-25 22:31:06 +03:00
|
|
|
nsresult nsFileChannel::ListenerBlockingPromise(BlockingPromise** aPromise) {
|
|
|
|
NS_ENSURE_ARG(aPromise);
|
|
|
|
*aPromise = nullptr;
|
|
|
|
|
|
|
|
if (mContentLength >= 0) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<nsIEventTarget> sts(
|
|
|
|
do_GetService(NS_STREAMTRANSPORTSERVICE_CONTRACTID));
|
|
|
|
if (!sts) {
|
|
|
|
return FixupContentLength(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
RefPtr<TaskQueue> taskQueue = new TaskQueue(sts.forget());
|
|
|
|
RefPtr<nsFileChannel> self = this;
|
|
|
|
RefPtr<BlockingPromise> promise =
|
|
|
|
mozilla::InvokeAsync(taskQueue, __func__, [self{std::move(self)}]() {
|
|
|
|
nsresult rv = self->FixupContentLength(true);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return BlockingPromise::CreateAndReject(rv, __func__);
|
|
|
|
}
|
|
|
|
return BlockingPromise::CreateAndResolve(NS_OK, __func__);
|
|
|
|
});
|
|
|
|
|
|
|
|
promise.forget(aPromise);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult nsFileChannel::FixupContentLength(bool async) {
|
|
|
|
MOZ_ASSERT(mContentLength < 0);
|
|
|
|
|
|
|
|
nsCOMPtr<nsIFile> file;
|
|
|
|
nsresult rv = GetFile(getter_AddRefs(file));
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
int64_t size;
|
|
|
|
rv = file->GetFileSize(&size);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
if (async && (NS_ERROR_FILE_NOT_FOUND == rv ||
|
|
|
|
NS_ERROR_FILE_TARGET_DOES_NOT_EXIST == rv)) {
|
|
|
|
size = 0;
|
|
|
|
} else {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
mContentLength = size;
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2003-01-18 05:15:14 +03:00
|
|
|
//-----------------------------------------------------------------------------
|
2005-11-12 21:17:19 +03:00
|
|
|
// nsFileChannel::nsISupports
|
|
|
|
|
2014-04-27 11:06:00 +04:00
|
|
|
NS_IMPL_ISUPPORTS_INHERITED(nsFileChannel, nsBaseChannel, nsIUploadChannel,
|
|
|
|
nsIFileChannel)
|
2005-11-12 21:17:19 +03:00
|
|
|
|
2003-01-18 05:15:14 +03:00
|
|
|
//-----------------------------------------------------------------------------
|
2005-11-12 21:17:19 +03:00
|
|
|
// nsFileChannel::nsIFileChannel
|
2003-01-18 05:15:14 +03:00
|
|
|
|
2000-06-02 09:19:16 +04:00
|
|
|
NS_IMETHODIMP
|
2003-01-18 05:15:14 +03:00
|
|
|
nsFileChannel::GetFile(nsIFile** file) {
|
2005-11-12 21:17:19 +03:00
|
|
|
nsCOMPtr<nsIFileURL> fileURL = do_QueryInterface(URI());
|
|
|
|
NS_ENSURE_STATE(fileURL);
|
|
|
|
|
|
|
|
// This returns a cloned nsIFile
|
|
|
|
return fileURL->GetFile(file);
|
2000-06-02 09:19:16 +04:00
|
|
|
}
|
|
|
|
|
2003-01-18 05:15:14 +03:00
|
|
|
//-----------------------------------------------------------------------------
|
2005-11-12 21:17:19 +03:00
|
|
|
// nsFileChannel::nsIUploadChannel
|
1999-06-11 05:37:24 +04:00
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2003-01-18 05:15:14 +03:00
|
|
|
nsFileChannel::SetUploadStream(nsIInputStream* stream,
|
|
|
|
const nsACString& contentType,
|
2012-09-19 12:09:08 +04:00
|
|
|
int64_t contentLength) {
|
2014-03-11 02:04:28 +04:00
|
|
|
NS_ENSURE_TRUE(!Pending(), NS_ERROR_IN_PROGRESS);
|
1999-06-12 11:14:19 +04:00
|
|
|
|
2005-11-12 21:17:19 +03:00
|
|
|
if ((mUploadStream = stream)) {
|
|
|
|
mUploadLength = contentLength;
|
|
|
|
if (mUploadLength < 0) {
|
|
|
|
// Make sure we know how much data we are uploading.
|
2012-08-22 19:56:38 +04:00
|
|
|
uint64_t avail;
|
2005-11-12 21:17:19 +03:00
|
|
|
nsresult rv = mUploadStream->Available(&avail);
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
2015-01-08 22:48:52 +03:00
|
|
|
// if this doesn't fit in the javascript MAX_SAFE_INTEGER
|
|
|
|
// pretend we don't know the size
|
|
|
|
mUploadLength = InScriptableRange(avail) ? avail : -1;
|
2003-01-18 05:15:14 +03:00
|
|
|
}
|
2005-11-12 21:17:19 +03:00
|
|
|
} else {
|
|
|
|
mUploadLength = -1;
|
|
|
|
}
|
|
|
|
return NS_OK;
|
1999-11-12 09:13:13 +03:00
|
|
|
}
|
|
|
|
|
2000-03-29 07:58:50 +04:00
|
|
|
NS_IMETHODIMP
|
2005-11-12 21:17:19 +03:00
|
|
|
nsFileChannel::GetUploadStream(nsIInputStream** result) {
|
|
|
|
NS_IF_ADDREF(*result = mUploadStream);
|
2000-03-29 07:58:50 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|