Back to index

lightning-sunbird  0.9+nobinonly
nsLDAPProtocolHandler.cpp
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
00002  * 
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 the mozilla.org LDAP XPCOM SDK.
00017  *
00018  * The Initial Developer of the Original Code is
00019  * Netscape Communications Corporation.
00020  * Portions created by the Initial Developer are Copyright (C) 2000
00021  * the Initial Developer. All Rights Reserved.
00022  *
00023  * Contributor(s):
00024  *   Dan Mosedale <dmose@mozilla.org>
00025  *   Brian Ryner <bryner@brianryner.com>
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 "nsLDAPProtocolHandler.h"
00042 #include "nsCRT.h"
00043 #include "nsIComponentManager.h"
00044 #include "nsLDAPURL.h"
00045 #include "nsLDAPChannel.h"
00046 
00047 static NS_DEFINE_CID(kLDAPURLCID, NS_LDAPURL_CID);
00048 
00049 // QueryInterface, AddRef, and Release
00050 //
00051 NS_IMPL_ISUPPORTS1(nsLDAPProtocolHandler, nsIProtocolHandler)
00052 
00053 nsLDAPProtocolHandler::nsLDAPProtocolHandler()
00054 {
00055 }
00056 
00057 nsLDAPProtocolHandler::~nsLDAPProtocolHandler()
00058 {
00059 }
00060 
00061 // nsIProtocolHandler methods
00062 
00063 // getter method for scheme attr
00064 //
00065 NS_IMETHODIMP
00066 nsLDAPProtocolHandler::GetScheme(nsACString &result)
00067 {
00068   result = "ldap";
00069   return NS_OK;
00070 }
00071 
00072 // getter method for defaultPort attribute
00073 //
00074 NS_IMETHODIMP
00075 nsLDAPProtocolHandler::GetDefaultPort(PRInt32 *result)
00076 {
00077   *result = 389;
00078   return NS_OK;
00079 }
00080 
00081 // getter method for protocol flags attribute
00082 //
00083 NS_IMETHODIMP
00084 nsLDAPProtocolHandler::GetProtocolFlags(PRUint32 *result)
00085 {
00086   *result = URI_NORELATIVE;
00087   return NS_OK;
00088 }
00089 
00090 // construct an appropriate URI
00091 //
00092 NS_IMETHODIMP
00093 nsLDAPProtocolHandler::NewURI(const nsACString &aSpec,
00094                               const char *aOriginCharset, // ignored
00095                               nsIURI *aBaseURI,
00096                               nsIURI **result) 
00097 {
00098     nsCOMPtr<nsILDAPURL> url;
00099     nsresult rv;
00100 
00101     url = do_CreateInstance(kLDAPURLCID, &rv);
00102     NS_ENSURE_SUCCESS(rv, rv);
00103 
00104     // XXX - better error handling
00105     //
00106     rv = url->SetSpec(aSpec);
00107     NS_ENSURE_SUCCESS(rv, rv);
00108 
00109     // this is a getter, so we need to AddRef on the way out
00110     //
00111     *result = url;
00112     NS_ADDREF(*result);
00113 
00114     return NS_OK;
00115 }
00116 
00117 NS_IMETHODIMP
00118 nsLDAPProtocolHandler::NewChannel(nsIURI* uri, 
00119                                   nsIChannel* *result)
00120 {
00121     NS_ENSURE_ARG_POINTER(uri);
00122     nsresult rv;
00123     nsLDAPChannel *channel;
00124 
00125     rv = nsLDAPChannel::Create(0, NS_GET_IID(nsIChannel),
00126                                NS_REINTERPRET_CAST(void **, &channel));
00127     NS_ENSURE_SUCCESS(rv, rv);
00128   
00129     rv = channel->Init(uri);
00130     if (NS_FAILED(rv)) {
00131         NS_RELEASE(channel);
00132         return rv;
00133     }
00134     // the channel was already AddRefed for us, and since this function itself
00135     // is a getter, there's no need to release it here.
00136     //
00137     *result = channel;
00138 
00139     return NS_OK;
00140 }
00141 
00142 NS_IMETHODIMP 
00143 nsLDAPProtocolHandler::AllowPort(PRInt32 port, const char *scheme, PRBool *_retval)
00144 {
00145     if (port == 389 || port == 636)  // 636 is LDAP/SSL
00146         *_retval = PR_TRUE;
00147     else
00148         *_retval = PR_FALSE;
00149     return NS_OK;
00150 }
00151