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 : /* ***** BEGIN LICENSE BLOCK *****
4 : * Version: MPL 1.1/GPL 2.0/LGPL 2.1
5 : *
6 : * The contents of this file are subject to the Mozilla Public License Version
7 : * 1.1 (the "License"); you may not use this file except in compliance with
8 : * the License. You may obtain a copy of the License at
9 : * http://www.mozilla.org/MPL/
10 : *
11 : * Software distributed under the License is distributed on an "AS IS" basis,
12 : * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
13 : * for the specific language governing rights and limitations under the
14 : * License.
15 : *
16 : * The Original Code is mozilla.org code.
17 : *
18 : * The Initial Developer of the Original Code is
19 : * Netscape Communications Corporation.
20 : * Portions created by the Initial Developer are Copyright (C) 1998
21 : * the Initial Developer. All Rights Reserved.
22 : *
23 : * Contributor(s):
24 : * Chak Nanga <chak@netscape.com>
25 : * Darin Fisher <darin@meer.net>
26 : *
27 : * Alternatively, the contents of this file may be used under the terms of
28 : * either the GNU General Public License Version 2 or later (the "GPL"), or
29 : * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
30 : * in which case the provisions of the GPL or the LGPL are applicable instead
31 : * of those above. If you wish to allow use of your version of this file only
32 : * under the terms of either the GPL or the LGPL, and not to allow others to
33 : * use your version of this file under the terms of the MPL, indicate your
34 : * decision by deleting the provisions above and replace them with the notice
35 : * and other provisions required by the GPL or the LGPL. If you do not delete
36 : * the provisions above, a recipient may use your version of this file under
37 : * the terms of any one of the MPL, the GPL or the LGPL.
38 : *
39 : * ***** END LICENSE BLOCK ***** */
40 :
41 : #include "nsViewSourceHandler.h"
42 : #include "nsViewSourceChannel.h"
43 : #include "nsNetUtil.h"
44 : #include "nsSimpleNestedURI.h"
45 :
46 : #define VIEW_SOURCE "view-source"
47 :
48 : ////////////////////////////////////////////////////////////////////////////////
49 :
50 377 : NS_IMPL_ISUPPORTS1(nsViewSourceHandler, nsIProtocolHandler)
51 :
52 : ////////////////////////////////////////////////////////////////////////////////
53 : // nsIProtocolHandler methods:
54 :
55 : NS_IMETHODIMP
56 0 : nsViewSourceHandler::GetScheme(nsACString &result)
57 : {
58 0 : result.AssignLiteral(VIEW_SOURCE);
59 0 : return NS_OK;
60 : }
61 :
62 : NS_IMETHODIMP
63 0 : nsViewSourceHandler::GetDefaultPort(PRInt32 *result)
64 : {
65 0 : *result = -1;
66 0 : return NS_OK;
67 : }
68 :
69 : NS_IMETHODIMP
70 0 : nsViewSourceHandler::GetProtocolFlags(PRUint32 *result)
71 : {
72 : *result = URI_NORELATIVE | URI_NOAUTH | URI_LOADABLE_BY_ANYONE |
73 0 : URI_NON_PERSISTABLE;
74 0 : return NS_OK;
75 : }
76 :
77 : NS_IMETHODIMP
78 44 : nsViewSourceHandler::NewURI(const nsACString &aSpec,
79 : const char *aCharset,
80 : nsIURI *aBaseURI,
81 : nsIURI **aResult)
82 : {
83 44 : *aResult = nsnull;
84 :
85 : // Extract inner URL and normalize to ASCII. This is done to properly
86 : // support IDN in cases like "view-source:http://www.szalagavató.hu/"
87 :
88 44 : PRInt32 colon = aSpec.FindChar(':');
89 44 : if (colon == kNotFound)
90 0 : return NS_ERROR_MALFORMED_URI;
91 :
92 88 : nsCOMPtr<nsIURI> innerURI;
93 44 : nsresult rv = NS_NewURI(getter_AddRefs(innerURI),
94 88 : Substring(aSpec, colon + 1), aCharset, aBaseURI);
95 44 : if (NS_FAILED(rv))
96 0 : return rv;
97 :
98 88 : nsCAutoString asciiSpec;
99 44 : rv = innerURI->GetAsciiSpec(asciiSpec);
100 44 : if (NS_FAILED(rv))
101 0 : return rv;
102 :
103 : // put back our scheme and construct a simple-uri wrapper
104 :
105 44 : asciiSpec.Insert(VIEW_SOURCE ":", 0);
106 :
107 : // We can't swap() from an nsRefPtr<nsSimpleNestedURI> to an nsIURI**,
108 : // sadly.
109 88 : nsSimpleNestedURI* ourURI = new nsSimpleNestedURI(innerURI);
110 88 : nsCOMPtr<nsIURI> uri = ourURI;
111 44 : if (!uri)
112 0 : return NS_ERROR_OUT_OF_MEMORY;
113 :
114 44 : rv = ourURI->SetSpec(asciiSpec);
115 44 : if (NS_FAILED(rv))
116 0 : return rv;
117 :
118 : // Make the URI immutable so it's impossible to get it out of sync
119 : // with its inner URI.
120 44 : ourURI->SetMutable(false);
121 :
122 44 : uri.swap(*aResult);
123 44 : return rv;
124 : }
125 :
126 : NS_IMETHODIMP
127 0 : nsViewSourceHandler::NewChannel(nsIURI* uri, nsIChannel* *result)
128 : {
129 0 : NS_ENSURE_ARG_POINTER(uri);
130 0 : nsViewSourceChannel *channel = new nsViewSourceChannel();
131 0 : if (!channel)
132 0 : return NS_ERROR_OUT_OF_MEMORY;
133 0 : NS_ADDREF(channel);
134 :
135 0 : nsresult rv = channel->Init(uri);
136 0 : if (NS_FAILED(rv)) {
137 0 : NS_RELEASE(channel);
138 0 : return rv;
139 : }
140 :
141 0 : *result = static_cast<nsIViewSourceChannel*>(channel);
142 0 : return NS_OK;
143 : }
144 :
145 : NS_IMETHODIMP
146 0 : nsViewSourceHandler::AllowPort(PRInt32 port, const char *scheme, bool *_retval)
147 : {
148 : // don't override anything.
149 0 : *_retval = false;
150 0 : return NS_OK;
151 : }
|