Bug 470455 - test_database_sync_embed_visits.js leaks, r=sdwilsh
[wine-gecko.git] / netwerk / protocol / data / src / nsDataChannel.cpp
blobfa63c9fb785062b66d165fcc5d21e7f2dd790f55
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /* ***** BEGIN LICENSE BLOCK *****
3 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
5 * The contents of this file are subject to the Mozilla Public License Version
6 * 1.1 (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 * http://www.mozilla.org/MPL/
10 * Software distributed under the License is distributed on an "AS IS" basis,
11 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 * for the specific language governing rights and limitations under the
13 * License.
15 * The Original Code is mozilla.org code.
17 * The Initial Developer of the Original Code is
18 * Netscape Communications Corporation.
19 * Portions created by the Initial Developer are Copyright (C) 1998
20 * the Initial Developer. All Rights Reserved.
22 * Contributor(s):
24 * Alternatively, the contents of this file may be used under the terms of
25 * either the GNU General Public License Version 2 or later (the "GPL"), or
26 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
27 * in which case the provisions of the GPL or the LGPL are applicable instead
28 * of those above. If you wish to allow use of your version of this file only
29 * under the terms of either the GPL or the LGPL, and not to allow others to
30 * use your version of this file under the terms of the MPL, indicate your
31 * decision by deleting the provisions above and replace them with the notice
32 * and other provisions required by the GPL or the LGPL. If you do not delete
33 * the provisions above, a recipient may use your version of this file under
34 * the terms of any one of the MPL, the GPL or the LGPL.
36 * ***** END LICENSE BLOCK ***** */
38 // data implementation
40 #include "nsDataChannel.h"
41 #include "nsDataHandler.h"
42 #include "nsNetUtil.h"
43 #include "nsIPipe.h"
44 #include "nsIInputStream.h"
45 #include "nsIOutputStream.h"
46 #include "nsReadableUtils.h"
47 #include "nsNetSegmentUtils.h"
48 #include "nsEscape.h"
49 #include "plbase64.h"
50 #include "plstr.h"
51 #include "prmem.h"
53 nsresult
54 nsDataChannel::OpenContentStream(PRBool async, nsIInputStream **result,
55 nsIChannel** channel)
57 NS_ENSURE_TRUE(URI(), NS_ERROR_NOT_INITIALIZED);
59 nsresult rv;
61 nsCAutoString spec;
62 rv = URI()->GetAsciiSpec(spec);
63 if (NS_FAILED(rv)) return rv;
65 nsCString contentType, contentCharset, dataBuffer;
66 PRBool lBase64;
67 rv = nsDataHandler::ParseURI(spec, contentType, contentCharset,
68 lBase64, dataBuffer);
70 NS_UnescapeURL(dataBuffer);
72 if (lBase64) {
73 // Don't allow spaces in base64-encoded content. This is only
74 // relevant for escaped spaces; other spaces are stripped in
75 // NewURI.
76 dataBuffer.StripWhitespace();
79 nsCOMPtr<nsIInputStream> bufInStream;
80 nsCOMPtr<nsIOutputStream> bufOutStream;
82 // create an unbounded pipe.
83 rv = NS_NewPipe(getter_AddRefs(bufInStream),
84 getter_AddRefs(bufOutStream),
85 NET_DEFAULT_SEGMENT_SIZE, PR_UINT32_MAX,
86 async, PR_TRUE);
87 if (NS_FAILED(rv))
88 return rv;
90 PRUint32 contentLen;
91 if (lBase64) {
92 const PRUint32 dataLen = dataBuffer.Length();
93 PRInt32 resultLen = 0;
94 if (dataLen >= 1 && dataBuffer[dataLen-1] == '=') {
95 if (dataLen >= 2 && dataBuffer[dataLen-2] == '=')
96 resultLen = dataLen-2;
97 else
98 resultLen = dataLen-1;
99 } else {
100 resultLen = dataLen;
102 resultLen = ((resultLen * 3) / 4);
104 // XXX PL_Base64Decode will return a null pointer for decoding
105 // errors. Since those are more likely than out-of-memory,
106 // should we return NS_ERROR_MALFORMED_URI instead?
107 char * decodedData = PL_Base64Decode(dataBuffer.get(), dataLen, nsnull);
108 if (!decodedData) {
109 return NS_ERROR_OUT_OF_MEMORY;
112 rv = bufOutStream->Write(decodedData, resultLen, &contentLen);
114 PR_Free(decodedData);
115 } else {
116 rv = bufOutStream->Write(dataBuffer.get(), dataBuffer.Length(), &contentLen);
118 if (NS_FAILED(rv))
119 return rv;
121 SetContentType(contentType);
122 SetContentCharset(contentCharset);
123 SetContentLength64(contentLen);
125 NS_ADDREF(*result = bufInStream);
127 return NS_OK;