mirror of
https://github.com/ZLMediaKit/ZLMediaKit.git
synced 2024-11-26 04:31:37 +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){
|
API_EXPORT const char* API_CALL mk_http_requester_get_response_status(mk_http_requester ctx){
|
||||||
assert(ctx);
|
assert(ctx);
|
||||||
HttpRequester::Ptr *obj = (HttpRequester::Ptr *)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){
|
API_EXPORT const char* API_CALL mk_http_requester_get_response_header(mk_http_requester ctx,const char *key){
|
||||||
@ -131,8 +131,8 @@ 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){
|
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);
|
assert(ctx && cb);
|
||||||
HttpRequester::Ptr *obj = (HttpRequester::Ptr *)ctx;
|
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());
|
cb(user_data, ex.getErrCode(), ex.what());
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -71,33 +71,31 @@ onceToken token([](){
|
|||||||
|
|
||||||
|
|
||||||
static void parse_http_response(const SockException &ex,
|
static void parse_http_response(const SockException &ex,
|
||||||
const string &status,
|
const Parser &res,
|
||||||
const HttpClient::HttpHeader &header,
|
|
||||||
const string &strRecvBody,
|
|
||||||
const function<void(const Value &,const string &)> &fun){
|
const function<void(const Value &,const string &)> &fun){
|
||||||
if(ex){
|
if (ex) {
|
||||||
auto errStr = StrPrinter << "[network err]:" << ex.what() << endl;
|
auto errStr = StrPrinter << "[network err]:" << ex.what() << endl;
|
||||||
fun(Json::nullValue,errStr);
|
fun(Json::nullValue, errStr);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if(status != "200"){
|
if (res.Url() != "200") {
|
||||||
auto errStr = StrPrinter << "[bad http status code]:" << status << endl;
|
auto errStr = StrPrinter << "[bad http status code]:" << res.Url() << endl;
|
||||||
fun(Json::nullValue,errStr);
|
fun(Json::nullValue, errStr);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
try {
|
try {
|
||||||
stringstream ss(strRecvBody);
|
stringstream ss(res.Content());
|
||||||
Value result;
|
Value result;
|
||||||
ss >> result;
|
ss >> result;
|
||||||
if(result["code"].asInt() != 0) {
|
if (result["code"].asInt() != 0) {
|
||||||
auto errStr = StrPrinter << "[json code]:" << "code=" << result["code"] << ",msg=" << result["msg"] << endl;
|
auto errStr = StrPrinter << "[json code]:" << "code=" << result["code"] << ",msg=" << result["msg"] << endl;
|
||||||
fun(Json::nullValue,errStr);
|
fun(Json::nullValue, errStr);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
fun(result,"");
|
fun(result, "");
|
||||||
}catch (std::exception &ex){
|
} catch (std::exception &ex) {
|
||||||
auto errStr = StrPrinter << "[parse json failed]:" << ex.what() << endl;
|
auto errStr = StrPrinter << "[parse json failed]:" << ex.what() << endl;
|
||||||
fun(Json::nullValue,errStr);
|
fun(Json::nullValue, errStr);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -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);
|
std::shared_ptr<Ticker> pTicker(new Ticker);
|
||||||
requester->startRequester(url, [url, func, bodyStr, requester, pTicker](const SockException &ex,
|
requester->startRequester(url, [url, func, bodyStr, requester, pTicker](const SockException &ex,
|
||||||
const string &status,
|
const Parser &res) mutable{
|
||||||
const HttpClient::HttpHeader &header,
|
|
||||||
const string &strRecvBody) mutable{
|
|
||||||
onceToken token(nullptr, [&]() mutable{
|
onceToken token(nullptr, [&]() mutable{
|
||||||
requester.reset();
|
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) {
|
if (func) {
|
||||||
func(obj, err);
|
func(obj, err);
|
||||||
}
|
}
|
||||||
|
@ -119,12 +119,12 @@ const string &Parser::Params() const {
|
|||||||
return _params;
|
return _params;
|
||||||
}
|
}
|
||||||
|
|
||||||
void Parser::setUrl(const string &url) {
|
void Parser::setUrl(string url) {
|
||||||
this->_strUrl = url;
|
this->_strUrl = std::move(url);
|
||||||
}
|
}
|
||||||
|
|
||||||
void Parser::setContent(const string &content) {
|
void Parser::setContent(string content) {
|
||||||
this->_strContent = content;
|
this->_strContent = std::move(content);
|
||||||
}
|
}
|
||||||
|
|
||||||
StrCaseMap &Parser::getHeader() const {
|
StrCaseMap &Parser::getHeader() const {
|
||||||
|
@ -14,10 +14,11 @@
|
|||||||
#include <map>
|
#include <map>
|
||||||
#include <string>
|
#include <string>
|
||||||
#include "Util/util.h"
|
#include "Util/util.h"
|
||||||
|
|
||||||
using namespace std;
|
using namespace std;
|
||||||
using namespace toolkit;
|
using namespace toolkit;
|
||||||
|
|
||||||
namespace mediakit{
|
namespace mediakit {
|
||||||
|
|
||||||
string FindField(const char *buf, const char *start, const char *end, size_t bufSize = 0);
|
string FindField(const char *buf, const char *start, const char *end, size_t bufSize = 0);
|
||||||
|
|
||||||
@ -27,33 +28,32 @@ struct StrCaseCompare {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
class StrCaseMap : public multimap<string, string, StrCaseCompare> {
|
||||||
class StrCaseMap : public multimap<string, string, StrCaseCompare>{
|
public:
|
||||||
public:
|
using Super = multimap<string, string, StrCaseCompare>;
|
||||||
typedef multimap<string, string, StrCaseCompare> Super ;
|
|
||||||
StrCaseMap() = default;
|
StrCaseMap() = default;
|
||||||
~StrCaseMap() = default;
|
~StrCaseMap() = default;
|
||||||
|
|
||||||
string &operator[](const string &k){
|
string &operator[](const string &k) {
|
||||||
auto it = find(k);
|
auto it = find(k);
|
||||||
if(it == end()){
|
if (it == end()) {
|
||||||
it = Super::emplace(k,"");
|
it = Super::emplace(k, "");
|
||||||
}
|
}
|
||||||
return it->second;
|
return it->second;
|
||||||
}
|
}
|
||||||
|
|
||||||
template <typename V>
|
template<typename V>
|
||||||
void emplace(const string &k, V &&v) {
|
void emplace(const string &k, V &&v) {
|
||||||
auto it = find(k);
|
auto it = find(k);
|
||||||
if(it != end()){
|
if (it != end()) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
Super::emplace(k,std::forward<V>(v));
|
Super::emplace(k, std::forward<V>(v));
|
||||||
}
|
}
|
||||||
|
|
||||||
template <typename V>
|
template<typename V>
|
||||||
void emplace_force(const string k , V &&v) {
|
void emplace_force(const string k, V &&v) {
|
||||||
Super::emplace(k,std::forward<V>(v));
|
Super::emplace(k, std::forward<V>(v));
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -62,34 +62,49 @@ class Parser {
|
|||||||
public:
|
public:
|
||||||
Parser();
|
Parser();
|
||||||
~Parser();
|
~Parser();
|
||||||
|
|
||||||
//解析信令
|
//解析信令
|
||||||
void Parse(const char *buf);
|
void Parse(const char *buf);
|
||||||
|
|
||||||
//获取命令字
|
//获取命令字
|
||||||
const string &Method() const;
|
const string &Method() const;
|
||||||
|
|
||||||
//获取中间url,不包含?后面的参数
|
//获取中间url,不包含?后面的参数
|
||||||
const string &Url() const;
|
const string &Url() const;
|
||||||
|
|
||||||
//获取中间url,包含?后面的参数
|
//获取中间url,包含?后面的参数
|
||||||
string FullUrl() const;
|
string FullUrl() const;
|
||||||
|
|
||||||
//获取命令协议名
|
//获取命令协议名
|
||||||
const string &Tail() const;
|
const string &Tail() const;
|
||||||
|
|
||||||
//根据header key名,获取请求header value值
|
//根据header key名,获取请求header value值
|
||||||
const string &operator[](const char *name) const;
|
const string &operator[](const char *name) const;
|
||||||
|
|
||||||
//获取http body或sdp
|
//获取http body或sdp
|
||||||
const string &Content() const;
|
const string &Content() const;
|
||||||
|
|
||||||
//清空,为了重用
|
//清空,为了重用
|
||||||
void Clear();
|
void Clear();
|
||||||
|
|
||||||
//获取?后面的参数
|
//获取?后面的参数
|
||||||
const string &Params() const;
|
const string &Params() const;
|
||||||
|
|
||||||
//重新设置url
|
//重新设置url
|
||||||
void setUrl(const string &url);
|
void setUrl(string url);
|
||||||
|
|
||||||
//重新设置content
|
//重新设置content
|
||||||
void setContent(const string &content);
|
void setContent(string content);
|
||||||
|
|
||||||
//获取header列表
|
//获取header列表
|
||||||
StrCaseMap &getHeader() const;
|
StrCaseMap &getHeader() const;
|
||||||
|
|
||||||
//获取url参数列表
|
//获取url参数列表
|
||||||
StrCaseMap &getUrlArgs() const;
|
StrCaseMap &getUrlArgs() const;
|
||||||
|
|
||||||
//解析?后面的参数
|
//解析?后面的参数
|
||||||
static StrCaseMap parseArgs(const string &str, const char *pair_delim = "&", const char *key_delim = "=");
|
static StrCaseMap parseArgs(const string &str, const char *pair_delim = "&", const char *key_delim = "=");
|
||||||
|
|
||||||
private:
|
private:
|
||||||
string _strMethod;
|
string _strMethod;
|
||||||
string _strUrl;
|
string _strUrl;
|
||||||
@ -101,7 +116,6 @@ private:
|
|||||||
mutable StrCaseMap _mapUrlArgs;
|
mutable StrCaseMap _mapUrlArgs;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
}//namespace mediakit
|
}//namespace mediakit
|
||||||
|
|
||||||
#endif //ZLMEDIAKIT_PARSER_H
|
#endif //ZLMEDIAKIT_PARSER_H
|
||||||
|
@ -23,8 +23,8 @@
|
|||||||
|
|
||||||
namespace mediakit {
|
namespace mediakit {
|
||||||
|
|
||||||
HttpStringBody::HttpStringBody(const string &str){
|
HttpStringBody::HttpStringBody(string str){
|
||||||
_str = str;
|
_str = std::move(str);
|
||||||
}
|
}
|
||||||
|
|
||||||
ssize_t HttpStringBody::remainSize() {
|
ssize_t HttpStringBody::remainSize() {
|
||||||
@ -43,6 +43,7 @@ Buffer::Ptr HttpStringBody::readData(size_t size) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
//////////////////////////////////////////////////////////////////
|
//////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
HttpFileBody::HttpFileBody(const string &filePath){
|
HttpFileBody::HttpFileBody(const string &filePath){
|
||||||
std::shared_ptr<FILE> fp(fopen(filePath.data(), "rb"), [](FILE *fp) {
|
std::shared_ptr<FILE> fp(fopen(filePath.data(), "rb"), [](FILE *fp) {
|
||||||
if(fp){
|
if(fp){
|
||||||
|
@ -68,7 +68,7 @@ public:
|
|||||||
class HttpStringBody : public HttpBody{
|
class HttpStringBody : public HttpBody{
|
||||||
public:
|
public:
|
||||||
typedef std::shared_ptr<HttpStringBody> Ptr;
|
typedef std::shared_ptr<HttpStringBody> Ptr;
|
||||||
HttpStringBody(const string &str);
|
HttpStringBody(string str);
|
||||||
~HttpStringBody() override = default;
|
~HttpStringBody() override = default;
|
||||||
|
|
||||||
ssize_t remainSize() override;
|
ssize_t remainSize() override;
|
||||||
|
@ -14,13 +14,6 @@
|
|||||||
|
|
||||||
namespace mediakit {
|
namespace mediakit {
|
||||||
|
|
||||||
|
|
||||||
HttpClient::HttpClient() {
|
|
||||||
}
|
|
||||||
|
|
||||||
HttpClient::~HttpClient() {
|
|
||||||
}
|
|
||||||
|
|
||||||
void HttpClient::sendRequest(const string &strUrl, float fTimeOutSec) {
|
void HttpClient::sendRequest(const string &strUrl, float fTimeOutSec) {
|
||||||
_aliveTicker.resetTime();
|
_aliveTicker.resetTime();
|
||||||
_url = strUrl;
|
_url = strUrl;
|
||||||
@ -60,7 +53,8 @@ void HttpClient::sendRequest(const string &strUrl, float fTimeOutSec) {
|
|||||||
_header.emplace("Connection", "keep-alive");
|
_header.emplace("Connection", "keep-alive");
|
||||||
_header.emplace("Accept", "*/*");
|
_header.emplace("Accept", "*/*");
|
||||||
_header.emplace("Accept-Language", "zh-CN,zh;q=0.8");
|
_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()) {
|
if (_body && _body->remainSize()) {
|
||||||
_header.emplace("Content-Length", to_string(_body->remainSize()));
|
_header.emplace("Content-Length", to_string(_body->remainSize()));
|
||||||
@ -72,19 +66,17 @@ void HttpClient::sendRequest(const string &strUrl, float fTimeOutSec) {
|
|||||||
_isHttps = isHttps;
|
_isHttps = isHttps;
|
||||||
_fTimeOutSec = fTimeOutSec;
|
_fTimeOutSec = fTimeOutSec;
|
||||||
|
|
||||||
auto cookies = HttpCookieStorage::Instance().get(_lastHost,_path);
|
auto cookies = HttpCookieStorage::Instance().get(_lastHost, _path);
|
||||||
_StrPrinter printer;
|
_StrPrinter printer;
|
||||||
for(auto &cookie : cookies){
|
for (auto &cookie : cookies) {
|
||||||
printer << cookie->getKey() << "=" << cookie->getVal() << ";";
|
printer << cookie->getKey() << "=" << cookie->getVal() << ";";
|
||||||
}
|
}
|
||||||
if(!printer.empty()){
|
if (!printer.empty()) {
|
||||||
printer.pop_back();
|
printer.pop_back();
|
||||||
_header.emplace("Cookie", printer);
|
_header.emplace("Cookie", printer);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
if (!alive() || bChanged) {
|
if (!alive() || bChanged) {
|
||||||
//InfoL << "reconnet:" << _lastHost;
|
|
||||||
startConnect(host, port, fTimeOutSec);
|
startConnect(host, port, fTimeOutSec);
|
||||||
} else {
|
} else {
|
||||||
SockException ex;
|
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) {
|
void HttpClient::onConnect(const SockException &ex) {
|
||||||
_aliveTicker.resetTime();
|
_aliveTicker.resetTime();
|
||||||
@ -131,16 +168,16 @@ void HttpClient::onErr(const SockException &ex) {
|
|||||||
|
|
||||||
ssize_t HttpClient::onRecvHeader(const char *data, size_t len) {
|
ssize_t HttpClient::onRecvHeader(const char *data, size_t len) {
|
||||||
_parser.Parse(data);
|
_parser.Parse(data);
|
||||||
if(_parser.Url() == "302" || _parser.Url() == "301"){
|
if (_parser.Url() == "302" || _parser.Url() == "301") {
|
||||||
auto newUrl = _parser["Location"];
|
auto newUrl = _parser["Location"];
|
||||||
if(newUrl.empty()){
|
if (newUrl.empty()) {
|
||||||
shutdown(SockException(Err_shutdown,"未找到Location字段(跳转url)"));
|
shutdown(SockException(Err_shutdown, "未找到Location字段(跳转url)"));
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
if(onRedirectUrl(newUrl,_parser.Url() == "302")){
|
if (onRedirectUrl(newUrl, _parser.Url() == "302")) {
|
||||||
HttpClient::clear();
|
HttpClient::clear();
|
||||||
setMethod("GET");
|
setMethod("GET");
|
||||||
HttpClient::sendRequest(newUrl,_fTimeOutSec);
|
HttpClient::sendRequest(newUrl, _fTimeOutSec);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -148,26 +185,26 @@ ssize_t HttpClient::onRecvHeader(const char *data, size_t len) {
|
|||||||
checkCookie(_parser.getHeader());
|
checkCookie(_parser.getHeader());
|
||||||
_totalBodySize = onResponseHeader(_parser.Url(), _parser.getHeader());
|
_totalBodySize = onResponseHeader(_parser.Url(), _parser.getHeader());
|
||||||
|
|
||||||
if(!_parser["Content-Length"].empty()){
|
if (!_parser["Content-Length"].empty()) {
|
||||||
//有Content-Length字段时忽略onResponseHeader的返回值
|
//有Content-Length字段时忽略onResponseHeader的返回值
|
||||||
_totalBodySize = atoll(_parser["Content-Length"].data());
|
_totalBodySize = atoll(_parser["Content-Length"].data());
|
||||||
}
|
}
|
||||||
|
|
||||||
if(_parser["Transfer-Encoding"] == "chunked"){
|
if (_parser["Transfer-Encoding"] == "chunked") {
|
||||||
//如果Transfer-Encoding字段等于chunked,则认为后续的content是不限制长度的
|
//如果Transfer-Encoding字段等于chunked,则认为后续的content是不限制长度的
|
||||||
_totalBodySize = -1;
|
_totalBodySize = -1;
|
||||||
_chunkedSplitter = std::make_shared<HttpChunkedSplitter>([this](const char *data,size_t len){
|
_chunkedSplitter = std::make_shared<HttpChunkedSplitter>([this](const char *data, size_t len) {
|
||||||
if(len > 0){
|
if (len > 0) {
|
||||||
auto recvedBodySize = _recvedBodySize + len;
|
auto recvedBodySize = _recvedBodySize + len;
|
||||||
onResponseBody(data, len, recvedBodySize, SIZE_MAX);
|
onResponseBody(data, len, recvedBodySize, SIZE_MAX);
|
||||||
_recvedBodySize = recvedBodySize;
|
_recvedBodySize = recvedBodySize;
|
||||||
}else{
|
} else {
|
||||||
onResponseCompleted_l();
|
onResponseCompleted_l();
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
if(_totalBodySize == 0){
|
if (_totalBodySize == 0) {
|
||||||
//后续没content,本次http请求结束
|
//后续没content,本次http请求结束
|
||||||
onResponseCompleted_l();
|
onResponseCompleted_l();
|
||||||
return 0;
|
return 0;
|
||||||
@ -182,12 +219,12 @@ ssize_t HttpClient::onRecvHeader(const char *data, size_t len) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void HttpClient::onRecvContent(const char *data, size_t len) {
|
void HttpClient::onRecvContent(const char *data, size_t len) {
|
||||||
if(_chunkedSplitter){
|
if (_chunkedSplitter) {
|
||||||
_chunkedSplitter->input(data,len);
|
_chunkedSplitter->input(data, len);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
auto recvedBodySize = _recvedBodySize + len;
|
auto recvedBodySize = _recvedBodySize + len;
|
||||||
if(_totalBodySize < 0){
|
if (_totalBodySize < 0) {
|
||||||
//不限长度的content,最大支持SIZE_MAX个字节
|
//不限长度的content,最大支持SIZE_MAX个字节
|
||||||
onResponseBody(data, len, recvedBodySize, SIZE_MAX);
|
onResponseBody(data, len, recvedBodySize, SIZE_MAX);
|
||||||
_recvedBodySize = recvedBodySize;
|
_recvedBodySize = recvedBodySize;
|
||||||
@ -195,7 +232,7 @@ void HttpClient::onRecvContent(const char *data, size_t len) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
//固定长度的content
|
//固定长度的content
|
||||||
if (recvedBodySize < (size_t)_totalBodySize ) {
|
if (recvedBodySize < (size_t) _totalBodySize) {
|
||||||
//content还未接收完毕
|
//content还未接收完毕
|
||||||
onResponseBody(data, len, recvedBodySize, _totalBodySize);
|
onResponseBody(data, len, recvedBodySize, _totalBodySize);
|
||||||
_recvedBodySize = recvedBodySize;
|
_recvedBodySize = recvedBodySize;
|
||||||
@ -204,9 +241,9 @@ void HttpClient::onRecvContent(const char *data, size_t len) {
|
|||||||
|
|
||||||
//content接收完毕
|
//content接收完毕
|
||||||
onResponseBody(data, _totalBodySize - _recvedBodySize, _totalBodySize, _totalBodySize);
|
onResponseBody(data, _totalBodySize - _recvedBodySize, _totalBodySize, _totalBodySize);
|
||||||
bool biggerThanExpected = recvedBodySize > (size_t)_totalBodySize;
|
bool biggerThanExpected = recvedBodySize > (size_t) _totalBodySize;
|
||||||
onResponseCompleted_l();
|
onResponseCompleted_l();
|
||||||
if(biggerThanExpected) {
|
if (biggerThanExpected) {
|
||||||
//声明的content数据比真实的小,那么我们只截取前面部分的并断开链接
|
//声明的content数据比真实的小,那么我们只截取前面部分的并断开链接
|
||||||
shutdown(SockException(Err_shutdown, "http response content size bigger than expected"));
|
shutdown(SockException(Err_shutdown, "http response content size bigger than expected"));
|
||||||
}
|
}
|
||||||
@ -214,7 +251,7 @@ void HttpClient::onRecvContent(const char *data, size_t len) {
|
|||||||
|
|
||||||
void HttpClient::onFlush() {
|
void HttpClient::onFlush() {
|
||||||
_aliveTicker.resetTime();
|
_aliveTicker.resetTime();
|
||||||
GET_CONFIG(uint32_t,sendBufSize,Http::kSendBufSize);
|
GET_CONFIG(uint32_t, sendBufSize, Http::kSendBufSize);
|
||||||
while (_body && _body->remainSize() && !isSocketBusy()) {
|
while (_body && _body->remainSize() && !isSocketBusy()) {
|
||||||
auto buffer = _body->readData(sendBufSize);
|
auto buffer = _body->readData(sendBufSize);
|
||||||
if (!buffer) {
|
if (!buffer) {
|
||||||
@ -250,34 +287,34 @@ void HttpClient::onResponseCompleted_l() {
|
|||||||
|
|
||||||
void HttpClient::checkCookie(HttpClient::HttpHeader &headers) {
|
void HttpClient::checkCookie(HttpClient::HttpHeader &headers) {
|
||||||
//Set-Cookie: IPTV_SERVER=8E03927B-CC8C-4389-BC00-31DBA7EC7B49;expires=Sun, Sep 23 2018 15:07:31 GMT;path=/index/api/
|
//Set-Cookie: IPTV_SERVER=8E03927B-CC8C-4389-BC00-31DBA7EC7B49;expires=Sun, Sep 23 2018 15:07:31 GMT;path=/index/api/
|
||||||
for(auto it_set_cookie = headers.find("Set-Cookie") ; it_set_cookie != headers.end() ; ++it_set_cookie ){
|
for (auto it_set_cookie = headers.find("Set-Cookie"); it_set_cookie != headers.end(); ++it_set_cookie) {
|
||||||
auto key_val = Parser::parseArgs(it_set_cookie->second,";","=");
|
auto key_val = Parser::parseArgs(it_set_cookie->second, ";", "=");
|
||||||
HttpCookie::Ptr cookie = std::make_shared<HttpCookie>();
|
HttpCookie::Ptr cookie = std::make_shared<HttpCookie>();
|
||||||
cookie->setHost(_lastHost);
|
cookie->setHost(_lastHost);
|
||||||
|
|
||||||
int index = 0;
|
int index = 0;
|
||||||
auto arg_vec = split(it_set_cookie->second, ";");
|
auto arg_vec = split(it_set_cookie->second, ";");
|
||||||
for (string &key_val : arg_vec) {
|
for (string &key_val : arg_vec) {
|
||||||
auto key = FindField(key_val.data(),NULL,"=");
|
auto key = FindField(key_val.data(), NULL, "=");
|
||||||
auto val = FindField(key_val.data(),"=", NULL);
|
auto val = FindField(key_val.data(), "=", NULL);
|
||||||
|
|
||||||
if(index++ == 0){
|
if (index++ == 0) {
|
||||||
cookie->setKeyVal(key,val);
|
cookie->setKeyVal(key, val);
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
if(key == "path") {
|
if (key == "path") {
|
||||||
cookie->setPath(val);
|
cookie->setPath(val);
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
if(key == "expires"){
|
if (key == "expires") {
|
||||||
cookie->setExpires(val,headers["Date"]);
|
cookie->setExpires(val, headers["Date"]);
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if(!(*cookie)){
|
if (!(*cookie)) {
|
||||||
//无效的cookie
|
//无效的cookie
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
@ -285,6 +322,4 @@ void HttpClient::checkCookie(HttpClient::HttpHeader &headers) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
} /* namespace mediakit */
|
} /* namespace mediakit */
|
||||||
|
|
||||||
|
@ -24,84 +24,88 @@
|
|||||||
#include "HttpChunkedSplitter.h"
|
#include "HttpChunkedSplitter.h"
|
||||||
#include "strCoding.h"
|
#include "strCoding.h"
|
||||||
#include "HttpBody.h"
|
#include "HttpBody.h"
|
||||||
|
|
||||||
using namespace std;
|
using namespace std;
|
||||||
using namespace toolkit;
|
using namespace toolkit;
|
||||||
|
|
||||||
namespace mediakit {
|
namespace mediakit {
|
||||||
|
|
||||||
class HttpArgs : public map<string, variant, StrCaseCompare> {
|
class HttpArgs : public map<string, variant, StrCaseCompare> {
|
||||||
public:
|
public:
|
||||||
HttpArgs(){}
|
HttpArgs() = default;
|
||||||
virtual ~HttpArgs(){}
|
~HttpArgs() = default;
|
||||||
|
|
||||||
string make() const {
|
string make() const {
|
||||||
string ret;
|
string ret;
|
||||||
for(auto &pr : *this){
|
for (auto &pr : *this) {
|
||||||
ret.append(pr.first);
|
ret.append(pr.first);
|
||||||
ret.append("=");
|
ret.append("=");
|
||||||
ret.append(strCoding::UrlEncode(pr.second));
|
ret.append(strCoding::UrlEncode(pr.second));
|
||||||
ret.append("&");
|
ret.append("&");
|
||||||
}
|
}
|
||||||
if(ret.size()){
|
if (ret.size()) {
|
||||||
ret.pop_back();
|
ret.pop_back();
|
||||||
}
|
}
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
class HttpClient : public TcpClient , public HttpRequestSplitter{
|
class HttpClient : public TcpClient, public HttpRequestSplitter {
|
||||||
public:
|
public:
|
||||||
typedef StrCaseMap HttpHeader;
|
using HttpHeader = StrCaseMap;
|
||||||
typedef std::shared_ptr<HttpClient> Ptr;
|
using Ptr = std::shared_ptr<HttpClient>;
|
||||||
HttpClient();
|
|
||||||
virtual ~HttpClient();
|
|
||||||
virtual void sendRequest(const string &url,float fTimeOutSec);
|
|
||||||
|
|
||||||
virtual void clear(){
|
HttpClient() = default;
|
||||||
_header.clear();
|
~HttpClient() override = default;
|
||||||
_body.reset();
|
|
||||||
_method.clear();
|
|
||||||
_path.clear();
|
|
||||||
_parser.Clear();
|
|
||||||
_recvedBodySize = 0;
|
|
||||||
_totalBodySize = 0;
|
|
||||||
_aliveTicker.resetTime();
|
|
||||||
_chunkedSplitter.reset();
|
|
||||||
HttpRequestSplitter::reset();
|
|
||||||
}
|
|
||||||
|
|
||||||
void setMethod(const string &method){
|
/**
|
||||||
_method = method;
|
* 发送http[s]请求
|
||||||
}
|
* @param url 请求url
|
||||||
void setHeader(const HttpHeader &header){
|
* @param fTimeOutSec 超时时间
|
||||||
_header = header;
|
*/
|
||||||
}
|
virtual void sendRequest(const string &url, float fTimeOutSec);
|
||||||
HttpClient & addHeader(const string &key,const string &val,bool force = false){
|
|
||||||
if(!force){
|
/**
|
||||||
_header.emplace(key,val);
|
* 重置对象
|
||||||
}else{
|
*/
|
||||||
_header[key] = val;
|
virtual void clear();
|
||||||
}
|
|
||||||
return *this;
|
/**
|
||||||
}
|
* 设置http方法
|
||||||
void setBody(const string &body){
|
* @param method GET/POST等
|
||||||
_body.reset(new HttpStringBody(body));
|
*/
|
||||||
}
|
void setMethod(string method);
|
||||||
void setBody(const HttpBody::Ptr &body){
|
|
||||||
_body = body;
|
/**
|
||||||
}
|
* 覆盖http头
|
||||||
const string &responseStatus() const{
|
* @param header
|
||||||
return _parser.Url();
|
*/
|
||||||
}
|
void setHeader(HttpHeader header);
|
||||||
const HttpHeader &responseHeader() const{
|
|
||||||
return _parser.getHeader();
|
HttpClient &addHeader(string key, string val, bool force = false);
|
||||||
}
|
|
||||||
const Parser& response() const{
|
/**
|
||||||
return _parser;
|
* 设置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:
|
protected:
|
||||||
/**
|
/**
|
||||||
* 收到http回复头
|
* 收到http回复头
|
||||||
@ -110,7 +114,7 @@ protected:
|
|||||||
* @return 返回后续content的长度;-1:后续数据全是content;>=0:固定长度content
|
* @return 返回后续content的长度;-1:后续数据全是content;>=0:固定长度content
|
||||||
* 需要指出的是,在http头中带有Content-Length字段时,该返回值无效
|
* 需要指出的是,在http头中带有Content-Length字段时,该返回值无效
|
||||||
*/
|
*/
|
||||||
virtual ssize_t onResponseHeader(const string &status,const HttpHeader &headers){
|
virtual ssize_t onResponseHeader(const string &status, const HttpHeader &headers) {
|
||||||
//无Content-Length字段时默认后面全是content
|
//无Content-Length字段时默认后面全是content
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
@ -129,7 +133,7 @@ protected:
|
|||||||
/**
|
/**
|
||||||
* 接收http回复完毕,
|
* 接收http回复完毕,
|
||||||
*/
|
*/
|
||||||
virtual void onResponseCompleted(){
|
virtual void onResponseCompleted() {
|
||||||
DebugL;
|
DebugL;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -137,7 +141,7 @@ protected:
|
|||||||
* http链接断开回调
|
* http链接断开回调
|
||||||
* @param ex 断开原因
|
* @param ex 断开原因
|
||||||
*/
|
*/
|
||||||
virtual void onDisconnect(const SockException &ex){}
|
virtual void onDisconnect(const SockException &ex) {}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 重定向事件
|
* 重定向事件
|
||||||
@ -145,22 +149,23 @@ protected:
|
|||||||
* @param temporary 是否为临时重定向
|
* @param temporary 是否为临时重定向
|
||||||
* @return 是否继续
|
* @return 是否继续
|
||||||
*/
|
*/
|
||||||
virtual bool onRedirectUrl(const string &url,bool temporary){ return true;};
|
virtual bool onRedirectUrl(const string &url, bool temporary) { return true; };
|
||||||
|
|
||||||
//HttpRequestSplitter override
|
//// HttpRequestSplitter override ////
|
||||||
ssize_t onRecvHeader(const char *data,size_t len) override;
|
ssize_t onRecvHeader(const char *data, size_t len) override;
|
||||||
void onRecvContent(const char *data,size_t len) override;
|
void onRecvContent(const char *data, size_t len) override;
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
virtual void onConnect(const SockException &ex) override;
|
//// TcpClient override ////
|
||||||
virtual void onRecv(const Buffer::Ptr &pBuf) override;
|
void onConnect(const SockException &ex) override;
|
||||||
virtual void onErr(const SockException &ex) override;
|
void onRecv(const Buffer::Ptr &pBuf) override;
|
||||||
virtual void onFlush() override;
|
void onErr(const SockException &ex) override;
|
||||||
virtual void onManager() override;
|
void onFlush() override;
|
||||||
|
void onManager() override;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
void onResponseCompleted_l();
|
void onResponseCompleted_l();
|
||||||
void checkCookie(HttpHeader &headers );
|
void checkCookie(HttpHeader &headers);
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
bool _isHttps;
|
bool _isHttps;
|
||||||
|
@ -10,42 +10,36 @@
|
|||||||
|
|
||||||
#include "HttpRequester.h"
|
#include "HttpRequester.h"
|
||||||
|
|
||||||
namespace mediakit{
|
namespace mediakit {
|
||||||
|
|
||||||
HttpRequester::HttpRequester(){
|
ssize_t HttpRequester::onResponseHeader(const string &status, const HttpHeader &headers) {
|
||||||
|
|
||||||
}
|
|
||||||
HttpRequester::~HttpRequester(){
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
ssize_t HttpRequester::onResponseHeader(const string &status,const HttpHeader &headers) {
|
|
||||||
_strRecvBody.clear();
|
_strRecvBody.clear();
|
||||||
return HttpClientImp::onResponseHeader(status, headers);
|
return HttpClientImp::onResponseHeader(status, headers);
|
||||||
}
|
}
|
||||||
|
|
||||||
void HttpRequester::onResponseBody(const char *buf,size_t size,size_t recvedSize,size_t totalSize) {
|
void HttpRequester::onResponseBody(const char *buf, size_t size, size_t recvedSize, size_t totalSize) {
|
||||||
_strRecvBody.append(buf,size);
|
_strRecvBody.append(buf, size);
|
||||||
}
|
}
|
||||||
|
|
||||||
void HttpRequester::onResponseCompleted() {
|
void HttpRequester::onResponseCompleted() {
|
||||||
if(_onResult){
|
const_cast<Parser &> (response()).setContent(std::move(_strRecvBody));
|
||||||
_onResult(SockException(),responseStatus(),responseHeader(),_strRecvBody);
|
if (_onResult) {
|
||||||
|
_onResult(SockException(), response());
|
||||||
_onResult = nullptr;
|
_onResult = nullptr;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void HttpRequester::onDisconnect(const SockException &ex){
|
void HttpRequester::onDisconnect(const SockException &ex) {
|
||||||
if(_onResult){
|
const_cast<Parser &> (response()).setContent(std::move(_strRecvBody));
|
||||||
const_cast<Parser &>(response()).setContent(_strRecvBody);
|
if (_onResult) {
|
||||||
_onResult(ex,responseStatus(),responseHeader(),_strRecvBody);
|
_onResult(ex, response());
|
||||||
_onResult = nullptr;
|
_onResult = nullptr;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void HttpRequester::startRequester(const string &url,const HttpRequesterResult &onResult , float timeOutSecond){
|
void HttpRequester::startRequester(const string &url, const HttpRequesterResult &onResult, float timeOutSecond) {
|
||||||
_onResult = onResult;
|
_onResult = onResult;
|
||||||
sendRequest(url,timeOutSecond);
|
sendRequest(url, timeOutSecond);
|
||||||
}
|
}
|
||||||
|
|
||||||
void HttpRequester::clear() {
|
void HttpRequester::clear() {
|
||||||
|
@ -13,23 +13,26 @@
|
|||||||
|
|
||||||
#include "HttpClientImp.h"
|
#include "HttpClientImp.h"
|
||||||
|
|
||||||
namespace mediakit{
|
namespace mediakit {
|
||||||
|
|
||||||
class HttpRequester : public HttpClientImp
|
class HttpRequester : public HttpClientImp {
|
||||||
{
|
|
||||||
public:
|
public:
|
||||||
typedef std::shared_ptr<HttpRequester> Ptr;
|
using Ptr = std::shared_ptr<HttpRequester>;
|
||||||
typedef std::function<void(const SockException &ex,const string &status,const HttpHeader &header,const string &strRecvBody)> HttpRequesterResult;
|
using HttpRequesterResult = std::function<void(const SockException &ex, const Parser &response)>;
|
||||||
HttpRequester();
|
|
||||||
virtual ~HttpRequester();
|
HttpRequester() = default;
|
||||||
|
~HttpRequester() override = default;
|
||||||
|
|
||||||
void setOnResult(const HttpRequesterResult &onResult);
|
void setOnResult(const HttpRequesterResult &onResult);
|
||||||
void startRequester(const string &url,const HttpRequesterResult &onResult,float timeOutSecond = 10);
|
void startRequester(const string &url, const HttpRequesterResult &onResult, float timeOutSecond = 10);
|
||||||
void clear() override ;
|
void clear() override;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
ssize_t onResponseHeader(const string &status,const HttpHeader &headers) override;
|
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 onResponseBody(const char *buf, size_t size, size_t recvedSize, size_t totalSize) override;
|
||||||
void onResponseCompleted() override;
|
void onResponseCompleted() override;
|
||||||
void onDisconnect(const SockException &ex) override;
|
void onDisconnect(const SockException &ex) override;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
string _strRecvBody;
|
string _strRecvBody;
|
||||||
HttpRequesterResult _onResult;
|
HttpRequesterResult _onResult;
|
||||||
|
@ -76,9 +76,7 @@ int main(int argc, char *argv[]) {
|
|||||||
//开启请求,该api会返回当前主机外网ip等信息
|
//开启请求,该api会返回当前主机外网ip等信息
|
||||||
requesterGet->startRequester("http://pv.sohu.com/cityjson?ie=utf-8",//url地址
|
requesterGet->startRequester("http://pv.sohu.com/cityjson?ie=utf-8",//url地址
|
||||||
[](const SockException &ex, //网络相关的失败信息,如果为空就代表成功
|
[](const SockException &ex, //网络相关的失败信息,如果为空就代表成功
|
||||||
const string &status, //http回复的状态码,比如说200/404
|
const Parser &parser) { //http回复body
|
||||||
const HttpClient::HttpHeader &header, //http回复头
|
|
||||||
const string &strRecvBody) { //http回复body
|
|
||||||
DebugL << "=====================HttpRequester GET===========================";
|
DebugL << "=====================HttpRequester GET===========================";
|
||||||
if (ex) {
|
if (ex) {
|
||||||
//网络相关的错误
|
//网络相关的错误
|
||||||
@ -86,12 +84,12 @@ int main(int argc, char *argv[]) {
|
|||||||
} else {
|
} else {
|
||||||
//打印http回复信息
|
//打印http回复信息
|
||||||
_StrPrinter printer;
|
_StrPrinter printer;
|
||||||
for (auto &pr: header) {
|
for (auto &pr: parser.getHeader()) {
|
||||||
printer << pr.first << ":" << pr.second << "\r\n";
|
printer << pr.first << ":" << pr.second << "\r\n";
|
||||||
}
|
}
|
||||||
InfoL << "status:" << status << "\r\n"
|
InfoL << "status:" << parser.Url() << "\r\n"
|
||||||
<< "header:\r\n" << (printer << endl)
|
<< "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地址
|
requesterPost->startRequester("http://fanyi.baidu.com/langdetect",//url地址
|
||||||
[](const SockException &ex, //网络相关的失败信息,如果为空就代表成功
|
[](const SockException &ex, //网络相关的失败信息,如果为空就代表成功
|
||||||
const string &status, //http回复的状态码,比如说200/404
|
const Parser &parser) { //http回复body
|
||||||
const HttpClient::HttpHeader &header, //http回复头
|
|
||||||
const string &strRecvBody) { //http回复body
|
|
||||||
DebugL << "=====================HttpRequester POST==========================";
|
DebugL << "=====================HttpRequester POST==========================";
|
||||||
if (ex) {
|
if (ex) {
|
||||||
//网络相关的错误
|
//网络相关的错误
|
||||||
@ -124,12 +120,12 @@ int main(int argc, char *argv[]) {
|
|||||||
} else {
|
} else {
|
||||||
//打印http回复信息
|
//打印http回复信息
|
||||||
_StrPrinter printer;
|
_StrPrinter printer;
|
||||||
for (auto &pr: header) {
|
for (auto &pr: parser.getHeader()) {
|
||||||
printer << pr.first << ":" << pr.second << "\r\n";
|
printer << pr.first << ":" << pr.second << "\r\n";
|
||||||
}
|
}
|
||||||
InfoL << "status:" << status << "\r\n"
|
InfoL << "status:" << parser.Url() << "\r\n"
|
||||||
<< "header:\r\n" << (printer << endl)
|
<< "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地址
|
requesterUploader->startRequester("http://fanyi.baidu.com/langdetect",//url地址
|
||||||
[](const SockException &ex, //网络相关的失败信息,如果为空就代表成功
|
[](const SockException &ex, //网络相关的失败信息,如果为空就代表成功
|
||||||
const string &status, //http回复的状态码,比如说200/404
|
const Parser &parser) { //http回复body
|
||||||
const HttpClient::HttpHeader &header, //http回复头
|
|
||||||
const string &strRecvBody) { //http回复body
|
|
||||||
DebugL << "=====================HttpRequester Uploader==========================";
|
DebugL << "=====================HttpRequester Uploader==========================";
|
||||||
if (ex) {
|
if (ex) {
|
||||||
//网络相关的错误
|
//网络相关的错误
|
||||||
@ -163,12 +157,12 @@ int main(int argc, char *argv[]) {
|
|||||||
} else {
|
} else {
|
||||||
//打印http回复信息
|
//打印http回复信息
|
||||||
_StrPrinter printer;
|
_StrPrinter printer;
|
||||||
for (auto &pr: header) {
|
for (auto &pr: parser.getHeader()) {
|
||||||
printer << pr.first << ":" << pr.second << "\r\n";
|
printer << pr.first << ":" << pr.second << "\r\n";
|
||||||
}
|
}
|
||||||
InfoL << "status:" << status << "\r\n"
|
InfoL << "status:" << parser.Url() << "\r\n"
|
||||||
<< "header:\r\n" << (printer << endl)
|
<< "header:\r\n" << (printer << endl)
|
||||||
<< "\r\nbody:" << strRecvBody;
|
<< "\r\nbody:" << parser.Content();
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user