зеркало из https://github.com/mozilla/gecko-dev.git
New files from andreas.otte@primus-online.de for numerous URL bugs. r=warren
This commit is contained in:
Родитель
04815a8996
Коммит
472898d037
|
@ -0,0 +1,436 @@
|
|||
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
|
||||
*
|
||||
* The contents of this file are subject to the Netscape Public
|
||||
* License Version 1.1 (the "License"); you may not use this file
|
||||
* except in compliance with the License. You may obtain a copy of
|
||||
* the License at http://www.mozilla.org/NPL/
|
||||
*
|
||||
* Software distributed under the License is distributed on an "AS
|
||||
* IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
|
||||
* implied. See the License for the specific language governing
|
||||
* rights and limitations under the License.
|
||||
*
|
||||
* The Original Code is mozilla.org code.
|
||||
*
|
||||
* The Initial Developer of the Original Code is Andreas Otte.
|
||||
*
|
||||
* Contributor(s):
|
||||
*/
|
||||
|
||||
#include "nsAuthURLParser.h"
|
||||
#include "nsURLHelper.h"
|
||||
#include "nsCRT.h"
|
||||
#include "nsString.h"
|
||||
#include "prprf.h"
|
||||
|
||||
NS_IMPL_ISUPPORTS1(nsAuthURLParser,
|
||||
nsIURLParser)
|
||||
|
||||
nsAuthURLParser::~nsAuthURLParser()
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
NS_METHOD
|
||||
nsAuthURLParser::Create(nsISupports *aOuter, REFNSIID aIID, void **aResult)
|
||||
{
|
||||
if (aOuter)
|
||||
return NS_ERROR_NO_AGGREGATION;
|
||||
nsAuthURLParser* p = new nsAuthURLParser();
|
||||
if (p == nsnull)
|
||||
return NS_ERROR_OUT_OF_MEMORY;
|
||||
NS_ADDREF(p);
|
||||
nsresult rv = p->QueryInterface(aIID, aResult);
|
||||
NS_RELEASE(p);
|
||||
return rv;
|
||||
}
|
||||
|
||||
nsresult
|
||||
nsAuthURLParser::ParseAtScheme(const char* i_Spec, char* *o_Scheme,
|
||||
char* *o_Username, char* *o_Password,
|
||||
char* *o_Host, PRInt32 *o_Port, char* *o_Path)
|
||||
{
|
||||
nsresult rv = NS_OK;
|
||||
|
||||
NS_PRECONDITION( (nsnull != i_Spec), "Parse called on empty url!");
|
||||
if (!i_Spec)
|
||||
return NS_ERROR_MALFORMED_URI;
|
||||
|
||||
int len = PL_strlen(i_Spec);
|
||||
if (len >= 2 && *i_Spec == '/' && *(i_Spec+1) == '/') // No Scheme
|
||||
{
|
||||
rv = ParseAtPreHost(i_Spec, o_Username, o_Password, o_Host, o_Port,
|
||||
o_Path);
|
||||
return rv;
|
||||
}
|
||||
|
||||
static const char delimiters[] = "/:@?"; //this order is optimized.
|
||||
char* brk = PL_strpbrk(i_Spec, delimiters);
|
||||
|
||||
if (!brk) // everything is a host
|
||||
{
|
||||
rv = ExtractString((char*)i_Spec, o_Host, len);
|
||||
return rv;
|
||||
} else
|
||||
len = PL_strlen(brk);
|
||||
|
||||
switch (*brk)
|
||||
{
|
||||
case '/' :
|
||||
case '?' :
|
||||
// If the URL starts with a slash then everything is a path
|
||||
if (brk == i_Spec)
|
||||
{
|
||||
rv = ParseAtPath(brk, o_Path);
|
||||
return rv;
|
||||
}
|
||||
else // The first part is host, so its host/path
|
||||
{
|
||||
rv = ExtractString((char*)i_Spec, o_Host, (brk - i_Spec));
|
||||
if (NS_FAILED(rv))
|
||||
return rv;
|
||||
rv = ParseAtPath(brk, o_Path);
|
||||
return rv;
|
||||
}
|
||||
break;
|
||||
case ':' :
|
||||
if (len >= 2 && *(brk+1) == '/') {
|
||||
// Standard http://... or malformed http:/...
|
||||
rv = ExtractString((char*)i_Spec, o_Scheme, (brk - i_Spec));
|
||||
if (NS_FAILED(rv))
|
||||
return rv;
|
||||
rv = ParseAtPreHost(brk+1, o_Username, o_Password, o_Host,
|
||||
o_Port, o_Path);
|
||||
return rv;
|
||||
} else {
|
||||
// Could be host:port, so try conversion to number
|
||||
PRInt32 port = ExtractPortFrom(brk+1);
|
||||
if (port > 0)
|
||||
{
|
||||
rv = ExtractString((char*)i_Spec, o_Host, (brk - i_Spec));
|
||||
if (NS_FAILED(rv))
|
||||
return rv;
|
||||
rv = ParseAtPort(brk+1, o_Port, o_Path);
|
||||
return rv;
|
||||
} else {
|
||||
// No, it's not a number try scheme:host...
|
||||
rv = ExtractString((char*)i_Spec, o_Scheme,
|
||||
(brk - i_Spec));
|
||||
if (NS_FAILED(rv))
|
||||
return rv;
|
||||
rv = ParseAtPreHost(brk+1, o_Username, o_Password, o_Host,
|
||||
o_Port, o_Path);
|
||||
return rv;
|
||||
}
|
||||
}
|
||||
break;
|
||||
case '@' :
|
||||
rv = ParseAtPreHost(i_Spec, o_Username, o_Password, o_Host,
|
||||
o_Port, o_Path);
|
||||
return rv;
|
||||
break;
|
||||
default:
|
||||
NS_ASSERTION(0, "This just can't be!");
|
||||
break;
|
||||
}
|
||||
return NS_OK;
|
||||
}
|
||||
|
||||
nsresult
|
||||
nsAuthURLParser::ParseAtPreHost(const char* i_Spec, char* *o_Username,
|
||||
char* *o_Password, char* *o_Host,
|
||||
PRInt32 *o_Port, char* *o_Path)
|
||||
{
|
||||
nsresult rv = NS_OK;
|
||||
// Skip leading two slashes
|
||||
char* fwdPtr= (char*) i_Spec;
|
||||
if (fwdPtr && (*fwdPtr != '\0') && (*fwdPtr == '/'))
|
||||
fwdPtr++;
|
||||
if (fwdPtr && (*fwdPtr != '\0') && (*fwdPtr == '/'))
|
||||
fwdPtr++;
|
||||
|
||||
// Search for @
|
||||
static const char delimiters[] = "@";
|
||||
char* brk = PL_strpbrk(fwdPtr, delimiters);
|
||||
|
||||
if (brk)
|
||||
{
|
||||
char* e_PreHost = nsnull;
|
||||
rv = ExtractString(fwdPtr, &e_PreHost, (brk - fwdPtr));
|
||||
if (NS_FAILED(rv)) {
|
||||
CRTFREEIF(e_PreHost);
|
||||
return rv;
|
||||
}
|
||||
rv = ParsePreHost(e_PreHost,o_Username,o_Password);
|
||||
CRTFREEIF(e_PreHost);
|
||||
if (NS_FAILED(rv))
|
||||
return rv;
|
||||
|
||||
rv = ParseAtHost(brk+1, o_Host, o_Port, o_Path);
|
||||
} else {
|
||||
rv = ParseAtHost(fwdPtr, o_Host, o_Port, o_Path);
|
||||
}
|
||||
return rv;
|
||||
}
|
||||
|
||||
nsresult
|
||||
nsAuthURLParser::ParseAtHost(const char* i_Spec, char* *o_Host,
|
||||
PRInt32 *o_Port, char* *o_Path)
|
||||
{
|
||||
nsresult rv = NS_OK;
|
||||
|
||||
int len = PL_strlen(i_Spec);
|
||||
static const char delimiters[] = ":/?"; //this order is optimized.
|
||||
char* brk = PL_strpbrk(i_Spec, delimiters);
|
||||
if (!brk) // everything is a host
|
||||
{
|
||||
rv = ExtractString((char*)i_Spec, o_Host, len);
|
||||
return rv;
|
||||
}
|
||||
|
||||
switch (*brk)
|
||||
{
|
||||
case '/' :
|
||||
case '?' :
|
||||
// Get the Host, the rest is Path
|
||||
rv = ExtractString((char*)i_Spec, o_Host, (brk - i_Spec));
|
||||
if (NS_FAILED(rv))
|
||||
return rv;
|
||||
rv = ParseAtPath(brk, o_Path);
|
||||
return rv;
|
||||
break;
|
||||
case ':' :
|
||||
// Get the Host
|
||||
rv = ExtractString((char*)i_Spec, o_Host, (brk - i_Spec));
|
||||
if (NS_FAILED(rv))
|
||||
return rv;
|
||||
rv = ParseAtPort(brk+1, o_Port, o_Path);
|
||||
return rv;
|
||||
break;
|
||||
default:
|
||||
NS_ASSERTION(0, "This just can't be!");
|
||||
break;
|
||||
}
|
||||
return NS_OK;
|
||||
}
|
||||
|
||||
nsresult
|
||||
nsAuthURLParser::ParseAtPort(const char* i_Spec, PRInt32 *o_Port,
|
||||
char* *o_Path)
|
||||
{
|
||||
nsresult rv = NS_OK;
|
||||
static const char delimiters[] = "/?"; //this order is optimized.
|
||||
char* brk = PL_strpbrk(i_Spec, delimiters);
|
||||
if (!brk) // everything is a Port
|
||||
{
|
||||
*o_Port = ExtractPortFrom(i_Spec);
|
||||
if (*o_Port <= 0)
|
||||
return NS_ERROR_MALFORMED_URI;
|
||||
else
|
||||
return NS_OK;
|
||||
}
|
||||
|
||||
switch (*brk)
|
||||
{
|
||||
case '/' :
|
||||
case '?' :
|
||||
// Get the Port, the rest is Path
|
||||
*o_Port = ExtractPortFrom(i_Spec);
|
||||
if (*o_Port <= 0)
|
||||
return NS_ERROR_MALFORMED_URI;
|
||||
rv = ParseAtPath(brk, o_Path);
|
||||
return rv;
|
||||
break;
|
||||
default:
|
||||
NS_ASSERTION(0, "This just can't be!");
|
||||
break;
|
||||
}
|
||||
return NS_OK;
|
||||
}
|
||||
|
||||
nsresult
|
||||
nsAuthURLParser::ParseAtPath(const char* i_Spec, char* *o_Path)
|
||||
{
|
||||
// Just write the path and check for a starting /
|
||||
nsAutoString dir;
|
||||
if ('/' != *i_Spec)
|
||||
dir += "/";
|
||||
|
||||
dir += i_Spec;
|
||||
|
||||
*o_Path = dir.ToNewCString();
|
||||
return (*o_Path ? NS_OK : NS_ERROR_OUT_OF_MEMORY);
|
||||
}
|
||||
|
||||
nsresult
|
||||
nsAuthURLParser::ParseAtDirectory(const char* i_Path, char* *o_Directory,
|
||||
char* *o_FileBaseName, char* *o_FileExtension,
|
||||
char* *o_Param, char* *o_Query, char* *o_Ref)
|
||||
{
|
||||
// Cleanout
|
||||
CRTFREEIF(*o_Directory);
|
||||
CRTFREEIF(*o_FileBaseName);
|
||||
CRTFREEIF(*o_FileExtension);
|
||||
CRTFREEIF(*o_Param);
|
||||
CRTFREEIF(*o_Query);
|
||||
CRTFREEIF(*o_Ref);
|
||||
|
||||
nsresult rv = NS_OK;
|
||||
|
||||
// Parse the Path into its components
|
||||
if (!i_Path)
|
||||
{
|
||||
DupString(o_Directory, "/");
|
||||
return (o_Directory ? NS_OK : NS_ERROR_OUT_OF_MEMORY);
|
||||
}
|
||||
|
||||
char* dirfile = nsnull;
|
||||
char* options = nsnull;
|
||||
|
||||
int len = PL_strlen(i_Path);
|
||||
|
||||
/* Factor out the optionpart with ;?# */
|
||||
static const char delimiters[] = ";?#"; // for param, query and ref
|
||||
char* brk = PL_strpbrk(i_Path, delimiters);
|
||||
|
||||
if (!brk) // Everything is just path and filename
|
||||
{
|
||||
DupString(&dirfile, i_Path);
|
||||
}
|
||||
else
|
||||
{
|
||||
int dirfileLen = brk - i_Path;
|
||||
ExtractString((char*)i_Path, &dirfile, dirfileLen);
|
||||
len -= dirfileLen;
|
||||
ExtractString((char*)i_Path + dirfileLen, &options, len);
|
||||
brk = options;
|
||||
}
|
||||
|
||||
/* now that we have broken up the path treat every part differently */
|
||||
/* first dir+file */
|
||||
|
||||
char* file;
|
||||
|
||||
int dlen = PL_strlen(dirfile);
|
||||
if (dlen == 0)
|
||||
{
|
||||
DupString(o_Directory, "/");
|
||||
file = dirfile;
|
||||
} else {
|
||||
CoaleseDirs(dirfile);
|
||||
// Get length again
|
||||
dlen = PL_strlen(dirfile);
|
||||
|
||||
// First find the last slash
|
||||
file = PL_strrchr(dirfile, '/');
|
||||
if (!file)
|
||||
{
|
||||
DupString(o_Directory, "/");
|
||||
file = dirfile;
|
||||
}
|
||||
|
||||
// If its not the same as the first slash then extract directory
|
||||
if (file != dirfile)
|
||||
{
|
||||
ExtractString(dirfile, o_Directory, (file - dirfile)+1);
|
||||
} else {
|
||||
DupString(o_Directory, "/");
|
||||
}
|
||||
}
|
||||
|
||||
/* Extract FileBaseName and FileExtension */
|
||||
if (dlen > 0) {
|
||||
// Look again if there was a slash
|
||||
char* slash = PL_strrchr(dirfile, '/');
|
||||
char* e_FileName = nsnull;
|
||||
if (slash) {
|
||||
ExtractString(file+1, &e_FileName, dlen-(slash-dirfile+1));
|
||||
} else {
|
||||
// Use the full String as Filename
|
||||
ExtractString(dirfile, &e_FileName, dlen);
|
||||
}
|
||||
|
||||
rv = ParseFileName(e_FileName,o_FileBaseName,o_FileExtension);
|
||||
|
||||
CRTFREEIF(e_FileName);
|
||||
}
|
||||
|
||||
// Now take a look at the options. "#" has precedence over "?"
|
||||
// which has precedence over ";"
|
||||
if (options) {
|
||||
// Look for "#" first. Everything following it is in the ref
|
||||
brk = PL_strchr(options, '#');
|
||||
if (brk) {
|
||||
int pieceLen = len - (brk + 1 - options);
|
||||
ExtractString(brk+1, o_Ref, pieceLen);
|
||||
len -= pieceLen + 1;
|
||||
*brk = '\0';
|
||||
}
|
||||
|
||||
// Now look for "?"
|
||||
brk = PL_strchr(options, '?');
|
||||
if (brk) {
|
||||
int pieceLen = len - (brk + 1 - options);
|
||||
ExtractString(brk+1, o_Query, pieceLen);
|
||||
len -= pieceLen + 1;
|
||||
}
|
||||
|
||||
// Now look for ';'
|
||||
brk = PL_strchr(options, ';');
|
||||
if (brk) {
|
||||
int pieceLen = len - (brk + 1 - options);
|
||||
ExtractString(brk+1, o_Param, pieceLen);
|
||||
len -= pieceLen + 1;
|
||||
*brk = '\0';
|
||||
}
|
||||
}
|
||||
|
||||
nsCRT::free(dirfile);
|
||||
nsCRT::free(options);
|
||||
return rv;
|
||||
}
|
||||
|
||||
nsresult
|
||||
nsAuthURLParser::ParsePreHost(const char* i_PreHost, char* *o_Username,
|
||||
char* *o_Password)
|
||||
{
|
||||
nsresult rv = NS_OK;
|
||||
// Search for :
|
||||
static const char delimiters[] = ":";
|
||||
char* brk = PL_strpbrk(i_PreHost, delimiters);
|
||||
if (brk)
|
||||
{
|
||||
rv = ExtractString((char*)i_PreHost, o_Username, (brk - i_PreHost));
|
||||
if (NS_FAILED(rv))
|
||||
return rv;
|
||||
rv = ExtractString(brk+1, o_Password,
|
||||
(i_PreHost+PL_strlen(i_PreHost) - brk - 1));
|
||||
} else {
|
||||
CRTFREEIF(*o_Password);
|
||||
rv = DupString(o_Username, i_PreHost);
|
||||
}
|
||||
return rv;
|
||||
}
|
||||
|
||||
nsresult
|
||||
nsAuthURLParser::ParseFileName(const char* i_FileName, char* *o_FileBaseName,
|
||||
char* *o_FileExtension)
|
||||
{
|
||||
nsresult rv = NS_OK;
|
||||
// Search for FileExtension
|
||||
// Search for last .
|
||||
// Ignore . at the beginning
|
||||
char* brk = PL_strrchr(i_FileName+1, '.');
|
||||
if (brk)
|
||||
{
|
||||
rv = ExtractString((char*)i_FileName, o_FileBaseName,
|
||||
(brk - i_FileName));
|
||||
if (NS_FAILED(rv))
|
||||
return rv;
|
||||
rv = ExtractString(brk + 1, o_FileExtension,
|
||||
(i_FileName+PL_strlen(i_FileName) - brk - 1));
|
||||
} else {
|
||||
rv = DupString(o_FileBaseName, i_FileName);
|
||||
}
|
||||
return rv;
|
||||
}
|
|
@ -0,0 +1,59 @@
|
|||
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 4 -*-
|
||||
*
|
||||
* The contents of this file are subject to the Netscape Public
|
||||
* License Version 1.1 (the "License"); you may not use this file
|
||||
* except in compliance with the License. You may obtain a copy of
|
||||
* the License at http://www.mozilla.org/NPL/
|
||||
*
|
||||
* Software distributed under the License is distributed on an "AS
|
||||
* IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
|
||||
* implied. See the License for the specific language governing
|
||||
* rights and limitations under the License.
|
||||
*
|
||||
* The Original Code is mozilla.org code.
|
||||
*
|
||||
* The Initial Developer of the Original Code is Netscape
|
||||
* Communications Corporation. Portions created by Netscape are
|
||||
* Copyright (C) 1998 Netscape Communications Corporation. All
|
||||
* Rights Reserved.
|
||||
*
|
||||
* Contributor(s):
|
||||
*/
|
||||
|
||||
#ifndef nsAuthURLParser_h__
|
||||
#define nsAuthURLParser_h__
|
||||
|
||||
#include "nsIURLParser.h"
|
||||
#include "nsIURI.h"
|
||||
#include "nsAgg.h"
|
||||
#include "nsCRT.h"
|
||||
|
||||
#define NS_AUTHORITYURLPARSER_CID \
|
||||
{ /* 90012125-1616-4fa1-ae14-4e7fa5766eb6 */ \
|
||||
0x90012125, \
|
||||
0x1616, \
|
||||
0x4fa1, \
|
||||
{0xae, 0x14, 0x4e, 0x7f, 0xa5, 0x76, 0x6e, 0xb6} \
|
||||
}
|
||||
|
||||
class nsAuthURLParser : public nsIURLParser
|
||||
{
|
||||
public:
|
||||
NS_DECL_ISUPPORTS
|
||||
///////////////////////////////////////////////////////////////////////////
|
||||
// nsAuthURLParser methods:
|
||||
nsAuthURLParser() {
|
||||
NS_INIT_REFCNT();
|
||||
}
|
||||
virtual ~nsAuthURLParser();
|
||||
|
||||
static NS_METHOD
|
||||
Create(nsISupports *aOuter, REFNSIID aIID, void **aResult);
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////
|
||||
// nsIURLParser methods:
|
||||
NS_DECL_NSIURLPARSER
|
||||
|
||||
};
|
||||
|
||||
#endif // nsAuthURLParser_h__
|
|
@ -0,0 +1,301 @@
|
|||
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
|
||||
*
|
||||
* The contents of this file are subject to the Netscape Public
|
||||
* License Version 1.1 (the "License"); you may not use this file
|
||||
* except in compliance with the License. You may obtain a copy of
|
||||
* the License at http://www.mozilla.org/NPL/
|
||||
*
|
||||
* Software distributed under the License is distributed on an "AS
|
||||
* IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
|
||||
* implied. See the License for the specific language governing
|
||||
* rights and limitations under the License.
|
||||
*
|
||||
* The Original Code is mozilla.org code.
|
||||
*
|
||||
* The Initial Developer of the Original Code is Andreas Otte.
|
||||
*
|
||||
* Contributor(s):
|
||||
*/
|
||||
|
||||
#include "nsNoAuthURLParser.h"
|
||||
#include "nsURLHelper.h"
|
||||
#include "nsCRT.h"
|
||||
#include "nsString.h"
|
||||
#include "prprf.h"
|
||||
|
||||
NS_IMPL_ISUPPORTS1(nsNoAuthURLParser,
|
||||
nsIURLParser)
|
||||
|
||||
nsNoAuthURLParser::~nsNoAuthURLParser()
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
NS_METHOD
|
||||
nsNoAuthURLParser::Create(nsISupports *aOuter, REFNSIID aIID, void **aResult)
|
||||
{
|
||||
if (aOuter)
|
||||
return NS_ERROR_NO_AGGREGATION;
|
||||
nsNoAuthURLParser* p = new nsNoAuthURLParser();
|
||||
if (p == nsnull)
|
||||
return NS_ERROR_OUT_OF_MEMORY;
|
||||
NS_ADDREF(p);
|
||||
nsresult rv = p->QueryInterface(aIID, aResult);
|
||||
NS_RELEASE(p);
|
||||
return rv;
|
||||
}
|
||||
|
||||
nsresult
|
||||
nsNoAuthURLParser::ParseAtScheme(const char* i_Spec, char* *o_Scheme,
|
||||
char* *o_Username, char* *o_Password,
|
||||
char* *o_Host, PRInt32 *o_Port, char* *o_Path)
|
||||
{
|
||||
nsresult rv = NS_OK;
|
||||
|
||||
NS_PRECONDITION( (nsnull != i_Spec), "Parse called on empty url!");
|
||||
if (!i_Spec)
|
||||
return NS_ERROR_MALFORMED_URI;
|
||||
|
||||
int len = PL_strlen(i_Spec);
|
||||
if (len >= 2 && *i_Spec == '/' && *(i_Spec+1) == '/') // No Scheme
|
||||
{
|
||||
rv = ParseAtPreHost(i_Spec, o_Username, o_Password, o_Host, o_Port,
|
||||
o_Path);
|
||||
return rv;
|
||||
}
|
||||
|
||||
static const char delimiters[] = ":"; //this order is optimized.
|
||||
char* brk = PL_strpbrk(i_Spec, delimiters);
|
||||
|
||||
if (!brk) // everything is a path
|
||||
{
|
||||
rv = ParseAtPath((char*)i_Spec, o_Path);
|
||||
return rv;
|
||||
}
|
||||
|
||||
switch (*brk)
|
||||
{
|
||||
case ':' :
|
||||
rv = ExtractString((char*)i_Spec, o_Scheme, (brk - i_Spec));
|
||||
if (NS_FAILED(rv))
|
||||
return rv;
|
||||
rv = ParseAtPreHost(brk+1, o_Username, o_Password, o_Host,
|
||||
o_Port, o_Path);
|
||||
return rv;
|
||||
break;
|
||||
default:
|
||||
NS_ASSERTION(0, "This just can't be!");
|
||||
break;
|
||||
}
|
||||
return NS_OK;
|
||||
}
|
||||
|
||||
nsresult
|
||||
nsNoAuthURLParser::ParseAtPreHost(const char* i_Spec, char* *o_Username,
|
||||
char* *o_Password, char* *o_Host,
|
||||
PRInt32 *o_Port, char* *o_Path)
|
||||
{
|
||||
nsresult rv = NS_OK;
|
||||
// Skip leading two slashes
|
||||
char* fwdPtr= (char*) i_Spec;
|
||||
if (fwdPtr && (*fwdPtr != '\0') && (*fwdPtr == '/'))
|
||||
fwdPtr++;
|
||||
if (fwdPtr && (*fwdPtr != '\0') && (*fwdPtr == '/'))
|
||||
fwdPtr++;
|
||||
|
||||
// There is no PreHost
|
||||
rv = ParseAtHost(fwdPtr, o_Host, o_Port, o_Path);
|
||||
return rv;
|
||||
|
||||
}
|
||||
|
||||
nsresult
|
||||
nsNoAuthURLParser::ParseAtHost(const char* i_Spec, char* *o_Host,
|
||||
PRInt32 *o_Port, char* *o_Path)
|
||||
{
|
||||
nsresult rv = NS_OK;
|
||||
// There is no Host
|
||||
rv = ParseAtPath(i_Spec, o_Path);
|
||||
return rv;
|
||||
}
|
||||
|
||||
nsresult
|
||||
nsNoAuthURLParser::ParseAtPort(const char* i_Spec, PRInt32 *o_Port, char* *o_Path)
|
||||
{
|
||||
nsresult rv = NS_OK;
|
||||
// There is no Port
|
||||
rv = ParseAtPath(i_Spec, o_Path);
|
||||
return NS_OK;
|
||||
}
|
||||
|
||||
nsresult
|
||||
nsNoAuthURLParser::ParseAtPath(const char* i_Spec, char* *o_Path)
|
||||
{
|
||||
// Just write the path and check for a starting /
|
||||
nsAutoString dir;
|
||||
if ('/' != *i_Spec)
|
||||
dir += "/";
|
||||
|
||||
dir += i_Spec;
|
||||
|
||||
*o_Path = dir.ToNewCString();
|
||||
return (*o_Path ? NS_OK : NS_ERROR_OUT_OF_MEMORY);
|
||||
}
|
||||
|
||||
nsresult
|
||||
nsNoAuthURLParser::ParseAtDirectory(const char* i_Path, char* *o_Directory,
|
||||
char* *o_FileBaseName, char* *o_FileExtension,
|
||||
char* *o_Param, char* *o_Query, char* *o_Ref)
|
||||
{
|
||||
// Cleanout
|
||||
CRTFREEIF(*o_Directory);
|
||||
CRTFREEIF(*o_FileBaseName);
|
||||
CRTFREEIF(*o_FileExtension);
|
||||
CRTFREEIF(*o_Param);
|
||||
CRTFREEIF(*o_Query);
|
||||
CRTFREEIF(*o_Ref);
|
||||
|
||||
nsresult rv = NS_OK;
|
||||
|
||||
// Parse the Path into its components
|
||||
if (!i_Path)
|
||||
{
|
||||
DupString(o_Directory, "/");
|
||||
return (o_Directory ? NS_OK : NS_ERROR_OUT_OF_MEMORY);
|
||||
}
|
||||
|
||||
char* dirfile = nsnull;
|
||||
char* options = nsnull;
|
||||
|
||||
int len = PL_strlen(i_Path);
|
||||
|
||||
/* Factor out the optionpart with ;?# */
|
||||
static const char delimiters[] = ";?#"; // for param, query and ref
|
||||
char* brk = PL_strpbrk(i_Path, delimiters);
|
||||
|
||||
if (!brk) // Everything is just path and filename
|
||||
{
|
||||
DupString(&dirfile, i_Path);
|
||||
}
|
||||
else
|
||||
{
|
||||
int dirfileLen = brk - i_Path;
|
||||
ExtractString((char*)i_Path, &dirfile, dirfileLen);
|
||||
len -= dirfileLen;
|
||||
ExtractString((char*)i_Path + dirfileLen, &options, len);
|
||||
brk = options;
|
||||
}
|
||||
|
||||
/* now that we have broken up the path treat every part differently */
|
||||
/* first dir+file */
|
||||
|
||||
char* file;
|
||||
|
||||
int dlen = PL_strlen(dirfile);
|
||||
if (dlen == 0)
|
||||
{
|
||||
DupString(o_Directory, "/");
|
||||
file = dirfile;
|
||||
} else {
|
||||
CoaleseDirs(dirfile);
|
||||
// Get length again
|
||||
dlen = PL_strlen(dirfile);
|
||||
|
||||
// First find the last slash
|
||||
file = PL_strrchr(dirfile, '/');
|
||||
if (!file)
|
||||
{
|
||||
DupString(o_Directory, "/");
|
||||
file = dirfile;
|
||||
}
|
||||
|
||||
// If its not the same as the first slash then extract directory
|
||||
if (file != dirfile)
|
||||
{
|
||||
ExtractString(dirfile, o_Directory, (file - dirfile)+1);
|
||||
} else {
|
||||
DupString(o_Directory, "/");
|
||||
}
|
||||
}
|
||||
|
||||
/* Extract FileBaseName and FileExtension */
|
||||
if (dlen > 0) {
|
||||
// Look again if there was a slash
|
||||
char* slash = PL_strrchr(dirfile, '/');
|
||||
char* e_FileName = nsnull;
|
||||
if (slash) {
|
||||
ExtractString(file+1, &e_FileName, dlen-(slash-dirfile+1));
|
||||
} else {
|
||||
// Use the full String as Filename
|
||||
ExtractString(dirfile, &e_FileName, dlen);
|
||||
}
|
||||
|
||||
rv = ParseFileName(e_FileName,o_FileBaseName,o_FileExtension);
|
||||
CRTFREEIF(e_FileName);
|
||||
}
|
||||
|
||||
// Now take a look at the options. "#" has precedence over "?"
|
||||
// which has precedence over ";"
|
||||
if (options) {
|
||||
// Look for "#" first. Everything following it is in the ref
|
||||
brk = PL_strchr(options, '#');
|
||||
if (brk) {
|
||||
int pieceLen = len - (brk + 1 - options);
|
||||
ExtractString(brk+1, o_Ref, pieceLen);
|
||||
len -= pieceLen + 1;
|
||||
*brk = '\0';
|
||||
}
|
||||
|
||||
// Now look for "?"
|
||||
brk = PL_strchr(options, '?');
|
||||
if (brk) {
|
||||
int pieceLen = len - (brk + 1 - options);
|
||||
ExtractString(brk+1, o_Query, pieceLen);
|
||||
len -= pieceLen + 1;
|
||||
}
|
||||
|
||||
// Now look for ';'
|
||||
brk = PL_strchr(options, ';');
|
||||
if (brk) {
|
||||
int pieceLen = len - (brk + 1 - options);
|
||||
ExtractString(brk+1, o_Param, pieceLen);
|
||||
len -= pieceLen + 1;
|
||||
*brk = '\0';
|
||||
}
|
||||
}
|
||||
|
||||
nsCRT::free(dirfile);
|
||||
nsCRT::free(options);
|
||||
return rv;
|
||||
}
|
||||
|
||||
nsresult
|
||||
nsNoAuthURLParser::ParsePreHost(const char* i_PreHost, char* *o_Username,
|
||||
char* *o_Password)
|
||||
{
|
||||
return NS_ERROR_NOT_IMPLEMENTED;
|
||||
}
|
||||
|
||||
nsresult
|
||||
nsNoAuthURLParser::ParseFileName(const char* i_FileName,
|
||||
char* *o_FileBaseName,
|
||||
char* *o_FileExtension)
|
||||
{
|
||||
nsresult rv = NS_OK;
|
||||
// Search for FileExtension
|
||||
// Search for last .
|
||||
// Ignore . at the beginning
|
||||
char* brk = PL_strrchr(i_FileName+1, '.');
|
||||
if (brk)
|
||||
{
|
||||
rv = ExtractString((char*)i_FileName, o_FileBaseName,
|
||||
(brk - i_FileName));
|
||||
if (NS_FAILED(rv))
|
||||
return rv;
|
||||
rv = ExtractString(brk + 1, o_FileExtension,
|
||||
(i_FileName+PL_strlen(i_FileName) - brk - 1));
|
||||
} else {
|
||||
rv = DupString(o_FileBaseName, i_FileName);
|
||||
}
|
||||
return rv;
|
||||
}
|
|
@ -0,0 +1,59 @@
|
|||
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 4 -*-
|
||||
*
|
||||
* The contents of this file are subject to the Netscape Public
|
||||
* License Version 1.1 (the "License"); you may not use this file
|
||||
* except in compliance with the License. You may obtain a copy of
|
||||
* the License at http://www.mozilla.org/NPL/
|
||||
*
|
||||
* Software distributed under the License is distributed on an "AS
|
||||
* IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
|
||||
* implied. See the License for the specific language governing
|
||||
* rights and limitations under the License.
|
||||
*
|
||||
* The Original Code is mozilla.org code.
|
||||
*
|
||||
* The Initial Developer of the Original Code is Netscape
|
||||
* Communications Corporation. Portions created by Netscape are
|
||||
* Copyright (C) 1998 Netscape Communications Corporation. All
|
||||
* Rights Reserved.
|
||||
*
|
||||
* Contributor(s):
|
||||
*/
|
||||
|
||||
#ifndef nsNoAuthURLParser_h__
|
||||
#define nsNoAuthURLParser_h__
|
||||
|
||||
#include "nsIURLParser.h"
|
||||
#include "nsIURI.h"
|
||||
#include "nsAgg.h"
|
||||
#include "nsCRT.h"
|
||||
|
||||
#define NS_NOAUTHORITYURLPARSER_CID \
|
||||
{ /* 9eeb1b89-c87e-4404-9de6-dbd41aeaf3d7 */ \
|
||||
0x9eeb1b89, \
|
||||
0xc87e, \
|
||||
0x4404, \
|
||||
{0x9d, 0xe6, 0xdb, 0xd4, 0x1a, 0xea, 0xf3, 0xd7} \
|
||||
}
|
||||
|
||||
class nsNoAuthURLParser : public nsIURLParser
|
||||
{
|
||||
public:
|
||||
NS_DECL_ISUPPORTS
|
||||
///////////////////////////////////////////////////////////////////////////
|
||||
// nsNoAuthURLParser methods:
|
||||
nsNoAuthURLParser() {
|
||||
NS_INIT_REFCNT();
|
||||
}
|
||||
virtual ~nsNoAuthURLParser();
|
||||
|
||||
static NS_METHOD
|
||||
Create(nsISupports *aOuter, REFNSIID aIID, void **aResult);
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////
|
||||
// nsIURLParser methods:
|
||||
NS_DECL_NSIURLPARSER
|
||||
|
||||
};
|
||||
|
||||
#endif // nsNoAuthURLParser_h__
|
|
@ -0,0 +1,463 @@
|
|||
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
|
||||
*
|
||||
* The contents of this file are subject to the Netscape Public
|
||||
* License Version 1.1 (the "License"); you may not use this file
|
||||
* except in compliance with the License. You may obtain a copy of
|
||||
* the License at http://www.mozilla.org/NPL/
|
||||
*
|
||||
* Software distributed under the License is distributed on an "AS
|
||||
* IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
|
||||
* implied. See the License for the specific language governing
|
||||
* rights and limitations under the License.
|
||||
*
|
||||
* The Original Code is mozilla.org code.
|
||||
*
|
||||
* The Initial Developer of the Original Code is Andreas Otte.
|
||||
*
|
||||
* Contributor(s):
|
||||
*/
|
||||
|
||||
#include "nsStdURLParser.h"
|
||||
#include "nsURLHelper.h"
|
||||
#include "nsCRT.h"
|
||||
#include "nsString.h"
|
||||
#include "prprf.h"
|
||||
|
||||
NS_IMPL_ISUPPORTS1(nsStdURLParser,
|
||||
nsIURLParser)
|
||||
|
||||
nsStdURLParser::~nsStdURLParser()
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
NS_METHOD
|
||||
nsStdURLParser::Create(nsISupports *aOuter, REFNSIID aIID, void **aResult)
|
||||
{
|
||||
if (aOuter)
|
||||
return NS_ERROR_NO_AGGREGATION;
|
||||
nsStdURLParser* p = new nsStdURLParser();
|
||||
if (p == nsnull)
|
||||
return NS_ERROR_OUT_OF_MEMORY;
|
||||
NS_ADDREF(p);
|
||||
nsresult rv = p->QueryInterface(aIID, aResult);
|
||||
NS_RELEASE(p);
|
||||
return rv;
|
||||
}
|
||||
|
||||
nsresult
|
||||
nsStdURLParser::ParseAtScheme(const char* i_Spec, char* *o_Scheme,
|
||||
char* *o_Username,
|
||||
char* *o_Password,
|
||||
char* *o_Host, PRInt32 *o_Port, char* *o_Path)
|
||||
{
|
||||
nsresult rv = NS_OK;
|
||||
|
||||
NS_PRECONDITION( (nsnull != i_Spec), "Parse called on empty url!");
|
||||
if (!i_Spec)
|
||||
return NS_ERROR_MALFORMED_URI;
|
||||
|
||||
int len = PL_strlen(i_Spec);
|
||||
if (len >= 2 && *i_Spec == '/' && *(i_Spec+1) == '/') // No Scheme
|
||||
{
|
||||
rv = ParseAtPreHost(i_Spec, o_Username, o_Password, o_Host, o_Port,
|
||||
o_Path);
|
||||
return rv;
|
||||
}
|
||||
|
||||
static const char delimiters[] = "/:@?"; //this order is optimized.
|
||||
char* brk = PL_strpbrk(i_Spec, delimiters);
|
||||
|
||||
if (!brk) // everything is a host
|
||||
{
|
||||
rv = ExtractString((char*)i_Spec, o_Host, len);
|
||||
return rv;
|
||||
} else
|
||||
len = PL_strlen(brk);
|
||||
|
||||
switch (*brk)
|
||||
{
|
||||
case '/' :
|
||||
case '?' :
|
||||
// If the URL starts with a slash then everything is a path
|
||||
if (brk == i_Spec)
|
||||
{
|
||||
rv = ParseAtPath(brk, o_Path);
|
||||
return rv;
|
||||
}
|
||||
else // The first part is host, so its host/path
|
||||
{
|
||||
rv = ExtractString((char*)i_Spec, o_Host, (brk - i_Spec));
|
||||
if (NS_FAILED(rv))
|
||||
return rv;
|
||||
rv = ParseAtPath(brk, o_Path);
|
||||
return rv;
|
||||
}
|
||||
break;
|
||||
case ':' :
|
||||
if (len >= 2 && *(brk+1) == '/' && *(brk+2) == '/') {
|
||||
// Standard http://...
|
||||
rv = ExtractString((char*)i_Spec, o_Scheme, (brk - i_Spec));
|
||||
if (NS_FAILED(rv))
|
||||
return rv;
|
||||
rv = ParseAtPreHost(brk+1, o_Username, o_Password, o_Host,
|
||||
o_Port, o_Path);
|
||||
if (rv == NS_ERROR_MALFORMED_URI) {
|
||||
// or not ? Try something else
|
||||
CRTFREEIF(*o_Username);
|
||||
CRTFREEIF(*o_Password);
|
||||
CRTFREEIF(*o_Host);
|
||||
*o_Port = -1;
|
||||
rv = ParseAtPath(brk+3, o_Path);
|
||||
}
|
||||
return rv;
|
||||
} else {
|
||||
if ( len >= 2 && *(brk+1) == '/' && *(brk+2) != '/') {
|
||||
// May be it is file:/....
|
||||
rv = ExtractString((char*)i_Spec, o_Scheme, (brk - i_Spec));
|
||||
if (NS_FAILED(rv))
|
||||
return rv;
|
||||
rv = ParseAtPath(brk+1, o_Path);
|
||||
return rv;
|
||||
} else {
|
||||
// Could be host:port, so try conversion to number
|
||||
PRInt32 port = ExtractPortFrom(brk+1);
|
||||
if (port <= 0)
|
||||
{
|
||||
// No, try normal procedure
|
||||
rv = ExtractString((char*)i_Spec, o_Scheme,
|
||||
(brk - i_Spec));
|
||||
if (NS_FAILED(rv))
|
||||
return rv;
|
||||
rv = ParseAtPreHost(brk+1, o_Username, o_Password, o_Host,
|
||||
o_Port, o_Path);
|
||||
if (rv == NS_ERROR_MALFORMED_URI) {
|
||||
// Try something else
|
||||
CRTFREEIF(*o_Username);
|
||||
CRTFREEIF(*o_Password);
|
||||
CRTFREEIF(*o_Host);
|
||||
*o_Port = -1;
|
||||
rv = ParseAtPath(brk+1, o_Path);
|
||||
}
|
||||
return rv;
|
||||
} else {
|
||||
rv = ExtractString((char*)i_Spec, o_Host, (brk - i_Spec));
|
||||
if (NS_FAILED(rv))
|
||||
return rv;
|
||||
rv = ParseAtPort(brk+1, o_Port, o_Path);
|
||||
return rv;
|
||||
}
|
||||
}
|
||||
}
|
||||
break;
|
||||
case '@' :
|
||||
rv = ParseAtPreHost(i_Spec, o_Username, o_Password, o_Host,
|
||||
o_Port, o_Path);
|
||||
return rv;
|
||||
break;
|
||||
default:
|
||||
NS_ASSERTION(0, "This just can't be!");
|
||||
break;
|
||||
}
|
||||
return NS_OK;
|
||||
}
|
||||
|
||||
nsresult
|
||||
nsStdURLParser::ParseAtPreHost(const char* i_Spec, char* *o_Username,
|
||||
char* *o_Password, char* *o_Host,
|
||||
PRInt32 *o_Port, char* *o_Path)
|
||||
{
|
||||
nsresult rv = NS_OK;
|
||||
|
||||
// Skip leading two slashes
|
||||
char* fwdPtr= (char*) i_Spec;
|
||||
if (fwdPtr && (*fwdPtr != '\0') && (*fwdPtr == '/'))
|
||||
fwdPtr++;
|
||||
if (fwdPtr && (*fwdPtr != '\0') && (*fwdPtr == '/'))
|
||||
fwdPtr++;
|
||||
|
||||
// Search for @
|
||||
static const char delimiters[] = "@";
|
||||
char* brk = PL_strpbrk(fwdPtr, delimiters);
|
||||
|
||||
if (brk)
|
||||
{
|
||||
char* e_PreHost = nsnull;
|
||||
rv = ExtractString(fwdPtr, &e_PreHost, (brk - fwdPtr));
|
||||
if (NS_FAILED(rv)) {
|
||||
CRTFREEIF(e_PreHost);
|
||||
return rv;
|
||||
}
|
||||
rv = ParsePreHost(e_PreHost,o_Username,o_Password);
|
||||
CRTFREEIF(e_PreHost);
|
||||
if (NS_FAILED(rv))
|
||||
return rv;
|
||||
|
||||
rv = ParseAtHost(brk+1, o_Host, o_Port, o_Path);
|
||||
return rv;
|
||||
} else {
|
||||
rv = ParseAtHost(fwdPtr, o_Host, o_Port, o_Path);
|
||||
return rv;
|
||||
}
|
||||
return NS_OK;
|
||||
}
|
||||
|
||||
nsresult
|
||||
nsStdURLParser::ParseAtHost(const char* i_Spec, char* *o_Host,
|
||||
PRInt32 *o_Port, char* *o_Path)
|
||||
{
|
||||
nsresult rv = NS_OK;
|
||||
|
||||
int len = PL_strlen(i_Spec);
|
||||
static const char delimiters[] = ":/?"; //this order is optimized.
|
||||
char* brk = PL_strpbrk(i_Spec, delimiters);
|
||||
if (!brk) // everything is a host
|
||||
{
|
||||
rv = ExtractString((char*)i_Spec, o_Host, len);
|
||||
return rv;
|
||||
}
|
||||
|
||||
switch (*brk)
|
||||
{
|
||||
case '/' :
|
||||
case '?' :
|
||||
// Get the Host, the rest is Path
|
||||
rv = ExtractString((char*)i_Spec, o_Host, (brk - i_Spec));
|
||||
if (NS_FAILED(rv))
|
||||
return rv;
|
||||
rv = ParseAtPath(brk, o_Path);
|
||||
return rv;
|
||||
break;
|
||||
case ':' :
|
||||
// Get the Host
|
||||
rv = ExtractString((char*)i_Spec, o_Host, (brk - i_Spec));
|
||||
if (NS_FAILED(rv))
|
||||
return rv;
|
||||
rv = ParseAtPort(brk+1, o_Port, o_Path);
|
||||
return rv;
|
||||
break;
|
||||
default:
|
||||
NS_ASSERTION(0, "This just can't be!");
|
||||
break;
|
||||
}
|
||||
return NS_OK;
|
||||
}
|
||||
|
||||
nsresult
|
||||
nsStdURLParser::ParseAtPort(const char* i_Spec, PRInt32 *o_Port, char* *o_Path)
|
||||
{
|
||||
nsresult rv = NS_OK;
|
||||
static const char delimiters[] = "/?"; //this order is optimized.
|
||||
char* brk = PL_strpbrk(i_Spec, delimiters);
|
||||
if (!brk) // everything is a Port
|
||||
{
|
||||
*o_Port = ExtractPortFrom(i_Spec);
|
||||
if (*o_Port <= 0)
|
||||
return NS_ERROR_MALFORMED_URI;
|
||||
else
|
||||
return NS_OK;
|
||||
}
|
||||
|
||||
switch (*brk)
|
||||
{
|
||||
case '/' :
|
||||
case '?' :
|
||||
// Get the Port, the rest is Path
|
||||
*o_Port = ExtractPortFrom(i_Spec);
|
||||
if (*o_Port <= 0)
|
||||
return NS_ERROR_MALFORMED_URI;
|
||||
rv = ParseAtPath(brk, o_Path);
|
||||
return rv;
|
||||
break;
|
||||
default:
|
||||
NS_ASSERTION(0, "This just can't be!");
|
||||
break;
|
||||
}
|
||||
return NS_OK;
|
||||
}
|
||||
|
||||
nsresult
|
||||
nsStdURLParser::ParseAtPath(const char* i_Spec, char* *o_Path)
|
||||
{
|
||||
// Just write the path and check for a starting /
|
||||
nsAutoString dir;
|
||||
if ('/' != *i_Spec)
|
||||
dir += "/";
|
||||
|
||||
dir += i_Spec;
|
||||
|
||||
*o_Path = dir.ToNewCString();
|
||||
return (*o_Path ? NS_OK : NS_ERROR_OUT_OF_MEMORY);
|
||||
}
|
||||
|
||||
nsresult
|
||||
nsStdURLParser::ParseAtDirectory(const char* i_Path, char* *o_Directory,
|
||||
char* *o_FileBaseName, char* *o_FileExtension,
|
||||
char* *o_Param, char* *o_Query, char* *o_Ref)
|
||||
{
|
||||
// Cleanout
|
||||
CRTFREEIF(*o_Directory);
|
||||
CRTFREEIF(*o_FileBaseName);
|
||||
CRTFREEIF(*o_FileExtension);
|
||||
CRTFREEIF(*o_Param);
|
||||
CRTFREEIF(*o_Query);
|
||||
CRTFREEIF(*o_Ref);
|
||||
|
||||
nsresult rv = NS_OK;
|
||||
|
||||
// Parse the Path into its components
|
||||
if (!i_Path)
|
||||
{
|
||||
DupString(o_Directory, "/");
|
||||
return (o_Directory ? NS_OK : NS_ERROR_OUT_OF_MEMORY);
|
||||
}
|
||||
|
||||
char* dirfile = nsnull;
|
||||
char* options = nsnull;
|
||||
|
||||
int len = PL_strlen(i_Path);
|
||||
|
||||
/* Factor out the optionpart with ;?# */
|
||||
static const char delimiters[] = ";?#"; // for param, query and ref
|
||||
char* brk = PL_strpbrk(i_Path, delimiters);
|
||||
|
||||
if (!brk) // Everything is just path and filename
|
||||
{
|
||||
DupString(&dirfile, i_Path);
|
||||
}
|
||||
else
|
||||
{
|
||||
int dirfileLen = brk - i_Path;
|
||||
ExtractString((char*)i_Path, &dirfile, dirfileLen);
|
||||
len -= dirfileLen;
|
||||
ExtractString((char*)i_Path + dirfileLen, &options, len);
|
||||
brk = options;
|
||||
}
|
||||
|
||||
/* now that we have broken up the path treat every part differently */
|
||||
/* first dir+file */
|
||||
|
||||
char* file= nsnull;
|
||||
|
||||
int dlen = PL_strlen(dirfile);
|
||||
if (dlen == 0)
|
||||
{
|
||||
DupString(o_Directory, "/");
|
||||
file = dirfile;
|
||||
} else {
|
||||
CoaleseDirs(dirfile);
|
||||
// Get length again
|
||||
dlen = PL_strlen(dirfile);
|
||||
|
||||
// First find the last slash
|
||||
file = PL_strrchr(dirfile, '/');
|
||||
if (!file)
|
||||
{
|
||||
DupString(o_Directory, "/");
|
||||
file = dirfile;
|
||||
}
|
||||
|
||||
// If its not the same as the first slash then extract directory
|
||||
if (file != dirfile)
|
||||
{
|
||||
ExtractString(dirfile, o_Directory, (file - dirfile)+1);
|
||||
} else {
|
||||
DupString(o_Directory, "/");
|
||||
}
|
||||
}
|
||||
|
||||
/* Extract FileBaseName and FileExtension */
|
||||
if (dlen > 0) {
|
||||
// Look again if there was a slash
|
||||
char* slash = PL_strrchr(dirfile, '/');
|
||||
char* e_FileName = nsnull;
|
||||
if (slash) {
|
||||
ExtractString(slash+1, &e_FileName, dlen-(slash-dirfile+1));
|
||||
} else {
|
||||
// Use the full String as Filename
|
||||
ExtractString(dirfile, &e_FileName, dlen);
|
||||
}
|
||||
|
||||
rv = ParseFileName(e_FileName,o_FileBaseName,o_FileExtension);
|
||||
CRTFREEIF(e_FileName);
|
||||
}
|
||||
|
||||
// Now take a look at the options. "#" has precedence over "?"
|
||||
// which has precedence over ";"
|
||||
if (options) {
|
||||
// Look for "#" first. Everything following it is in the ref
|
||||
brk = PL_strchr(options, '#');
|
||||
if (brk) {
|
||||
int pieceLen = len - (brk + 1 - options);
|
||||
ExtractString(brk+1, o_Ref, pieceLen);
|
||||
len -= pieceLen + 1;
|
||||
*brk = '\0';
|
||||
}
|
||||
|
||||
// Now look for "?"
|
||||
brk = PL_strchr(options, '?');
|
||||
if (brk) {
|
||||
int pieceLen = len - (brk + 1 - options);
|
||||
ExtractString(brk+1, o_Query, pieceLen);
|
||||
len -= pieceLen + 1;
|
||||
}
|
||||
|
||||
// Now look for ';'
|
||||
brk = PL_strchr(options, ';');
|
||||
if (brk) {
|
||||
int pieceLen = len - (brk + 1 - options);
|
||||
ExtractString(brk+1, o_Param, pieceLen);
|
||||
len -= pieceLen + 1;
|
||||
*brk = '\0';
|
||||
}
|
||||
}
|
||||
|
||||
CRTFREEIF(dirfile);
|
||||
CRTFREEIF(options);
|
||||
return rv;
|
||||
}
|
||||
|
||||
nsresult
|
||||
nsStdURLParser::ParsePreHost(const char* i_PreHost, char* *o_Username,
|
||||
char* *o_Password)
|
||||
{
|
||||
nsresult rv = NS_OK;
|
||||
// Search for :
|
||||
static const char delimiters[] = ":";
|
||||
char* brk = PL_strpbrk(i_PreHost, delimiters);
|
||||
if (brk)
|
||||
{
|
||||
rv = ExtractString((char*)i_PreHost, o_Username, (brk - i_PreHost));
|
||||
if (NS_FAILED(rv))
|
||||
return rv;
|
||||
rv = ExtractString(brk+1, o_Password,
|
||||
(i_PreHost+PL_strlen(i_PreHost) - brk - 1));
|
||||
} else {
|
||||
CRTFREEIF(*o_Password);
|
||||
rv = DupString(o_Username, i_PreHost);
|
||||
}
|
||||
return rv;
|
||||
}
|
||||
|
||||
nsresult
|
||||
nsStdURLParser::ParseFileName(const char* i_FileName, char* *o_FileBaseName,
|
||||
char* *o_FileExtension)
|
||||
{
|
||||
nsresult rv = NS_OK;
|
||||
// Search for FileExtension
|
||||
// Search for last .
|
||||
// Ignore . at the beginning
|
||||
char* brk = PL_strrchr(i_FileName+1, '.');
|
||||
if (brk)
|
||||
{
|
||||
rv = ExtractString((char*)i_FileName, o_FileBaseName,
|
||||
(brk - i_FileName));
|
||||
if (NS_FAILED(rv))
|
||||
return rv;
|
||||
rv = ExtractString(brk + 1, o_FileExtension,
|
||||
(i_FileName+PL_strlen(i_FileName) - brk - 1));
|
||||
} else {
|
||||
rv = DupString(o_FileBaseName, i_FileName);
|
||||
}
|
||||
return rv;
|
||||
}
|
|
@ -0,0 +1,59 @@
|
|||
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 4 -*-
|
||||
*
|
||||
* The contents of this file are subject to the Netscape Public
|
||||
* License Version 1.1 (the "License"); you may not use this file
|
||||
* except in compliance with the License. You may obtain a copy of
|
||||
* the License at http://www.mozilla.org/NPL/
|
||||
*
|
||||
* Software distributed under the License is distributed on an "AS
|
||||
* IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
|
||||
* implied. See the License for the specific language governing
|
||||
* rights and limitations under the License.
|
||||
*
|
||||
* The Original Code is mozilla.org code.
|
||||
*
|
||||
* The Initial Developer of the Original Code is Netscape
|
||||
* Communications Corporation. Portions created by Netscape are
|
||||
* Copyright (C) 1998 Netscape Communications Corporation. All
|
||||
* Rights Reserved.
|
||||
*
|
||||
* Contributor(s):
|
||||
*/
|
||||
|
||||
#ifndef nsStdURLParser_h__
|
||||
#define nsStdURLParser_h__
|
||||
|
||||
#include "nsIURLParser.h"
|
||||
#include "nsIURI.h"
|
||||
#include "nsAgg.h"
|
||||
#include "nsCRT.h"
|
||||
|
||||
#define NS_STANDARDURLPARSER_CID \
|
||||
{ /* dbf72351-4fd8-46f0-9dbc-fa5ba60a30c5 */ \
|
||||
0xdbf72351, \
|
||||
0x4fd8, \
|
||||
0x46f0, \
|
||||
{0x9d, 0xbc, 0xfa, 0x5b, 0xa6, 0x0a, 0x30, 0x5c} \
|
||||
}
|
||||
|
||||
class nsStdURLParser : public nsIURLParser
|
||||
{
|
||||
public:
|
||||
NS_DECL_ISUPPORTS
|
||||
///////////////////////////////////////////////////////////////////////////
|
||||
// nsStdURLParser methods:
|
||||
nsStdURLParser() {
|
||||
NS_INIT_REFCNT();
|
||||
}
|
||||
virtual ~nsStdURLParser();
|
||||
|
||||
static NS_METHOD
|
||||
Create(nsISupports *aOuter, REFNSIID aIID, void **aResult);
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////
|
||||
// nsIURLParser methods:
|
||||
NS_DECL_NSIURLPARSER
|
||||
|
||||
};
|
||||
|
||||
#endif // nsStdURLParser_h__
|
|
@ -0,0 +1,251 @@
|
|||
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
|
||||
*
|
||||
* The contents of this file are subject to the Netscape Public
|
||||
* License Version 1.1 (the "License"); you may not use this file
|
||||
* except in compliance with the License. You may obtain a copy of
|
||||
* the License at http://www.mozilla.org/NPL/
|
||||
*
|
||||
* Software distributed under the License is distributed on an "AS
|
||||
* IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
|
||||
* implied. See the License for the specific language governing
|
||||
* rights and limitations under the License.
|
||||
*
|
||||
* The Original Code is mozilla.org code.
|
||||
*
|
||||
* The Initial Developer of the Original Code is Andreas Otte.
|
||||
*
|
||||
* Contributor(s):
|
||||
*/
|
||||
|
||||
#include "nsURLHelper.h"
|
||||
#include "prprf.h"
|
||||
#include "nsCRT.h"
|
||||
#include "nsIAllocator.h"
|
||||
|
||||
/* This array tells which chars has to be escaped */
|
||||
|
||||
const int EscapeChars[256] =
|
||||
/* 0 1 2 3 4 5 6 7 8 9 A B C D E F */
|
||||
{
|
||||
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x */
|
||||
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 1x */
|
||||
0,1023, 0, 512, 0, 0,1023, 0,1023,1023,1023,1023,1023,1023, 959,1016, /* 2x !"#$%&'()*+,-./ */
|
||||
1023,1023,1023,1023,1023,1023,1023,1023,1023,1023, 896, 896, 0, 896, 0, 768, /* 3x 0123456789:;<=>? */
|
||||
896,1023,1023,1023,1023,1023,1023,1023,1023,1023,1023,1023,1023,1023,1023,1023, /* 4x @ABCDEFGHIJKLMNO */
|
||||
1023,1023,1023,1023,1023,1023,1023,1023,1023,1023,1023, 896, 896, 896, 896,1023, /* 5x PQRSTUVWXYZ[\]^_ */
|
||||
0,1023,1023,1023,1023,1023,1023,1023,1023,1023,1023,1023,1023,1023,1023,1023, /* 6x `abcdefghijklmno */
|
||||
1023,1023,1023,1023,1023,1023,1023,1023,1023,1023,1023, 896,1012, 896,1008, 0, /* 7x pqrstuvwxyz{|}~ */
|
||||
0 /* 8x DEL */
|
||||
};
|
||||
|
||||
/* decode % escaped hex codes into character values
|
||||
*/
|
||||
#define UNHEX(C) \
|
||||
((C >= '0' && C <= '9') ? C - '0' : \
|
||||
((C >= 'A' && C <= 'F') ? C - 'A' + 10 : \
|
||||
((C >= 'a' && C <= 'f') ? C - 'a' + 10 : 0)))
|
||||
|
||||
/* check if char has to be escaped */
|
||||
#define IS_OK(C) (EscapeChars[((unsigned int) (C))] & (mask))
|
||||
|
||||
/* HEX mask char */
|
||||
#define HEX_ESCAPE '%'
|
||||
|
||||
/* returns an escaped string */
|
||||
NS_NET nsresult
|
||||
nsURLEscape(const char* str, PRInt16 mask, char **result)
|
||||
{
|
||||
if (!str)
|
||||
return NS_OK;
|
||||
|
||||
int i, extra = 0;
|
||||
char* hexChars = "0123456789ABCDEF";
|
||||
static const char CheckHexChars[] = "0123456789ABCDEFabcdef";
|
||||
int len = PL_strlen(str);
|
||||
|
||||
register const unsigned char* src = (const unsigned char *) str;
|
||||
for (i = 0; i < len; i++)
|
||||
{
|
||||
if (!IS_OK(*src++)) {
|
||||
extra += 2; /* the escape, plus an extra byte for each nibble */
|
||||
}
|
||||
}
|
||||
|
||||
*result = (char *)nsAllocator::Alloc(len + extra + 1);
|
||||
if (!*result)
|
||||
return NS_ERROR_OUT_OF_MEMORY;
|
||||
|
||||
register unsigned char* dst = (unsigned char *) *result;
|
||||
src = (const unsigned char *) str;
|
||||
|
||||
for (i = 0; i < len; i++)
|
||||
{
|
||||
const char* checker = (char*) src;
|
||||
unsigned char c = *src++;
|
||||
/* if the char has not to be escaped or whatever follows % is
|
||||
a valid escaped string, just copy the char */
|
||||
if (IS_OK(c) || (c == HEX_ESCAPE && (checker+1) && (checker+2) &&
|
||||
PL_strpbrk(((checker+1)), CheckHexChars) != 0 &&
|
||||
PL_strpbrk(((checker+2)), CheckHexChars) != 0))
|
||||
*dst++ = c;
|
||||
else
|
||||
/* do the escape magic */
|
||||
{
|
||||
*dst++ = HEX_ESCAPE;
|
||||
*dst++ = hexChars[c >> 4]; /* high nibble */
|
||||
*dst++ = hexChars[c & 0x0f]; /* low nibble */
|
||||
}
|
||||
}
|
||||
*dst = '\0';
|
||||
return NS_OK;
|
||||
}
|
||||
|
||||
/* returns an unescaped string */
|
||||
NS_NET nsresult
|
||||
nsURLUnescape(char* str, char **result)
|
||||
{
|
||||
if (!str)
|
||||
return NS_OK;
|
||||
|
||||
register char *src = str;
|
||||
static const char hexChars[] = "0123456789ABCDEF";
|
||||
int len = PL_strlen(str);
|
||||
|
||||
*result = (char *)nsAllocator::Alloc(len + 1);
|
||||
if (!*result)
|
||||
return NS_ERROR_OUT_OF_MEMORY;
|
||||
|
||||
src = str;
|
||||
register unsigned char* dst = (unsigned char *) *result;
|
||||
|
||||
while (*src)
|
||||
/* check for valid escaped sequence */
|
||||
if (*src != HEX_ESCAPE || PL_strpbrk(((src+1)), hexChars) == 0 ||
|
||||
PL_strpbrk(((src+2)), hexChars) == 0 )
|
||||
*dst++ = *src++;
|
||||
else
|
||||
{
|
||||
src++; /* walk over escape */
|
||||
if (*src)
|
||||
{
|
||||
*dst = UNHEX(*src) << 4;
|
||||
src++;
|
||||
}
|
||||
if (*src)
|
||||
{
|
||||
*dst = (*dst + UNHEX(*src));
|
||||
src++;
|
||||
}
|
||||
dst++;
|
||||
}
|
||||
|
||||
*dst = '\0';
|
||||
return NS_OK;
|
||||
}
|
||||
|
||||
/* extract portnumber from string */
|
||||
NS_NET PRInt32
|
||||
ExtractPortFrom(const char* src)
|
||||
{
|
||||
PRInt32 returnValue = -1;
|
||||
return (0 < PR_sscanf(src, "%d", &returnValue)) ? returnValue : -1;
|
||||
}
|
||||
|
||||
/* extract string from other string */
|
||||
NS_NET nsresult
|
||||
ExtractString(char* i_Src, char* *o_Dest, PRUint32 length)
|
||||
{
|
||||
NS_PRECONDITION( (nsnull != i_Src), "Extract called on empty string!");
|
||||
CRTFREEIF(*o_Dest);
|
||||
*o_Dest = PL_strndup(i_Src, length);
|
||||
return (*o_Dest ? NS_OK : NS_ERROR_OUT_OF_MEMORY);
|
||||
}
|
||||
|
||||
/* duplicate string */
|
||||
NS_NET nsresult
|
||||
DupString(char* *o_Dest, const char* i_Src)
|
||||
{
|
||||
if (!o_Dest)
|
||||
return NS_ERROR_NULL_POINTER;
|
||||
if (i_Src)
|
||||
{
|
||||
*o_Dest = nsCRT::strdup(i_Src);
|
||||
return (*o_Dest == nsnull) ? NS_ERROR_OUT_OF_MEMORY : NS_OK;
|
||||
}
|
||||
else
|
||||
{
|
||||
*o_Dest = nsnull;
|
||||
return NS_OK;
|
||||
}
|
||||
}
|
||||
|
||||
// Replace all /./ with a /
|
||||
// Also changes all \ to /
|
||||
// But only till #?;
|
||||
NS_NET void
|
||||
CoaleseDirs(char* io_Path)
|
||||
{
|
||||
/* Stolen from the old netlib's mkparse.c.
|
||||
*
|
||||
* modifies a url of the form /foo/../foo1 -> /foo1
|
||||
* and /foo/./foo1 -> /foo/foo1
|
||||
* and /foo/foo1/.. -> /foo/
|
||||
*/
|
||||
char *fwdPtr = io_Path;
|
||||
char *urlPtr = io_Path;
|
||||
|
||||
for(; (*fwdPtr != '\0') &&
|
||||
(*fwdPtr != ';') &&
|
||||
(*fwdPtr != '?') &&
|
||||
(*fwdPtr != '#'); ++fwdPtr)
|
||||
{
|
||||
if (*fwdPtr == '\\')
|
||||
*fwdPtr = '/';
|
||||
if (*fwdPtr == '/' && *(fwdPtr+1) == '.' &&
|
||||
(*(fwdPtr+2) == '/' || *(fwdPtr+2) == '\\'))
|
||||
{
|
||||
// remove . followed by slash or a backslash
|
||||
fwdPtr += 1;
|
||||
}
|
||||
else if(*fwdPtr == '/' && *(fwdPtr+1) == '.' && *(fwdPtr+2) == '.' &&
|
||||
(*(fwdPtr+3) == '/' ||
|
||||
*(fwdPtr+3) == '\0' ||
|
||||
*(fwdPtr+3) == ';' || // This will take care of likes of
|
||||
*(fwdPtr+3) == '?' || // foo/bar/..#sometag
|
||||
*(fwdPtr+3) == '#' ||
|
||||
*(fwdPtr+3) == '\\'))
|
||||
{
|
||||
// remove foo/..
|
||||
// reverse the urlPtr to the previous slash
|
||||
if(urlPtr != io_Path)
|
||||
urlPtr--; // we must be going back at least by one
|
||||
for(;*urlPtr != '/' && urlPtr != io_Path; urlPtr--)
|
||||
; // null body
|
||||
|
||||
// forward the fwd_prt past the ../
|
||||
fwdPtr += 2;
|
||||
// special case if we have reached the end to preserve the last /
|
||||
if (*fwdPtr == '.' && *(fwdPtr+1) == '\0')
|
||||
urlPtr +=1;
|
||||
}
|
||||
else
|
||||
{
|
||||
// copy the url incrementaly
|
||||
*urlPtr++ = *fwdPtr;
|
||||
}
|
||||
}
|
||||
// Copy remaining stuff past the #?;
|
||||
for (; *fwdPtr != '\0'; ++fwdPtr)
|
||||
{
|
||||
*urlPtr++ = *fwdPtr;
|
||||
}
|
||||
*urlPtr = '\0'; // terminate the url
|
||||
|
||||
/*
|
||||
* Now lets remove trailing . case
|
||||
* /foo/foo1/. -> /foo/foo1/
|
||||
*/
|
||||
|
||||
if ((urlPtr > (io_Path+1)) && (*(urlPtr-1) == '.') && (*(urlPtr-2) == '/'))
|
||||
*(urlPtr-1) = '\0';
|
||||
}
|
|
@ -0,0 +1,54 @@
|
|||
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
|
||||
*
|
||||
* The contents of this file are subject to the Netscape Public
|
||||
* License Version 1.1 (the "License"); you may not use this file
|
||||
* except in compliance with the License. You may obtain a copy of
|
||||
* the License at http://www.mozilla.org/NPL/
|
||||
*
|
||||
* Software distributed under the License is distributed on an "AS
|
||||
* IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
|
||||
* implied. See the License for the specific language governing
|
||||
* rights and limitations under the License.
|
||||
*
|
||||
* The Original Code is mozilla.org code.
|
||||
*
|
||||
* The Initial Developer of the Original Code is Andreas Otte.
|
||||
*
|
||||
* Contributor(s):
|
||||
*/
|
||||
|
||||
#ifndef _nsURLHelper_h_
|
||||
#define _nsURLHelper_h_
|
||||
|
||||
#include "prtypes.h"
|
||||
#include "nscore.h"
|
||||
#include "nsCRT.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/* encode characters into % escaped hexcodes */
|
||||
NS_NET nsresult nsURLEscape (const char* str, PRInt16 mask, char ** result);
|
||||
|
||||
/* decode % escaped hex codes into character values */
|
||||
NS_NET nsresult nsURLUnescape(char* str, char ** result);
|
||||
|
||||
/* Get port from string */
|
||||
NS_NET PRInt32 ExtractPortFrom(const char* src);
|
||||
|
||||
/* Extract string out of another */
|
||||
NS_NET nsresult ExtractString(char* i_Src, char* *o_Dest, PRUint32 length);
|
||||
|
||||
/* Duplicate string */
|
||||
NS_NET nsresult DupString(char* *o_Dest, const char* i_Src);
|
||||
|
||||
/* handle .. in dirs */
|
||||
NS_NET void CoaleseDirs(char* io_Path);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
||||
|
Загрузка…
Ссылка в новой задаче