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
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.
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"
44 #include "nsIInputStream.h"
45 #include "nsIOutputStream.h"
46 #include "nsReadableUtils.h"
47 #include "nsNetSegmentUtils.h"
54 nsDataChannel::OpenContentStream(PRBool async
, nsIInputStream
**result
,
57 NS_ENSURE_TRUE(URI(), NS_ERROR_NOT_INITIALIZED
);
62 rv
= URI()->GetAsciiSpec(spec
);
63 if (NS_FAILED(rv
)) return rv
;
65 nsCString contentType
, contentCharset
, dataBuffer
;
67 rv
= nsDataHandler::ParseURI(spec
, contentType
, contentCharset
,
70 NS_UnescapeURL(dataBuffer
);
73 // Don't allow spaces in base64-encoded content. This is only
74 // relevant for escaped spaces; other spaces are stripped in
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
,
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;
98 resultLen
= dataLen
-1;
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
);
109 return NS_ERROR_OUT_OF_MEMORY
;
112 rv
= bufOutStream
->Write(decodedData
, resultLen
, &contentLen
);
114 PR_Free(decodedData
);
116 rv
= bufOutStream
->Write(dataBuffer
.get(), dataBuffer
.Length(), &contentLen
);
121 SetContentType(contentType
);
122 SetContentCharset(contentCharset
);
123 SetContentLength64(contentLen
);
125 NS_ADDREF(*result
= bufInStream
);