diff --git a/lib_acl/samples/rfc1035/main.c b/lib_acl/samples/rfc1035/main.c index 44fcbc892..caa61ba0c 100644 --- a/lib_acl/samples/rfc1035/main.c +++ b/lib_acl/samples/rfc1035/main.c @@ -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[]) diff --git a/lib_acl_cpp/Makefile b/lib_acl_cpp/Makefile index 1a063fbd3..0a53765e2 100644 --- a/lib_acl_cpp/Makefile +++ b/lib_acl_cpp/Makefile @@ -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 \ diff --git a/lib_acl_cpp/samples/ssl/https_request_static/Makefile b/lib_acl_cpp/samples/ssl/https_request_static/Makefile new file mode 100644 index 000000000..d5671c9e2 --- /dev/null +++ b/lib_acl_cpp/samples/ssl/https_request_static/Makefile @@ -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 diff --git a/lib_acl_cpp/samples/ssl/https_request_static/https_request.cpp b/lib_acl_cpp/samples/ssl/https_request_static/https_request.cpp new file mode 100644 index 000000000..65191d277 --- /dev/null +++ b/lib_acl_cpp/samples/ssl/https_request_static/https_request.cpp @@ -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; +} diff --git a/lib_acl_cpp/samples/ssl/https_request_static/https_request.h b/lib_acl_cpp/samples/ssl/https_request_static/https_request.h new file mode 100644 index 000000000..9eec0499f --- /dev/null +++ b/lib_acl_cpp/samples/ssl/https_request_static/https_request.h @@ -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); +}; diff --git a/lib_acl_cpp/samples/ssl/https_request_static/main.cpp b/lib_acl_cpp/samples/ssl/https_request_static/main.cpp new file mode 100644 index 000000000..2f02ce038 --- /dev/null +++ b/lib_acl_cpp/samples/ssl/https_request_static/main.cpp @@ -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 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::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; +} diff --git a/lib_acl_cpp/samples/ssl/https_request_static/stdafx.cpp b/lib_acl_cpp/samples/ssl/https_request_static/stdafx.cpp new file mode 100644 index 000000000..285e429ba --- /dev/null +++ b/lib_acl_cpp/samples/ssl/https_request_static/stdafx.cpp @@ -0,0 +1,8 @@ +// stdafx.cpp : 只包括标准包含文件的源文件 +// master_threads.pch 将成为预编译头 +// stdafx.obj 将包含预编译类型信息 + +#include "stdafx.h" + +// TODO: STDAFX.H +//引用圈饰所需的附加头文件,而不是在此文件中引用 diff --git a/lib_acl_cpp/samples/ssl/https_request_static/stdafx.h b/lib_acl_cpp/samples/ssl/https_request_static/stdafx.h new file mode 100644 index 000000000..453050b1c --- /dev/null +++ b/lib_acl_cpp/samples/ssl/https_request_static/stdafx.h @@ -0,0 +1,19 @@ +// stdafx.h : 标准系统包含文件的包含文件, +// 或是常用但不常更改的项目特定的包含文件 +// + +#pragma once + + +//#include +//#include + +// TODO: 在此处引莹序要求的附加头文件 + +#include "acl_cpp/lib_acl.hpp" +#include "lib_acl.h" + +#ifdef WIN32 +#define snprintf _snprintf +#endif + diff --git a/lib_acl_cpp/samples/ssl/https_request_static/t.sh b/lib_acl_cpp/samples/ssl/https_request_static/t.sh new file mode 100755 index 000000000..2e5539a84 --- /dev/null +++ b/lib_acl_cpp/samples/ssl/https_request_static/t.sh @@ -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 diff --git a/lib_acl_cpp/samples/ssl/https_request_static/tt.sh b/lib_acl_cpp/samples/ssl/https_request_static/tt.sh new file mode 100755 index 000000000..978c36a51 --- /dev/null +++ b/lib_acl_cpp/samples/ssl/https_request_static/tt.sh @@ -0,0 +1,3 @@ +#!/bin/sh + +./https_request -s www.sina.com.cn:443 diff --git a/lib_acl_cpp/src/stream/polarssl_conf.cpp b/lib_acl_cpp/src/stream/polarssl_conf.cpp index afba41635..39f331f24 100644 --- a/lib_acl_cpp/src/stream/polarssl_conf.cpp +++ b/lib_acl_cpp/src/stream/polarssl_conf.cpp @@ -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 }