Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /* vim:set ts=4 sw=4 sts=4 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 : namespace mozilla {
15 : namespace net {
16 :
17 : ////////////////////////////////////////////////////////////////////////////////
18 :
19 0 : NS_IMPL_ISUPPORTS(nsViewSourceHandler, nsIProtocolHandler)
20 :
21 : ////////////////////////////////////////////////////////////////////////////////
22 : // nsIProtocolHandler methods:
23 :
24 : NS_IMETHODIMP
25 0 : nsViewSourceHandler::GetScheme(nsACString &result)
26 : {
27 0 : result.AssignLiteral(VIEW_SOURCE);
28 0 : return NS_OK;
29 : }
30 :
31 : NS_IMETHODIMP
32 0 : nsViewSourceHandler::GetDefaultPort(int32_t *result)
33 : {
34 0 : *result = -1;
35 0 : return NS_OK;
36 : }
37 :
38 : NS_IMETHODIMP
39 0 : nsViewSourceHandler::GetProtocolFlags(uint32_t *result)
40 : {
41 0 : *result = URI_NORELATIVE | URI_NOAUTH | URI_DANGEROUS_TO_LOAD |
42 : URI_NON_PERSISTABLE;
43 0 : return NS_OK;
44 : }
45 :
46 : NS_IMETHODIMP
47 0 : nsViewSourceHandler::NewURI(const nsACString &aSpec,
48 : const char *aCharset,
49 : nsIURI *aBaseURI,
50 : nsIURI **aResult)
51 : {
52 0 : *aResult = nullptr;
53 :
54 : // Extract inner URL and normalize to ASCII. This is done to properly
55 : // support IDN in cases like "view-source:http://www.szalagavató.hu/"
56 :
57 0 : int32_t colon = aSpec.FindChar(':');
58 0 : if (colon == kNotFound)
59 0 : return NS_ERROR_MALFORMED_URI;
60 :
61 0 : nsCOMPtr<nsIURI> innerURI;
62 0 : nsresult rv = NS_NewURI(getter_AddRefs(innerURI),
63 0 : Substring(aSpec, colon + 1), aCharset, aBaseURI);
64 0 : if (NS_FAILED(rv))
65 0 : return rv;
66 :
67 0 : nsAutoCString asciiSpec;
68 0 : rv = innerURI->GetAsciiSpec(asciiSpec);
69 0 : if (NS_FAILED(rv))
70 0 : return rv;
71 :
72 : // put back our scheme and construct a simple-uri wrapper
73 :
74 0 : asciiSpec.Insert(VIEW_SOURCE ":", 0);
75 :
76 : // We can't swap() from an RefPtr<nsSimpleNestedURI> to an nsIURI**,
77 : // sadly.
78 0 : nsSimpleNestedURI* ourURI = new nsSimpleNestedURI(innerURI);
79 0 : nsCOMPtr<nsIURI> uri = ourURI;
80 0 : if (!uri)
81 0 : return NS_ERROR_OUT_OF_MEMORY;
82 :
83 0 : rv = ourURI->SetSpec(asciiSpec);
84 0 : if (NS_FAILED(rv))
85 0 : return rv;
86 :
87 : // Make the URI immutable so it's impossible to get it out of sync
88 : // with its inner URI.
89 0 : ourURI->SetMutable(false);
90 :
91 0 : uri.swap(*aResult);
92 0 : return rv;
93 : }
94 :
95 : NS_IMETHODIMP
96 0 : nsViewSourceHandler::NewChannel2(nsIURI* uri,
97 : nsILoadInfo* aLoadInfo,
98 : nsIChannel** result)
99 : {
100 0 : NS_ENSURE_ARG_POINTER(uri);
101 0 : nsViewSourceChannel *channel = new nsViewSourceChannel();
102 0 : if (!channel)
103 0 : return NS_ERROR_OUT_OF_MEMORY;
104 0 : NS_ADDREF(channel);
105 :
106 0 : nsresult rv = channel->Init(uri);
107 0 : if (NS_FAILED(rv)) {
108 0 : NS_RELEASE(channel);
109 0 : return rv;
110 : }
111 :
112 : // set the loadInfo on the new channel
113 0 : rv = channel->SetLoadInfo(aLoadInfo);
114 0 : if (NS_FAILED(rv)) {
115 0 : NS_RELEASE(channel);
116 0 : return rv;
117 : }
118 :
119 0 : *result = static_cast<nsIViewSourceChannel*>(channel);
120 0 : return NS_OK;
121 : }
122 :
123 : NS_IMETHODIMP
124 0 : nsViewSourceHandler::NewChannel(nsIURI* uri, nsIChannel* *result)
125 : {
126 0 : return NewChannel2(uri, nullptr, result);
127 : }
128 :
129 : nsresult
130 0 : nsViewSourceHandler::NewSrcdocChannel(nsIURI *aURI,
131 : nsIURI *aBaseURI,
132 : const nsAString &aSrcdoc,
133 : nsILoadInfo* aLoadInfo,
134 : nsIChannel** outChannel)
135 : {
136 0 : NS_ENSURE_ARG_POINTER(aURI);
137 0 : RefPtr<nsViewSourceChannel> channel = new nsViewSourceChannel();
138 :
139 0 : nsresult rv = channel->InitSrcdoc(aURI, aBaseURI, aSrcdoc, aLoadInfo);
140 0 : if (NS_FAILED(rv)) {
141 0 : return rv;
142 : }
143 :
144 0 : *outChannel = static_cast<nsIViewSourceChannel*>(channel.forget().take());
145 0 : return NS_OK;
146 : }
147 :
148 : NS_IMETHODIMP
149 0 : nsViewSourceHandler::AllowPort(int32_t port, const char *scheme, bool *_retval)
150 : {
151 : // don't override anything.
152 0 : *_retval = false;
153 0 : return NS_OK;
154 : }
155 :
156 0 : nsViewSourceHandler::nsViewSourceHandler()
157 : {
158 0 : gInstance = this;
159 0 : }
160 :
161 0 : nsViewSourceHandler::~nsViewSourceHandler()
162 : {
163 0 : gInstance = nullptr;
164 0 : }
165 :
166 : nsViewSourceHandler* nsViewSourceHandler::gInstance = nullptr;
167 :
168 : nsViewSourceHandler*
169 0 : nsViewSourceHandler::GetInstance()
170 : {
171 0 : return gInstance;
172 : }
173 :
174 : } // namespace net
175 : } // namespace mozilla
|