2001-09-28 20:14:13 +00:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
|
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-09-03 00:39:55 +00:00
|
|
|
|
|
|
|
// data implementation
|
|
|
|
|
2012-12-08 10:50:11 +00:00
|
|
|
#include "nsDataChannel.h"
|
2012-12-10 08:19:02 +00:00
|
|
|
|
|
|
|
#include "mozilla/Base64.h"
|
|
|
|
#include "nsIOService.h"
|
2007-08-21 17:28:22 +00:00
|
|
|
#include "nsDataHandler.h"
|
1999-09-03 00:39:55 +00:00
|
|
|
#include "nsIPipe.h"
|
2000-08-22 07:03:33 +00:00
|
|
|
#include "nsIInputStream.h"
|
|
|
|
#include "nsIOutputStream.h"
|
2002-04-20 06:54:02 +00:00
|
|
|
#include "nsEscape.h"
|
2012-12-10 08:19:02 +00:00
|
|
|
|
|
|
|
using namespace mozilla;
|
1999-09-03 00:39:55 +00:00
|
|
|
|
|
|
|
nsresult
|
2011-09-29 06:19:26 +00:00
|
|
|
nsDataChannel::OpenContentStream(bool async, nsIInputStream **result,
|
2008-09-29 21:02:44 +00:00
|
|
|
nsIChannel** channel)
|
1999-10-26 09:16:24 +00:00
|
|
|
{
|
2005-11-12 18:17:19 +00:00
|
|
|
NS_ENSURE_TRUE(URI(), NS_ERROR_NOT_INITIALIZED);
|
1999-09-03 00:39:55 +00:00
|
|
|
|
|
|
|
nsresult rv;
|
|
|
|
|
2012-09-02 02:35:17 +00:00
|
|
|
nsAutoCString spec;
|
2005-11-12 18:17:19 +00:00
|
|
|
rv = URI()->GetAsciiSpec(spec);
|
1999-09-03 00:39:55 +00:00
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
2011-05-24 15:40:10 +00:00
|
|
|
nsCString contentType, contentCharset, dataBuffer, hashRef;
|
2011-09-29 06:19:26 +00:00
|
|
|
bool lBase64;
|
2007-08-21 17:28:22 +00:00
|
|
|
rv = nsDataHandler::ParseURI(spec, contentType, contentCharset,
|
2011-05-24 15:40:10 +00:00
|
|
|
lBase64, dataBuffer, hashRef);
|
2000-04-27 03:55:20 +00:00
|
|
|
|
2007-08-10 19:52:50 +00:00
|
|
|
NS_UnescapeURL(dataBuffer);
|
|
|
|
|
2007-08-21 17:28:22 +00:00
|
|
|
if (lBase64) {
|
|
|
|
// Don't allow spaces in base64-encoded content. This is only
|
|
|
|
// relevant for escaped spaces; other spaces are stripped in
|
|
|
|
// NewURI.
|
2007-08-10 19:52:50 +00:00
|
|
|
dataBuffer.StripWhitespace();
|
2000-04-27 03:55:20 +00:00
|
|
|
}
|
1999-09-03 00:39:55 +00:00
|
|
|
|
2000-08-22 07:03:33 +00:00
|
|
|
nsCOMPtr<nsIInputStream> bufInStream;
|
|
|
|
nsCOMPtr<nsIOutputStream> bufOutStream;
|
2002-04-19 22:37:41 +00:00
|
|
|
|
2003-02-17 01:37:28 +00:00
|
|
|
// create an unbounded pipe.
|
|
|
|
rv = NS_NewPipe(getter_AddRefs(bufInStream),
|
|
|
|
getter_AddRefs(bufOutStream),
|
2010-04-20 16:32:28 +00:00
|
|
|
nsIOService::gDefaultSegmentSize,
|
2012-09-28 06:57:33 +00:00
|
|
|
UINT32_MAX,
|
2011-10-17 14:59:28 +00:00
|
|
|
async, true);
|
2002-04-19 22:37:41 +00:00
|
|
|
if (NS_FAILED(rv))
|
2007-08-10 19:52:50 +00:00
|
|
|
return rv;
|
1999-09-03 00:39:55 +00:00
|
|
|
|
2012-08-22 15:56:38 +00:00
|
|
|
uint32_t contentLen;
|
1999-09-03 00:39:55 +00:00
|
|
|
if (lBase64) {
|
2012-08-22 15:56:38 +00:00
|
|
|
const uint32_t dataLen = dataBuffer.Length();
|
|
|
|
int32_t resultLen = 0;
|
2006-01-19 19:04:27 +00:00
|
|
|
if (dataLen >= 1 && dataBuffer[dataLen-1] == '=') {
|
|
|
|
if (dataLen >= 2 && dataBuffer[dataLen-2] == '=')
|
2000-03-15 23:20:38 +00:00
|
|
|
resultLen = dataLen-2;
|
|
|
|
else
|
|
|
|
resultLen = dataLen-1;
|
2000-04-18 13:39:59 +00:00
|
|
|
} else {
|
|
|
|
resultLen = dataLen;
|
|
|
|
}
|
2001-05-15 12:45:50 +00:00
|
|
|
resultLen = ((resultLen * 3) / 4);
|
1999-09-03 00:39:55 +00:00
|
|
|
|
2012-12-10 08:19:02 +00:00
|
|
|
nsAutoCString decodedData;
|
|
|
|
rv = Base64Decode(dataBuffer, decodedData);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
rv = bufOutStream->Write(decodedData.get(), resultLen, &contentLen);
|
1999-09-03 00:39:55 +00:00
|
|
|
} else {
|
2007-08-10 19:52:50 +00:00
|
|
|
rv = bufOutStream->Write(dataBuffer.get(), dataBuffer.Length(), &contentLen);
|
1999-09-03 00:39:55 +00:00
|
|
|
}
|
2002-04-19 22:37:41 +00:00
|
|
|
if (NS_FAILED(rv))
|
2007-08-10 19:52:50 +00:00
|
|
|
return rv;
|
1999-09-03 00:39:55 +00:00
|
|
|
|
2005-11-12 18:17:19 +00:00
|
|
|
SetContentType(contentType);
|
|
|
|
SetContentCharset(contentCharset);
|
2012-10-22 17:51:07 +00:00
|
|
|
mContentLength = contentLen;
|
2002-04-19 22:37:41 +00:00
|
|
|
|
2005-11-12 18:17:19 +00:00
|
|
|
NS_ADDREF(*result = bufInStream);
|
|
|
|
|
2007-08-10 19:52:50 +00:00
|
|
|
return NS_OK;
|
1999-09-03 00:39:55 +00:00
|
|
|
}
|