2001-09-29 00:14:13 +04:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
|
|
|
|
/* ***** BEGIN LICENSE BLOCK *****
|
2004-04-19 02:01:16 +04:00
|
|
|
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
|
1999-08-27 18:10:21 +04:00
|
|
|
*
|
2004-04-19 02:01:16 +04: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-08-27 18:10:21 +04:00
|
|
|
*
|
2001-09-29 00:14:13 +04: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-08-27 18:10:21 +04:00
|
|
|
*
|
1999-11-06 06:40:37 +03:00
|
|
|
* The Original Code is mozilla.org code.
|
|
|
|
*
|
2004-04-19 02:01:16 +04:00
|
|
|
* The Initial Developer of the Original Code is
|
2001-09-29 00:14:13 +04:00
|
|
|
* Netscape Communications Corporation.
|
|
|
|
* Portions created by the Initial Developer are Copyright (C) 1998
|
|
|
|
* the Initial Developer. All Rights Reserved.
|
1999-11-06 06:40:37 +03:00
|
|
|
*
|
2001-09-29 00:14:13 +04:00
|
|
|
* Contributor(s):
|
|
|
|
*
|
|
|
|
* Alternatively, the contents of this file may be used under the terms of
|
2004-04-19 02:01:16 +04:00
|
|
|
* either the GNU General Public License Version 2 or later (the "GPL"), or
|
2001-09-29 00:14:13 +04: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-19 02:01:16 +04:00
|
|
|
* use your version of this file under the terms of the MPL, indicate your
|
2001-09-29 00:14:13 +04: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-19 02:01:16 +04:00
|
|
|
* the terms of any one of the MPL, the GPL or the LGPL.
|
2001-09-29 00:14:13 +04:00
|
|
|
*
|
|
|
|
* ***** END LICENSE BLOCK ***** */
|
1999-08-27 18:10:21 +04:00
|
|
|
|
1999-08-26 18:53:01 +04:00
|
|
|
#include "nsMultiMixedConv.h"
|
2000-06-03 13:46:12 +04:00
|
|
|
#include "nsMemory.h"
|
2005-01-09 23:43:35 +03:00
|
|
|
#include "nsInt64.h"
|
1999-08-26 18:53:01 +04:00
|
|
|
#include "plstr.h"
|
2001-05-12 01:04:09 +04:00
|
|
|
#include "nsIHttpChannel.h"
|
1999-08-26 18:53:01 +04:00
|
|
|
#include "nsIServiceManager.h"
|
1999-11-30 07:50:42 +03:00
|
|
|
#include "nsNetUtil.h"
|
2000-04-15 01:27:32 +04:00
|
|
|
#include "nsMimeTypes.h"
|
2002-05-24 03:46:01 +04:00
|
|
|
#include "nsIStringStream.h"
|
2001-09-29 12:28:41 +04:00
|
|
|
#include "nsReadableUtils.h"
|
2002-05-15 22:55:21 +04:00
|
|
|
#include "nsCRT.h"
|
2003-08-05 05:44:27 +04:00
|
|
|
#include "nsIHttpChannelInternal.h"
|
2005-07-26 00:27:04 +04:00
|
|
|
#include "nsURLHelper.h"
|
1999-09-10 18:36:56 +04:00
|
|
|
|
2002-08-14 03:39:36 +04:00
|
|
|
//
|
|
|
|
// Helper function for determining the length of data bytes up to
|
|
|
|
// the next multipart token. A token is usually preceded by a LF
|
|
|
|
// or CRLF delimiter.
|
|
|
|
//
|
|
|
|
static PRUint32
|
|
|
|
LengthToToken(const char *cursor, const char *token)
|
|
|
|
{
|
|
|
|
PRUint32 len = token - cursor;
|
|
|
|
// Trim off any LF or CRLF preceding the token
|
|
|
|
if (len && *(token-1) == '\n') {
|
|
|
|
--len;
|
|
|
|
if (len && *(token-2) == '\r')
|
|
|
|
--len;
|
|
|
|
}
|
|
|
|
return len;
|
|
|
|
}
|
2001-05-30 02:10:50 +04:00
|
|
|
|
2005-01-15 20:47:23 +03:00
|
|
|
nsPartChannel::nsPartChannel(nsIChannel *aMultipartChannel, PRUint32 aPartID) :
|
2001-05-30 02:10:50 +04:00
|
|
|
mStatus(NS_OK),
|
2005-01-09 23:43:35 +03:00
|
|
|
mContentLength(LL_MAXUINT),
|
2001-05-30 02:10:50 +04:00
|
|
|
mIsByteRangeRequest(PR_FALSE),
|
|
|
|
mByteRangeStart(0),
|
2005-01-15 20:47:23 +03:00
|
|
|
mByteRangeEnd(0),
|
2005-10-04 02:27:20 +04:00
|
|
|
mPartID(aPartID),
|
|
|
|
mIsLastPart(PR_FALSE)
|
2001-05-30 02:10:50 +04:00
|
|
|
{
|
|
|
|
mMultipartChannel = aMultipartChannel;
|
|
|
|
|
|
|
|
// Inherit the load flags from the original channel...
|
|
|
|
mMultipartChannel->GetLoadFlags(&mLoadFlags);
|
|
|
|
|
|
|
|
mMultipartChannel->GetLoadGroup(getter_AddRefs(mLoadGroup));
|
|
|
|
}
|
|
|
|
|
|
|
|
nsPartChannel::~nsPartChannel()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2005-01-09 23:43:35 +03:00
|
|
|
void nsPartChannel::InitializeByteRange(PRInt64 aStart, PRInt64 aEnd)
|
2001-05-30 02:10:50 +04:00
|
|
|
{
|
|
|
|
mIsByteRangeRequest = PR_TRUE;
|
|
|
|
|
|
|
|
mByteRangeStart = aStart;
|
|
|
|
mByteRangeEnd = aEnd;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//
|
|
|
|
// nsISupports implementation...
|
|
|
|
//
|
|
|
|
|
|
|
|
NS_IMPL_ADDREF(nsPartChannel)
|
|
|
|
NS_IMPL_RELEASE(nsPartChannel)
|
|
|
|
|
|
|
|
NS_INTERFACE_MAP_BEGIN(nsPartChannel)
|
|
|
|
NS_INTERFACE_MAP_ENTRY_AMBIGUOUS(nsISupports, nsIChannel)
|
|
|
|
NS_INTERFACE_MAP_ENTRY(nsIRequest)
|
|
|
|
NS_INTERFACE_MAP_ENTRY(nsIChannel)
|
|
|
|
NS_INTERFACE_MAP_ENTRY(nsIByteRangeRequest)
|
2001-11-28 02:13:10 +03:00
|
|
|
NS_INTERFACE_MAP_ENTRY(nsIMultiPartChannel)
|
2001-05-30 02:10:50 +04:00
|
|
|
NS_INTERFACE_MAP_END
|
|
|
|
|
|
|
|
//
|
|
|
|
// nsIRequest implementation...
|
|
|
|
//
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2002-03-21 01:50:33 +03:00
|
|
|
nsPartChannel::GetName(nsACString &aResult)
|
2001-05-30 02:10:50 +04:00
|
|
|
{
|
|
|
|
return mMultipartChannel->GetName(aResult);
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsPartChannel::IsPending(PRBool *aResult)
|
|
|
|
{
|
|
|
|
// For now, consider the active lifetime of each part the same as
|
|
|
|
// the underlying multipart channel... This is not exactly right,
|
|
|
|
// but it is good enough :-)
|
|
|
|
return mMultipartChannel->IsPending(aResult);
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsPartChannel::GetStatus(nsresult *aResult)
|
|
|
|
{
|
|
|
|
nsresult rv = NS_OK;
|
|
|
|
|
|
|
|
if (NS_FAILED(mStatus)) {
|
|
|
|
*aResult = mStatus;
|
|
|
|
} else {
|
|
|
|
rv = mMultipartChannel->GetStatus(aResult);
|
|
|
|
}
|
|
|
|
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsPartChannel::Cancel(nsresult aStatus)
|
|
|
|
{
|
|
|
|
// Cancelling an individual part must not cancel the underlying
|
|
|
|
// multipart channel...
|
2004-06-16 23:51:21 +04:00
|
|
|
// XXX but we should stop sending data for _this_ part channel!
|
2001-05-30 02:10:50 +04:00
|
|
|
mStatus = aStatus;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsPartChannel::Suspend(void)
|
|
|
|
{
|
|
|
|
// Suspending an individual part must not suspend the underlying
|
|
|
|
// multipart channel...
|
2004-06-16 23:51:21 +04:00
|
|
|
// XXX why not?
|
2001-05-30 02:10:50 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsPartChannel::Resume(void)
|
|
|
|
{
|
|
|
|
// Resuming an individual part must not resume the underlying
|
|
|
|
// multipart channel...
|
2004-06-16 23:51:21 +04:00
|
|
|
// XXX why not?
|
2001-05-30 02:10:50 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// nsIChannel implementation
|
|
|
|
//
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsPartChannel::GetOriginalURI(nsIURI * *aURI)
|
|
|
|
{
|
|
|
|
return mMultipartChannel->GetOriginalURI(aURI);
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsPartChannel::SetOriginalURI(nsIURI *aURI)
|
|
|
|
{
|
|
|
|
return mMultipartChannel->SetOriginalURI(aURI);
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsPartChannel::GetURI(nsIURI * *aURI)
|
|
|
|
{
|
|
|
|
return mMultipartChannel->GetURI(aURI);
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsPartChannel::Open(nsIInputStream **result)
|
|
|
|
{
|
|
|
|
// This channel cannot be opened!
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsPartChannel::AsyncOpen(nsIStreamListener *aListener, nsISupports *aContext)
|
|
|
|
{
|
|
|
|
// This channel cannot be opened!
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsPartChannel::GetLoadFlags(nsLoadFlags *aLoadFlags)
|
|
|
|
{
|
|
|
|
*aLoadFlags = mLoadFlags;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsPartChannel::SetLoadFlags(nsLoadFlags aLoadFlags)
|
|
|
|
{
|
|
|
|
mLoadFlags = aLoadFlags;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsPartChannel::GetLoadGroup(nsILoadGroup* *aLoadGroup)
|
|
|
|
{
|
|
|
|
*aLoadGroup = mLoadGroup;
|
|
|
|
NS_IF_ADDREF(*aLoadGroup);
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsPartChannel::SetLoadGroup(nsILoadGroup* aLoadGroup)
|
|
|
|
{
|
|
|
|
mLoadGroup = aLoadGroup;
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsPartChannel::GetOwner(nsISupports* *aOwner)
|
|
|
|
{
|
|
|
|
return mMultipartChannel->GetOwner(aOwner);
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsPartChannel::SetOwner(nsISupports* aOwner)
|
|
|
|
{
|
|
|
|
return mMultipartChannel->SetOwner(aOwner);
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsPartChannel::GetNotificationCallbacks(nsIInterfaceRequestor* *aCallbacks)
|
|
|
|
{
|
|
|
|
return mMultipartChannel->GetNotificationCallbacks(aCallbacks);
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsPartChannel::SetNotificationCallbacks(nsIInterfaceRequestor* aCallbacks)
|
|
|
|
{
|
|
|
|
return mMultipartChannel->SetNotificationCallbacks(aCallbacks);
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsPartChannel::GetSecurityInfo(nsISupports * *aSecurityInfo)
|
|
|
|
{
|
|
|
|
return mMultipartChannel->GetSecurityInfo(aSecurityInfo);
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2002-03-21 01:50:33 +03:00
|
|
|
nsPartChannel::GetContentType(nsACString &aContentType)
|
2001-05-30 02:10:50 +04:00
|
|
|
{
|
2002-03-21 01:50:33 +03:00
|
|
|
aContentType = mContentType;
|
2001-05-30 02:10:50 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2002-03-21 01:50:33 +03:00
|
|
|
nsPartChannel::SetContentType(const nsACString &aContentType)
|
2001-05-30 02:10:50 +04:00
|
|
|
{
|
2005-07-26 00:27:04 +04:00
|
|
|
PRBool dummy;
|
|
|
|
net_ParseContentType(aContentType, mContentType, mContentCharset, &dummy);
|
2002-03-21 01:50:33 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsPartChannel::GetContentCharset(nsACString &aContentCharset)
|
|
|
|
{
|
|
|
|
aContentCharset = mContentCharset;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsPartChannel::SetContentCharset(const nsACString &aContentCharset)
|
|
|
|
{
|
|
|
|
mContentCharset = aContentCharset;
|
2001-05-30 02:10:50 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsPartChannel::GetContentLength(PRInt32 *aContentLength)
|
|
|
|
{
|
2005-01-09 23:43:35 +03:00
|
|
|
*aContentLength = mContentLength; // XXX truncates 64-bit value
|
2001-05-30 02:10:50 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsPartChannel::SetContentLength(PRInt32 aContentLength)
|
|
|
|
{
|
|
|
|
mContentLength = aContentLength;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2002-02-06 06:39:04 +03:00
|
|
|
NS_IMETHODIMP
|
2002-03-21 01:50:33 +03:00
|
|
|
nsPartChannel::GetContentDisposition(nsACString &aContentDisposition)
|
2002-02-06 06:39:04 +03:00
|
|
|
{
|
2002-03-21 01:50:33 +03:00
|
|
|
aContentDisposition = mContentDisposition;
|
2002-02-06 06:39:04 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2002-03-21 01:50:33 +03:00
|
|
|
nsPartChannel::SetContentDisposition(const nsACString &aContentDisposition)
|
2002-02-06 06:39:04 +03:00
|
|
|
{
|
2002-03-21 01:50:33 +03:00
|
|
|
mContentDisposition = aContentDisposition;
|
2002-02-06 06:39:04 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
2001-05-30 02:10:50 +04:00
|
|
|
|
2005-01-15 20:47:23 +03:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsPartChannel::GetPartID(PRUint32 *aPartID)
|
|
|
|
{
|
|
|
|
*aPartID = mPartID;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2005-10-04 02:27:20 +04:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsPartChannel::GetIsLastPart(PRBool *aIsLastPart)
|
|
|
|
{
|
|
|
|
*aIsLastPart = mIsLastPart;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2001-05-30 02:10:50 +04:00
|
|
|
//
|
|
|
|
// nsIByteRangeRequest implementation...
|
|
|
|
//
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsPartChannel::GetIsByteRangeRequest(PRBool *aIsByteRangeRequest)
|
|
|
|
{
|
|
|
|
*aIsByteRangeRequest = mIsByteRangeRequest;
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2005-01-09 23:43:35 +03:00
|
|
|
nsPartChannel::GetStartRange(PRInt64 *aStartRange)
|
2001-05-30 02:10:50 +04:00
|
|
|
{
|
|
|
|
*aStartRange = mByteRangeStart;
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2005-01-09 23:43:35 +03:00
|
|
|
nsPartChannel::GetEndRange(PRInt64 *aEndRange)
|
2001-05-30 02:10:50 +04:00
|
|
|
{
|
|
|
|
*aEndRange = mByteRangeEnd;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2001-11-28 02:13:10 +03:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsPartChannel::GetBaseChannel(nsIChannel ** aReturn)
|
|
|
|
{
|
|
|
|
NS_ENSURE_ARG_POINTER(aReturn);
|
2001-05-30 02:10:50 +04:00
|
|
|
|
2001-11-28 02:13:10 +03:00
|
|
|
*aReturn = mMultipartChannel;
|
|
|
|
NS_IF_ADDREF(*aReturn);
|
2001-11-28 03:24:00 +03:00
|
|
|
return NS_OK;
|
2001-11-28 02:13:10 +03:00
|
|
|
}
|
2001-05-30 02:10:50 +04:00
|
|
|
|
|
|
|
|
1999-08-26 18:53:01 +04:00
|
|
|
// nsISupports implementation
|
2001-05-30 02:10:50 +04:00
|
|
|
NS_IMPL_THREADSAFE_ISUPPORTS3(nsMultiMixedConv,
|
2001-02-21 23:38:08 +03:00
|
|
|
nsIStreamConverter,
|
2001-05-30 02:10:50 +04:00
|
|
|
nsIStreamListener,
|
2003-09-08 03:12:15 +04:00
|
|
|
nsIRequestObserver)
|
1999-08-26 18:53:01 +04:00
|
|
|
|
|
|
|
|
|
|
|
// nsIStreamConverter implementation
|
1999-08-27 19:12:40 +04:00
|
|
|
|
|
|
|
// No syncronous conversion at this time.
|
1999-08-26 18:53:01 +04:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsMultiMixedConv::Convert(nsIInputStream *aFromStream,
|
2004-06-29 20:45:07 +04:00
|
|
|
const char *aFromType,
|
|
|
|
const char *aToType,
|
1999-08-26 18:53:01 +04:00
|
|
|
nsISupports *aCtxt, nsIInputStream **_retval) {
|
|
|
|
return NS_ERROR_NOT_IMPLEMENTED;
|
|
|
|
}
|
|
|
|
|
1999-08-27 19:12:40 +04:00
|
|
|
// Stream converter service calls this to initialize the actual stream converter (us).
|
1999-08-26 18:53:01 +04:00
|
|
|
NS_IMETHODIMP
|
2004-06-29 20:45:07 +04:00
|
|
|
nsMultiMixedConv::AsyncConvertData(const char *aFromType, const char *aToType,
|
1999-08-26 18:53:01 +04:00
|
|
|
nsIStreamListener *aListener, nsISupports *aCtxt) {
|
|
|
|
NS_ASSERTION(aListener && aFromType && aToType, "null pointer passed into multi mixed converter");
|
|
|
|
|
1999-08-27 19:12:40 +04:00
|
|
|
// hook up our final listener. this guy gets the various On*() calls we want to throw
|
|
|
|
// at him.
|
|
|
|
//
|
|
|
|
// WARNING: this listener must be able to handle multiple OnStartRequest, OnDataAvail()
|
|
|
|
// and OnStopRequest() call combinations. We call of series of these for each sub-part
|
|
|
|
// in the raw stream.
|
1999-08-26 18:53:01 +04:00
|
|
|
mFinalListener = aListener;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2002-05-24 03:46:01 +04:00
|
|
|
#define ERR_OUT { free(buffer); return rv; }
|
2000-04-15 01:27:32 +04:00
|
|
|
|
1999-08-26 18:53:01 +04:00
|
|
|
// nsIStreamListener implementation
|
|
|
|
NS_IMETHODIMP
|
2001-02-21 23:38:08 +03:00
|
|
|
nsMultiMixedConv::OnDataAvailable(nsIRequest *request, nsISupports *context,
|
1999-08-26 18:53:01 +04:00
|
|
|
nsIInputStream *inStr, PRUint32 sourceOffset, PRUint32 count) {
|
2001-06-22 02:06:19 +04:00
|
|
|
|
2002-03-21 01:50:33 +03:00
|
|
|
if (mToken.IsEmpty()) // no token, no love.
|
2001-06-22 02:06:19 +04:00
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
|
2000-04-15 01:27:32 +04:00
|
|
|
nsresult rv = NS_OK;
|
|
|
|
char *buffer = nsnull;
|
2002-05-24 03:46:01 +04:00
|
|
|
PRUint32 bufLen = 0, read = 0;
|
1999-08-26 18:53:01 +04:00
|
|
|
|
2001-02-21 23:38:08 +03:00
|
|
|
NS_ASSERTION(request, "multimixed converter needs a request");
|
1999-08-27 18:10:21 +04:00
|
|
|
|
2001-02-21 23:38:08 +03:00
|
|
|
nsCOMPtr<nsIChannel> channel = do_QueryInterface(request, &rv);
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
1999-08-26 18:53:01 +04:00
|
|
|
|
2002-05-24 03:46:01 +04:00
|
|
|
// fill buffer
|
|
|
|
{
|
|
|
|
bufLen = count + mBufLen;
|
|
|
|
buffer = (char *) malloc(bufLen);
|
|
|
|
if (!buffer)
|
2001-02-27 15:33:12 +03:00
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
2002-05-24 03:46:01 +04:00
|
|
|
|
|
|
|
if (mBufLen) {
|
|
|
|
// incorporate any buffered data into the parsing
|
|
|
|
memcpy(buffer, mBuffer, mBufLen);
|
|
|
|
free(mBuffer);
|
|
|
|
mBuffer = 0;
|
2002-09-12 22:12:51 +04:00
|
|
|
mBufLen = 0;
|
2001-02-27 15:33:12 +03:00
|
|
|
}
|
2002-05-24 03:46:01 +04:00
|
|
|
|
2002-09-12 22:12:51 +04:00
|
|
|
rv = inStr->Read(buffer + (bufLen - count), count, &read);
|
2002-05-24 03:46:01 +04:00
|
|
|
|
|
|
|
if (NS_FAILED(rv) || read == 0) return rv;
|
|
|
|
NS_ASSERTION(read == count, "poor data size assumption");
|
1999-11-11 03:34:01 +03:00
|
|
|
}
|
|
|
|
|
2001-05-19 02:22:15 +04:00
|
|
|
char *cursor = buffer;
|
|
|
|
|
2001-02-27 15:33:12 +03:00
|
|
|
if (mFirstOnData) {
|
|
|
|
// this is the first OnData() for this request. some servers
|
|
|
|
// don't bother sending a token in the first "part." This is
|
|
|
|
// illegal, but we'll handle the case anyway by shoving the
|
|
|
|
// boundary token in for the server.
|
|
|
|
mFirstOnData = PR_FALSE;
|
|
|
|
NS_ASSERTION(!mBufLen, "this is our first time through, we can't have buffered data");
|
2002-03-21 01:50:33 +03:00
|
|
|
const char * token = mToken.get();
|
2001-05-19 02:22:15 +04:00
|
|
|
|
|
|
|
PushOverLine(cursor, bufLen);
|
2001-02-27 15:33:12 +03:00
|
|
|
|
|
|
|
if (bufLen < mTokenLen+2) {
|
|
|
|
// we don't have enough data yet to make this comparison.
|
|
|
|
// skip this check, and try again the next time OnData()
|
|
|
|
// is called.
|
|
|
|
mFirstOnData = PR_TRUE;
|
2002-05-24 03:46:01 +04:00
|
|
|
}
|
|
|
|
else if (!PL_strnstr(cursor, token, mTokenLen+2)) {
|
|
|
|
buffer = (char *) realloc(buffer, bufLen + mTokenLen + 1);
|
|
|
|
if (!buffer)
|
2001-02-27 15:33:12 +03:00
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
2002-05-24 03:46:01 +04:00
|
|
|
|
2002-09-12 22:12:51 +04:00
|
|
|
memmove(buffer + mTokenLen + 1, buffer, bufLen);
|
2002-05-24 03:46:01 +04:00
|
|
|
memcpy(buffer, token, mTokenLen);
|
|
|
|
buffer[mTokenLen] = '\n';
|
|
|
|
|
|
|
|
bufLen += (mTokenLen + 1);
|
|
|
|
|
2001-11-01 02:51:38 +03:00
|
|
|
// need to reset cursor to the buffer again (bug 100595)
|
|
|
|
cursor = buffer;
|
2001-02-27 15:33:12 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2001-05-19 02:22:15 +04:00
|
|
|
char *token = nsnull;
|
1999-08-27 18:10:21 +04:00
|
|
|
|
2000-04-15 01:27:32 +04:00
|
|
|
if (mProcessingHeaders) {
|
2001-02-27 15:33:12 +03:00
|
|
|
// we were not able to process all the headers
|
|
|
|
// for this "part" given the previous buffer given to
|
|
|
|
// us in the previous OnDataAvailable callback.
|
2000-04-15 01:27:32 +04:00
|
|
|
PRBool done = PR_FALSE;
|
2002-09-12 22:12:51 +04:00
|
|
|
rv = ParseHeaders(channel, cursor, bufLen, &done);
|
|
|
|
if (NS_FAILED(rv)) ERR_OUT
|
1999-11-11 03:34:01 +03:00
|
|
|
|
2000-04-15 01:27:32 +04:00
|
|
|
if (done) {
|
|
|
|
mProcessingHeaders = PR_FALSE;
|
2000-04-18 17:42:40 +04:00
|
|
|
rv = SendStart(channel);
|
2000-04-15 01:27:32 +04:00
|
|
|
if (NS_FAILED(rv)) ERR_OUT
|
|
|
|
}
|
|
|
|
}
|
1999-11-11 03:34:01 +03:00
|
|
|
|
2001-05-19 02:22:15 +04:00
|
|
|
PRInt32 tokenLinefeed = 1;
|
2000-04-15 01:27:32 +04:00
|
|
|
while ( (token = FindToken(cursor, bufLen)) ) {
|
|
|
|
|
|
|
|
if (*(token+mTokenLen+1) == '-') {
|
2002-09-12 22:12:51 +04:00
|
|
|
// This was the last delimiter so we can stop processing
|
2002-08-14 03:39:36 +04:00
|
|
|
rv = SendData(cursor, LengthToToken(cursor, token));
|
2002-05-24 03:46:01 +04:00
|
|
|
free(buffer);
|
2001-02-27 15:33:12 +03:00
|
|
|
if (NS_FAILED(rv)) return rv;
|
2001-04-10 10:01:08 +04:00
|
|
|
return SendStop(NS_OK);
|
1999-11-11 03:34:01 +03:00
|
|
|
}
|
1999-08-29 19:53:59 +04:00
|
|
|
|
2000-04-15 01:27:32 +04:00
|
|
|
if (!mNewPart && token > cursor) {
|
2002-09-12 22:12:51 +04:00
|
|
|
// headers are processed, we're pushing data now.
|
|
|
|
NS_ASSERTION(!mProcessingHeaders, "we should be pushing raw data");
|
2002-08-14 03:39:36 +04:00
|
|
|
rv = SendData(cursor, LengthToToken(cursor, token));
|
2000-04-15 01:27:32 +04:00
|
|
|
bufLen -= token - cursor;
|
|
|
|
if (NS_FAILED(rv)) ERR_OUT
|
|
|
|
}
|
2002-05-24 03:46:01 +04:00
|
|
|
// XXX else NS_ASSERTION(token == cursor, "?");
|
2000-04-15 01:27:32 +04:00
|
|
|
token += mTokenLen;
|
|
|
|
bufLen -= mTokenLen;
|
|
|
|
tokenLinefeed = PushOverLine(token, bufLen);
|
2000-01-15 09:12:54 +03:00
|
|
|
|
2000-04-15 01:27:32 +04:00
|
|
|
if (mNewPart) {
|
|
|
|
// parse headers
|
|
|
|
mNewPart = PR_FALSE;
|
|
|
|
cursor = token;
|
|
|
|
PRBool done = PR_FALSE;
|
2000-04-18 17:42:40 +04:00
|
|
|
rv = ParseHeaders(channel, cursor, bufLen, &done);
|
2000-04-15 01:27:32 +04:00
|
|
|
if (NS_FAILED(rv)) ERR_OUT
|
|
|
|
if (done) {
|
2000-04-18 17:42:40 +04:00
|
|
|
rv = SendStart(channel);
|
2000-04-15 01:27:32 +04:00
|
|
|
if (NS_FAILED(rv)) ERR_OUT
|
2002-05-24 03:46:01 +04:00
|
|
|
}
|
|
|
|
else {
|
2000-04-15 01:27:32 +04:00
|
|
|
// we haven't finished processing header info.
|
|
|
|
// we'll break out and try to process later.
|
|
|
|
mProcessingHeaders = PR_TRUE;
|
|
|
|
break;
|
1999-08-29 19:53:59 +04:00
|
|
|
}
|
2002-05-24 03:46:01 +04:00
|
|
|
}
|
|
|
|
else {
|
2000-04-15 01:27:32 +04:00
|
|
|
mNewPart = PR_TRUE;
|
2002-09-04 02:00:22 +04:00
|
|
|
// Reset state so we don't carry it over from part to part
|
|
|
|
mContentType.Truncate();
|
2005-01-09 23:43:35 +03:00
|
|
|
mContentLength = LL_MAXUINT;
|
2002-09-04 02:00:22 +04:00
|
|
|
mContentDisposition.Truncate();
|
|
|
|
mIsByteRangeRequest = PR_FALSE;
|
|
|
|
mByteRangeStart = 0;
|
|
|
|
mByteRangeEnd = 0;
|
|
|
|
|
2001-04-10 10:01:08 +04:00
|
|
|
rv = SendStop(NS_OK);
|
2000-04-15 01:27:32 +04:00
|
|
|
if (NS_FAILED(rv)) ERR_OUT
|
2002-05-24 03:46:01 +04:00
|
|
|
// reset the token to front. this allows us to treat
|
|
|
|
// the token as a starting token.
|
2000-04-15 01:27:32 +04:00
|
|
|
token -= mTokenLen + tokenLinefeed;
|
|
|
|
bufLen += mTokenLen + tokenLinefeed;
|
|
|
|
cursor = token;
|
1999-08-26 18:53:01 +04:00
|
|
|
}
|
2000-04-15 01:27:32 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// at this point, we want to buffer up whatever amount (bufLen)
|
|
|
|
// we have leftover. However, we *always* want to ensure that
|
|
|
|
// we buffer enough data to handle a broken token.
|
|
|
|
|
|
|
|
// carry over
|
|
|
|
PRUint32 bufAmt = 0;
|
2002-05-24 03:46:01 +04:00
|
|
|
if (mProcessingHeaders)
|
2000-04-15 01:27:32 +04:00
|
|
|
bufAmt = bufLen;
|
2002-07-28 02:59:44 +04:00
|
|
|
else if (bufLen) {
|
2001-03-03 03:59:42 +03:00
|
|
|
// if the data ends in a linefeed, and we're in the middle
|
|
|
|
// of a "part" (ie. mPartChannel exists) don't bother
|
|
|
|
// buffering, go ahead and send the data we have. Otherwise
|
|
|
|
// if we don't have a channel already, then we don't even
|
|
|
|
// have enough info to start a part, go ahead and buffer
|
|
|
|
// enough to collect a boundary token.
|
2001-04-18 09:59:43 +04:00
|
|
|
if (!mPartChannel || !(cursor[bufLen-1] == nsCRT::LF) )
|
2000-04-15 01:27:32 +04:00
|
|
|
bufAmt = PR_MIN(mTokenLen - 1, bufLen);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (bufAmt) {
|
|
|
|
rv = BufferData(cursor + (bufLen - bufAmt), bufAmt);
|
|
|
|
if (NS_FAILED(rv)) ERR_OUT
|
|
|
|
bufLen -= bufAmt;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (bufLen) {
|
2000-04-18 17:42:40 +04:00
|
|
|
rv = SendData(cursor, bufLen);
|
2000-04-15 01:27:32 +04:00
|
|
|
if (NS_FAILED(rv)) ERR_OUT
|
|
|
|
}
|
|
|
|
|
2002-05-24 03:46:01 +04:00
|
|
|
free(buffer);
|
1999-11-11 03:34:01 +03:00
|
|
|
return rv;
|
1999-08-26 18:53:01 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-04-10 10:01:08 +04:00
|
|
|
// nsIRequestObserver implementation
|
1999-08-26 18:53:01 +04:00
|
|
|
NS_IMETHODIMP
|
2001-02-21 23:38:08 +03:00
|
|
|
nsMultiMixedConv::OnStartRequest(nsIRequest *request, nsISupports *ctxt) {
|
2002-09-12 22:12:51 +04:00
|
|
|
// we're assuming the content-type is available at this stage
|
|
|
|
NS_ASSERTION(mToken.IsEmpty(), "a second on start???");
|
2002-03-21 01:50:33 +03:00
|
|
|
const char *bndry = nsnull;
|
|
|
|
nsCAutoString delimiter;
|
2000-04-15 01:27:32 +04:00
|
|
|
nsresult rv = NS_OK;
|
2000-04-18 17:42:40 +04:00
|
|
|
mContext = ctxt;
|
2000-04-15 01:27:32 +04:00
|
|
|
|
2001-02-27 15:33:12 +03:00
|
|
|
mFirstOnData = PR_TRUE;
|
2001-07-05 21:35:20 +04:00
|
|
|
mTotalSent = 0;
|
2001-02-27 15:33:12 +03:00
|
|
|
|
2001-02-21 23:38:08 +03:00
|
|
|
nsCOMPtr<nsIChannel> channel = do_QueryInterface(request, &rv);
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
2000-04-15 01:27:32 +04:00
|
|
|
// ask the HTTP channel for the content-type and extract the boundary from it.
|
2001-05-12 01:04:09 +04:00
|
|
|
nsCOMPtr<nsIHttpChannel> httpChannel = do_QueryInterface(channel, &rv);
|
2000-04-15 01:27:32 +04:00
|
|
|
if (NS_SUCCEEDED(rv)) {
|
2002-03-21 01:50:33 +03:00
|
|
|
rv = httpChannel->GetResponseHeader(NS_LITERAL_CSTRING("content-type"), delimiter);
|
2000-04-15 01:27:32 +04:00
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
} else {
|
|
|
|
// try asking the channel directly
|
2002-03-21 01:50:33 +03:00
|
|
|
rv = channel->GetContentType(delimiter);
|
|
|
|
if (NS_FAILED(rv)) return NS_ERROR_FAILURE;
|
2000-04-15 01:27:32 +04:00
|
|
|
}
|
|
|
|
|
2004-04-03 21:16:17 +04:00
|
|
|
bndry = strstr(delimiter.BeginWriting(), "boundary");
|
2000-04-15 01:27:32 +04:00
|
|
|
if (!bndry) return NS_ERROR_FAILURE;
|
|
|
|
|
2002-03-21 01:50:33 +03:00
|
|
|
bndry = strchr(bndry, '=');
|
2000-04-15 01:27:32 +04:00
|
|
|
if (!bndry) return NS_ERROR_FAILURE;
|
|
|
|
|
|
|
|
bndry++; // move past the equals sign
|
|
|
|
|
2002-03-21 01:50:33 +03:00
|
|
|
char *attrib = (char *) strchr(bndry, ';');
|
2000-04-15 01:27:32 +04:00
|
|
|
if (attrib) *attrib = '\0';
|
|
|
|
|
|
|
|
nsCAutoString boundaryString(bndry);
|
|
|
|
if (attrib) *attrib = ';';
|
2000-07-26 02:03:15 +04:00
|
|
|
|
|
|
|
boundaryString.Trim(" \"");
|
2000-04-15 01:27:32 +04:00
|
|
|
|
2002-03-21 01:50:33 +03:00
|
|
|
mToken = boundaryString;
|
2000-04-15 01:27:32 +04:00
|
|
|
mTokenLen = boundaryString.Length();
|
2001-05-19 02:22:15 +04:00
|
|
|
|
2002-03-21 01:50:33 +03:00
|
|
|
if (mTokenLen == 0)
|
2001-06-22 02:06:19 +04:00
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
|
1999-08-26 18:53:01 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2001-02-21 23:38:08 +03:00
|
|
|
nsMultiMixedConv::OnStopRequest(nsIRequest *request, nsISupports *ctxt,
|
2001-04-10 10:01:08 +04:00
|
|
|
nsresult aStatus) {
|
2001-06-22 02:06:19 +04:00
|
|
|
|
2002-03-21 01:50:33 +03:00
|
|
|
if (mToken.IsEmpty()) // no token, no love.
|
2001-06-22 02:06:19 +04:00
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
|
2001-02-27 15:33:12 +03:00
|
|
|
if (mPartChannel) {
|
2005-10-04 02:27:20 +04:00
|
|
|
mPartChannel->SetIsLastPart();
|
|
|
|
|
2001-02-27 15:33:12 +03:00
|
|
|
// we've already called SendStart() (which sets up the mPartChannel,
|
|
|
|
// and fires an OnStart()) send any data left over, and then fire the stop.
|
|
|
|
if (mBufLen > 0 && mBuffer) {
|
|
|
|
(void) SendData(mBuffer, mBufLen);
|
|
|
|
// don't bother checking the return value here, if the send failed
|
|
|
|
// we're done anyway as we're in the OnStop() callback.
|
2002-05-24 03:46:01 +04:00
|
|
|
free(mBuffer);
|
2000-07-25 09:45:56 +04:00
|
|
|
mBuffer = nsnull;
|
|
|
|
mBufLen = 0;
|
2000-04-18 17:42:40 +04:00
|
|
|
}
|
2001-04-10 10:01:08 +04:00
|
|
|
(void) SendStop(aStatus);
|
2001-02-27 15:33:12 +03:00
|
|
|
} else if (NS_FAILED(aStatus)) {
|
|
|
|
// underlying data production problem. we should not be in
|
|
|
|
// the middle of sending data. if we were, mPartChannel,
|
|
|
|
// above, would have been true.
|
2001-06-22 02:06:19 +04:00
|
|
|
|
|
|
|
// if we send the start, the URI Loader's m_targetStreamListener, may
|
|
|
|
// be pointing at us causing a nice stack overflow. So, don't call
|
|
|
|
// OnStartRequest! - This breaks necko's semantecs.
|
|
|
|
//(void) mFinalListener->OnStartRequest(request, ctxt);
|
|
|
|
|
2001-04-10 10:01:08 +04:00
|
|
|
(void) mFinalListener->OnStopRequest(request, ctxt, aStatus);
|
2000-06-02 02:47:53 +04:00
|
|
|
}
|
2001-02-27 15:33:12 +03:00
|
|
|
|
|
|
|
return NS_OK;
|
1999-08-26 18:53:01 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// nsMultiMixedConv methods
|
2005-01-15 20:47:23 +03:00
|
|
|
nsMultiMixedConv::nsMultiMixedConv() :
|
|
|
|
mCurrentPartID(0)
|
|
|
|
{
|
2001-02-27 15:33:12 +03:00
|
|
|
mTokenLen = 0;
|
1999-11-11 03:34:01 +03:00
|
|
|
mNewPart = PR_TRUE;
|
2005-01-09 23:43:35 +03:00
|
|
|
mContentLength = LL_MAXUINT;
|
2000-04-15 01:27:32 +04:00
|
|
|
mBuffer = nsnull;
|
|
|
|
mBufLen = 0;
|
|
|
|
mProcessingHeaders = PR_FALSE;
|
2001-05-19 02:22:15 +04:00
|
|
|
mByteRangeStart = 0;
|
|
|
|
mByteRangeEnd = 0;
|
2001-07-05 21:35:20 +04:00
|
|
|
mTotalSent = 0;
|
2001-05-19 02:22:15 +04:00
|
|
|
mIsByteRangeRequest = PR_FALSE;
|
1999-08-26 18:53:01 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
nsMultiMixedConv::~nsMultiMixedConv() {
|
2000-04-15 01:27:32 +04:00
|
|
|
NS_ASSERTION(!mBuffer, "all buffered data should be gone");
|
2001-05-30 02:10:50 +04:00
|
|
|
if (mBuffer) {
|
2002-05-24 03:46:01 +04:00
|
|
|
free(mBuffer);
|
2001-05-30 02:10:50 +04:00
|
|
|
mBuffer = nsnull;
|
|
|
|
}
|
1999-08-26 18:53:01 +04:00
|
|
|
}
|
|
|
|
|
2000-04-15 01:27:32 +04:00
|
|
|
nsresult
|
|
|
|
nsMultiMixedConv::BufferData(char *aData, PRUint32 aLen) {
|
|
|
|
NS_ASSERTION(!mBuffer, "trying to over-write buffer");
|
|
|
|
|
2002-05-24 03:46:01 +04:00
|
|
|
char *buffer = (char *) malloc(aLen);
|
2000-04-15 01:27:32 +04:00
|
|
|
if (!buffer) return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
|
2002-01-12 06:18:55 +03:00
|
|
|
memcpy(buffer, aData, aLen);
|
2000-04-15 01:27:32 +04:00
|
|
|
mBuffer = buffer;
|
|
|
|
mBufLen = aLen;
|
1999-08-26 18:53:01 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
nsresult
|
2000-04-18 17:42:40 +04:00
|
|
|
nsMultiMixedConv::SendStart(nsIChannel *aChannel) {
|
2001-05-30 02:10:50 +04:00
|
|
|
nsresult rv = NS_OK;
|
1999-08-26 18:53:01 +04:00
|
|
|
|
2000-04-15 01:27:32 +04:00
|
|
|
if (mContentType.IsEmpty())
|
2004-06-17 04:13:25 +04:00
|
|
|
mContentType.AssignLiteral(UNKNOWN_CONTENT_TYPE);
|
2000-04-15 01:27:32 +04:00
|
|
|
|
2001-02-27 15:33:12 +03:00
|
|
|
// if we already have an mPartChannel, that means we never sent a Stop()
|
|
|
|
// before starting up another "part." that would be bad.
|
|
|
|
NS_ASSERTION(!mPartChannel, "tisk tisk, shouldn't be overwriting a channel");
|
|
|
|
|
2001-05-30 02:10:50 +04:00
|
|
|
nsPartChannel *newChannel;
|
2005-01-15 20:47:23 +03:00
|
|
|
newChannel = new nsPartChannel(aChannel, mCurrentPartID++);
|
2001-05-30 02:10:50 +04:00
|
|
|
if (!newChannel)
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
|
|
|
|
if (mIsByteRangeRequest) {
|
|
|
|
newChannel->InitializeByteRange(mByteRangeStart, mByteRangeEnd);
|
|
|
|
}
|
|
|
|
|
2001-07-05 21:35:20 +04:00
|
|
|
mTotalSent = 0;
|
|
|
|
|
2001-05-30 02:10:50 +04:00
|
|
|
// Set up the new part channel...
|
|
|
|
mPartChannel = newChannel;
|
|
|
|
|
2002-03-21 01:50:33 +03:00
|
|
|
rv = mPartChannel->SetContentType(mContentType);
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
2005-01-09 23:43:35 +03:00
|
|
|
rv = mPartChannel->SetContentLength(mContentLength); // XXX Truncates 64-bit!
|
1999-08-26 18:53:01 +04:00
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
2005-10-04 02:27:20 +04:00
|
|
|
rv = mPartChannel->SetContentDisposition(mContentDisposition);
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
2002-02-06 06:39:04 +03:00
|
|
|
|
2000-07-08 02:02:47 +04:00
|
|
|
nsLoadFlags loadFlags = 0;
|
2001-04-10 10:01:08 +04:00
|
|
|
mPartChannel->GetLoadFlags(&loadFlags);
|
2001-04-14 06:02:19 +04:00
|
|
|
loadFlags |= nsIChannel::LOAD_REPLACE;
|
2001-04-10 10:01:08 +04:00
|
|
|
mPartChannel->SetLoadFlags(loadFlags);
|
2000-07-08 02:02:47 +04:00
|
|
|
|
2001-05-30 02:10:50 +04:00
|
|
|
nsCOMPtr<nsILoadGroup> loadGroup;
|
|
|
|
(void)mPartChannel->GetLoadGroup(getter_AddRefs(loadGroup));
|
|
|
|
|
2000-04-15 01:27:32 +04:00
|
|
|
// Add the new channel to the load group (if any)
|
|
|
|
if (loadGroup) {
|
2001-02-21 23:38:08 +03:00
|
|
|
rv = loadGroup->AddRequest(mPartChannel, nsnull);
|
2000-04-15 01:27:32 +04:00
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Let's start off the load. NOTE: we don't forward on the channel passed
|
|
|
|
// into our OnDataAvailable() as it's the root channel for the raw stream.
|
2001-05-30 02:10:50 +04:00
|
|
|
return mFinalListener->OnStartRequest(mPartChannel, mContext);
|
2000-04-15 01:27:32 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
nsresult
|
2001-04-10 10:01:08 +04:00
|
|
|
nsMultiMixedConv::SendStop(nsresult aStatus) {
|
2001-02-27 15:33:12 +03:00
|
|
|
|
|
|
|
nsresult rv = NS_OK;
|
|
|
|
if (mPartChannel) {
|
2001-05-30 02:10:50 +04:00
|
|
|
rv = mFinalListener->OnStopRequest(mPartChannel, mContext, aStatus);
|
2001-02-27 15:33:12 +03:00
|
|
|
// don't check for failure here, we need to remove the channel from
|
|
|
|
// the loadgroup.
|
|
|
|
|
|
|
|
// Remove the channel from its load group (if any)
|
|
|
|
nsCOMPtr<nsILoadGroup> loadGroup;
|
|
|
|
(void) mPartChannel->GetLoadGroup(getter_AddRefs(loadGroup));
|
|
|
|
if (loadGroup)
|
2001-04-10 10:01:08 +04:00
|
|
|
(void) loadGroup->RemoveRequest(mPartChannel, mContext, aStatus);
|
2000-04-15 01:27:32 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
mPartChannel = 0;
|
|
|
|
return rv;
|
1999-08-26 18:53:01 +04:00
|
|
|
}
|
|
|
|
|
1999-08-27 18:10:21 +04:00
|
|
|
nsresult
|
2000-04-18 17:42:40 +04:00
|
|
|
nsMultiMixedConv::SendData(char *aBuffer, PRUint32 aLen) {
|
2000-04-15 01:27:32 +04:00
|
|
|
|
|
|
|
nsresult rv = NS_OK;
|
2001-02-27 15:33:12 +03:00
|
|
|
|
|
|
|
if (!mPartChannel) return NS_ERROR_FAILURE; // something went wrong w/ processing
|
|
|
|
|
2005-01-09 23:43:35 +03:00
|
|
|
if (mContentLength != LL_MAXUINT) {
|
2001-07-05 21:35:20 +04:00
|
|
|
// make sure that we don't send more than the mContentLength
|
2002-05-24 03:46:01 +04:00
|
|
|
// XXX why? perhaps the Content-Length header was actually wrong!!
|
2005-01-09 23:43:35 +03:00
|
|
|
if ((nsUint64(aLen) + mTotalSent) > mContentLength)
|
2001-07-05 21:35:20 +04:00
|
|
|
aLen = mContentLength - mTotalSent;
|
|
|
|
|
|
|
|
if (aLen == 0)
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2002-05-24 03:46:01 +04:00
|
|
|
PRUint32 offset = mTotalSent;
|
2001-07-05 21:35:20 +04:00
|
|
|
mTotalSent += aLen;
|
|
|
|
|
2002-05-24 03:46:01 +04:00
|
|
|
nsCOMPtr<nsIStringInputStream> ss(
|
|
|
|
do_CreateInstance("@mozilla.org/io/string-input-stream;1", &rv));
|
|
|
|
if (NS_FAILED(rv))
|
2000-04-15 01:27:32 +04:00
|
|
|
return rv;
|
|
|
|
|
2002-05-24 03:46:01 +04:00
|
|
|
rv = ss->ShareData(aBuffer, aLen);
|
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return rv;
|
1999-08-27 18:10:21 +04:00
|
|
|
|
2002-05-24 03:46:01 +04:00
|
|
|
nsCOMPtr<nsIInputStream> inStream(do_QueryInterface(ss, &rv));
|
1999-08-27 18:10:21 +04:00
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
2002-05-24 03:46:01 +04:00
|
|
|
return mFinalListener->OnDataAvailable(mPartChannel, mContext, inStream, offset, aLen);
|
2000-04-15 01:27:32 +04:00
|
|
|
}
|
1999-08-27 18:10:21 +04:00
|
|
|
|
2001-05-19 02:22:15 +04:00
|
|
|
PRInt32
|
2000-04-15 01:27:32 +04:00
|
|
|
nsMultiMixedConv::PushOverLine(char *&aPtr, PRUint32 &aLen) {
|
2001-05-19 02:22:15 +04:00
|
|
|
PRInt32 chars = 0;
|
2002-05-24 03:46:01 +04:00
|
|
|
if ((aLen > 0) && (*aPtr == nsCRT::CR || *aPtr == nsCRT::LF)) {
|
|
|
|
if ((aLen > 1) && (aPtr[1] == nsCRT::LF))
|
2000-04-15 01:27:32 +04:00
|
|
|
chars++;
|
|
|
|
chars++;
|
2002-05-24 03:46:01 +04:00
|
|
|
aPtr += chars;
|
|
|
|
aLen -= chars;
|
2000-04-15 01:27:32 +04:00
|
|
|
}
|
|
|
|
return chars;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2000-04-18 17:42:40 +04:00
|
|
|
nsMultiMixedConv::ParseHeaders(nsIChannel *aChannel, char *&aPtr,
|
|
|
|
PRUint32 &aLen, PRBool *_retval) {
|
2000-04-15 01:27:32 +04:00
|
|
|
// NOTE: this data must be ascii.
|
2002-05-24 03:46:01 +04:00
|
|
|
// NOTE: aPtr is NOT null terminated!
|
2002-09-04 02:00:22 +04:00
|
|
|
nsresult rv = NS_OK;
|
2000-04-15 01:27:32 +04:00
|
|
|
char *cursor = aPtr, *newLine = nsnull;
|
|
|
|
PRUint32 cursorLen = aLen;
|
|
|
|
PRBool done = PR_FALSE;
|
2001-05-19 02:22:15 +04:00
|
|
|
PRUint32 lineFeedIncrement = 1;
|
2001-07-05 21:35:20 +04:00
|
|
|
|
2005-01-09 23:43:35 +03:00
|
|
|
mContentLength = LL_MAXUINT; // XXX what if we were already called?
|
2002-05-24 03:46:01 +04:00
|
|
|
while (cursorLen && (newLine = (char *) memchr(cursor, nsCRT::LF, cursorLen))) {
|
2000-04-15 01:27:32 +04:00
|
|
|
// adjust for linefeeds
|
2002-05-24 03:46:01 +04:00
|
|
|
if ((newLine > cursor) && (newLine[-1] == nsCRT::CR) ) { // CRLF
|
|
|
|
lineFeedIncrement = 2;
|
|
|
|
newLine--;
|
2000-04-15 01:27:32 +04:00
|
|
|
}
|
2002-05-24 03:46:01 +04:00
|
|
|
else
|
|
|
|
lineFeedIncrement = 1; // reset
|
2000-04-15 01:27:32 +04:00
|
|
|
|
|
|
|
if (newLine == cursor) {
|
2002-05-24 03:46:01 +04:00
|
|
|
// move the newLine beyond the linefeed marker
|
|
|
|
NS_ASSERTION(cursorLen >= lineFeedIncrement, "oops!");
|
|
|
|
|
|
|
|
cursor += lineFeedIncrement;
|
|
|
|
cursorLen -= lineFeedIncrement;
|
2000-04-15 01:27:32 +04:00
|
|
|
|
|
|
|
done = PR_TRUE;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
char tmpChar = *newLine;
|
2002-05-24 03:46:01 +04:00
|
|
|
*newLine = '\0'; // cursor is now null terminated
|
|
|
|
char *colon = (char *) strchr(cursor, ':');
|
2000-04-15 01:27:32 +04:00
|
|
|
if (colon) {
|
|
|
|
*colon = '\0';
|
|
|
|
nsCAutoString headerStr(cursor);
|
2000-05-04 01:14:39 +04:00
|
|
|
headerStr.CompressWhitespace();
|
2000-04-15 01:27:32 +04:00
|
|
|
*colon = ':';
|
|
|
|
|
|
|
|
nsCAutoString headerVal(colon + 1);
|
2000-05-04 01:14:39 +04:00
|
|
|
headerVal.CompressWhitespace();
|
2000-04-15 01:27:32 +04:00
|
|
|
|
|
|
|
// examine header
|
2004-06-17 04:13:25 +04:00
|
|
|
if (headerStr.LowerCaseEqualsLiteral("content-type")) {
|
2002-04-23 03:55:30 +04:00
|
|
|
mContentType = headerVal;
|
2004-06-17 04:13:25 +04:00
|
|
|
} else if (headerStr.LowerCaseEqualsLiteral("content-length")) {
|
2005-01-09 23:43:35 +03:00
|
|
|
mContentLength = atoi(headerVal.get()); // XXX 64-bit math?
|
2004-06-17 04:13:25 +04:00
|
|
|
} else if (headerStr.LowerCaseEqualsLiteral("content-disposition")) {
|
2002-02-06 06:39:04 +03:00
|
|
|
mContentDisposition = headerVal;
|
2004-06-17 04:13:25 +04:00
|
|
|
} else if (headerStr.LowerCaseEqualsLiteral("set-cookie")) {
|
2003-08-05 05:44:27 +04:00
|
|
|
nsCOMPtr<nsIHttpChannelInternal> httpInternal =
|
|
|
|
do_QueryInterface(aChannel);
|
|
|
|
if (httpInternal) {
|
|
|
|
httpInternal->SetCookie(headerVal.get());
|
2000-04-15 01:27:32 +04:00
|
|
|
}
|
2004-06-17 04:13:25 +04:00
|
|
|
} else if (headerStr.LowerCaseEqualsLiteral("content-range") ||
|
|
|
|
headerStr.LowerCaseEqualsLiteral("range") ) {
|
2001-05-19 02:22:15 +04:00
|
|
|
// something like: Content-range: bytes 7000-7999/8000
|
|
|
|
char* tmpPtr;
|
|
|
|
|
2002-05-24 03:46:01 +04:00
|
|
|
tmpPtr = (char *) strchr(colon + 1, '/');
|
2001-05-19 02:22:15 +04:00
|
|
|
if (tmpPtr)
|
|
|
|
*tmpPtr = '\0';
|
|
|
|
|
|
|
|
// pass the bytes-unit and the SP
|
2002-05-24 03:46:01 +04:00
|
|
|
char *range = (char *) strchr(colon + 2, ' ');
|
2001-05-19 02:22:15 +04:00
|
|
|
|
|
|
|
if (!range)
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
|
|
|
|
if (range[0] == '*'){
|
|
|
|
mByteRangeStart = mByteRangeEnd = 0;
|
|
|
|
}
|
|
|
|
else {
|
2002-05-24 03:46:01 +04:00
|
|
|
tmpPtr = (char *) strchr(range, '-');
|
2001-05-19 02:22:15 +04:00
|
|
|
if (!tmpPtr)
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
|
|
|
|
tmpPtr[0] = '\0';
|
|
|
|
|
2005-01-09 23:43:35 +03:00
|
|
|
mByteRangeStart = atoi(range); // XXX want 64-bit conv
|
2001-05-19 02:22:15 +04:00
|
|
|
tmpPtr++;
|
|
|
|
mByteRangeEnd = atoi(tmpPtr);
|
|
|
|
}
|
|
|
|
|
|
|
|
mIsByteRangeRequest = PR_TRUE;
|
2005-01-09 23:43:35 +03:00
|
|
|
if (mContentLength == LL_MAXUINT)
|
|
|
|
mContentLength = PRUint64(PRInt64(mByteRangeEnd - mByteRangeStart + nsInt64(1)));
|
2000-04-15 01:27:32 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
*newLine = tmpChar;
|
|
|
|
newLine += lineFeedIncrement;
|
|
|
|
cursorLen -= (newLine - cursor);
|
|
|
|
cursor = newLine;
|
2002-05-24 03:46:01 +04:00
|
|
|
}
|
2000-04-15 01:27:32 +04:00
|
|
|
|
|
|
|
aPtr = cursor;
|
|
|
|
aLen = cursorLen;
|
|
|
|
|
2002-09-12 22:12:51 +04:00
|
|
|
*_retval = done;
|
2000-04-15 01:27:32 +04:00
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
char *
|
|
|
|
nsMultiMixedConv::FindToken(char *aCursor, PRUint32 aLen) {
|
|
|
|
// strnstr without looking for null termination
|
2002-03-21 01:50:33 +03:00
|
|
|
const char *token = mToken.get();
|
2000-05-13 00:56:47 +04:00
|
|
|
char *cur = aCursor;
|
2000-04-15 01:27:32 +04:00
|
|
|
|
2002-05-24 03:46:01 +04:00
|
|
|
if (!(token && aCursor && *token)) {
|
|
|
|
NS_WARNING("bad data");
|
|
|
|
return nsnull;
|
|
|
|
}
|
2000-05-13 00:56:47 +04:00
|
|
|
|
2002-05-24 03:46:01 +04:00
|
|
|
for (; aLen >= mTokenLen; aCursor++, aLen--) {
|
|
|
|
if (!memcmp(aCursor, token, mTokenLen) ) {
|
|
|
|
if ((aCursor - cur) >= 2) {
|
|
|
|
// back the cursor up over a double dash for backwards compat.
|
|
|
|
if ((*(aCursor-1) == '-') && (*(aCursor-2) == '-')) {
|
|
|
|
aCursor -= 2;
|
|
|
|
aLen += 2;
|
|
|
|
|
|
|
|
// we're playing w/ double dash tokens, adjust.
|
|
|
|
mToken.Assign(aCursor, mTokenLen + 2);
|
|
|
|
mTokenLen = mToken.Length();
|
2000-05-13 00:56:47 +04:00
|
|
|
}
|
|
|
|
}
|
2002-05-24 03:46:01 +04:00
|
|
|
return aCursor;
|
|
|
|
}
|
|
|
|
}
|
1999-11-11 03:34:01 +03:00
|
|
|
|
2000-04-15 01:27:32 +04:00
|
|
|
return nsnull;
|
1999-08-27 18:10:21 +04:00
|
|
|
}
|
|
|
|
|
1999-10-14 17:36:33 +04:00
|
|
|
nsresult
|
|
|
|
NS_NewMultiMixedConv(nsMultiMixedConv** aMultiMixedConv)
|
1999-08-26 18:53:01 +04:00
|
|
|
{
|
1999-10-14 17:36:33 +04:00
|
|
|
NS_PRECONDITION(aMultiMixedConv != nsnull, "null ptr");
|
|
|
|
if (! aMultiMixedConv)
|
1999-08-26 18:53:01 +04:00
|
|
|
return NS_ERROR_NULL_POINTER;
|
|
|
|
|
1999-10-14 17:36:33 +04:00
|
|
|
*aMultiMixedConv = new nsMultiMixedConv();
|
|
|
|
if (! *aMultiMixedConv)
|
1999-08-26 18:53:01 +04:00
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
|
1999-10-14 17:36:33 +04:00
|
|
|
NS_ADDREF(*aMultiMixedConv);
|
2001-02-27 15:33:12 +03:00
|
|
|
return NS_OK;
|
1999-08-26 18:53:01 +04:00
|
|
|
}
|
2001-02-21 23:38:08 +03:00
|
|
|
|