2014-03-27 04:14:04 +00:00
|
|
|
/*
|
|
|
|
The MIT License (MIT)
|
|
|
|
|
|
|
|
Copyright (c) 2013-2014 winlin
|
|
|
|
|
|
|
|
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
|
|
|
|
the Software without restriction, including without limitation the rights to
|
|
|
|
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,
|
|
|
|
subject to the following conditions:
|
|
|
|
|
|
|
|
The above copyright notice and this permission notice shall be included in all
|
|
|
|
copies or substantial portions of the Software.
|
|
|
|
|
|
|
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
|
|
|
|
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
|
|
|
|
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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <srs_app_http_api.hpp>
|
|
|
|
|
2014-04-15 06:01:57 +00:00
|
|
|
#ifdef SRS_AUTO_HTTP_API
|
2014-04-01 08:06:32 +00:00
|
|
|
|
2014-04-02 04:55:10 +00:00
|
|
|
#include <sstream>
|
|
|
|
using namespace std;
|
|
|
|
|
2014-03-27 05:25:08 +00:00
|
|
|
#include <srs_kernel_log.hpp>
|
|
|
|
#include <srs_kernel_error.hpp>
|
2014-04-02 04:55:10 +00:00
|
|
|
#include <srs_app_http.hpp>
|
|
|
|
#include <srs_app_socket.hpp>
|
|
|
|
#include <srs_core_autofree.hpp>
|
2014-04-03 03:49:14 +00:00
|
|
|
#include <srs_app_json.hpp>
|
2014-03-27 05:25:08 +00:00
|
|
|
|
2014-04-02 10:07:34 +00:00
|
|
|
SrsApiRoot::SrsApiRoot()
|
|
|
|
{
|
2014-04-02 11:10:22 +00:00
|
|
|
handlers.push_back(new SrsApiApi());
|
2014-04-02 10:07:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
SrsApiRoot::~SrsApiRoot()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2014-04-03 07:17:02 +00:00
|
|
|
bool SrsApiRoot::is_handler_valid(SrsHttpMessage* req, int& status_code, std::string& reason_phrase)
|
|
|
|
{
|
|
|
|
if (!SrsHttpHandler::is_handler_valid(req, status_code, reason_phrase)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (req->match()->matched_url.length() != 1) {
|
|
|
|
status_code = HTTP_NotFound;
|
|
|
|
reason_phrase = HTTP_NotFound_str;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-04-03 03:49:14 +00:00
|
|
|
bool SrsApiRoot::can_handle(const char* path, int length, const char** pchild)
|
2014-04-02 10:07:34 +00:00
|
|
|
{
|
2014-04-03 03:49:14 +00:00
|
|
|
// reset the child path to path,
|
|
|
|
// for child to reparse the path.
|
|
|
|
*pchild = path;
|
2014-04-02 11:10:22 +00:00
|
|
|
|
2014-04-03 03:49:14 +00:00
|
|
|
// only compare the first char.
|
|
|
|
return srs_path_equals("/", path, 1);
|
2014-04-02 10:07:34 +00:00
|
|
|
}
|
|
|
|
|
2014-04-03 03:49:14 +00:00
|
|
|
int SrsApiRoot::do_process_request(SrsSocket* skt, SrsHttpMessage* req)
|
2014-04-02 10:07:34 +00:00
|
|
|
{
|
2014-04-03 03:49:14 +00:00
|
|
|
std::stringstream ss;
|
|
|
|
|
|
|
|
ss << JOBJECT_START
|
|
|
|
<< JFIELD_ERROR(ERROR_SUCCESS) << JFIELD_CONT
|
|
|
|
<< JFIELD_ORG("urls", JOBJECT_START)
|
2014-04-03 04:08:22 +00:00
|
|
|
<< JFIELD_STR("api", "the api root")
|
2014-04-03 03:49:14 +00:00
|
|
|
<< JOBJECT_END
|
|
|
|
<< JOBJECT_END;
|
|
|
|
|
2014-04-03 08:39:55 +00:00
|
|
|
return res_json(skt, req, ss.str());
|
2014-04-02 11:10:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
SrsApiApi::SrsApiApi()
|
|
|
|
{
|
2014-04-03 04:08:22 +00:00
|
|
|
handlers.push_back(new SrsApiV1());
|
2014-04-02 11:10:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
SrsApiApi::~SrsApiApi()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2014-04-03 03:49:14 +00:00
|
|
|
bool SrsApiApi::can_handle(const char* path, int length, const char** /*pchild*/)
|
2014-04-02 11:10:22 +00:00
|
|
|
{
|
2014-04-03 03:49:14 +00:00
|
|
|
return srs_path_equals("/api", path, length);
|
2014-04-02 11:10:22 +00:00
|
|
|
}
|
|
|
|
|
2014-04-03 03:49:14 +00:00
|
|
|
int SrsApiApi::do_process_request(SrsSocket* skt, SrsHttpMessage* req)
|
2014-04-02 11:10:22 +00:00
|
|
|
{
|
2014-04-03 04:08:22 +00:00
|
|
|
std::stringstream ss;
|
|
|
|
|
|
|
|
ss << JOBJECT_START
|
|
|
|
<< JFIELD_ERROR(ERROR_SUCCESS) << JFIELD_CONT
|
|
|
|
<< JFIELD_ORG("urls", JOBJECT_START)
|
|
|
|
<< JFIELD_STR("v1", "the api version 1.0")
|
|
|
|
<< JOBJECT_END
|
|
|
|
<< JOBJECT_END;
|
|
|
|
|
2014-04-03 08:39:55 +00:00
|
|
|
return res_json(skt, req, ss.str());
|
2014-04-03 04:08:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
SrsApiV1::SrsApiV1()
|
|
|
|
{
|
|
|
|
handlers.push_back(new SrsApiVersion());
|
2014-04-03 05:48:52 +00:00
|
|
|
handlers.push_back(new SrsApiAuthors());
|
2014-04-03 04:08:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
SrsApiV1::~SrsApiV1()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
bool SrsApiV1::can_handle(const char* path, int length, const char** /*pchild*/)
|
|
|
|
{
|
|
|
|
return srs_path_equals("/v1", path, length);
|
|
|
|
}
|
|
|
|
|
|
|
|
int SrsApiV1::do_process_request(SrsSocket* skt, SrsHttpMessage* req)
|
|
|
|
{
|
|
|
|
std::stringstream ss;
|
|
|
|
|
|
|
|
ss << JOBJECT_START
|
|
|
|
<< JFIELD_ERROR(ERROR_SUCCESS) << JFIELD_CONT
|
|
|
|
<< JFIELD_ORG("urls", JOBJECT_START)
|
2014-04-03 06:17:00 +00:00
|
|
|
<< JFIELD_STR("versions", "the version of SRS") << JFIELD_CONT
|
2014-04-03 05:48:52 +00:00
|
|
|
<< JFIELD_STR("authors", "the primary authors and contributors")
|
2014-04-03 04:08:22 +00:00
|
|
|
<< JOBJECT_END
|
|
|
|
<< JOBJECT_END;
|
|
|
|
|
2014-04-03 08:39:55 +00:00
|
|
|
return res_json(skt, req, ss.str());
|
2014-04-03 04:08:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
SrsApiVersion::SrsApiVersion()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
SrsApiVersion::~SrsApiVersion()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
bool SrsApiVersion::can_handle(const char* path, int length, const char** /*pchild*/)
|
|
|
|
{
|
2014-04-03 06:17:00 +00:00
|
|
|
return srs_path_equals("/versions", path, length);
|
2014-04-03 04:08:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int SrsApiVersion::do_process_request(SrsSocket* skt, SrsHttpMessage* req)
|
|
|
|
{
|
|
|
|
std::stringstream ss;
|
|
|
|
|
|
|
|
ss << JOBJECT_START
|
|
|
|
<< JFIELD_ERROR(ERROR_SUCCESS) << JFIELD_CONT
|
|
|
|
<< JFIELD_ORG("data", JOBJECT_START)
|
|
|
|
<< JFIELD_ORG("major", VERSION_MAJOR) << JFIELD_CONT
|
|
|
|
<< JFIELD_ORG("minor", VERSION_MINOR) << JFIELD_CONT
|
|
|
|
<< JFIELD_ORG("revision", VERSION_REVISION) << JFIELD_CONT
|
2014-04-03 05:48:52 +00:00
|
|
|
<< JFIELD_STR("version", RTMP_SIG_SRS_VERSION)
|
|
|
|
<< JOBJECT_END
|
|
|
|
<< JOBJECT_END;
|
|
|
|
|
2014-04-03 08:39:55 +00:00
|
|
|
return res_json(skt, req, ss.str());
|
2014-04-03 05:48:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
SrsApiAuthors::SrsApiAuthors()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
SrsApiAuthors::~SrsApiAuthors()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
bool SrsApiAuthors::can_handle(const char* path, int length, const char** /*pchild*/)
|
|
|
|
{
|
|
|
|
return srs_path_equals("/authors", path, length);
|
|
|
|
}
|
|
|
|
|
|
|
|
int SrsApiAuthors::do_process_request(SrsSocket* skt, SrsHttpMessage* req)
|
|
|
|
{
|
|
|
|
std::stringstream ss;
|
|
|
|
|
|
|
|
ss << JOBJECT_START
|
|
|
|
<< JFIELD_ERROR(ERROR_SUCCESS) << JFIELD_CONT
|
|
|
|
<< JFIELD_ORG("data", JOBJECT_START)
|
|
|
|
<< JFIELD_STR("primary_authors", RTMP_SIG_SRS_PRIMARY_AUTHROS) << JFIELD_CONT
|
|
|
|
<< JFIELD_STR("contributors_link", RTMP_SIG_SRS_CONTRIBUTORS_URL) << JFIELD_CONT
|
2014-04-15 06:01:57 +00:00
|
|
|
<< JFIELD_STR("contributors", SRS_AUTO_CONSTRIBUTORS)
|
2014-04-03 04:08:22 +00:00
|
|
|
<< JOBJECT_END
|
|
|
|
<< JOBJECT_END;
|
|
|
|
|
2014-04-03 08:39:55 +00:00
|
|
|
return res_json(skt, req, ss.str());
|
2014-04-02 10:07:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
SrsHttpApi::SrsHttpApi(SrsServer* srs_server, st_netfd_t client_stfd, SrsHttpHandler* _handler)
|
2014-03-27 05:25:08 +00:00
|
|
|
: SrsConnection(srs_server, client_stfd)
|
|
|
|
{
|
2014-04-02 04:55:10 +00:00
|
|
|
parser = new SrsHttpParser();
|
2014-04-02 10:07:34 +00:00
|
|
|
handler = _handler;
|
2014-04-03 08:39:55 +00:00
|
|
|
requires_crossdomain = false;
|
2014-03-27 05:25:08 +00:00
|
|
|
}
|
|
|
|
|
2014-03-27 09:13:26 +00:00
|
|
|
SrsHttpApi::~SrsHttpApi()
|
2014-03-27 05:25:08 +00:00
|
|
|
{
|
2014-04-02 04:55:10 +00:00
|
|
|
srs_freep(parser);
|
2014-03-27 04:14:04 +00:00
|
|
|
}
|
|
|
|
|
2014-03-27 09:13:26 +00:00
|
|
|
int SrsHttpApi::do_cycle()
|
2014-03-27 05:25:08 +00:00
|
|
|
{
|
|
|
|
int ret = ERROR_SUCCESS;
|
|
|
|
|
|
|
|
if ((ret = get_peer_ip()) != ERROR_SUCCESS) {
|
|
|
|
srs_error("get peer ip failed. ret=%d", ret);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
srs_trace("api get peer ip success. ip=%s", ip);
|
|
|
|
|
2014-04-02 04:55:10 +00:00
|
|
|
// initialize parser
|
|
|
|
if ((ret = parser->initialize(HTTP_REQUEST)) != ERROR_SUCCESS) {
|
|
|
|
srs_error("api initialize http parser failed. ret=%d", ret);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
// underlayer socket
|
|
|
|
SrsSocket skt(stfd);
|
|
|
|
|
|
|
|
// process http messages.
|
|
|
|
for (;;) {
|
|
|
|
SrsHttpMessage* req = NULL;
|
|
|
|
|
|
|
|
// get a http message
|
|
|
|
if ((ret = parser->parse_message(&skt, &req)) != ERROR_SUCCESS) {
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
// if SUCCESS, always NOT-NULL and completed message.
|
|
|
|
srs_assert(req);
|
|
|
|
srs_assert(req->is_complete());
|
|
|
|
|
|
|
|
// always free it in this scope.
|
|
|
|
SrsAutoFree(SrsHttpMessage, req, false);
|
|
|
|
|
|
|
|
// ok, handle http request.
|
|
|
|
if ((ret = process_request(&skt, req)) != ERROR_SUCCESS) {
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
int SrsHttpApi::process_request(SrsSocket* skt, SrsHttpMessage* req)
|
|
|
|
{
|
|
|
|
int ret = ERROR_SUCCESS;
|
2014-04-03 03:49:14 +00:00
|
|
|
|
|
|
|
// parse uri to schema/server:port/path?query
|
|
|
|
if ((ret = req->parse_uri()) != ERROR_SUCCESS) {
|
|
|
|
return ret;
|
|
|
|
}
|
2014-04-02 04:55:10 +00:00
|
|
|
|
2014-04-03 05:48:52 +00:00
|
|
|
srs_trace("http request parsed, method=%d, url=%s, content-length=%"PRId64"",
|
|
|
|
req->method(), req->url().c_str(), req->content_length());
|
|
|
|
|
2014-04-02 10:07:34 +00:00
|
|
|
// TODO: maybe need to parse the url.
|
2014-04-03 03:49:14 +00:00
|
|
|
std::string url = req->path();
|
2014-04-02 10:07:34 +00:00
|
|
|
|
2014-04-03 03:49:14 +00:00
|
|
|
SrsHttpHandlerMatch* p = NULL;
|
|
|
|
if ((ret = handler->best_match(url.data(), url.length(), &p)) != ERROR_SUCCESS) {
|
2014-04-02 10:07:34 +00:00
|
|
|
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);
|
2014-04-03 03:49:14 +00:00
|
|
|
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);
|
2014-04-03 08:39:55 +00:00
|
|
|
req->set_requires_crossdomain(requires_crossdomain);
|
2014-04-02 10:07:34 +00:00
|
|
|
|
|
|
|
// use handler to process request.
|
2014-04-03 03:49:14 +00:00
|
|
|
if ((ret = p->handler->process_request(skt, req)) != ERROR_SUCCESS) {
|
2014-04-02 10:07:34 +00:00
|
|
|
srs_warn("handler failed to process http request. ret=%d", ret);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2014-04-03 08:39:55 +00:00
|
|
|
if (req->requires_crossdomain()) {
|
|
|
|
requires_crossdomain = true;
|
|
|
|
}
|
|
|
|
|
2014-03-27 05:25:08 +00:00
|
|
|
return ret;
|
2014-03-27 04:14:04 +00:00
|
|
|
}
|
2014-04-01 08:06:32 +00:00
|
|
|
|
|
|
|
#endif
|