From 831c78062b9b1e6e9dce903e3fda1e3005fedbd8 Mon Sep 17 00:00:00 2001 From: winlin Date: Sun, 27 Dec 2020 12:55:27 +0800 Subject: [PATCH] Remove vhost in query if not present it --- trunk/src/app/srs_app_edge.cpp | 11 ++++-- trunk/src/app/srs_app_forward.cpp | 5 ++- trunk/src/protocol/srs_protocol_utility.cpp | 39 ++++++++++++++------- trunk/src/service/srs_service_rtmp_conn.cpp | 14 ++++++-- trunk/src/service/srs_service_rtmp_conn.hpp | 4 +-- 5 files changed, 53 insertions(+), 20 deletions(-) diff --git a/trunk/src/app/srs_app_edge.cpp b/trunk/src/app/srs_app_edge.cpp index f883524a33..2eff21d950 100644 --- a/trunk/src/app/srs_app_edge.cpp +++ b/trunk/src/app/srs_app_edge.cpp @@ -127,9 +127,12 @@ srs_error_t SrsEdgeRtmpUpstream::connect(SrsRequest* r, SrsLbRoundRobin* lb) // For RTMP client, we pass the vhost in tcUrl when connecting, // so we publish without vhost in stream. - if ((err = sdk->play(_srs_config->get_chunk_size(req->vhost), false)) != srs_success) { + string stream; + if ((err = sdk->play(_srs_config->get_chunk_size(req->vhost), false, &stream)) != srs_success) { return srs_error_wrap(err, "edge pull %s stream failed", url.c_str()); } + + srs_trace("edge-pull publish url %s, stream=%s%s as %s", url.c_str(), req->stream.c_str(), req->param.c_str(), stream.c_str()); return err; } @@ -509,7 +512,8 @@ srs_error_t SrsEdgeForwarder::start() // For RTMP client, we pass the vhost in tcUrl when connecting, // so we publish without vhost in stream. - if ((err = sdk->publish(_srs_config->get_chunk_size(req->vhost), false)) != srs_success) { + string stream; + if ((err = sdk->publish(_srs_config->get_chunk_size(req->vhost), false, &stream)) != srs_success) { return srs_error_wrap(err, "sdk publish"); } @@ -519,7 +523,8 @@ srs_error_t SrsEdgeForwarder::start() if ((err = trd->start()) != srs_success) { return srs_error_wrap(err, "coroutine"); } - srs_trace("edge-fwr publish url %s, stream=%s%s", url.c_str(), req->stream.c_str(), req->param.c_str()); + + srs_trace("edge-fwr publish url %s, stream=%s%s as %s", url.c_str(), req->stream.c_str(), req->param.c_str(), stream.c_str()); return err; } diff --git a/trunk/src/app/srs_app_forward.cpp b/trunk/src/app/srs_app_forward.cpp index 2890324a43..957ea7ccbd 100755 --- a/trunk/src/app/srs_app_forward.cpp +++ b/trunk/src/app/srs_app_forward.cpp @@ -225,7 +225,8 @@ srs_error_t SrsForwarder::do_cycle() // For RTMP client, we pass the vhost in tcUrl when connecting, // so we publish without vhost in stream. - if ((err = sdk->publish(_srs_config->get_chunk_size(req->vhost), false)) != srs_success) { + string stream; + if ((err = sdk->publish(_srs_config->get_chunk_size(req->vhost), false, &stream)) != srs_success) { return srs_error_wrap(err, "sdk publish"); } @@ -236,6 +237,8 @@ srs_error_t SrsForwarder::do_cycle() if ((err = forward()) != srs_success) { return srs_error_wrap(err, "forward"); } + + srs_trace("forward publish url %s, stream=%s%s as %s", url.c_str(), req->stream.c_str(), req->param.c_str(), stream.c_str()); return err; } diff --git a/trunk/src/protocol/srs_protocol_utility.cpp b/trunk/src/protocol/srs_protocol_utility.cpp index 03fcab3bf2..3937ddc296 100644 --- a/trunk/src/protocol/srs_protocol_utility.cpp +++ b/trunk/src/protocol/srs_protocol_utility.cpp @@ -186,20 +186,35 @@ string srs_generate_stream_with_query(string host, string vhost, string stream, string url = stream; string query = param; - if (with_vhost) { - // If no vhost in param, try to append one. - string guessVhost; - if (query.find("vhost=") == string::npos) { - if (vhost != SRS_CONSTS_RTMP_DEFAULT_VHOST) { - guessVhost = vhost; - } else if (!srs_is_ipv4(host)) { - guessVhost = host; - } + // If no vhost in param, try to append one. + string guessVhost; + if (query.find("vhost=") == string::npos) { + if (vhost != SRS_CONSTS_RTMP_DEFAULT_VHOST) { + guessVhost = vhost; + } else if (!srs_is_ipv4(host)) { + guessVhost = host; + } + } + + // Well, if vhost exists, always append in query string. + if (!guessVhost.empty() && query.find("vhost=") == string::npos) { + query += "&vhost=" + guessVhost; + } + + // If not pass in query, remove it. + if (!with_vhost) { + size_t pos = query.find("&vhost="); + if (pos == string::npos) { + pos = query.find("vhost="); + } + + size_t end = query.find("&", pos + 1); + if (end == string::npos) { + end = query.length(); } - // Well, if vhost exists, always append in query string. - if (!guessVhost.empty()) { - query += "&vhost=" + guessVhost; + if (pos != string::npos && end != string::npos && end > pos) { + query = query.substr(0, pos) + query.substr(end); } } diff --git a/trunk/src/service/srs_service_rtmp_conn.cpp b/trunk/src/service/srs_service_rtmp_conn.cpp index 28908cf62e..2a7c7c8bbc 100644 --- a/trunk/src/service/srs_service_rtmp_conn.cpp +++ b/trunk/src/service/srs_service_rtmp_conn.cpp @@ -147,12 +147,17 @@ srs_error_t SrsBasicRtmpClient::do_connect_app(string local_ip, bool debug) return err; } -srs_error_t SrsBasicRtmpClient::publish(int chunk_size, bool with_vhost) +srs_error_t SrsBasicRtmpClient::publish(int chunk_size, bool with_vhost, std::string* pstream) { srs_error_t err = srs_success; // Pass params in stream, @see https://github.com/ossrs/srs/issues/1031#issuecomment-409745733 string stream = srs_generate_stream_with_query(req->host, req->vhost, req->stream, req->param, with_vhost); + + // Return the generated stream. + if (pstream) { + *pstream = stream; + } // publish. if ((err = client->publish(stream, stream_id, chunk_size)) != srs_success) { @@ -162,12 +167,17 @@ srs_error_t SrsBasicRtmpClient::publish(int chunk_size, bool with_vhost) return err; } -srs_error_t SrsBasicRtmpClient::play(int chunk_size, bool with_vhost) +srs_error_t SrsBasicRtmpClient::play(int chunk_size, bool with_vhost, std::string* pstream) { srs_error_t err = srs_success; // Pass params in stream, @see https://github.com/ossrs/srs/issues/1031#issuecomment-409745733 string stream = srs_generate_stream_with_query(req->host, req->vhost, req->stream, req->param, with_vhost); + + // Return the generated stream. + if (pstream) { + *pstream = stream; + } if ((err = client->play(stream, stream_id, chunk_size)) != srs_success) { return srs_error_wrap(err, "connect with server failed, stream=%s, stream_id=%d", stream.c_str(), stream_id); diff --git a/trunk/src/service/srs_service_rtmp_conn.hpp b/trunk/src/service/srs_service_rtmp_conn.hpp index afa06e8474..316b22403f 100644 --- a/trunk/src/service/srs_service_rtmp_conn.hpp +++ b/trunk/src/service/srs_service_rtmp_conn.hpp @@ -74,8 +74,8 @@ class SrsBasicRtmpClient virtual srs_error_t connect_app(); virtual srs_error_t do_connect_app(std::string local_ip, bool debug); public: - virtual srs_error_t publish(int chunk_size, bool with_vhost = true); - virtual srs_error_t play(int chunk_size, bool with_vhost = true); + virtual srs_error_t publish(int chunk_size, bool with_vhost = true, std::string* pstream = NULL); + virtual srs_error_t play(int chunk_size, bool with_vhost = true, std::string* pstream = NULL); virtual void kbps_sample(const char* label, int64_t age); virtual void kbps_sample(const char* label, int64_t age, int msgs); virtual int sid();