2019-07-28 10:31:56 +08:00
|
|
|
|
// ssl_client.cpp : <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>̨Ӧ<CCA8>ó<EFBFBD><C3B3><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ڵ㡣
|
2014-11-19 00:25:21 +08:00
|
|
|
|
//
|
|
|
|
|
|
|
|
|
|
#include "stdafx.h"
|
|
|
|
|
#include "lib_acl.h"
|
|
|
|
|
#include <iostream>
|
|
|
|
|
#include "acl_cpp/acl_cpp_init.hpp"
|
2017-08-04 23:13:12 +08:00
|
|
|
|
#include "acl_cpp/stdlib/log.hpp"
|
2014-11-19 00:25:21 +08:00
|
|
|
|
#include "acl_cpp/http/http_header.hpp"
|
|
|
|
|
#include "acl_cpp/stdlib/string.hpp"
|
|
|
|
|
#include "acl_cpp/stream/socket_stream.hpp"
|
|
|
|
|
#include "acl_cpp/stream/polarssl_io.hpp"
|
|
|
|
|
#include "acl_cpp/stream/polarssl_conf.hpp"
|
|
|
|
|
#include "acl_cpp/http/http_client.hpp"
|
|
|
|
|
|
2017-08-04 23:13:12 +08:00
|
|
|
|
static acl::polarssl_conf* __ssl_conf;
|
2014-11-19 00:25:21 +08:00
|
|
|
|
|
|
|
|
|
static void test0(int i)
|
|
|
|
|
{
|
|
|
|
|
acl::socket_stream client;
|
|
|
|
|
acl::string addr("127.0.0.1:441");
|
2019-07-12 11:05:17 +08:00
|
|
|
|
if (!client.open(addr.c_str(), 60, 60)) {
|
2014-11-19 00:25:21 +08:00
|
|
|
|
std::cout << "connect " << addr.c_str()
|
|
|
|
|
<< " error!" << std::endl;
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2017-08-04 23:13:12 +08:00
|
|
|
|
acl::polarssl_io* ssl = new acl::polarssl_io(*__ssl_conf, false);
|
2019-07-12 11:05:17 +08:00
|
|
|
|
if (client.setup_hook(ssl) == ssl) {
|
2014-11-19 00:25:21 +08:00
|
|
|
|
std::cout << "open ssl " << addr.c_str()
|
|
|
|
|
<< " error!" << std::endl;
|
|
|
|
|
ssl->destroy();
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
char line[1024];
|
|
|
|
|
memset(line, 'x', sizeof(line));
|
|
|
|
|
line[1023] = 0;
|
|
|
|
|
line[1022] = '\n';
|
2019-07-12 11:05:17 +08:00
|
|
|
|
if (client.write(line, strlen(line)) == -1) {
|
2014-11-19 00:25:21 +08:00
|
|
|
|
std::cout << "write to " << addr.c_str()
|
|
|
|
|
<< " error!" << std::endl;
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
size_t n = sizeof(line);
|
2019-07-12 11:05:17 +08:00
|
|
|
|
if (!client.gets(line, &n)) {
|
2014-11-19 00:25:21 +08:00
|
|
|
|
std::cout << "gets from " << addr.c_str()
|
|
|
|
|
<< " error!" << std::endl;
|
|
|
|
|
return;
|
|
|
|
|
}
|
2019-07-12 11:05:17 +08:00
|
|
|
|
if (i < 10) {
|
2014-11-19 00:25:21 +08:00
|
|
|
|
std::cout << ">>gets: " << line << std::endl;
|
2019-07-12 11:05:17 +08:00
|
|
|
|
}
|
2014-11-19 00:25:21 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void test1(const char* domain, int port, bool use_gzip, bool use_ssl)
|
|
|
|
|
{
|
2019-07-28 10:31:56 +08:00
|
|
|
|
// <20><><EFBFBD><EFBFBD> WEB <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
2014-11-19 00:25:21 +08:00
|
|
|
|
|
|
|
|
|
acl::string addr;
|
|
|
|
|
addr << domain << ':' << port;
|
|
|
|
|
|
|
|
|
|
acl::socket_stream client;
|
2019-07-12 11:05:17 +08:00
|
|
|
|
if (!client.open(addr.c_str(), 60, 60)) {
|
2014-11-19 00:25:21 +08:00
|
|
|
|
std::cout << "connect " << addr.c_str()
|
|
|
|
|
<< " error!" << std::endl;
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2019-07-28 10:31:56 +08:00
|
|
|
|
// <20><><EFBFBD><EFBFBD>ʹ<EFBFBD><CAB9> SSL <20><>ʽ<EFBFBD><CABD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> SSL <20><><EFBFBD>ֹ<EFBFBD><D6B9><EFBFBD>
|
2019-07-12 11:05:17 +08:00
|
|
|
|
if (use_ssl) {
|
2017-08-04 23:13:12 +08:00
|
|
|
|
acl::polarssl_io* ssl = new acl::polarssl_io(*__ssl_conf, false);
|
2019-07-12 11:05:17 +08:00
|
|
|
|
if (client.setup_hook(ssl) == ssl) {
|
2014-11-19 00:25:21 +08:00
|
|
|
|
std::cout << "open ssl client " << addr.c_str()
|
|
|
|
|
<< " error!" << std::endl;
|
|
|
|
|
ssl->destroy();
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-07-28 10:31:56 +08:00
|
|
|
|
// <20><><EFBFBD><EFBFBD> HTTP <20><><EFBFBD><EFBFBD>ͷ
|
2014-11-19 00:25:21 +08:00
|
|
|
|
acl::http_header header;
|
|
|
|
|
header.set_url("/")
|
|
|
|
|
.set_host(domain)
|
|
|
|
|
.accept_gzip(use_gzip)
|
|
|
|
|
.set_keep_alive(false);
|
2019-07-28 10:31:56 +08:00
|
|
|
|
// mail.126.com <20>Ƚ<EFBFBD><C8BD><EFBFBD><EFBFBD><EFBFBD><EEA3AC>ʱ<EFBFBD>ͻ<EFBFBD><CDBB><EFBFBD>Ҫ<EFBFBD><D2AA><EFBFBD><EFBFBD>ѹ<EFBFBD><D1B9><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ҳ<EFBFBD>᷵<EFBFBD><E1B7B5>ѹ<EFBFBD><D1B9><EFBFBD><EFBFBD><EFBFBD>ݣ<EFBFBD><DDA3><EFBFBD><EFBFBD>Դ˴<D4B4>
|
|
|
|
|
// ǿ<><C7BF>Ҫ<EFBFBD><D2AA><EFBFBD><EFBFBD>ѹ<EFBFBD><D1B9><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
2019-07-12 11:05:17 +08:00
|
|
|
|
if (!use_gzip) {
|
2014-11-19 00:25:21 +08:00
|
|
|
|
header.add_entry("Accept-Encoding", "text/plain");
|
2019-07-12 11:05:17 +08:00
|
|
|
|
}
|
2014-11-19 00:25:21 +08:00
|
|
|
|
|
|
|
|
|
acl::string request;
|
|
|
|
|
header.build_request(request);
|
|
|
|
|
|
|
|
|
|
std::cout << "request(len: " << request.length() << "):" << std::endl;
|
|
|
|
|
std::cout << "----------------------------------------" << std::endl;
|
|
|
|
|
std::cout << request.c_str();
|
|
|
|
|
std::cout << "----------------------------------------" << std::endl;
|
|
|
|
|
|
2019-07-28 10:31:56 +08:00
|
|
|
|
// <20><><EFBFBD><EFBFBD> HTTP GET <20><><EFBFBD><EFBFBD>ͷ
|
2019-07-12 11:05:17 +08:00
|
|
|
|
if (!client.write(request)) {
|
2014-11-19 00:25:21 +08:00
|
|
|
|
std::cout << "write to " << addr.c_str() <<
|
|
|
|
|
" error!" << std::endl;
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2019-07-28 10:31:56 +08:00
|
|
|
|
// <20><>ȡ HTTP <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
2014-11-19 00:25:21 +08:00
|
|
|
|
|
|
|
|
|
char buf[8192];
|
|
|
|
|
size_t size;
|
|
|
|
|
int ret;
|
|
|
|
|
|
2019-07-12 11:05:17 +08:00
|
|
|
|
while (true) {
|
2014-11-19 00:25:21 +08:00
|
|
|
|
size = sizeof(buf) - 1;
|
2019-07-12 11:05:17 +08:00
|
|
|
|
if ((ret = client.read(buf, size, false)) == -1) {
|
2014-11-19 00:25:21 +08:00
|
|
|
|
std::cout << "read over!" << std::endl;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
buf[ret] = 0;
|
|
|
|
|
std::cout << buf;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void test2(const char* domain, int port, bool use_gzip, bool use_ssl)
|
|
|
|
|
{
|
2019-07-28 10:31:56 +08:00
|
|
|
|
// <20><><EFBFBD><EFBFBD> WEB <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
2014-11-19 00:25:21 +08:00
|
|
|
|
|
|
|
|
|
acl::string addr;
|
|
|
|
|
addr << domain << ':' << port;
|
|
|
|
|
|
|
|
|
|
acl::http_client client;
|
2019-07-12 11:05:17 +08:00
|
|
|
|
if (!client.open(addr.c_str(), 60, 60, use_gzip)) {
|
2014-11-19 00:25:21 +08:00
|
|
|
|
std::cout << "connect " << addr.c_str()
|
|
|
|
|
<< " error!" << std::endl;
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2019-07-12 11:05:17 +08:00
|
|
|
|
if (use_ssl) {
|
2019-07-28 10:31:56 +08:00
|
|
|
|
// <20><><EFBFBD><EFBFBD> SSL <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ͻ<EFBFBD><CDBB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><F3B6A8A3><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ͷ<EFBFBD>ǰ<EFBFBD><C7B0> SSL <20><><EFBFBD><EFBFBD>
|
|
|
|
|
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ڲ<EFBFBD>ͨ<EFBFBD><CDA8><EFBFBD><EFBFBD><EFBFBD><EFBFBD> stream_hook::destroy() <20>ͷ<EFBFBD>
|
2017-08-04 23:13:12 +08:00
|
|
|
|
acl::polarssl_io* ssl = new acl::polarssl_io(*__ssl_conf, false);
|
2019-07-12 11:05:17 +08:00
|
|
|
|
if (client.get_stream().setup_hook(ssl) == ssl) {
|
2014-11-19 00:25:21 +08:00
|
|
|
|
std::cout << "open ssl client " << addr.c_str()
|
|
|
|
|
<< " error!" << std::endl;
|
|
|
|
|
ssl->destroy();
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-07-28 10:31:56 +08:00
|
|
|
|
// <20><><EFBFBD><EFBFBD> HTTP <20><><EFBFBD><EFBFBD>ͷ
|
2014-11-19 00:25:21 +08:00
|
|
|
|
|
|
|
|
|
acl::http_header header;
|
|
|
|
|
header.set_url("/")
|
|
|
|
|
.set_host(domain)
|
|
|
|
|
.accept_gzip(use_gzip)
|
|
|
|
|
.add_entry("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:31.0) Gecko/20100101 Firefox/31.0")
|
|
|
|
|
.add_entry("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8")
|
|
|
|
|
.add_entry("Accept-Languge", "zh-cn,en;q=0.5")
|
|
|
|
|
.add_entry("Pragma", "no-cache")
|
|
|
|
|
.add_entry("Cache-Control", "no-cache");
|
|
|
|
|
|
|
|
|
|
acl::string request;
|
|
|
|
|
header.build_request(request);
|
|
|
|
|
|
|
|
|
|
std::cout << "request:" << std::endl;
|
|
|
|
|
std::cout << "----------------------------------------" << std::endl;
|
|
|
|
|
std::cout << request.c_str();
|
|
|
|
|
std::cout << "----------------------------------------" << std::endl;
|
|
|
|
|
|
2019-07-28 10:31:56 +08:00
|
|
|
|
// <20><><EFBFBD><EFBFBD> HTTP GET <20><><EFBFBD><EFBFBD>ͷ
|
2014-11-19 00:25:21 +08:00
|
|
|
|
|
2019-07-12 11:05:17 +08:00
|
|
|
|
if (!client.write_head(header)) {
|
2014-11-19 00:25:21 +08:00
|
|
|
|
std::cout << "write to " << addr.c_str()
|
|
|
|
|
<< " error!" << std::endl;
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2019-07-28 10:31:56 +08:00
|
|
|
|
// <20><>ȡ HTTP <20><>Ӧͷ
|
2019-07-12 11:05:17 +08:00
|
|
|
|
if (!client.read_head()) {
|
2014-11-19 00:25:21 +08:00
|
|
|
|
std::cout << "read http respond header error!" << std::endl;
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
client.get_respond_head(&request);
|
|
|
|
|
std::cout << "respond header:" << std::endl;
|
|
|
|
|
std::cout << request.c_str();
|
|
|
|
|
|
2019-07-28 10:31:56 +08:00
|
|
|
|
// <20><>ȡ<EFBFBD><C8A1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ӧ<EFBFBD><D3A6> HTTP <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
2014-11-19 00:25:21 +08:00
|
|
|
|
|
|
|
|
|
char buf[8192];
|
|
|
|
|
size_t size;
|
|
|
|
|
int ret;
|
|
|
|
|
|
2019-07-12 11:05:17 +08:00
|
|
|
|
while (true) {
|
2014-11-19 00:25:21 +08:00
|
|
|
|
size = sizeof(buf) - 1;
|
2019-07-12 11:05:17 +08:00
|
|
|
|
if ((ret = client.read_body(buf, size)) <= 0) {
|
2014-11-19 00:25:21 +08:00
|
|
|
|
std::cout << "read over!" << std::endl;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
buf[ret] = 0;
|
|
|
|
|
std::cout << buf;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int main(int argc, char* argv[])
|
|
|
|
|
{
|
|
|
|
|
acl::acl_cpp_init();
|
2017-08-04 23:13:12 +08:00
|
|
|
|
acl::log::stdout_open(true);
|
|
|
|
|
|
2017-08-04 15:45:17 +08:00
|
|
|
|
#if defined(_WIN32) || defined(_WIN64)
|
|
|
|
|
acl::polarssl_conf::set_libpath("libpolarssl.dll");
|
|
|
|
|
#else
|
|
|
|
|
acl::polarssl_conf::set_libpath("../libpolarssl.so");
|
|
|
|
|
#endif
|
|
|
|
|
acl::polarssl_conf::load();
|
2014-11-19 00:25:21 +08:00
|
|
|
|
|
2017-08-04 23:13:12 +08:00
|
|
|
|
__ssl_conf = new acl::polarssl_conf;
|
|
|
|
|
|
2014-11-19 00:25:21 +08:00
|
|
|
|
int n = 1;
|
2019-07-12 11:05:17 +08:00
|
|
|
|
if (argc >= 2) {
|
2014-11-19 00:25:21 +08:00
|
|
|
|
n = atoi(argv[1]);
|
2019-07-12 11:05:17 +08:00
|
|
|
|
}
|
|
|
|
|
if (n <= 0) {
|
2014-11-19 00:25:21 +08:00
|
|
|
|
n = 100;
|
2019-07-12 11:05:17 +08:00
|
|
|
|
}
|
2014-11-19 00:25:21 +08:00
|
|
|
|
ACL_METER_TIME("---------- begin ----------");
|
2019-07-12 11:05:17 +08:00
|
|
|
|
for (int i = 0; i < 0; i++) {
|
2014-11-19 00:25:21 +08:00
|
|
|
|
test0(i);
|
2019-07-12 11:05:17 +08:00
|
|
|
|
}
|
2014-11-19 00:25:21 +08:00
|
|
|
|
ACL_METER_TIME("---------- end ----------");
|
|
|
|
|
|
2019-07-28 10:31:56 +08:00
|
|
|
|
// 126 <20><> SSL <20><><EFBFBD><EFBFBD>ʱ<EFBFBD><CAB1> HTTP <20><><EFBFBD><EFBFBD>ͷ<EFBFBD>е<EFBFBD> Host ֵΪ mail.126.com:443 ʱ<><CAB1> nginx
|
|
|
|
|
// <20>ᱨ<EFBFBD><E1B1A8><EFBFBD><EFBFBD>ֻ<EFBFBD><D6BB><EFBFBD>ǣ<EFBFBD>Host: mail.126.com<6F><6D><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
2014-11-19 00:25:21 +08:00
|
|
|
|
|
|
|
|
|
test1("mail.126.com", 443, false, true);
|
|
|
|
|
test2("mail.126.com", 443, false, true);
|
|
|
|
|
test2("mail.qq.com", 443, false, true);
|
|
|
|
|
test2("mail.sohu.com", 443, false, true);
|
|
|
|
|
test2("mail.sina.com.cn", 443, false, true);
|
|
|
|
|
|
|
|
|
|
printf("Over, enter any key to exit!\n");
|
|
|
|
|
getchar();
|
2017-08-04 23:13:12 +08:00
|
|
|
|
delete __ssl_conf;
|
2014-11-19 00:25:21 +08:00
|
|
|
|
return (0);
|
|
|
|
|
}
|