Back to index

lightning-sunbird  0.9+nobinonly
nsViewSourceHandler.cpp
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
00002 /* vim:set ts=4 sw=4 sts=4 et: */
00003 /* ***** BEGIN LICENSE BLOCK *****
00004  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00005  *
00006  * The contents of this file are subject to the Mozilla Public License Version
00007  * 1.1 (the "License"); you may not use this file except in compliance with
00008  * the License. You may obtain a copy of the License at
00009  * http://www.mozilla.org/MPL/
00010  *
00011  * Software distributed under the License is distributed on an "AS IS" basis,
00012  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00013  * for the specific language governing rights and limitations under the
00014  * License.
00015  *
00016  * The Original Code is mozilla.org code.
00017  *
00018  * The Initial Developer of the Original Code is
00019  * Netscape Communications Corporation.
00020  * Portions created by the Initial Developer are Copyright (C) 1998
00021  * the Initial Developer. All Rights Reserved.
00022  *
00023  * Contributor(s):
00024  *   Chak Nanga <chak@netscape.com>
00025  *   Darin Fisher <darin@meer.net>
00026  *
00027  * Alternatively, the contents of this file may be used under the terms of
00028  * either the GNU General Public License Version 2 or later (the "GPL"), or
00029  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00030  * in which case the provisions of the GPL or the LGPL are applicable instead
00031  * of those above. If you wish to allow use of your version of this file only
00032  * under the terms of either the GPL or the LGPL, and not to allow others to
00033  * use your version of this file under the terms of the MPL, indicate your
00034  * decision by deleting the provisions above and replace them with the notice
00035  * and other provisions required by the GPL or the LGPL. If you do not delete
00036  * the provisions above, a recipient may use your version of this file under
00037  * the terms of any one of the MPL, the GPL or the LGPL.
00038  *
00039  * ***** END LICENSE BLOCK ***** */
00040 
00041 #include "nsViewSourceHandler.h"
00042 #include "nsViewSourceChannel.h"
00043 #include "nsNetUtil.h"
00044 
00046 
00047 NS_IMPL_ISUPPORTS1(nsViewSourceHandler, nsIProtocolHandler)
00048 
00049 
00050 // nsIProtocolHandler methods:
00051 
00052 NS_IMETHODIMP
00053 nsViewSourceHandler::GetScheme(nsACString &result)
00054 {
00055     result.AssignLiteral("view-source");
00056     return NS_OK;
00057 }
00058 
00059 NS_IMETHODIMP
00060 nsViewSourceHandler::GetDefaultPort(PRInt32 *result)
00061 {
00062     *result = -1;
00063     return NS_OK;
00064 }
00065 
00066 NS_IMETHODIMP
00067 nsViewSourceHandler::GetProtocolFlags(PRUint32 *result)
00068 {
00069     *result = URI_NORELATIVE | URI_NOAUTH;
00070     return NS_OK;
00071 }
00072 
00073 NS_IMETHODIMP
00074 nsViewSourceHandler::NewURI(const nsACString &aSpec,
00075                             const char *aCharset,
00076                             nsIURI *aBaseURI,
00077                             nsIURI **aResult)
00078 {
00079     nsresult rv;
00080 
00081     // Extract inner URL and normalize to ASCII.  This is done to properly
00082     // support IDN in cases like "view-source:http://www.szalagavató.hu/"
00083 
00084     PRInt32 colon = aSpec.FindChar(':');
00085     if (colon == kNotFound)
00086         return NS_ERROR_MALFORMED_URI;
00087 
00088     nsCOMPtr<nsIURI> innerURI;
00089     rv = NS_NewURI(getter_AddRefs(innerURI), Substring(aSpec, colon + 1), aCharset);
00090     if (NS_FAILED(rv))
00091         return rv;
00092 
00093     nsCAutoString asciiSpec;
00094     rv = innerURI->GetAsciiSpec(asciiSpec);
00095     if (NS_FAILED(rv))
00096         return rv;
00097 
00098     // put back our scheme and construct a simple-uri wrapper
00099 
00100     asciiSpec.Insert("view-source:", 0);
00101 
00102     nsIURI *uri;
00103     rv = CallCreateInstance(NS_SIMPLEURI_CONTRACTID, nsnull, &uri);
00104     if (NS_FAILED(rv))
00105         return rv;
00106     
00107     rv = uri->SetSpec(asciiSpec);
00108     if (NS_FAILED(rv))
00109         NS_RELEASE(uri);
00110     else
00111         *aResult = uri;
00112     return rv;
00113 }
00114 
00115 NS_IMETHODIMP
00116 nsViewSourceHandler::NewChannel(nsIURI* uri, nsIChannel* *result)
00117 {
00118     NS_ENSURE_ARG_POINTER(uri);
00119     nsViewSourceChannel *channel = new nsViewSourceChannel();
00120     if (!channel)
00121         return NS_ERROR_OUT_OF_MEMORY;
00122     NS_ADDREF(channel);
00123 
00124     nsresult rv = channel->Init(uri);
00125     if (NS_FAILED(rv)) {
00126         NS_RELEASE(channel);
00127         return rv;
00128     }
00129 
00130     *result = NS_STATIC_CAST(nsIViewSourceChannel*, channel);
00131     return NS_OK;
00132 }
00133 
00134 NS_IMETHODIMP 
00135 nsViewSourceHandler::AllowPort(PRInt32 port, const char *scheme, PRBool *_retval)
00136 {
00137     // don't override anything.  
00138     *_retval = PR_FALSE;
00139     return NS_OK;
00140 }