From 07c04a042a36e8a7a546532bcf962e777bc30b57 Mon Sep 17 00:00:00 2001 From: winlin Date: Tue, 10 Nov 2020 18:51:21 +0800 Subject: [PATCH] URI: Refine uri parser --- trunk/src/kernel/srs_kernel_consts.hpp | 14 ++++++- trunk/src/protocol/srs_http_stack.cpp | 37 ++++++++++++++--- trunk/src/protocol/srs_http_stack.hpp | 7 ++-- trunk/src/utest/srs_utest_http.cpp | 56 ++++++++++++++++++++++++++ 4 files changed, 103 insertions(+), 11 deletions(-) diff --git a/trunk/src/kernel/srs_kernel_consts.hpp b/trunk/src/kernel/srs_kernel_consts.hpp index addda5696..acb56b4c6 100644 --- a/trunk/src/kernel/srs_kernel_consts.hpp +++ b/trunk/src/kernel/srs_kernel_consts.hpp @@ -26,6 +26,18 @@ #include +// Default port of rtmp +#define SRS_CONSTS_RTMP_DEFAULT_PORT 1935 + +// Default http listen port. +#define SRS_DEFAULT_HTTP_PORT 80 + +// Default https listen port. +#define SRS_DEFAULT_HTTPS_PORT 443 + +// Default Redis listen port. +#define SRS_DEFAULT_REDIS_PORT 6379 + /////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////// @@ -39,8 +51,6 @@ // Default vhost of rtmp #define SRS_CONSTS_RTMP_DEFAULT_VHOST "__defaultVhost__" #define SRS_CONSTS_RTMP_DEFAULT_APP "__defaultApp__" -// Default port of rtmp -#define SRS_CONSTS_RTMP_DEFAULT_PORT 1935 // The default chunk size for system. #define SRS_CONSTS_RTMP_SRS_CHUNK_SIZE 60000 diff --git a/trunk/src/protocol/srs_http_stack.cpp b/trunk/src/protocol/srs_http_stack.cpp index f0db261af..48c1f456f 100644 --- a/trunk/src/protocol/srs_http_stack.cpp +++ b/trunk/src/protocol/srs_http_stack.cpp @@ -873,7 +873,7 @@ ISrsHttpMessage::~ISrsHttpMessage() SrsHttpUri::SrsHttpUri() { - port = SRS_DEFAULT_HTTP_PORT; + port = 0; } SrsHttpUri::~SrsHttpUri() @@ -891,28 +891,43 @@ srs_error_t SrsHttpUri::initialize(string _url) http_parser_url hp_u; int r0; - if((r0 = http_parser_parse_url(purl, url.length(), 0, &hp_u)) != 0){ + if ((r0 = http_parser_parse_url(purl, url.length(), 0, &hp_u)) != 0){ return srs_error_new(ERROR_HTTP_PARSE_URI, "parse url %s failed, code=%d", purl, r0); } std::string field = get_uri_field(url, &hp_u, UF_SCHEMA); - if(!field.empty()){ + if (!field.empty()){ schema = field; } host = get_uri_field(url, &hp_u, UF_HOST); field = get_uri_field(url, &hp_u, UF_PORT); - if(!field.empty()){ + if (!field.empty()) { port = atoi(field.c_str()); } - if(port<=0){ - port = SRS_DEFAULT_HTTP_PORT; + if (port <= 0) { + if (schema == "https") { + port = SRS_DEFAULT_HTTPS_PORT; + } else if (schema == "rtmp") { + port = SRS_CONSTS_RTMP_DEFAULT_PORT; + } else if (schema == "redis") { + port = SRS_DEFAULT_REDIS_PORT; + } else { + port = SRS_DEFAULT_HTTP_PORT; + } } path = get_uri_field(url, &hp_u, UF_PATH); query = get_uri_field(url, &hp_u, UF_QUERY); + username_ = get_uri_field(url, &hp_u, UF_USERINFO); + size_t pos = username_.find(":"); + if (pos != string::npos) { + password_ = username_.substr(pos+1); + username_ = username_.substr(0, pos); + } + return err; } @@ -957,6 +972,16 @@ string SrsHttpUri::get_query() return query; } +std::string SrsHttpUri::username() +{ + return username_; +} + +std::string SrsHttpUri::password() +{ + return password_; +} + string SrsHttpUri::get_uri_field(string uri, void* php_u, int ifield) { http_parser_url* hp_u = (http_parser_url*)php_u; diff --git a/trunk/src/protocol/srs_http_stack.hpp b/trunk/src/protocol/srs_http_stack.hpp index 14a6fc8cc..37848a560 100644 --- a/trunk/src/protocol/srs_http_stack.hpp +++ b/trunk/src/protocol/srs_http_stack.hpp @@ -28,9 +28,6 @@ #include -// Default http listen port. -#define SRS_DEFAULT_HTTP_PORT 80 - #if !defined(SRS_EXPORT_LIBRTMP) #include @@ -519,6 +516,8 @@ private: int port; std::string path; std::string query; + std::string username_; + std::string password_; public: SrsHttpUri(); virtual ~SrsHttpUri(); @@ -534,6 +533,8 @@ public: virtual int get_port(); virtual std::string get_path(); virtual std::string get_query(); + virtual std::string username(); + virtual std::string password(); private: // Get the parsed url field. // @return return empty string if not set. diff --git a/trunk/src/utest/srs_utest_http.cpp b/trunk/src/utest/srs_utest_http.cpp index 7a65368aa..bfd3b5291 100644 --- a/trunk/src/utest/srs_utest_http.cpp +++ b/trunk/src/utest/srs_utest_http.cpp @@ -1847,3 +1847,59 @@ VOID TEST(ProtocolHTTPTest, ParsingLargeMessages) EXPECT_EQ(354, (int)body.length()); } } + +VOID TEST(ProtocolHTTPTest, ParseUri) +{ + srs_error_t err; + + if (true) { + SrsHttpUri uri; + HELPER_EXPECT_SUCCESS(uri.initialize("http://ossrs.net/index.html")); + EXPECT_STREQ("http", uri.get_schema().c_str()); + EXPECT_STREQ("ossrs.net", uri.get_host().c_str()); + EXPECT_EQ(80, uri.get_port()); + EXPECT_STREQ("/index.html", uri.get_path().c_str()); + } + + if (true) { + SrsHttpUri uri; + HELPER_EXPECT_SUCCESS(uri.initialize("rtmp://ossrs.net/live/livestream")); + EXPECT_STREQ("rtmp", uri.get_schema().c_str()); + EXPECT_STREQ("ossrs.net", uri.get_host().c_str()); + EXPECT_EQ(1935, uri.get_port()); + EXPECT_STREQ("/live/livestream", uri.get_path().c_str()); + } + + if (true) { + SrsHttpUri uri; + HELPER_EXPECT_SUCCESS(uri.initialize("http://user:passwd@ossrs.net/index.html")); + EXPECT_STREQ("http", uri.get_schema().c_str()); + EXPECT_STREQ("ossrs.net", uri.get_host().c_str()); + EXPECT_STREQ("user", uri.username().c_str()); + EXPECT_STREQ("passwd", uri.password().c_str()); + EXPECT_EQ(80, uri.get_port()); + EXPECT_STREQ("/index.html", uri.get_path().c_str()); + } + + if (true) { + SrsHttpUri uri; + HELPER_EXPECT_SUCCESS(uri.initialize("https://user:passwd@ossrs.net/index.html")); + EXPECT_STREQ("https", uri.get_schema().c_str()); + EXPECT_STREQ("ossrs.net", uri.get_host().c_str()); + EXPECT_STREQ("user", uri.username().c_str()); + EXPECT_STREQ("passwd", uri.password().c_str()); + EXPECT_EQ(443, uri.get_port()); + EXPECT_STREQ("/index.html", uri.get_path().c_str()); + } + + if (true) { + SrsHttpUri uri; + HELPER_EXPECT_SUCCESS(uri.initialize("redis://user:passwd@ossrs.net/0")); + EXPECT_STREQ("redis", uri.get_schema().c_str()); + EXPECT_STREQ("ossrs.net", uri.get_host().c_str()); + EXPECT_STREQ("user", uri.username().c_str()); + EXPECT_STREQ("passwd", uri.password().c_str()); + EXPECT_EQ(6379, uri.get_port()); + EXPECT_STREQ("/0", uri.get_path().c_str()); + } +}