2017-10-09 22:11:01 +08:00
|
|
|
|
/*
|
2020-04-04 20:30:09 +08:00
|
|
|
|
* Copyright (c) 2016 The ZLMediaKit project authors. All Rights Reserved.
|
2017-09-27 16:20:30 +08:00
|
|
|
|
*
|
2021-01-17 18:31:50 +08:00
|
|
|
|
* This file is part of ZLMediaKit(https://github.com/xia-chu/ZLMediaKit).
|
2017-09-27 16:20:30 +08:00
|
|
|
|
*
|
2020-04-04 20:30:09 +08:00
|
|
|
|
* Use of this source code is governed by MIT license that can be found in the
|
|
|
|
|
* LICENSE file in the root of the source tree. All contributing project authors
|
|
|
|
|
* may be found in the AUTHORS file in the root of the source tree.
|
2017-09-27 16:20:30 +08:00
|
|
|
|
*/
|
2017-05-05 18:02:54 +08:00
|
|
|
|
|
|
|
|
|
#ifndef Http_HttpClient_h
|
|
|
|
|
#define Http_HttpClient_h
|
|
|
|
|
|
2018-06-21 14:03:43 +08:00
|
|
|
|
#include <stdio.h>
|
2017-05-05 18:02:54 +08:00
|
|
|
|
#include <string.h>
|
|
|
|
|
#include <functional>
|
|
|
|
|
#include <memory>
|
|
|
|
|
#include "Util/util.h"
|
2019-06-28 16:48:02 +08:00
|
|
|
|
#include "Util/mini.h"
|
2017-05-05 18:02:54 +08:00
|
|
|
|
#include "Network/TcpClient.h"
|
2019-06-28 16:48:02 +08:00
|
|
|
|
#include "Common/Parser.h"
|
2018-09-23 21:10:17 +08:00
|
|
|
|
#include "HttpRequestSplitter.h"
|
2018-09-24 00:50:02 +08:00
|
|
|
|
#include "HttpCookie.h"
|
2018-11-13 23:59:06 +08:00
|
|
|
|
#include "HttpChunkedSplitter.h"
|
2019-05-20 16:26:04 +08:00
|
|
|
|
#include "strCoding.h"
|
2019-10-27 02:04:51 +08:00
|
|
|
|
#include "HttpBody.h"
|
2017-05-05 18:02:54 +08:00
|
|
|
|
using namespace std;
|
2018-10-24 17:17:55 +08:00
|
|
|
|
using namespace toolkit;
|
2017-05-05 18:02:54 +08:00
|
|
|
|
|
2018-10-24 17:17:55 +08:00
|
|
|
|
namespace mediakit {
|
2017-05-05 18:02:54 +08:00
|
|
|
|
|
2019-05-20 16:26:04 +08:00
|
|
|
|
class HttpArgs : public map<string, variant, StrCaseCompare> {
|
2017-05-05 18:02:54 +08:00
|
|
|
|
public:
|
|
|
|
|
HttpArgs(){}
|
|
|
|
|
virtual ~HttpArgs(){}
|
|
|
|
|
string make() const {
|
|
|
|
|
string ret;
|
|
|
|
|
for(auto &pr : *this){
|
|
|
|
|
ret.append(pr.first);
|
|
|
|
|
ret.append("=");
|
2019-05-27 12:13:27 +08:00
|
|
|
|
ret.append(strCoding::UrlEncode(pr.second));
|
2017-05-05 18:02:54 +08:00
|
|
|
|
ret.append("&");
|
|
|
|
|
}
|
|
|
|
|
if(ret.size()){
|
|
|
|
|
ret.pop_back();
|
|
|
|
|
}
|
|
|
|
|
return ret;
|
|
|
|
|
}
|
|
|
|
|
};
|
2018-06-21 14:03:43 +08:00
|
|
|
|
|
2020-05-17 18:00:37 +08:00
|
|
|
|
class HttpClient : public TcpClient , public HttpRequestSplitter{
|
2017-05-05 18:02:54 +08:00
|
|
|
|
public:
|
|
|
|
|
typedef StrCaseMap HttpHeader;
|
|
|
|
|
typedef std::shared_ptr<HttpClient> Ptr;
|
|
|
|
|
HttpClient();
|
|
|
|
|
virtual ~HttpClient();
|
2018-02-28 17:30:12 +08:00
|
|
|
|
virtual void sendRequest(const string &url,float fTimeOutSec);
|
2019-01-03 15:05:52 +08:00
|
|
|
|
|
|
|
|
|
virtual void clear(){
|
2017-05-05 18:02:54 +08:00
|
|
|
|
_header.clear();
|
2018-06-21 14:03:43 +08:00
|
|
|
|
_body.reset();
|
2017-05-05 18:02:54 +08:00
|
|
|
|
_method.clear();
|
|
|
|
|
_path.clear();
|
|
|
|
|
_parser.Clear();
|
2019-01-03 15:05:52 +08:00
|
|
|
|
_recvedBodySize = 0;
|
|
|
|
|
_totalBodySize = 0;
|
|
|
|
|
_aliveTicker.resetTime();
|
|
|
|
|
_chunkedSplitter.reset();
|
|
|
|
|
HttpRequestSplitter::reset();
|
2017-05-05 18:02:54 +08:00
|
|
|
|
}
|
2019-01-03 15:05:52 +08:00
|
|
|
|
|
2017-05-05 18:02:54 +08:00
|
|
|
|
void setMethod(const string &method){
|
|
|
|
|
_method = method;
|
|
|
|
|
}
|
|
|
|
|
void setHeader(const HttpHeader &header){
|
2017-06-09 16:05:33 +08:00
|
|
|
|
_header = header;
|
2017-05-05 18:02:54 +08:00
|
|
|
|
}
|
2018-06-21 14:03:43 +08:00
|
|
|
|
HttpClient & addHeader(const string &key,const string &val,bool force = false){
|
|
|
|
|
if(!force){
|
|
|
|
|
_header.emplace(key,val);
|
|
|
|
|
}else{
|
|
|
|
|
_header[key] = val;
|
|
|
|
|
}
|
|
|
|
|
return *this;
|
2017-05-05 18:02:54 +08:00
|
|
|
|
}
|
|
|
|
|
void setBody(const string &body){
|
2018-06-21 14:14:05 +08:00
|
|
|
|
_body.reset(new HttpStringBody(body));
|
2018-06-21 14:03:43 +08:00
|
|
|
|
}
|
|
|
|
|
void setBody(const HttpBody::Ptr &body){
|
2017-05-05 18:02:54 +08:00
|
|
|
|
_body = body;
|
|
|
|
|
}
|
2018-11-13 16:34:45 +08:00
|
|
|
|
const string &responseStatus() const{
|
2017-05-05 18:02:54 +08:00
|
|
|
|
return _parser.Url();
|
|
|
|
|
}
|
2018-11-13 16:34:45 +08:00
|
|
|
|
const HttpHeader &responseHeader() const{
|
2020-04-20 18:13:45 +08:00
|
|
|
|
return _parser.getHeader();
|
2017-05-05 18:02:54 +08:00
|
|
|
|
}
|
2018-11-13 16:34:45 +08:00
|
|
|
|
const Parser& response() const{
|
|
|
|
|
return _parser;
|
|
|
|
|
}
|
2019-07-01 18:35:26 +08:00
|
|
|
|
|
|
|
|
|
const string &getUrl() const{
|
|
|
|
|
return _url;
|
|
|
|
|
}
|
2017-05-05 18:02:54 +08:00
|
|
|
|
protected:
|
2018-09-23 21:10:17 +08:00
|
|
|
|
/**
|
|
|
|
|
* 收到http回复头
|
|
|
|
|
* @param status 状态码,譬如:200 OK
|
|
|
|
|
* @param headers http头
|
2018-11-13 22:50:43 +08:00
|
|
|
|
* @return 返回后续content的长度;-1:后续数据全是content;>=0:固定长度content
|
|
|
|
|
* 需要指出的是,在http头中带有Content-Length字段时,该返回值无效
|
2018-09-23 21:10:17 +08:00
|
|
|
|
*/
|
2021-01-17 18:31:50 +08:00
|
|
|
|
virtual size_t onResponseHeader(const string &status,const HttpHeader &headers){
|
2017-05-05 18:02:54 +08:00
|
|
|
|
DebugL << status;
|
2018-11-13 22:50:43 +08:00
|
|
|
|
//无Content-Length字段时默认后面全是content
|
|
|
|
|
return -1;
|
2017-05-05 18:02:54 +08:00
|
|
|
|
};
|
2018-09-23 21:10:17 +08:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 收到http conten数据
|
|
|
|
|
* @param buf 数据指针
|
|
|
|
|
* @param size 数据大小
|
|
|
|
|
* @param recvedSize 已收数据大小(包含本次数据大小),当其等于totalSize时将触发onResponseCompleted回调
|
|
|
|
|
* @param totalSize 总数据大小
|
|
|
|
|
*/
|
2021-01-17 18:31:50 +08:00
|
|
|
|
virtual void onResponseBody(const char *buf,size_t size,size_t recvedSize,size_t totalSize){
|
2017-05-05 18:02:54 +08:00
|
|
|
|
DebugL << size << " " << recvedSize << " " << totalSize;
|
|
|
|
|
};
|
2018-09-23 21:10:17 +08:00
|
|
|
|
|
|
|
|
|
/**
|
2018-11-13 17:59:12 +08:00
|
|
|
|
* 接收http回复完毕,
|
2018-09-23 21:10:17 +08:00
|
|
|
|
*/
|
2018-11-13 22:50:43 +08:00
|
|
|
|
virtual void onResponseCompleted(){
|
2020-03-20 11:51:24 +08:00
|
|
|
|
DebugL;
|
2017-05-05 18:02:54 +08:00
|
|
|
|
}
|
2018-09-23 21:10:17 +08:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* http链接断开回调
|
|
|
|
|
* @param ex 断开原因
|
|
|
|
|
*/
|
2017-05-05 18:02:54 +08:00
|
|
|
|
virtual void onDisconnect(const SockException &ex){}
|
2018-09-23 21:10:17 +08:00
|
|
|
|
|
2019-07-01 20:55:31 +08:00
|
|
|
|
/**
|
|
|
|
|
* 重定向事件
|
|
|
|
|
* @param url 重定向url
|
|
|
|
|
* @param temporary 是否为临时重定向
|
|
|
|
|
* @return 是否继续
|
|
|
|
|
*/
|
|
|
|
|
virtual bool onRedirectUrl(const string &url,bool temporary){ return true;};
|
|
|
|
|
|
2018-09-23 21:10:17 +08:00
|
|
|
|
//HttpRequestSplitter override
|
2021-01-17 18:31:50 +08:00
|
|
|
|
size_t onRecvHeader(const char *data,size_t len) override;
|
|
|
|
|
void onRecvContent(const char *data,size_t len) override;
|
|
|
|
|
|
2018-06-21 14:03:43 +08:00
|
|
|
|
protected:
|
2017-05-05 18:02:54 +08:00
|
|
|
|
virtual void onConnect(const SockException &ex) override;
|
2018-02-23 15:36:51 +08:00
|
|
|
|
virtual void onRecv(const Buffer::Ptr &pBuf) override;
|
2017-05-05 18:02:54 +08:00
|
|
|
|
virtual void onErr(const SockException &ex) override;
|
2019-05-29 18:08:50 +08:00
|
|
|
|
virtual void onFlush() override;
|
2018-06-21 14:03:43 +08:00
|
|
|
|
virtual void onManager() override;
|
2021-01-17 18:31:50 +08:00
|
|
|
|
|
2018-09-23 21:10:17 +08:00
|
|
|
|
private:
|
2018-11-13 22:50:43 +08:00
|
|
|
|
void onResponseCompleted_l();
|
2019-03-14 09:59:07 +08:00
|
|
|
|
void checkCookie(HttpHeader &headers );
|
2021-01-17 18:31:50 +08:00
|
|
|
|
|
2018-06-21 14:03:43 +08:00
|
|
|
|
protected:
|
|
|
|
|
bool _isHttps;
|
2021-01-17 18:31:50 +08:00
|
|
|
|
|
2018-06-21 14:03:43 +08:00
|
|
|
|
private:
|
2019-07-01 18:35:26 +08:00
|
|
|
|
string _url;
|
2017-05-05 18:02:54 +08:00
|
|
|
|
HttpHeader _header;
|
2018-06-21 14:03:43 +08:00
|
|
|
|
HttpBody::Ptr _body;
|
2017-05-05 18:02:54 +08:00
|
|
|
|
string _method;
|
|
|
|
|
string _path;
|
|
|
|
|
//recv
|
2021-01-17 18:31:50 +08:00
|
|
|
|
size_t _recvedBodySize;
|
|
|
|
|
size_t _totalBodySize;
|
2017-05-05 18:02:54 +08:00
|
|
|
|
Parser _parser;
|
|
|
|
|
string _lastHost;
|
2018-06-21 14:03:43 +08:00
|
|
|
|
Ticker _aliveTicker;
|
|
|
|
|
float _fTimeOutSec = 0;
|
2018-11-13 23:59:06 +08:00
|
|
|
|
std::shared_ptr<HttpChunkedSplitter> _chunkedSplitter;
|
2017-05-05 18:02:54 +08:00
|
|
|
|
};
|
|
|
|
|
|
2018-10-24 17:17:55 +08:00
|
|
|
|
} /* namespace mediakit */
|
2017-05-05 18:02:54 +08:00
|
|
|
|
|
|
|
|
|
#endif /* Http_HttpClient_h */
|