1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/netwerk/protocol/viewsource/nsViewSourceHandler.cpp Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,155 @@ 1.4 +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ 1.5 +/* vim:set ts=4 sw=4 sts=4 et: */ 1.6 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.7 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.8 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.9 + 1.10 +#include "nsViewSourceHandler.h" 1.11 +#include "nsViewSourceChannel.h" 1.12 +#include "nsNetUtil.h" 1.13 +#include "nsSimpleNestedURI.h" 1.14 + 1.15 +#define VIEW_SOURCE "view-source" 1.16 + 1.17 +//////////////////////////////////////////////////////////////////////////////// 1.18 + 1.19 +NS_IMPL_ISUPPORTS(nsViewSourceHandler, nsIProtocolHandler) 1.20 + 1.21 +//////////////////////////////////////////////////////////////////////////////// 1.22 +// nsIProtocolHandler methods: 1.23 + 1.24 +NS_IMETHODIMP 1.25 +nsViewSourceHandler::GetScheme(nsACString &result) 1.26 +{ 1.27 + result.AssignLiteral(VIEW_SOURCE); 1.28 + return NS_OK; 1.29 +} 1.30 + 1.31 +NS_IMETHODIMP 1.32 +nsViewSourceHandler::GetDefaultPort(int32_t *result) 1.33 +{ 1.34 + *result = -1; 1.35 + return NS_OK; 1.36 +} 1.37 + 1.38 +NS_IMETHODIMP 1.39 +nsViewSourceHandler::GetProtocolFlags(uint32_t *result) 1.40 +{ 1.41 + *result = URI_NORELATIVE | URI_NOAUTH | URI_LOADABLE_BY_ANYONE | 1.42 + URI_NON_PERSISTABLE; 1.43 + return NS_OK; 1.44 +} 1.45 + 1.46 +NS_IMETHODIMP 1.47 +nsViewSourceHandler::NewURI(const nsACString &aSpec, 1.48 + const char *aCharset, 1.49 + nsIURI *aBaseURI, 1.50 + nsIURI **aResult) 1.51 +{ 1.52 + *aResult = nullptr; 1.53 + 1.54 + // Extract inner URL and normalize to ASCII. This is done to properly 1.55 + // support IDN in cases like "view-source:http://www.szalagavató.hu/" 1.56 + 1.57 + int32_t colon = aSpec.FindChar(':'); 1.58 + if (colon == kNotFound) 1.59 + return NS_ERROR_MALFORMED_URI; 1.60 + 1.61 + nsCOMPtr<nsIURI> innerURI; 1.62 + nsresult rv = NS_NewURI(getter_AddRefs(innerURI), 1.63 + Substring(aSpec, colon + 1), aCharset, aBaseURI); 1.64 + if (NS_FAILED(rv)) 1.65 + return rv; 1.66 + 1.67 + nsAutoCString asciiSpec; 1.68 + rv = innerURI->GetAsciiSpec(asciiSpec); 1.69 + if (NS_FAILED(rv)) 1.70 + return rv; 1.71 + 1.72 + // put back our scheme and construct a simple-uri wrapper 1.73 + 1.74 + asciiSpec.Insert(VIEW_SOURCE ":", 0); 1.75 + 1.76 + // We can't swap() from an nsRefPtr<nsSimpleNestedURI> to an nsIURI**, 1.77 + // sadly. 1.78 + nsSimpleNestedURI* ourURI = new nsSimpleNestedURI(innerURI); 1.79 + nsCOMPtr<nsIURI> uri = ourURI; 1.80 + if (!uri) 1.81 + return NS_ERROR_OUT_OF_MEMORY; 1.82 + 1.83 + rv = ourURI->SetSpec(asciiSpec); 1.84 + if (NS_FAILED(rv)) 1.85 + return rv; 1.86 + 1.87 + // Make the URI immutable so it's impossible to get it out of sync 1.88 + // with its inner URI. 1.89 + ourURI->SetMutable(false); 1.90 + 1.91 + uri.swap(*aResult); 1.92 + return rv; 1.93 +} 1.94 + 1.95 +NS_IMETHODIMP 1.96 +nsViewSourceHandler::NewChannel(nsIURI* uri, nsIChannel* *result) 1.97 +{ 1.98 + NS_ENSURE_ARG_POINTER(uri); 1.99 + nsViewSourceChannel *channel = new nsViewSourceChannel(); 1.100 + if (!channel) 1.101 + return NS_ERROR_OUT_OF_MEMORY; 1.102 + NS_ADDREF(channel); 1.103 + 1.104 + nsresult rv = channel->Init(uri); 1.105 + if (NS_FAILED(rv)) { 1.106 + NS_RELEASE(channel); 1.107 + return rv; 1.108 + } 1.109 + 1.110 + *result = static_cast<nsIViewSourceChannel*>(channel); 1.111 + return NS_OK; 1.112 +} 1.113 + 1.114 +nsresult 1.115 +nsViewSourceHandler::NewSrcdocChannel(nsIURI* uri, const nsAString &srcdoc, 1.116 + nsIURI* baseURI, nsIChannel* *result) 1.117 +{ 1.118 + NS_ENSURE_ARG_POINTER(uri); 1.119 + nsViewSourceChannel *channel = new nsViewSourceChannel(); 1.120 + if (!channel) 1.121 + return NS_ERROR_OUT_OF_MEMORY; 1.122 + NS_ADDREF(channel); 1.123 + 1.124 + nsresult rv = channel->InitSrcdoc(uri, srcdoc, baseURI); 1.125 + if (NS_FAILED(rv)) { 1.126 + NS_RELEASE(channel); 1.127 + return rv; 1.128 + } 1.129 + 1.130 + *result = static_cast<nsIViewSourceChannel*>(channel); 1.131 + return NS_OK; 1.132 +} 1.133 + 1.134 +NS_IMETHODIMP 1.135 +nsViewSourceHandler::AllowPort(int32_t port, const char *scheme, bool *_retval) 1.136 +{ 1.137 + // don't override anything. 1.138 + *_retval = false; 1.139 + return NS_OK; 1.140 +} 1.141 + 1.142 +nsViewSourceHandler::nsViewSourceHandler() 1.143 +{ 1.144 + gInstance = this; 1.145 +} 1.146 + 1.147 +nsViewSourceHandler::~nsViewSourceHandler() 1.148 +{ 1.149 + gInstance = nullptr; 1.150 +} 1.151 + 1.152 +nsViewSourceHandler* nsViewSourceHandler::gInstance = nullptr; 1.153 + 1.154 +nsViewSourceHandler* 1.155 +nsViewSourceHandler::GetInstance() 1.156 +{ 1.157 + return gInstance; 1.158 +}