From c6bbb11548856f96526c963242f44c84dd5eeb13 Mon Sep 17 00:00:00 2001 From: wzl12356 Date: Wed, 8 Sep 2021 18:47:50 +0800 Subject: [PATCH] fix password bug --- src/util/URIParser.cc | 135 +++++++++++++++++-------------------- src/util/URIParser.h | 46 +++---------- test/CMakeLists.txt | 1 - test/uriparser_unittest.cc | 9 +++ 4 files changed, 77 insertions(+), 114 deletions(-) diff --git a/src/util/URIParser.cc b/src/util/URIParser.cc index 456fc3a5..20828565 100644 --- a/src/util/URIParser.cc +++ b/src/util/URIParser.cc @@ -38,7 +38,7 @@ enum //scheme://[userinfo@]host[:port][/path][?query][#fragment] //0-6 (scheme, userinfo, host, port, path, query, fragment) -static constexpr unsigned char valid_char[URI_PART_ELEMENTS][256] = { +static constexpr unsigned char valid_char[4][256] = { { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, @@ -111,60 +111,6 @@ static constexpr unsigned char valid_char[URI_PART_ELEMENTS][256] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, - { - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, - 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 0, 0, - 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, - 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 1, - 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, - 1, 1, 1, 1, 1, 1, 1, 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, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, }; -void ParsedURI::__copy(const ParsedURI& copy) +ParsedURI::ParsedURI(ParsedURI&& uri) +{ + scheme = uri.scheme; + userinfo = uri.userinfo; + host = uri.host; + port = uri.port; + path = uri.path; + query = uri.query; + fragment = uri.fragment; + state = uri.state; + error = uri.error; + uri.init(); +} + +ParsedURI& ParsedURI::operator= (ParsedURI&& uri) +{ + if (this != &uri) + { + deinit(); + scheme = uri.scheme; + userinfo = uri.userinfo; + host = uri.host; + port = uri.port; + path = uri.path; + query = uri.query; + fragment = uri.fragment; + state = uri.state; + error = uri.error; + uri.init(); + } + + return *this; +} + +void ParsedURI::copy(const ParsedURI& uri) { init(); - state = copy.state; - error = copy.error; + state = uri.state; + error = uri.error; if (state == URI_STATE_SUCCESS) { bool succ = false; do { - if (copy.scheme) + if (uri.scheme) { - scheme = strdup(copy.scheme); + scheme = strdup(uri.scheme); if (!scheme) break; } - if (copy.userinfo) + if (uri.userinfo) { - userinfo = strdup(copy.userinfo); + userinfo = strdup(uri.userinfo); if (!userinfo) break; } - if (copy.host) + if (uri.host) { - host = strdup(copy.host); + host = strdup(uri.host); if (!host) break; } - if (copy.port) + if (uri.port) { - port = strdup(copy.port); + port = strdup(uri.port); if (!port) break; } - if (copy.path) + if (uri.path) { - path = strdup(copy.path); + path = strdup(uri.path); if (!path) break; } - if (copy.query) + if (uri.query) { - query = strdup(copy.query); + query = strdup(uri.query); if (!query) break; } - if (copy.fragment) + if (uri.fragment) { - fragment = strdup(copy.fragment); + fragment = strdup(uri.fragment); if (!fragment) break; } @@ -367,9 +347,7 @@ int URIParser::parse(const char *str, ParsedURI& uri) break; default: - if (pre_state != URI_QUERY && - !valid_char[pre_state][(unsigned char)str[i]]) - return -1;//invalid char + break; } if (state >= URI_FRAGMENT) @@ -404,6 +382,13 @@ int URIParser::parse(const char *str, ParsedURI& uri) end_idx[pre_state] = i + strlen(str + i); } + for (int i = 0; i < URI_QUERY; i++) + { + for (int j = start_idx[i]; j < end_idx[i]; j++) + if (!valid_char[i][(unsigned char)str[j]]) + return -1;//invalid char + } + char **dst[URI_PART_ELEMENTS] = {&uri.scheme, &uri.userinfo, &uri.host, &uri.port, &uri.query, &uri.fragment, &uri.path}; diff --git a/src/util/URIParser.h b/src/util/URIParser.h index 4e5765f9..677125cc 100644 --- a/src/util/URIParser.h +++ b/src/util/URIParser.h @@ -14,7 +14,7 @@ limitations under the License. Authors: Wu Jiaxu (wujiaxu@sogou-inc.com) - Wang Zhulei (wangzhulei@sogou-inc.com) + Wang Zhulei (wangzhulei@sogou-inc.com) */ #ifndef _URIPARSER_H_ @@ -53,53 +53,23 @@ public: virtual ~ParsedURI() { deinit(); } //copy constructor - ParsedURI(const ParsedURI& copy) { __copy(copy); } + ParsedURI(const ParsedURI& uri) { copy(uri); } //copy operator - ParsedURI& operator= (const ParsedURI& copy) + ParsedURI& operator= (const ParsedURI& uri) { - if (this != ©) + if (this != &uri) { deinit(); - __copy(copy); + copy(uri); } return *this; } //move constructor - ParsedURI(ParsedURI&& move) - { - scheme = move.scheme; - userinfo = move.userinfo; - host = move.host; - port = move.port; - path = move.path; - query = move.query; - fragment = move.fragment; - state = move.state; - error = move.error; - move.init(); - } + ParsedURI(ParsedURI&& uri); //move operator - ParsedURI& operator= (ParsedURI&& move) - { - if (this != &move) - { - deinit(); - scheme = move.scheme; - userinfo = move.userinfo; - host = move.host; - port = move.port; - path = move.path; - query = move.query; - fragment = move.fragment; - state = move.state; - error = move.error; - move.init(); - } - - return *this; - } + ParsedURI& operator= (ParsedURI&& uri); private: void init() @@ -126,7 +96,7 @@ private: free(fragment); } - void __copy(const ParsedURI& copy); + void copy(const ParsedURI& uri); }; // static class diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index 84cf47c7..49f6b691 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -54,7 +54,6 @@ foreach(src ${TEST_LIST}) target_link_libraries(${src} ${WORKFLOW_LIB} GTest::GTest GTest::Main) add_test(${src} ${src}) add_dependencies(check ${src}) - set_property(SOURCE ${src} APPEND PROPERTY COMPILE_OPTIONS "-fno-rtti") endforeach() foreach(src ${TEST_LIST}) diff --git a/test/uriparser_unittest.cc b/test/uriparser_unittest.cc index af0de2e0..fad249e6 100644 --- a/test/uriparser_unittest.cc +++ b/test/uriparser_unittest.cc @@ -23,6 +23,15 @@ TEST(uriparser_unittest, parse) { ParsedURI uri; + EXPECT_EQ(URIParser::parse("https://john.doe:pass@www.example.com:123/forum/questions/?tag=networking&order=newest#top", uri), 0); + EXPECT_EQ(strcmp(uri.scheme, "https"), 0); + EXPECT_EQ(strcmp(uri.userinfo, "john.doe:pass"), 0); + EXPECT_EQ(strcmp(uri.host, "www.example.com"), 0); + EXPECT_EQ(strcmp(uri.port, "123"), 0); + EXPECT_EQ(strcmp(uri.path, "/forum/questions/"), 0); + EXPECT_EQ(strcmp(uri.query, "tag=networking&order=newest"), 0); + EXPECT_EQ(strcmp(uri.fragment, "top"), 0); + EXPECT_EQ(URIParser::parse("https://john.doe@www.example.com:123/forum/questions/?tag=networking&order=newest#top", uri), 0); EXPECT_EQ(strcmp(uri.scheme, "https"), 0); EXPECT_EQ(strcmp(uri.userinfo, "john.doe"), 0);