mirror of
https://github.com/ossrs/srs.git
synced 2025-03-09 15:49:59 +00:00
refine code for hooks and http core.
This commit is contained in:
parent
79fdc4e766
commit
f516636448
12 changed files with 144 additions and 43 deletions
|
@ -36,10 +36,12 @@ using namespace std;
|
|||
#include <srs_app_utility.hpp>
|
||||
#include <srs_core_autofree.hpp>
|
||||
#include <srs_app_http_conn.hpp>
|
||||
#include <srs_protocol_kbps.hpp>
|
||||
|
||||
SrsHttpClient::SrsHttpClient()
|
||||
{
|
||||
transport = new SrsTcpClient();
|
||||
kbps = new SrsKbps();
|
||||
parser = NULL;
|
||||
timeout_us = 0;
|
||||
port = 0;
|
||||
|
@ -49,6 +51,7 @@ SrsHttpClient::~SrsHttpClient()
|
|||
{
|
||||
disconnect();
|
||||
|
||||
srs_freep(kbps);
|
||||
srs_freep(transport);
|
||||
srs_freep(parser);
|
||||
}
|
||||
|
@ -74,15 +77,37 @@ int SrsHttpClient::initialize(string h, int p, int64_t t_us)
|
|||
port = p;
|
||||
timeout_us = t_us;
|
||||
|
||||
// ep used for host in header.
|
||||
string ep = host;
|
||||
if (port > 0 && port != SRS_CONSTS_HTTP_DEFAULT_PORT) {
|
||||
ep += ":" + srs_int2str(port);
|
||||
}
|
||||
|
||||
// set default value for headers.
|
||||
headers["Host"] = ep;
|
||||
headers["Connection"] = "Keep-Alive";
|
||||
headers["User-Agent"] = RTMP_SIG_SRS_SERVER;
|
||||
headers["Content-Type"] = "application/json";
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
SrsHttpClient* SrsHttpClient::set_header(string k, string v)
|
||||
{
|
||||
headers[k] = v;
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
int SrsHttpClient::post(string path, string req, ISrsHttpMessage** ppmsg)
|
||||
{
|
||||
*ppmsg = NULL;
|
||||
|
||||
int ret = ERROR_SUCCESS;
|
||||
|
||||
// always set the content length.
|
||||
headers["Content-Length"] = srs_int2str(req.length());
|
||||
|
||||
if ((ret = connect()) != ERROR_SUCCESS) {
|
||||
srs_warn("http connect server failed. ret=%d", ret);
|
||||
return ret;
|
||||
|
@ -91,15 +116,13 @@ int SrsHttpClient::post(string path, string req, ISrsHttpMessage** ppmsg)
|
|||
// send POST request to uri
|
||||
// POST %s HTTP/1.1\r\nHost: %s\r\nContent-Length: %d\r\n\r\n%s
|
||||
std::stringstream ss;
|
||||
ss << "POST " << path << " "
|
||||
<< "HTTP/1.1" << SRS_HTTP_CRLF
|
||||
<< "Host: " << host << SRS_HTTP_CRLF
|
||||
<< "Connection: Keep-Alive" << SRS_HTTP_CRLF
|
||||
<< "Content-Length: " << std::dec << req.length() << SRS_HTTP_CRLF
|
||||
<< "User-Agent: " << RTMP_SIG_SRS_NAME << RTMP_SIG_SRS_VERSION << SRS_HTTP_CRLF
|
||||
<< "Content-Type: application/json" << SRS_HTTP_CRLF
|
||||
<< SRS_HTTP_CRLF
|
||||
<< req;
|
||||
ss << "POST " << path << " " << "HTTP/1.1" << SRS_HTTP_CRLF;
|
||||
for (map<string, string>::iterator it = headers.begin(); it != headers.end(); ++it) {
|
||||
string key = it->first;
|
||||
string value = it->second;
|
||||
ss << key << ": " << value << SRS_HTTP_CRLF;
|
||||
}
|
||||
ss << SRS_HTTP_CRLF << req;
|
||||
|
||||
std::string data = ss.str();
|
||||
if ((ret = transport->write((void*)data.c_str(), data.length(), NULL)) != ERROR_SUCCESS) {
|
||||
|
@ -123,11 +146,14 @@ int SrsHttpClient::post(string path, string req, ISrsHttpMessage** ppmsg)
|
|||
return ret;
|
||||
}
|
||||
|
||||
int SrsHttpClient::get(string path, std::string req, ISrsHttpMessage** ppmsg)
|
||||
int SrsHttpClient::get(string path, string req, ISrsHttpMessage** ppmsg)
|
||||
{
|
||||
*ppmsg = NULL;
|
||||
|
||||
int ret = ERROR_SUCCESS;
|
||||
|
||||
// always set the content length.
|
||||
headers["Content-Length"] = srs_int2str(req.length());
|
||||
|
||||
if ((ret = connect()) != ERROR_SUCCESS) {
|
||||
srs_warn("http connect server failed. ret=%d", ret);
|
||||
|
@ -137,15 +163,13 @@ int SrsHttpClient::get(string path, std::string req, ISrsHttpMessage** ppmsg)
|
|||
// send POST request to uri
|
||||
// GET %s HTTP/1.1\r\nHost: %s\r\nContent-Length: %d\r\n\r\n%s
|
||||
std::stringstream ss;
|
||||
ss << "GET " << path << " "
|
||||
<< "HTTP/1.1" << SRS_HTTP_CRLF
|
||||
<< "Host: " << host << SRS_HTTP_CRLF
|
||||
<< "Connection: Keep-Alive" << SRS_HTTP_CRLF
|
||||
<< "Content-Length: " << std::dec << req.length() << SRS_HTTP_CRLF
|
||||
<< "User-Agent: " << RTMP_SIG_SRS_NAME << RTMP_SIG_SRS_VERSION << SRS_HTTP_CRLF
|
||||
<< "Content-Type: application/json" << SRS_HTTP_CRLF
|
||||
<< SRS_HTTP_CRLF
|
||||
<< req;
|
||||
ss << "GET " << path << " " << "HTTP/1.1" << SRS_HTTP_CRLF;
|
||||
for (map<string, string>::iterator it = headers.begin(); it != headers.end(); ++it) {
|
||||
string key = it->first;
|
||||
string value = it->second;
|
||||
ss << key << ": " << value << SRS_HTTP_CRLF;
|
||||
}
|
||||
ss << SRS_HTTP_CRLF << req;
|
||||
|
||||
std::string data = ss.str();
|
||||
if ((ret = transport->write((void*)data.c_str(), data.length(), NULL)) != ERROR_SUCCESS) {
|
||||
|
@ -169,8 +193,28 @@ int SrsHttpClient::get(string path, std::string req, ISrsHttpMessage** ppmsg)
|
|||
return ret;
|
||||
}
|
||||
|
||||
void SrsHttpClient::set_recv_timeout(int64_t timeout)
|
||||
{
|
||||
transport->set_recv_timeout(timeout);
|
||||
}
|
||||
|
||||
void SrsHttpClient::kbps_sample(const char* label, int64_t age)
|
||||
{
|
||||
kbps->sample();
|
||||
|
||||
int sr = kbps->get_send_kbps();
|
||||
int sr30s = kbps->get_send_kbps_30s();
|
||||
int sr5m = kbps->get_send_kbps_5m();
|
||||
int rr = kbps->get_recv_kbps();
|
||||
int rr30s = kbps->get_recv_kbps_30s();
|
||||
int rr5m = kbps->get_recv_kbps_5m();
|
||||
|
||||
srs_trace("<- %s time=%"PRId64", okbps=%d,%d,%d, ikbps=%d,%d,%d", label, age, sr, sr30s, sr5m, rr, rr30s, rr5m);
|
||||
}
|
||||
|
||||
void SrsHttpClient::disconnect()
|
||||
{
|
||||
kbps->set_io(NULL, NULL);
|
||||
transport->close();
|
||||
}
|
||||
|
||||
|
@ -196,6 +240,8 @@ int SrsHttpClient::connect()
|
|||
transport->set_recv_timeout(timeout_us);
|
||||
transport->set_send_timeout(timeout_us);
|
||||
|
||||
kbps->set_io(transport, transport);
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue