2019-07-28 10:31:56 +08:00
|
|
|
#include "stdafx.h"
|
2018-11-30 14:38:22 +08:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include "http_servlet.h"
|
|
|
|
|
2020-01-05 20:23:34 +08:00
|
|
|
#define STACK_SIZE 128000
|
2018-11-30 14:38:22 +08:00
|
|
|
|
|
|
|
static int __rw_timeout = 0;
|
|
|
|
static acl::string __ssl_crt("./ssl_crt.pem");
|
|
|
|
static acl::string __ssl_key("./ssl_key.pem");
|
2020-01-05 20:23:34 +08:00
|
|
|
static acl::sslbase_conf* __ssl_conf;
|
2018-11-30 14:38:22 +08:00
|
|
|
|
|
|
|
static void http_server(ACL_FIBER *, void *ctx)
|
|
|
|
{
|
|
|
|
acl::socket_stream *conn = (acl::socket_stream *) ctx;
|
|
|
|
|
|
|
|
printf("start one http_server\r\n");
|
|
|
|
|
2020-07-16 09:12:39 +08:00
|
|
|
acl::sslbase_io* ssl = __ssl_conf->create(false);
|
2018-11-30 14:38:22 +08:00
|
|
|
|
2020-01-05 20:23:34 +08:00
|
|
|
if (conn->setup_hook(ssl) == ssl) {
|
2018-11-30 14:38:22 +08:00
|
|
|
printf("setup_hook error\r\n");
|
|
|
|
ssl->destroy();
|
|
|
|
delete conn;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
#if 0
|
2020-01-05 20:23:34 +08:00
|
|
|
if (!ssl->handshake()) {
|
2018-11-30 14:38:22 +08:00
|
|
|
printf("ssl handshake error\r\n");
|
|
|
|
ssl->destroy();
|
|
|
|
delete conn;
|
|
|
|
return;
|
|
|
|
}
|
2020-01-05 20:23:34 +08:00
|
|
|
if (!ssl->handshake_ok()) {
|
2018-11-30 14:38:22 +08:00
|
|
|
printf("ssl handshake error\r\n");
|
|
|
|
ssl->destroy();
|
|
|
|
delete conn;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
printf("ssl handshake_ok\r\n");
|
|
|
|
|
|
|
|
acl::memcache_session session("127.0.0.1:11211");
|
|
|
|
http_servlet servlet(conn, &session);
|
|
|
|
servlet.setLocalCharset("gb2312");
|
|
|
|
|
2020-01-05 20:23:34 +08:00
|
|
|
while (true) {
|
|
|
|
if (!servlet.doRun()) {
|
2018-11-30 14:38:22 +08:00
|
|
|
break;
|
2020-01-05 20:23:34 +08:00
|
|
|
}
|
2018-11-30 14:38:22 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
printf("close one connection: %d, %s\r\n", conn->sock_handle(),
|
|
|
|
acl::last_serror());
|
|
|
|
delete conn;
|
|
|
|
}
|
|
|
|
|
2020-01-05 20:23:34 +08:00
|
|
|
static void ssl_init(acl::sslbase_conf& conf, const acl::string& crt,
|
2018-11-30 14:38:22 +08:00
|
|
|
const acl::string& key)
|
|
|
|
{
|
|
|
|
conf.enable_cache(1);
|
|
|
|
|
2020-01-05 20:23:34 +08:00
|
|
|
if (!conf.add_cert(crt)) {
|
2018-11-30 14:38:22 +08:00
|
|
|
printf("load %s error\r\n", crt.c_str());
|
|
|
|
exit (1);
|
|
|
|
}
|
|
|
|
|
2020-01-05 20:23:34 +08:00
|
|
|
if (!conf.set_key(key)) {
|
2018-11-30 14:38:22 +08:00
|
|
|
printf("set_key %s error\r\n", key.c_str());
|
|
|
|
exit (1);
|
|
|
|
}
|
|
|
|
|
|
|
|
printf(">>> ssl_init ok, ssl_crt: %s, ssl_key: %s\r\n",
|
|
|
|
crt.c_str(), key.c_str());
|
|
|
|
}
|
|
|
|
|
|
|
|
static void fiber_accept(ACL_FIBER *, void *ctx)
|
|
|
|
{
|
|
|
|
const char* addr = (const char* ) ctx;
|
|
|
|
acl::server_socket server;
|
|
|
|
|
2020-01-05 20:23:34 +08:00
|
|
|
ssl_init(*__ssl_conf, __ssl_crt, __ssl_key);
|
2018-11-30 14:38:22 +08:00
|
|
|
|
2020-01-05 20:23:34 +08:00
|
|
|
if (!server.open(addr)) {
|
2018-11-30 14:38:22 +08:00
|
|
|
printf("open %s error\r\n", addr);
|
|
|
|
exit (1);
|
2020-01-05 20:23:34 +08:00
|
|
|
} else {
|
2018-11-30 14:38:22 +08:00
|
|
|
printf(">>> listen %s ok\r\n", addr);
|
2020-01-05 20:23:34 +08:00
|
|
|
}
|
2018-11-30 14:38:22 +08:00
|
|
|
|
2020-01-05 20:23:34 +08:00
|
|
|
while (true) {
|
2018-11-30 14:38:22 +08:00
|
|
|
acl::socket_stream* client = server.accept();
|
2020-01-05 20:23:34 +08:00
|
|
|
if (client == NULL) {
|
2018-11-30 14:38:22 +08:00
|
|
|
printf("accept failed: %s\r\n", acl::last_serror());
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
client->set_rw_timeout(__rw_timeout);
|
|
|
|
printf("accept one: %d\r\n", client->sock_handle());
|
|
|
|
acl_fiber_create(http_server, client, STACK_SIZE);
|
|
|
|
}
|
|
|
|
|
|
|
|
exit (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void usage(const char* procname)
|
|
|
|
{
|
|
|
|
printf("usage: %s -h [help]\r\n"
|
2020-01-05 20:23:34 +08:00
|
|
|
" -l ssl_lib_path\r\n"
|
2018-11-30 14:38:22 +08:00
|
|
|
" -s listen_addr\r\n"
|
|
|
|
" -r rw_timeout\r\n"
|
|
|
|
" -c ssl_crt.pem\r\n"
|
|
|
|
" -k ssl_key.pem\r\n", procname);
|
|
|
|
}
|
|
|
|
|
|
|
|
int main(int argc, char *argv[])
|
|
|
|
{
|
2020-01-05 20:23:34 +08:00
|
|
|
acl::string addr(":9001");
|
|
|
|
#ifdef __APPLE__
|
|
|
|
acl::string libpath("../libmbedtls_all.dylib");
|
|
|
|
#else
|
|
|
|
acl::string libpath("../libmbedtls_all.so");
|
|
|
|
#endif
|
2018-11-30 14:38:22 +08:00
|
|
|
int ch;
|
|
|
|
|
|
|
|
acl::acl_cpp_init();
|
|
|
|
acl::log::stdout_open(true);
|
|
|
|
|
2020-01-05 20:23:34 +08:00
|
|
|
while ((ch = getopt(argc, argv, "hs:r:c:k:l:")) > 0) {
|
|
|
|
switch (ch) {
|
2018-11-30 14:38:22 +08:00
|
|
|
case 'h':
|
|
|
|
usage(argv[0]);
|
|
|
|
return 0;
|
|
|
|
case 's':
|
|
|
|
addr = optarg;
|
|
|
|
break;
|
|
|
|
case 'r':
|
|
|
|
__rw_timeout = atoi(optarg);
|
|
|
|
break;
|
|
|
|
case 'c':
|
|
|
|
__ssl_crt = optarg;
|
|
|
|
break;
|
|
|
|
case 'k':
|
|
|
|
__ssl_key = optarg;
|
|
|
|
break;
|
|
|
|
case 'l':
|
|
|
|
libpath = optarg;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-05 20:23:34 +08:00
|
|
|
if (libpath.find("mbedtls") != NULL) {
|
2020-01-06 15:29:03 +08:00
|
|
|
const std::vector<acl::string>& libs = libpath.split2("; \t");
|
|
|
|
if (libs.size() != 3) {
|
|
|
|
printf("invalid libpath=%s\r\n", libpath.c_str());
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
acl::mbedtls_conf::set_libpath(libs[0], libs[1], libs[2]);
|
2020-01-05 20:23:34 +08:00
|
|
|
if (acl::mbedtls_conf::load()) {
|
|
|
|
__ssl_conf = new acl::mbedtls_conf(true);
|
|
|
|
} else {
|
|
|
|
printf("load %s error\r\n", libpath.c_str());
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
} else if (libpath.find("polarlssl") != NULL) {
|
2018-11-30 14:38:22 +08:00
|
|
|
acl::polarssl_conf::set_libpath(libpath);
|
2020-01-05 20:23:34 +08:00
|
|
|
if (acl::polarssl_conf::load()) {
|
|
|
|
__ssl_conf = new acl::polarssl_conf;
|
|
|
|
} else {
|
|
|
|
printf("load %s error\r\n", libpath.c_str());
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
printf("invalid ssl lib=%s\r\n", libpath.c_str());
|
|
|
|
return 1;
|
|
|
|
}
|
2018-11-30 14:38:22 +08:00
|
|
|
|
|
|
|
acl_fiber_create(fiber_accept, addr.c_str(), STACK_SIZE);
|
|
|
|
acl_fiber_schedule();
|
2020-01-05 20:23:34 +08:00
|
|
|
delete __ssl_conf;
|
2018-11-30 14:38:22 +08:00
|
|
|
return 0;
|
|
|
|
}
|