1
0
Fork 0
mirror of https://github.com/ossrs/srs.git synced 2025-02-12 11:21:52 +00:00

Refactor http static file server path resolving.

This commit is contained in:
winlin 2019-12-16 20:07:06 +08:00
parent fa362607b2
commit 97f2c5bf0c
3 changed files with 28 additions and 11 deletions

View file

@ -304,23 +304,32 @@ srs_error_t SrsHttpNotFoundHandler::serve_http(ISrsHttpResponseWriter* w, ISrsHt
return srs_go_http_error(w, SRS_CONSTS_HTTP_NotFound);
}
string srs_http_fs_fullpath(string dir, string upath, string pattern)
string srs_http_fs_fullpath(string dir, string pattern, string upath)
{
// add default pages.
if (srs_string_ends_with(upath, "/")) {
upath += SRS_HTTP_DEFAULT_PAGE;
}
string fullpath = dir + "/";
// remove the virtual directory.
// Remove the virtual directory.
// For example:
// pattern=/api, the virtual directory is api, upath=/api/index.html, fullpath={dir}/index.html
// pattern=/api, the virtual directory is api, upath=/api/views/index.html, fullpath={dir}/views/index.html
// The vhost prefix is ignored, for example:
// pattern=ossrs.net/api, the vhost is ossrs.net, the pattern equals to /api under this vhost,
// so the virtual directory is also api
size_t pos = pattern.find("/");
string filename = upath;
if (upath.length() > pattern.length() && pos != string::npos) {
fullpath += upath.substr(pattern.length() - pos);
} else {
fullpath += upath;
filename = upath.substr(pattern.length() - pos);
}
string fullpath = srs_string_trim_end(dir, "/");
if (!srs_string_starts_with(filename, "/")) {
fullpath += "/";
}
fullpath += filename;
return fullpath;
}
@ -354,7 +363,7 @@ srs_error_t SrsHttpFileServer::serve_http(ISrsHttpResponseWriter* w, ISrsHttpMes
srs_assert(entry);
string upath = r->path();
string fullpath = srs_http_fs_fullpath(dir, upath, entry->pattern);
string fullpath = srs_http_fs_fullpath(dir, entry->pattern, upath);
// stat current dir, if exists, return error.
if (!_srs_path_exists(fullpath)) {

View file

@ -281,7 +281,7 @@ public:
typedef bool (*_pfn_srs_path_exists)(std::string path);
// Build the file path from request r.
extern std::string srs_http_fs_fullpath(std::string dir, std::string upath, std::string pattern);
extern std::string srs_http_fs_fullpath(std::string dir, std::string pattern, std::string upath);
// FileServer returns a handler that serves HTTP requests
// with the contents of the file system rooted at root.

View file

@ -185,7 +185,7 @@ public:
}
};
bool _mock_srs_path_exists(std::string path)
bool _mock_srs_path_exists(std::string /*path*/)
{
return true;
}
@ -195,7 +195,15 @@ VOID TEST(ProtocolHTTPTest, BasicHandlers)
srs_error_t err;
if (true) {
EXPECT_STREQ("/tmp/index.html", srs_http_fs_fullpath("/tmp", "/tmp/index.html", "/").c_str());
EXPECT_STREQ("/tmp/index.html", srs_http_fs_fullpath("/tmp", "/", "/").c_str());
EXPECT_STREQ("/tmp/index.html", srs_http_fs_fullpath("/tmp", "/", "/index.html").c_str());
EXPECT_STREQ("/tmp/index.html", srs_http_fs_fullpath("/tmp/", "/", "/index.html").c_str());
EXPECT_STREQ("/tmp/ndex.html", srs_http_fs_fullpath("/tmp/", "//", "/index.html").c_str());
EXPECT_STREQ("/tmp/index.html", srs_http_fs_fullpath("/tmp/", "/api", "/api/index.html").c_str());
EXPECT_STREQ("/tmp/index.html", srs_http_fs_fullpath("/tmp/", "ossrs.net/api", "/api/index.html").c_str());
EXPECT_STREQ("/tmp/views/index.html", srs_http_fs_fullpath("/tmp/", "/api", "/api/views/index.html").c_str());
EXPECT_STREQ("/tmp/index.html", srs_http_fs_fullpath("/tmp/", "/api/", "/api/index.html").c_str());
EXPECT_STREQ("/tmp/ndex.html", srs_http_fs_fullpath("/tmp/", "/api//", "/api/index.html").c_str());
}
if (true) {