2001-09-29 00:14:13 +04:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
|
2012-05-21 15:12:37 +04:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
1999-09-03 04:39:55 +04:00
|
|
|
|
|
|
|
#include "nsDataChannel.h"
|
|
|
|
#include "nsDataHandler.h"
|
2001-04-10 10:01:08 +04:00
|
|
|
#include "nsNetCID.h"
|
2012-07-27 18:03:27 +04:00
|
|
|
#include "nsError.h"
|
2015-03-27 23:12:37 +03:00
|
|
|
#include "DataChannelChild.h"
|
1999-09-03 04:39:55 +04:00
|
|
|
|
2001-04-10 10:01:08 +04:00
|
|
|
static NS_DEFINE_CID(kSimpleURICID, NS_SIMPLEURI_CID);
|
1999-09-03 04:39:55 +04:00
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
nsDataHandler::nsDataHandler() {
|
|
|
|
}
|
|
|
|
|
|
|
|
nsDataHandler::~nsDataHandler() {
|
|
|
|
}
|
|
|
|
|
2015-03-30 22:32:26 +03:00
|
|
|
NS_IMPL_ISUPPORTS(nsDataHandler, nsIProtocolHandler, nsISupportsWeakReference)
|
1999-09-03 04:39:55 +04:00
|
|
|
|
2010-06-10 22:11:40 +04:00
|
|
|
nsresult
|
1999-09-03 04:39:55 +04:00
|
|
|
nsDataHandler::Create(nsISupports* aOuter, const nsIID& aIID, void* *aResult) {
|
|
|
|
|
|
|
|
nsDataHandler* ph = new nsDataHandler();
|
2012-07-30 18:20:58 +04:00
|
|
|
if (ph == nullptr)
|
1999-09-03 04:39:55 +04:00
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
NS_ADDREF(ph);
|
|
|
|
nsresult rv = ph->QueryInterface(aIID, aResult);
|
|
|
|
NS_RELEASE(ph);
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// nsIProtocolHandler methods:
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2002-03-06 10:48:55 +03:00
|
|
|
nsDataHandler::GetScheme(nsACString &result) {
|
2004-12-04 13:19:29 +03:00
|
|
|
result.AssignLiteral("data");
|
1999-09-03 04:39:55 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2012-08-22 19:56:38 +04:00
|
|
|
nsDataHandler::GetDefaultPort(int32_t *result) {
|
1999-09-03 04:39:55 +04:00
|
|
|
// no ports for data protocol
|
|
|
|
*result = -1;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2001-08-08 00:42:57 +04:00
|
|
|
NS_IMETHODIMP
|
2012-08-22 19:56:38 +04:00
|
|
|
nsDataHandler::GetProtocolFlags(uint32_t *result) {
|
2006-11-11 02:49:08 +03:00
|
|
|
*result = URI_NORELATIVE | URI_NOAUTH | URI_INHERITS_SECURITY_CONTEXT |
|
2012-03-16 07:31:01 +04:00
|
|
|
URI_LOADABLE_BY_ANYONE | URI_NON_PERSISTABLE | URI_IS_LOCAL_RESOURCE |
|
2012-03-16 07:31:01 +04:00
|
|
|
URI_SYNC_LOAD_IS_OK;
|
2001-08-08 00:42:57 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
1999-09-03 04:39:55 +04:00
|
|
|
NS_IMETHODIMP
|
2002-03-06 10:48:55 +03:00
|
|
|
nsDataHandler::NewURI(const nsACString &aSpec,
|
|
|
|
const char *aCharset, // ignore charset info
|
|
|
|
nsIURI *aBaseURI,
|
|
|
|
nsIURI **result) {
|
1999-09-03 04:39:55 +04:00
|
|
|
nsresult rv;
|
2011-05-22 05:12:46 +04:00
|
|
|
nsRefPtr<nsIURI> uri;
|
1999-09-03 04:39:55 +04:00
|
|
|
|
2007-08-21 21:28:22 +04:00
|
|
|
nsCString spec(aSpec);
|
|
|
|
|
2011-05-22 05:12:46 +04:00
|
|
|
if (aBaseURI && !spec.IsEmpty() && spec[0] == '#') {
|
|
|
|
// Looks like a reference instead of a fully-specified URI.
|
|
|
|
// --> initialize |uri| as a clone of |aBaseURI|, with ref appended.
|
|
|
|
rv = aBaseURI->Clone(getter_AddRefs(uri));
|
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return rv;
|
|
|
|
rv = uri->SetRef(spec);
|
|
|
|
} else {
|
|
|
|
// Otherwise, we'll assume |spec| is a fully-specified data URI
|
2012-09-02 06:35:17 +04:00
|
|
|
nsAutoCString contentType, contentCharset, dataBuffer, hashRef;
|
2011-09-29 10:19:26 +04:00
|
|
|
bool base64;
|
2011-05-24 19:40:10 +04:00
|
|
|
rv = ParseURI(spec, contentType, contentCharset, base64, dataBuffer, hashRef);
|
2011-05-22 05:12:46 +04:00
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return rv;
|
|
|
|
|
|
|
|
// Strip whitespace unless this is text, where whitespace is important
|
|
|
|
// Don't strip escaped whitespace though (bug 391951)
|
|
|
|
if (base64 || (strncmp(contentType.get(),"text/",5) != 0 &&
|
|
|
|
contentType.Find("xml") == kNotFound)) {
|
|
|
|
// it's ascii encoded binary, don't let any spaces in
|
|
|
|
spec.StripWhitespace();
|
|
|
|
}
|
|
|
|
|
|
|
|
uri = do_CreateInstance(kSimpleURICID, &rv);
|
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return rv;
|
|
|
|
rv = uri->SetSpec(spec);
|
2007-08-21 21:28:22 +04:00
|
|
|
}
|
2004-11-01 21:50:36 +03:00
|
|
|
|
2011-05-22 05:12:46 +04:00
|
|
|
if (NS_FAILED(rv))
|
1999-09-03 04:39:55 +04:00
|
|
|
return rv;
|
|
|
|
|
2011-05-22 05:12:46 +04:00
|
|
|
uri.forget(result);
|
1999-09-03 04:39:55 +04:00
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2014-10-23 04:16:57 +04:00
|
|
|
nsDataHandler::NewChannel2(nsIURI* uri,
|
|
|
|
nsILoadInfo* aLoadInfo,
|
|
|
|
nsIChannel** result)
|
|
|
|
{
|
2005-12-18 04:50:50 +03:00
|
|
|
NS_ENSURE_ARG_POINTER(uri);
|
2015-03-27 23:12:37 +03:00
|
|
|
nsDataChannel* channel;
|
|
|
|
if (XRE_GetProcessType() == GeckoProcessType_Default) {
|
|
|
|
channel = new nsDataChannel(uri);
|
|
|
|
} else {
|
|
|
|
channel = new mozilla::net::DataChannelChild(uri);
|
|
|
|
}
|
2005-11-12 21:17:19 +03:00
|
|
|
NS_ADDREF(channel);
|
1999-09-03 04:39:55 +04:00
|
|
|
|
2005-11-12 21:17:19 +03:00
|
|
|
nsresult rv = channel->Init();
|
1999-09-03 04:39:55 +04:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
NS_RELEASE(channel);
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2014-12-12 20:07:36 +03:00
|
|
|
// set the loadInfo on the new channel
|
|
|
|
rv = channel->SetLoadInfo(aLoadInfo);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
NS_RELEASE(channel);
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
1999-09-03 04:39:55 +04:00
|
|
|
*result = channel;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2014-10-23 04:16:57 +04:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsDataHandler::NewChannel(nsIURI* uri, nsIChannel* *result)
|
|
|
|
{
|
|
|
|
return NewChannel2(uri, nullptr, result);
|
|
|
|
}
|
|
|
|
|
2001-06-06 04:10:09 +04:00
|
|
|
NS_IMETHODIMP
|
2012-08-22 19:56:38 +04:00
|
|
|
nsDataHandler::AllowPort(int32_t port, const char *scheme, bool *_retval) {
|
2001-06-06 04:10:09 +04:00
|
|
|
// don't override anything.
|
2011-10-17 18:59:28 +04:00
|
|
|
*_retval = false;
|
2001-06-06 04:10:09 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
2007-08-21 21:28:22 +04:00
|
|
|
|
2012-07-17 04:38:47 +04:00
|
|
|
#define BASE64_EXTENSION ";base64"
|
|
|
|
|
2007-08-21 21:28:22 +04:00
|
|
|
nsresult
|
|
|
|
nsDataHandler::ParseURI(nsCString& spec,
|
|
|
|
nsCString& contentType,
|
|
|
|
nsCString& contentCharset,
|
2011-09-29 10:19:26 +04:00
|
|
|
bool& isBase64,
|
2011-05-24 19:40:10 +04:00
|
|
|
nsCString& dataBuffer,
|
|
|
|
nsCString& hashRef) {
|
2011-10-17 18:59:28 +04:00
|
|
|
isBase64 = false;
|
2007-08-21 21:28:22 +04:00
|
|
|
|
|
|
|
// move past "data:"
|
2009-06-23 15:44:37 +04:00
|
|
|
char *buffer = (char *) PL_strcasestr(spec.BeginWriting(), "data:");
|
2007-08-21 21:28:22 +04:00
|
|
|
if (!buffer) {
|
|
|
|
// malformed uri
|
|
|
|
return NS_ERROR_MALFORMED_URI;
|
|
|
|
}
|
|
|
|
buffer += 5;
|
|
|
|
|
|
|
|
// First, find the start of the data
|
|
|
|
char *comma = strchr(buffer, ',');
|
|
|
|
if (!comma)
|
|
|
|
return NS_ERROR_MALFORMED_URI;
|
|
|
|
|
|
|
|
*comma = '\0';
|
|
|
|
|
|
|
|
// determine if the data is base64 encoded.
|
2012-07-17 04:38:47 +04:00
|
|
|
char *base64 = PL_strcasestr(buffer, BASE64_EXTENSION);
|
2012-08-22 14:57:02 +04:00
|
|
|
if (base64) {
|
|
|
|
char *beyond = base64 + strlen(BASE64_EXTENSION);
|
|
|
|
// per the RFC 2397 grammar, "base64" MUST be followed by a comma
|
|
|
|
// previously substituted by '\0', but we also allow it in between
|
|
|
|
// parameters so a subsequent ";" is ok as well (this deals with
|
|
|
|
// *broken* data URIs, see bug 781693 for an example)
|
|
|
|
if (*beyond == '\0' || *beyond == ';') {
|
|
|
|
isBase64 = true;
|
|
|
|
*base64 = '\0';
|
|
|
|
}
|
2007-08-21 21:28:22 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (comma == buffer) {
|
|
|
|
// nothing but data
|
|
|
|
contentType.AssignLiteral("text/plain");
|
|
|
|
contentCharset.AssignLiteral("US-ASCII");
|
|
|
|
} else {
|
|
|
|
// everything else is content type
|
|
|
|
char *semiColon = (char *) strchr(buffer, ';');
|
|
|
|
if (semiColon)
|
|
|
|
*semiColon = '\0';
|
|
|
|
|
|
|
|
if (semiColon == buffer || base64 == buffer) {
|
|
|
|
// there is no content type, but there are other parameters
|
|
|
|
contentType.AssignLiteral("text/plain");
|
|
|
|
} else {
|
|
|
|
contentType = buffer;
|
|
|
|
ToLowerCase(contentType);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (semiColon) {
|
|
|
|
char *charset = PL_strcasestr(semiColon + 1, "charset=");
|
|
|
|
if (charset)
|
|
|
|
contentCharset = charset + sizeof("charset=") - 1;
|
|
|
|
|
|
|
|
*semiColon = ';';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
*comma = ',';
|
|
|
|
if (isBase64)
|
|
|
|
*base64 = ';';
|
|
|
|
|
|
|
|
contentType.StripWhitespace();
|
|
|
|
contentCharset.StripWhitespace();
|
|
|
|
|
2011-05-24 19:40:10 +04:00
|
|
|
// Split encoded data from terminal "#ref" (if present)
|
|
|
|
char *data = comma + 1;
|
|
|
|
char *hash = strchr(data, '#');
|
|
|
|
if (!hash) {
|
|
|
|
dataBuffer.Assign(data);
|
|
|
|
hashRef.Truncate();
|
|
|
|
} else {
|
|
|
|
dataBuffer.Assign(data, hash - data);
|
|
|
|
hashRef.Assign(hash);
|
|
|
|
}
|
2007-08-21 21:28:22 +04:00
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|