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

fix #277, refine http server refer to go http-framework. 2.0.98

This commit is contained in:
winlin 2015-01-17 23:00:40 +08:00
parent 9bbbaad288
commit cbed2bbca4
9 changed files with 148 additions and 1116 deletions

View file

@ -37,6 +37,7 @@ using namespace std;
#include <srs_app_json.hpp>
#include <srs_kernel_utility.hpp>
#include <srs_protocol_buffer.hpp>
#include <srs_kernel_file.hpp>
#define SRS_DEFAULT_HTTP_PORT 80
@ -49,24 +50,7 @@ using namespace std;
#define SRS_CONSTS_HTTP_PUT HTTP_PUT
#define SRS_CONSTS_HTTP_DELETE HTTP_DELETE
bool srs_path_equals(const char* expect, const char* path, int nb_path)
{
int size = strlen(expect);
if (size != nb_path) {
return false;
}
bool equals = !memcmp(expect, path, size);
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;
}
#define SRS_HTTP_DEFAULT_PAGE "index.html"
int srs_go_http_response_json(ISrsGoHttpResponseWriter* w, string data)
{
@ -278,6 +262,82 @@ int SrsGoHttpNotFoundHandler::serve_http(ISrsGoHttpResponseWriter* w, SrsHttpMes
SRS_CONSTS_HTTP_NotFound, SRS_CONSTS_HTTP_NotFound_str);
}
SrsGoHttpFileServer::SrsGoHttpFileServer(string root_dir)
{
dir = root_dir;
}
SrsGoHttpFileServer::~SrsGoHttpFileServer()
{
}
int SrsGoHttpFileServer::serve_http(ISrsGoHttpResponseWriter* w, SrsHttpMessage* r)
{
int ret = ERROR_SUCCESS;
string upath = r->path();
// add default pages.
if (srs_string_ends_with(upath, "/")) {
upath += SRS_HTTP_DEFAULT_PAGE;
}
string fullpath = dir + "/" + upath;
// open the target file.
SrsFileReader fs;
if ((ret = fs.open(fullpath)) != ERROR_SUCCESS) {
srs_warn("open file %s failed, ret=%d", fullpath.c_str(), ret);
return ret;
}
int64_t length = fs.filesize();
w->header()->set_content_length(length);
if (srs_string_ends_with(fullpath, ".ts")) {
w->header()->set_content_type("video/MP2T");
} else if (srs_string_ends_with(fullpath, ".m3u8")) {
w->header()->set_content_type("application/x-mpegURL;charset=utf-8");
} else if (srs_string_ends_with(fullpath, ".flv")) {
w->header()->set_content_type("video/x-flv");
} else if (srs_string_ends_with(fullpath, ".xml")) {
w->header()->set_content_type("text/xml;charset=utf-8");
} else if (srs_string_ends_with(fullpath, ".js")) {
w->header()->set_content_type("text/javascript");
} else if (srs_string_ends_with(fullpath, ".json")) {
w->header()->set_content_type("application/json;charset=utf-8");
} else if (srs_string_ends_with(fullpath, ".swf")) {
w->header()->set_content_type("application/x-shockwave-flash");
} else if (srs_string_ends_with(fullpath, ".css")) {
w->header()->set_content_type("text/css;charset=utf-8");
} else if (srs_string_ends_with(fullpath, ".ico")) {
w->header()->set_content_type("image/x-icon");
} else {
w->header()->set_content_type("text/html;charset=utf-8");
}
// write body.
int64_t left = length;
char* buf = r->http_ts_send_buffer();
while (left > 0) {
ssize_t nread = -1;
if ((ret = fs.read(buf, __SRS_HTTP_TS_SEND_BUFFER_SIZE, &nread)) != ERROR_SUCCESS) {
srs_warn("read file %s failed, ret=%d", fullpath.c_str(), ret);
break;
}
left -= nread;
if ((ret = w->write(buf, nread)) != ERROR_SUCCESS) {
break;
}
}
return ret;
}
SrsGoHttpMuxEntry::SrsGoHttpMuxEntry()
{
explicit_match = false;
@ -565,473 +625,11 @@ int SrsGoHttpResponseWriter::send_header(char* data, int size)
return skt->write((void*)buf.c_str(), buf.length(), NULL);
}
SrsHttpHandlerMatch::SrsHttpHandlerMatch()
{
handler = NULL;
}
SrsHttpHandler::SrsHttpHandler()
{
}
SrsHttpHandler::~SrsHttpHandler()
{
std::vector<SrsHttpHandler*>::iterator it;
for (it = handlers.begin(); it != handlers.end(); ++it) {
SrsHttpHandler* handler = *it;
srs_freep(handler);
}
handlers.clear();
}
int SrsHttpHandler::initialize()
{
int ret = ERROR_SUCCESS;
return ret;
}
bool SrsHttpHandler::can_handle(const char* /*path*/, int /*length*/, const char** /*pchild*/)
{
return false;
}
int SrsHttpHandler::process_request(SrsStSocket* skt, SrsHttpMessage* req)
{
if (req->method() == SRS_CONSTS_HTTP_OPTIONS) {
req->set_requires_crossdomain(true);
return res_options(skt);
}
int status_code;
std::string reason_phrase;
if (!is_handler_valid(req, status_code, reason_phrase)) {
std::stringstream ss;
ss << __SRS_JOBJECT_START
<< __SRS_JFIELD_ERROR(ERROR_HTTP_HANDLER_INVALID) << __SRS_JFIELD_CONT
<< __SRS_JFIELD_ORG("data", __SRS_JOBJECT_START)
<< __SRS_JFIELD_ORG("status_code", status_code) << __SRS_JFIELD_CONT
<< __SRS_JFIELD_STR("reason_phrase", reason_phrase) << __SRS_JFIELD_CONT
<< __SRS_JFIELD_STR("url", req->url())
<< __SRS_JOBJECT_END
<< __SRS_JOBJECT_END;
return res_error(skt, req, status_code, reason_phrase, ss.str());
}
return do_process_request(skt, req);
}
bool SrsHttpHandler::is_handler_valid(SrsHttpMessage* req, int& status_code, string& reason_phrase)
{
if (!req->match()->unmatched_url.empty()) {
status_code = SRS_CONSTS_HTTP_NotFound;
reason_phrase = SRS_CONSTS_HTTP_NotFound_str;
return false;
}
return true;
}
int SrsHttpHandler::do_process_request(SrsStSocket* /*skt*/, SrsHttpMessage* /*req*/)
{
int ret = ERROR_SUCCESS;
return ret;
}
int SrsHttpHandler::response_error(SrsStSocket* skt, SrsHttpMessage* req, int code, string desc)
{
std::stringstream ss;
ss << __SRS_JOBJECT_START
<< __SRS_JFIELD_ERROR(code) << __SRS_JFIELD_CONT
<< __SRS_JFIELD_STR("desc", desc)
<< __SRS_JOBJECT_END;
return res_json(skt, req, ss.str());
}
int SrsHttpHandler::best_match(const char* path, int length, SrsHttpHandlerMatch** ppmatch)
{
int ret = ERROR_SUCCESS;
SrsHttpHandler* handler = NULL;
const char* match_start = NULL;
int match_length = 0;
for (;;) {
// ensure cur is not NULL.
// ensure p not NULL and has bytes to parse.
if (!path || length <= 0) {
break;
}
const char* p = NULL;
for (p = path + 1; p - path < length && *p != SRS_CONSTS_HTTP_PATH_SEP; p++) {
}
// whether the handler can handler the node.
const char* pchild = p;
if (!can_handle(path, p - path, &pchild)) {
break;
}
// save current handler, it's ok for current handler atleast.
handler = this;
match_start = path;
match_length = p - path;
// find the best matched child handler.
std::vector<SrsHttpHandler*>::iterator it;
for (it = handlers.begin(); it != handlers.end(); ++it) {
SrsHttpHandler* h = *it;
// matched, donot search more.
if (h->best_match(pchild, length - (pchild - path), ppmatch) == ERROR_SUCCESS) {
break;
}
}
// whatever, donot loop.
break;
}
// if already matched by child, return.
if (*ppmatch) {
return ret;
}
// not matched, error.
if (handler == NULL) {
ret = ERROR_HTTP_HANDLER_MATCH_URL;
return ret;
}
// matched by this handler.
*ppmatch = new SrsHttpHandlerMatch();
(*ppmatch)->handler = handler;
(*ppmatch)->matched_url.append(match_start, match_length);
int unmatch_length = length - match_length;
if (unmatch_length > 0) {
(*ppmatch)->unmatched_url.append(match_start + match_length, unmatch_length);
}
return ret;
}
SrsHttpHandler* SrsHttpHandler::res_status_line(stringstream& ss)
{
ss << "HTTP/1.1 200 OK " << __SRS_CRLF
<< "Server: "RTMP_SIG_SRS_KEY"/"RTMP_SIG_SRS_VERSION"" << __SRS_CRLF;
return this;
}
SrsHttpHandler* SrsHttpHandler::res_status_line_error(stringstream& ss, int code, string reason_phrase)
{
ss << "HTTP/1.1 " << code << " " << reason_phrase << __SRS_CRLF
<< "Server: SRS/"RTMP_SIG_SRS_VERSION"" << __SRS_CRLF;
return this;
}
SrsHttpHandler* SrsHttpHandler::res_content_type(stringstream& ss)
{
ss << "Content-Type: text/html;charset=utf-8" << __SRS_CRLF
<< "Allow: DELETE, GET, HEAD, OPTIONS, POST, PUT" << __SRS_CRLF;
return this;
}
SrsHttpHandler* SrsHttpHandler::res_content_type_xml(stringstream& ss)
{
ss << "Content-Type: text/xml;charset=utf-8" << __SRS_CRLF
<< "Allow: DELETE, GET, HEAD, OPTIONS, POST, PUT" << __SRS_CRLF;
return this;
}
SrsHttpHandler* SrsHttpHandler::res_content_type_javascript(stringstream& ss)
{
ss << "Content-Type: text/javascript" << __SRS_CRLF
<< "Allow: DELETE, GET, HEAD, OPTIONS, POST, PUT" << __SRS_CRLF;
return this;
}
SrsHttpHandler* SrsHttpHandler::res_content_type_swf(stringstream& ss)
{
ss << "Content-Type: application/x-shockwave-flash" << __SRS_CRLF
<< "Allow: DELETE, GET, HEAD, OPTIONS, POST, PUT" << __SRS_CRLF;
return this;
}
SrsHttpHandler* SrsHttpHandler::res_content_type_css(stringstream& ss)
{
ss << "Content-Type: text/css;charset=utf-8" << __SRS_CRLF
<< "Allow: DELETE, GET, HEAD, OPTIONS, POST, PUT" << __SRS_CRLF;
return this;
}
SrsHttpHandler* SrsHttpHandler::res_content_type_ico(stringstream& ss)
{
ss << "Content-Type: image/x-icon" << __SRS_CRLF
<< "Allow: DELETE, GET, HEAD, OPTIONS, POST, PUT" << __SRS_CRLF;
return this;
}
SrsHttpHandler* SrsHttpHandler::res_content_type_json(stringstream& ss)
{
ss << "Content-Type: application/json;charset=utf-8" << __SRS_CRLF
<< "Allow: DELETE, GET, HEAD, OPTIONS, POST, PUT" << __SRS_CRLF;
return this;
}
SrsHttpHandler* SrsHttpHandler::res_content_type_m3u8(stringstream& ss)
{
ss << "Content-Type: application/x-mpegURL;charset=utf-8" << __SRS_CRLF
<< "Allow: DELETE, GET, HEAD, OPTIONS, POST, PUT" << __SRS_CRLF;
return this;
}
SrsHttpHandler* SrsHttpHandler::res_content_type_mpegts(stringstream& ss)
{
ss << "Content-Type: video/MP2T" << __SRS_CRLF
<< "Allow: DELETE, GET, HEAD, OPTIONS, POST, PUT" << __SRS_CRLF;
return this;
}
SrsHttpHandler* SrsHttpHandler::res_content_type_flv(stringstream& ss)
{
ss << "Content-Type: video/x-flv" << __SRS_CRLF
<< "Allow: DELETE, GET, HEAD, OPTIONS, POST, PUT" << __SRS_CRLF;
return this;
}
SrsHttpHandler* SrsHttpHandler::res_content_length(stringstream& ss, int64_t length)
{
ss << "Content-Length: "<< length << __SRS_CRLF;
return this;
}
SrsHttpHandler* SrsHttpHandler::res_enable_crossdomain(stringstream& ss)
{
ss << "Access-Control-Allow-Origin: *" << __SRS_CRLF
<< "Access-Control-Allow-Methods: "
<< "GET, POST, HEAD, PUT, DELETE" << __SRS_CRLF
<< "Access-Control-Allow-Headers: "
<< "Cache-Control,X-Proxy-Authorization,X-Requested-With,Content-Type" << __SRS_CRLF;
return this;
}
SrsHttpHandler* SrsHttpHandler::res_header_eof(stringstream& ss)
{
ss << __SRS_CRLF;
return this;
}
SrsHttpHandler* SrsHttpHandler::res_body(stringstream& ss, string body)
{
ss << body;
return this;
}
int SrsHttpHandler::res_flush(SrsStSocket* skt, stringstream& ss)
{
return skt->write((void*)ss.str().c_str(), ss.str().length(), NULL);
}
int SrsHttpHandler::res_options(SrsStSocket* skt)
{
std::stringstream ss;
res_status_line(ss)->res_content_type(ss)
->res_content_length(ss, 0)->res_enable_crossdomain(ss)
->res_header_eof(ss);
return res_flush(skt, ss);
}
int SrsHttpHandler::res_text(SrsStSocket* skt, SrsHttpMessage* req, string body)
{
std::stringstream ss;
res_status_line(ss)->res_content_type(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_xml(SrsStSocket* skt, SrsHttpMessage* req, 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(SrsStSocket* skt, SrsHttpMessage* req, 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(SrsStSocket* skt, SrsHttpMessage* req, 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(SrsStSocket* skt, SrsHttpMessage* req, 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_ico(SrsStSocket* skt, SrsHttpMessage* req, string body)
{
std::stringstream ss;
res_status_line(ss)->res_content_type_ico(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(SrsStSocket* skt, SrsHttpMessage* req, string body)
{
std::stringstream ss;
res_status_line(ss)->res_content_type_m3u8(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_mpegts(SrsStSocket* skt, SrsHttpMessage* req, string body)
{
std::stringstream ss;
res_status_line(ss)->res_content_type_mpegts(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_json(SrsStSocket* skt, SrsHttpMessage* req, string json)
{
std::stringstream ss;
res_status_line(ss)->res_content_type_json(ss)
->res_content_length(ss, (int)json.length());
if (req->requires_crossdomain()) {
res_enable_crossdomain(ss);
}
res_header_eof(ss)
->res_body(ss, json);
return res_flush(skt, ss);
}
int SrsHttpHandler::res_error(SrsStSocket* skt, SrsHttpMessage* req, int code, string reason_phrase, string body)
{
std::stringstream ss;
res_status_line_error(ss, code, reason_phrase)->res_content_type_json(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);
}
#ifdef SRS_AUTO_HTTP_SERVER
SrsHttpHandler* SrsHttpHandler::create_http_stream()
{
return new SrsHttpRoot();
}
#endif
SrsHttpMessage::SrsHttpMessage()
{
_body = new SrsSimpleBuffer();
_state = SrsHttpParseStateInit;
_uri = new SrsHttpUri();
_match = NULL;
_requires_crossdomain = false;
_http_ts_send_buffer = new char[__SRS_HTTP_TS_SEND_BUFFER_SIZE];
}
@ -1039,7 +637,6 @@ SrsHttpMessage::~SrsHttpMessage()
{
srs_freep(_body);
srs_freep(_uri);
srs_freep(_match);
srs_freep(_http_ts_send_buffer);
}
@ -1204,16 +801,6 @@ int64_t SrsHttpMessage::content_length()
return _header.content_length;
}
SrsHttpHandlerMatch* SrsHttpMessage::match()
{
return _match;
}
bool SrsHttpMessage::requires_crossdomain()
{
return _requires_crossdomain;
}
void SrsHttpMessage::set_url(string url)
{
_url = url;
@ -1229,17 +816,6 @@ void SrsHttpMessage::set_header(http_parser* header)
memcpy(&_header, header, sizeof(http_parser));
}
void SrsHttpMessage::set_match(SrsHttpHandlerMatch* match)
{
srs_freep(_match);
_match = match;
}
void SrsHttpMessage::set_requires_crossdomain(bool requires_crossdomain)
{
_requires_crossdomain = requires_crossdomain;
}
void SrsHttpMessage::append_body(const char* body, int length)
{
_body->append(body, length);