mirror of
https://github.com/ZLMediaKit/ZLMediaKit.git
synced 2024-11-22 10:40:05 +08:00
整理http相关代码
This commit is contained in:
parent
a548fcd709
commit
15edbeac3e
@ -104,7 +104,7 @@ API_EXPORT void API_CALL mk_http_requester_add_header(mk_http_requester ctx,cons
|
||||
API_EXPORT const char* API_CALL mk_http_requester_get_response_status(mk_http_requester ctx){
|
||||
assert(ctx);
|
||||
HttpRequester::Ptr *obj = (HttpRequester::Ptr *)ctx;
|
||||
return (*obj)->responseStatus().c_str();
|
||||
return (*obj)->response().Url().c_str();
|
||||
}
|
||||
|
||||
API_EXPORT const char* API_CALL mk_http_requester_get_response_header(mk_http_requester ctx,const char *key){
|
||||
@ -131,7 +131,7 @@ API_EXPORT mk_parser API_CALL mk_http_requester_get_response(mk_http_requester c
|
||||
API_EXPORT void API_CALL mk_http_requester_set_cb(mk_http_requester ctx,on_mk_http_requester_complete cb, void *user_data){
|
||||
assert(ctx && cb);
|
||||
HttpRequester::Ptr *obj = (HttpRequester::Ptr *)ctx;
|
||||
(*obj)->setOnResult([cb,user_data](const SockException &ex,const string &status,const StrCaseMap &header,const string &strRecvBody){
|
||||
(*obj)->setOnResult([cb, user_data](const SockException &ex, const Parser &res) {
|
||||
cb(user_data, ex.getErrCode(), ex.what());
|
||||
});
|
||||
}
|
||||
|
@ -71,22 +71,20 @@ onceToken token([](){
|
||||
|
||||
|
||||
static void parse_http_response(const SockException &ex,
|
||||
const string &status,
|
||||
const HttpClient::HttpHeader &header,
|
||||
const string &strRecvBody,
|
||||
const Parser &res,
|
||||
const function<void(const Value &,const string &)> &fun){
|
||||
if (ex) {
|
||||
auto errStr = StrPrinter << "[network err]:" << ex.what() << endl;
|
||||
fun(Json::nullValue, errStr);
|
||||
return;
|
||||
}
|
||||
if(status != "200"){
|
||||
auto errStr = StrPrinter << "[bad http status code]:" << status << endl;
|
||||
if (res.Url() != "200") {
|
||||
auto errStr = StrPrinter << "[bad http status code]:" << res.Url() << endl;
|
||||
fun(Json::nullValue, errStr);
|
||||
return;
|
||||
}
|
||||
try {
|
||||
stringstream ss(strRecvBody);
|
||||
stringstream ss(res.Content());
|
||||
Value result;
|
||||
ss >> result;
|
||||
if (result["code"].asInt() != 0) {
|
||||
@ -144,13 +142,11 @@ void do_http_hook(const string &url,const ArgsType &body,const function<void(con
|
||||
}
|
||||
std::shared_ptr<Ticker> pTicker(new Ticker);
|
||||
requester->startRequester(url, [url, func, bodyStr, requester, pTicker](const SockException &ex,
|
||||
const string &status,
|
||||
const HttpClient::HttpHeader &header,
|
||||
const string &strRecvBody) mutable{
|
||||
const Parser &res) mutable{
|
||||
onceToken token(nullptr, [&]() mutable{
|
||||
requester.reset();
|
||||
});
|
||||
parse_http_response(ex,status,header,strRecvBody,[&](const Value &obj,const string &err){
|
||||
parse_http_response(ex, res, [&](const Value &obj, const string &err) {
|
||||
if (func) {
|
||||
func(obj, err);
|
||||
}
|
||||
|
@ -119,12 +119,12 @@ const string &Parser::Params() const {
|
||||
return _params;
|
||||
}
|
||||
|
||||
void Parser::setUrl(const string &url) {
|
||||
this->_strUrl = url;
|
||||
void Parser::setUrl(string url) {
|
||||
this->_strUrl = std::move(url);
|
||||
}
|
||||
|
||||
void Parser::setContent(const string &content) {
|
||||
this->_strContent = content;
|
||||
void Parser::setContent(string content) {
|
||||
this->_strContent = std::move(content);
|
||||
}
|
||||
|
||||
StrCaseMap &Parser::getHeader() const {
|
||||
|
@ -14,6 +14,7 @@
|
||||
#include <map>
|
||||
#include <string>
|
||||
#include "Util/util.h"
|
||||
|
||||
using namespace std;
|
||||
using namespace toolkit;
|
||||
|
||||
@ -27,10 +28,9 @@ struct StrCaseCompare {
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
class StrCaseMap : public multimap<string, string, StrCaseCompare> {
|
||||
public:
|
||||
typedef multimap<string, string, StrCaseCompare> Super ;
|
||||
using Super = multimap<string, string, StrCaseCompare>;
|
||||
StrCaseMap() = default;
|
||||
~StrCaseMap() = default;
|
||||
|
||||
@ -62,34 +62,49 @@ class Parser {
|
||||
public:
|
||||
Parser();
|
||||
~Parser();
|
||||
|
||||
//解析信令
|
||||
void Parse(const char *buf);
|
||||
|
||||
//获取命令字
|
||||
const string &Method() const;
|
||||
|
||||
//获取中间url,不包含?后面的参数
|
||||
const string &Url() const;
|
||||
|
||||
//获取中间url,包含?后面的参数
|
||||
string FullUrl() const;
|
||||
|
||||
//获取命令协议名
|
||||
const string &Tail() const;
|
||||
|
||||
//根据header key名,获取请求header value值
|
||||
const string &operator[](const char *name) const;
|
||||
|
||||
//获取http body或sdp
|
||||
const string &Content() const;
|
||||
|
||||
//清空,为了重用
|
||||
void Clear();
|
||||
|
||||
//获取?后面的参数
|
||||
const string &Params() const;
|
||||
|
||||
//重新设置url
|
||||
void setUrl(const string &url);
|
||||
void setUrl(string url);
|
||||
|
||||
//重新设置content
|
||||
void setContent(const string &content);
|
||||
void setContent(string content);
|
||||
|
||||
//获取header列表
|
||||
StrCaseMap &getHeader() const;
|
||||
|
||||
//获取url参数列表
|
||||
StrCaseMap &getUrlArgs() const;
|
||||
|
||||
//解析?后面的参数
|
||||
static StrCaseMap parseArgs(const string &str, const char *pair_delim = "&", const char *key_delim = "=");
|
||||
|
||||
private:
|
||||
string _strMethod;
|
||||
string _strUrl;
|
||||
@ -101,7 +116,6 @@ private:
|
||||
mutable StrCaseMap _mapUrlArgs;
|
||||
};
|
||||
|
||||
|
||||
}//namespace mediakit
|
||||
|
||||
#endif //ZLMEDIAKIT_PARSER_H
|
||||
|
@ -23,8 +23,8 @@
|
||||
|
||||
namespace mediakit {
|
||||
|
||||
HttpStringBody::HttpStringBody(const string &str){
|
||||
_str = str;
|
||||
HttpStringBody::HttpStringBody(string str){
|
||||
_str = std::move(str);
|
||||
}
|
||||
|
||||
ssize_t HttpStringBody::remainSize() {
|
||||
@ -43,6 +43,7 @@ Buffer::Ptr HttpStringBody::readData(size_t size) {
|
||||
}
|
||||
|
||||
//////////////////////////////////////////////////////////////////
|
||||
|
||||
HttpFileBody::HttpFileBody(const string &filePath){
|
||||
std::shared_ptr<FILE> fp(fopen(filePath.data(), "rb"), [](FILE *fp) {
|
||||
if(fp){
|
||||
|
@ -68,7 +68,7 @@ public:
|
||||
class HttpStringBody : public HttpBody{
|
||||
public:
|
||||
typedef std::shared_ptr<HttpStringBody> Ptr;
|
||||
HttpStringBody(const string &str);
|
||||
HttpStringBody(string str);
|
||||
~HttpStringBody() override = default;
|
||||
|
||||
ssize_t remainSize() override;
|
||||
|
@ -14,13 +14,6 @@
|
||||
|
||||
namespace mediakit {
|
||||
|
||||
|
||||
HttpClient::HttpClient() {
|
||||
}
|
||||
|
||||
HttpClient::~HttpClient() {
|
||||
}
|
||||
|
||||
void HttpClient::sendRequest(const string &strUrl, float fTimeOutSec) {
|
||||
_aliveTicker.resetTime();
|
||||
_url = strUrl;
|
||||
@ -60,7 +53,8 @@ void HttpClient::sendRequest(const string &strUrl, float fTimeOutSec) {
|
||||
_header.emplace("Connection", "keep-alive");
|
||||
_header.emplace("Accept", "*/*");
|
||||
_header.emplace("Accept-Language", "zh-CN,zh;q=0.8");
|
||||
_header.emplace("User-Agent","Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/57.0.2987.133 Safari/537.36");
|
||||
_header.emplace("User-Agent",
|
||||
"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/57.0.2987.133 Safari/537.36");
|
||||
|
||||
if (_body && _body->remainSize()) {
|
||||
_header.emplace("Content-Length", to_string(_body->remainSize()));
|
||||
@ -82,9 +76,7 @@ void HttpClient::sendRequest(const string &strUrl, float fTimeOutSec) {
|
||||
_header.emplace("Cookie", printer);
|
||||
}
|
||||
|
||||
|
||||
if (!alive() || bChanged) {
|
||||
//InfoL << "reconnet:" << _lastHost;
|
||||
startConnect(host, port, fTimeOutSec);
|
||||
} else {
|
||||
SockException ex;
|
||||
@ -92,6 +84,51 @@ void HttpClient::sendRequest(const string &strUrl, float fTimeOutSec) {
|
||||
}
|
||||
}
|
||||
|
||||
void HttpClient::clear() {
|
||||
_header.clear();
|
||||
_body.reset();
|
||||
_method.clear();
|
||||
_path.clear();
|
||||
_parser.Clear();
|
||||
_recvedBodySize = 0;
|
||||
_totalBodySize = 0;
|
||||
_aliveTicker.resetTime();
|
||||
_chunkedSplitter.reset();
|
||||
HttpRequestSplitter::reset();
|
||||
}
|
||||
|
||||
void HttpClient::setMethod(string method) {
|
||||
_method = std::move(method);
|
||||
}
|
||||
|
||||
void HttpClient::setHeader(HttpHeader header) {
|
||||
_header = std::move(header);
|
||||
}
|
||||
|
||||
HttpClient &HttpClient::addHeader(string key, string val, bool force) {
|
||||
if (!force) {
|
||||
_header.emplace(std::move(key), std::move(val));
|
||||
} else {
|
||||
_header[std::move(key)] = std::move(val);
|
||||
}
|
||||
return *this;
|
||||
}
|
||||
|
||||
void HttpClient::setBody(string body) {
|
||||
_body.reset(new HttpStringBody(std::move(body)));
|
||||
}
|
||||
|
||||
void HttpClient::setBody(HttpBody::Ptr body) {
|
||||
_body = std::move(body);
|
||||
}
|
||||
|
||||
const Parser &HttpClient::response() const {
|
||||
return _parser;
|
||||
}
|
||||
|
||||
const string &HttpClient::getUrl() const {
|
||||
return _url;
|
||||
}
|
||||
|
||||
void HttpClient::onConnect(const SockException &ex) {
|
||||
_aliveTicker.resetTime();
|
||||
@ -285,6 +322,4 @@ void HttpClient::checkCookie(HttpClient::HttpHeader &headers) {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
} /* namespace mediakit */
|
||||
|
||||
|
@ -24,6 +24,7 @@
|
||||
#include "HttpChunkedSplitter.h"
|
||||
#include "strCoding.h"
|
||||
#include "HttpBody.h"
|
||||
|
||||
using namespace std;
|
||||
using namespace toolkit;
|
||||
|
||||
@ -31,8 +32,9 @@ namespace mediakit {
|
||||
|
||||
class HttpArgs : public map<string, variant, StrCaseCompare> {
|
||||
public:
|
||||
HttpArgs(){}
|
||||
virtual ~HttpArgs(){}
|
||||
HttpArgs() = default;
|
||||
~HttpArgs() = default;
|
||||
|
||||
string make() const {
|
||||
string ret;
|
||||
for (auto &pr : *this) {
|
||||
@ -50,58 +52,60 @@ public:
|
||||
|
||||
class HttpClient : public TcpClient, public HttpRequestSplitter {
|
||||
public:
|
||||
typedef StrCaseMap HttpHeader;
|
||||
typedef std::shared_ptr<HttpClient> Ptr;
|
||||
HttpClient();
|
||||
virtual ~HttpClient();
|
||||
using HttpHeader = StrCaseMap;
|
||||
using Ptr = std::shared_ptr<HttpClient>;
|
||||
|
||||
HttpClient() = default;
|
||||
~HttpClient() override = default;
|
||||
|
||||
/**
|
||||
* 发送http[s]请求
|
||||
* @param url 请求url
|
||||
* @param fTimeOutSec 超时时间
|
||||
*/
|
||||
virtual void sendRequest(const string &url, float fTimeOutSec);
|
||||
|
||||
virtual void clear(){
|
||||
_header.clear();
|
||||
_body.reset();
|
||||
_method.clear();
|
||||
_path.clear();
|
||||
_parser.Clear();
|
||||
_recvedBodySize = 0;
|
||||
_totalBodySize = 0;
|
||||
_aliveTicker.resetTime();
|
||||
_chunkedSplitter.reset();
|
||||
HttpRequestSplitter::reset();
|
||||
}
|
||||
/**
|
||||
* 重置对象
|
||||
*/
|
||||
virtual void clear();
|
||||
|
||||
void setMethod(const string &method){
|
||||
_method = method;
|
||||
}
|
||||
void setHeader(const HttpHeader &header){
|
||||
_header = header;
|
||||
}
|
||||
HttpClient & addHeader(const string &key,const string &val,bool force = false){
|
||||
if(!force){
|
||||
_header.emplace(key,val);
|
||||
}else{
|
||||
_header[key] = val;
|
||||
}
|
||||
return *this;
|
||||
}
|
||||
void setBody(const string &body){
|
||||
_body.reset(new HttpStringBody(body));
|
||||
}
|
||||
void setBody(const HttpBody::Ptr &body){
|
||||
_body = body;
|
||||
}
|
||||
const string &responseStatus() const{
|
||||
return _parser.Url();
|
||||
}
|
||||
const HttpHeader &responseHeader() const{
|
||||
return _parser.getHeader();
|
||||
}
|
||||
const Parser& response() const{
|
||||
return _parser;
|
||||
}
|
||||
/**
|
||||
* 设置http方法
|
||||
* @param method GET/POST等
|
||||
*/
|
||||
void setMethod(string method);
|
||||
|
||||
/**
|
||||
* 覆盖http头
|
||||
* @param header
|
||||
*/
|
||||
void setHeader(HttpHeader header);
|
||||
|
||||
HttpClient &addHeader(string key, string val, bool force = false);
|
||||
|
||||
/**
|
||||
* 设置http content
|
||||
* @param body http content
|
||||
*/
|
||||
void setBody(string body);
|
||||
|
||||
/**
|
||||
* 设置http content
|
||||
* @param body http content
|
||||
*/
|
||||
void setBody(HttpBody::Ptr body);
|
||||
|
||||
/**
|
||||
* 获取回复,在收到完整回复后有效
|
||||
*/
|
||||
const Parser &response() const;
|
||||
|
||||
/**
|
||||
* 获取请求url
|
||||
*/
|
||||
const string &getUrl() const;
|
||||
|
||||
const string &getUrl() const{
|
||||
return _url;
|
||||
}
|
||||
protected:
|
||||
/**
|
||||
* 收到http回复头
|
||||
@ -147,16 +151,17 @@ protected:
|
||||
*/
|
||||
virtual bool onRedirectUrl(const string &url, bool temporary) { return true; };
|
||||
|
||||
//HttpRequestSplitter override
|
||||
//// HttpRequestSplitter override ////
|
||||
ssize_t onRecvHeader(const char *data, size_t len) override;
|
||||
void onRecvContent(const char *data, size_t len) override;
|
||||
|
||||
protected:
|
||||
virtual void onConnect(const SockException &ex) override;
|
||||
virtual void onRecv(const Buffer::Ptr &pBuf) override;
|
||||
virtual void onErr(const SockException &ex) override;
|
||||
virtual void onFlush() override;
|
||||
virtual void onManager() override;
|
||||
//// TcpClient override ////
|
||||
void onConnect(const SockException &ex) override;
|
||||
void onRecv(const Buffer::Ptr &pBuf) override;
|
||||
void onErr(const SockException &ex) override;
|
||||
void onFlush() override;
|
||||
void onManager() override;
|
||||
|
||||
private:
|
||||
void onResponseCompleted_l();
|
||||
|
@ -12,13 +12,6 @@
|
||||
|
||||
namespace mediakit {
|
||||
|
||||
HttpRequester::HttpRequester(){
|
||||
|
||||
}
|
||||
HttpRequester::~HttpRequester(){
|
||||
|
||||
}
|
||||
|
||||
ssize_t HttpRequester::onResponseHeader(const string &status, const HttpHeader &headers) {
|
||||
_strRecvBody.clear();
|
||||
return HttpClientImp::onResponseHeader(status, headers);
|
||||
@ -29,16 +22,17 @@ void HttpRequester::onResponseBody(const char *buf,size_t size,size_t recvedSize
|
||||
}
|
||||
|
||||
void HttpRequester::onResponseCompleted() {
|
||||
const_cast<Parser &> (response()).setContent(std::move(_strRecvBody));
|
||||
if (_onResult) {
|
||||
_onResult(SockException(),responseStatus(),responseHeader(),_strRecvBody);
|
||||
_onResult(SockException(), response());
|
||||
_onResult = nullptr;
|
||||
}
|
||||
}
|
||||
|
||||
void HttpRequester::onDisconnect(const SockException &ex) {
|
||||
const_cast<Parser &> (response()).setContent(std::move(_strRecvBody));
|
||||
if (_onResult) {
|
||||
const_cast<Parser &>(response()).setContent(_strRecvBody);
|
||||
_onResult(ex,responseStatus(),responseHeader(),_strRecvBody);
|
||||
_onResult(ex, response());
|
||||
_onResult = nullptr;
|
||||
}
|
||||
}
|
||||
|
@ -15,21 +15,24 @@
|
||||
|
||||
namespace mediakit {
|
||||
|
||||
class HttpRequester : public HttpClientImp
|
||||
{
|
||||
class HttpRequester : public HttpClientImp {
|
||||
public:
|
||||
typedef std::shared_ptr<HttpRequester> Ptr;
|
||||
typedef std::function<void(const SockException &ex,const string &status,const HttpHeader &header,const string &strRecvBody)> HttpRequesterResult;
|
||||
HttpRequester();
|
||||
virtual ~HttpRequester();
|
||||
using Ptr = std::shared_ptr<HttpRequester>;
|
||||
using HttpRequesterResult = std::function<void(const SockException &ex, const Parser &response)>;
|
||||
|
||||
HttpRequester() = default;
|
||||
~HttpRequester() override = default;
|
||||
|
||||
void setOnResult(const HttpRequesterResult &onResult);
|
||||
void startRequester(const string &url, const HttpRequesterResult &onResult, float timeOutSecond = 10);
|
||||
void clear() override;
|
||||
|
||||
private:
|
||||
ssize_t onResponseHeader(const string &status, const HttpHeader &headers) override;
|
||||
void onResponseBody(const char *buf, size_t size, size_t recvedSize, size_t totalSize) override;
|
||||
void onResponseCompleted() override;
|
||||
void onDisconnect(const SockException &ex) override;
|
||||
|
||||
private:
|
||||
string _strRecvBody;
|
||||
HttpRequesterResult _onResult;
|
||||
|
@ -76,9 +76,7 @@ int main(int argc, char *argv[]) {
|
||||
//开启请求,该api会返回当前主机外网ip等信息
|
||||
requesterGet->startRequester("http://pv.sohu.com/cityjson?ie=utf-8",//url地址
|
||||
[](const SockException &ex, //网络相关的失败信息,如果为空就代表成功
|
||||
const string &status, //http回复的状态码,比如说200/404
|
||||
const HttpClient::HttpHeader &header, //http回复头
|
||||
const string &strRecvBody) { //http回复body
|
||||
const Parser &parser) { //http回复body
|
||||
DebugL << "=====================HttpRequester GET===========================";
|
||||
if (ex) {
|
||||
//网络相关的错误
|
||||
@ -86,12 +84,12 @@ int main(int argc, char *argv[]) {
|
||||
} else {
|
||||
//打印http回复信息
|
||||
_StrPrinter printer;
|
||||
for (auto &pr: header) {
|
||||
for (auto &pr: parser.getHeader()) {
|
||||
printer << pr.first << ":" << pr.second << "\r\n";
|
||||
}
|
||||
InfoL << "status:" << status << "\r\n"
|
||||
InfoL << "status:" << parser.Url() << "\r\n"
|
||||
<< "header:\r\n" << (printer << endl)
|
||||
<< "\r\nbody:" << strRecvBody;
|
||||
<< "\r\nbody:" << parser.Content();
|
||||
}
|
||||
});
|
||||
|
||||
@ -114,9 +112,7 @@ int main(int argc, char *argv[]) {
|
||||
//开启请求
|
||||
requesterPost->startRequester("http://fanyi.baidu.com/langdetect",//url地址
|
||||
[](const SockException &ex, //网络相关的失败信息,如果为空就代表成功
|
||||
const string &status, //http回复的状态码,比如说200/404
|
||||
const HttpClient::HttpHeader &header, //http回复头
|
||||
const string &strRecvBody) { //http回复body
|
||||
const Parser &parser) { //http回复body
|
||||
DebugL << "=====================HttpRequester POST==========================";
|
||||
if (ex) {
|
||||
//网络相关的错误
|
||||
@ -124,12 +120,12 @@ int main(int argc, char *argv[]) {
|
||||
} else {
|
||||
//打印http回复信息
|
||||
_StrPrinter printer;
|
||||
for (auto &pr: header) {
|
||||
for (auto &pr: parser.getHeader()) {
|
||||
printer << pr.first << ":" << pr.second << "\r\n";
|
||||
}
|
||||
InfoL << "status:" << status << "\r\n"
|
||||
InfoL << "status:" << parser.Url() << "\r\n"
|
||||
<< "header:\r\n" << (printer << endl)
|
||||
<< "\r\nbody:" << strRecvBody;
|
||||
<< "\r\nbody:" << parser.Content();
|
||||
}
|
||||
});
|
||||
|
||||
@ -153,9 +149,7 @@ int main(int argc, char *argv[]) {
|
||||
//开启请求
|
||||
requesterUploader->startRequester("http://fanyi.baidu.com/langdetect",//url地址
|
||||
[](const SockException &ex, //网络相关的失败信息,如果为空就代表成功
|
||||
const string &status, //http回复的状态码,比如说200/404
|
||||
const HttpClient::HttpHeader &header, //http回复头
|
||||
const string &strRecvBody) { //http回复body
|
||||
const Parser &parser) { //http回复body
|
||||
DebugL << "=====================HttpRequester Uploader==========================";
|
||||
if (ex) {
|
||||
//网络相关的错误
|
||||
@ -163,12 +157,12 @@ int main(int argc, char *argv[]) {
|
||||
} else {
|
||||
//打印http回复信息
|
||||
_StrPrinter printer;
|
||||
for (auto &pr: header) {
|
||||
for (auto &pr: parser.getHeader()) {
|
||||
printer << pr.first << ":" << pr.second << "\r\n";
|
||||
}
|
||||
InfoL << "status:" << status << "\r\n"
|
||||
InfoL << "status:" << parser.Url() << "\r\n"
|
||||
<< "header:\r\n" << (printer << endl)
|
||||
<< "\r\nbody:" << strRecvBody;
|
||||
<< "\r\nbody:" << parser.Content();
|
||||
}
|
||||
});
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user