2017-10-09 22:11:01 +08:00
|
|
|
|
/*
|
2017-09-27 16:20:30 +08:00
|
|
|
|
* MIT License
|
|
|
|
|
*
|
|
|
|
|
* Copyright (c) 2016 xiongziliang <771730766@qq.com>
|
|
|
|
|
*
|
|
|
|
|
* This file is part of ZLMediaKit(https://github.com/xiongziliang/ZLMediaKit).
|
|
|
|
|
*
|
|
|
|
|
* 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.
|
|
|
|
|
*/
|
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 "Rtsp/Rtsp.h"
|
|
|
|
|
#include "Util/util.h"
|
|
|
|
|
#include "Network/TcpClient.h"
|
2018-09-23 21:10:17 +08:00
|
|
|
|
#include "HttpRequestSplitter.h"
|
2018-09-24 00:50:02 +08:00
|
|
|
|
#include "HttpCookie.h"
|
2017-05-05 18:02:54 +08:00
|
|
|
|
|
|
|
|
|
using namespace std;
|
|
|
|
|
using namespace ZL::Util;
|
|
|
|
|
using namespace ZL::Network;
|
|
|
|
|
|
|
|
|
|
namespace ZL {
|
|
|
|
|
namespace Http {
|
|
|
|
|
|
2018-06-21 14:03:43 +08:00
|
|
|
|
class HttpArgs : public StrCaseMap {
|
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("=");
|
|
|
|
|
ret.append(pr.second);
|
|
|
|
|
ret.append("&");
|
|
|
|
|
}
|
|
|
|
|
if(ret.size()){
|
|
|
|
|
ret.pop_back();
|
|
|
|
|
}
|
|
|
|
|
return ret;
|
|
|
|
|
}
|
|
|
|
|
};
|
2018-06-21 14:03:43 +08:00
|
|
|
|
|
|
|
|
|
class HttpBody{
|
|
|
|
|
public:
|
|
|
|
|
typedef std::shared_ptr<HttpBody> Ptr;
|
|
|
|
|
HttpBody(){}
|
|
|
|
|
virtual ~HttpBody(){}
|
|
|
|
|
//剩余数据大小
|
|
|
|
|
virtual uint64_t remainSize() = 0;
|
|
|
|
|
virtual Buffer::Ptr readData() = 0;
|
|
|
|
|
};
|
|
|
|
|
|
2018-06-21 14:14:05 +08:00
|
|
|
|
class HttpStringBody : public HttpBody{
|
2018-06-21 14:03:43 +08:00
|
|
|
|
public:
|
2018-06-21 14:14:05 +08:00
|
|
|
|
typedef std::shared_ptr<HttpStringBody> Ptr;
|
|
|
|
|
HttpStringBody(const string &str){
|
2018-06-21 14:03:43 +08:00
|
|
|
|
_str = str;
|
|
|
|
|
}
|
2018-06-21 14:14:05 +08:00
|
|
|
|
virtual ~HttpStringBody(){}
|
2018-06-21 14:03:43 +08:00
|
|
|
|
|
|
|
|
|
uint64_t remainSize() override {
|
|
|
|
|
return _str.size();
|
|
|
|
|
}
|
|
|
|
|
Buffer::Ptr readData() override {
|
|
|
|
|
auto ret = std::make_shared<BufferString>(_str);
|
|
|
|
|
_str.clear();
|
|
|
|
|
return ret;
|
|
|
|
|
}
|
|
|
|
|
private:
|
|
|
|
|
mutable string _str;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class HttpMultiFormBody : public HttpBody {
|
|
|
|
|
public:
|
|
|
|
|
typedef std::shared_ptr<HttpMultiFormBody> Ptr;
|
|
|
|
|
HttpMultiFormBody(const StrCaseMap &args,const string &filePath,const string &boundary,uint32_t sliceSize = 4 * 1024){
|
|
|
|
|
_fp = fopen(filePath.data(),"rb");
|
|
|
|
|
if(!_fp){
|
|
|
|
|
throw std::invalid_argument(StrPrinter << "打开文件失败:" << filePath << " " << get_uv_errmsg());
|
|
|
|
|
}
|
|
|
|
|
auto fileName = filePath;
|
|
|
|
|
auto pos = filePath.rfind('/');
|
|
|
|
|
if(pos != string::npos){
|
|
|
|
|
fileName = filePath.substr(pos + 1);
|
|
|
|
|
}
|
|
|
|
|
_bodyPrefix = multiFormBodyPrefix(args,boundary,fileName);
|
|
|
|
|
_bodySuffix = multiFormBodySuffix(boundary);
|
|
|
|
|
_totalSize = _bodyPrefix.size() + _bodySuffix.size() + fileSize(_fp);
|
|
|
|
|
_sliceSize = sliceSize;
|
|
|
|
|
}
|
|
|
|
|
virtual ~HttpMultiFormBody(){
|
|
|
|
|
fclose(_fp);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
uint64_t remainSize() override {
|
|
|
|
|
return _totalSize - _offset;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Buffer::Ptr readData() override{
|
|
|
|
|
if(_bodyPrefix.size()){
|
|
|
|
|
auto ret = std::make_shared<BufferString>(_bodyPrefix);
|
|
|
|
|
_offset += _bodyPrefix.size();
|
|
|
|
|
_bodyPrefix.clear();
|
|
|
|
|
return ret;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if(0 == feof(_fp)){
|
|
|
|
|
auto ret = std::make_shared<BufferRaw>(_sliceSize);
|
|
|
|
|
//读文件
|
|
|
|
|
int size;
|
|
|
|
|
do{
|
|
|
|
|
size = fread(ret->data(),1,_sliceSize,_fp);
|
|
|
|
|
}while(-1 == size && UV_EINTR == get_uv_error(false));
|
|
|
|
|
|
|
|
|
|
if(size == -1){
|
|
|
|
|
_offset = _totalSize;
|
|
|
|
|
WarnL << "fread failed:" << get_uv_errmsg();
|
|
|
|
|
return nullptr;
|
|
|
|
|
}
|
|
|
|
|
_offset += size;
|
|
|
|
|
ret->setSize(size);
|
|
|
|
|
return ret;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if(_bodySuffix.size()){
|
|
|
|
|
auto ret = std::make_shared<BufferString>(_bodySuffix);
|
|
|
|
|
_offset = _totalSize;
|
|
|
|
|
_bodySuffix.clear();
|
|
|
|
|
return ret;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return nullptr;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
static string multiFormBodyPrefix(const StrCaseMap &args,const string &boundary,const string &fileName){
|
|
|
|
|
string MPboundary = string("--") + boundary;
|
|
|
|
|
_StrPrinter body;
|
|
|
|
|
for(auto &pr : args){
|
|
|
|
|
body << MPboundary << "\r\n";
|
|
|
|
|
body << "Content-Disposition: form-data; name=\"" << pr.first << "\"\r\n\r\n";
|
|
|
|
|
body << pr.second << "\r\n";
|
|
|
|
|
}
|
|
|
|
|
body << MPboundary << "\r\n";
|
|
|
|
|
body << "Content-Disposition: form-data; name=\"" << "file" << "\";filename=\"" << fileName << "\"\r\n";
|
|
|
|
|
body << "Content-Type: application/octet-stream\r\n\r\n" ;
|
|
|
|
|
return body;
|
|
|
|
|
}
|
|
|
|
|
static string multiFormBodySuffix(const string &boundary){
|
|
|
|
|
string MPboundary = string("--") + boundary;
|
|
|
|
|
string endMPboundary = MPboundary + "--";
|
|
|
|
|
_StrPrinter body;
|
|
|
|
|
body << "\r\n" << endMPboundary;
|
|
|
|
|
return body;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static uint64_t fileSize(FILE *fp) {
|
|
|
|
|
auto current = ftell(fp);
|
|
|
|
|
fseek(fp,0L,SEEK_END); /* 定位到文件末尾 */
|
|
|
|
|
auto end = ftell(fp); /* 得到文件大小 */
|
|
|
|
|
fseek(fp,current,SEEK_SET);
|
|
|
|
|
return end - current;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static string multiFormContentType(const string &boundary){
|
|
|
|
|
return StrPrinter << "multipart/form-data; boundary=" << boundary;
|
|
|
|
|
}
|
|
|
|
|
private:
|
|
|
|
|
FILE *_fp;
|
|
|
|
|
string _bodyPrefix;
|
|
|
|
|
string _bodySuffix;
|
|
|
|
|
uint64_t _offset = 0;
|
|
|
|
|
uint64_t _totalSize;
|
|
|
|
|
uint32_t _sliceSize;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2018-09-23 21:10:17 +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);
|
2017-05-05 18:02:54 +08:00
|
|
|
|
void clear(){
|
|
|
|
|
_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();
|
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
const string &responseStatus(){
|
|
|
|
|
return _parser.Url();
|
|
|
|
|
}
|
|
|
|
|
const HttpHeader &responseHeader(){
|
|
|
|
|
return _parser.getValues();
|
|
|
|
|
}
|
|
|
|
|
protected:
|
2018-09-23 21:10:17 +08:00
|
|
|
|
/**
|
|
|
|
|
* 收到http回复头
|
|
|
|
|
* @param status 状态码,譬如:200 OK
|
|
|
|
|
* @param headers http头
|
|
|
|
|
*/
|
2017-05-05 18:02:54 +08:00
|
|
|
|
virtual void onResponseHeader(const string &status,const HttpHeader &headers){
|
|
|
|
|
DebugL << status;
|
|
|
|
|
};
|
2018-09-23 21:10:17 +08:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 收到http conten数据
|
|
|
|
|
* @param buf 数据指针
|
|
|
|
|
* @param size 数据大小
|
|
|
|
|
* @param recvedSize 已收数据大小(包含本次数据大小),当其等于totalSize时将触发onResponseCompleted回调
|
|
|
|
|
* @param totalSize 总数据大小
|
|
|
|
|
*/
|
2017-05-05 18:02:54 +08:00
|
|
|
|
virtual void onResponseBody(const char *buf,size_t size,size_t recvedSize,size_t totalSize){
|
|
|
|
|
DebugL << size << " " << recvedSize << " " << totalSize;
|
|
|
|
|
};
|
2018-09-23 21:10:17 +08:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 接收http回复完毕
|
|
|
|
|
*/
|
2017-05-05 18:02:54 +08:00
|
|
|
|
virtual void onResponseCompleted(){
|
|
|
|
|
DebugL;
|
|
|
|
|
}
|
2018-09-23 21:10:17 +08:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 收到http回复数据回调
|
|
|
|
|
* @param data 数据指针
|
|
|
|
|
* @param size 数据大小
|
|
|
|
|
*/
|
2017-05-05 18:02:54 +08:00
|
|
|
|
virtual void onRecvBytes(const char *data,int size);
|
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
|
|
|
|
|
|
|
|
|
//HttpRequestSplitter override
|
|
|
|
|
int64_t onRecvHeader(const char *data,uint64_t len) override ;
|
|
|
|
|
void onRecvContent(const char *data,uint64_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;
|
2018-06-21 14:03:43 +08:00
|
|
|
|
virtual void onSend() override;
|
|
|
|
|
virtual void onManager() override;
|
2018-09-23 21:10:17 +08:00
|
|
|
|
private:
|
|
|
|
|
void onResponseCompleted_l();
|
2018-09-24 00:50:02 +08:00
|
|
|
|
void checkCookie(HttpHeader &headers );
|
2018-06-21 14:03:43 +08:00
|
|
|
|
protected:
|
|
|
|
|
bool _isHttps;
|
|
|
|
|
private:
|
2017-05-05 18:02:54 +08:00
|
|
|
|
//send
|
|
|
|
|
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
|
2018-06-21 14:03:43 +08:00
|
|
|
|
int64_t _recvedBodySize;
|
|
|
|
|
int64_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;
|
2017-05-05 18:02:54 +08:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
} /* namespace Http */
|
|
|
|
|
} /* namespace ZL */
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#endif /* Http_HttpClient_h */
|