代码润色
This commit is contained in:
parent
88dc59ec68
commit
98f7c37cd5
|
|
@ -8,13 +8,12 @@
|
||||||
* may be found in the AUTHORS file in the root of the source tree.
|
* may be found in the AUTHORS file in the root of the source tree.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#include "Parser.h"
|
|
||||||
#include "Common/macros.h"
|
|
||||||
#include "Network/sockutil.h"
|
|
||||||
#include "Util/base64.h"
|
|
||||||
#include "macros.h"
|
|
||||||
#include "strCoding.h"
|
|
||||||
#include <cinttypes>
|
#include <cinttypes>
|
||||||
|
#include "Parser.h"
|
||||||
|
#include "strCoding.h"
|
||||||
|
#include "Util/base64.h"
|
||||||
|
#include "Network/sockutil.h"
|
||||||
|
#include "Common/macros.h"
|
||||||
|
|
||||||
using namespace std;
|
using namespace std;
|
||||||
using namespace toolkit;
|
using namespace toolkit;
|
||||||
|
|
@ -327,16 +326,16 @@ void splitUrl(const std::string &url, std::string &host, uint16_t &port) {
|
||||||
checkHost(host);
|
checkHost(host);
|
||||||
}
|
}
|
||||||
|
|
||||||
void parseProxyUrl(const std::string &proxy_url, std::string &proxy_host, uint16_t &proxy_port, std::string &proxy_auth){
|
void parseProxyUrl(const std::string &proxy_url, std::string &proxy_host, uint16_t &proxy_port, std::string &proxy_auth) {
|
||||||
//判断是否包含http://, 如果是则去掉
|
// 判断是否包含http://, 如果是则去掉
|
||||||
std::string host;
|
std::string host;
|
||||||
auto pos = proxy_url.find("://");
|
auto pos = proxy_url.find("://");
|
||||||
if (pos != string::npos) {
|
if (pos != string::npos) {
|
||||||
host = proxy_url.substr(pos + 3);
|
host = proxy_url.substr(pos + 3);
|
||||||
}else{
|
} else {
|
||||||
host = proxy_url;
|
host = proxy_url;
|
||||||
}
|
}
|
||||||
//判断是否包含用户名和密码
|
// 判断是否包含用户名和密码
|
||||||
pos = host.rfind('@');
|
pos = host.rfind('@');
|
||||||
if (pos != string::npos) {
|
if (pos != string::npos) {
|
||||||
proxy_auth = encodeBase64(host.substr(0, pos));
|
proxy_auth = encodeBase64(host.substr(0, pos));
|
||||||
|
|
@ -344,6 +343,7 @@ void parseProxyUrl(const std::string &proxy_url, std::string &proxy_host, uint16
|
||||||
}
|
}
|
||||||
splitUrl(host, proxy_host, proxy_port);
|
splitUrl(host, proxy_host, proxy_port);
|
||||||
}
|
}
|
||||||
|
|
||||||
#if 0
|
#if 0
|
||||||
//测试代码
|
//测试代码
|
||||||
static onceToken token([](){
|
static onceToken token([](){
|
||||||
|
|
|
||||||
|
|
@ -163,7 +163,7 @@ void HttpClient::onConnect_l(const SockException &ex) {
|
||||||
}
|
}
|
||||||
_StrPrinter printer;
|
_StrPrinter printer;
|
||||||
//不使用代理或者代理服务器已经连接成功
|
//不使用代理或者代理服务器已经连接成功
|
||||||
if(_proxy_connected || !isUsedProxy()) {
|
if (_proxy_connected || !isUsedProxy()) {
|
||||||
printer << _method + " " << _path + " HTTP/1.1\r\n";
|
printer << _method + " " << _path + " HTTP/1.1\r\n";
|
||||||
for (auto &pr : _header) {
|
for (auto &pr : _header) {
|
||||||
printer << pr.first + ": ";
|
printer << pr.first + ": ";
|
||||||
|
|
@ -171,10 +171,10 @@ void HttpClient::onConnect_l(const SockException &ex) {
|
||||||
}
|
}
|
||||||
_header.clear();
|
_header.clear();
|
||||||
_path.clear();
|
_path.clear();
|
||||||
}else{
|
} else {
|
||||||
printer << "CONNECT "<< _last_host <<" HTTP/1.1\r\n";
|
printer << "CONNECT " << _last_host << " HTTP/1.1\r\n";
|
||||||
printer << "Proxy-Connection: keep-alive\r\n";
|
printer << "Proxy-Connection: keep-alive\r\n";
|
||||||
if(!_proxy_auth.empty()) {
|
if (!_proxy_auth.empty()) {
|
||||||
printer << "Proxy-Authorization: Basic " << _proxy_auth << "\r\n";
|
printer << "Proxy-Authorization: Basic " << _proxy_auth << "\r\n";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -411,9 +411,11 @@ void HttpClient::setBodyTimeout(size_t timeout_ms) {
|
||||||
void HttpClient::setCompleteTimeout(size_t timeout_ms) {
|
void HttpClient::setCompleteTimeout(size_t timeout_ms) {
|
||||||
_wait_complete_ms = timeout_ms;
|
_wait_complete_ms = timeout_ms;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool HttpClient::isUsedProxy() const {
|
bool HttpClient::isUsedProxy() const {
|
||||||
return _used_proxy;
|
return _used_proxy;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool HttpClient::isProxyConnected() const {
|
bool HttpClient::isProxyConnected() const {
|
||||||
return _proxy_connected;
|
return _proxy_connected;
|
||||||
}
|
}
|
||||||
|
|
@ -423,7 +425,7 @@ void HttpClient::setProxyUrl(const string &proxyUrl) {
|
||||||
if (!_proxy_url.empty()) {
|
if (!_proxy_url.empty()) {
|
||||||
parseProxyUrl(_proxy_url, _proxy_host, _proxy_port, _proxy_auth);
|
parseProxyUrl(_proxy_url, _proxy_host, _proxy_port, _proxy_auth);
|
||||||
_used_proxy = true;
|
_used_proxy = true;
|
||||||
}else{
|
} else {
|
||||||
_used_proxy = false;
|
_used_proxy = false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -434,5 +436,4 @@ bool HttpClient::checkProxyConnected(const char *data, size_t len) {
|
||||||
return _proxy_connected;
|
return _proxy_connected;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
} /* namespace mediakit */
|
} /* namespace mediakit */
|
||||||
|
|
|
||||||
|
|
@ -183,20 +183,17 @@ protected:
|
||||||
void onFlush() override;
|
void onFlush() override;
|
||||||
void onManager() override;
|
void onManager() override;
|
||||||
|
|
||||||
bool checkProxyConnected(const char *data, size_t len);
|
|
||||||
|
|
||||||
bool isUsedProxy() const;
|
|
||||||
|
|
||||||
bool isProxyConnected() const;
|
|
||||||
|
|
||||||
void clearResponse();
|
void clearResponse();
|
||||||
|
|
||||||
|
bool checkProxyConnected(const char *data, size_t len);
|
||||||
|
bool isUsedProxy() const;
|
||||||
|
bool isProxyConnected() const;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
void onResponseCompleted_l(const toolkit::SockException &ex);
|
void onResponseCompleted_l(const toolkit::SockException &ex);
|
||||||
void onConnect_l(const toolkit::SockException &ex);
|
void onConnect_l(const toolkit::SockException &ex);
|
||||||
void checkCookie(HttpHeader &headers);
|
void checkCookie(HttpHeader &headers);
|
||||||
|
|
||||||
|
|
||||||
private:
|
private:
|
||||||
//for http response
|
//for http response
|
||||||
bool _complete = false;
|
bool _complete = false;
|
||||||
|
|
@ -225,12 +222,13 @@ private:
|
||||||
toolkit::Ticker _wait_header;
|
toolkit::Ticker _wait_header;
|
||||||
toolkit::Ticker _wait_body;
|
toolkit::Ticker _wait_body;
|
||||||
toolkit::Ticker _wait_complete;
|
toolkit::Ticker _wait_complete;
|
||||||
|
|
||||||
|
bool _used_proxy = false;
|
||||||
|
bool _proxy_connected = false;
|
||||||
|
uint16_t _proxy_port;
|
||||||
std::string _proxy_url;
|
std::string _proxy_url;
|
||||||
std::string _proxy_host;
|
std::string _proxy_host;
|
||||||
std::string _proxy_auth;
|
std::string _proxy_auth;
|
||||||
uint16_t _proxy_port;
|
|
||||||
bool _proxy_connected = false;
|
|
||||||
bool _used_proxy = false;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
} /* namespace mediakit */
|
} /* namespace mediakit */
|
||||||
|
|
|
||||||
|
|
@ -16,12 +16,12 @@ namespace mediakit {
|
||||||
|
|
||||||
void HttpClientImp::onConnect(const SockException &ex) {
|
void HttpClientImp::onConnect(const SockException &ex) {
|
||||||
if (isUsedProxy() && !isProxyConnected()) {
|
if (isUsedProxy() && !isProxyConnected()) {
|
||||||
//连接代理服务器
|
// 连接代理服务器
|
||||||
setDoNotUseSSL();
|
setDoNotUseSSL();
|
||||||
HttpClient::onConnect(ex);
|
HttpClient::onConnect(ex);
|
||||||
} else {
|
} else {
|
||||||
if (!isHttps()) {
|
if (!isHttps()) {
|
||||||
//https 302跳转 http时,需要关闭ssl
|
// https 302跳转 http时,需要关闭ssl
|
||||||
setDoNotUseSSL();
|
setDoNotUseSSL();
|
||||||
HttpClient::onConnect(ex);
|
HttpClient::onConnect(ex);
|
||||||
} else {
|
} else {
|
||||||
|
|
@ -29,9 +29,10 @@ void HttpClientImp::onConnect(const SockException &ex) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
ssize_t HttpClientImp::onRecvHeader(const char *data, size_t len) {
|
ssize_t HttpClientImp::onRecvHeader(const char *data, size_t len) {
|
||||||
if (isUsedProxy() && !isProxyConnected()) {
|
if (isUsedProxy() && !isProxyConnected()) {
|
||||||
if(checkProxyConnected(data, len)) {
|
if (checkProxyConnected(data, len)) {
|
||||||
clearResponse();
|
clearResponse();
|
||||||
onConnect(SockException(Err_success, "proxy connected"));
|
onConnect(SockException(Err_success, "proxy connected"));
|
||||||
return 0;
|
return 0;
|
||||||
|
|
|
||||||
|
|
@ -39,8 +39,7 @@ const char *TSSegment::onSearchPacketTail(const char *data, size_t len) {
|
||||||
if (((uint8_t *) data)[_size] == TS_SYNC_BYTE) {
|
if (((uint8_t *) data)[_size] == TS_SYNC_BYTE) {
|
||||||
return data + _size;
|
return data + _size;
|
||||||
}
|
}
|
||||||
//搜索ts包头
|
auto pos = memchr(data + _size, TS_SYNC_BYTE, len - _size);
|
||||||
auto pos = memchr(data, TS_SYNC_BYTE, len);
|
|
||||||
if (pos) {
|
if (pos) {
|
||||||
return (char *) pos;
|
return (char *) pos;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue