michael@0: /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: // data implementation michael@0: michael@0: #include "nsDataChannel.h" michael@0: michael@0: #include "mozilla/Base64.h" michael@0: #include "nsIOService.h" michael@0: #include "nsDataHandler.h" michael@0: #include "nsIPipe.h" michael@0: #include "nsIInputStream.h" michael@0: #include "nsIOutputStream.h" michael@0: #include "nsEscape.h" michael@0: michael@0: using namespace mozilla; michael@0: michael@0: nsresult michael@0: nsDataChannel::OpenContentStream(bool async, nsIInputStream **result, michael@0: nsIChannel** channel) michael@0: { michael@0: NS_ENSURE_TRUE(URI(), NS_ERROR_NOT_INITIALIZED); michael@0: michael@0: nsresult rv; michael@0: michael@0: nsAutoCString spec; michael@0: rv = URI()->GetAsciiSpec(spec); michael@0: if (NS_FAILED(rv)) return rv; michael@0: michael@0: nsCString contentType, contentCharset, dataBuffer, hashRef; michael@0: bool lBase64; michael@0: rv = nsDataHandler::ParseURI(spec, contentType, contentCharset, michael@0: lBase64, dataBuffer, hashRef); michael@0: michael@0: NS_UnescapeURL(dataBuffer); michael@0: michael@0: if (lBase64) { michael@0: // Don't allow spaces in base64-encoded content. This is only michael@0: // relevant for escaped spaces; other spaces are stripped in michael@0: // NewURI. michael@0: dataBuffer.StripWhitespace(); michael@0: } michael@0: michael@0: nsCOMPtr bufInStream; michael@0: nsCOMPtr bufOutStream; michael@0: michael@0: // create an unbounded pipe. michael@0: rv = NS_NewPipe(getter_AddRefs(bufInStream), michael@0: getter_AddRefs(bufOutStream), michael@0: nsIOService::gDefaultSegmentSize, michael@0: UINT32_MAX, michael@0: async, true); michael@0: if (NS_FAILED(rv)) michael@0: return rv; michael@0: michael@0: uint32_t contentLen; michael@0: if (lBase64) { michael@0: const uint32_t dataLen = dataBuffer.Length(); michael@0: int32_t resultLen = 0; michael@0: if (dataLen >= 1 && dataBuffer[dataLen-1] == '=') { michael@0: if (dataLen >= 2 && dataBuffer[dataLen-2] == '=') michael@0: resultLen = dataLen-2; michael@0: else michael@0: resultLen = dataLen-1; michael@0: } else { michael@0: resultLen = dataLen; michael@0: } michael@0: resultLen = ((resultLen * 3) / 4); michael@0: michael@0: nsAutoCString decodedData; michael@0: rv = Base64Decode(dataBuffer, decodedData); michael@0: NS_ENSURE_SUCCESS(rv, rv); michael@0: rv = bufOutStream->Write(decodedData.get(), resultLen, &contentLen); michael@0: } else { michael@0: rv = bufOutStream->Write(dataBuffer.get(), dataBuffer.Length(), &contentLen); michael@0: } michael@0: if (NS_FAILED(rv)) michael@0: return rv; michael@0: michael@0: SetContentType(contentType); michael@0: SetContentCharset(contentCharset); michael@0: mContentLength = contentLen; michael@0: michael@0: NS_ADDREF(*result = bufInStream); michael@0: michael@0: return NS_OK; michael@0: }