2019-08-08 19:01:45 +08:00
|
|
|
|
/*
|
2020-04-04 20:30:09 +08:00
|
|
|
|
* Copyright (c) 2016 The ZLMediaKit project authors. All Rights Reserved.
|
|
|
|
|
*
|
2021-01-17 18:31:50 +08:00
|
|
|
|
* This file is part of ZLMediaKit(https://github.com/xia-chu/ZLMediaKit).
|
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-05-05 18:02:54 +08:00
|
|
|
|
|
2019-01-16 14:26:06 +08:00
|
|
|
|
#include <cstdlib>
|
2021-12-17 17:48:05 +08:00
|
|
|
|
#include "Util/base64.h"
|
2017-05-05 18:02:54 +08:00
|
|
|
|
#include "HttpClient.h"
|
2019-06-28 16:48:02 +08:00
|
|
|
|
#include "Common/config.h"
|
2017-05-05 18:02:54 +08:00
|
|
|
|
|
2022-02-02 20:34:50 +08:00
|
|
|
|
using namespace std;
|
|
|
|
|
using namespace toolkit;
|
|
|
|
|
|
2018-10-24 17:17:55 +08:00
|
|
|
|
namespace mediakit {
|
2017-05-05 18:02:54 +08:00
|
|
|
|
|
2022-01-19 22:50:44 +08:00
|
|
|
|
void HttpClient::sendRequest(const string &url) {
|
2021-12-24 11:22:17 +08:00
|
|
|
|
clearResponse();
|
|
|
|
|
_url = url;
|
|
|
|
|
auto protocol = FindField(url.data(), NULL, "://");
|
2022-05-08 16:33:33 +08:00
|
|
|
|
uint16_t port;
|
2021-12-24 11:22:17 +08:00
|
|
|
|
bool is_https;
|
2018-11-27 11:05:44 +08:00
|
|
|
|
if (strcasecmp(protocol.data(), "http") == 0) {
|
2022-05-08 16:33:33 +08:00
|
|
|
|
port = 80;
|
2021-12-24 11:22:17 +08:00
|
|
|
|
is_https = false;
|
2018-11-27 11:05:44 +08:00
|
|
|
|
} else if (strcasecmp(protocol.data(), "https") == 0) {
|
2022-05-08 16:33:33 +08:00
|
|
|
|
port = 443;
|
2021-12-24 11:22:17 +08:00
|
|
|
|
is_https = true;
|
2018-09-23 21:10:17 +08:00
|
|
|
|
} else {
|
2021-12-24 11:22:17 +08:00
|
|
|
|
auto strErr = StrPrinter << "非法的http url:" << url << endl;
|
2017-05-05 18:02:54 +08:00
|
|
|
|
throw std::invalid_argument(strErr);
|
|
|
|
|
}
|
2018-09-23 21:10:17 +08:00
|
|
|
|
|
2021-12-24 11:22:17 +08:00
|
|
|
|
auto host = FindField(url.data(), "://", "/");
|
2017-05-05 18:02:54 +08:00
|
|
|
|
if (host.empty()) {
|
2021-12-24 11:22:17 +08:00
|
|
|
|
host = FindField(url.data(), "://", NULL);
|
2017-05-05 18:02:54 +08:00
|
|
|
|
}
|
2021-12-24 11:22:17 +08:00
|
|
|
|
_path = FindField(url.data(), host.data(), NULL);
|
2017-05-05 18:02:54 +08:00
|
|
|
|
if (_path.empty()) {
|
|
|
|
|
_path = "/";
|
|
|
|
|
}
|
2022-01-11 10:44:49 +08:00
|
|
|
|
//重新设置header,防止上次请求的header干扰
|
|
|
|
|
_header = _user_set_header;
|
2021-12-17 17:48:05 +08:00
|
|
|
|
auto pos = host.find('@');
|
|
|
|
|
if (pos != string::npos) {
|
|
|
|
|
//去除?后面的字符串
|
|
|
|
|
auto authStr = host.substr(0, pos);
|
|
|
|
|
host = host.substr(pos + 1, host.size());
|
|
|
|
|
_header.emplace("Authorization", "Basic " + encodeBase64(authStr));
|
|
|
|
|
}
|
2020-11-21 22:16:58 +08:00
|
|
|
|
auto host_header = host;
|
2022-05-08 16:33:33 +08:00
|
|
|
|
splitUrl(host, host, port);
|
2020-11-21 22:16:58 +08:00
|
|
|
|
_header.emplace("Host", host_header);
|
2021-12-24 11:22:17 +08:00
|
|
|
|
_header.emplace("User-Agent", kServerName);
|
2019-06-13 18:39:57 +08:00
|
|
|
|
_header.emplace("Connection", "keep-alive");
|
|
|
|
|
_header.emplace("Accept", "*/*");
|
|
|
|
|
_header.emplace("Accept-Language", "zh-CN,zh;q=0.8");
|
2018-09-23 21:10:17 +08:00
|
|
|
|
|
|
|
|
|
if (_body && _body->remainSize()) {
|
2019-06-13 18:39:57 +08:00
|
|
|
|
_header.emplace("Content-Length", to_string(_body->remainSize()));
|
|
|
|
|
_header.emplace("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
|
2017-05-05 18:02:54 +08:00
|
|
|
|
}
|
|
|
|
|
|
2021-12-24 11:22:17 +08:00
|
|
|
|
bool host_changed = (_last_host != host + ":" + to_string(port)) || (_is_https != is_https);
|
|
|
|
|
_last_host = host + ":" + to_string(port);
|
|
|
|
|
_is_https = is_https;
|
2018-09-24 00:50:02 +08:00
|
|
|
|
|
2021-12-24 11:22:17 +08:00
|
|
|
|
auto cookies = HttpCookieStorage::Instance().get(_last_host, _path);
|
2018-09-24 00:50:02 +08:00
|
|
|
|
_StrPrinter printer;
|
2021-09-30 16:10:09 +08:00
|
|
|
|
for (auto &cookie : cookies) {
|
2018-09-24 00:50:02 +08:00
|
|
|
|
printer << cookie->getKey() << "=" << cookie->getVal() << ";";
|
|
|
|
|
}
|
2021-09-30 16:10:09 +08:00
|
|
|
|
if (!printer.empty()) {
|
2018-09-24 00:50:02 +08:00
|
|
|
|
printer.pop_back();
|
2019-06-13 18:39:57 +08:00
|
|
|
|
_header.emplace("Cookie", printer);
|
2018-09-24 00:50:02 +08:00
|
|
|
|
}
|
|
|
|
|
|
2021-12-24 11:22:17 +08:00
|
|
|
|
if (!alive() || host_changed) {
|
2022-01-19 22:50:44 +08:00
|
|
|
|
startConnect(host, port, _wait_header_ms);
|
2018-09-23 21:10:17 +08:00
|
|
|
|
} else {
|
2017-05-05 18:02:54 +08:00
|
|
|
|
SockException ex;
|
2021-12-22 11:33:40 +08:00
|
|
|
|
onConnect_l(ex);
|
2017-05-05 18:02:54 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-09-30 16:10:09 +08:00
|
|
|
|
void HttpClient::clear() {
|
2021-12-22 11:33:40 +08:00
|
|
|
|
_url.clear();
|
2022-01-11 10:44:49 +08:00
|
|
|
|
_user_set_header.clear();
|
2021-09-30 16:10:09 +08:00
|
|
|
|
_body.reset();
|
|
|
|
|
_method.clear();
|
2021-12-22 11:33:40 +08:00
|
|
|
|
clearResponse();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void HttpClient::clearResponse() {
|
2021-12-24 13:10:52 +08:00
|
|
|
|
_complete = false;
|
2022-01-19 22:50:44 +08:00
|
|
|
|
_header_recved = false;
|
2021-12-24 11:22:17 +08:00
|
|
|
|
_recved_body_size = 0;
|
|
|
|
|
_total_body_size = 0;
|
2021-12-22 11:33:40 +08:00
|
|
|
|
_parser.Clear();
|
2021-12-24 11:22:17 +08:00
|
|
|
|
_chunked_splitter = nullptr;
|
2022-01-19 22:50:44 +08:00
|
|
|
|
_wait_header.resetTime();
|
|
|
|
|
_wait_body.resetTime();
|
|
|
|
|
_wait_complete.resetTime();
|
2021-09-30 16:10:09 +08:00
|
|
|
|
HttpRequestSplitter::reset();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void HttpClient::setMethod(string method) {
|
|
|
|
|
_method = std::move(method);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void HttpClient::setHeader(HttpHeader header) {
|
2022-01-11 10:44:49 +08:00
|
|
|
|
_user_set_header = std::move(header);
|
2021-09-30 16:10:09 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
HttpClient &HttpClient::addHeader(string key, string val, bool force) {
|
|
|
|
|
if (!force) {
|
2022-01-11 10:44:49 +08:00
|
|
|
|
_user_set_header.emplace(std::move(key), std::move(val));
|
2021-09-30 16:10:09 +08:00
|
|
|
|
} else {
|
2022-01-11 10:44:49 +08:00
|
|
|
|
_user_set_header[std::move(key)] = std::move(val);
|
2021-09-30 16:10:09 +08:00
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
2022-01-20 14:48:45 +08:00
|
|
|
|
ssize_t HttpClient::responseBodyTotalSize() const {
|
|
|
|
|
return _total_body_size;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
size_t HttpClient::responseBodySize() const {
|
|
|
|
|
return _recved_body_size;
|
|
|
|
|
}
|
|
|
|
|
|
2021-09-30 16:10:09 +08:00
|
|
|
|
const string &HttpClient::getUrl() const {
|
|
|
|
|
return _url;
|
|
|
|
|
}
|
2017-05-05 18:02:54 +08:00
|
|
|
|
|
2018-09-23 21:10:17 +08:00
|
|
|
|
void HttpClient::onConnect(const SockException &ex) {
|
2021-12-22 11:33:40 +08:00
|
|
|
|
onConnect_l(ex);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void HttpClient::onConnect_l(const SockException &ex) {
|
2018-09-23 21:10:17 +08:00
|
|
|
|
if (ex) {
|
2022-01-20 14:48:45 +08:00
|
|
|
|
onResponseCompleted_l(ex);
|
2018-09-23 21:10:17 +08:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2018-06-21 14:03:43 +08:00
|
|
|
|
_StrPrinter printer;
|
|
|
|
|
printer << _method + " " << _path + " HTTP/1.1\r\n";
|
2017-05-05 18:02:54 +08:00
|
|
|
|
for (auto &pr : _header) {
|
2018-09-23 21:10:17 +08:00
|
|
|
|
printer << pr.first + ": ";
|
2018-06-21 14:03:43 +08:00
|
|
|
|
printer << pr.second + "\r\n";
|
2017-05-05 18:02:54 +08:00
|
|
|
|
}
|
2022-01-19 22:50:44 +08:00
|
|
|
|
_header.clear();
|
|
|
|
|
_path.clear();
|
2020-04-23 17:50:12 +08:00
|
|
|
|
SockSender::send(printer << "\r\n");
|
2019-05-29 18:08:50 +08:00
|
|
|
|
onFlush();
|
2017-05-05 18:02:54 +08:00
|
|
|
|
}
|
2018-09-23 21:10:17 +08:00
|
|
|
|
|
|
|
|
|
void HttpClient::onRecv(const Buffer::Ptr &pBuf) {
|
2022-01-19 22:50:44 +08:00
|
|
|
|
_wait_body.resetTime();
|
2019-03-27 10:01:18 +08:00
|
|
|
|
HttpRequestSplitter::input(pBuf->data(), pBuf->size());
|
2017-05-05 18:02:54 +08:00
|
|
|
|
}
|
|
|
|
|
|
2023-04-28 22:03:16 +08:00
|
|
|
|
void HttpClient::onError(const SockException &ex) {
|
2022-01-20 14:48:45 +08:00
|
|
|
|
onResponseCompleted_l(ex);
|
2017-05-05 18:02:54 +08:00
|
|
|
|
}
|
|
|
|
|
|
2021-01-19 16:05:38 +08:00
|
|
|
|
ssize_t HttpClient::onRecvHeader(const char *data, size_t len) {
|
2018-09-23 21:10:17 +08:00
|
|
|
|
_parser.Parse(data);
|
2023-02-20 10:52:00 +08:00
|
|
|
|
if (_parser.Url() == "302" || _parser.Url() == "301" || _parser.Url() == "303") {
|
|
|
|
|
auto new_url = Parser::merge_url(_url, _parser["Location"]);
|
2021-12-24 11:22:17 +08:00
|
|
|
|
if (new_url.empty()) {
|
2022-01-20 14:48:45 +08:00
|
|
|
|
throw invalid_argument("未找到Location字段(跳转url)");
|
2019-07-01 09:53:58 +08:00
|
|
|
|
}
|
2021-12-24 11:22:17 +08:00
|
|
|
|
if (onRedirectUrl(new_url, _parser.Url() == "302")) {
|
2022-01-19 22:50:44 +08:00
|
|
|
|
HttpClient::sendRequest(new_url);
|
2019-07-01 20:55:31 +08:00
|
|
|
|
return 0;
|
|
|
|
|
}
|
2019-07-01 09:53:58 +08:00
|
|
|
|
}
|
|
|
|
|
|
2020-04-20 18:13:45 +08:00
|
|
|
|
checkCookie(_parser.getHeader());
|
2022-01-20 14:48:45 +08:00
|
|
|
|
onResponseHeader(_parser.Url(), _parser.getHeader());
|
2022-01-19 22:50:44 +08:00
|
|
|
|
_header_recved = true;
|
2018-09-23 21:19:24 +08:00
|
|
|
|
|
2021-09-30 16:10:09 +08:00
|
|
|
|
if (_parser["Transfer-Encoding"] == "chunked") {
|
2018-11-13 23:59:06 +08:00
|
|
|
|
//如果Transfer-Encoding字段等于chunked,则认为后续的content是不限制长度的
|
2021-12-24 11:22:17 +08:00
|
|
|
|
_total_body_size = -1;
|
|
|
|
|
_chunked_splitter = std::make_shared<HttpChunkedSplitter>([this](const char *data, size_t len) {
|
2021-09-30 16:10:09 +08:00
|
|
|
|
if (len > 0) {
|
2022-01-20 14:48:45 +08:00
|
|
|
|
_recved_body_size += len;
|
|
|
|
|
onResponseBody(data, len);
|
2021-09-30 16:10:09 +08:00
|
|
|
|
} else {
|
2022-01-20 14:48:45 +08:00
|
|
|
|
_total_body_size = _recved_body_size;
|
2023-02-20 10:52:00 +08:00
|
|
|
|
if (_recved_body_size > 0) {
|
|
|
|
|
onResponseCompleted_l(SockException(Err_success, "success"));
|
|
|
|
|
}else{
|
|
|
|
|
onResponseCompleted_l(SockException(Err_other, "no body"));
|
|
|
|
|
}
|
2018-11-13 23:59:06 +08:00
|
|
|
|
}
|
|
|
|
|
});
|
2022-01-20 14:48:45 +08:00
|
|
|
|
//后续为源源不断的body
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!_parser["Content-Length"].empty()) {
|
|
|
|
|
//有Content-Length字段时忽略onResponseHeader的返回值
|
|
|
|
|
_total_body_size = atoll(_parser["Content-Length"].data());
|
|
|
|
|
} else {
|
|
|
|
|
_total_body_size = -1;
|
2018-11-13 23:59:06 +08:00
|
|
|
|
}
|
|
|
|
|
|
2021-12-24 11:22:17 +08:00
|
|
|
|
if (_total_body_size == 0) {
|
2018-11-13 22:50:43 +08:00
|
|
|
|
//后续没content,本次http请求结束
|
2023-04-28 01:33:17 +08:00
|
|
|
|
onResponseCompleted_l(SockException(Err_success, "The request is successful but has no body"));
|
2018-09-23 21:19:24 +08:00
|
|
|
|
return 0;
|
|
|
|
|
}
|
2018-09-23 21:10:17 +08:00
|
|
|
|
|
2022-01-19 22:50:44 +08:00
|
|
|
|
//当_total_body_size != 0时到达这里,代表后续有content
|
|
|
|
|
//虽然我们在_total_body_size >0 时知道content的确切大小,
|
2018-09-23 21:10:17 +08:00
|
|
|
|
//但是由于我们没必要等content接收完毕才回调onRecvContent(因为这样浪费内存并且要多次拷贝数据)
|
|
|
|
|
//所以返回-1代表我们接下来分段接收content
|
2021-12-24 11:22:17 +08:00
|
|
|
|
_recved_body_size = 0;
|
2018-09-23 21:10:17 +08:00
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
2021-01-17 18:31:50 +08:00
|
|
|
|
void HttpClient::onRecvContent(const char *data, size_t len) {
|
2021-12-24 11:22:17 +08:00
|
|
|
|
if (_chunked_splitter) {
|
|
|
|
|
_chunked_splitter->input(data, len);
|
2018-11-13 23:59:06 +08:00
|
|
|
|
return;
|
|
|
|
|
}
|
2022-01-20 14:48:45 +08:00
|
|
|
|
_recved_body_size += len;
|
2021-12-24 11:22:17 +08:00
|
|
|
|
if (_total_body_size < 0) {
|
2022-01-20 14:48:45 +08:00
|
|
|
|
//不限长度的content
|
|
|
|
|
onResponseBody(data, len);
|
2018-11-13 22:50:43 +08:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//固定长度的content
|
2022-01-20 14:48:45 +08:00
|
|
|
|
if (_recved_body_size < (size_t) _total_body_size) {
|
2018-11-13 22:50:43 +08:00
|
|
|
|
//content还未接收完毕
|
2022-01-20 14:48:45 +08:00
|
|
|
|
onResponseBody(data, len);
|
2018-11-13 22:50:43 +08:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2022-01-20 14:48:45 +08:00
|
|
|
|
if (_recved_body_size == (size_t)_total_body_size) {
|
|
|
|
|
//content接收完毕
|
|
|
|
|
onResponseBody(data, len);
|
2023-04-28 01:33:17 +08:00
|
|
|
|
onResponseCompleted_l(SockException(Err_success, "completed"));
|
2022-01-20 14:48:45 +08:00
|
|
|
|
return;
|
2018-09-23 21:10:17 +08:00
|
|
|
|
}
|
2022-01-20 14:48:45 +08:00
|
|
|
|
|
|
|
|
|
//声明的content数据比真实的小,断开链接
|
|
|
|
|
onResponseBody(data, len);
|
|
|
|
|
throw invalid_argument("http response content size bigger than expected");
|
2017-05-05 18:02:54 +08:00
|
|
|
|
}
|
2018-06-21 14:03:43 +08:00
|
|
|
|
|
2019-05-29 18:08:50 +08:00
|
|
|
|
void HttpClient::onFlush() {
|
2021-12-24 11:22:17 +08:00
|
|
|
|
GET_CONFIG(uint32_t, send_buf_size, Http::kSendBufSize);
|
2018-09-23 21:10:17 +08:00
|
|
|
|
while (_body && _body->remainSize() && !isSocketBusy()) {
|
2021-12-24 11:22:17 +08:00
|
|
|
|
auto buffer = _body->readData(send_buf_size);
|
2018-09-23 21:10:17 +08:00
|
|
|
|
if (!buffer) {
|
2018-06-21 14:03:43 +08:00
|
|
|
|
//数据发送结束或读取数据异常
|
|
|
|
|
break;
|
|
|
|
|
}
|
2018-09-23 21:10:17 +08:00
|
|
|
|
if (send(buffer) <= 0) {
|
2018-06-21 14:03:43 +08:00
|
|
|
|
//发送数据失败,不需要回滚数据,因为发送前已经通过isSocketBusy()判断socket可写
|
|
|
|
|
//所以发送缓存区肯定未满,该buffer肯定已经写入socket
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void HttpClient::onManager() {
|
2022-01-19 22:50:44 +08:00
|
|
|
|
//onManager回调在连接中或已连接状态才会调用
|
|
|
|
|
|
|
|
|
|
if (_wait_complete_ms > 0) {
|
|
|
|
|
//设置了总超时时间
|
|
|
|
|
if (!_complete && _wait_complete.elapsedTime() > _wait_complete_ms) {
|
|
|
|
|
//等待http回复完毕超时
|
|
|
|
|
shutdown(SockException(Err_timeout, "wait http response complete timeout"));
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
return;
|
2018-06-21 14:03:43 +08:00
|
|
|
|
}
|
|
|
|
|
|
2022-01-19 22:50:44 +08:00
|
|
|
|
//未设置总超时时间
|
|
|
|
|
if (!_header_recved) {
|
|
|
|
|
//等待header中
|
|
|
|
|
if (_wait_header.elapsedTime() > _wait_header_ms) {
|
|
|
|
|
//等待header中超时
|
|
|
|
|
shutdown(SockException(Err_timeout, "wait http response header timeout"));
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
} else if (_wait_body_ms > 0 && _wait_body.elapsedTime() > _wait_body_ms) {
|
|
|
|
|
//等待body中,等待超时
|
|
|
|
|
shutdown(SockException(Err_timeout, "wait http response body timeout"));
|
|
|
|
|
return;
|
2018-06-21 14:03:43 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-01-20 14:48:45 +08:00
|
|
|
|
void HttpClient::onResponseCompleted_l(const SockException &ex) {
|
|
|
|
|
if (_complete) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
2021-12-24 13:10:52 +08:00
|
|
|
|
_complete = true;
|
2022-01-19 22:50:44 +08:00
|
|
|
|
_wait_complete.resetTime();
|
2022-01-20 14:48:45 +08:00
|
|
|
|
|
|
|
|
|
if (!ex) {
|
|
|
|
|
//确认无疑的成功
|
|
|
|
|
onResponseCompleted(ex);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
//可疑的失败
|
|
|
|
|
|
2022-01-24 10:48:15 +08:00
|
|
|
|
if (_total_body_size > 0 && _recved_body_size >= (size_t)_total_body_size) {
|
2022-01-20 14:48:45 +08:00
|
|
|
|
//回复header中有content-length信息,那么收到的body大于等于声明值则认为成功
|
2023-04-28 01:33:17 +08:00
|
|
|
|
onResponseCompleted(SockException(Err_success, "read body completed"));
|
2022-01-20 14:48:45 +08:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (_total_body_size == -1 && _recved_body_size > 0) {
|
|
|
|
|
//回复header中无content-length信息,那么收到一点body也认为成功
|
|
|
|
|
onResponseCompleted(SockException(Err_success, ex.what()));
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//确认无疑的失败
|
|
|
|
|
onResponseCompleted(ex);
|
2018-09-23 21:10:17 +08:00
|
|
|
|
}
|
|
|
|
|
|
2021-12-24 13:10:52 +08:00
|
|
|
|
bool HttpClient::waitResponse() const {
|
|
|
|
|
return !_complete && alive();
|
|
|
|
|
}
|
|
|
|
|
|
2022-01-19 22:50:44 +08:00
|
|
|
|
bool HttpClient::isHttps() const {
|
|
|
|
|
return _is_https;
|
|
|
|
|
}
|
|
|
|
|
|
2019-03-14 09:59:07 +08:00
|
|
|
|
void HttpClient::checkCookie(HttpClient::HttpHeader &headers) {
|
2018-09-24 00:50:02 +08:00
|
|
|
|
//Set-Cookie: IPTV_SERVER=8E03927B-CC8C-4389-BC00-31DBA7EC7B49;expires=Sun, Sep 23 2018 15:07:31 GMT;path=/index/api/
|
2021-09-30 16:10:09 +08:00
|
|
|
|
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, ";", "=");
|
2019-06-13 11:45:13 +08:00
|
|
|
|
HttpCookie::Ptr cookie = std::make_shared<HttpCookie>();
|
2021-12-24 11:22:17 +08:00
|
|
|
|
cookie->setHost(_last_host);
|
2019-06-13 11:45:13 +08:00
|
|
|
|
|
|
|
|
|
int index = 0;
|
|
|
|
|
auto arg_vec = split(it_set_cookie->second, ";");
|
|
|
|
|
for (string &key_val : arg_vec) {
|
2021-09-30 16:10:09 +08:00
|
|
|
|
auto key = FindField(key_val.data(), NULL, "=");
|
|
|
|
|
auto val = FindField(key_val.data(), "=", NULL);
|
2019-06-13 11:45:13 +08:00
|
|
|
|
|
2021-09-30 16:10:09 +08:00
|
|
|
|
if (index++ == 0) {
|
|
|
|
|
cookie->setKeyVal(key, val);
|
2019-06-13 11:45:13 +08:00
|
|
|
|
continue;
|
|
|
|
|
}
|
2018-09-25 09:26:23 +08:00
|
|
|
|
|
2021-09-30 16:10:09 +08:00
|
|
|
|
if (key == "path") {
|
2019-06-13 11:45:13 +08:00
|
|
|
|
cookie->setPath(val);
|
|
|
|
|
continue;
|
|
|
|
|
}
|
2019-03-14 09:59:07 +08:00
|
|
|
|
|
2021-09-30 16:10:09 +08:00
|
|
|
|
if (key == "expires") {
|
|
|
|
|
cookie->setExpires(val, headers["Date"]);
|
2019-06-13 11:45:13 +08:00
|
|
|
|
continue;
|
|
|
|
|
}
|
2019-03-14 09:59:07 +08:00
|
|
|
|
}
|
|
|
|
|
|
2021-09-30 16:10:09 +08:00
|
|
|
|
if (!(*cookie)) {
|
2019-06-13 11:45:13 +08:00
|
|
|
|
//无效的cookie
|
2019-03-14 09:59:07 +08:00
|
|
|
|
continue;
|
2018-09-24 00:50:02 +08:00
|
|
|
|
}
|
2019-06-13 11:45:13 +08:00
|
|
|
|
HttpCookieStorage::Instance().set(cookie);
|
2018-09-24 00:50:02 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-01-19 22:50:44 +08:00
|
|
|
|
void HttpClient::setHeaderTimeout(size_t timeout_ms) {
|
|
|
|
|
CHECK(timeout_ms > 0);
|
|
|
|
|
_wait_header_ms = timeout_ms;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void HttpClient::setBodyTimeout(size_t timeout_ms) {
|
|
|
|
|
_wait_body_ms = timeout_ms;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void HttpClient::setCompleteTimeout(size_t timeout_ms) {
|
|
|
|
|
_wait_complete_ms = timeout_ms;
|
|
|
|
|
}
|
|
|
|
|
|
2021-12-17 17:48:05 +08:00
|
|
|
|
} /* namespace mediakit */
|