1
0
Fork 0
mirror of https://github.com/ossrs/srs.git synced 2025-03-09 15:49:59 +00:00

refine http server pages. change to 0.9.51

This commit is contained in:
winlin 2014-04-05 13:14:59 +08:00
parent 1c02f4551c
commit 4e3fe36ae2
24 changed files with 307 additions and 96 deletions

View file

@ -52,6 +52,13 @@ bool srs_path_equals(const char* expect, const char* path, int nb_path)
return equals;
}
bool srs_path_like(const char* expect, const char* path, int nb_path)
{
int size = strlen(expect);
bool equals = !strncmp(expect, path, srs_min(size, nb_path));
return equals;
}
SrsHttpHandlerMatch::SrsHttpHandlerMatch()
{
handler = NULL;
@ -217,6 +224,34 @@ SrsHttpHandler* SrsHttpHandler::res_content_type(std::stringstream& ss)
return this;
}
SrsHttpHandler* SrsHttpHandler::res_content_type_xml(std::stringstream& ss)
{
ss << "Content-Type: text/xml;charset=utf-8" << __CRLF
<< "Allow: DELETE, GET, HEAD, OPTIONS, POST, PUT" << __CRLF;
return this;
}
SrsHttpHandler* SrsHttpHandler::res_content_type_javascript(std::stringstream& ss)
{
ss << "Content-Type: text/javascript;charset=utf-8" << __CRLF
<< "Allow: DELETE, GET, HEAD, OPTIONS, POST, PUT" << __CRLF;
return this;
}
SrsHttpHandler* SrsHttpHandler::res_content_type_swf(std::stringstream& ss)
{
ss << "Content-Type: application/x-shockwave-flash;charset=utf-8" << __CRLF
<< "Allow: DELETE, GET, HEAD, OPTIONS, POST, PUT" << __CRLF;
return this;
}
SrsHttpHandler* SrsHttpHandler::res_content_type_css(std::stringstream& ss)
{
ss << "Content-Type: text/css;charset=utf-8" << __CRLF
<< "Allow: DELETE, GET, HEAD, OPTIONS, POST, PUT" << __CRLF;
return this;
}
SrsHttpHandler* SrsHttpHandler::res_content_type_json(std::stringstream& ss)
{
ss << "Content-Type: application/json;charset=utf-8" << __CRLF
@ -299,6 +334,74 @@ int SrsHttpHandler::res_text(SrsSocket* skt, SrsHttpMessage* req, std::string bo
return res_flush(skt, ss);
}
int SrsHttpHandler::res_xml(SrsSocket* skt, SrsHttpMessage* req, std::string body)
{
std::stringstream ss;
res_status_line(ss)->res_content_type_xml(ss)
->res_content_length(ss, (int)body.length());
if (req->requires_crossdomain()) {
res_enable_crossdomain(ss);
}
res_header_eof(ss)
->res_body(ss, body);
return res_flush(skt, ss);
}
int SrsHttpHandler::res_javascript(SrsSocket* skt, SrsHttpMessage* req, std::string body)
{
std::stringstream ss;
res_status_line(ss)->res_content_type_javascript(ss)
->res_content_length(ss, (int)body.length());
if (req->requires_crossdomain()) {
res_enable_crossdomain(ss);
}
res_header_eof(ss)
->res_body(ss, body);
return res_flush(skt, ss);
}
int SrsHttpHandler::res_swf(SrsSocket* skt, SrsHttpMessage* req, std::string body)
{
std::stringstream ss;
res_status_line(ss)->res_content_type_swf(ss)
->res_content_length(ss, (int)body.length());
if (req->requires_crossdomain()) {
res_enable_crossdomain(ss);
}
res_header_eof(ss)
->res_body(ss, body);
return res_flush(skt, ss);
}
int SrsHttpHandler::res_css(SrsSocket* skt, SrsHttpMessage* req, std::string body)
{
std::stringstream ss;
res_status_line(ss)->res_content_type_css(ss)
->res_content_length(ss, (int)body.length());
if (req->requires_crossdomain()) {
res_enable_crossdomain(ss);
}
res_header_eof(ss)
->res_body(ss, body);
return res_flush(skt, ss);
}
int SrsHttpHandler::res_m3u8(SrsSocket* skt, SrsHttpMessage* req, std::string body)
{
std::stringstream ss;

View file

@ -152,7 +152,12 @@ class SrsHttpHandler;
// compare the path.
// full compare, extractly match.
// used for api match.
extern bool srs_path_equals(const char* expect, const char* path, int nb_path);
// compare the path use like,
// used for http stream to match,
// if the path like the requires
extern bool srs_path_like(const char* expect, const char* path, int nb_path);
// state of message
enum SrsHttpParseState {
@ -228,6 +233,10 @@ public:
virtual SrsHttpHandler* res_status_line(std::stringstream& ss);
virtual SrsHttpHandler* res_status_line_error(std::stringstream& ss, int code, std::string reason_phrase);
virtual SrsHttpHandler* res_content_type(std::stringstream& ss);
virtual SrsHttpHandler* res_content_type_xml(std::stringstream& ss);
virtual SrsHttpHandler* res_content_type_javascript(std::stringstream& ss);
virtual SrsHttpHandler* res_content_type_swf(std::stringstream& ss);
virtual SrsHttpHandler* res_content_type_css(std::stringstream& ss);
virtual SrsHttpHandler* res_content_type_json(std::stringstream& ss);
virtual SrsHttpHandler* res_content_type_m3u8(std::stringstream& ss);
virtual SrsHttpHandler* res_content_type_mpegts(std::stringstream& ss);
@ -239,6 +248,10 @@ public:
public:
virtual int res_options(SrsSocket* skt);
virtual int res_text(SrsSocket* skt, SrsHttpMessage* req, std::string body);
virtual int res_xml(SrsSocket* skt, SrsHttpMessage* req, std::string body);
virtual int res_javascript(SrsSocket* skt, SrsHttpMessage* req, std::string body);
virtual int res_swf(SrsSocket* skt, SrsHttpMessage* req, std::string body);
virtual int res_css(SrsSocket* skt, SrsHttpMessage* req, std::string body);
virtual int res_m3u8(SrsSocket* skt, SrsHttpMessage* req, std::string body);
virtual int res_mpegts(SrsSocket* skt, SrsHttpMessage* req, std::string body);
virtual int res_json(SrsSocket* skt, SrsHttpMessage* req, std::string json);

View file

@ -40,6 +40,8 @@ using namespace std;
#include <srs_app_json.hpp>
#include <srs_app_config.hpp>
#define SRS_HTTP_DEFAULT_PAGE "index.html"
SrsHttpRoot::SrsHttpRoot()
{
// TODO: FIXME: support reload vhosts.
@ -88,14 +90,26 @@ int SrsHttpRoot::initialize()
return ret;
}
bool SrsHttpRoot::can_handle(const char* path, int length, const char** pchild)
int SrsHttpRoot::best_match(const char* path, int length, SrsHttpHandlerMatch** ppmatch)
{
// reset the child path to path,
// for child to reparse the path.
*pchild = path;
int ret = ERROR_SUCCESS;
// find the best matched child handler.
std::vector<SrsHttpHandler*>::iterator it;
for (it = handlers.begin(); it != handlers.end(); ++it) {
SrsHttpHandler* h = *it;
// search all child handlers.
h->best_match(path, length, ppmatch);
}
// never handle request for root.
return true;
// if already matched by child, return.
if (*ppmatch) {
return ret;
}
// not matched, error.
return ERROR_HTTP_HANDLER_MATCH_URL;
}
bool SrsHttpRoot::is_handler_valid(SrsHttpMessage* req, int& status_code, std::string& reason_phrase)
@ -125,19 +139,12 @@ SrsHttpVhost::~SrsHttpVhost()
bool SrsHttpVhost::can_handle(const char* path, int length, const char** /*pchild*/)
{
int min_match = srs_min(length, (int)_mount.length());
return srs_path_equals(_mount.c_str(), path, min_match);
return srs_path_like(_mount.c_str(), path, length);
}
bool SrsHttpVhost::is_handler_valid(SrsHttpMessage* req, int& status_code, std::string& reason_phrase)
{
std::string fullpath = _dir + "/" + req->match()->unmatched_url;
if (_mount == "/") {
fullpath = _dir + "/" + req->match()->matched_url;
if (!req->match()->unmatched_url.empty()) {
fullpath += "/" + req->match()->unmatched_url;
}
}
std::string fullpath = get_request_file(req);
if (::access(fullpath.c_str(), F_OK | R_OK) < 0) {
srs_warn("check file %s does not exists", fullpath.c_str());
@ -154,17 +161,7 @@ int SrsHttpVhost::do_process_request(SrsSocket* skt, SrsHttpMessage* req)
{
int ret = ERROR_SUCCESS;
std::string fullpath = _dir + "/" + req->match()->unmatched_url;
if (_mount == "/") {
fullpath = _dir + "/" + req->match()->matched_url;
if (!req->match()->unmatched_url.empty()) {
fullpath += "/" + req->match()->unmatched_url;
}
}
if (srs_string_ends_with(fullpath, "/")) {
fullpath += "index.html";
}
std::string fullpath = get_request_file(req);
int fd = ::open(fullpath.c_str(), O_RDONLY);
if (fd < 0) {
@ -194,6 +191,14 @@ int SrsHttpVhost::do_process_request(SrsSocket* skt, SrsHttpMessage* req)
return res_mpegts(skt, req, str);
} else if (srs_string_ends_with(fullpath, ".m3u8")) {
return res_m3u8(skt, req, str);
} else if (srs_string_ends_with(fullpath, ".xml")) {
return res_xml(skt, req, str);
} else if (srs_string_ends_with(fullpath, ".js")) {
return res_javascript(skt, req, str);
} else if (srs_string_ends_with(fullpath, ".swf")) {
return res_swf(skt, req, str);
} else if (srs_string_ends_with(fullpath, ".css")) {
return res_css(skt, req, str);
} else {
return res_text(skt, req, str);
}
@ -201,6 +206,31 @@ int SrsHttpVhost::do_process_request(SrsSocket* skt, SrsHttpMessage* req)
return ret;
}
string SrsHttpVhost::get_request_file(SrsHttpMessage* req)
{
std::string fullpath = _dir + "/";
// if root, directly use the matched url.
if (_mount == "/") {
// add the dir
fullpath += req->match()->matched_url;
// if file speicified, add the file.
if (!req->match()->unmatched_url.empty()) {
fullpath += "/" + req->match()->unmatched_url;
}
} else {
// virtual path, ignore the virutal path.
fullpath += req->match()->unmatched_url;
}
// add default pages.
if (srs_string_ends_with(fullpath, "/")) {
fullpath += SRS_HTTP_DEFAULT_PAGE;
}
return fullpath;
}
string SrsHttpVhost::vhost()
{
return _vhost;

View file

@ -49,7 +49,7 @@ public:
virtual ~SrsHttpRoot();
public:
virtual int initialize();
virtual bool can_handle(const char* path, int length, const char** pchild);
virtual int best_match(const char* path, int length, SrsHttpHandlerMatch** ppmatch);
protected:
virtual bool is_handler_valid(SrsHttpMessage* req, int& status_code, std::string& reason_phrase);
virtual int do_process_request(SrsSocket* skt, SrsHttpMessage* req);
@ -69,6 +69,8 @@ public:
protected:
virtual bool is_handler_valid(SrsHttpMessage* req, int& status_code, std::string& reason_phrase);
virtual int do_process_request(SrsSocket* skt, SrsHttpMessage* req);
private:
virtual std::string get_request_file(SrsHttpMessage* req);
public:
virtual std::string vhost();
virtual std::string mount();

View file

@ -239,6 +239,7 @@ int SrsRtmpConn::stream_service_cycle()
srs_error("identify client failed. ret=%d", ret);
return ret;
}
req->strip();
srs_trace("identify client success. type=%s, stream_name=%s",
srs_client_type_string(type).c_str(), req->stream.c_str());

View file

@ -63,6 +63,24 @@ string srs_string_trim_end(string str, string trim_chars)
return ret;
}
string srs_string_trim_start(string str, string trim_chars)
{
std::string ret = str;
for (int i = 0; i < (int)trim_chars.length(); i++) {
char ch = trim_chars.at(i);
while (!ret.empty() && ret.at(0) == ch) {
ret.erase(ret.begin());
// ok, matched, should reset the search
i = 0;
}
}
return ret;
}
string srs_string_remove(string str, string remove_chars)
{
std::string ret = str;

View file

@ -31,7 +31,7 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
// current release version
#define VERSION_MAJOR "0"
#define VERSION_MINOR "9"
#define VERSION_REVISION "50"
#define VERSION_REVISION "51"
#define RTMP_SIG_SRS_VERSION VERSION_MAJOR"."VERSION_MINOR"."VERSION_REVISION
// server info.
#define RTMP_SIG_SRS_KEY "srs"
@ -101,6 +101,8 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
extern std::string srs_string_replace(std::string str, std::string old_str, std::string new_str);
// trim char in trim_chars of str
extern std::string srs_string_trim_end(std::string str, std::string trim_chars);
// trim char in trim_chars of str
extern std::string srs_string_trim_start(std::string str, std::string trim_chars);
// remove char in remove_chars of str
extern std::string srs_string_remove(std::string str, std::string remove_chars);
// whether string end with

View file

@ -127,15 +127,8 @@ int SrsRequest::discovery_app()
app = url;
vhost = host;
srs_vhost_resolve(vhost, app);
// remove the unsupported chars in names.
vhost = srs_string_remove(vhost, "/ \n\r\t");
app = srs_string_remove(app, " \n\r\t");
stream = srs_string_remove(stream, " \n\r\t");
// remove end slash of app
app = srs_string_trim_end(app, "/");
stream = srs_string_trim_end(stream, "/");
strip();
return ret;
}
@ -153,6 +146,22 @@ string SrsRequest::get_stream_url()
return url;
}
void SrsRequest::strip()
{
// remove the unsupported chars in names.
vhost = srs_string_remove(vhost, "/ \n\r\t");
app = srs_string_remove(app, " \n\r\t");
stream = srs_string_remove(stream, " \n\r\t");
// remove end slash of app/stream
app = srs_string_trim_end(app, "/");
stream = srs_string_trim_end(stream, "/");
// remove start slash of app/stream
app = srs_string_trim_start(app, "/");
stream = srs_string_trim_start(stream, "/");
}
SrsResponse::SrsResponse()
{
stream_id = SRS_DEFAULT_SID;

View file

@ -82,6 +82,9 @@ public:
*/
virtual int discovery_app();
virtual std::string get_stream_url();
// strip url, user must strip when update the url.
virtual void strip();
};
/**