netwerk/protocol/data/nsDataChannel.cpp

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
michael@0 2 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 3 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 5
michael@0 6 // data implementation
michael@0 7
michael@0 8 #include "nsDataChannel.h"
michael@0 9
michael@0 10 #include "mozilla/Base64.h"
michael@0 11 #include "nsIOService.h"
michael@0 12 #include "nsDataHandler.h"
michael@0 13 #include "nsIPipe.h"
michael@0 14 #include "nsIInputStream.h"
michael@0 15 #include "nsIOutputStream.h"
michael@0 16 #include "nsEscape.h"
michael@0 17
michael@0 18 using namespace mozilla;
michael@0 19
michael@0 20 nsresult
michael@0 21 nsDataChannel::OpenContentStream(bool async, nsIInputStream **result,
michael@0 22 nsIChannel** channel)
michael@0 23 {
michael@0 24 NS_ENSURE_TRUE(URI(), NS_ERROR_NOT_INITIALIZED);
michael@0 25
michael@0 26 nsresult rv;
michael@0 27
michael@0 28 nsAutoCString spec;
michael@0 29 rv = URI()->GetAsciiSpec(spec);
michael@0 30 if (NS_FAILED(rv)) return rv;
michael@0 31
michael@0 32 nsCString contentType, contentCharset, dataBuffer, hashRef;
michael@0 33 bool lBase64;
michael@0 34 rv = nsDataHandler::ParseURI(spec, contentType, contentCharset,
michael@0 35 lBase64, dataBuffer, hashRef);
michael@0 36
michael@0 37 NS_UnescapeURL(dataBuffer);
michael@0 38
michael@0 39 if (lBase64) {
michael@0 40 // Don't allow spaces in base64-encoded content. This is only
michael@0 41 // relevant for escaped spaces; other spaces are stripped in
michael@0 42 // NewURI.
michael@0 43 dataBuffer.StripWhitespace();
michael@0 44 }
michael@0 45
michael@0 46 nsCOMPtr<nsIInputStream> bufInStream;
michael@0 47 nsCOMPtr<nsIOutputStream> bufOutStream;
michael@0 48
michael@0 49 // create an unbounded pipe.
michael@0 50 rv = NS_NewPipe(getter_AddRefs(bufInStream),
michael@0 51 getter_AddRefs(bufOutStream),
michael@0 52 nsIOService::gDefaultSegmentSize,
michael@0 53 UINT32_MAX,
michael@0 54 async, true);
michael@0 55 if (NS_FAILED(rv))
michael@0 56 return rv;
michael@0 57
michael@0 58 uint32_t contentLen;
michael@0 59 if (lBase64) {
michael@0 60 const uint32_t dataLen = dataBuffer.Length();
michael@0 61 int32_t resultLen = 0;
michael@0 62 if (dataLen >= 1 && dataBuffer[dataLen-1] == '=') {
michael@0 63 if (dataLen >= 2 && dataBuffer[dataLen-2] == '=')
michael@0 64 resultLen = dataLen-2;
michael@0 65 else
michael@0 66 resultLen = dataLen-1;
michael@0 67 } else {
michael@0 68 resultLen = dataLen;
michael@0 69 }
michael@0 70 resultLen = ((resultLen * 3) / 4);
michael@0 71
michael@0 72 nsAutoCString decodedData;
michael@0 73 rv = Base64Decode(dataBuffer, decodedData);
michael@0 74 NS_ENSURE_SUCCESS(rv, rv);
michael@0 75 rv = bufOutStream->Write(decodedData.get(), resultLen, &contentLen);
michael@0 76 } else {
michael@0 77 rv = bufOutStream->Write(dataBuffer.get(), dataBuffer.Length(), &contentLen);
michael@0 78 }
michael@0 79 if (NS_FAILED(rv))
michael@0 80 return rv;
michael@0 81
michael@0 82 SetContentType(contentType);
michael@0 83 SetContentCharset(contentCharset);
michael@0 84 mContentLength = contentLen;
michael@0 85
michael@0 86 NS_ADDREF(*result = bufInStream);
michael@0 87
michael@0 88 return NS_OK;
michael@0 89 }

mercurial