mirror of
https://github.com/ossrs/srs.git
synced 2025-03-09 15:49:59 +00:00
Fix #2881: HTTP: Support merging api to server. v5.0.47
This commit is contained in:
parent
6508a082e9
commit
457738f6eb
34 changed files with 333 additions and 934 deletions
|
@ -1062,149 +1062,3 @@ srs_error_t SrsGoApiTcmalloc::serve_http(ISrsHttpResponseWriter* w, ISrsHttpMess
|
|||
}
|
||||
#endif
|
||||
|
||||
SrsHttpApi::SrsHttpApi(bool https, ISrsResourceManager* cm, srs_netfd_t fd, SrsHttpServeMux* m, string cip, int port)
|
||||
{
|
||||
// Create a identify for this client.
|
||||
_srs_context->set_id(_srs_context->generate_id());
|
||||
|
||||
manager = cm;
|
||||
skt = new SrsTcpConnection(fd);
|
||||
|
||||
if (https) {
|
||||
ssl = new SrsSslConnection(skt);
|
||||
conn = new SrsHttpConn(this, ssl, m, cip, port);
|
||||
} else {
|
||||
ssl = NULL;
|
||||
conn = new SrsHttpConn(this, skt, m, cip, port);
|
||||
}
|
||||
|
||||
_srs_config->subscribe(this);
|
||||
}
|
||||
|
||||
SrsHttpApi::~SrsHttpApi()
|
||||
{
|
||||
_srs_config->unsubscribe(this);
|
||||
|
||||
srs_freep(conn);
|
||||
srs_freep(ssl);
|
||||
srs_freep(skt);
|
||||
}
|
||||
|
||||
srs_error_t SrsHttpApi::on_start()
|
||||
{
|
||||
srs_error_t err = srs_success;
|
||||
|
||||
if ((err = conn->set_jsonp(true)) != srs_success) {
|
||||
return srs_error_wrap(err, "set jsonp");
|
||||
}
|
||||
|
||||
if (ssl) {
|
||||
srs_utime_t starttime = srs_update_system_time();
|
||||
string crt_file = _srs_config->get_https_api_ssl_cert();
|
||||
string key_file = _srs_config->get_https_api_ssl_key();
|
||||
if ((err = ssl->handshake(key_file, crt_file)) != srs_success) {
|
||||
return srs_error_wrap(err, "handshake");
|
||||
}
|
||||
|
||||
int cost = srsu2msi(srs_update_system_time() - starttime);
|
||||
srs_trace("https: api server done, use key %s and cert %s, cost=%dms",
|
||||
key_file.c_str(), crt_file.c_str(), cost);
|
||||
}
|
||||
|
||||
return err;
|
||||
}
|
||||
|
||||
srs_error_t SrsHttpApi::on_http_message(ISrsHttpMessage* r, SrsHttpResponseWriter* w)
|
||||
{
|
||||
srs_error_t err = srs_success;
|
||||
|
||||
// After parsed the message, set the schema to https.
|
||||
if (ssl) {
|
||||
SrsHttpMessage* hm = dynamic_cast<SrsHttpMessage*>(r);
|
||||
hm->set_https(true);
|
||||
}
|
||||
|
||||
// TODO: For each API session, we use short-term HTTP connection.
|
||||
//SrsHttpHeader* hdr = w->header();
|
||||
//hdr->set("Connection", "Close");
|
||||
|
||||
return err;
|
||||
}
|
||||
|
||||
srs_error_t SrsHttpApi::on_message_done(ISrsHttpMessage* r, SrsHttpResponseWriter* w)
|
||||
{
|
||||
srs_error_t err = srs_success;
|
||||
|
||||
// read all rest bytes in request body.
|
||||
char buf[SRS_HTTP_READ_CACHE_BYTES];
|
||||
ISrsHttpResponseReader* br = r->body_reader();
|
||||
while (!br->eof()) {
|
||||
if ((err = br->read(buf, SRS_HTTP_READ_CACHE_BYTES, NULL)) != srs_success) {
|
||||
return srs_error_wrap(err, "read response");
|
||||
}
|
||||
}
|
||||
|
||||
return err;
|
||||
}
|
||||
|
||||
srs_error_t SrsHttpApi::on_conn_done(srs_error_t r0)
|
||||
{
|
||||
// Because we use manager to manage this object,
|
||||
// not the http connection object, so we must remove it here.
|
||||
manager->remove(this);
|
||||
|
||||
// For HTTP-API timeout, we think it's done successfully,
|
||||
// because there may be no request or response for HTTP-API.
|
||||
if (srs_error_code(r0) == ERROR_SOCKET_TIMEOUT) {
|
||||
srs_freep(r0);
|
||||
return srs_success;
|
||||
}
|
||||
|
||||
return r0;
|
||||
}
|
||||
|
||||
std::string SrsHttpApi::desc()
|
||||
{
|
||||
if (ssl) {
|
||||
return "HttpsConn";
|
||||
}
|
||||
return "HttpConn";
|
||||
}
|
||||
|
||||
void SrsHttpApi::remark(int64_t* in, int64_t* out)
|
||||
{
|
||||
conn->remark(in, out);
|
||||
}
|
||||
|
||||
srs_error_t SrsHttpApi::on_reload_http_api_crossdomain()
|
||||
{
|
||||
bool v = _srs_config->get_http_api_crossdomain();
|
||||
return conn->set_crossdomain_enabled(v);
|
||||
}
|
||||
|
||||
srs_error_t SrsHttpApi::start()
|
||||
{
|
||||
srs_error_t err = srs_success;
|
||||
|
||||
bool v = _srs_config->get_http_api_crossdomain();
|
||||
if ((err = conn->set_crossdomain_enabled(v)) != srs_success) {
|
||||
return srs_error_wrap(err, "set cors=%d", v);
|
||||
}
|
||||
|
||||
if ((err = skt->initialize()) != srs_success) {
|
||||
return srs_error_wrap(err, "init socket");
|
||||
}
|
||||
|
||||
return conn->start();
|
||||
}
|
||||
|
||||
string SrsHttpApi::remote_ip()
|
||||
{
|
||||
return conn->remote_ip();
|
||||
}
|
||||
|
||||
const SrsContextId& SrsHttpApi::get_id()
|
||||
{
|
||||
return conn->get_id();
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue