2006-05-02 18:54:19 +00:00
|
|
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
|
2004-07-01 23:45:34 +00:00
|
|
|
/* vim:set ts=4 sw=4 sts=4 et: */
|
2012-05-21 11:12:37 +00: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/. */
|
2001-04-16 20:00:20 +00:00
|
|
|
|
|
|
|
#include "nsViewSourceHandler.h"
|
|
|
|
#include "nsViewSourceChannel.h"
|
2004-07-01 23:45:34 +00:00
|
|
|
#include "nsNetUtil.h"
|
2006-06-19 21:02:12 +00:00
|
|
|
#include "nsSimpleNestedURI.h"
|
2006-05-02 18:54:19 +00:00
|
|
|
|
|
|
|
#define VIEW_SOURCE "view-source"
|
2001-04-16 20:00:20 +00:00
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
NS_IMPL_ISUPPORTS1(nsViewSourceHandler, nsIProtocolHandler)
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// nsIProtocolHandler methods:
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2004-07-01 23:45:34 +00:00
|
|
|
nsViewSourceHandler::GetScheme(nsACString &result)
|
|
|
|
{
|
2006-05-02 18:54:19 +00:00
|
|
|
result.AssignLiteral(VIEW_SOURCE);
|
2001-04-16 20:00:20 +00:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2012-08-22 15:56:38 +00:00
|
|
|
nsViewSourceHandler::GetDefaultPort(int32_t *result)
|
2004-07-01 23:45:34 +00:00
|
|
|
{
|
2001-04-16 20:00:20 +00:00
|
|
|
*result = -1;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2001-08-07 20:42:57 +00:00
|
|
|
NS_IMETHODIMP
|
2012-08-22 15:56:38 +00:00
|
|
|
nsViewSourceHandler::GetProtocolFlags(uint32_t *result)
|
2004-07-01 23:45:34 +00:00
|
|
|
{
|
2007-04-15 12:31:18 +00:00
|
|
|
*result = URI_NORELATIVE | URI_NOAUTH | URI_LOADABLE_BY_ANYONE |
|
|
|
|
URI_NON_PERSISTABLE;
|
2001-08-07 20:42:57 +00:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2001-04-16 20:00:20 +00:00
|
|
|
NS_IMETHODIMP
|
2002-03-06 07:48:55 +00:00
|
|
|
nsViewSourceHandler::NewURI(const nsACString &aSpec,
|
2004-07-01 23:45:34 +00:00
|
|
|
const char *aCharset,
|
2002-03-06 07:48:55 +00:00
|
|
|
nsIURI *aBaseURI,
|
2004-07-01 23:45:34 +00:00
|
|
|
nsIURI **aResult)
|
|
|
|
{
|
2012-07-30 14:20:58 +00:00
|
|
|
*aResult = nullptr;
|
2001-04-16 20:00:20 +00:00
|
|
|
|
2004-07-01 23:45:34 +00:00
|
|
|
// Extract inner URL and normalize to ASCII. This is done to properly
|
|
|
|
// support IDN in cases like "view-source:http://www.szalagavató.hu/"
|
|
|
|
|
2012-08-22 15:56:38 +00:00
|
|
|
int32_t colon = aSpec.FindChar(':');
|
2004-07-01 23:45:34 +00:00
|
|
|
if (colon == kNotFound)
|
|
|
|
return NS_ERROR_MALFORMED_URI;
|
|
|
|
|
|
|
|
nsCOMPtr<nsIURI> innerURI;
|
2006-05-02 18:54:19 +00:00
|
|
|
nsresult rv = NS_NewURI(getter_AddRefs(innerURI),
|
2011-05-30 06:07:48 +00:00
|
|
|
Substring(aSpec, colon + 1), aCharset, aBaseURI);
|
2004-07-01 23:45:34 +00:00
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return rv;
|
|
|
|
|
2012-09-02 02:35:17 +00:00
|
|
|
nsAutoCString asciiSpec;
|
2004-07-01 23:45:34 +00:00
|
|
|
rv = innerURI->GetAsciiSpec(asciiSpec);
|
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return rv;
|
|
|
|
|
|
|
|
// put back our scheme and construct a simple-uri wrapper
|
2001-04-16 20:00:20 +00:00
|
|
|
|
2006-05-02 18:54:19 +00:00
|
|
|
asciiSpec.Insert(VIEW_SOURCE ":", 0);
|
|
|
|
|
2006-06-19 21:02:12 +00:00
|
|
|
// We can't swap() from an nsRefPtr<nsSimpleNestedURI> to an nsIURI**,
|
|
|
|
// sadly.
|
|
|
|
nsSimpleNestedURI* ourURI = new nsSimpleNestedURI(innerURI);
|
2006-05-02 18:54:19 +00:00
|
|
|
nsCOMPtr<nsIURI> uri = ourURI;
|
|
|
|
if (!uri)
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
2004-07-01 23:45:34 +00:00
|
|
|
|
2006-05-02 18:54:19 +00:00
|
|
|
rv = ourURI->SetSpec(asciiSpec);
|
2004-07-01 23:45:34 +00:00
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return rv;
|
2006-05-02 18:54:19 +00:00
|
|
|
|
|
|
|
// Make the URI immutable so it's impossible to get it out of sync
|
2006-06-19 21:02:12 +00:00
|
|
|
// with its inner URI.
|
2011-10-17 14:59:28 +00:00
|
|
|
ourURI->SetMutable(false);
|
2006-05-02 18:54:19 +00:00
|
|
|
|
|
|
|
uri.swap(*aResult);
|
2001-04-16 20:00:20 +00:00
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsViewSourceHandler::NewChannel(nsIURI* uri, nsIChannel* *result)
|
|
|
|
{
|
2005-12-18 01:50:50 +00:00
|
|
|
NS_ENSURE_ARG_POINTER(uri);
|
2004-07-01 23:45:34 +00:00
|
|
|
nsViewSourceChannel *channel = new nsViewSourceChannel();
|
|
|
|
if (!channel)
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
NS_ADDREF(channel);
|
2001-04-16 20:00:20 +00:00
|
|
|
|
2004-07-01 23:45:34 +00:00
|
|
|
nsresult rv = channel->Init(uri);
|
2001-04-16 20:00:20 +00:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
NS_RELEASE(channel);
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2007-07-08 07:08:04 +00:00
|
|
|
*result = static_cast<nsIViewSourceChannel*>(channel);
|
2001-04-16 20:00:20 +00:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2013-06-29 03:04:42 +00:00
|
|
|
nsresult
|
|
|
|
nsViewSourceHandler::NewSrcdocChannel(nsIURI* uri, const nsAString &srcdoc,
|
2014-02-06 14:46:29 +00:00
|
|
|
nsIURI* baseURI, nsIChannel* *result)
|
2013-06-29 03:04:42 +00:00
|
|
|
{
|
|
|
|
NS_ENSURE_ARG_POINTER(uri);
|
|
|
|
nsViewSourceChannel *channel = new nsViewSourceChannel();
|
|
|
|
if (!channel)
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
NS_ADDREF(channel);
|
|
|
|
|
2014-02-06 14:46:29 +00:00
|
|
|
nsresult rv = channel->InitSrcdoc(uri, srcdoc, baseURI);
|
2013-06-29 03:04:42 +00:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
NS_RELEASE(channel);
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
*result = static_cast<nsIViewSourceChannel*>(channel);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2001-06-06 00:10:09 +00:00
|
|
|
NS_IMETHODIMP
|
2012-08-22 15:56:38 +00:00
|
|
|
nsViewSourceHandler::AllowPort(int32_t port, const char *scheme, bool *_retval)
|
2001-06-06 00:10:09 +00:00
|
|
|
{
|
|
|
|
// don't override anything.
|
2011-10-17 14:59:28 +00:00
|
|
|
*_retval = false;
|
2001-06-06 00:10:09 +00:00
|
|
|
return NS_OK;
|
|
|
|
}
|
2013-06-29 03:04:42 +00:00
|
|
|
|
|
|
|
nsViewSourceHandler::nsViewSourceHandler()
|
|
|
|
{
|
|
|
|
gInstance = this;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsViewSourceHandler::~nsViewSourceHandler()
|
|
|
|
{
|
|
|
|
gInstance = nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsViewSourceHandler* nsViewSourceHandler::gInstance = nullptr;
|
|
|
|
|
|
|
|
nsViewSourceHandler*
|
|
|
|
nsViewSourceHandler::GetInstance()
|
|
|
|
{
|
|
|
|
return gInstance;
|
|
|
|
}
|