winlin

for bug #277, refine code, support default http vhost.

@@ -286,6 +286,7 @@ int SrsGoHttpFileServer::serve_http(ISrsGoHttpResponseWriter* w, SrsHttpMessage* @@ -286,6 +286,7 @@ int SrsGoHttpFileServer::serve_http(ISrsGoHttpResponseWriter* w, SrsHttpMessage*
286 286
287 string fullpath = dir + "/"; 287 string fullpath = dir + "/";
288 288
  289 + // remove the virtual directory.
289 srs_assert(entry); 290 srs_assert(entry);
290 size_t pos = entry->pattern.find("/"); 291 size_t pos = entry->pattern.find("/");
291 if (upath.length() > entry->pattern.length() && pos != string::npos) { 292 if (upath.length() > entry->pattern.length() && pos != string::npos) {
@@ -296,6 +297,7 @@ int SrsGoHttpFileServer::serve_http(ISrsGoHttpResponseWriter* w, SrsHttpMessage* @@ -296,6 +297,7 @@ int SrsGoHttpFileServer::serve_http(ISrsGoHttpResponseWriter* w, SrsHttpMessage*
296 srs_trace("http match file=%s, pattern=%s, upath=%s", 297 srs_trace("http match file=%s, pattern=%s, upath=%s",
297 fullpath.c_str(), entry->pattern.c_str(), upath.c_str()); 298 fullpath.c_str(), entry->pattern.c_str(), upath.c_str());
298 299
  300 + // handle file extension.
299 if (srs_string_ends_with(fullpath, ".flv") || srs_string_ends_with(fullpath, ".fhv")) { 301 if (srs_string_ends_with(fullpath, ".flv") || srs_string_ends_with(fullpath, ".fhv")) {
300 std::string start = r->query_get("start"); 302 std::string start = r->query_get("start");
301 if (start.empty()) { 303 if (start.empty()) {
@@ -403,7 +405,6 @@ SrsGoHttpMuxEntry::~SrsGoHttpMuxEntry() @@ -403,7 +405,6 @@ SrsGoHttpMuxEntry::~SrsGoHttpMuxEntry()
403 405
404 SrsGoHttpServeMux::SrsGoHttpServeMux() 406 SrsGoHttpServeMux::SrsGoHttpServeMux()
405 { 407 {
406 - hosts = false;  
407 } 408 }
408 409
409 SrsGoHttpServeMux::~SrsGoHttpServeMux() 410 SrsGoHttpServeMux::~SrsGoHttpServeMux()
@@ -414,6 +415,8 @@ SrsGoHttpServeMux::~SrsGoHttpServeMux() @@ -414,6 +415,8 @@ SrsGoHttpServeMux::~SrsGoHttpServeMux()
414 srs_freep(entry); 415 srs_freep(entry);
415 } 416 }
416 entries.clear(); 417 entries.clear();
  418 +
  419 + vhosts.clear();
417 } 420 }
418 421
419 int SrsGoHttpServeMux::initialize() 422 int SrsGoHttpServeMux::initialize()
@@ -444,8 +447,12 @@ int SrsGoHttpServeMux::handle(std::string pattern, ISrsGoHttpHandler* handler) @@ -444,8 +447,12 @@ int SrsGoHttpServeMux::handle(std::string pattern, ISrsGoHttpHandler* handler)
444 } 447 }
445 } 448 }
446 449
  450 + std::string vhost = pattern;
447 if (pattern.at(0) != '/') { 451 if (pattern.at(0) != '/') {
448 - hosts = true; 452 + if (pattern.find("/") != string::npos) {
  453 + vhost = pattern.substr(0, pattern.find("/"));
  454 + }
  455 + vhosts[vhost] = handler;
449 } 456 }
450 457
451 if (true) { 458 if (true) {
@@ -545,7 +552,7 @@ int SrsGoHttpServeMux::match(SrsHttpMessage* r, ISrsGoHttpHandler** ph) @@ -545,7 +552,7 @@ int SrsGoHttpServeMux::match(SrsHttpMessage* r, ISrsGoHttpHandler** ph)
545 std::string path = r->path(); 552 std::string path = r->path();
546 553
547 // Host-specific pattern takes precedence over generic ones 554 // Host-specific pattern takes precedence over generic ones
548 - if (hosts) { 555 + if (!vhosts.empty() && vhosts.find(r->host()) != vhosts.end()) {
549 path = r->host() + path; 556 path = r->host() + path;
550 } 557 }
551 558
@@ -266,8 +266,8 @@ class SrsGoHttpServeMux @@ -266,8 +266,8 @@ class SrsGoHttpServeMux
266 { 266 {
267 private: 267 private:
268 std::map<std::string, SrsGoHttpMuxEntry*> entries; 268 std::map<std::string, SrsGoHttpMuxEntry*> entries;
269 - // whether any patterns contain hostnames  
270 - bool hosts; 269 + // the vhost handler.
  270 + std::map<std::string, ISrsGoHttpHandler*> vhosts;
271 public: 271 public:
272 SrsGoHttpServeMux(); 272 SrsGoHttpServeMux();
273 virtual ~SrsGoHttpServeMux(); 273 virtual ~SrsGoHttpServeMux();