implements the http stream module.

pull/133/head
winlin 11 years ago
parent 00eda0d7b2
commit ef26e77560

@ -1,335 +1,303 @@
/* /*
The MIT License (MIT) The MIT License (MIT)
Copyright (c) 2013-2014 winlin Copyright (c) 2013-2014 winlin
Permission is hereby granted, free of charge, to any person obtaining a copy of Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software is furnished to do so, the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions: subject to the following conditions:
The above copyright notice and this permission notice shall be included in all The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software. copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/ */
#include <srs_app_http_conn.hpp> #include <srs_app_http_conn.hpp>
#ifdef SRS_HTTP_SERVER #ifdef SRS_HTTP_SERVER
#include <sstream> #include <sstream>
using namespace std; using namespace std;
#include <sys/types.h> #include <sys/types.h>
#include <sys/stat.h> #include <sys/stat.h>
#include <fcntl.h> #include <fcntl.h>
#include <srs_kernel_log.hpp> #include <srs_kernel_log.hpp>
#include <srs_kernel_error.hpp> #include <srs_kernel_error.hpp>
#include <srs_app_socket.hpp> #include <srs_app_socket.hpp>
#include <srs_app_http.hpp> #include <srs_app_http.hpp>
#include <srs_core_autofree.hpp> #include <srs_core_autofree.hpp>
#include <srs_app_json.hpp> #include <srs_app_json.hpp>
#include <srs_app_config.hpp> #include <srs_app_config.hpp>
SrsHttpRoot::SrsHttpRoot() SrsHttpRoot::SrsHttpRoot()
{ {
// TODO: FIXME: support reload vhosts. // TODO: FIXME: support reload vhosts.
} }
SrsHttpRoot::~SrsHttpRoot() SrsHttpRoot::~SrsHttpRoot()
{ {
} }
int SrsHttpRoot::initialize() int SrsHttpRoot::initialize()
{ {
int ret = ERROR_SUCCESS; int ret = ERROR_SUCCESS;
// add root // add root
handlers.push_back(new SrsHttpVhost("__http__", "/", _srs_config->get_http_stream_dir())); handlers.push_back(new SrsHttpVhost("__http__", "/", _srs_config->get_http_stream_dir()));
// add other virtual path // add other virtual path
SrsConfDirective* root = _srs_config->get_root(); SrsConfDirective* root = _srs_config->get_root();
for (int i = 0; i < (int)root->directives.size(); i++) { for (int i = 0; i < (int)root->directives.size(); i++) {
SrsConfDirective* conf = root->at(i); SrsConfDirective* conf = root->at(i);
if (!conf->is_vhost()) { if (!conf->is_vhost()) {
continue; continue;
} }
std::string vhost = conf->arg0(); std::string vhost = conf->arg0();
if (!_srs_config->get_vhost_http_enabled(vhost)) { if (!_srs_config->get_vhost_http_enabled(vhost)) {
continue; continue;
} }
std::string mount = _srs_config->get_vhost_http_mount(vhost); std::string mount = _srs_config->get_vhost_http_mount(vhost);
std::string dir = _srs_config->get_vhost_http_dir(vhost); std::string dir = _srs_config->get_vhost_http_dir(vhost);
handlers.push_back(new SrsHttpVhost(vhost, mount, dir)); handlers.push_back(new SrsHttpVhost(vhost, mount, dir));
} }
return ret; return ret;
} }
bool SrsHttpRoot::can_handle(const char* path, int length, const char** pchild) bool SrsHttpRoot::can_handle(const char* path, int length, const char** pchild)
{ {
// reset the child path to path, // reset the child path to path,
// for child to reparse the path. // for child to reparse the path.
*pchild = path; *pchild = path;
// only compare the first char. // never handle request for root.
return srs_path_equals("/", path, 1); return false;
} }
bool SrsHttpRoot::is_handler_valid(SrsHttpMessage* req, int& status_code, std::string& reason_phrase) bool SrsHttpRoot::is_handler_valid(SrsHttpMessage* req, int& status_code, std::string& reason_phrase)
{ {
if (!SrsHttpHandler::is_handler_valid(req, status_code, reason_phrase)) { status_code = HTTP_InternalServerError;
return false; reason_phrase = HTTP_InternalServerError_str;
} return false;
}
if (req->match()->matched_url.length() != 1) {
status_code = HTTP_NotFound; int SrsHttpRoot::do_process_request(SrsSocket* skt, SrsHttpMessage* req)
reason_phrase = HTTP_NotFound_str; {
return false; int ret = ERROR_SUCCESS;
} return ret;
}
return true;
} SrsHttpVhost::SrsHttpVhost(std::string vhost, std::string mount, std::string dir)
{
int SrsHttpRoot::do_process_request(SrsSocket* skt, SrsHttpMessage* req) _vhost = vhost;
{ _mount = mount;
std::stringstream ss; _dir = dir;
}
ss << JOBJECT_START
<< JFIELD_ERROR(ERROR_SUCCESS) << JFIELD_CONT SrsHttpVhost::~SrsHttpVhost()
<< JFIELD_ORG("urls", JOBJECT_START); {
}
vector<SrsHttpHandler*>::iterator it;
for (it = handlers.begin(); it != handlers.end(); ++it) { bool SrsHttpVhost::can_handle(const char* path, int length, const char** /*pchild*/)
SrsHttpVhost* handler = dynamic_cast<SrsHttpVhost*>(*it); {
srs_assert(handler); int min_match = srs_min(length, (int)_mount.length());
return srs_path_equals(_mount.c_str(), path, min_match);
ss << JFIELD_ORG(handler->mount(), JOBJECT_START) }
<< JFIELD_STR("mount", handler->mount()) << JFIELD_CONT
<< JFIELD_STR("vhost", handler->vhost()) << JFIELD_CONT bool SrsHttpVhost::is_handler_valid(SrsHttpMessage* req, int& status_code, std::string& reason_phrase)
<< JFIELD_STR("dir", handler->dir()) {
<< JOBJECT_END; std::string fullpath = _dir + "/" + req->match()->unmatched_url;
if (req->match()->unmatched_url.empty()) {
if (it + 1 != handlers.end()) { fullpath += req->match()->matched_url;
ss << JFIELD_CONT; }
}
} if (::access(fullpath.c_str(), F_OK | R_OK) < 0) {
srs_warn("check file %s does not exists", fullpath.c_str());
ss << JOBJECT_END
<< JOBJECT_END; status_code = HTTP_NotFound;
reason_phrase = HTTP_NotFound_str;
return res_json(skt, req, ss.str()); return false;
} }
SrsHttpVhost::SrsHttpVhost(std::string vhost, std::string mount, std::string dir) return true;
{ }
_vhost = vhost;
_mount = mount; int SrsHttpVhost::do_process_request(SrsSocket* skt, SrsHttpMessage* req)
_dir = dir; {
} int ret = ERROR_SUCCESS;
SrsHttpVhost::~SrsHttpVhost() std::string fullpath = _dir + "/" + req->match()->unmatched_url;
{ if (req->match()->unmatched_url.empty()) {
} fullpath += req->match()->matched_url;
}
bool SrsHttpVhost::can_handle(const char* path, int length, const char** /*pchild*/)
{ if (srs_string_ends_with(fullpath, "/")) {
int min_match = srs_min(length, (int)_mount.length()); fullpath += "index.html";
return srs_path_equals(_mount.c_str(), path, min_match); }
}
int fd = ::open(fullpath.c_str(), O_RDONLY);
bool SrsHttpVhost::is_handler_valid(SrsHttpMessage* req, int& status_code, std::string& reason_phrase) if (fd < 0) {
{ ret = ERROR_HTTP_OPEN_FILE;
std::string fullpath = _dir + "/" + req->match()->unmatched_url; srs_warn("open file %s failed, ret=%d", fullpath.c_str(), ret);
if (req->match()->unmatched_url.empty()) { return ret;
fullpath += req->match()->matched_url; }
}
int64_t length = (int64_t)::lseek(fd, 0, SEEK_END);
if (::access(fullpath.c_str(), F_OK | R_OK) < 0) { ::lseek(fd, 0, SEEK_SET);
srs_warn("check file %s does not exists", fullpath.c_str());
char* buf = new char[length];
status_code = HTTP_NotFound; SrsAutoFree(char, buf, true);
reason_phrase = HTTP_NotFound_str;
return false; if (::read(fd, buf, length) < 0) {
} ::close(fd);
ret = ERROR_HTTP_READ_FILE;
return true; srs_warn("read file %s failed, ret=%d", fullpath.c_str(), ret);
} return ret;
}
int SrsHttpVhost::do_process_request(SrsSocket* skt, SrsHttpMessage* req) ::close(fd);
{
int ret = ERROR_SUCCESS; std::string str;
str.append(buf, length);
std::string fullpath = _dir + "/" + req->match()->unmatched_url;
if (req->match()->unmatched_url.empty()) { if (srs_string_ends_with(fullpath, ".ts")) {
fullpath += req->match()->matched_url; return res_mpegts(skt, req, str);
} } else if (srs_string_ends_with(fullpath, ".m3u8")) {
return res_m3u8(skt, req, str);
if (srs_string_ends_with(fullpath, "/")) { } else {
fullpath += "index.html"; return res_text(skt, req, str);
} }
int fd = ::open(fullpath.c_str(), O_RDONLY); return ret;
if (fd < 0) { }
ret = ERROR_HTTP_OPEN_FILE;
srs_warn("open file %s failed, ret=%d", fullpath.c_str(), ret); string SrsHttpVhost::vhost()
return ret; {
} return _vhost;
}
int64_t length = (int64_t)::lseek(fd, 0, SEEK_END);
::lseek(fd, 0, SEEK_SET); string SrsHttpVhost::mount()
{
char* buf = new char[length]; return _mount;
SrsAutoFree(char, buf, true); }
if (::read(fd, buf, length) < 0) { string SrsHttpVhost::dir()
::close(fd); {
ret = ERROR_HTTP_READ_FILE; return _dir;
srs_warn("read file %s failed, ret=%d", fullpath.c_str(), ret); }
return ret;
} SrsHttpConn::SrsHttpConn(SrsServer* srs_server, st_netfd_t client_stfd, SrsHttpHandler* _handler)
::close(fd); : SrsConnection(srs_server, client_stfd)
{
std::string str; parser = new SrsHttpParser();
str.append(buf, length); handler = _handler;
requires_crossdomain = false;
if (srs_string_ends_with(fullpath, ".ts")) { }
return res_mpegts(skt, req, str);
} else if (srs_string_ends_with(fullpath, ".m3u8")) { SrsHttpConn::~SrsHttpConn()
return res_m3u8(skt, req, str); {
} else { srs_freep(parser);
return res_text(skt, req, str); }
}
int SrsHttpConn::do_cycle()
return ret; {
} int ret = ERROR_SUCCESS;
string SrsHttpVhost::vhost() if ((ret = get_peer_ip()) != ERROR_SUCCESS) {
{ srs_error("get peer ip failed. ret=%d", ret);
return _vhost; return ret;
} }
srs_trace("http get peer ip success. ip=%s", ip);
string SrsHttpVhost::mount()
{ // initialize parser
return _mount; if ((ret = parser->initialize(HTTP_REQUEST)) != ERROR_SUCCESS) {
} srs_error("http initialize http parser failed. ret=%d", ret);
return ret;
string SrsHttpVhost::dir() }
{
return _dir; // underlayer socket
} SrsSocket skt(stfd);
SrsHttpConn::SrsHttpConn(SrsServer* srs_server, st_netfd_t client_stfd, SrsHttpHandler* _handler) // process http messages.
: SrsConnection(srs_server, client_stfd) for (;;) {
{ SrsHttpMessage* req = NULL;
parser = new SrsHttpParser();
handler = _handler; // get a http message
requires_crossdomain = false; if ((ret = parser->parse_message(&skt, &req)) != ERROR_SUCCESS) {
} return ret;
}
SrsHttpConn::~SrsHttpConn()
{ // if SUCCESS, always NOT-NULL and completed message.
srs_freep(parser); srs_assert(req);
} srs_assert(req->is_complete());
int SrsHttpConn::do_cycle() // always free it in this scope.
{ SrsAutoFree(SrsHttpMessage, req, false);
int ret = ERROR_SUCCESS;
// ok, handle http request.
if ((ret = get_peer_ip()) != ERROR_SUCCESS) { if ((ret = process_request(&skt, req)) != ERROR_SUCCESS) {
srs_error("get peer ip failed. ret=%d", ret); return ret;
return ret; }
} }
srs_trace("http get peer ip success. ip=%s", ip);
return ret;
// initialize parser }
if ((ret = parser->initialize(HTTP_REQUEST)) != ERROR_SUCCESS) {
srs_error("http initialize http parser failed. ret=%d", ret); int SrsHttpConn::process_request(SrsSocket* skt, SrsHttpMessage* req)
return ret; {
} int ret = ERROR_SUCCESS;
// underlayer socket // parse uri to schema/server:port/path?query
SrsSocket skt(stfd); if ((ret = req->parse_uri()) != ERROR_SUCCESS) {
return ret;
// process http messages. }
for (;;) {
SrsHttpMessage* req = NULL; srs_trace("http request parsed, method=%d, url=%s, content-length=%"PRId64"",
req->method(), req->url().c_str(), req->content_length());
// get a http message
if ((ret = parser->parse_message(&skt, &req)) != ERROR_SUCCESS) { // TODO: maybe need to parse the url.
return ret; std::string url = req->path();
}
SrsHttpHandlerMatch* p = NULL;
// if SUCCESS, always NOT-NULL and completed message. if ((ret = handler->best_match(url.data(), url.length(), &p)) != ERROR_SUCCESS) {
srs_assert(req); srs_warn("failed to find the best match handler for url. ret=%d", ret);
srs_assert(req->is_complete()); return ret;
}
// always free it in this scope.
SrsAutoFree(SrsHttpMessage, req, false); // if success, p and pstart should be valid.
srs_assert(p);
// ok, handle http request. srs_assert(p->handler);
if ((ret = process_request(&skt, req)) != ERROR_SUCCESS) { srs_assert(p->matched_url.length() <= url.length());
return ret; srs_info("best match handler, matched_url=%s", p->matched_url.c_str());
}
} req->set_match(p);
req->set_requires_crossdomain(requires_crossdomain);
return ret;
} // use handler to process request.
if ((ret = p->handler->process_request(skt, req)) != ERROR_SUCCESS) {
int SrsHttpConn::process_request(SrsSocket* skt, SrsHttpMessage* req) srs_warn("handler failed to process http request. ret=%d", ret);
{ return ret;
int ret = ERROR_SUCCESS; }
// parse uri to schema/server:port/path?query if (req->requires_crossdomain()) {
if ((ret = req->parse_uri()) != ERROR_SUCCESS) { requires_crossdomain = true;
return ret; }
}
return ret;
srs_trace("http request parsed, method=%d, url=%s, content-length=%"PRId64"", }
req->method(), req->url().c_str(), req->content_length());
#endif
// TODO: maybe need to parse the url.
std::string url = req->path();
SrsHttpHandlerMatch* p = NULL;
if ((ret = handler->best_match(url.data(), url.length(), &p)) != ERROR_SUCCESS) {
srs_warn("failed to find the best match handler for url. ret=%d", ret);
return ret;
}
// if success, p and pstart should be valid.
srs_assert(p);
srs_assert(p->handler);
srs_assert(p->matched_url.length() <= url.length());
srs_info("best match handler, matched_url=%s", p->matched_url.c_str());
req->set_match(p);
req->set_requires_crossdomain(requires_crossdomain);
// use handler to process request.
if ((ret = p->handler->process_request(skt, req)) != ERROR_SUCCESS) {
srs_warn("handler failed to process http request. ret=%d", ret);
return ret;
}
if (req->requires_crossdomain()) {
requires_crossdomain = true;
}
return ret;
}
#endif

Loading…
Cancel
Save