2019-07-28 10:31:56 +08:00
|
|
|
|
#include "stdafx.h"
|
2015-07-13 16:28:09 +08:00
|
|
|
|
#include <memory>
|
|
|
|
|
#include <iostream>
|
|
|
|
|
#include "master_service.h"
|
|
|
|
|
#include "https_client.h"
|
|
|
|
|
|
|
|
|
|
#define DEBUG 100
|
|
|
|
|
|
2020-01-05 15:41:08 +08:00
|
|
|
|
https_client::https_client(acl::ostream& out, acl::sslbase_conf* conf)
|
2015-07-13 16:28:09 +08:00
|
|
|
|
: out_(out)
|
|
|
|
|
, ssl_conf_(conf)
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
https_client::~https_client()
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool https_client::connect_server(const acl::string& server_addr,
|
|
|
|
|
acl::http_client& client)
|
|
|
|
|
{
|
2019-07-28 10:31:56 +08:00
|
|
|
|
// <20>Ȳ鱾<C8B2><E9B1BE>ӳ<EFBFBD><D3B3><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>û<EFBFBD><C3BB>ӳ<EFBFBD><D3B3><EFBFBD><EFBFBD>
|
2015-07-13 16:28:09 +08:00
|
|
|
|
master_service& ms = acl::singleton2<master_service>::get_instance();
|
|
|
|
|
const char* addr = ms.get_addr(server_addr.c_str());
|
2020-01-05 15:41:08 +08:00
|
|
|
|
if (addr == NULL) {
|
2015-07-13 16:28:09 +08:00
|
|
|
|
addr = server_addr.c_str();
|
2020-01-05 15:41:08 +08:00
|
|
|
|
}
|
2015-07-13 16:28:09 +08:00
|
|
|
|
|
2020-01-05 15:41:08 +08:00
|
|
|
|
if (!client.open(addr, 60, 60, true)) {
|
2015-07-13 16:28:09 +08:00
|
|
|
|
out_.format("connect server %s error", addr);
|
|
|
|
|
return false;
|
2020-01-05 15:41:08 +08:00
|
|
|
|
} else {
|
2015-07-13 16:28:09 +08:00
|
|
|
|
logger_debug(DEBUG, 1, "connect server ok");
|
2020-01-05 15:41:08 +08:00
|
|
|
|
}
|
2015-07-13 16:28:09 +08:00
|
|
|
|
|
2020-01-05 15:41:08 +08:00
|
|
|
|
if (ssl_conf_) {
|
2015-07-13 16:28:09 +08:00
|
|
|
|
logger_debug(DEBUG, 1, "begin open ssl");
|
|
|
|
|
|
2020-07-16 09:12:39 +08:00
|
|
|
|
acl::sslbase_io* ssl = ssl_conf_->create(false);
|
2020-01-05 15:41:08 +08:00
|
|
|
|
if (client.get_stream().setup_hook(ssl) == ssl) {
|
2015-07-13 16:28:09 +08:00
|
|
|
|
out_.puts("open ssl client error");
|
|
|
|
|
ssl->destroy();
|
|
|
|
|
return false;
|
2020-01-05 15:41:08 +08:00
|
|
|
|
} else {
|
2015-07-13 16:28:09 +08:00
|
|
|
|
logger_debug(DEBUG, 1, "open ssl ok");
|
2020-01-05 15:41:08 +08:00
|
|
|
|
}
|
2015-07-13 16:28:09 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool https_client::http_request(acl::HttpServletRequest& req,
|
|
|
|
|
acl::HttpServletResponse& res)
|
|
|
|
|
{
|
|
|
|
|
const char* host = req.getRemoteHost();
|
2020-01-05 15:41:08 +08:00
|
|
|
|
if (host == NULL || *host == 0) {
|
2015-07-13 16:28:09 +08:00
|
|
|
|
host = req.getLocalAddr();
|
2020-01-05 15:41:08 +08:00
|
|
|
|
}
|
2015-07-13 16:28:09 +08:00
|
|
|
|
|
|
|
|
|
acl::string server_addr;
|
2020-01-05 15:41:08 +08:00
|
|
|
|
if (ssl_conf_ == NULL) {
|
2015-07-13 16:28:09 +08:00
|
|
|
|
server_addr.format("%s:80", host);
|
2020-01-05 15:41:08 +08:00
|
|
|
|
} else {
|
2015-07-13 16:28:09 +08:00
|
|
|
|
server_addr.format("%s:443", host);
|
2020-01-05 15:41:08 +08:00
|
|
|
|
}
|
2015-07-13 16:28:09 +08:00
|
|
|
|
|
|
|
|
|
std::auto_ptr<acl::http_client> backend(new acl::http_client);
|
|
|
|
|
|
2019-07-28 10:31:56 +08:00
|
|
|
|
// <20><><EFBFBD>ӷ<EFBFBD><D3B7><EFBFBD><EFBFBD><EFBFBD>
|
2020-01-05 15:41:08 +08:00
|
|
|
|
if (!connect_server(server_addr, *backend)) {
|
2015-07-13 16:28:09 +08:00
|
|
|
|
return false;
|
2020-01-05 15:41:08 +08:00
|
|
|
|
}
|
2015-07-13 16:28:09 +08:00
|
|
|
|
|
|
|
|
|
acl::http_client* front = req.getClient();
|
|
|
|
|
|
2019-07-28 10:31:56 +08:00
|
|
|
|
// <20><><EFBFBD><EFBFBD><EFBFBD>ڴ˴<DAB4>ǿ<EFBFBD><C7BF><EFBFBD>滻 HTTP <20><><EFBFBD><EFBFBD>ͷ<EFBFBD>е<EFBFBD> HOST <20>ֶ<EFBFBD>
|
2015-09-27 20:59:21 +08:00
|
|
|
|
//front->header_update("Host", "www.test.com:443");
|
|
|
|
|
|
2019-07-28 10:31:56 +08:00
|
|
|
|
// ȡ<><C8A1> HTTP <20><><EFBFBD><EFBFBD>ͷ<EFBFBD><CDB7><EFBFBD><EFBFBD>
|
2015-07-13 16:28:09 +08:00
|
|
|
|
acl::string req_hdr;
|
|
|
|
|
front->sprint_header(req_hdr, NULL);
|
|
|
|
|
|
2015-09-27 20:59:21 +08:00
|
|
|
|
printf(">>>req_hdr: %s\r\n", req_hdr.c_str());
|
|
|
|
|
|
2019-07-28 10:31:56 +08:00
|
|
|
|
// ת<><D7AA> HTTP <20><><EFBFBD><EFBFBD>ͷ<EFBFBD><CDB7><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
2020-01-05 15:41:08 +08:00
|
|
|
|
if (backend->get_ostream().write(req_hdr) == -1) {
|
2015-07-13 16:28:09 +08:00
|
|
|
|
out_.puts(">>>>write header error");
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2020-01-05 15:41:08 +08:00
|
|
|
|
if (backend->get_ostream().write("\r\n") == -1) {
|
2015-07-13 16:28:09 +08:00
|
|
|
|
out_.puts(">>>>write CRLF error");
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2019-07-28 10:31:56 +08:00
|
|
|
|
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>壬<EFBFBD><E5A3AC>ת<EFBFBD><D7AA><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
2015-07-13 16:28:09 +08:00
|
|
|
|
long long int len = req.getContentLength();
|
|
|
|
|
|
2020-01-05 15:41:08 +08:00
|
|
|
|
if (len > 0) {
|
2015-07-13 16:28:09 +08:00
|
|
|
|
char req_body[8192];
|
|
|
|
|
int ret;
|
|
|
|
|
|
2020-01-05 15:41:08 +08:00
|
|
|
|
while (true) {
|
2015-07-13 16:28:09 +08:00
|
|
|
|
ret = front->read_body(req_body, sizeof(req_body) - 1);
|
2020-01-05 15:41:08 +08:00
|
|
|
|
if (ret < 0) {
|
2015-07-13 16:28:09 +08:00
|
|
|
|
out_.puts(">>> read req body error");
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2020-01-05 15:41:08 +08:00
|
|
|
|
if (ret == 0) {
|
2015-07-13 16:28:09 +08:00
|
|
|
|
break;
|
2020-01-05 15:41:08 +08:00
|
|
|
|
}
|
2015-07-13 16:28:09 +08:00
|
|
|
|
|
|
|
|
|
req_body[ret] = 0;
|
|
|
|
|
out_.write(req_body, ret);
|
2020-01-05 15:41:08 +08:00
|
|
|
|
if (backend->get_ostream().write(req_body, ret) == -1) {
|
2015-07-13 16:28:09 +08:00
|
|
|
|
out_.puts(">>> write body to server error");
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
out_.puts("");
|
|
|
|
|
|
2019-07-28 10:31:56 +08:00
|
|
|
|
// <20><>ʼ<EFBFBD>Ӻ<EFBFBD><D3BA>˷<EFBFBD><CBB7><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ȡ<EFBFBD><C8A1>Ӧͷ<D3A6><CDB7><EFBFBD><EFBFBD>Ӧ<EFBFBD><D3A6><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
2015-07-13 16:28:09 +08:00
|
|
|
|
|
|
|
|
|
out_.puts(">>>> begin read res header<<<<<");
|
2020-01-05 15:41:08 +08:00
|
|
|
|
if (!backend->read_head()) {
|
2015-07-13 16:28:09 +08:00
|
|
|
|
out_.puts(">>>>>>>>read header error<<<<<<<<<<");
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
acl::string res_hdr;
|
|
|
|
|
backend->sprint_header(res_hdr, NULL);
|
2020-01-05 15:41:08 +08:00
|
|
|
|
if (res.getOutputStream().write(res_hdr) == -1) {
|
2015-07-13 16:28:09 +08:00
|
|
|
|
out_.puts(">>>>>write res hdr error<<<<<<");
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2020-01-05 15:41:08 +08:00
|
|
|
|
if (res.getOutputStream().write("\r\n") == -1) {
|
2015-07-13 16:28:09 +08:00
|
|
|
|
out_.puts(">>>write CRLF error");
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
out_.puts("------------------res hdr----------------");
|
|
|
|
|
out_.write(res_hdr);
|
|
|
|
|
out_.puts("------------------res hdr end------------");
|
|
|
|
|
|
|
|
|
|
char buf[8192];
|
|
|
|
|
|
2020-01-05 15:41:08 +08:00
|
|
|
|
while (true) {
|
2015-07-13 16:28:09 +08:00
|
|
|
|
int ret = backend->read_body(buf, sizeof(buf) - 1);
|
2020-01-05 15:41:08 +08:00
|
|
|
|
if (ret < 0) {
|
2015-07-13 16:28:09 +08:00
|
|
|
|
logger_error(">>> read body error");
|
|
|
|
|
return false;
|
2020-01-05 15:41:08 +08:00
|
|
|
|
} else if (ret == 0) {
|
2015-07-13 16:28:09 +08:00
|
|
|
|
break;
|
2020-01-05 15:41:08 +08:00
|
|
|
|
}
|
2015-07-13 16:28:09 +08:00
|
|
|
|
|
|
|
|
|
buf[ret] = 0;
|
|
|
|
|
out_.write(buf, ret);
|
2020-01-05 15:41:08 +08:00
|
|
|
|
if (res.getOutputStream().write(buf, ret) == -1) {
|
2015-07-13 16:28:09 +08:00
|
|
|
|
out_.puts(">>> write res body error");
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const char* ptr = backend->header_value("Transfer-Encoding");
|
2020-01-05 15:41:08 +08:00
|
|
|
|
if (ptr == NULL || *ptr == 0 || strcasecmp(ptr, "chunked") != 0) {
|
2015-07-13 16:28:09 +08:00
|
|
|
|
return backend->keep_alive();
|
2020-01-05 15:41:08 +08:00
|
|
|
|
}
|
2015-07-13 16:28:09 +08:00
|
|
|
|
|
2019-07-28 10:31:56 +08:00
|
|
|
|
// <20><><EFBFBD><EFBFBD> http <20><>Ӧ<EFBFBD>壬<EFBFBD><E5A3AC>Ϊ<EFBFBD><CEAA><EFBFBD><EFBFBD><EFBFBD><EFBFBD> chunk <20><><EFBFBD><EFBFBD>ģʽ<C4A3><CABD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ҫ<EFBFBD><D2AA><EFBFBD><EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD><D2BB>
|
|
|
|
|
// res.write <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϊ 0 <20>Ա<EFBFBD>ʾ chunk <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݽ<EFBFBD><DDBD><EFBFBD>
|
2015-07-13 16:28:09 +08:00
|
|
|
|
return res.write(NULL, 0);
|
|
|
|
|
}
|