mirror of
https://github.com/ossrs/srs.git
synced 2025-02-15 04:42:04 +00:00
URI: Refine uri parser
This commit is contained in:
parent
efca38cd89
commit
07c04a042a
4 changed files with 103 additions and 11 deletions
|
@ -26,6 +26,18 @@
|
|||
|
||||
#include <srs_core.hpp>
|
||||
|
||||
// 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
|
||||
|
|
|
@ -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;
|
||||
|
|
|
@ -28,9 +28,6 @@
|
|||
|
||||
#include <srs_kernel_io.hpp>
|
||||
|
||||
// Default http listen port.
|
||||
#define SRS_DEFAULT_HTTP_PORT 80
|
||||
|
||||
#if !defined(SRS_EXPORT_LIBRTMP)
|
||||
|
||||
#include <map>
|
||||
|
@ -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.
|
||||
|
|
|
@ -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());
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue