Uri Parse improvements

This commit is contained in:
Steve Hanson 2021-06-30 17:09:52 +01:00
parent a21cf9f7b8
commit 8d16abd980
2 changed files with 28 additions and 20 deletions

View File

@ -295,24 +295,17 @@ private:
// Look for auth (//([^/?#]*))? // Look for auth (//([^/?#]*))?
auth_ = scheme_ + GetSchemeStringLength() + 1; auth_ = scheme_ + GetSchemeStringLength() + 1;
*auth_ = '\0'; *auth_ = '\0';
if (start < len) { if (start < len - 1 && uri[start] == '/' && uri[start + 1] == '/') {
pos1 = start; pos2 = start + 2;
while (pos1 < len) { while (pos2 < len) {
if (uri[pos1] == '/' && uri[pos1 + 1] == '/') break; if (uri[pos2] == '/') break;
pos1++; if (uri[pos2] == '?') break;
} if (uri[pos2] == '#') break;
if (pos1 == start) { pos2++;
pos2 = start + 2;
while (pos2 < len) {
if (uri[pos2] == '/') break;
if (uri[pos2] == '?') break;
if (uri[pos2] == '#') break;
pos2++;
}
std::memcpy(auth_, &uri[start], (pos2 - start) * sizeof(Ch));
auth_[pos2 - start] = '\0';
start = pos2;
} }
std::memcpy(auth_, &uri[start], (pos2 - start) * sizeof(Ch));
auth_[pos2 - start] = '\0';
start = pos2;
} }
// Look for path ([^?#]*) // Look for path ([^?#]*)
path_ = auth_ + GetAuthStringLength() + 1; path_ = auth_ + GetAuthStringLength() + 1;
@ -335,8 +328,8 @@ private:
// Look for query (\?([^#]*))? // Look for query (\?([^#]*))?
query_ = path_ + GetPathStringLength() + 1; query_ = path_ + GetPathStringLength() + 1;
*query_ = '\0'; *query_ = '\0';
if (start < len) { if (start < len && uri[start] == '?') {
pos2 = start; pos2 = start + 1;
while (pos2 < len) { while (pos2 < len) {
if (uri[pos2] == '#') break; if (uri[pos2] == '#') break;
pos2++; pos2++;
@ -350,7 +343,7 @@ private:
// Look for fragment (#(.*))? // Look for fragment (#(.*))?
frag_ = query_ + GetQueryStringLength() + 1; frag_ = query_ + GetQueryStringLength() + 1;
*frag_ = '\0'; *frag_ = '\0';
if (start < len) { if (start < len && uri[start] == '#') {
std::memcpy(frag_, &uri[start], (len - start) * sizeof(Ch)); std::memcpy(frag_, &uri[start], (len - start) * sizeof(Ch));
frag_[len - start] = '\0'; frag_[len - start] = '\0';
} }

View File

@ -160,6 +160,14 @@ TEST(Uri, Parse) {
EXPECT_TRUE(u.GetBaseStringLength() == 0); EXPECT_TRUE(u.GetBaseStringLength() == 0);
EXPECT_TRUE(StrCmp(u.GetFragString(), "#frag/stuff") == 0); EXPECT_TRUE(StrCmp(u.GetFragString(), "#frag/stuff") == 0);
EXPECT_TRUE(u.GetFragStringLength() == len); EXPECT_TRUE(u.GetFragStringLength() == len);
// Incomplete auth treated as path
str = "http:/";
const UriType u2 = UriType(str);
EXPECT_TRUE(StrCmp(u2.GetSchemeString(), "http:") == 0);
EXPECT_TRUE(u2.GetAuthStringLength() == 0);
EXPECT_TRUE(StrCmp(u2.GetPathString(), "/") == 0);
EXPECT_TRUE(StrCmp(u2.GetBaseString(), "http:/") == 0);
} }
TEST(Uri, Parse_UTF16) { TEST(Uri, Parse_UTF16) {
@ -274,6 +282,13 @@ TEST(Uri, Parse_UTF16) {
EXPECT_TRUE(u.GetBaseStringLength() == 0); EXPECT_TRUE(u.GetBaseStringLength() == 0);
EXPECT_TRUE(StrCmp(u.GetFragString(), L"#frag/stuff") == 0); EXPECT_TRUE(StrCmp(u.GetFragString(), L"#frag/stuff") == 0);
EXPECT_TRUE(u.GetFragStringLength() == len); EXPECT_TRUE(u.GetFragStringLength() == len);
// Incomplete auth treated as path
u = UriType(L"http:/");
EXPECT_TRUE(StrCmp(u.GetSchemeString(), L"http:") == 0);
EXPECT_TRUE(u.GetAuthStringLength() == 0);
EXPECT_TRUE(StrCmp(u.GetPathString(), L"/") == 0);
EXPECT_TRUE(StrCmp(u.GetBaseString(), L"http:/") == 0);
} }
TEST(Uri, CopyConstructor) { TEST(Uri, CopyConstructor) {