Back to index

lightning-sunbird  0.9+nobinonly
nsGopherHandler.cpp
Go to the documentation of this file.
00001 
00002 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
00003  *
00004  * ***** BEGIN LICENSE BLOCK *****
00005  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00006  *
00007  * The contents of this file are subject to the Mozilla Public License Version
00008  * 1.1 (the "License"); you may not use this file except in compliance with
00009  * the License. You may obtain a copy of the License at
00010  * http://www.mozilla.org/MPL/
00011  *
00012  * Software distributed under the License is distributed on an "AS IS" basis,
00013  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00014  * for the specific language governing rights and limitations under the
00015  * License.
00016  *
00017  * The Original Code is the Gopher protocol code.
00018  *
00019  * The Initial Developer of the Original Code is
00020  * Bradley Baetz.
00021  * Portions created by the Initial Developer are Copyright (C) 2000
00022  * the Initial Developer. All Rights Reserved.
00023  *
00024  * Contributor(s):
00025  *   Bradley Baetz <bbaetz@student.usyd.edu.au>
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 // This is based rather heavily on the datetime and finger implementations
00042 // and documentation
00043 
00044 #include "nspr.h"
00045 #include "nsGopherChannel.h"
00046 #include "nsGopherHandler.h"
00047 #include "nsIURL.h"
00048 #include "nsCRT.h"
00049 #include "nsIComponentManager.h"
00050 #include "nsIServiceManager.h"
00051 #include "nsIInterfaceRequestor.h"
00052 #include "nsIInterfaceRequestorUtils.h"
00053 #include "nsIProgressEventSink.h"
00054 #include "nsIStandardURL.h"
00055 #include "nsNetUtil.h"
00056 #include "prlog.h"
00057 
00058 #ifdef PR_LOGGING
00059 PRLogModuleInfo* gGopherLog = nsnull;
00060 #endif
00061 
00062 static NS_DEFINE_CID(kStandardURLCID, NS_STANDARDURL_CID);
00063 
00065 
00066 nsGopherHandler::nsGopherHandler() {
00067 #ifdef PR_LOGGING
00068     if (!gGopherLog)
00069         gGopherLog = PR_NewLogModule("nsGopherProtocol");
00070 #endif
00071 }
00072 
00073 nsGopherHandler::~nsGopherHandler() {
00074     PR_LOG(gGopherLog, PR_LOG_ALWAYS, ("~nsGopherHandler() called"));
00075 }
00076 
00077 NS_IMPL_THREADSAFE_ISUPPORTS2(nsGopherHandler,
00078                               nsIProxiedProtocolHandler,
00079                               nsIProtocolHandler)
00080 
00081 NS_METHOD
00082 nsGopherHandler::Create(nsISupports* aOuter, const nsIID& aIID, void* *aResult) {
00083     nsGopherHandler* gh = new nsGopherHandler();
00084     if (!gh)
00085         return NS_ERROR_OUT_OF_MEMORY;
00086     NS_ADDREF(gh);
00087     nsresult rv = gh->QueryInterface(aIID, aResult);
00088     NS_RELEASE(gh);
00089     return rv;
00090 }
00091     
00093 // nsIProtocolHandler methods:
00094 
00095 NS_IMETHODIMP
00096 nsGopherHandler::GetScheme(nsACString &result) {
00097     result.AssignLiteral("gopher");
00098     return NS_OK;
00099 }
00100 
00101 NS_IMETHODIMP
00102 nsGopherHandler::GetDefaultPort(PRInt32 *result) {
00103     *result = GOPHER_PORT;
00104     return NS_OK;
00105 }
00106 
00107 NS_IMETHODIMP
00108 nsGopherHandler::GetProtocolFlags(PRUint32 *result) {
00109     *result = URI_NORELATIVE | ALLOWS_PROXY | ALLOWS_PROXY_HTTP;
00110     return NS_OK;
00111 }
00112 
00113 NS_IMETHODIMP
00114 nsGopherHandler::NewURI(const nsACString &aSpec,
00115                         const char *aCharset,
00116                         nsIURI *aBaseURI,
00117                         nsIURI **result) {
00118     nsresult rv;
00119 
00120     nsCOMPtr<nsIStandardURL> url = do_CreateInstance(kStandardURLCID, &rv);
00121     if (NS_FAILED(rv)) return rv;
00122 
00123     rv = url->Init(nsIStandardURL::URLTYPE_STANDARD, GOPHER_PORT,
00124                    aSpec, aCharset, aBaseURI);
00125     if (NS_FAILED(rv)) return rv;
00126 
00127     return CallQueryInterface(url, result);
00128 }
00129 
00130 NS_IMETHODIMP
00131 nsGopherHandler::NewProxiedChannel(nsIURI* url, nsIProxyInfo* proxyInfo,
00132                                    nsIChannel* *result)
00133 {
00134     NS_ENSURE_ARG_POINTER(url);
00135     nsGopherChannel *chan = new nsGopherChannel();
00136     if (!chan)
00137         return NS_ERROR_OUT_OF_MEMORY;
00138     NS_ADDREF(chan);
00139     
00140     nsresult rv = chan->Init(url, proxyInfo);
00141     if (NS_FAILED(rv)) {
00142         NS_RELEASE(chan);
00143         return rv;
00144     }
00145 
00146     *result = chan;
00147     return rv;
00148 }
00149 
00150 NS_IMETHODIMP
00151 nsGopherHandler::NewChannel(nsIURI* url, nsIChannel* *result)
00152 {
00153     return NewProxiedChannel(url, nsnull, result);
00154 }
00155 
00156 NS_IMETHODIMP 
00157 nsGopherHandler::AllowPort(PRInt32 port, const char *scheme, PRBool *_retval)
00158 {
00159     if (port == GOPHER_PORT)
00160         *_retval = PR_TRUE;
00161     else
00162         *_retval = PR_FALSE;
00163     return NS_OK;
00164 }