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

implements basic http response framework

This commit is contained in:
winlin 2014-04-02 19:10:22 +08:00
parent 341b5151d9
commit bfa07465f0
4 changed files with 143 additions and 41 deletions

View file

@ -36,21 +36,56 @@ using namespace std;
SrsApiRoot::SrsApiRoot()
{
handlers.push_back(new SrsApiApi());
}
SrsApiRoot::~SrsApiRoot()
{
}
bool SrsApiRoot::can_handle(const char* /*path*/, int /*length*/)
bool SrsApiRoot::can_handle(const char* path, int length, const char** pnext_path)
{
// reset the next path for child to parse.
*pnext_path = path;
return true;
}
int SrsApiRoot::process_request(SrsSocket* /*skt*/, SrsHttpMessage* /*req*/, const char* /*path*/, int /*length*/)
int SrsApiRoot::process_request(SrsSocket* skt, SrsHttpMessage* req, const char* /*path*/, int /*length*/)
{
int ret = ERROR_SUCCESS;
return ret;
if (req->method() == HTTP_OPTIONS) {
return res_options(skt);
} else {
std::string body = "hello, root";
return res_text(skt, body);
}
return ERROR_SUCCESS;
}
SrsApiApi::SrsApiApi()
{
}
SrsApiApi::~SrsApiApi()
{
}
bool SrsApiApi::can_handle(const char* path, int length, const char** /*pnext_path*/)
{
return !memcmp("/api", path, length);
}
int SrsApiApi::process_request(SrsSocket* skt, SrsHttpMessage* req, const char* /*path*/, int /*length*/)
{
if (req->method() == HTTP_OPTIONS) {
return res_options(skt);
} else {
std::string body = "hello, api";
return res_text(skt, body);
}
return ERROR_SUCCESS;
}
SrsHttpApi::SrsHttpApi(SrsServer* srs_server, st_netfd_t client_stfd, SrsHttpHandler* _handler)
@ -135,35 +170,6 @@ int SrsHttpApi::process_request(SrsSocket* skt, SrsHttpMessage* req)
return ret;
}
if (req->method() == HTTP_OPTIONS) {
char data[] = "HTTP/1.1 200 OK" __CRLF
"Content-Length: 0"__CRLF
"Server: SRS/"RTMP_SIG_SRS_VERSION""__CRLF
"Allow: DELETE, GET, HEAD, OPTIONS, POST, PUT"__CRLF
"Access-Control-Allow-Origin: *"__CRLF
"Access-Control-Allow-Methods: GET, POST, HEAD, PUT, DELETE"__CRLF
"Access-Control-Allow-Headers: Cache-Control,X-Proxy-Authorization,X-Requested-With,Content-Type"__CRLF
"Content-Type: text/html;charset=utf-8"__CRLFCRLF
"";
return skt->write(data, sizeof(data), NULL);
} else {
std::string tilte = "SRS/"RTMP_SIG_SRS_VERSION;
tilte += " hello http/1.1 api~\n";
std::stringstream ss;
ss << "HTTP/1.1 200 OK " << __CRLF
<< "Content-Length: "<< tilte.length() + req->body_size() << __CRLF
<< "Server: SRS/"RTMP_SIG_SRS_VERSION"" << __CRLF
<< "Allow: DELETE, GET, HEAD, OPTIONS, POST, PUT" << __CRLF
<< "Access-Control-Allow-Origin: *" << __CRLF
<< "Access-Control-Allow-Methods: GET, POST, HEAD, PUT, DELETE" << __CRLF
<< "Access-Control-Allow-Headers: Cache-Control,X-Proxy-Authorization,X-Requested-With,Content-Type" << __CRLF
<< "Content-Type: text/html;charset=utf-8" << __CRLFCRLF
<< tilte << req->body().c_str()
<< "";
return skt->write(ss.str().c_str(), ss.str().length(), NULL);
}
return ret;
}