From f971538cf3162df5e76d43e7807d848dca9ccbf6 Mon Sep 17 00:00:00 2001 From: winlin Date: Tue, 12 Jan 2016 11:53:52 +0800 Subject: [PATCH] refine http, support no host. --- trunk/src/app/srs_app_http_conn.cpp | 34 +++++---------------- trunk/src/app/srs_app_utility.hpp | 3 ++ trunk/src/protocol/srs_protocol_utility.cpp | 21 +++++++++++++ 3 files changed, 31 insertions(+), 27 deletions(-) diff --git a/trunk/src/app/srs_app_http_conn.cpp b/trunk/src/app/srs_app_http_conn.cpp index 5a6dcf402..d23117c1a 100644 --- a/trunk/src/app/srs_app_http_conn.cpp +++ b/trunk/src/app/srs_app_http_conn.cpp @@ -58,6 +58,7 @@ using namespace std; #include #include #include +#include #endif @@ -541,11 +542,10 @@ int SrsHttpMessage::update(string url, bool allow_jsonp, http_parser* header, Sr // parse uri from url. std::string host = get_request_header("Host"); - // donot parse the empty host for uri, - // for example, the response contains no host, - // ignore it is ok. + // use server public ip when no host specified. + // to make telnet happy. if (host.empty()) { - return ret; + host= srs_get_public_internet_address(); } // parse uri to schema/server:port/path?query @@ -554,32 +554,12 @@ int SrsHttpMessage::update(string url, bool allow_jsonp, http_parser* header, Sr return ret; } - // must format as key=value&...&keyN=valueN - std::string q = _uri->get_query(); - size_t pos = string::npos; - while (!q.empty()) { - std::string k = q; - if ((pos = q.find("=")) != string::npos) { - k = q.substr(0, pos); - q = q.substr(pos + 1); - } else { - q = ""; - } - - std::string v = q; - if ((pos = q.find("&")) != string::npos) { - v = q.substr(0, pos); - q = q.substr(pos + 1); - } else { - q = ""; - } - - _query[k] = v; - } - // parse ext. _ext = srs_path_filext(_uri->get_path()); + // parse query string. + srs_parse_query_string(_uri->get_query(), _query); + // parse jsonp request message. if (allow_jsonp) { if (!query_get("callback").empty()) { diff --git a/trunk/src/app/srs_app_utility.hpp b/trunk/src/app/srs_app_utility.hpp index 433d01447..b53b12479 100644 --- a/trunk/src/app/srs_app_utility.hpp +++ b/trunk/src/app/srs_app_utility.hpp @@ -654,6 +654,9 @@ extern void srs_update_rtmp_server(int nb_conn, SrsKbps* kbps); // get local ip, fill to @param ips extern std::vector& srs_get_local_ipv4_ips(); +// get local public ip, empty string if no public internet address found. +extern std::string srs_get_public_internet_address(); + // get local or peer ip. // where local ip is the server ip which client connected. extern std::string srs_get_local_ip(int fd); diff --git a/trunk/src/protocol/srs_protocol_utility.cpp b/trunk/src/protocol/srs_protocol_utility.cpp index 1f76d94fe..8046a3fd1 100644 --- a/trunk/src/protocol/srs_protocol_utility.cpp +++ b/trunk/src/protocol/srs_protocol_utility.cpp @@ -117,6 +117,27 @@ void srs_discovery_tc_url( srs_vhost_resolve(vhost, app, param); } +void srs_parse_query_string(string q, map& query) +{ + // query string flags. + static vector flags; + if (flags.empty()) { + flags.push_back("="); + flags.push_back(","); + flags.push_back("&&"); + flags.push_back("&"); + flags.push_back(";"); + } + + vector kvs = srs_string_split(q, flags); + for (int i = 0; i < (int)kvs.size(); i+=2) { + string k = kvs.at(i); + string v = (i < (int)kvs.size() - 1)? kvs.at(i+1):""; + + query[k] = v; + } +} + void srs_random_generate(char* bytes, int size) { static bool _random_initialized = false;