2005-11-12 18:17:19 +00: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 11:12:37 +00: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 01:37:24 +00:00
|
|
|
|
2010-04-20 16:32:28 +00:00
|
|
|
#include "nsIOService.h"
|
1999-06-11 01:37:24 +00:00
|
|
|
#include "nsFileChannel.h"
|
2005-11-12 18:17:19 +00:00
|
|
|
#include "nsBaseContentStream.h"
|
2003-01-18 02:15:14 +00:00
|
|
|
#include "nsDirectoryIndexStream.h"
|
2006-05-10 17:30:15 +00:00
|
|
|
#include "nsThreadUtils.h"
|
2005-11-12 18:17:19 +00:00
|
|
|
#include "nsTransportUtils.h"
|
|
|
|
#include "nsStreamUtils.h"
|
|
|
|
#include "nsURLHelper.h"
|
2003-01-18 02:15:14 +00:00
|
|
|
#include "nsMimeTypes.h"
|
|
|
|
#include "nsNetUtil.h"
|
2006-05-10 22:49:04 +00:00
|
|
|
#include "nsProxyRelease.h"
|
2005-11-12 18:17:19 +00:00
|
|
|
#include "nsAutoPtr.h"
|
2006-10-25 16:00:02 +00:00
|
|
|
#include "nsStandardURL.h"
|
2003-01-18 02:15:14 +00:00
|
|
|
|
2002-03-06 07:48:55 +00:00
|
|
|
#include "nsIFileURL.h"
|
1999-07-03 00:59:51 +00:00
|
|
|
#include "nsIMIMEService.h"
|
1999-06-11 01:37:24 +00:00
|
|
|
|
2003-01-18 02:15:14 +00:00
|
|
|
//-----------------------------------------------------------------------------
|
1999-06-11 01:37:24 +00:00
|
|
|
|
2006-05-10 17:30:15 +00:00
|
|
|
class nsFileCopyEvent : public nsRunnable {
|
2005-11-12 18:17:19 +00:00
|
|
|
public:
|
2012-08-22 15:56:38 +00:00
|
|
|
nsFileCopyEvent(nsIOutputStream *dest, nsIInputStream *source, int64_t len)
|
2005-11-12 18:17:19 +00:00
|
|
|
: mDest(dest)
|
|
|
|
, mSource(source)
|
|
|
|
, mLen(len)
|
2003-01-18 02:15:14 +00:00
|
|
|
, mStatus(NS_OK)
|
2005-11-12 18:17:19 +00:00
|
|
|
, mInterruptStatus(NS_OK) {
|
|
|
|
}
|
|
|
|
|
|
|
|
// Read the current status of the file copy operation.
|
|
|
|
nsresult Status() { return mStatus; }
|
|
|
|
|
|
|
|
// 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-10 22:49:04 +00:00
|
|
|
nsresult Dispatch(nsIRunnable *callback,
|
2005-11-12 18:17:19 +00:00
|
|
|
nsITransportEventSink *sink,
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2006-05-10 17:30:15 +00:00
|
|
|
NS_IMETHOD Run() {
|
|
|
|
DoCopy();
|
|
|
|
return NS_OK;
|
2005-11-12 18:17:19 +00:00
|
|
|
}
|
|
|
|
|
2006-05-10 17:30:15 +00:00
|
|
|
private:
|
2006-05-10 22:49:04 +00:00
|
|
|
nsCOMPtr<nsIEventTarget> mCallbackTarget;
|
|
|
|
nsCOMPtr<nsIRunnable> mCallback;
|
2005-11-12 18:17:19 +00:00
|
|
|
nsCOMPtr<nsITransportEventSink> mSink;
|
|
|
|
nsCOMPtr<nsIOutputStream> mDest;
|
|
|
|
nsCOMPtr<nsIInputStream> mSource;
|
2012-08-22 15:56:38 +00:00
|
|
|
int64_t mLen;
|
2005-11-12 18:17:19 +00: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 15:56:38 +00:00
|
|
|
const int32_t chunk = nsIOService::gDefaultSegmentSize * nsIOService::gDefaultSegmentCount;
|
2005-11-12 18:17:19 +00:00
|
|
|
|
|
|
|
nsresult rv = NS_OK;
|
|
|
|
|
2012-08-22 15:56:38 +00:00
|
|
|
int64_t len = mLen, progress = 0;
|
2005-11-12 18:17:19 +00:00
|
|
|
while (len) {
|
|
|
|
// If we've been interrupted, then stop copying.
|
|
|
|
rv = mInterruptStatus;
|
2005-04-05 17:40:32 +00:00
|
|
|
if (NS_FAILED(rv))
|
2005-11-12 18:17:19 +00:00
|
|
|
break;
|
2002-04-30 20:48:15 +00:00
|
|
|
|
2012-08-22 15:56:38 +00:00
|
|
|
int32_t num = NS_MIN((int32_t) len, chunk);
|
2002-04-30 20:48:15 +00:00
|
|
|
|
2012-08-22 15:56:38 +00:00
|
|
|
uint32_t result;
|
2005-11-12 18:17:19 +00:00
|
|
|
rv = mSource->ReadSegments(NS_CopySegmentToStream, mDest, num, &result);
|
|
|
|
if (NS_FAILED(rv))
|
|
|
|
break;
|
2012-08-22 15:56:38 +00:00
|
|
|
if (result != (uint32_t) num) {
|
2005-11-12 18:17:19 +00:00
|
|
|
rv = NS_ERROR_FILE_DISK_FULL; // stopped prematurely (out of disk space)
|
|
|
|
break;
|
2003-01-18 02:15:14 +00:00
|
|
|
}
|
2002-04-30 20:48:15 +00:00
|
|
|
|
2005-11-12 18:17:19 +00:00
|
|
|
// Dispatch progress notification
|
|
|
|
if (mSink) {
|
|
|
|
progress += num;
|
2012-08-01 08:17:09 +00:00
|
|
|
mSink->OnTransportStatus(nullptr, NS_NET_STATUS_WRITING, progress,
|
2005-11-12 18:17:19 +00:00
|
|
|
mLen);
|
|
|
|
}
|
|
|
|
|
|
|
|
len -= num;
|
|
|
|
}
|
2003-02-22 19:09:04 +00:00
|
|
|
|
2005-11-12 18:17:19 +00:00
|
|
|
if (NS_FAILED(rv))
|
|
|
|
mStatus = rv;
|
2003-01-18 02:15:14 +00:00
|
|
|
|
2005-11-12 18:17:19 +00:00
|
|
|
// Close the output stream before notifying our callback so that others may
|
|
|
|
// freely "play" with the file.
|
|
|
|
mDest->Close();
|
2003-01-18 02:15:14 +00:00
|
|
|
|
2005-11-12 18:17:19 +00:00
|
|
|
// Notify completion
|
2006-05-10 17:30:15 +00:00
|
|
|
if (mCallback) {
|
2006-05-10 22:49:04 +00:00
|
|
|
mCallbackTarget->Dispatch(mCallback, NS_DISPATCH_NORMAL);
|
|
|
|
|
|
|
|
// Release the callback on the target thread to avoid destroying stuff on
|
|
|
|
// the wrong thread.
|
2012-07-30 14:20:58 +00:00
|
|
|
nsIRunnable *doomed = nullptr;
|
2006-05-10 22:49:04 +00:00
|
|
|
mCallback.swap(doomed);
|
|
|
|
NS_ProxyRelease(mCallbackTarget, doomed);
|
2006-05-10 17:30:15 +00:00
|
|
|
}
|
1999-06-11 01:37:24 +00:00
|
|
|
}
|
|
|
|
|
2005-11-12 18:17:19 +00:00
|
|
|
nsresult
|
2006-05-10 22:49:04 +00:00
|
|
|
nsFileCopyEvent::Dispatch(nsIRunnable *callback,
|
2005-11-12 18:17:19 +00:00
|
|
|
nsITransportEventSink *sink,
|
|
|
|
nsIEventTarget *target)
|
2000-03-29 03:58:50 +00:00
|
|
|
{
|
2005-11-12 18:17:19 +00:00
|
|
|
// Use the supplied event target for all asynchronous operations.
|
1999-06-12 02:53:21 +00:00
|
|
|
|
2006-05-10 22:49:04 +00:00
|
|
|
mCallback = callback;
|
|
|
|
mCallbackTarget = target;
|
1999-06-12 02:53:21 +00:00
|
|
|
|
2005-11-12 18:17:19 +00:00
|
|
|
// Build a coalescing proxy for progress events
|
2006-05-10 22:49:04 +00:00
|
|
|
nsresult rv = net_NewTransportEventSinkProxy(getter_AddRefs(mSink), sink,
|
2011-10-17 14:59:28 +00:00
|
|
|
target, true);
|
2005-11-12 18:17:19 +00:00
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return rv;
|
1999-06-12 02:53:21 +00:00
|
|
|
|
2006-05-10 17:30:15 +00:00
|
|
|
// Dispatch ourselves to I/O thread pool...
|
2005-11-12 18:17:19 +00:00
|
|
|
nsCOMPtr<nsIEventTarget> pool =
|
2006-05-10 17:30:15 +00:00
|
|
|
do_GetService(NS_STREAMTRANSPORTSERVICE_CONTRACTID, &rv);
|
2005-11-12 18:17:19 +00:00
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return rv;
|
1999-10-26 09:16:24 +00:00
|
|
|
|
2006-05-10 17:30:15 +00:00
|
|
|
return pool->Dispatch(this, NS_DISPATCH_NORMAL);
|
2000-03-29 03:58:50 +00:00
|
|
|
}
|
|
|
|
|
2005-11-12 18:17:19 +00:00
|
|
|
//-----------------------------------------------------------------------------
|
1999-06-11 01:37:24 +00:00
|
|
|
|
2005-11-12 18:17:19 +00: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 08:14:52 +00:00
|
|
|
|
2006-05-10 22:49:04 +00:00
|
|
|
class nsFileUploadContentStream : public nsBaseContentStream {
|
2005-11-12 18:17:19 +00:00
|
|
|
public:
|
|
|
|
NS_DECL_ISUPPORTS_INHERITED
|
2000-03-29 03:58:50 +00:00
|
|
|
|
2011-09-29 06:19:26 +00:00
|
|
|
nsFileUploadContentStream(bool nonBlocking,
|
2005-11-12 18:17:19 +00:00
|
|
|
nsIOutputStream *dest,
|
|
|
|
nsIInputStream *source,
|
2012-08-22 15:56:38 +00:00
|
|
|
int64_t len,
|
2005-11-12 18:17:19 +00:00
|
|
|
nsITransportEventSink *sink)
|
|
|
|
: nsBaseContentStream(nonBlocking)
|
2006-05-10 17:30:15 +00:00
|
|
|
, mCopyEvent(new nsFileCopyEvent(dest, source, len))
|
2005-11-12 18:17:19 +00:00
|
|
|
, mSink(sink) {
|
|
|
|
}
|
1999-06-15 05:18:40 +00:00
|
|
|
|
2011-09-29 06:19:26 +00:00
|
|
|
bool IsInitialized() {
|
2012-07-30 14:20:58 +00:00
|
|
|
return mCopyEvent != nullptr;
|
2006-05-10 17:30:15 +00:00
|
|
|
}
|
|
|
|
|
2005-11-12 18:17:19 +00:00
|
|
|
NS_IMETHODIMP ReadSegments(nsWriteSegmentFun fun, void *closure,
|
2012-08-22 15:56:38 +00:00
|
|
|
uint32_t count, uint32_t *result);
|
|
|
|
NS_IMETHODIMP AsyncWait(nsIInputStreamCallback *callback, uint32_t flags,
|
|
|
|
uint32_t count, nsIEventTarget *target);
|
1999-06-11 01:37:24 +00:00
|
|
|
|
2005-11-12 18:17:19 +00:00
|
|
|
private:
|
2006-05-10 22:49:04 +00:00
|
|
|
void OnCopyComplete();
|
|
|
|
|
2006-05-10 17:30:15 +00:00
|
|
|
nsRefPtr<nsFileCopyEvent> mCopyEvent;
|
2005-11-12 18:17:19 +00:00
|
|
|
nsCOMPtr<nsITransportEventSink> mSink;
|
|
|
|
};
|
1999-06-22 00:19:58 +00:00
|
|
|
|
2006-05-10 22:49:04 +00:00
|
|
|
NS_IMPL_ISUPPORTS_INHERITED0(nsFileUploadContentStream,
|
|
|
|
nsBaseContentStream)
|
1999-06-22 00:19:58 +00:00
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2005-11-12 18:17:19 +00:00
|
|
|
nsFileUploadContentStream::ReadSegments(nsWriteSegmentFun fun, void *closure,
|
2012-08-22 15:56:38 +00:00
|
|
|
uint32_t count, uint32_t *result)
|
1999-06-22 00:19:58 +00:00
|
|
|
{
|
2005-11-12 18:17:19 +00:00
|
|
|
*result = 0; // nothing is ever actually read from this stream
|
1999-07-03 00:59:51 +00:00
|
|
|
|
2005-11-12 18:17:19 +00:00
|
|
|
if (IsClosed())
|
2006-01-02 03:20:04 +00:00
|
|
|
return NS_OK;
|
2002-03-20 22:50:33 +00:00
|
|
|
|
2005-11-12 18:17:19 +00: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 00:19:58 +00:00
|
|
|
|
2005-11-12 18:17:19 +00:00
|
|
|
// Perform copy synchronously, and then close out the stream.
|
2006-05-10 17:30:15 +00:00
|
|
|
mCopyEvent->DoCopy();
|
|
|
|
nsresult status = mCopyEvent->Status();
|
2005-11-12 18:17:19 +00:00
|
|
|
CloseWithStatus(NS_FAILED(status) ? status : NS_BASE_STREAM_CLOSED);
|
|
|
|
return status;
|
2000-01-08 06:26:04 +00:00
|
|
|
}
|
|
|
|
|
1999-09-09 05:07:30 +00:00
|
|
|
NS_IMETHODIMP
|
2005-11-12 18:17:19 +00:00
|
|
|
nsFileUploadContentStream::AsyncWait(nsIInputStreamCallback *callback,
|
2012-08-22 15:56:38 +00:00
|
|
|
uint32_t flags, uint32_t count,
|
2005-11-12 18:17:19 +00:00
|
|
|
nsIEventTarget *target)
|
1999-09-09 05:07:30 +00:00
|
|
|
{
|
2005-11-12 18:17:19 +00:00
|
|
|
nsresult rv = nsBaseContentStream::AsyncWait(callback, flags, count, target);
|
|
|
|
if (NS_FAILED(rv) || IsClosed())
|
|
|
|
return rv;
|
1999-09-09 05:07:30 +00:00
|
|
|
|
2006-05-10 22:49:04 +00:00
|
|
|
if (IsNonBlocking()) {
|
|
|
|
nsCOMPtr<nsIRunnable> callback =
|
2010-04-20 23:21:35 +00:00
|
|
|
NS_NewRunnableMethod(this, &nsFileUploadContentStream::OnCopyComplete);
|
2006-05-10 22:49:04 +00:00
|
|
|
mCopyEvent->Dispatch(callback, mSink, target);
|
|
|
|
}
|
2003-06-23 21:00:46 +00:00
|
|
|
|
2005-11-12 18:17:19 +00:00
|
|
|
return NS_OK;
|
2000-03-29 03:58:50 +00:00
|
|
|
}
|
|
|
|
|
2006-05-10 22:49:04 +00:00
|
|
|
void
|
|
|
|
nsFileUploadContentStream::OnCopyComplete()
|
1999-07-31 06:53:12 +00:00
|
|
|
{
|
2005-11-12 18:17:19 +00:00
|
|
|
// This method is being called to indicate that we are done copying.
|
2006-05-10 17:30:15 +00:00
|
|
|
nsresult status = mCopyEvent->Status();
|
1999-07-31 06:53:12 +00:00
|
|
|
|
2005-11-12 18:17:19 +00:00
|
|
|
CloseWithStatus(NS_FAILED(status) ? status : NS_BASE_STREAM_CLOSED);
|
1999-11-17 08:14:52 +00:00
|
|
|
}
|
|
|
|
|
2005-11-12 18:17:19 +00:00
|
|
|
//-----------------------------------------------------------------------------
|
1999-08-29 21:58:42 +00:00
|
|
|
|
2012-05-03 19:23:28 +00:00
|
|
|
nsFileChannel::nsFileChannel(nsIURI *uri)
|
|
|
|
{
|
|
|
|
// 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;
|
2012-09-02 02:35:17 +00:00
|
|
|
nsAutoCString fileTarget;
|
2012-06-06 02:08:30 +00:00
|
|
|
nsCOMPtr<nsIFile> resolvedFile;
|
2012-05-03 19:23:28 +00:00
|
|
|
bool symLink;
|
|
|
|
nsCOMPtr<nsIFileURL> fileURL = do_QueryInterface(uri);
|
|
|
|
if (fileURL &&
|
|
|
|
NS_SUCCEEDED(fileURL->GetFile(getter_AddRefs(file))) &&
|
|
|
|
NS_SUCCEEDED(file->IsSymlink(&symLink)) &&
|
|
|
|
symLink &&
|
|
|
|
NS_SUCCEEDED(file->GetNativeTarget(fileTarget)) &&
|
|
|
|
NS_SUCCEEDED(NS_NewNativeLocalFile(fileTarget, PR_TRUE,
|
|
|
|
getter_AddRefs(resolvedFile))) &&
|
|
|
|
NS_SUCCEEDED(NS_NewFileURI(getter_AddRefs(targetURI),
|
2012-07-30 14:20:58 +00:00
|
|
|
resolvedFile, nullptr))) {
|
2012-05-03 19:23:28 +00:00
|
|
|
SetURI(targetURI);
|
|
|
|
SetOriginalURI(uri);
|
|
|
|
nsLoadFlags loadFlags = 0;
|
|
|
|
GetLoadFlags(&loadFlags);
|
|
|
|
SetLoadFlags(loadFlags | nsIChannel::LOAD_REPLACE);
|
|
|
|
} else {
|
|
|
|
SetURI(uri);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-02-14 18:46:12 +00:00
|
|
|
nsresult
|
|
|
|
nsFileChannel::MakeFileInputStream(nsIFile *file,
|
|
|
|
nsCOMPtr<nsIInputStream> &stream,
|
|
|
|
nsCString &contentType)
|
2005-11-12 18:17:19 +00:00
|
|
|
{
|
|
|
|
// we accept that this might result in a disk hit to stat the file
|
2011-09-29 06:19:26 +00:00
|
|
|
bool isDir;
|
2005-11-12 18:17:19 +00:00
|
|
|
nsresult rv = file->IsDirectory(&isDir);
|
|
|
|
if (NS_FAILED(rv)) {
|
2012-05-10 00:02:40 +00:00
|
|
|
// canonicalize error message
|
|
|
|
if (rv == NS_ERROR_FILE_TARGET_DOES_NOT_EXIST)
|
|
|
|
rv = NS_ERROR_FILE_NOT_FOUND;
|
|
|
|
return rv;
|
2005-11-12 18:17:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (isDir) {
|
|
|
|
rv = nsDirectoryIndexStream::Create(file, getter_AddRefs(stream));
|
2006-02-14 18:46:12 +00:00
|
|
|
if (NS_SUCCEEDED(rv) && !HasContentTypeHint())
|
2005-11-12 18:17:19 +00:00
|
|
|
contentType.AssignLiteral(APPLICATION_HTTP_INDEX_FORMAT);
|
|
|
|
} else {
|
2012-05-10 00:02:40 +00:00
|
|
|
rv = NS_NewLocalFileInputStream(getter_AddRefs(stream), file);
|
2006-02-14 18:46:12 +00:00
|
|
|
if (NS_SUCCEEDED(rv) && !HasContentTypeHint()) {
|
2005-11-12 18:17:19 +00: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-29 21:58:42 +00:00
|
|
|
}
|
1999-07-31 06:53:12 +00:00
|
|
|
|
2005-11-12 18:17:19 +00:00
|
|
|
nsresult
|
2011-09-29 06:19:26 +00:00
|
|
|
nsFileChannel::OpenContentStream(bool async, nsIInputStream **result,
|
2008-09-29 21:02:44 +00:00
|
|
|
nsIChannel** channel)
|
1999-11-17 08:14:52 +00:00
|
|
|
{
|
2005-11-12 18:17:19 +00: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 08:14:52 +00:00
|
|
|
|
2008-09-29 21:02:44 +00:00
|
|
|
nsCOMPtr<nsIFileProtocolHandler> fileHandler;
|
|
|
|
rv = NS_GetFileProtocolHandler(getter_AddRefs(fileHandler));
|
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return rv;
|
|
|
|
|
|
|
|
nsCOMPtr<nsIURI> newURI;
|
|
|
|
rv = fileHandler->ReadURLFile(file, getter_AddRefs(newURI));
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
|
|
nsCOMPtr<nsIChannel> newChannel;
|
|
|
|
rv = NS_NewChannel(getter_AddRefs(newChannel), newURI);
|
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return rv;
|
|
|
|
|
2012-07-30 14:20:58 +00:00
|
|
|
*result = nullptr;
|
2008-09-29 21:02:44 +00:00
|
|
|
newChannel.forget(channel);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2005-11-12 18:17:19 +00:00
|
|
|
nsCOMPtr<nsIInputStream> stream;
|
2000-06-02 05:19:16 +00:00
|
|
|
|
2005-11-12 18:17:19 +00: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 05:19:16 +00:00
|
|
|
|
2005-11-12 18:17:19 +00: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;
|
|
|
|
|
2006-05-10 17:30:15 +00:00
|
|
|
nsFileUploadContentStream *uploadStream =
|
|
|
|
new nsFileUploadContentStream(async, fileStream, mUploadStream,
|
|
|
|
mUploadLength, this);
|
2006-10-25 16:00:02 +00:00
|
|
|
if (!uploadStream || !uploadStream->IsInitialized()) {
|
|
|
|
delete uploadStream;
|
2005-11-12 18:17:19 +00:00
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
2006-10-25 16:00:02 +00:00
|
|
|
}
|
2006-05-10 17:30:15 +00:00
|
|
|
stream = uploadStream;
|
2005-11-12 18:17:19 +00:00
|
|
|
|
2010-08-30 20:20:39 +00:00
|
|
|
SetContentLength64(0);
|
2005-11-12 18:17:19 +00: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 18:46:12 +00:00
|
|
|
// However, don't override explicitly set types.
|
|
|
|
if (!HasContentTypeHint())
|
|
|
|
SetContentType(NS_LITERAL_CSTRING(APPLICATION_OCTET_STREAM));
|
2005-11-12 18:17:19 +00:00
|
|
|
} else {
|
2012-09-02 02:35:17 +00:00
|
|
|
nsAutoCString contentType;
|
2011-10-30 14:47:51 +00:00
|
|
|
rv = MakeFileInputStream(file, stream, contentType);
|
2005-11-12 18:17:19 +00:00
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return rv;
|
2003-01-18 02:15:14 +00:00
|
|
|
|
2011-10-17 14:59:28 +00:00
|
|
|
EnableSynthesizedProgressEvents(true);
|
1999-08-26 22:45:55 +00:00
|
|
|
|
2005-11-12 18:17:19 +00:00
|
|
|
// fixup content length and type
|
2010-08-30 20:20:39 +00:00
|
|
|
if (ContentLength64() < 0) {
|
2012-08-22 15:56:38 +00:00
|
|
|
int64_t size;
|
2005-11-12 18:17:19 +00:00
|
|
|
rv = file->GetFileSize(&size);
|
2012-05-10 00:02:40 +00:00
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return rv;
|
2010-08-30 20:20:39 +00:00
|
|
|
SetContentLength64(size);
|
2000-06-02 05:19:16 +00:00
|
|
|
}
|
2005-11-12 18:17:19 +00:00
|
|
|
if (!contentType.IsEmpty())
|
|
|
|
SetContentType(contentType);
|
|
|
|
}
|
2000-06-02 05:19:16 +00:00
|
|
|
|
2012-07-30 14:20:58 +00:00
|
|
|
*result = nullptr;
|
2005-11-12 18:17:19 +00:00
|
|
|
stream.swap(*result);
|
|
|
|
return NS_OK;
|
2000-06-02 05:19:16 +00:00
|
|
|
}
|
|
|
|
|
2003-01-18 02:15:14 +00:00
|
|
|
//-----------------------------------------------------------------------------
|
2005-11-12 18:17:19 +00:00
|
|
|
// nsFileChannel::nsISupports
|
|
|
|
|
|
|
|
NS_IMPL_ISUPPORTS_INHERITED2(nsFileChannel,
|
|
|
|
nsBaseChannel,
|
|
|
|
nsIUploadChannel,
|
|
|
|
nsIFileChannel)
|
|
|
|
|
2003-01-18 02:15:14 +00:00
|
|
|
//-----------------------------------------------------------------------------
|
2005-11-12 18:17:19 +00:00
|
|
|
// nsFileChannel::nsIFileChannel
|
2003-01-18 02:15:14 +00:00
|
|
|
|
2000-06-02 05:19:16 +00:00
|
|
|
NS_IMETHODIMP
|
2003-01-18 02:15:14 +00:00
|
|
|
nsFileChannel::GetFile(nsIFile **file)
|
2000-06-02 05:19:16 +00:00
|
|
|
{
|
2005-11-12 18:17:19 +00:00
|
|
|
nsCOMPtr<nsIFileURL> fileURL = do_QueryInterface(URI());
|
|
|
|
NS_ENSURE_STATE(fileURL);
|
|
|
|
|
|
|
|
// This returns a cloned nsIFile
|
|
|
|
return fileURL->GetFile(file);
|
2000-06-02 05:19:16 +00:00
|
|
|
}
|
|
|
|
|
2003-01-18 02:15:14 +00:00
|
|
|
//-----------------------------------------------------------------------------
|
2005-11-12 18:17:19 +00:00
|
|
|
// nsFileChannel::nsIUploadChannel
|
1999-06-11 01:37:24 +00:00
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2003-01-18 02:15:14 +00:00
|
|
|
nsFileChannel::SetUploadStream(nsIInputStream *stream,
|
|
|
|
const nsACString &contentType,
|
2012-08-22 15:56:38 +00:00
|
|
|
int32_t contentLength)
|
1999-06-12 07:14:19 +00:00
|
|
|
{
|
2005-11-12 18:17:19 +00:00
|
|
|
NS_ENSURE_TRUE(!IsPending(), NS_ERROR_IN_PROGRESS);
|
1999-06-12 07:14:19 +00:00
|
|
|
|
2005-11-12 18:17:19 +00:00
|
|
|
if ((mUploadStream = stream)) {
|
|
|
|
mUploadLength = contentLength;
|
|
|
|
if (mUploadLength < 0) {
|
|
|
|
// Make sure we know how much data we are uploading.
|
2012-08-22 15:56:38 +00:00
|
|
|
uint64_t avail;
|
2005-11-12 18:17:19 +00:00
|
|
|
nsresult rv = mUploadStream->Available(&avail);
|
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return rv;
|
2012-08-11 02:44:11 +00:00
|
|
|
if (avail < PR_INT64_MAX)
|
|
|
|
mUploadLength = avail;
|
2003-01-18 02:15:14 +00:00
|
|
|
}
|
2005-11-12 18:17:19 +00:00
|
|
|
} else {
|
|
|
|
mUploadLength = -1;
|
|
|
|
}
|
|
|
|
return NS_OK;
|
1999-11-12 06:13:13 +00:00
|
|
|
}
|
|
|
|
|
2000-03-29 03:58:50 +00:00
|
|
|
NS_IMETHODIMP
|
2005-11-12 18:17:19 +00:00
|
|
|
nsFileChannel::GetUploadStream(nsIInputStream **result)
|
2000-03-29 03:58:50 +00:00
|
|
|
{
|
2005-11-12 18:17:19 +00:00
|
|
|
NS_IF_ADDREF(*result = mUploadStream);
|
2000-03-29 03:58:50 +00:00
|
|
|
return NS_OK;
|
|
|
|
}
|