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
4 : *
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/
9 : *
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.
14 : *
15 : * The Original Code is mozilla.org code.
16 : *
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.
21 : *
22 : * Contributor(s):
23 : *
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.
35 : *
36 : * ***** END LICENSE BLOCK ***** */
37 :
38 : // data implementation
39 :
40 : #include "nsIOService.h"
41 : #include "nsDataChannel.h"
42 : #include "nsDataHandler.h"
43 : #include "nsNetUtil.h"
44 : #include "nsIPipe.h"
45 : #include "nsIInputStream.h"
46 : #include "nsIOutputStream.h"
47 : #include "nsReadableUtils.h"
48 : #include "nsEscape.h"
49 : #include "plbase64.h"
50 : #include "plstr.h"
51 : #include "prmem.h"
52 :
53 : nsresult
54 400 : nsDataChannel::OpenContentStream(bool async, nsIInputStream **result,
55 : nsIChannel** channel)
56 : {
57 400 : NS_ENSURE_TRUE(URI(), NS_ERROR_NOT_INITIALIZED);
58 :
59 : nsresult rv;
60 :
61 800 : nsCAutoString spec;
62 400 : rv = URI()->GetAsciiSpec(spec);
63 400 : if (NS_FAILED(rv)) return rv;
64 :
65 800 : nsCString contentType, contentCharset, dataBuffer, hashRef;
66 : bool lBase64;
67 : rv = nsDataHandler::ParseURI(spec, contentType, contentCharset,
68 400 : lBase64, dataBuffer, hashRef);
69 :
70 400 : NS_UnescapeURL(dataBuffer);
71 :
72 400 : 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 107 : dataBuffer.StripWhitespace();
77 : }
78 :
79 800 : nsCOMPtr<nsIInputStream> bufInStream;
80 800 : nsCOMPtr<nsIOutputStream> bufOutStream;
81 :
82 : // create an unbounded pipe.
83 400 : rv = NS_NewPipe(getter_AddRefs(bufInStream),
84 400 : getter_AddRefs(bufOutStream),
85 : nsIOService::gDefaultSegmentSize,
86 : PR_UINT32_MAX,
87 400 : async, true);
88 400 : if (NS_FAILED(rv))
89 0 : return rv;
90 :
91 : PRUint32 contentLen;
92 400 : if (lBase64) {
93 107 : const PRUint32 dataLen = dataBuffer.Length();
94 107 : PRInt32 resultLen = 0;
95 107 : if (dataLen >= 1 && dataBuffer[dataLen-1] == '=') {
96 100 : if (dataLen >= 2 && dataBuffer[dataLen-2] == '=')
97 89 : resultLen = dataLen-2;
98 : else
99 11 : resultLen = dataLen-1;
100 : } else {
101 7 : resultLen = dataLen;
102 : }
103 107 : resultLen = ((resultLen * 3) / 4);
104 :
105 : // XXX PL_Base64Decode will return a null pointer for decoding
106 : // errors. Since those are more likely than out-of-memory,
107 : // should we return NS_ERROR_MALFORMED_URI instead?
108 107 : char * decodedData = PL_Base64Decode(dataBuffer.get(), dataLen, nsnull);
109 107 : if (!decodedData) {
110 0 : return NS_ERROR_OUT_OF_MEMORY;
111 : }
112 :
113 107 : rv = bufOutStream->Write(decodedData, resultLen, &contentLen);
114 :
115 107 : PR_Free(decodedData);
116 : } else {
117 293 : rv = bufOutStream->Write(dataBuffer.get(), dataBuffer.Length(), &contentLen);
118 : }
119 400 : if (NS_FAILED(rv))
120 0 : return rv;
121 :
122 400 : SetContentType(contentType);
123 400 : SetContentCharset(contentCharset);
124 400 : SetContentLength64(contentLen);
125 :
126 400 : NS_ADDREF(*result = bufInStream);
127 :
128 400 : return NS_OK;
129 : }
|