diff --git a/netwerk/base/src/nsStandardURL.cpp b/netwerk/base/src/nsStandardURL.cpp index db6d95b0baf..273fff5ad4e 100644 --- a/netwerk/base/src/nsStandardURL.cpp +++ b/netwerk/base/src/nsStandardURL.cpp @@ -71,13 +71,11 @@ static PRInt32 AppendEscaped(nsACString &buf, const char *str, PRInt32 len, PRInt16 mask) { nsCAutoString escaped; - NS_EscapeURL(str, len, mask, escaped); - if (escaped.IsEmpty()) - buf.Append(str, len); - else { - buf.Append(escaped); + if (NS_EscapeURLPart(str, len, mask, escaped)) { + str = escaped.get(); len = escaped.Length(); } + buf.Append(str, len); return len; } @@ -183,11 +181,10 @@ nsStandardURL::EscapeSegment(const char *str, const URLSegment &seg, PRInt16 mas { PRInt32 len = 0; if (seg.mLen > 0) { - NS_EscapeURL(str + seg.mPos, seg.mLen, mask, result); - if (result.IsEmpty()) - len = seg.mLen; - else + if (NS_EscapeURLPart(str + seg.mPos, seg.mLen, mask, result)) len = result.Length(); + else + len = seg.mLen; } return len; } @@ -873,8 +870,7 @@ nsStandardURL::SetUsername(const char *username) // escape username if necessary nsCAutoString escaped; - NS_EscapeURL(username, len, esc_Username, escaped); - if (!escaped.IsEmpty()) { + if (NS_EscapeURLPart(username, len, esc_Username, escaped)) { username = escaped.get(); len = escaped.Length(); } @@ -934,8 +930,7 @@ nsStandardURL::SetPassword(const char *password) // escape password if necessary nsCAutoString escaped; - NS_EscapeURL(password, len, esc_Password, escaped); - if (!escaped.IsEmpty()) { + if (NS_EscapeURLPart(password, len, esc_Password, escaped)) { password = escaped.get(); len = escaped.Length(); } @@ -1423,8 +1418,7 @@ nsStandardURL::SetQuery(const char *query) // escape query if necessary nsCAutoString escaped; - NS_EscapeURL(query, queryLen, esc_Query, escaped); - if (!escaped.IsEmpty()) { + if (NS_EscapeURLPart(query, queryLen, esc_Query, escaped)) { query = escaped.get(); queryLen = escaped.Length(); } @@ -1475,8 +1469,7 @@ nsStandardURL::SetRef(const char *ref) // escape ref if necessary nsCAutoString escaped; - NS_EscapeURL(ref, refLen, esc_Ref, escaped); - if (!escaped.IsEmpty()) { + if (NS_EscapeURLPart(ref, refLen, esc_Ref, escaped)) { ref = escaped.get(); refLen = escaped.Length(); } diff --git a/netwerk/base/src/nsURLHelper.cpp b/netwerk/base/src/nsURLHelper.cpp index 72eb0970b61..286fd4e14dd 100644 --- a/netwerk/base/src/nsURLHelper.cpp +++ b/netwerk/base/src/nsURLHelper.cpp @@ -47,16 +47,6 @@ #include // ::IsDBCSLeadByte need #endif -/* helper call function */ -nsresult -nsAppendURLEscapedString(nsCString& originalStr, const char* str, PRInt16 mask) -{ - nsCAutoString result; - nsresult rv = nsStdEscape(str, mask, result); - originalStr += result; - return rv; -} - /* extracts first number from a string and assumes that this is the port number*/ PRInt32 ExtractPortFrom(const char* src) diff --git a/netwerk/base/src/nsURLHelper.h b/netwerk/base/src/nsURLHelper.h index f4b3f0ef342..9469811c596 100644 --- a/netwerk/base/src/nsURLHelper.h +++ b/netwerk/base/src/nsURLHelper.h @@ -47,9 +47,6 @@ extern "C" { #endif -/* helper call function */ -nsresult nsAppendURLEscapedString(nsCString& originalStr, const char* str, PRInt16 mask); - /* Get port from string */ PRInt32 ExtractPortFrom(const char* src); diff --git a/xpcom/io/nsEscape.cpp b/xpcom/io/nsEscape.cpp index ef847ce324e..f2031185906 100644 --- a/xpcom/io/nsEscape.cpp +++ b/xpcom/io/nsEscape.cpp @@ -349,8 +349,8 @@ const int EscapeChars[256] = NS_COM nsresult nsStdEscape(const char* str, PRInt16 mask, nsCString &result) { - result.Truncate(0); - nsresult rv = NS_EscapeURL(str, -1, mask, result); + result.Truncate(); + nsresult rv = NS_EscapeURLPart(str, -1, mask, result); if (NS_SUCCEEDED(rv) && result.IsEmpty()) result = str; return rv; @@ -365,30 +365,30 @@ NS_COM nsresult nsStdUnescape(char *str, char **result) return NS_OK; } -NS_COM nsresult NS_EscapeURL(const char *str, - PRInt32 len, - PRInt16 mask, - nsACString &result) +NS_COM PRBool NS_EscapeURLPart(const char *part, + PRInt32 partLen, + PRInt16 mask, + nsACString &result) { - if (!str) - return NS_OK; + if (!part) + return PR_FALSE; int i = 0; static const char hexChars[] = "0123456789ABCDEF"; - if (len < 0) - len = strlen(str); + if (partLen < 0) + partLen = strlen(part); PRBool forced = PR_FALSE; PRBool writing = PR_FALSE; if (mask & esc_Forced) forced = PR_TRUE; - register const unsigned char* src = (const unsigned char *) str; + register const unsigned char* src = (const unsigned char *) part; char tempBuffer[100]; unsigned int tempBufferPos = 0; - for (i = 0; i < len; i++) + for (i = 0; i < partLen; i++) { unsigned char c = *src++; @@ -407,8 +407,7 @@ NS_COM nsresult NS_EscapeURL(const char *str, { if (!writing) { - result.Truncate(0); - result.Append(str, i); + result.Append(part, i); writing = PR_TRUE; } tempBuffer[tempBufferPos++] = HEX_ESCAPE; @@ -428,6 +427,10 @@ NS_COM nsresult NS_EscapeURL(const char *str, tempBuffer[tempBufferPos] = '\0'; result += tempBuffer; } - return NS_OK; + return writing; } +NS_COM void NS_UnescapeURL(char *str) +{ + nsUnescape(str); +} diff --git a/xpcom/io/nsEscape.h b/xpcom/io/nsEscape.h index fa680a08f68..cf71f51c499 100644 --- a/xpcom/io/nsEscape.h +++ b/xpcom/io/nsEscape.h @@ -85,10 +85,20 @@ nsEscapeHTML2(const PRUnichar *aSourceBuffer, */ - /** - * Constants for the mask in the call to nsStdEscape - */ +/** + * DEPRACATED API: use NS_EscapeURLPart/NS_UnescapeURL instead + */ +NS_COM nsresult nsStdEscape(const char* str, PRInt16 mask, nsCString &result); +NS_COM nsresult nsStdUnescape(char* str, char **result); +#ifdef __cplusplus +} +#endif + + +/** + * Constants for partType in the call to NS_EscapeURLPart + */ enum EscapeMask { esc_Scheme = 1, esc_Username = 2, @@ -103,30 +113,28 @@ enum EscapeMask { esc_Forced = 1024 }; -NS_COM nsresult nsStdEscape(const char* str, PRInt16 mask, nsCString &result); -NS_COM nsresult nsStdUnescape(char* str, char **result); +/** + * NS_EscapeURLPart + * + * Escapes invalid char's in an URL segment. Has no side-effect if the URL + * segment is already escaped. Otherwise, the escaped URL segment is appended + * to |result|. + * + * @param part - url segment string + * @param partLen - url segment string length (-1 if unknown) + * @param partType - url segment type flag + * @param result - result buffer, untouched if part is already escaped + * + * @return TRUE if escaping was performed, FALSE otherwise. + */ +NS_COM PRBool NS_EscapeURLPart(const char *part, + PRInt32 partLen, + PRInt16 partType, + nsACString &result); /** - * NS_EscapeURL - * - * Escapes invalid char's in an URL segment. - * - * @param str - the url string - * @param len - optional string length (-1 if unknown) - * @param mask - url segment type mask - * @param result - result buffer, untouched if escaping & filtering not necessary + * Expands URL escape sequences. Equivalent to nsUnescape. */ -NS_COM nsresult NS_EscapeURL(const char *str, - PRInt32 len, - PRInt16 mask, - nsACString &result); - -#define NS_UnescapeURL(str) nsUnescape(str) - -#ifdef __cplusplus -} -#endif - - +NS_COM void NS_UnescapeURL(char *str); #endif // _ESCAPE_H_