acl/lib_acl_cpp/samples/ssl/https_client/main.cpp

185 lines
4.1 KiB
C++
Raw Normal View History

#include "stdafx.h"
2014-11-19 00:25:21 +08:00
#include "util.h"
#include "https_client.h"
#include "https_request.h"
2014-11-19 00:25:21 +08:00
static void usage(const char* procname)
{
printf("usage: %s -h [help]\r\n"
" -f path of mbedtls or polarss\r\n"
" -s server_addr [default: 127.0.0.1:1443]\r\n"
2022-08-26 17:51:41 +08:00
" -H host\r\n"
2020-01-03 17:01:51 +08:00
" -k [keep alive, default: false]\r\n"
" -L data_length [default: 1024]\r\n"
" -c cocurrent [default: 1]\r\n"
" -S [use ssl, default: no]\r\n"
2022-08-26 17:51:41 +08:00
" -Z [if support gzip, default: no]\r\n"
" -B [if show response body, default: no]\r\n"
2020-01-03 17:01:51 +08:00
" -n count [default: 10]\r\n", procname);
2014-11-19 00:25:21 +08:00
}
int main(int argc, char* argv[])
{
int ch, cocurrent = 1, count = 10, length = 1024;
2022-08-26 17:51:41 +08:00
bool keep_alive = false, use_ssl = false, accept_gzip = false;
bool show_body = false;
2014-11-19 00:25:21 +08:00
acl::string server_addr("127.0.0.1:1443");
acl::string domain, libpath;
2014-11-19 00:25:21 +08:00
acl::acl_cpp_init();
acl::log::stdout_open(true);
2022-08-26 17:51:41 +08:00
while ((ch = getopt(argc, argv, "hf:s:c:n:kSH:ZB")) > 0) {
2020-01-03 17:01:51 +08:00
switch (ch) {
2014-11-19 00:25:21 +08:00
case 'h':
usage(argv[0]);
return 0;
case 'f':
libpath = optarg;
break;
2014-11-19 00:25:21 +08:00
case 'c':
cocurrent = atoi(optarg);
break;
case 'n':
count = atoi(optarg);
break;
case 'k':
keep_alive = true;
break;
case 's':
server_addr = optarg;
break;
case 'S':
use_ssl = true;
break;
case 'H':
domain = optarg;
break;
2022-08-26 17:51:41 +08:00
case 'Z':
accept_gzip = true;
break;
case 'B':
show_body = true;
break;
2014-11-19 00:25:21 +08:00
default:
break;
}
}
2020-01-03 17:01:51 +08:00
(void) keep_alive;
acl::sslbase_conf* ssl_conf = NULL;
if (libpath.find("mbedtls")) {
const std::vector<acl::string>& libs = libpath.split2(";");
if (libs.size() != 3) {
printf("invalid libpath=%s\r\n", libpath.c_str());
return 1;
}
2022-08-26 17:51:41 +08:00
acl::mbedtls_conf::set_libpath(libs[0], libs[1], libs[2]);
2020-01-03 17:01:51 +08:00
if (!acl::mbedtls_conf::load()) {
printf("load %s error\r\n", libpath.c_str());
return 1;
}
2022-08-26 17:51:41 +08:00
ssl_conf = new acl::mbedtls_conf(false);
} else if (libpath.find("polarssl")) {
2019-12-20 15:08:36 +08:00
acl::polarssl_conf::set_libpath(libpath);
2020-01-03 17:01:51 +08:00
if (!acl::polarssl_conf::load()) {
printf("load %s error\r\n", libpath.c_str());
return 1;
}
2022-08-26 17:51:41 +08:00
ssl_conf = new acl::polarssl_conf;
} else if (libpath.find("libssl")) {
const std::vector<acl::string>& libs = libpath.split2(";");
if (libs.size() != 2) {
printf("please specify libcrypto.so, libssl.so\r\n");
return 1;
}
acl::openssl_conf::set_libpath(libs[0], libs[1]);
if (!acl::openssl_conf::load()) {
printf("load %s %s error\r\n", libs[0].c_str(), libs[1].c_str());
return 1;
}
ssl_conf = new acl::openssl_conf(false);
2019-12-20 15:08:36 +08:00
}
2020-01-03 17:01:51 +08:00
if (domain.empty()) {
2014-11-19 00:25:21 +08:00
domain = server_addr;
2020-01-03 17:01:51 +08:00
}
2014-11-19 00:25:21 +08:00
struct timeval begin;
gettimeofday(&begin, NULL);
#if 1
2014-11-19 00:25:21 +08:00
std::list<https_client*> threads;
2020-01-03 17:01:51 +08:00
for (int i = 0; i < cocurrent; i++) {
2014-11-19 00:25:21 +08:00
https_client* thread = new https_client(server_addr, domain,
keep_alive, count, length);
2022-08-26 17:51:41 +08:00
thread->accept_gzip(accept_gzip);
2020-01-03 17:01:51 +08:00
if (use_ssl) {
2019-12-20 15:08:36 +08:00
thread->set_ssl_conf(ssl_conf);
2020-01-03 17:01:51 +08:00
}
2014-11-19 00:25:21 +08:00
2022-08-26 17:51:41 +08:00
thread->set_show_body(show_body);
2014-11-19 00:25:21 +08:00
thread->set_detachable(false);
threads.push_back(thread);
thread->start();
}
std::list<https_client*>::iterator it = threads.begin();
2020-01-03 17:01:51 +08:00
for (; it != threads.end(); ++it) {
(*it)->wait(NULL);
2014-11-19 00:25:21 +08:00
delete *it;
}
#else
(void) length;
std::list<https_request*> threads;
2020-01-03 17:01:51 +08:00
for (int i = 0; i < cocurrent; i++) {
https_request* thread = new https_request(server_addr,
2019-12-20 15:08:36 +08:00
use_ssl ? ssl_conf : NULL);
thread->set_detachable(false);
threads.push_back(thread);
thread->start();
}
// sleep(2);
std::list<https_request*>::iterator it = threads.begin();
2020-01-03 17:01:51 +08:00
for (; it != threads.end(); ++it) {
if ((*it)->wait(NULL)) {
printf("wait one thread(%lu) error\r\n",
(*it)->thread_id());
2020-01-03 17:01:51 +08:00
} else {
printf("wait thread(%lu) ok\r\n", (*it)->thread_id());
}
delete *it;
}
#endif
2014-11-19 00:25:21 +08:00
struct timeval end;
gettimeofday(&end, NULL);
double spent = util::stamp_sub(&end, &begin);
printf("total: %d, spent: %.2f, speed: %.f\r\n", count,
spent, (count * 1000) / (spent > 1 ? spent : 1));
printf("enter any key to exit\r\n");
getchar();
2019-12-20 15:08:36 +08:00
delete ssl_conf;
2014-11-19 00:25:21 +08:00
return 0;
}