CFLib.org – Common Function Library Project

GetPasswordFromURL(this_url)

Last updated February 22, 2002

author

Shawn Seley

Version: 1 | Requires: CF5 | Library: StrLib

Description:
Returns the password (ending with "@") for the supplied URL. If no password is found, then returns an empty string. Works with any protocol that follows a "username:password@" syntax including ftp, telnet, and imap and others.

Return Values:
Returns a string.

Example:

<cfoutput>
#GetPasswordFromURL("telnet://guest:pass@somehost.com:21/")#<br>
#GetPasswordFromURL("guest:pass@somehost.com")#<br>
#GetPasswordFromURL("http://host.com/")#<br>
#GetPasswordFromURL("ftp://me:@host.com/")#<br>
#GetPasswordFromURL("ftp://me@host.com/")#<br>
#GetPasswordFromURL("ftp://:@host.com/")#<br>
</cfoutput>

Parameters:

Name Description Required
this_url URL to parse. Yes

Full UDF Source:

/**
 * Returns the password from a specified URL.
 * 
 * @param this_url      URL to parse. 
 * @return Returns a string. 
 * @author Shawn Seley (shawnse@aol.com) 
 * @version 1, February 21, 2002 
 */
function GetPasswordFromURL(this_url) {
    var first_char       = "";
    var re_found_struct  = "";
    
    this_url = trim(this_url);
    
    first_char = Left(this_url, 1);
    if (Find(first_char, "./")) {
        return "";   // relative URL = no password   (ex: "../dir1/filename.html" or "/dir1/filename.html")
    } else if(Find("://", this_url)){
        // absolute URL    (ex: "ftp://user:pass@ftp.host.com")
        re_found_struct = REFind("[^:]*:([^@]*@)", this_url, Find("://", this_url)+3, "True");
    } else {
        // abbreviated URL (ex: "user:pass@ftp.host.com")
        re_found_struct = REFind("[^:]*:([^@]*@)", this_url, 1, "True");
    }
    
    if (re_found_struct.pos[1] GT 0) {
        return Mid(this_url, re_found_struct.pos[2], re_found_struct.len[2]-1);
    } else {
        return "";
    }
}

Search CFLib.org


Latest Additions

Raymond Camden added
QueryDeleteRows
November 04, 2017

Leigh added
nullPad
May 11, 2016

Raymond Camden added
stripHTML
May 10, 2016

Kevin Cotton added
date2ExcelDate
May 05, 2016

Raymond Camden added
CapFirst
April 25, 2016

Created by Raymond Camden / Design by Justin Johnson