2014-05-19 07:45:04 +00:00
|
|
|
/*
|
|
|
|
The MIT License (MIT)
|
|
|
|
|
2014-12-31 12:32:09 +00:00
|
|
|
Copyright (c) 2013-2015 winlin
|
2014-05-19 07:45:04 +00:00
|
|
|
|
|
|
|
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_client.hpp>
|
|
|
|
|
2014-05-19 09:39:01 +00:00
|
|
|
#ifdef SRS_AUTO_HTTP_PARSER
|
|
|
|
|
|
|
|
#include <arpa/inet.h>
|
|
|
|
|
|
|
|
using namespace std;
|
|
|
|
|
|
|
|
#include <srs_app_http.hpp>
|
|
|
|
#include <srs_kernel_error.hpp>
|
|
|
|
#include <srs_kernel_log.hpp>
|
2014-07-26 12:07:12 +00:00
|
|
|
#include <srs_app_st_socket.hpp>
|
2014-06-08 05:03:03 +00:00
|
|
|
#include <srs_kernel_utility.hpp>
|
2014-06-29 06:39:56 +00:00
|
|
|
#include <srs_app_utility.hpp>
|
2015-03-04 10:20:15 +00:00
|
|
|
#include <srs_core_autofree.hpp>
|
2014-06-29 06:39:56 +00:00
|
|
|
|
2014-05-19 09:39:01 +00:00
|
|
|
SrsHttpClient::SrsHttpClient()
|
|
|
|
{
|
|
|
|
connected = false;
|
|
|
|
stfd = NULL;
|
2015-03-05 13:45:01 +00:00
|
|
|
skt = NULL;
|
2015-03-06 04:07:12 +00:00
|
|
|
parser = NULL;
|
2015-04-10 05:45:21 +00:00
|
|
|
timeout_us = 0;
|
2014-05-19 09:39:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
SrsHttpClient::~SrsHttpClient()
|
|
|
|
{
|
|
|
|
disconnect();
|
|
|
|
srs_freep(parser);
|
|
|
|
}
|
|
|
|
|
2015-04-10 05:45:21 +00:00
|
|
|
int SrsHttpClient::initialize(string h, int p, int64_t t_us)
|
2014-05-19 09:39:01 +00:00
|
|
|
{
|
|
|
|
int ret = ERROR_SUCCESS;
|
|
|
|
|
2015-03-06 04:07:12 +00:00
|
|
|
srs_freep(parser);
|
|
|
|
parser = new SrsHttpParser();
|
|
|
|
|
|
|
|
if ((ret = parser->initialize(HTTP_RESPONSE)) != ERROR_SUCCESS) {
|
|
|
|
srs_error("initialize parser failed. ret=%d", ret);
|
|
|
|
return ret;
|
2014-05-19 09:39:01 +00:00
|
|
|
}
|
|
|
|
|
2015-03-06 04:07:12 +00:00
|
|
|
host = h;
|
|
|
|
port = p;
|
2015-04-10 05:45:21 +00:00
|
|
|
timeout_us = t_us;
|
2015-03-06 04:07:12 +00:00
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
int SrsHttpClient::post(string path, string req, SrsHttpMessage** ppmsg)
|
|
|
|
{
|
|
|
|
*ppmsg = NULL;
|
|
|
|
|
|
|
|
int ret = ERROR_SUCCESS;
|
|
|
|
|
|
|
|
if ((ret = connect()) != ERROR_SUCCESS) {
|
2014-06-23 07:44:49 +00:00
|
|
|
srs_warn("http connect server failed. ret=%d", ret);
|
2014-05-19 09:39:01 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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;
|
2015-03-06 04:07:12 +00:00
|
|
|
ss << "POST " << path << " "
|
2015-03-21 02:25:03 +00:00
|
|
|
<< "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
|
2014-05-19 09:39:01 +00:00
|
|
|
<< req;
|
|
|
|
|
|
|
|
std::string data = ss.str();
|
2015-03-05 13:45:01 +00:00
|
|
|
if ((ret = skt->write((void*)data.c_str(), data.length(), NULL)) != ERROR_SUCCESS) {
|
2014-05-19 09:39:01 +00:00
|
|
|
// disconnect when error.
|
|
|
|
disconnect();
|
|
|
|
|
|
|
|
srs_error("write http post failed. ret=%d", ret);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
SrsHttpMessage* msg = NULL;
|
2015-03-05 13:45:01 +00:00
|
|
|
if ((ret = parser->parse_message(skt, &msg)) != ERROR_SUCCESS) {
|
2014-05-19 09:39:01 +00:00
|
|
|
srs_error("parse http post response failed. ret=%d", ret);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
srs_assert(msg);
|
2015-03-06 03:51:20 +00:00
|
|
|
*ppmsg = msg;
|
2014-05-19 09:39:01 +00:00
|
|
|
srs_info("parse http post response success.");
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2015-03-06 04:07:12 +00:00
|
|
|
int SrsHttpClient::get(string path, std::string req, SrsHttpMessage** ppmsg)
|
2015-03-05 13:45:01 +00:00
|
|
|
{
|
|
|
|
*ppmsg = NULL;
|
|
|
|
|
|
|
|
int ret = ERROR_SUCCESS;
|
|
|
|
|
2015-03-06 04:07:12 +00:00
|
|
|
if ((ret = connect()) != ERROR_SUCCESS) {
|
2015-03-05 13:45:01 +00:00
|
|
|
srs_warn("http connect server failed. ret=%d", ret);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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;
|
2015-03-06 04:07:12 +00:00
|
|
|
ss << "GET " << path << " "
|
2015-03-21 02:25:03 +00:00
|
|
|
<< "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
|
2015-03-05 13:45:01 +00:00
|
|
|
<< req;
|
|
|
|
|
|
|
|
std::string data = ss.str();
|
|
|
|
if ((ret = skt->write((void*)data.c_str(), data.length(), NULL)) != ERROR_SUCCESS) {
|
|
|
|
// disconnect when error.
|
|
|
|
disconnect();
|
|
|
|
|
|
|
|
srs_error("write http get failed. ret=%d", ret);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
SrsHttpMessage* msg = NULL;
|
|
|
|
if ((ret = parser->parse_message(skt, &msg)) != ERROR_SUCCESS) {
|
|
|
|
srs_error("parse http post response failed. ret=%d", ret);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
srs_assert(msg);
|
2015-03-06 03:36:26 +00:00
|
|
|
|
2015-03-05 13:45:01 +00:00
|
|
|
*ppmsg = msg;
|
|
|
|
srs_info("parse http get response success.");
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2014-05-19 09:39:01 +00:00
|
|
|
void SrsHttpClient::disconnect()
|
|
|
|
{
|
|
|
|
connected = false;
|
|
|
|
|
|
|
|
srs_close_stfd(stfd);
|
2015-03-05 13:45:01 +00:00
|
|
|
srs_freep(skt);
|
2014-05-19 09:39:01 +00:00
|
|
|
}
|
|
|
|
|
2015-03-06 04:07:12 +00:00
|
|
|
int SrsHttpClient::connect()
|
2014-05-19 09:39:01 +00:00
|
|
|
{
|
|
|
|
int ret = ERROR_SUCCESS;
|
|
|
|
|
|
|
|
if (connected) {
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
disconnect();
|
|
|
|
|
2014-06-29 06:39:56 +00:00
|
|
|
// open socket.
|
2015-04-10 05:45:21 +00:00
|
|
|
if ((ret = srs_socket_connect(host, port, timeout_us, &stfd)) != ERROR_SUCCESS) {
|
2014-06-29 06:39:56 +00:00
|
|
|
srs_warn("http client failed, server=%s, port=%d, timeout=%"PRId64", ret=%d",
|
2015-04-10 05:45:21 +00:00
|
|
|
host.c_str(), port, timeout_us, ret);
|
2014-05-19 09:39:01 +00:00
|
|
|
return ret;
|
|
|
|
}
|
2015-03-06 04:07:12 +00:00
|
|
|
srs_info("connect to server success. server=%s, port=%d", host, port);
|
2014-05-19 09:39:01 +00:00
|
|
|
|
2015-03-05 13:45:01 +00:00
|
|
|
srs_assert(!skt);
|
|
|
|
skt = new SrsStSocket(stfd);
|
2014-05-19 09:39:01 +00:00
|
|
|
connected = true;
|
|
|
|
|
2015-04-10 05:45:21 +00:00
|
|
|
// set the recv/send timeout in us.
|
|
|
|
skt->set_recv_timeout(timeout_us);
|
|
|
|
skt->set_send_timeout(timeout_us);
|
|
|
|
|
2014-05-19 09:39:01 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
2014-08-02 14:18:39 +00:00
|
|
|
|