add one demo for testing ssl module when using static mebedtls library.

This commit is contained in:
shuxin   zheng 2020-08-25 10:22:27 +08:00
parent 58754c8952
commit cb43c1fe45
11 changed files with 287 additions and 5 deletions

View File

@ -54,7 +54,13 @@ static void parse_result(const char *filepath)
struct in_addr sin_addr;
memcpy(&sin_addr, answers->answer[i].rdata, 4);
inet_ntop(AF_INET, &sin_addr, ip, sizeof(ip));
printf("ip=%s\r\n", ip);
printf(">>ip=%s\r\n", ip);
} else if (answers->answer[i].type == RFC1035_TYPE_CNAME) {
char name[128];
size_t n = sizeof(name) > answers->answer[i].rdlength
? answers->answer[i].rdlength : sizeof(name);
ACL_SAFE_STRNCPY(name, answers->answer[i].rdata, n);
printf(">>cname=%s\r\n", name);
} else {
printf("not support type=%d\n", answers->answer[i].type);
}
@ -65,7 +71,7 @@ static void parse_result(const char *filepath)
static void usage(const char *procname)
{
printf("usage: %s -h [help]\r\n", procname);
printf("usage: %s -h [help] -e [encoding] -d [decoding] xxx\r\n", procname);
}
int main(int argc, char *argv[])

View File

@ -21,7 +21,7 @@ CFLAGS = -c -g -W \
-DHAS_PGSQL_DLL \
-DHAS_SQLITE_DLL \
-DHAS_POLARSSL_DLL \
-DHAS_MBEDTLS_DLL \
-DHAS_MBEDTLS \
-Winvalid-pch \
-DACL_PREPARE_COMPILE \
#-DACL_CLIENT_ONLY \

View File

@ -0,0 +1,17 @@
PROG = https_request
util_path = ../..
base_path = ../../..
include ../../Makefile.in
#Path for SunOS
ifeq ($(findstring SunOS, $(UNIXNAME)), SunOS)
EXTLIBS = -liconv
endif
ifeq ($(findstring FreeBSD, $(UNIXNAME)), FreeBSD)
EXTLIBS = -L/usr/local/lib -liconv
endif
ifeq ($(findstring Darwin, $(UNIXNAME)), Darwin)
EXTLIBS += -L/usr/lib -liconv
endif
CFLAGS += -I../..
EXTLIBS += ../libmbedtls.a ../libmbedx509.a ../libmbedcrypto.a -lz -ldl

View File

@ -0,0 +1,104 @@
#include "stdafx.h"
#include "https_request.h"
https_request::https_request(acl::sslbase_conf* ssl_conf, const char* addr,
const char* host, const char* url)
: request_(addr)
, host_(host)
, url_(url)
, to_charset_("utf-8")
{
printf("server addr: %s\r\n", addr);
printf("host: %s\r\n", host);
printf("url: %s\r\n", url);
printf("\r\n");
if (ssl_conf) {
request_.set_ssl(ssl_conf);
}
}
https_request::~https_request(void)
{
}
void* https_request::run(void)
{
acl::http_header& hdr = request_.request_header();
hdr.set_url(url_)
.set_host(host_)
.set_content_type("text/plain")
.set_keep_alive(true);
if (!request_.request(NULL, 0)) {
printf("send request error\r\n");
return NULL;
}
const char* ptr = request_.header_value("Content-Type");
if (ptr == NULL || *ptr == 0) {
printf("Content-Type empty!\r\n");
return NULL;
}
acl::http_ctype ctype;
ctype.parse(ptr);
// 响应头数据类型的子类型
const char* stype = ctype.get_stype();
bool ret;
if (stype == NULL) {
ret = do_plain(request_);
} else if (strcasecmp(stype, "xml") == 0) {
ret = do_xml(request_);
} else if (strcasecmp(stype, "json") == 0) {
ret = do_json(request_);
} else {
ret = do_plain(request_);
}
if (ret) {
printf("%s(%d): read ok!\r\n", __FILE__, __LINE__);
} else {
printf("read error\r\n");
}
return NULL;
}
bool https_request::do_plain(acl::http_request& req)
{
acl::string body;
if (!req.get_body(body, to_charset_)) {
logger_error("get http body error");
return false;
}
printf("plain body:\r\n(%s)\r\n", body.c_str());
return true;
}
bool https_request::do_xml(acl::http_request& req)
{
acl::xml1 body;
if (req.get_body(body, to_charset_)) {
logger_error("get http body error");
return false;
}
printf(">>>xml body:\r\n[%s]\r\n", body.to_string());
return true;
}
bool https_request::do_json(acl::http_request& req)
{
acl::json body;
if (!req.get_body(body, to_charset_)) {
logger_error("get http body error");
return false;
}
printf(">>>json body:\r\n[%s]\r\n", body.to_string().c_str());
return true;
}

