From 216a23f70903cc624071d692ade6bbcfaf777869 Mon Sep 17 00:00:00 2001 From: winlin Date: Tue, 17 Dec 2019 21:24:24 +0800 Subject: [PATCH] Fix the http implicit handler bug --- trunk/src/protocol/srs_http_stack.cpp | 9 +++------ trunk/src/utest/srs_utest_http.cpp | 19 +++++++++++++++++++ 2 files changed, 22 insertions(+), 6 deletions(-) diff --git a/trunk/src/protocol/srs_http_stack.cpp b/trunk/src/protocol/srs_http_stack.cpp index 63a70b8d7..e2d1d1e57 100644 --- a/trunk/src/protocol/srs_http_stack.cpp +++ b/trunk/src/protocol/srs_http_stack.cpp @@ -667,16 +667,13 @@ srs_error_t SrsHttpServeMux::handle(std::string pattern, ISrsHttpHandler* handle std::string rpattern = pattern.substr(0, pattern.length() - 1); SrsHttpMuxEntry* entry = NULL; - // free the exists not explicit entry + // free the exists implicit entry if (entries.find(rpattern) != entries.end()) { - SrsHttpMuxEntry* exists = entries[rpattern]; - if (!exists->explicit_match) { - entry = exists; - } + entry = entries[rpattern]; } // create implicit redirect. - if (!entry || entry->explicit_match) { + if (!entry || !entry->explicit_match) { srs_freep(entry); entry = new SrsHttpMuxEntry(); diff --git a/trunk/src/utest/srs_utest_http.cpp b/trunk/src/utest/srs_utest_http.cpp index b43bdaaac..c1d49fcc4 100644 --- a/trunk/src/utest/srs_utest_http.cpp +++ b/trunk/src/utest/srs_utest_http.cpp @@ -381,6 +381,25 @@ VOID TEST(ProtocolHTTPTest, HTTPServerMuxerImplicitHandler) { srs_error_t err; + // Implicit handler. + if (true) { + SrsHttpServeMux s; + HELPER_ASSERT_SUCCESS(s.initialize()); + + MockHttpHandler* h1 = new MockHttpHandler("Done"); + HELPER_ASSERT_SUCCESS(s.handle("/api", h1)); + + MockHttpHandler* h0 = new MockHttpHandler("Hello, world!"); + HELPER_ASSERT_SUCCESS(s.handle("/api/", h0)); + + MockResponseWriter w; + SrsHttpMessage r(NULL, NULL); + HELPER_ASSERT_SUCCESS(r.set_url("/api", false)); + + HELPER_ASSERT_SUCCESS(s.serve_http(&w, &r)); + __MOCK_HTTP_EXPECT_STREQ(200, "Done", w); + } + // Fail if explicit handler exists. if (true) { SrsHttpServeMux s;