nsViewSourceHandler.cpp (4332B)
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 2 /* vim:set ts=4 sw=2 sts=2 et: */ 3 /* This Source Code Form is subject to the terms of the Mozilla Public 4 * License, v. 2.0. If a copy of the MPL was not distributed with this 5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 6 7 #include "nsViewSourceHandler.h" 8 #include "nsViewSourceChannel.h" 9 #include "nsNetUtil.h" 10 #include "nsSimpleNestedURI.h" 11 12 #define VIEW_SOURCE "view-source" 13 14 #define DEFAULT_FLAGS \ 15 (URI_NORELATIVE | URI_NOAUTH | URI_DANGEROUS_TO_LOAD | URI_NON_PERSISTABLE) 16 17 namespace mozilla { 18 namespace net { 19 20 //////////////////////////////////////////////////////////////////////////////// 21 22 NS_IMPL_ISUPPORTS(nsViewSourceHandler, nsIProtocolHandler, 23 nsIProtocolHandlerWithDynamicFlags) 24 25 //////////////////////////////////////////////////////////////////////////////// 26 // nsIProtocolHandler methods: 27 28 NS_IMETHODIMP 29 nsViewSourceHandler::GetScheme(nsACString& result) { 30 result.AssignLiteral(VIEW_SOURCE); 31 return NS_OK; 32 } 33 34 NS_IMETHODIMP 35 nsViewSourceHandler::GetFlagsForURI(nsIURI* aURI, uint32_t* result) { 36 *result = DEFAULT_FLAGS; 37 nsCOMPtr<nsINestedURI> nestedURI(do_QueryInterface(aURI)); 38 if (!nestedURI) { 39 return NS_OK; 40 } 41 42 nsCOMPtr<nsIURI> innerURI; 43 nestedURI->GetInnerURI(getter_AddRefs(innerURI)); 44 nsCOMPtr<nsINetUtil> netUtil = do_GetNetUtil(); 45 bool isLoadable = false; 46 nsresult rv = 47 netUtil->ProtocolHasFlags(innerURI, URI_LOADABLE_BY_ANYONE, &isLoadable); 48 NS_ENSURE_SUCCESS(rv, rv); 49 if (isLoadable) { 50 *result |= URI_LOADABLE_BY_EXTENSIONS; 51 } 52 return NS_OK; 53 } 54 55 // static 56 nsresult nsViewSourceHandler::CreateNewURI(const nsACString& aSpec, 57 const char* aCharset, 58 nsIURI* aBaseURI, nsIURI** aResult) { 59 *aResult = nullptr; 60 61 // Extract inner URL and normalize to ASCII. This is done to properly 62 // support IDN in cases like "view-source:http://www.szalagavató.hu/" 63 64 int32_t colon = aSpec.FindChar(':'); 65 if (colon == kNotFound) return NS_ERROR_MALFORMED_URI; 66 67 nsCOMPtr<nsIURI> innerURI; 68 nsresult rv = NS_NewURI(getter_AddRefs(innerURI), Substring(aSpec, colon + 1), 69 aCharset, aBaseURI); 70 if (NS_FAILED(rv)) return rv; 71 72 nsAutoCString asciiSpec; 73 rv = innerURI->GetAsciiSpec(asciiSpec); 74 if (NS_FAILED(rv)) return rv; 75 76 // put back our scheme and construct a simple-uri wrapper 77 78 asciiSpec.Insert(VIEW_SOURCE ":", 0); 79 80 nsCOMPtr<nsIURI> uri; 81 rv = NS_MutateURI(new nsSimpleNestedURI::Mutator()) 82 .Apply(&nsINestedURIMutator::Init, innerURI) 83 .SetSpec(asciiSpec) 84 .Finalize(uri); 85 if (NS_FAILED(rv)) { 86 return rv; 87 } 88 89 uri.swap(*aResult); 90 return rv; 91 } 92 93 NS_IMETHODIMP 94 nsViewSourceHandler::NewChannel(nsIURI* uri, nsILoadInfo* aLoadInfo, 95 nsIChannel** result) { 96 NS_ENSURE_ARG_POINTER(uri); 97 RefPtr<nsViewSourceChannel> channel = new nsViewSourceChannel(); 98 99 nsresult rv = channel->Init(uri, aLoadInfo); 100 if (NS_FAILED(rv)) { 101 return rv; 102 } 103 104 *result = channel.forget().downcast<nsIViewSourceChannel>().take(); 105 return NS_OK; 106 } 107 108 nsresult nsViewSourceHandler::NewSrcdocChannel(nsIURI* aURI, nsIURI* aBaseURI, 109 const nsAString& aSrcdoc, 110 nsILoadInfo* aLoadInfo, 111 nsIChannel** outChannel) { 112 NS_ENSURE_ARG_POINTER(aURI); 113 RefPtr<nsViewSourceChannel> channel = new nsViewSourceChannel(); 114 115 nsresult rv = channel->InitSrcdoc(aURI, aBaseURI, aSrcdoc, aLoadInfo); 116 if (NS_FAILED(rv)) { 117 return rv; 118 } 119 120 *outChannel = static_cast<nsIViewSourceChannel*>(channel.forget().take()); 121 return NS_OK; 122 } 123 124 NS_IMETHODIMP 125 nsViewSourceHandler::AllowPort(int32_t port, const char* scheme, 126 bool* _retval) { 127 // don't override anything. 128 *_retval = false; 129 return NS_OK; 130 } 131 132 nsViewSourceHandler::nsViewSourceHandler() { gInstance = this; } 133 134 nsViewSourceHandler::~nsViewSourceHandler() { gInstance = nullptr; } 135 136 nsViewSourceHandler* nsViewSourceHandler::gInstance = nullptr; 137 138 nsViewSourceHandler* nsViewSourceHandler::GetInstance() { return gInstance; } 139 140 } // namespace net 141 } // namespace mozilla