2014-11-19 00:25:21 +08:00
|
|
|
|
// ssl_client.cpp : <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>̨Ӧ<CCA8>ó<EFBFBD><C3B3><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ڵ㡣
|
|
|
|
|
//
|
|
|
|
|
|
|
|
|
|
#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");
|
|
|
|
|
if (client.open(addr.c_str(), 60, 60) == false)
|
|
|
|
|
{
|
|
|
|
|
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);
|
2014-11-19 00:25:21 +08:00
|
|
|
|
if (client.setup_hook(ssl) == ssl)
|
|
|
|
|
{
|
|
|
|
|
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';
|
|
|
|
|
if (client.write(line, strlen(line)) == -1)
|
|
|
|
|
{
|
|
|
|
|
std::cout << "write to " << addr.c_str()
|
|
|
|
|
<< " error!" << std::endl;
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
size_t n = sizeof(line);
|
|
|
|
|
if (client.gets(line, &n) == false)
|
|
|
|
|
{
|
|
|
|
|
std::cout << "gets from " << addr.c_str()
|
|
|
|
|
<< " error!" << std::endl;
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
if (i < 10)
|
|
|
|
|
std::cout << ">>gets: " << line << std::endl;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void test1(const char* domain, int port, bool use_gzip, bool use_ssl)
|
|
|
|
|
{
|
|
|
|
|
// <20><><EFBFBD><EFBFBD> WEB <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
|
|
|
|
|
acl::string addr;
|
|
|
|
|
addr << domain << ':' << port;
|
|
|
|
|
|
|
|
|
|
acl::socket_stream client;
|
|
|
|
|
if (client.open(addr.c_str(), 60, 60) == false)
|
|
|
|
|
{
|
|
|
|
|
std::cout << "connect " << addr.c_str()
|
|
|
|
|
<< " error!" << std::endl;
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// <20><><EFBFBD><EFBFBD>ʹ<EFBFBD><CAB9> SSL <20><>ʽ<EFBFBD><CABD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> SSL <20><><EFBFBD>ֹ<EFBFBD><D6B9><EFBFBD>
|
|
|
|
|
if (use_ssl)
|
|
|
|
|
{
|
2017-08-04 23:13:12 +08:00
|
|
|
|
acl::polarssl_io* ssl = new acl::polarssl_io(*__ssl_conf, false);
|
2014-11-19 00:25:21 +08:00
|
|
|
|
if (client.setup_hook(ssl) == ssl)
|
|
|
|
|
{
|
|
|
|
|
std::cout << "open ssl client " << addr.c_str()
|
|
|
|
|
<< " error!" << std::endl;
|
|
|
|
|
ssl->destroy();
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// <20><><EFBFBD><EFBFBD> HTTP <20><><EFBFBD><EFBFBD>ͷ
|
|
|
|
|
acl::http_header header;
|
|
|
|
|
header.set_url("/")
|
|
|
|
|
.set_host(domain)
|
|
|
|
|
.accept_gzip(use_gzip)
|
|
|
|
|
.set_keep_alive(false);
|
|
|
|
|
// 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>
|
|
|
|
|
if (!use_gzip)
|
|
|
|
|
header.add_entry("Accept-Encoding", "text/plain");
|
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
|
|
// <20><><EFBFBD><EFBFBD> HTTP GET <20><><EFBFBD><EFBFBD>ͷ
|
|
|
|
|
if (client.write(request) == false)
|
|
|
|
|
{
|
|
|
|
|
std::cout << "write to " << addr.c_str() <<
|
|
|
|
|
" error!" << std::endl;
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// <20><>ȡ HTTP <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
|
|
|
|
|
char buf[8192];
|
|
|
|
|
size_t size;
|
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
|
|
while (true)
|
|
|
|
|
{
|
|
|
|
|
size = sizeof(buf) - 1;
|
|
|
|
|
if ((ret = client.read(buf, size, false)) == -1)
|
|
|
|
|
{
|
|
|
|
|
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)
|
|
|
|
|
{
|
|
|
|
|
// <20><><EFBFBD><EFBFBD> WEB <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
|
|
|
|
|
acl::string addr;
|
|
|
|
|
addr << domain << ':' << port;
|
|
|
|
|
|
|
|
|
|
acl::http_client client;
|
|
|
|
|
if (client.open(addr.c_str(), 60, 60, use_gzip) == false)
|
|
|
|
|
{
|
|
|
|
|
std::cout << "connect " << addr.c_str()
|
|
|
|
|
<< " error!" << std::endl;
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (use_ssl)
|
|
|
|
|
{
|
|
|
|
|
// <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);
|
2014-11-19 00:25:21 +08:00
|
|
|
|
if (client.get_stream().setup_hook(ssl) == ssl)
|
|
|
|
|
{
|
|
|
|
|
std::cout << "open ssl client " << addr.c_str()
|
|
|
|
|
<< " error!" << std::endl;
|
|
|
|
|
ssl->destroy();
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// <20><><EFBFBD><EFBFBD> HTTP <20><><EFBFBD><EFBFBD>ͷ
|
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
|
|
// <20><><EFBFBD><EFBFBD> HTTP GET <20><><EFBFBD><EFBFBD>ͷ
|
|
|
|
|
|
|
|
|
|
if (client.write_head(header) == false)
|
|
|
|
|
{
|
|
|
|
|
std::cout << "write to " << addr.c_str()
|
|
|
|
|
<< " error!" << std::endl;
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// <20><>ȡ HTTP <20><>Ӧͷ
|
|
|
|
|
if (client.read_head() == false)
|
|
|
|
|
{
|
|
|
|
|
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();
|
|
|
|
|
|
|
|
|
|
// <20><>ȡ<EFBFBD><C8A1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ӧ<EFBFBD><D3A6> HTTP <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
|
|
|
|
|
char buf[8192];
|
|
|
|
|
size_t size;
|
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
|
|
while (true)
|
|
|
|
|
{
|
|
|
|
|
size = sizeof(buf) - 1;
|
|
|
|
|
if ((ret = client.read_body(buf, size)) <= 0)
|
|
|
|
|
{
|
|
|
|
|
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;
|
|
|
|
|
if (argc >= 2)
|
|
|
|
|
n = atoi(argv[1]);
|
|
|
|
|
if (n <= 0)
|
|
|
|
|
n = 100;
|
|
|
|
|
ACL_METER_TIME("---------- begin ----------");
|
|
|
|
|
for (int i = 0; i < 0; i++)
|
|
|
|
|
test0(i);
|
|
|
|
|
ACL_METER_TIME("---------- end ----------");
|
|
|
|
|
|
|
|
|
|
// 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>
|
|
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
}
|