Skip to content

Commit 8d16abd

Browse files
author
Steve Hanson
committed
Uri Parse improvements
1 parent a21cf9f commit 8d16abd

File tree

2 files changed

+28
-20
lines changed

2 files changed

+28
-20
lines changed

include/rapidjson/uri.h

Lines changed: 13 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -295,24 +295,17 @@ class GenericUri {
295295
// Look for auth (//([^/?#]*))?
296296
auth_ = scheme_ + GetSchemeStringLength() + 1;
297297
*auth_ = '\0';
298-
if (start < len) {
299-
pos1 = start;
300-
while (pos1 < len) {
301-
if (uri[pos1] == '/' && uri[pos1 + 1] == '/') break;
302-
pos1++;
303-
}
304-
if (pos1 == start) {
305-
pos2 = start + 2;
306-
while (pos2 < len) {
307-
if (uri[pos2] == '/') break;
308-
if (uri[pos2] == '?') break;
309-
if (uri[pos2] == '#') break;
310-
pos2++;
311-
}
312-
std::memcpy(auth_, &uri[start], (pos2 - start) * sizeof(Ch));
313-
auth_[pos2 - start] = '\0';
314-
start = pos2;
298+
if (start < len - 1 && uri[start] == '/' && uri[start + 1] == '/') {
299+
pos2 = start + 2;
300+
while (pos2 < len) {
301+
if (uri[pos2] == '/') break;
302+
if (uri[pos2] == '?') break;
303+
if (uri[pos2] == '#') break;
304+
pos2++;
315305
}
306+
std::memcpy(auth_, &uri[start], (pos2 - start) * sizeof(Ch));
307+
auth_[pos2 - start] = '\0';
308+
start = pos2;
316309
}
317310
// Look for path ([^?#]*)
318311
path_ = auth_ + GetAuthStringLength() + 1;
@@ -335,8 +328,8 @@ class GenericUri {
335328
// Look for query (\?([^#]*))?
336329
query_ = path_ + GetPathStringLength() + 1;
337330
*query_ = '\0';
338-
if (start < len) {
339-
pos2 = start;
331+
if (start < len && uri[start] == '?') {
332+
pos2 = start + 1;
340333
while (pos2 < len) {
341334
if (uri[pos2] == '#') break;
342335
pos2++;
@@ -350,7 +343,7 @@ class GenericUri {
350343
// Look for fragment (#(.*))?
351344
frag_ = query_ + GetQueryStringLength() + 1;
352345
*frag_ = '\0';
353-
if (start < len) {
346+
if (start < len && uri[start] == '#') {
354347
std::memcpy(frag_, &uri[start], (len - start) * sizeof(Ch));
355348
frag_[len - start] = '\0';
356349
}

test/unittest/uritest.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,14 @@ TEST(Uri, Parse) {
160160
EXPECT_TRUE(u.GetBaseStringLength() == 0);
161161
EXPECT_TRUE(StrCmp(u.GetFragString(), "#frag/stuff") == 0);
162162
EXPECT_TRUE(u.GetFragStringLength() == len);
163+
164+
// Incomplete auth treated as path
165+
str = "http:/";
166+
const UriType u2 = UriType(str);
167+
EXPECT_TRUE(StrCmp(u2.GetSchemeString(), "http:") == 0);
168+
EXPECT_TRUE(u2.GetAuthStringLength() == 0);
169+
EXPECT_TRUE(StrCmp(u2.GetPathString(), "/") == 0);
170+
EXPECT_TRUE(StrCmp(u2.GetBaseString(), "http:/") == 0);
163171
}
164172

165173
TEST(Uri, Parse_UTF16) {
@@ -274,6 +282,13 @@ TEST(Uri, Parse_UTF16) {
274282
EXPECT_TRUE(u.GetBaseStringLength() == 0);
275283
EXPECT_TRUE(StrCmp(u.GetFragString(), L"#frag/stuff") == 0);
276284
EXPECT_TRUE(u.GetFragStringLength() == len);
285+
286+
// Incomplete auth treated as path
287+
u = UriType(L"http:/");
288+
EXPECT_TRUE(StrCmp(u.GetSchemeString(), L"http:") == 0);
289+
EXPECT_TRUE(u.GetAuthStringLength() == 0);
290+
EXPECT_TRUE(StrCmp(u.GetPathString(), L"/") == 0);
291+
EXPECT_TRUE(StrCmp(u.GetBaseString(), L"http:/") == 0);
277292
}
278293

279294
TEST(Uri, CopyConstructor) {

0 commit comments

Comments
 (0)