winlin

refine http server, support mount dir for vhost.

  1 +# the config for srs to serve as http server
  2 +# @see full.conf for detail config.
  3 +
  4 +listen 1935;
  5 +max_connections 1000;
  6 +http_stream {
  7 + enabled on;
  8 + listen 8080;
  9 + dir ./objs/nginx/html;
  10 +}
  11 +vhost __defaultVhost__ {
  12 + http {
  13 + enabled on;
  14 + mount /default;
  15 + dir ./objs/nginx/html;
  16 + }
  17 +}
@@ -226,6 +226,7 @@ ISrsGoHttpResponseWriter::~ISrsGoHttpResponseWriter() @@ -226,6 +226,7 @@ ISrsGoHttpResponseWriter::~ISrsGoHttpResponseWriter()
226 226
227 ISrsGoHttpHandler::ISrsGoHttpHandler() 227 ISrsGoHttpHandler::ISrsGoHttpHandler()
228 { 228 {
  229 + entry = NULL;
229 } 230 }
230 231
231 ISrsGoHttpHandler::~ISrsGoHttpHandler() 232 ISrsGoHttpHandler::~ISrsGoHttpHandler()
@@ -283,7 +284,16 @@ int SrsGoHttpFileServer::serve_http(ISrsGoHttpResponseWriter* w, SrsHttpMessage* @@ -283,7 +284,16 @@ int SrsGoHttpFileServer::serve_http(ISrsGoHttpResponseWriter* w, SrsHttpMessage*
283 upath += SRS_HTTP_DEFAULT_PAGE; 284 upath += SRS_HTTP_DEFAULT_PAGE;
284 } 285 }
285 286
286 - string fullpath = dir + "/" + upath; 287 + string fullpath = dir + "/";
  288 +
  289 + srs_assert(entry);
  290 + if (upath.length() > entry->pattern.length()) {
  291 + fullpath += upath.substr(entry->pattern.length());
  292 + } else {
  293 + fullpath += upath;
  294 + }
  295 + srs_trace("http match file=%s, pattern=%s, upath=%s",
  296 + fullpath.c_str(), entry->pattern.c_str(), upath.c_str());
