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"
|
2021-09-30 16:10:09 +08:00
|
|
|
|
|
2018-10-24 17:17:55 +08:00
|
|
|
|
namespace mediakit {
|
2017-05-05 18:02:54 +08:00
|
|
|
|
|
2022-02-02 20:34:50 +08:00
|
|
|
|
class HttpArgs : public std::map<std::string, toolkit::variant, StrCaseCompare> {
|
2017-05-05 18:02:54 +08:00
|
|
|
|
public:
|
2021-09-30 16:10:09 +08:00
|
|
|
|
HttpArgs() = default;
|
|
|
|
|
~HttpArgs() = default;
|
|
|
|
|
|
2022-02-02 20:34:50 +08:00
|
|
|
|
std::string make() const {
|
|
|
|
|
std::string ret;
|
2021-09-30 16:10:09 +08:00
|
|
|
|
for (auto &pr : *this) {
|
2017-05-05 18:02:54 +08:00
|
|
|
|
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("&");
|
|
|
|
|
}
|
2021-09-30 16:10:09 +08:00
|
|
|
|
if (ret.size()) {
|
2017-05-05 18:02:54 +08:00
|
|
|
|
ret.pop_back();
|
|
|
|
|
}
|
|
|
|
|
return ret;
|
|
|
|
|
}
|
|
|
|
|
};
|
2018-06-21 14:03:43 +08:00
|
|
|
|
|
2022-02-02 20:34:50 +08:00
|
|
|
|
class HttpClient : public toolkit::TcpClient, public HttpRequestSplitter {
|
2017-05-05 18:02:54 +08:00
|
|
|
|
public:
|
2021-09-30 16:10:09 +08:00
|
|
|
|
using HttpHeader = StrCaseMap;
|
|
|
|
|
using Ptr = std::shared_ptr<HttpClient>;
|
2019-01-03 15:05:52 +08:00
|
|
|
|
|
2021-09-30 16:10:09 +08:00
|
|
|
|
HttpClient() = default;
|
|
|
|
|
~HttpClient() override = default;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 发送http[s]请求
|
|
|
|
|
* @param url 请求url
|
|
|
|
|
*/
|
2022-02-02 20:34:50 +08:00
|
|
|
|
virtual void sendRequest(const std::string &url);
|
2021-09-30 16:10:09 +08:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 重置对象
|
|
|
|
|
*/
|
|
|
|
|
virtual void clear();
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 设置http方法
|
|
|
|
|
* @param method GET/POST等
|
|
|
|
|
*/
|
2022-02-02 20:34:50 +08:00
|
|
|
|
void setMethod(std::string method);
|
2021-09-30 16:10:09 +08:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 覆盖http头
|
|
|
|
|
* @param header
|
|
|
|
|
*/
|
|
|
|
|
void setHeader(HttpHeader header);
|
|
|
|
|
|
2022-02-02 20:34:50 +08:00
|
|
|
|
HttpClient &addHeader(std::string key, std::string val, bool force = false);
|
2021-09-30 16:10:09 +08:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 设置http content
|
|
|
|
|
* @param body http content
|
|
|
|
|
*/
|
2022-02-02 20:34:50 +08:00
|
|
|
|
void setBody(std::string body);
|
2021-09-30 16:10:09 +08:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 设置http content
|
|
|
|
|
* @param body http content
|
|
|
|
|
*/
|
|
|
|
|
void setBody(HttpBody::Ptr body);
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 获取回复,在收到完整回复后有效
|
|
|
|
|
*/
|
|
|
|
|
const Parser &response() const;
|
|
|
|
|
|
2022-01-20 14:48:45 +08:00
|
|
|
|
/**
|
|
|
|
|
* 获取回复header声明的body大小
|
|
|
|
|
*/
|
|
|
|
|
ssize_t responseBodyTotalSize() const;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 获取已经下载body的大小
|
|
|
|
|
*/
|
|
|
|
|
size_t responseBodySize() const;
|
|
|
|
|
|
2021-09-30 16:10:09 +08:00
|
|
|
|
/**
|
|
|
|
|
* 获取请求url
|
|
|
|
|
*/
|
2022-02-02 20:34:50 +08:00
|
|
|
|
const std::string &getUrl() const;
|
2019-07-01 18:35:26 +08:00
|
|
|
|
|
2021-12-24 13:10:52 +08:00
|
|
|
|
/**
|
|
|
|
|
* 判断是否正在等待响应
|
|
|
|
|
*/
|
|
|
|
|
bool waitResponse() const;
|
|
|
|
|
|
2022-01-19 22:50:44 +08:00
|
|
|
|
/**
|
|
|
|
|
* 判断是否为https
|
|
|
|
|
*/
|
|
|
|
|
bool isHttps() const;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 设置从发起连接到接收header完毕的延时,默认10秒
|
|
|
|
|
* 此参数必须大于0
|
|
|
|
|
*/
|
|
|
|
|
void setHeaderTimeout(size_t timeout_ms);
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 设置接收body数据超时时间, 默认5秒
|
|
|
|
|
* 此参数可以用于处理超大body回复的超时问题
|
|
|
|
|
* 此参数可以等于0
|
|
|
|
|
*/
|
|
|
|
|
void setBodyTimeout(size_t timeout_ms);
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 设置整个链接超时超时时间, 默认0
|
|
|
|
|
* 该值设置不为0后,HeaderTimeout和BodyTimeout无效
|
|
|
|
|
*/
|
|
|
|
|
void setCompleteTimeout(size_t timeout_ms);
|
|
|
|
|
|
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头
|
|
|
|
|
*/
|
2022-02-02 20:34:50 +08:00
|
|
|
|
virtual void onResponseHeader(const std::string &status, const HttpHeader &headers) = 0;
|
2018-09-23 21:10:17 +08:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 收到http conten数据
|
|
|
|
|
* @param buf 数据指针
|
|
|
|
|
* @param size 数据大小
|
|
|
|
|
*/
|
2022-01-20 14:48:45 +08:00
|
|
|
|
virtual void onResponseBody(const char *buf, size_t size) = 0;
|
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
|
|
|
|
*/
|
2022-02-02 20:34:50 +08:00
|
|
|
|
virtual void onResponseCompleted(const toolkit::SockException &ex) = 0;
|
2018-09-23 21:10:17 +08:00
|
|
|
|
|
2019-07-01 20:55:31 +08:00
|
|
|
|
/**
|
|
|
|
|
* 重定向事件
|
|
|
|
|
* @param url 重定向url
|
|
|
|
|
* @param temporary 是否为临时重定向
|
|
|
|
|
* @return 是否继续
|
|
|
|
|
*/
|
2022-02-02 20:34:50 +08:00
|
|
|
|
virtual bool onRedirectUrl(const std::string &url, bool temporary) { return true; };
|
2019-07-01 20:55:31 +08:00
|
|
|
|
|
2022-01-20 14:48:45 +08:00
|
|
|
|
protected:
|
2021-09-30 16:10:09 +08:00
|
|
|
|
//// HttpRequestSplitter override ////
|
|
|
|
|
ssize_t onRecvHeader(const char *data, size_t len) override;
|
|
|
|
|
void onRecvContent(const char *data, size_t len) override;
|
2021-01-17 18:31:50 +08:00
|
|
|
|
|
2021-09-30 16:10:09 +08:00
|
|
|
|
//// TcpClient override ////
|
2022-02-02 20:34:50 +08:00
|
|
|
|
void onConnect(const toolkit::SockException &ex) override;
|
|
|
|
|
void onRecv(const toolkit::Buffer::Ptr &pBuf) override;
|
|
|
|
|
void onErr(const toolkit::SockException &ex) override;
|
2021-09-30 16:10:09 +08:00
|
|
|
|
void onFlush() override;
|
|
|
|
|
void onManager() override;
|
2021-01-17 18:31:50 +08:00
|
|
|
|
|
2018-09-23 21:10:17 +08:00
|
|
|
|
private:
|
2022-02-02 20:34:50 +08:00
|
|
|
|
void onResponseCompleted_l(const toolkit::SockException &ex);
|
|
|
|
|
void onConnect_l(const toolkit::SockException &ex);
|
2021-09-30 16:10:09 +08:00
|
|
|
|
void checkCookie(HttpHeader &headers);
|
2021-12-22 11:33:40 +08:00
|
|
|
|
void clearResponse();
|
2021-01-17 18:31:50 +08:00
|
|
|
|
|
2018-06-21 14:03:43 +08:00
|
|
|
|
private:
|
2022-01-19 22:50:44 +08:00
|
|
|
|
//for http response
|
2021-12-24 13:10:52 +08:00
|
|
|
|
bool _complete = false;
|
2022-01-19 22:50:44 +08:00
|
|
|
|
bool _header_recved = false;
|
|
|
|
|
size_t _recved_body_size;
|
|
|
|
|
ssize_t _total_body_size;
|
|
|
|
|
Parser _parser;
|
|
|
|
|
std::shared_ptr<HttpChunkedSplitter> _chunked_splitter;
|
|
|
|
|
|
|
|
|
|
//for request args
|
|
|
|
|
bool _is_https;
|
2022-02-02 20:34:50 +08:00
|
|
|
|
std::string _url;
|
2022-01-11 10:44:49 +08:00
|
|
|
|
HttpHeader _user_set_header;
|
2018-06-21 14:03:43 +08:00
|
|
|
|
HttpBody::Ptr _body;
|
2022-02-02 20:34:50 +08:00
|
|
|
|
std::string _method;
|
|
|
|
|
std::string _last_host;
|
2021-12-22 11:33:40 +08:00
|
|
|
|
|
2022-01-19 22:50:44 +08:00
|
|
|
|
//for this request
|
2022-02-02 20:34:50 +08:00
|
|
|
|
std::string _path;
|
2022-01-19 22:50:44 +08:00
|
|
|
|
HttpHeader _header;
|
|
|
|
|
|
|
|
|
|
//for timeout
|
|
|
|
|
size_t _wait_header_ms = 10 * 1000;
|
|
|
|
|
size_t _wait_body_ms = 10 * 1000;
|
|
|
|
|
size_t _wait_complete_ms = 0;
|
2022-02-02 20:34:50 +08:00
|
|
|
|
toolkit::Ticker _wait_header;
|
|
|
|
|
toolkit::Ticker _wait_body;
|
|
|
|
|
toolkit::Ticker _wait_complete;
|
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 */
|