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 ***** */
39 #include "nsDataChannel.h"
40 #include "nsDataHandler.h"
43 #include "nsIComponentManager.h"
44 #include "nsIServiceManager.h"
45 #include "nsIInterfaceRequestor.h"
46 #include "nsIInterfaceRequestorUtils.h"
47 #include "nsIProgressEventSink.h"
49 #include "nsNetError.h"
51 static NS_DEFINE_CID(kSimpleURICID
, NS_SIMPLEURI_CID
);
53 ////////////////////////////////////////////////////////////////////////////////
55 nsDataHandler::nsDataHandler() {
58 nsDataHandler::~nsDataHandler() {
61 NS_IMPL_ISUPPORTS1(nsDataHandler
, nsIProtocolHandler
)
64 nsDataHandler::Create(nsISupports
* aOuter
, const nsIID
& aIID
, void* *aResult
) {
66 nsDataHandler
* ph
= new nsDataHandler();
68 return NS_ERROR_OUT_OF_MEMORY
;
70 nsresult rv
= ph
->QueryInterface(aIID
, aResult
);
75 ////////////////////////////////////////////////////////////////////////////////
76 // nsIProtocolHandler methods:
79 nsDataHandler::GetScheme(nsACString
&result
) {
80 result
.AssignLiteral("data");
85 nsDataHandler::GetDefaultPort(PRInt32
*result
) {
86 // no ports for data protocol
92 nsDataHandler::GetProtocolFlags(PRUint32
*result
) {
93 *result
= URI_NORELATIVE
| URI_NOAUTH
| URI_INHERITS_SECURITY_CONTEXT
|
94 URI_LOADABLE_BY_ANYONE
| URI_NON_PERSISTABLE
;
99 nsDataHandler::NewURI(const nsACString
&aSpec
,
100 const char *aCharset
, // ignore charset info
105 nsCString
spec(aSpec
);
106 nsCAutoString contentType
, contentCharset
, dataBuffer
;
108 rv
= ParseURI(spec
, contentType
, contentCharset
, base64
, dataBuffer
);
112 // Strip whitespace unless this is text, where whitespace is important
113 // Don't strip escaped whitespace though (bug 391951)
114 if (base64
|| (strncmp(contentType
.get(),"text/",5) != 0 &&
115 contentType
.Find("xml") == kNotFound
)) {
116 // it's ascii encoded binary, don't let any spaces in
117 spec
.StripWhitespace();
122 rv
= CallCreateInstance(kSimpleURICID
, &url
);
123 if (NS_FAILED(rv
)) return rv
;
125 rv
= url
->SetSpec(spec
);
136 nsDataHandler::NewChannel(nsIURI
* uri
, nsIChannel
* *result
) {
137 NS_ENSURE_ARG_POINTER(uri
);
138 nsDataChannel
* channel
= new nsDataChannel(uri
);
140 return NS_ERROR_OUT_OF_MEMORY
;
143 nsresult rv
= channel
->Init();
154 nsDataHandler::AllowPort(PRInt32 port
, const char *scheme
, PRBool
*_retval
) {
155 // don't override anything.
161 nsDataHandler::ParseURI(nsCString
& spec
,
162 nsCString
& contentType
,
163 nsCString
& contentCharset
,
165 nsCString
& dataBuffer
) {
169 char *buffer
= (char *) strstr(spec
.BeginWriting(), "data:");
172 return NS_ERROR_MALFORMED_URI
;
176 // First, find the start of the data
177 char *comma
= strchr(buffer
, ',');
179 return NS_ERROR_MALFORMED_URI
;
183 // determine if the data is base64 encoded.
184 char *base64
= strstr(buffer
, ";base64");
190 if (comma
== buffer
) {
192 contentType
.AssignLiteral("text/plain");
193 contentCharset
.AssignLiteral("US-ASCII");
195 // everything else is content type
196 char *semiColon
= (char *) strchr(buffer
, ';');
200 if (semiColon
== buffer
|| base64
== buffer
) {
201 // there is no content type, but there are other parameters
202 contentType
.AssignLiteral("text/plain");
204 contentType
= buffer
;
205 ToLowerCase(contentType
);
209 char *charset
= PL_strcasestr(semiColon
+ 1, "charset=");
211 contentCharset
= charset
+ sizeof("charset=") - 1;
221 contentType
.StripWhitespace();
222 contentCharset
.StripWhitespace();
224 dataBuffer
.Assign(comma
+ 1);