From 6d3c4521f9bfd21c750382f67158e9771a94b592 Mon Sep 17 00:00:00 2001 From: "andreas.otte%primus-online.de" Date: Sat, 3 Jun 2000 09:17:16 +0000 Subject: [PATCH] fix reopened bug 27930 [URL parser should support RFC 2732 - IPv6 address literals] and bug 39340 [regression: file:/ URLs no longer work], r=gagan@netscape.com, a=waterson@mozilla.org --- netwerk/base/src/nsNoAuthURLParser.cpp | 66 +++++++++++++++++++------- 1 file changed, 50 insertions(+), 16 deletions(-) diff --git a/netwerk/base/src/nsNoAuthURLParser.cpp b/netwerk/base/src/nsNoAuthURLParser.cpp index 7adc63febe2d..5cfb79a74897 100644 --- a/netwerk/base/src/nsNoAuthURLParser.cpp +++ b/netwerk/base/src/nsNoAuthURLParser.cpp @@ -22,6 +22,7 @@ #include "nsCRT.h" #include "nsString.h" #include "prprf.h" +#include "prnetdb.h" NS_IMPL_THREADSAFE_ISUPPORTS(nsNoAuthURLParser, NS_GET_IID(nsIURLParser)) @@ -96,14 +97,25 @@ nsNoAuthURLParser::ParseAtPreHost(const char* i_Spec, char* *o_Username, PRInt32 *o_Port, char* *o_Path) { nsresult rv = NS_OK; - // Skip leading two slashes + // Skip leading two slashes if possible char* fwdPtr= (char*) i_Spec; - if (fwdPtr && (*fwdPtr != '\0') && (*fwdPtr == '/')) - fwdPtr++; - if (fwdPtr && (*fwdPtr != '\0') && (*fwdPtr == '/')) - fwdPtr++; - - // There is no PreHost + if (fwdPtr) { + if (*fwdPtr != '\0') + if (*fwdPtr != '/') + // No Host, go directly to path + return ParseAtPath(fwdPtr,o_Path); + else + // Move over the slash + fwdPtr++; + if (*fwdPtr != '\0') + if (*fwdPtr != '/') + // No Host, go directly to path + return ParseAtPath(fwdPtr,o_Path); + else + // Move over the slash + fwdPtr++; + } + // There maybe is a host rv = ParseAtHost(fwdPtr, o_Host, o_Port, o_Path); return rv; @@ -126,19 +138,41 @@ nsNoAuthURLParser::ParseAtHost(const char* i_Spec, char* *o_Host, // No host, okay ... rv = ParseAtPath(i_Spec, o_Path); } else { - // There seems be a host, drop it - char* brk = PL_strchr(i_Spec, '/'); + // There seems be a host ... + if (len > 1 && *i_Spec == '[') { + // Possible IPv6 address + PRNetAddr netaddr; + char* fwdPtr = strchr(i_Spec+1, ']'); + if (fwdPtr) { + rv = ExtractString((char*)i_Spec+1, o_Host, + (fwdPtr - i_Spec - 1)); + if (NS_FAILED(rv)) return rv; + rv = PR_StringToNetAddr(*o_Host, &netaddr); + if (rv != PR_SUCCESS || netaddr.raw.family != PR_AF_INET6) { + // try something else + CRTFREEIF(*o_Host); + } + } + } + static const char delimiters[] = "/?#"; + char* brk = PL_strpbrk(i_Spec, delimiters); if (!brk) { - // everything is the host - rv = DupString(o_Host, i_Spec); - if (NS_FAILED(rv)) return rv; - ToLowerCase(*o_Host); + // do we already have a host? + if (!*o_Host) { + // everything is the host + rv = DupString(o_Host, i_Spec); + if (NS_FAILED(rv)) return rv; + ToLowerCase(*o_Host); + } // parse after the host rv = ParseAtPath(i_Spec+len, o_Path); } else { - rv = ExtractString((char*)i_Spec, o_Host, (brk - i_Spec)); - if (NS_FAILED(rv)) return rv; - ToLowerCase(*o_Host); + // do we already have a host? + if (!*o_Host) { + rv = ExtractString((char*)i_Spec, o_Host, (brk - i_Spec)); + if (NS_FAILED(rv)) return rv; + ToLowerCase(*o_Host); + } // parse after the host rv = ParseAtPath(brk, o_Path); }