winlin

for #277, support http vhost mount.

@@ -351,9 +351,12 @@ vhost http.srs.com { @@ -351,9 +351,12 @@ vhost http.srs.com {
351 # default: off 351 # default: off
352 enabled on; 352 enabled on;
353 # the virtual directory root for this vhost to mount at 353 # the virtual directory root for this vhost to mount at
354 - # for example, if mount to /hls, user access by http://server/hls  
355 - # default: /  
356 - mount /hls; 354 + # for example, if mount to [vhost]/hls, user access by http://[vhost]/hls
  355 + # the variables:
  356 + # [vhost] current vhost for http server.
  357 + # @remark the http of __defaultVhost__ will override the http_stream section.
  358 + # default: [vhost]/
  359 + mount [vhost]/hls;
357 # main dir of vhost, 360 # main dir of vhost,
358 # to delivery HTTP stream of this vhost. 361 # to delivery HTTP stream of this vhost.
359 # default: ./objs/nginx/html 362 # default: ./objs/nginx/html
@@ -8,10 +8,10 @@ http_stream { @@ -8,10 +8,10 @@ http_stream {
8 listen 8080; 8 listen 8080;
9 dir ./objs/nginx/html; 9 dir ./objs/nginx/html;
10 } 10 }
11 -vhost __defaultVhost__ { 11 +vhost ossrs.net {
12 http { 12 http {
13 enabled on; 13 enabled on;
14 - mount /default; 14 + mount [vhost]/;
15 dir ./objs/nginx/html; 15 dir ./objs/nginx/html;
16 } 16 }
17 } 17 }
@@ -287,8 +287,9 @@ int SrsGoHttpFileServer::serve_http(ISrsGoHttpResponseWriter* w, SrsHttpMessage* @@ -287,8 +287,9 @@ int SrsGoHttpFileServer::serve_http(ISrsGoHttpResponseWriter* w, SrsHttpMessage*
287 string fullpath = dir + "/"; 287 string fullpath = dir + "/";
288 288
289 srs_assert(entry); 289 srs_assert(entry);
290 - if (upath.length() > entry->pattern.length()) {  
291 - fullpath += upath.substr(entry->pattern.length()); 290 + size_t pos = entry->pattern.find("/");
  291 + if (upath.length() > entry->pattern.length() && pos != string::npos) {
  292 + fullpath += upath.substr(entry->pattern.length() - pos);
292 } else { 293 } else {
293 fullpath += upath; 294 fullpath += upath;
294 } 295 }
@@ -402,6 +403,7 @@ SrsGoHttpMuxEntry::~SrsGoHttpMuxEntry() @@ -402,6 +403,7 @@ SrsGoHttpMuxEntry::~SrsGoHttpMuxEntry()
402 403
403 SrsGoHttpServeMux::SrsGoHttpServeMux() 404 SrsGoHttpServeMux::SrsGoHttpServeMux()
404 { 405 {
  406 + hosts = false;
405 } 407 }
406 408
407 SrsGoHttpServeMux::~SrsGoHttpServeMux() 409 SrsGoHttpServeMux::~SrsGoHttpServeMux()
@@ -442,6 +444,10 @@ int SrsGoHttpServeMux::handle(std::string pattern, ISrsGoHttpHandler* handler) @@ -442,6 +444,10 @@ int SrsGoHttpServeMux::handle(std::string pattern, ISrsGoHttpHandler* handler)
442 } 444 }
443 } 445 }
444 446
  447 + if (pattern.at(0) != '/') {
  448 + hosts = true;
  449 + }
  450 +
445 if (true) { 451 if (true) {
446 SrsGoHttpMuxEntry* entry = new SrsGoHttpMuxEntry(); 452 SrsGoHttpMuxEntry* entry = new SrsGoHttpMuxEntry();
447 entry->explicit_match = true; 453 entry->explicit_match = true;
@@ -538,6 +544,11 @@ int SrsGoHttpServeMux::match(SrsHttpMessage* r, ISrsGoHttpHandler** ph) @@ -538,6 +544,11 @@ int SrsGoHttpServeMux::match(SrsHttpMessage* r, ISrsGoHttpHandler** ph)
538 544
539 std::string path = r->path(); 545 std::string path = r->path();
540 546
  547 + // Host-specific pattern takes precedence over generic ones
  548 + if (hosts) {
  549 + path = r->host() + path;
  550 + }
  551 +
541 int nb_matched = 0; 552 int nb_matched = 0;
542 ISrsGoHttpHandler* h = NULL; 553 ISrsGoHttpHandler* h = NULL;
543 554
@@ -700,7 +711,8 @@ int SrsHttpMessage::initialize() @@ -700,7 +711,8 @@ int SrsHttpMessage::initialize()
700 int ret = ERROR_SUCCESS; 711 int ret = ERROR_SUCCESS;
701 712
702 // parse uri to schema/server:port/path?query 713 // parse uri to schema/server:port/path?query
703 - if ((ret = _uri->initialize(_url)) != ERROR_SUCCESS) { 714 + std::string uri = "http://" + get_request_header("Host") + _url;
  715 + if ((ret = _uri->initialize(uri)) != ERROR_SUCCESS) {
704 return ret; 716 return ret;
705 } 717 }
706 718
@@ -822,7 +834,7 @@ string SrsHttpMessage::url() @@ -822,7 +834,7 @@ string SrsHttpMessage::url()
822 834
823 string SrsHttpMessage::host() 835 string SrsHttpMessage::host()
824 { 836 {
825 - return get_request_header("Host"); 837 + return _uri->get_host();
826 } 838 }
827 839
828 string SrsHttpMessage::path() 840 string SrsHttpMessage::path()
@@ -266,6 +266,8 @@ class SrsGoHttpServeMux @@ -266,6 +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 public: 271 public:
270 SrsGoHttpServeMux(); 272 SrsGoHttpServeMux();
271 virtual ~SrsGoHttpServeMux(); 273 virtual ~SrsGoHttpServeMux();
@@ -166,11 +166,15 @@ int SrsHttpServer::initialize() @@ -166,11 +166,15 @@ int SrsHttpServer::initialize()
166 std::string mount = _srs_config->get_vhost_http_mount(vhost); 166 std::string mount = _srs_config->get_vhost_http_mount(vhost);
167 std::string dir = _srs_config->get_vhost_http_dir(vhost); 167 std::string dir = _srs_config->get_vhost_http_dir(vhost);
168 168
  169 + // replace the vhost variable
  170 + mount = srs_string_replace(mount, "[vhost]", vhost);
  171 +
169 // the dir mount must always ends with "/" 172 // the dir mount must always ends with "/"
170 if (mount != "/" && mount.rfind("/") != mount.length() - 1) { 173 if (mount != "/" && mount.rfind("/") != mount.length() - 1) {
171 mount += "/"; 174 mount += "/";
172 } 175 }
173 176
  177 + // mount the http of vhost.
174 if ((ret = mux.handle(mount, new SrsVodStream(dir))) != ERROR_SUCCESS) { 178 if ((ret = mux.handle(mount, new SrsVodStream(dir))) != ERROR_SUCCESS) {
175 srs_error("http: mount dir=%s for vhost=%s failed. ret=%d", dir.c_str(), vhost.c_str(), ret); 179 srs_error("http: mount dir=%s for vhost=%s failed. ret=%d", dir.c_str(), vhost.c_str(), ret);
176 return ret; 180 return ret;