Bug 10736 - URL parsing / MakeAbsolute performance. code=putterman,r=warren,andreas,a=jar

This commit is contained in:
warren%netscape.com 2000-02-25 06:17:57 +00:00
parent c961693de5
commit d50d3d3318
4 changed files with 69 additions and 39 deletions

View File

@ -294,9 +294,17 @@ nsIOService::SetOffline(PRBool offline)
// URL parsing utilities
NS_IMETHODIMP
nsIOService::Escape(const char *str, PRInt16 mask, char **result)
nsIOService::Escape(const char *str, PRInt16 mask, char** result)
{
return nsURLEscape((char*)str,mask,result);
nsCAutoString esc_str;
nsresult rv = nsURLEscape((char*)str,mask,esc_str);
CRTFREEIF(*result);
if (NS_FAILED(rv))
return rv;
*result = esc_str.ToNewCString();
if (!*result)
return NS_ERROR_OUT_OF_MEMORY;
return NS_OK;
}
NS_IMETHODIMP

View File

@ -286,16 +286,12 @@ nsStdURL::AppendString(nsCString& buffer, char * fromUnescapedStr,
if (!fromUnescapedStr)
return NS_ERROR_FAILURE;
char* temp = nsnull;
if (toFormat == ESCAPED) {
rv = nsURLEscape(fromUnescapedStr, mask, &temp);
if (NS_SUCCEEDED(rv))
buffer += temp;
rv = nsAppendURLEscapedString(buffer, fromUnescapedStr, mask);
} else {
buffer += fromUnescapedStr;
}
CRTFREEIF(temp);
return rv;
}
@ -348,7 +344,6 @@ nsStdURL::GetSpec(char **o_Spec)
{
nsresult rv = NS_OK;
nsCAutoString finalSpec; // guaranteed to be singlebyte.
finalSpec.SetCapacity(64);
if (mScheme)
{
rv = AppendString(finalSpec,mScheme,ESCAPED,nsIIOService::url_Scheme);
@ -761,7 +756,6 @@ nsStdURL::GetPath(char** o_Path)
//Take all the elements of the path and construct it
nsCAutoString path;
nsresult rv = NS_OK;
path.SetCapacity(64);
if (mDirectory)
{
rv = AppendString(path,mDirectory,ESCAPED,nsIIOService::url_Directory);

View File

@ -29,8 +29,8 @@ const int EscapeChars[256] =
{
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,1008, 896, 0, 896, 0, 768, /* 3x 0123456789:;<=>? */
0,1023, 0, 512, 761, 0,1023, 0,1023,1023,1023,1023,1023,1023, 959,1016, /* 2x !"#$%&'()*+,-./ */
1023,1023,1023,1023,1023,1023,1023,1023,1023,1023,1008, 896, 0,1008, 0, 768, /* 3x 0123456789:;<=>? */
992,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 */
@ -53,54 +53,70 @@ const int EscapeChars[256] =
/* returns an escaped string */
NS_NET nsresult
nsURLEscape(const char* str, PRInt16 mask, char **result)
nsURLEscape(const char* str, PRInt16 mask, nsCString &result)
{
if (!str) {
*result = nsnull;
return NS_OK;
}
int i, extra = 0;
int i = 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;
char tempBuffer[100];
int tempBufferPos = 0;
char c1[] = " ";
char c2[] = " ";
char* const pc1 = c1;
char* const pc2 = c2;
for (i = 0; i < len; i++)
{
const char* checker = (char*) src;
c1[0] = *(src+1);
c2[0] = *(src+2);
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;
if (IS_OK(c) || (c == HEX_ESCAPE && (pc1) && (pc2) &&
PL_strpbrk(pc1, CheckHexChars) != 0 &&
PL_strpbrk(pc2, CheckHexChars) != 0)) {
tempBuffer[tempBufferPos++]=c;
}
else
/* do the escape magic */
{
*dst++ = HEX_ESCAPE;
*dst++ = hexChars[c >> 4]; /* high nibble */
*dst++ = hexChars[c & 0x0f]; /* low nibble */
tempBuffer[tempBufferPos++] = HEX_ESCAPE;
tempBuffer[tempBufferPos++] = hexChars[c >> 4]; /* high nibble */
tempBuffer[tempBufferPos++] = hexChars[c & 0x0f]; /* low nibble */
}
if(tempBufferPos == 96)
{
tempBuffer[tempBufferPos] = '\0';
result += tempBuffer;
tempBufferPos = 0;
}
}
*dst = '\0';
tempBuffer[tempBufferPos] = '\0';
result += tempBuffer;
return NS_OK;
}
/* helper call function */
NS_NET nsresult
nsAppendURLEscapedString(nsCString& originalStr, const char* str, PRInt16 mask)
{
return(nsURLEscape(str, mask, originalStr));
}
/* returns an unescaped string */
NS_NET nsresult
nsURLUnescape(char* str, char **result)
@ -117,13 +133,21 @@ nsURLUnescape(char* str, char **result)
if (!*result)
return NS_ERROR_OUT_OF_MEMORY;
src = str;
register unsigned char* dst = (unsigned char *) *result;
while (*src)
char c1[] = " ";
char c2[] = " ";
char* const pc1 = c1;
char* const pc2 = c2;
while (*src) {
c1[0] = *(src+1);
c2[0] = *(src+2);
/* check for valid escaped sequence */
if (*src != HEX_ESCAPE || PL_strpbrk(((src+1)), hexChars) == 0 ||
PL_strpbrk(((src+2)), hexChars) == 0 )
if (*src != HEX_ESCAPE || PL_strpbrk(pc1, hexChars) == 0 ||
PL_strpbrk(pc2, hexChars) == 0 )
*dst++ = *src++;
else
{
@ -140,7 +164,7 @@ nsURLUnescape(char* str, char **result)
}
dst++;
}
}
*dst = '\0';
return NS_OK;
}

View File

@ -23,13 +23,17 @@
#include "prtypes.h"
#include "nscore.h"
#include "nsCRT.h"
#include "nsString.h"
#ifdef __cplusplus
extern "C" {
#endif
/* encode characters into % escaped hexcodes */
NS_NET nsresult nsURLEscape (const char* str, PRInt16 mask, char ** result);
NS_NET nsresult nsURLEscape (const char* str, PRInt16 mask, nsCString &result);
/* helper call function */
NS_NET nsresult nsAppendURLEscapedString(nsCString& originalStr, const char* str, PRInt16 mask);
/* decode % escaped hex codes into character values */
NS_NET nsresult nsURLUnescape(char* str, char ** result);