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: */
|
2001-09-28 20:14:13 +00:00
|
|
|
/* ***** BEGIN LICENSE BLOCK *****
|
2004-04-18 22:01:16 +00:00
|
|
|
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
|
1999-06-11 01:37:24 +00:00
|
|
|
*
|
2004-04-18 22:01:16 +00:00
|
|
|
* The contents of this file are subject to the Mozilla Public License Version
|
|
|
|
* 1.1 (the "License"); you may not use this file except in compliance with
|
|
|
|
* the License. You may obtain a copy of the License at
|
|
|
|
* http://www.mozilla.org/MPL/
|
1999-06-11 01:37:24 +00:00
|
|
|
*
|
2001-09-28 20:14:13 +00:00
|
|
|
* Software distributed under the License is distributed on an "AS IS" basis,
|
|
|
|
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
|
|
|
|
* for the specific language governing rights and limitations under the
|
|
|
|
* License.
|
1999-06-11 01:37:24 +00:00
|
|
|
*
|
1999-11-06 03:40:37 +00:00
|
|
|
* The Original Code is mozilla.org code.
|
|
|
|
*
|
2002-09-29 17:46:18 +00:00
|
|
|
* The Initial Developer of the Original Code is
|
2001-09-28 20:14:13 +00:00
|
|
|
* Netscape Communications Corporation.
|
|
|
|
* Portions created by the Initial Developer are Copyright (C) 1998
|
|
|
|
* the Initial Developer. All Rights Reserved.
|
1999-11-06 03:40:37 +00:00
|
|
|
*
|
2001-09-28 20:14:13 +00:00
|
|
|
* Contributor(s):
|
2005-11-12 18:17:19 +00:00
|
|
|
* Darin Fisher <darin@meer.net>
|
2001-09-28 20:14:13 +00:00
|
|
|
*
|
|
|
|
* Alternatively, the contents of this file may be used under the terms of
|
2002-09-29 17:46:18 +00:00
|
|
|
* either the GNU General Public License Version 2 or later (the "GPL"), or
|
2001-09-28 20:14:13 +00:00
|
|
|
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
|
|
|
|
* in which case the provisions of the GPL or the LGPL are applicable instead
|
|
|
|
* of those above. If you wish to allow use of your version of this file only
|
|
|
|
* under the terms of either the GPL or the LGPL, and not to allow others to
|
2004-04-18 22:01:16 +00:00
|
|
|
* use your version of this file under the terms of the MPL, indicate your
|
2001-09-28 20:14:13 +00:00
|
|
|
* decision by deleting the provisions above and replace them with the notice
|
|
|
|
* and other provisions required by the GPL or the LGPL. If you do not delete
|
|
|
|
* the provisions above, a recipient may use your version of this file under
|
2004-04-18 22:01:16 +00:00
|
|
|
* the terms of any one of the MPL, the GPL or the LGPL.
|
2001-09-28 20:14:13 +00:00
|
|
|
*
|
|
|
|
* ***** END LICENSE BLOCK ***** */
|
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"
|
2005-11-12 18:17:19 +00:00
|
|
|
#include "nsNetSegmentUtils.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:
|
|
|
|
nsFileCopyEvent(nsIOutputStream *dest, nsIInputStream *source, PRInt64 len)
|
|
|
|
: 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;
|
|
|
|
PRInt64 mLen;
|
|
|
|
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.
|
|
|
|
const PRInt32 chunk = NET_DEFAULT_SEGMENT_SIZE * NET_DEFAULT_SEGMENT_COUNT;
|
|
|
|
|
|
|
|
nsresult rv = NS_OK;
|
|
|
|
|
|
|
|
PRInt64 len = mLen, progress = 0;
|
|
|
|
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
|
|
|
|
2005-11-12 18:17:19 +00:00
|
|
|
PRInt32 num = PR_MIN((PRInt32) len, chunk);
|
2002-04-30 20:48:15 +00:00
|
|
|
|
2005-11-12 18:17:19 +00:00
|
|
|
PRUint32 result;
|
|
|
|
rv = mSource->ReadSegments(NS_CopySegmentToStream, mDest, num, &result);
|
|
|
|
if (NS_FAILED(rv))
|
|
|
|
break;
|
|
|
|
if (result != (PRUint32) num) {
|
|
|
|
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;
|
|
|
|
mSink->OnTransportStatus(nsnull, nsITransport::STATUS_WRITING, progress,
|
|
|
|
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.
|
|
|
|
nsIRunnable *doomed = nsnull;
|
|
|
|
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,
|
|
|
|
target, PR_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
|
|
|
|
2005-11-12 18:17:19 +00:00
|
|
|
nsFileUploadContentStream(PRBool nonBlocking,
|
|
|
|
nsIOutputStream *dest,
|
|
|
|
nsIInputStream *source,
|
|
|
|
PRInt64 len,
|
|
|
|
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
|
|
|
|
2006-05-10 17:30:15 +00:00
|
|
|
PRBool IsInitialized() {
|
|
|
|
return mCopyEvent != nsnull;
|
|
|
|
}
|
|
|
|
|
2005-11-12 18:17:19 +00:00
|
|
|
NS_IMETHODIMP ReadSegments(nsWriteSegmentFun fun, void *closure,
|
|
|
|
PRUint32 count, PRUint32 *result);
|
|
|
|
NS_IMETHODIMP AsyncWait(nsIInputStreamCallback *callback, PRUint32 flags,
|
|
|
|
PRUint32 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,
|
|
|
|
PRUint32 count, PRUint32 *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,
|
|
|
|
PRUint32 flags, PRUint32 count,
|
|
|
|
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 =
|
|
|
|
NS_NEW_RUNNABLE_METHOD(nsFileUploadContentStream, this,
|
|
|
|
OnCopyComplete);
|
|
|
|
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
|
|
|
|
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
|
|
|
|
PRBool isDir;
|
|
|
|
nsresult rv = file->IsDirectory(&isDir);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
// canonicalize error message
|
|
|
|
if (rv == NS_ERROR_FILE_TARGET_DOES_NOT_EXIST)
|
|
|
|
rv = NS_ERROR_FILE_NOT_FOUND;
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
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 {
|
|
|
|
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
|
|
|
|
nsFileChannel::OpenContentStream(PRBool async, nsIInputStream **result)
|
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
|
|
|
|
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
|
|
|
|
|
|
|
SetContentLength64(0);
|
|
|
|
|
|
|
|
// 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 {
|
|
|
|
nsCAutoString contentType;
|
|
|
|
nsresult rv = MakeFileInputStream(file, stream, contentType);
|
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return rv;
|
2003-01-18 02:15:14 +00:00
|
|
|
|
2005-11-12 18:17:19 +00:00
|
|
|
EnableSynthesizedProgressEvents(PR_TRUE);
|
1999-08-26 22:45:55 +00:00
|
|
|
|
2005-11-12 18:17:19 +00:00
|
|
|
// fixup content length and type
|
|
|
|
if (ContentLength64() < 0) {
|
|
|
|
PRInt64 size;
|
|
|
|
rv = file->GetFileSize(&size);
|
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return rv;
|
|
|
|
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
|
|
|
|
2005-11-12 18:17:19 +00:00
|
|
|
*result = nsnull;
|
|
|
|
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,
|
|
|
|
PRInt32 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.
|
|
|
|
PRUint32 avail;
|
|
|
|
nsresult rv = mUploadStream->Available(&avail);
|
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return rv;
|
|
|
|
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;
|
|
|
|
}
|