bug 47437, nsCookie.cpp is duplicating URL parsing logic, c=andreas.otte, r=morse, sr=alecf

git-svn-id: svn://10.0.0.236/trunk@105114 18797224-902f-48f8-a5cc-f745e15eee43
This commit is contained in:
morse%netscape.com
2001-10-10 23:16:03 +00:00
parent 8722951db4
commit 7b3d740ef8
13 changed files with 125 additions and 213 deletions

View File

@@ -51,6 +51,8 @@
#include "nsIPref.h"
#include "nsTextFormatter.h"
#include "nsAppDirectoryServiceDefs.h"
#include "nsIIOService.h"
#include "nsNetCID.h"
#define MAX_NUMBER_OF_COOKIES 300
#define MAX_COOKIES_PER_SERVER 20
@@ -132,6 +134,7 @@ PRIVATE char* cookie_P3P = nsnull;
#define cookie_P3P_Default "drdraaaa"
PRIVATE nsVoidArray * cookie_list=0;
PRIVATE nsCOMPtr<nsIIOService> IOService = nsnull;
static
time_t
@@ -177,6 +180,7 @@ COOKIE_RemoveAll()
delete cookie_list;
cookie_list = nsnull;
Recycle(cookie_P3P);
IOService = nsnull;
}
}
@@ -606,8 +610,17 @@ COOKIE_GetCookie(char * address) {
if (cookie_list == nsnull) {
return nsnull;
}
char *host = CKutil_ParseURL(address, GET_HOST_PART);
char *path = CKutil_ParseURL(address, GET_PATH_PART);
char *host = nsnull;
char *path = nsnull;
PRUint32 start, end;
// Get host and path
nsresult result;
NS_ASSERTION(IOService, "IOService not available");
result = IOService->ExtractUrlPart(address, nsIIOService::url_Host |
nsIIOService::url_Port, &start, &end,
&host);
result = IOService->ExtractUrlPart(address, nsIIOService::url_Path,
&start, &end, &path);
for (PRInt32 i = 0; i <cookie_list->Count(); i++) {
cookie_s = NS_STATIC_CAST(cookie_CookieStruct*, cookie_list->ElementAt(i));
NS_ASSERTION(cookie_s, "corrupt cookie list");
@@ -730,8 +743,18 @@ cookie_isForeign (char * curURL, char * firstURL) {
if (!firstURL) {
return PR_FALSE;
}
char * curHost = CKutil_ParseURL(curURL, GET_HOST_PART);
char * firstHost = CKutil_ParseURL(firstURL, GET_HOST_PART);
char *curHost = nsnull;
char *firstHost = nsnull;
PRUint32 start,end;
nsresult rv;
NS_ASSERTION(IOService, "IOService not available");
// Get hosts
rv = IOService->ExtractUrlPart(curURL, nsIIOService::url_Host |
nsIIOService::url_Port, &start, &end,
&curHost);
rv = IOService->ExtractUrlPart(firstURL, nsIIOService::url_Host |
nsIIOService::url_Port, &start, &end,
&firstHost);
char * curHostColon = 0;
char * firstHostColon = 0;
@@ -875,8 +898,17 @@ cookie_SetCookieString(char * curURL, nsIPrompt *aPrompter, const char * setCook
cookie_CookieStruct * prev_cookie;
char *path_from_header=nsnull, *host_from_header=nsnull;
char *name_from_header=nsnull, *cookie_from_header=nsnull;
char *cur_path = CKutil_ParseURL(curURL, GET_PATH_PART);
char *cur_host = CKutil_ParseURL(curURL, GET_HOST_PART);
char *cur_host = nsnull;
char *cur_path = nsnull;
PRUint32 start,end;
nsresult rv;
NS_ASSERTION(IOService, "IOService not available");
// Get host and path
rv = IOService->ExtractUrlPart(curURL, nsIIOService::url_Host |
nsIIOService::url_Port, &start, &end,
&cur_host);
rv = IOService->ExtractUrlPart(curURL, nsIIOService::url_Path,
&start, &end, &cur_path);
char *semi_colon, *ptr, *equal;
char *setCookieHeaderInternal = (char *) setCookieHeader;
PRBool isSecure=PR_FALSE, isDomain=PR_FALSE;
@@ -1401,12 +1433,15 @@ COOKIE_Read() {
if (cookie_list) {
return NS_OK;
}
nsresult rv = NS_OK;
IOService = do_GetService(NS_IOSERVICE_CONTRACTID, &rv);
if (NS_FAILED(rv)) return rv;
cookie_CookieStruct *new_cookie, *tmp_cookie_ptr;
size_t new_len;
nsAutoString buffer;
PRBool added_to_list;
nsFileSpec dirSpec;
nsresult rv = CKutil_ProfileDirectory(dirSpec);
rv = CKutil_ProfileDirectory(dirSpec);
if (NS_FAILED(rv)) {
return rv;
}

View File

@@ -45,6 +45,7 @@
#include "nsIPref.h"
#include "nsTextFormatter.h"
#include "nsIServiceManager.h"
#include "nsIIOService.h"
#define image_behaviorPref "network.image.imageBehavior"
#define image_warningPref "network.image.warnAboutImages"
@@ -197,11 +198,17 @@ IMAGE_CheckForPermission
}
PUBLIC nsresult
IMAGE_Block(const char* imageURL) {
IMAGE_Block(const char* imageURL,
nsIIOService* ioService) {
if (!imageURL || !(*imageURL)) {
return NS_ERROR_NULL_POINTER;
}
char *host = CKutil_ParseURL(imageURL, GET_HOST_PART);
nsresult rv = NS_OK;
char *host = nsnull;
PRUint32 start,end;
NS_ASSERTION(ioService, "IOService not available");
rv = ioService->ExtractUrlPart(imageURL, nsIIOService::url_Host |
nsIIOService::url_Port, &start, &end, &host);
Permission_AddHost(host, PR_FALSE, IMAGEPERMISSION, PR_TRUE);
return NS_OK;
}

View File

@@ -43,7 +43,7 @@
extern nsresult IMAGE_CheckForPermission
(const char * hostname, const char * firstHostname, PRBool *permission);
extern nsresult IMAGE_Block(const char * imageURL);
extern nsresult IMAGE_Block(const char * imageURL, nsIIOService *ioService);
extern void IMAGE_RegisterPrefCallbacks(void);
#endif /* IMAGES_H */

View File

@@ -43,6 +43,10 @@
#include "nsCOMPtr.h"
#include "nsXPIDLString.h"
#include "nsIURI.h"
#include "nsNetCID.h"
#include "nsIServiceManager.h"
static NS_DEFINE_CID(kIOServiceCID, NS_IOSERVICE_CID);
////////////////////////////////////////////////////////////////////////////////
@@ -66,13 +70,17 @@ nsImgManager::~nsImgManager(void)
nsresult nsImgManager::Init()
{
IMAGE_RegisterPrefCallbacks();
return NS_OK;
nsresult rv;
rv = nsServiceManager::GetService(kIOServiceCID,
NS_GET_IID(nsIIOService),
getter_AddRefs(mIOService));
return rv;
}
NS_IMETHODIMP nsImgManager::Block(const char * imageURL)
{
::IMAGE_Block(imageURL);
::IMAGE_Block(imageURL,mIOService);
return NS_OK;
}

View File

@@ -41,6 +41,8 @@
#include "nsIImgManager.h"
#include "nsIContentPolicy.h"
#include "nsCOMPtr.h"
#include "nsIIOService.h"
////////////////////////////////////////////////////////////////////////////////
@@ -57,7 +59,11 @@ public:
nsImgManager();
virtual ~nsImgManager(void);
nsresult Init();
protected:
// cached IOService
nsCOMPtr<nsIIOService> mIOService;
};
#endif /* nsImgManager_h__ */

View File

@@ -47,6 +47,9 @@
#include "nsIPrompt.h"
#include "nsIObserverService.h"
#include "nsPermission.h"
#include "nsNetCID.h"
static NS_DEFINE_CID(kIOServiceCID, NS_IOSERVICE_CID);
////////////////////////////////////////////////////////////////////////////////
@@ -126,13 +129,15 @@ nsresult nsPermissionManager::Init()
observerService->AddObserver(this, NS_LITERAL_STRING("profile-before-change").get());
observerService->AddObserver(this, NS_LITERAL_STRING("profile-do-change").get());
}
rv = nsServiceManager::GetService(kIOServiceCID,
NS_GET_IID(nsIIOService),
getter_AddRefs(mIOService));
return NS_OK;
}
NS_IMETHODIMP nsPermissionManager::Add
(const char * objectURL, PRBool permission, PRInt32 type) {
::PERMISSION_Add(objectURL, permission, type);
::PERMISSION_Add(objectURL, permission, type, mIOService);
return NS_OK;
}

View File

@@ -42,6 +42,8 @@
#include "nsIPermissionManager.h"
#include "nsIObserver.h"
#include "nsWeakReference.h"
#include "nsCOMPtr.h"
#include "nsIIOService.h"
////////////////////////////////////////////////////////////////////////////////
@@ -58,6 +60,10 @@ public:
nsPermissionManager();
virtual ~nsPermissionManager(void);
nsresult Init();
protected:
// cached IOService
nsCOMPtr<nsIIOService> mIOService;
};

View File

@@ -51,6 +51,8 @@
#include "xp_core.h"
#include "prmem.h"
#include "nsAppDirectoryServiceDefs.h"
#include "nsIIOService.h"
#include "nsNetCID.h"
static const char *kCookiesPermFileName = "cookperm.txt";
@@ -684,11 +686,17 @@ PERMISSION_DeletePersistentUserData(void)
}
PUBLIC void
PERMISSION_Add(const char * objectURL, PRBool permission, PRInt32 type) {
PERMISSION_Add(const char * objectURL, PRBool permission, PRInt32 type,
nsIIOService* ioService) {
if (!objectURL) {
return;
}
char *host = CKutil_ParseURL(objectURL, GET_HOST_PART);
nsresult rv = NS_OK;
char *host = nsnull;
PRUint32 start,end;
NS_ASSERTION(ioService, "IOService not available");
rv = ioService->ExtractUrlPart(objectURL, nsIIOService::url_Host |
nsIIOService::url_Port, &start, &end, &host);
/*
* if permission is false, it will be added to the permission list

View File

@@ -40,6 +40,7 @@
#define PERMISSIONS_H
#include "nsString.h"
#include "nsIIOService.h"
#define COOKIEPERMISSION 0
#define IMAGEPERMISSION 1
@@ -55,7 +56,7 @@ typedef enum {
class nsIPrompt;
extern nsresult PERMISSION_Read();
extern void PERMISSION_Add(const char * objectURL, PRBool permission, PRInt32 type);
extern void PERMISSION_Add(const char * objectURL, PRBool permission, PRInt32 type, nsIIOService* ioService);
extern void PERMISSION_RemoveAll();
extern void PERMISSION_DeletePersistentUserData(void);

View File

@@ -47,7 +47,6 @@
static NS_DEFINE_IID(kStringBundleServiceCID, NS_STRINGBUNDLESERVICE_CID);
#define MAX_HOST_NAME_LEN 64
#define BUFSIZE 128
#define LOCALIZATION "chrome://communicator/locale/wallet/cookie.properties"
@@ -100,184 +99,6 @@ CKutil_GetLine(nsInputFileStream& strm, nsString& aLine) {
return 0;
}
PUBLIC char *
CKutil_ParseURL (const char *url, int parts_requested) {
char *rv=0,*colon, *slash, *ques_mark, *hash_mark;
char *atSign, *host, *passwordColon, *gtThan;
assert(url);
if(!url) {
return(CKutil_StrAllocCat(rv, ""));
}
colon = PL_strchr(url, ':'); /* returns a const char */
/* Get the protocol part, not including anything beyond the colon */
if (parts_requested & GET_PROTOCOL_PART) {
if(colon) {
char val = *(colon+1);
*(colon+1) = '\0';
CKutil_StrAllocCopy(rv, url);
*(colon+1) = val;
/* If the user wants more url info, tack on extra slashes. */
if( (parts_requested & GET_HOST_PART)
|| (parts_requested & GET_USERNAME_PART)
|| (parts_requested & GET_PASSWORD_PART)) {
if( *(colon+1) == '/' && *(colon+2) == '/') {
CKutil_StrAllocCat(rv, "//");
}
/* If there's a third slash consider it file:/// and tack on the last slash. */
if( *(colon+3) == '/' ) {
CKutil_StrAllocCat(rv, "/");
}
}
}
}
/* Get the username if one exists */
if (parts_requested & GET_USERNAME_PART) {
if (colon && (*(colon+1) == '/') && (*(colon+2) == '/') && (*(colon+3) != '\0')) {
if ( (slash = PL_strchr(colon+3, '/')) != NULL) {
*slash = '\0';
}
if ( (atSign = PL_strchr(colon+3, '@')) != NULL) {
*atSign = '\0';
if ( (passwordColon = PL_strchr(colon+3, ':')) != NULL) {
*passwordColon = '\0';
}
CKutil_StrAllocCat(rv, colon+3);
/* Get the password if one exists */
if (parts_requested & GET_PASSWORD_PART) {
if (passwordColon) {
CKutil_StrAllocCat(rv, ":");
CKutil_StrAllocCat(rv, passwordColon+1);
}
}
if (parts_requested & GET_HOST_PART) {
CKutil_StrAllocCat(rv, "@");
}
if (passwordColon) {
*passwordColon = ':';
}
*atSign = '@';
}
if (slash) {
*slash = '/';
}
}
}
/* Get the host part */
if (parts_requested & GET_HOST_PART) {
if(colon) {
if(*(colon+1) == '/' && *(colon+2) == '/') {
slash = PL_strchr(colon+3, '/');
if(slash) {
*slash = '\0';
}
if( (atSign = PL_strchr(colon+3, '@')) != NULL) {
host = atSign+1;
} else {
host = colon+3;
}
ques_mark = PL_strchr(host, '?');
if(ques_mark) {
*ques_mark = '\0';
}
gtThan = PL_strchr(host, '>');
if (gtThan) {
*gtThan = '\0';
}
/* limit hostnames to within MAX_HOST_NAME_LEN characters to keep from crashing */
if(PL_strlen(host) > MAX_HOST_NAME_LEN) {
char * cp;
char old_char;
cp = host+MAX_HOST_NAME_LEN;
old_char = *cp;
*cp = '\0';
CKutil_StrAllocCat(rv, host);
*cp = old_char;
} else {
CKutil_StrAllocCat(rv, host);
}
if(slash) {
*slash = '/';
}
if(ques_mark) {
*ques_mark = '?';
}
if (gtThan) {
*gtThan = '>';
}
}
}
}
/* Get the path part */
if (parts_requested & GET_PATH_PART) {
if(colon) {
if(*(colon+1) == '/' && *(colon+2) == '/') {
/* skip host part */
slash = PL_strchr(colon+3, '/');
} else {
/* path is right after the colon */
slash = colon+1;
}
if(slash) {
ques_mark = PL_strchr(slash, '?');
hash_mark = PL_strchr(slash, '#');
if(ques_mark) {
*ques_mark = '\0';
}
if(hash_mark) {
*hash_mark = '\0';
}
CKutil_StrAllocCat(rv, slash);
if(ques_mark) {
*ques_mark = '?';
}
if(hash_mark) {
*hash_mark = '#';
}
}
}
}
if(parts_requested & GET_HASH_PART) {
hash_mark = PL_strchr(url, '#'); /* returns a const char * */
if(hash_mark) {
ques_mark = PL_strchr(hash_mark, '?');
if(ques_mark) {
*ques_mark = '\0';
}
CKutil_StrAllocCat(rv, hash_mark);
if(ques_mark) {
*ques_mark = '?';
}
}
}
if(parts_requested & GET_SEARCH_PART) {
ques_mark = PL_strchr(url, '?'); /* returns a const char * */
if(ques_mark) {
hash_mark = PL_strchr(ques_mark, '#');
if(hash_mark) {
*hash_mark = '\0';
}
CKutil_StrAllocCat(rv, ques_mark);
if(hash_mark) {
*hash_mark = '#';
}
}
}
/* copy in a null string if nothing was copied in */
if(!rv) {
CKutil_StrAllocCopy(rv, "");
}
return rv;
}
PRUnichar *
CKutil_Localize(const PRUnichar *genericString) {
nsresult ret;

View File

@@ -42,17 +42,7 @@
#include "nsString.h"
#include "nsFileStream.h"
//#define GET_ALL_PARTS 127
#define GET_PASSWORD_PART 64
#define GET_USERNAME_PART 32
#define GET_PROTOCOL_PART 16
#define GET_HOST_PART 8
#define GET_PATH_PART 4
#define GET_HASH_PART 2
#define GET_SEARCH_PART 1
extern PRInt32 CKutil_GetLine(nsInputFileStream& strm, nsString& aLine);
extern char * CKutil_ParseURL (const char *url, int parts_requested);
extern PRUnichar* CKutil_Localize(const PRUnichar *genericString);
extern nsresult CKutil_ProfileDirectory(nsFileSpec& dirSpec);
extern char * CKutil_StrAllocCopy(char *&destination, const char *source);

View File

@@ -183,6 +183,7 @@ interface nsIIOService : nsISupports
const short url_Query = (1<<8);
const short url_Ref = (1<<9);
const short url_Path = (1<<10);
const short url_Port = (1<<11);
/**
* Get port from string.

View File

@@ -539,8 +539,9 @@ nsIOService::ExtractUrlPart(const char *urlString, PRInt16 flag, PRUint32 *start
rv = GetParserForScheme(scheme, getter_AddRefs(parser));
if (NS_FAILED(rv)) return rv;
PRInt32 port;
PRInt32 port = -1;
nsXPIDLCString dummyScheme, username, password, host, path;
char* portstr = nsnull;
rv = parser->ParseAtScheme(urlString,
getter_Copies(dummyScheme),
@@ -573,6 +574,33 @@ nsIOService::ExtractUrlPart(const char *urlString, PRInt16 flag, PRUint32 *start
return NS_OK;
}
if (flag == url_Port) {
if (port != -1) {
portstr = PR_smprintf("%d", port);
CalculateStartEndPos(urlString, portstr, startPos, endPos);
} else {
startPos = 0;
endPos = 0;
}
if (urlPart)
*urlPart = nsCRT::strdup(portstr);
PR_smprintf_free(portstr);
return NS_OK;
}
if (flag == (url_Host | url_Port)) {
nsCAutoString hostport(host);
if (port != -1) {
portstr = PR_smprintf(":%d", port);
hostport += portstr;
}
if (urlPart)
*urlPart = ToNewCString(hostport);
CalculateStartEndPos(urlString, *urlPart, startPos, endPos);
PR_smprintf_free(portstr);
return NS_OK;
}
if (flag == url_Path) {
CalculateStartEndPos(urlString, path, startPos, endPos);
if (urlPart)
@@ -580,7 +608,6 @@ nsIOService::ExtractUrlPart(const char *urlString, PRInt16 flag, PRUint32 *start
return NS_OK;
}
nsXPIDLCString directory, fileBaseName, fileExtension, param, query, ref;
rv = parser->ParseAtDirectory(path,
@@ -593,7 +620,6 @@ nsIOService::ExtractUrlPart(const char *urlString, PRInt16 flag, PRUint32 *start
if (NS_FAILED(rv)) return rv;
if (flag == url_Directory) {
CalculateStartEndPos(urlString, directory, startPos, endPos);
if (urlPart)
@@ -636,7 +662,6 @@ nsIOService::ExtractUrlPart(const char *urlString, PRInt16 flag, PRUint32 *start
return NS_OK;
}
return NS_OK;
}
@@ -851,8 +876,7 @@ nsIOService::AllowPort(PRInt32 inPort, const char *scheme, PRBool *_retval)
NS_IMETHODIMP
nsIOService::ExtractPort(const char *str, PRInt32 *result)
{
PRInt32 returnValue = -1;
*result = (0 < PR_sscanf(str, "%d", &returnValue)) ? returnValue : -1;
*result = ExtractPortFrom(str);
return NS_OK;
}