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
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/
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
16 * The Original Code is mozilla.org code.
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.
24 * Chak Nanga <chak@netscape.com>
25 * Darin Fisher <darin@meer.net>
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.
39 * ***** END LICENSE BLOCK ***** */
41 #include "nsViewSourceHandler.h"
42 #include "nsViewSourceChannel.h"
43 #include "nsNetUtil.h"
44 #include "nsSimpleNestedURI.h"
46 #define VIEW_SOURCE "view-source"
48 ////////////////////////////////////////////////////////////////////////////////
50 NS_IMPL_ISUPPORTS1(nsViewSourceHandler
, nsIProtocolHandler
)
52 ////////////////////////////////////////////////////////////////////////////////
53 // nsIProtocolHandler methods:
56 nsViewSourceHandler::GetScheme(nsACString
&result
)
58 result
.AssignLiteral(VIEW_SOURCE
);
63 nsViewSourceHandler::GetDefaultPort(PRInt32
*result
)
70 nsViewSourceHandler::GetProtocolFlags(PRUint32
*result
)
72 *result
= URI_NORELATIVE
| URI_NOAUTH
| URI_LOADABLE_BY_ANYONE
|
78 nsViewSourceHandler::NewURI(const nsACString
&aSpec
,
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/"
88 PRInt32 colon
= aSpec
.FindChar(':');
89 if (colon
== kNotFound
)
90 return NS_ERROR_MALFORMED_URI
;
92 nsCOMPtr
<nsIURI
> innerURI
;
93 nsresult rv
= NS_NewURI(getter_AddRefs(innerURI
),
94 Substring(aSpec
, colon
+ 1), aCharset
);
98 nsCAutoString asciiSpec
;
99 rv
= innerURI
->GetAsciiSpec(asciiSpec
);
103 // put back our scheme and construct a simple-uri wrapper
105 asciiSpec
.Insert(VIEW_SOURCE
":", 0);
107 // We can't swap() from an nsRefPtr<nsSimpleNestedURI> to an nsIURI**,
109 nsSimpleNestedURI
* ourURI
= new nsSimpleNestedURI(innerURI
);
110 nsCOMPtr
<nsIURI
> uri
= ourURI
;
112 return NS_ERROR_OUT_OF_MEMORY
;
114 rv
= ourURI
->SetSpec(asciiSpec
);
118 // Make the URI immutable so it's impossible to get it out of sync
119 // with its inner URI.
120 ourURI
->SetMutable(PR_FALSE
);
127 nsViewSourceHandler::NewChannel(nsIURI
* uri
, nsIChannel
* *result
)
129 NS_ENSURE_ARG_POINTER(uri
);
130 nsViewSourceChannel
*channel
= new nsViewSourceChannel();
132 return NS_ERROR_OUT_OF_MEMORY
;
135 nsresult rv
= channel
->Init(uri
);
141 *result
= static_cast<nsIViewSourceChannel
*>(channel
);
146 nsViewSourceHandler::AllowPort(PRInt32 port
, const char *scheme
, PRBool
*_retval
)
148 // don't override anything.