287 297
288 if (srs_string_ends_with(fullpath, ".flv") || srs_string_ends_with(fullpath, ".fhv")) { 298 if (srs_string_ends_with(fullpath, ".flv") || srs_string_ends_with(fullpath, ".fhv")) {
289 std::string start = r->query_get("start"); 299 std::string start = r->query_get("start");
@@ -437,6 +447,7 @@ int SrsGoHttpServeMux::handle(std::string pattern, ISrsGoHttpHandler* handler) @@ -437,6 +447,7 @@ int SrsGoHttpServeMux::handle(std::string pattern, ISrsGoHttpHandler* handler)
437 entry->explicit_match = true; 447 entry->explicit_match = true;
438 entry->handler = handler; 448 entry->handler = handler;
439 entry->pattern = pattern; 449 entry->pattern = pattern;
  450 + entry->handler->entry = entry;
440 451
441 if (entries.find(pattern) != entries.end()) { 452 if (entries.find(pattern) != entries.end()) {
442 SrsGoHttpMuxEntry* exists = entries[pattern]; 453 SrsGoHttpMuxEntry* exists = entries[pattern];
@@ -448,7 +459,7 @@ int SrsGoHttpServeMux::handle(std::string pattern, ISrsGoHttpHandler* handler) @@ -448,7 +459,7 @@ int SrsGoHttpServeMux::handle(std::string pattern, ISrsGoHttpHandler* handler)
448 // Helpful behavior: 459 // Helpful behavior:
449 // If pattern is /tree/, insert an implicit permanent redirect for /tree. 460 // If pattern is /tree/, insert an implicit permanent redirect for /tree.
450 // It can be overridden by an explicit registration. 461 // It can be overridden by an explicit registration.
451 - if (!pattern.empty() && pattern.at(pattern.length() - 1) == '/') { 462 + if (pattern != "/" && !pattern.empty() && pattern.at(pattern.length() - 1) == '/') {
452 std::string rpattern = pattern.substr(0, pattern.length() - 1); 463 std::string rpattern = pattern.substr(0, pattern.length() - 1);
453 SrsGoHttpMuxEntry* entry = NULL; 464 SrsGoHttpMuxEntry* entry = NULL;
454 465
@@ -468,6 +479,7 @@ int SrsGoHttpServeMux::handle(std::string pattern, ISrsGoHttpHandler* handler) @@ -468,6 +479,7 @@ int SrsGoHttpServeMux::handle(std::string pattern, ISrsGoHttpHandler* handler)
468 entry->explicit_match = false; 479 entry->explicit_match = false;
469 entry->handler = new SrsGoHttpRedirectHandler(pattern, SRS_CONSTS_HTTP_MovedPermanently); 480 entry->handler = new SrsGoHttpRedirectHandler(pattern, SRS_CONSTS_HTTP_MovedPermanently);
470 entry->pattern = pattern; 481 entry->pattern = pattern;
  482 + entry->handler->entry = entry;
471 483
472 entries[rpattern] = entry; 484 entries[rpattern] = entry;
473 } 485 }
@@ -46,6 +46,7 @@ class SrsHttpUri; @@ -46,6 +46,7 @@ class SrsHttpUri;
46 class SrsHttpMessage; 46 class SrsHttpMessage;
47 class SrsFileReader; 47 class SrsFileReader;
48 class SrsSimpleBuffer; 48 class SrsSimpleBuffer;
  49 +class SrsGoHttpMuxEntry;
49 class ISrsGoHttpResponseWriter; 50 class ISrsGoHttpResponseWriter;
50 51
51 // http specification 52 // http specification
@@ -158,6 +159,8 @@ public: @@ -158,6 +159,8 @@ public:
158 class ISrsGoHttpHandler 159 class ISrsGoHttpHandler
159 { 160 {
160 public: 161 public:
  162 + SrsGoHttpMuxEntry* entry;
  163 +public:
161 ISrsGoHttpHandler(); 164 ISrsGoHttpHandler();
162 virtual ~ISrsGoHttpHandler(); 165 virtual ~ISrsGoHttpHandler();
163 public: 166 public:
@@ -166,6 +166,11 @@ int SrsHttpServer::initialize() @@ -166,6 +166,11 @@ 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 + // the dir mount must always ends with "/"
  170 + if (mount != "/" && mount.rfind("/") != mount.length() - 1) {
  171 + mount += "/";
  172 + }
  173 +
169 if ((ret = mux.handle(mount, new SrsVodStream(dir))) != ERROR_SUCCESS) { 174 if ((ret = mux.handle(mount, new SrsVodStream(dir))) != ERROR_SUCCESS) {
170 srs_error("http: mount dir=%s for vhost=%s failed. ret=%d", dir.c_str(), vhost.c_str(), ret); 175 srs_error("http: mount dir=%s for vhost=%s failed. ret=%d", dir.c_str(), vhost.c_str(), ret);
171 return ret; 176 return ret;
@@ -173,6 +178,7 @@ int SrsHttpServer::initialize() @@ -173,6 +178,7 @@ int SrsHttpServer::initialize()
173 178
174 if (mount == "/") { 179 if (mount == "/") {
175 default_root_exists = true; 180 default_root_exists = true;
  181 + srs_warn("http: root mount to %s", dir.c_str());
176 } 182 }
177 } 183 }
178 184
@@ -183,6 +189,7 @@ int SrsHttpServer::initialize() @@ -183,6 +189,7 @@ int SrsHttpServer::initialize()
183 srs_error("http: mount root dir=%s failed. ret=%d", dir.c_str(), ret); 189 srs_error("http: mount root dir=%s failed. ret=%d", dir.c_str(), ret);
184 return ret; 190 return ret;
185 } 191 }
  192 + srs_trace("http: root mount to %s", dir.c_str());
186 } 193 }
187 194
188 return ret; 195 return ret;