netwerk/protocol/viewsource/nsViewSourceHandler.cpp

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
michael@0 2 /* vim:set ts=4 sw=4 sts=4 et: */
michael@0 3 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 4 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 6
michael@0 7 #include "nsViewSourceHandler.h"
michael@0 8 #include "nsViewSourceChannel.h"
michael@0 9 #include "nsNetUtil.h"
michael@0 10 #include "nsSimpleNestedURI.h"
michael@0 11
michael@0 12 #define VIEW_SOURCE "view-source"
michael@0 13
michael@0 14 ////////////////////////////////////////////////////////////////////////////////
michael@0 15
michael@0 16 NS_IMPL_ISUPPORTS(nsViewSourceHandler, nsIProtocolHandler)
michael@0 17
michael@0 18 ////////////////////////////////////////////////////////////////////////////////
michael@0 19 // nsIProtocolHandler methods:
michael@0 20
michael@0 21 NS_IMETHODIMP
michael@0 22 nsViewSourceHandler::GetScheme(nsACString &result)
michael@0 23 {
michael@0 24 result.AssignLiteral(VIEW_SOURCE);
michael@0 25 return NS_OK;
michael@0 26 }
michael@0 27
michael@0 28 NS_IMETHODIMP
michael@0 29 nsViewSourceHandler::GetDefaultPort(int32_t *result)
michael@0 30 {
michael@0 31 *result = -1;
michael@0 32 return NS_OK;
michael@0 33 }
michael@0 34
michael@0 35 NS_IMETHODIMP
michael@0 36 nsViewSourceHandler::GetProtocolFlags(uint32_t *result)
michael@0 37 {
michael@0 38 *result = URI_NORELATIVE | URI_NOAUTH | URI_LOADABLE_BY_ANYONE |
michael@0 39 URI_NON_PERSISTABLE;
michael@0 40 return NS_OK;
michael@0 41 }
michael@0 42
michael@0 43 NS_IMETHODIMP
michael@0 44 nsViewSourceHandler::NewURI(const nsACString &aSpec,
michael@0 45 const char *aCharset,
michael@0 46 nsIURI *aBaseURI,
michael@0 47 nsIURI **aResult)
michael@0 48 {
michael@0 49 *aResult = nullptr;
michael@0 50
michael@0 51 // Extract inner URL and normalize to ASCII. This is done to properly
michael@0 52 // support IDN in cases like "view-source:http://www.szalagavató.hu/"
michael@0 53
michael@0 54 int32_t colon = aSpec.FindChar(':');
michael@0 55 if (colon == kNotFound)
michael@0 56 return NS_ERROR_MALFORMED_URI;
michael@0 57
michael@0 58 nsCOMPtr<nsIURI> innerURI;
michael@0 59 nsresult rv = NS_NewURI(getter_AddRefs(innerURI),
michael@0 60 Substring(aSpec, colon + 1), aCharset, aBaseURI);
michael@0 61 if (NS_FAILED(rv))
michael@0 62 return rv;
michael@0 63
michael@0 64 nsAutoCString asciiSpec;
michael@0 65 rv = innerURI->GetAsciiSpec(asciiSpec);
michael@0 66 if (NS_FAILED(rv))
michael@0 67 return rv;
michael@0 68
michael@0 69 // put back our scheme and construct a simple-uri wrapper
michael@0 70
michael@0 71 asciiSpec.Insert(VIEW_SOURCE ":", 0);
michael@0 72
michael@0 73 // We can't swap() from an nsRefPtr<nsSimpleNestedURI> to an nsIURI**,
michael@0 74 // sadly.
michael@0 75 nsSimpleNestedURI* ourURI = new nsSimpleNestedURI(innerURI);
michael@0 76 nsCOMPtr<nsIURI> uri = ourURI;
michael@0 77 if (!uri)
michael@0 78 return NS_ERROR_OUT_OF_MEMORY;
michael@0 79
michael@0 80 rv = ourURI->SetSpec(asciiSpec);
michael@0 81 if (NS_FAILED(rv))
michael@0 82 return rv;
michael@0 83
michael@0 84 // Make the URI immutable so it's impossible to get it out of sync
michael@0 85 // with its inner URI.
michael@0 86 ourURI->SetMutable(false);
michael@0 87
michael@0 88 uri.swap(*aResult);
michael@0 89 return rv;
michael@0 90 }
michael@0 91
michael@0 92 NS_IMETHODIMP
michael@0 93 nsViewSourceHandler::NewChannel(nsIURI* uri, nsIChannel* *result)
michael@0 94 {
michael@0 95 NS_ENSURE_ARG_POINTER(uri);
michael@0 96 nsViewSourceChannel *channel = new nsViewSourceChannel();
michael@0 97 if (!channel)
michael@0 98 return NS_ERROR_OUT_OF_MEMORY;
michael@0 99 NS_ADDREF(channel);
michael@0 100
michael@0 101 nsresult rv = channel->Init(uri);
michael@0 102 if (NS_FAILED(rv)) {
michael@0 103 NS_RELEASE(channel);
michael@0 104 return rv;
michael@0 105 }
michael@0 106
michael@0 107 *result = static_cast<nsIViewSourceChannel*>(channel);
michael@0 108 return NS_OK;
michael@0 109 }
michael@0 110
michael@0 111 nsresult
michael@0 112 nsViewSourceHandler::NewSrcdocChannel(nsIURI* uri, const nsAString &srcdoc,
michael@0 113 nsIURI* baseURI, nsIChannel* *result)
michael@0 114 {
michael@0 115 NS_ENSURE_ARG_POINTER(uri);
michael@0 116 nsViewSourceChannel *channel = new nsViewSourceChannel();
michael@0 117 if (!channel)
michael@0 118 return NS_ERROR_OUT_OF_MEMORY;
michael@0 119 NS_ADDREF(channel);
michael@0 120
michael@0 121 nsresult rv = channel->InitSrcdoc(uri, srcdoc, baseURI);
michael@0 122 if (NS_FAILED(rv)) {
michael@0 123 NS_RELEASE(channel);
michael@0 124 return rv;
michael@0 125 }
michael@0 126
michael@0 127 *result = static_cast<nsIViewSourceChannel*>(channel);
michael@0 128 return NS_OK;
michael@0 129 }
michael@0 130
michael@0 131 NS_IMETHODIMP
michael@0 132 nsViewSourceHandler::AllowPort(int32_t port, const char *scheme, bool *_retval)
michael@0 133 {
michael@0 134 // don't override anything.
michael@0 135 *_retval = false;
michael@0 136 return NS_OK;
michael@0 137 }
michael@0 138
michael@0 139 nsViewSourceHandler::nsViewSourceHandler()
michael@0 140 {
michael@0 141 gInstance = this;
michael@0 142 }
michael@0 143
michael@0 144 nsViewSourceHandler::~nsViewSourceHandler()
michael@0 145 {
michael@0 146 gInstance = nullptr;
michael@0 147 }
michael@0 148
michael@0 149 nsViewSourceHandler* nsViewSourceHandler::gInstance = nullptr;
michael@0 150
michael@0 151 nsViewSourceHandler*
michael@0 152 nsViewSourceHandler::GetInstance()
michael@0 153 {
michael@0 154 return gInstance;
michael@0 155 }

mercurial