View File

@ -0,0 +1,28 @@
#pragma once
class https_request : public acl::thread
{
public:
https_request(acl::sslbase_conf* ssl_conf, const char* addr,
const char* host, const char* url);
~https_request(void);
public:
// override
void* run(void);
private:
acl::http_request request_;
acl::string host_;
acl::string url_;
acl::string to_charset_;
// 处理 text/plain 类型数据
bool do_plain(acl::http_request& req);
// 处理 text/xml 类型数据
bool do_xml(acl::http_request& req);
// 处理 text/json 类型数据
bool do_json(acl::http_request& req);
};

View File

@ -0,0 +1,90 @@
#include "stdafx.h"
#include "util.h"
#include "https_request.h"
static void usage(const char* procname)
{
printf("usage: %s -h [help]\r\n"
" -s server_addr [default: 127.0.0.1:8888]\r\n"
" -H host\r\n"
" -U url\r\n"
" -c cocurrent [default: 1]\r\n"
" -n count [default: 10]\r\n", procname);
}
int main(int argc, char* argv[])
{
int ch, cocurrent = 1, count = 10;
acl::string server_addr("127.0.0.1:1443"), host;
acl::string url("/");
acl::acl_cpp_init();
acl::log::stdout_open(true);
while ((ch = getopt(argc, argv, "hs:c:n:H:U:")) > 0) {
switch (ch) {
case 'h':
usage(argv[0]);
return 0;
case 'c':
cocurrent = atoi(optarg);
break;
case 'n':
count = atoi(optarg);
break;
case 's':
server_addr = optarg;
break;
case 'H':
host = optarg;
break;
case 'U':
url = optarg;
break;
default:
break;
}
}
acl::sslbase_conf* ssl_conf = new acl::mbedtls_conf(false);
if (host.empty()) {
host = server_addr;
}
struct timeval begin;
gettimeofday(&begin, NULL);
std::list<https_request*> threads;
for (int i = 0; i < cocurrent; i++) {
https_request* thread = new https_request(
ssl_conf, server_addr, host, url);
thread->set_detachable(false);
threads.push_back(thread);
thread->start();
}
std::list<https_request*>::iterator it = threads.begin();
for (; it != threads.end(); ++it) {
if ((*it)->wait(NULL)) {
printf("wait thread(%lu) ok\r\n", (*it)->thread_id());
} else {
printf("wait one thread(%lu) error\r\n",
(*it)->thread_id());
}
delete *it;
}
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));
delete ssl_conf;
return 0;
}

View File

@ -0,0 +1,8 @@
// stdafx.cpp : 只包括标准包含文件的源文件
// master_threads.pch 将成为预编译头
// stdafx.obj 将包含预编译类型信息
#include "stdafx.h"
// TODO: <20> STDAFX.H <20>
//引用圈饰所需的附加头文件,而不是在此文件中引用

View File

@ -0,0 +1,19 @@
// stdafx.h : 标准系统包含文件的包含文件,
// 或是常用但不常更改的项目特定的包含文件
//
#pragma once
//#include <iostream>
//#include <tchar.h>
// TODO: 在此处引莹<E5BC95>序要求的附加头文件
#include "acl_cpp/lib_acl.hpp"
#include "lib_acl.h"
#ifdef WIN32
#define snprintf _snprintf
#endif

View File

@ -0,0 +1,7 @@
#!/bin/sh
./https_request -s www.sina.com.cn:443
echo ""
./https_request -s www.baidu.com:443
echo ""
./https_request -s echo.websocket.org:443

View File

@ -0,0 +1,3 @@
#!/bin/sh
./https_request -s www.sina.com.cn:443

View File

@ -271,8 +271,8 @@ bool polarssl_conf::load(void)
}
return true;
#else
logger_warn("link polarssl library in statis way!");
return false;
logger_warn("link polarssl library in static way!");
return true;
#endif
}