From 398b212bfa7a94917c0bd3bdf01bb49ee0d60efe Mon Sep 17 00:00:00 2001 From: winlin Date: Sun, 18 Jan 2015 17:01:39 +0800 Subject: [PATCH] for bug #277, refine code, support default http vhost. --- trunk/src/app/srs_app_http.cpp | 13 ++++++++++--- trunk/src/app/srs_app_http.hpp | 4 ++-- 2 files changed, 12 insertions(+), 5 deletions(-) diff --git a/trunk/src/app/srs_app_http.cpp b/trunk/src/app/srs_app_http.cpp index 4c76d3359..c5014ce0d 100644 --- a/trunk/src/app/srs_app_http.cpp +++ b/trunk/src/app/srs_app_http.cpp @@ -286,6 +286,7 @@ int SrsGoHttpFileServer::serve_http(ISrsGoHttpResponseWriter* w, SrsHttpMessage* string fullpath = dir + "/"; + // remove the virtual directory. srs_assert(entry); size_t pos = entry->pattern.find("/"); if (upath.length() > entry->pattern.length() && pos != string::npos) { @@ -296,6 +297,7 @@ int SrsGoHttpFileServer::serve_http(ISrsGoHttpResponseWriter* w, SrsHttpMessage* srs_trace("http match file=%s, pattern=%s, upath=%s", fullpath.c_str(), entry->pattern.c_str(), upath.c_str()); + // handle file extension. if (srs_string_ends_with(fullpath, ".flv") || srs_string_ends_with(fullpath, ".fhv")) { std::string start = r->query_get("start"); if (start.empty()) { @@ -403,7 +405,6 @@ SrsGoHttpMuxEntry::~SrsGoHttpMuxEntry() SrsGoHttpServeMux::SrsGoHttpServeMux() { - hosts = false; } SrsGoHttpServeMux::~SrsGoHttpServeMux() @@ -414,6 +415,8 @@ SrsGoHttpServeMux::~SrsGoHttpServeMux() srs_freep(entry); } entries.clear(); + + vhosts.clear(); } int SrsGoHttpServeMux::initialize() @@ -444,8 +447,12 @@ int SrsGoHttpServeMux::handle(std::string pattern, ISrsGoHttpHandler* handler) } } + std::string vhost = pattern; if (pattern.at(0) != '/') { - hosts = true; + if (pattern.find("/") != string::npos) { + vhost = pattern.substr(0, pattern.find("/")); + } + vhosts[vhost] = handler; } if (true) { @@ -545,7 +552,7 @@ int SrsGoHttpServeMux::match(SrsHttpMessage* r, ISrsGoHttpHandler** ph) std::string path = r->path(); // Host-specific pattern takes precedence over generic ones - if (hosts) { + if (!vhosts.empty() && vhosts.find(r->host()) != vhosts.end()) { path = r->host() + path; } diff --git a/trunk/src/app/srs_app_http.hpp b/trunk/src/app/srs_app_http.hpp index 63b7ba26c..df8906834 100644 --- a/trunk/src/app/srs_app_http.hpp +++ b/trunk/src/app/srs_app_http.hpp @@ -266,8 +266,8 @@ class SrsGoHttpServeMux { private: std::map entries; - // whether any patterns contain hostnames - bool hosts; + // the vhost handler. + std::map vhosts; public: SrsGoHttpServeMux(); virtual ~SrsGoHttpServeMux();