acl/lib_acl_cpp/samples/ssl/ssl_server/ssl_server.cpp

531 lines
13 KiB
C++
Raw Normal View History

// main.cpp : <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>̨Ӧ<CCA8>ó<EFBFBD><C3B3><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ڵ㡣
2014-11-19 00:25:21 +08:00
//
#include "stdafx.h"
#include <assert.h>
#include "lib_acl.h"
#include "acl_cpp/lib_acl.hpp"
2019-12-20 15:08:36 +08:00
#define USE_MBEDTLS
2014-11-19 00:25:21 +08:00
//////////////////////////////////////////////////////////////////////////
2019-12-20 15:08:36 +08:00
class http_servlet : public acl::HttpServlet
2014-11-19 00:25:21 +08:00
{
public:
http_servlet()
{
param1_ = NULL;
param2_ = NULL;
param3_ = NULL;
file1_ = NULL;
file2_ = NULL;
file3_ = NULL;
first_ = true;
}
~http_servlet(void)
{
}
2019-12-20 15:08:36 +08:00
// @override
bool doError(acl::HttpServletRequest&, acl::HttpServletResponse&)
2014-11-19 00:25:21 +08:00
{
2019-12-20 15:08:36 +08:00
if (first_) {
2014-11-19 00:25:21 +08:00
logger_error("first request error");
2019-12-20 15:08:36 +08:00
}
2014-11-19 00:25:21 +08:00
return false;
}
// GET <20><><EFBFBD><EFBFBD>
2019-12-20 15:08:36 +08:00
// @override
bool doGet(acl::HttpServletRequest& req, acl::HttpServletResponse& res)
2014-11-19 00:25:21 +08:00
{
bool ret = doPost(req, res);
2019-12-20 15:08:36 +08:00
if (ret == false) {
2014-11-19 00:25:21 +08:00
logger_error("doPost error!");
2019-12-20 15:08:36 +08:00
} else {
2014-11-19 00:25:21 +08:00
logger("doPost OK!");
2019-12-20 15:08:36 +08:00
}
2014-11-19 00:25:21 +08:00
return ret;
}
// POST <20><><EFBFBD><EFBFBD>
2019-12-20 15:08:36 +08:00
// @override
bool doPost(acl::HttpServletRequest& req, acl::HttpServletResponse& res)
2014-11-19 00:25:21 +08:00
{
first_ = false;
logger("request one now");
// <20><><EFBFBD><EFBFBD> HTTP <20><>Ӧͷ
2014-11-19 00:25:21 +08:00
res.addCookie("name1", "value1");
res.addCookie("name2", "value2", ".test.com", "/", 3600 * 24);
res.setChunkedTransferEncoding(true);
res.setKeepAlive(true);
// res.setStatus(400); // <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>÷<EFBFBD><C3B7>ص<EFBFBD>״̬<D7B4><CCAC>
2014-11-19 00:25:21 +08:00
// <20><><EFBFBD>ַ<EFBFBD>ʽ<EFBFBD><CABD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ַ<EFBFBD><D6B7><EFBFBD>
2019-12-20 15:08:36 +08:00
if (0) {
2014-11-19 00:25:21 +08:00
res.setContentType("text/xml; charset=gb2312");
2019-12-20 15:08:36 +08:00
} else {
2014-11-19 00:25:21 +08:00
res.setContentType("text/xml");
res.setCharacterEncoding("gb2312");
}
// <20><><EFBFBD><EFBFBD> HTTP <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ͣ<EFBFBD><CDA3><EFBFBD><EFBFBD><EFBFBD><EFBFBD>IJ<EFBFBD><C4B2><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ͣ<EFBFBD><CDA3><EFBFBD> name&value <20><>ʽ
// <20><><EFBFBD><EFBFBD> MIME <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ͣ<EFBFBD><CDA3><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
2019-12-20 15:08:36 +08:00
acl::http_request_t request_type = req.getRequestType();
if (request_type == acl::HTTP_REQUEST_NORMAL) {
2014-11-19 00:25:21 +08:00
return doParams(req, res);
2019-12-20 15:08:36 +08:00
} else if (request_type == acl::HTTP_REQUEST_MULTIPART_FORM) {
2014-11-19 00:25:21 +08:00
return doUpload(req, res);
2019-12-20 15:08:36 +08:00
}
assert(request_type == acl::HTTP_REQUEST_OCTET_STREAM);
2014-11-19 00:25:21 +08:00
return doOctetStream(req, res);
}
2019-12-20 15:08:36 +08:00
bool doResponse(acl::HttpServletRequest& req, acl::HttpServletResponse& res)
2014-11-19 00:25:21 +08:00
{
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> cookie ֵ
2014-11-19 00:25:21 +08:00
const char* cookie1 = req.getCookieValue("name1");
const char* cookie2 = req.getCookieValue("name2");
// <20><><EFBFBD><EFBFBD> xml <20><>ʽ<EFBFBD><CABD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
2019-12-20 15:08:36 +08:00
acl::xml1 body;
2014-11-19 00:25:21 +08:00
body.get_root().add_child("root", true)
.add_child("content_type", true)
.add_attr("type", (int) req.getRequestType())
.get_parent()
.add_child("cookies", true)
.add_child("cookie", true)
.add_attr("name1", cookie1 ? cookie1 : "null")
.get_parent()
.add_child("cookie", true)
.add_attr("name2", cookie2 ? cookie2 : "null")
.get_parent()
.get_parent()
.add_child("params", true)
.add_child("param", true)
.add_attr("name1", param1_ ? param1_ : "null")
.get_parent()
.add_child("param", true)
.add_attr("name2", param2_ ? param2_ : "null")
.get_parent()
.add_child("param", true)
.add_attr("name3", param3_ ? param3_ : "null")
.get_parent()
.get_parent()
.add_child("files", true)
.add_child("file", true)
.add_attr("filename", file1_ ? file1_ : "null")
.get_parent()
.add_child("file", true)
.add_attr("filename", file2_ ? file2_ : "null")
.get_parent()
.add_child("file", true)
.add_attr("filename", file3_ ? file3_ : "null");
2019-12-20 15:08:36 +08:00
acl::string buf("<?xml version=\"1.0\"?>");
2014-11-19 00:25:21 +08:00
body.build_xml(buf);
//printf(">>>response: %s\r\n", buf.c_str());
//res.setContentLength(buf.length());
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʾ<EFBFBD><CABE><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> http <20><>Ӧͷ
2014-11-19 00:25:21 +08:00
//if (res.sendHeader() == false)
// return false;
// <20><><EFBFBD><EFBFBD> http <20><>Ӧ<EFBFBD><EFBFBD><E5A3AC>ʹ<EFBFBD><CAB9> chunk <20><><EFBFBD><EFBFBD>ʱ<EFBFBD><CAB1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD>η<EFBFBD><CEB7>Ϳ<EFBFBD><CDBF><EFBFBD><EFBFBD><EFBFBD>
2019-12-20 15:08:36 +08:00
if (res.write(buf) == false || res.write(NULL, 0) == false) {
2014-11-19 00:25:21 +08:00
return false;
2019-12-20 15:08:36 +08:00
}
2014-11-19 00:25:21 +08:00
return true;
}
// GET <20><>ʽ<EFBFBD><CABD> POST <20><>ʽ<EFBFBD><CABD><EFBFBD><EFBFBD><EFBFBD>
2014-11-19 00:25:21 +08:00
// Content-Type: application/x-www-form-urlencoded
2019-12-20 15:08:36 +08:00
bool doParams(acl::HttpServletRequest& req, acl::HttpServletResponse& res)
2014-11-19 00:25:21 +08:00
{
param1_ = req.getParameter("name1");
param2_ = req.getParameter("name2");
2020-01-07 18:33:44 +08:00
param3_ = req.getParameter("name3");
2014-11-19 00:25:21 +08:00
return doResponse(req, res);
}
// POST <20><>ʽ<EFBFBD><CABD><EFBFBD><EFBFBD><EFBFBD>
2014-11-19 00:25:21 +08:00
// Content-Type: multipart/form-data; boundary=xxx
2019-12-20 15:08:36 +08:00
bool doUpload(acl::HttpServletRequest& req, acl::HttpServletResponse& res)
2014-11-19 00:25:21 +08:00
{
// <20>Ȼ<EFBFBD><C8BB><EFBFBD> Content-Type <20><>Ӧ<EFBFBD><D3A6> http_ctype <20><><EFBFBD><EFBFBD>
2019-12-20 15:08:36 +08:00
acl::http_mime* mime = req.getHttpMime();
if (mime == NULL) {
2014-11-19 00:25:21 +08:00
logger_error("http_mime null");
return false;
}
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ij<EFBFBD><C4B3><EFBFBD>
2014-11-19 00:25:21 +08:00
long long int len = req.getContentLength();
2019-12-20 15:08:36 +08:00
if (len <= 0) {
2014-11-19 00:25:21 +08:00
logger_error("body empty");
return false;
}
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
2019-12-20 15:08:36 +08:00
acl::istream& in = req.getInputStream();
2014-11-19 00:25:21 +08:00
char buf[8192];
int ret;
bool finish = false;
const char* filepath = "./var/mime_file";
2019-12-20 15:08:36 +08:00
acl::ofstream out;
2014-11-19 00:25:21 +08:00
out.open_write(filepath);
// <20><><EFBFBD><EFBFBD>ԭʼ<D4AD>ļ<EFBFBD><C4BC><EFBFBD><EFBFBD><EFBFBD>·<EFBFBD><C2B7>
2014-11-19 00:25:21 +08:00
mime->set_saved_path(filepath);
size_t k;
// <20><>ȡ HTTP <20>ͻ<EFBFBD><CDBB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
2019-12-20 15:08:36 +08:00
while (len > 0) {
2014-11-19 00:25:21 +08:00
k = (size_t) len > sizeof(buf) ? sizeof(buf) : (size_t) len;
ret = in.read(buf, k, false);
2019-12-20 15:08:36 +08:00
if (ret == -1) {
2014-11-19 00:25:21 +08:00
logger_error("read POST data error");
return false;
}
out.write(buf, ret);
len -= ret;
// <20><><EFBFBD><EFBFBD><EFBFBD>õ<EFBFBD><C3B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>н<EFBFBD><D0BD><EFBFBD>
2019-12-20 15:08:36 +08:00
if (!finish && mime->update(buf, ret) == true) {
2014-11-19 00:25:21 +08:00
finish = true;
2019-12-20 15:08:36 +08:00
}
2014-11-19 00:25:21 +08:00
}
out.close();
2019-12-20 15:08:36 +08:00
if (len != 0 || finish == false) {
2014-11-19 00:25:21 +08:00
logger_warn("not read all data from client");
2019-12-20 15:08:36 +08:00
}
2014-11-19 00:25:21 +08:00
param1_ = req.getParameter("name1");
param2_ = req.getParameter("name2");
param3_ = req.getParameter("name3");
2019-12-20 15:08:36 +08:00
acl::string path;
2014-11-19 00:25:21 +08:00
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>е<EFBFBD> MIME <20><><EFBFBD><EFBFBD>ҳ<EFBFBD><D2B3><EFBFBD><EFBFBD><EFBFBD>Ϊ<EFBFBD>ļ<EFBFBD><C4BC><EFBFBD><EFBFBD><EFBFBD><EFBFBD>IJ<EFBFBD><C4B2>ֽ<EFBFBD><D6BD><EFBFBD>ת<EFBFBD><D7AA>
2019-12-20 15:08:36 +08:00
const std::list<acl::http_mime_node*>& nodes = mime->get_nodes();
std::list<acl::http_mime_node*>::const_iterator cit = nodes.begin();
for (; cit != nodes.end(); ++cit) {
2014-11-19 00:25:21 +08:00
const char* name = (*cit)->get_name();
2019-12-20 15:08:36 +08:00
if (name == NULL) {
2014-11-19 00:25:21 +08:00
continue;
2019-12-20 15:08:36 +08:00
}
2014-11-19 00:25:21 +08:00
2019-12-20 15:08:36 +08:00
acl::http_mime_t mime_type = (*cit)->get_mime_type();
if (mime_type == acl::HTTP_MIME_FILE) {
2014-11-19 00:25:21 +08:00
const char* filename = (*cit)->get_filename();
2019-12-20 15:08:36 +08:00
if (filename == NULL) {
2014-11-19 00:25:21 +08:00
logger("filename null");
continue;
}
2019-12-20 15:08:36 +08:00
if (strcmp(name, "file1") == 0) {
2014-11-19 00:25:21 +08:00
file1_ = filename;
2019-12-20 15:08:36 +08:00
} else if (strcmp(name, "file2") == 0) {
2014-11-19 00:25:21 +08:00
file2_ = filename;
2019-12-20 15:08:36 +08:00
} else if (strcmp(name, "file3") == 0) {
2014-11-19 00:25:21 +08:00
file3_ = filename;
2019-12-20 15:08:36 +08:00
}
2014-11-19 00:25:21 +08:00
// <20>е<EFBFBD><D0B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>IE<49><45><EFBFBD>ϴ<EFBFBD><CFB4>ļ<EFBFBD>ʱ<EFBFBD><CAB1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ļ<EFBFBD>·<EFBFBD><C2B7><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
// <20><>Ҫ<EFBFBD>Ƚ<EFBFBD>·<EFBFBD><C2B7>ȥ<EFBFBD><C8A5>
2014-11-19 00:25:21 +08:00
filename = acl_safe_basename(filename);
#ifdef WIN32
path.format("var\\%s", filename);
#else
path.format("./var/%s", filename);
#endif
(void) (*cit)->save(path.c_str());
}
}
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ص<EFBFBD>ij<EFBFBD><C4B3><EFBFBD>ļ<EFBFBD><C4BC><EFBFBD>ת<EFBFBD><D7AA>
2019-12-20 15:08:36 +08:00
const acl::http_mime_node* node = mime->get_node("file1");
if (node && node->get_mime_type() == acl::HTTP_MIME_FILE) {
2014-11-19 00:25:21 +08:00
const char* ptr = node->get_filename();
2019-12-20 15:08:36 +08:00
if (ptr) {
// <20>е<EFBFBD><D0B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>IE<49><45><EFBFBD>ϴ<EFBFBD><CFB4>ļ<EFBFBD>ʱ<EFBFBD><CAB1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ļ<EFBFBD>·<EFBFBD><C2B7><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
// <20><>Ҫ<EFBFBD>Ƚ<EFBFBD>·<EFBFBD><C2B7>ȥ<EFBFBD><C8A5>
2014-11-19 00:25:21 +08:00
ptr = acl_safe_basename(ptr);
#ifdef WIN32
path.format(".\\var\\1_%s", ptr);
#else
path.format("./var/1_%s", ptr);
#endif
(void) node->save(path.c_str());
}
}
return doResponse(req, res);
}
// POST <20><>ʽ<EFBFBD><CABD><EFBFBD><EFBFBD><EFBFBD>
2014-11-19 00:25:21 +08:00
// Content-Type: application/octet-stream
2019-12-20 15:08:36 +08:00
bool doOctetStream(acl::HttpServletRequest&, acl::HttpServletResponse&)
2014-11-19 00:25:21 +08:00
{
logger_error("not support now!");
return false;
}
protected:
private:
const char* param1_;
const char* param2_;
const char* param3_;
const char* file1_;
const char* file2_;
const char* file3_;
bool first_;
};
//////////////////////////////////////////////////////////////////////////
2019-12-20 15:08:36 +08:00
static void do_run(acl::socket_stream* stream)
2014-11-19 00:25:21 +08:00
{
2019-12-20 15:08:36 +08:00
acl::memcache_session session("127.0.0.1:11211");
2014-11-19 00:25:21 +08:00
http_servlet servlet;
servlet.setLocalCharset("gb2312");
servlet.doRun(session, stream);
}
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʽ<EFBFBD><CABD><EFBFBD><EFBFBD>ʱ<EFBFBD>ķ<EFBFBD><C4B7><EFBFBD><EFBFBD><EFBFBD>
2019-12-20 15:08:36 +08:00
class master_service : public acl::master_proc
2014-11-19 00:25:21 +08:00
{
public:
master_service(const char* crt_file, const char* key_file,
2019-12-20 15:08:36 +08:00
#ifdef USE_MBEDTLS
acl::mbedtls_verify_t verify_mode)
#else
2014-11-19 00:25:21 +08:00
acl::polarssl_verify_t verify_mode)
2019-12-20 15:08:36 +08:00
#endif
2014-11-19 00:25:21 +08:00
{
2019-12-20 15:08:36 +08:00
if (crt_file && *crt_file && key_file && *key_file) {
2014-11-19 00:25:21 +08:00
crt_file_ = crt_file;
key_file_ = key_file;
2019-12-20 15:08:36 +08:00
#ifdef USE_MBEDTLS
conf_ = new acl::mbedtls_conf(true);
#else
conf_ = new acl::polarssl_conf(true);
2019-12-20 15:08:36 +08:00
#endif
} else {
2014-11-19 00:25:21 +08:00
conf_ = NULL;
2019-12-20 15:08:36 +08:00
}
2014-11-19 00:25:21 +08:00
verify_mode_ = verify_mode;
}
~master_service()
{
2019-12-20 15:08:36 +08:00
delete conf_;
2014-11-19 00:25:21 +08:00
}
protected:
2019-12-20 15:08:36 +08:00
// @override
void on_accept(acl::socket_stream* stream)
2014-11-19 00:25:21 +08:00
{
stream->set_rw_timeout(60);
2020-01-02 23:29:06 +08:00
printf("accept one fd=%d\r\n", stream->sock_handle());
2014-11-19 00:25:21 +08:00
2019-12-20 15:08:36 +08:00
if (conf_) {
// <20><><EFBFBD><EFBFBD>ʹ<EFBFBD><CAB9> SSL <20><>ʽ<EFBFBD><CABD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ҫ<EFBFBD><D2AA> SSL IO <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ע<EFBFBD><D7A2>
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>У<EFBFBD><D0A3><EFBFBD><EFBFBD><EFBFBD> ssl io <20>滻 stream <20><>Ĭ<EFBFBD><C4AC>
// <20>ĵײ<C4B5> IO <20><><EFBFBD><EFBFBD>
2014-11-19 00:25:21 +08:00
logger("begin setup ssl hook...");
acl::sslbase_io* ssl = conf_->create(false);
2019-12-20 15:08:36 +08:00
if (stream->setup_hook(ssl) == ssl) {
2014-11-19 00:25:21 +08:00
logger_error("setup_hook error!");
ssl->destroy();
2019-12-20 15:08:36 +08:00
} else {
2014-11-19 00:25:21 +08:00
logger("setup ssl hook ok");
2019-12-20 15:08:36 +08:00
}
2014-11-19 00:25:21 +08:00
}
2014-11-19 00:25:21 +08:00
do_run(stream);
}
2019-12-20 15:08:36 +08:00
// @override
void proc_on_init()
2014-11-19 00:25:21 +08:00
{
2019-12-20 15:08:36 +08:00
if (conf_ == NULL) {
2014-11-19 00:25:21 +08:00
return;
2019-12-20 15:08:36 +08:00
}
2014-11-19 00:25:21 +08:00
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>˵<EFBFBD> SSL <20><EFBFBD><E1BBB0><EFBFBD><EFBFBD><E6B9A6>
2020-01-08 16:00:41 +08:00
conf_->enable_cache(true);
2014-11-19 00:25:21 +08:00
// <20><><EFBFBD>ӱ<EFBFBD><D3B1>ط<EFBFBD><D8B7><EFBFBD><EFBFBD><EFBFBD>֤<EFBFBD><D6A4>
2019-12-20 15:08:36 +08:00
if (conf_->add_cert(crt_file_.c_str()) == false) {
2014-11-19 00:25:21 +08:00
logger_error("add cert failed, crt: %s, key: %s",
crt_file_.c_str(), key_file_.c_str());
delete conf_;
conf_ = NULL;
return;
}
2019-12-20 15:08:36 +08:00
logger("load cert ok, crt: %s", crt_file_.c_str());
2014-11-19 00:25:21 +08:00
// <20><><EFBFBD>ӱ<EFBFBD><D3B1>ط<EFBFBD><D8B7><EFBFBD><EFBFBD><EFBFBD>Կ
2019-12-20 15:08:36 +08:00
if (conf_->set_key(key_file_.c_str()) == false) {
2014-11-19 00:25:21 +08:00
logger_error("set private key error");
delete conf_;
conf_ = NULL;
2019-12-20 15:08:36 +08:00
return;
2014-11-19 00:25:21 +08:00
}
2019-12-20 15:08:36 +08:00
logger("set key ok, key: %s", key_file_.c_str());
2014-11-19 00:25:21 +08:00
2019-12-20 15:08:36 +08:00
#ifndef USE_MBEDTLS
2014-11-19 00:25:21 +08:00
conf_->set_authmode(verify_mode_);
2019-12-20 15:08:36 +08:00
#endif
2014-11-19 00:25:21 +08:00
}
private:
2019-12-20 15:08:36 +08:00
#ifdef USE_MBEDTLS
acl::mbedtls_conf* conf_;
acl::mbedtls_verify_t verify_mode_;
#else
acl::polarssl_conf* conf_;
2014-11-19 00:25:21 +08:00
acl::polarssl_verify_t verify_mode_;
2019-12-20 15:08:36 +08:00
#endif
acl::string crt_file_;
acl::string key_file_;
2014-11-19 00:25:21 +08:00
};
// WEB <20><><EFBFBD><EFBFBD>ģʽ
2014-11-19 00:25:21 +08:00
static void do_alone(const char* crt_file, const char* key_file,
2019-12-20 15:08:36 +08:00
#ifdef USE_MBEDTLS
acl::mbedtls_verify_t verify_mode)
#else
2014-11-19 00:25:21 +08:00
acl::polarssl_verify_t verify_mode)
2019-12-20 15:08:36 +08:00
#endif
2014-11-19 00:25:21 +08:00
{
master_service service(crt_file, key_file, verify_mode);
acl::log::stdout_open(true);
const char* addr = "0.0.0.0:2443";
printf("listen: %s ...\r\n", addr);
service.run_alone(addr, NULL, 0); // <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>з<EFBFBD>ʽ
2014-11-19 00:25:21 +08:00
}
// WEB CGI ģʽ
2014-11-19 00:25:21 +08:00
static void do_cgi(void)
{
do_run(NULL);
}
//////////////////////////////////////////////////////////////////////////
int main(int argc, char* argv[])
{
#ifdef WIN32
acl::acl_cpp_init();
#endif
acl::log::stdout_open(true);
2019-12-20 15:08:36 +08:00
#ifdef USE_MBEDTLS
# ifdef __APPLE__
2020-01-07 18:45:52 +08:00
# define USE_ONE
# ifdef USE_ONE
const char* libssl_path = "../libmbedtls_all.dylib";
acl::mbedtls_conf::set_libpath(libssl_path);
# else
const char* libcrypto_path = "../libmbedcrypto.dylib";
const char* libx509_path = "../libmbedx509.dylib";
const char* libssl_path = "../libmbedtls.dylib";
2020-01-07 18:33:44 +08:00
acl::mbedtls_conf::set_libpath(libcrypto_path, libx509_path, libssl_path);
2020-01-07 18:45:52 +08:00
# endif
2020-01-07 18:33:44 +08:00
# elif defined(_WIN32) || defined(_WIN64)
const char* libssl_path = "../mbedtls.dll";
acl::mbedtls_conf::set_libpath(libssl_path); // һ<><D2BB><EFBFBD>ܵĶ<DCB5>̬<EFBFBD><CCAC>
2020-01-07 18:41:01 +08:00
# elif defined(__linux__)
2020-01-07 18:41:38 +08:00
# define USE_ONE
2020-01-07 18:41:01 +08:00
# ifdef USE_ONE
const char* libssl_path = "../libmbedtls_all.so";
acl::mbedtls_conf::set_libpath(libssl_path);
# else
const char* libcrypto_path = "../libmbedcrypto.so";
const char* libx509_path = "../libmbedx509.so";
const char* libssl_path = "../libmbedtls.so";
acl::mbedtls_conf::set_libpath(libcrypto_path, libx509_path, libssl_path);
2020-01-07 18:41:01 +08:00
# endif
2020-01-07 18:33:44 +08:00
# endif
2020-01-02 23:29:06 +08:00
if (!acl::mbedtls_conf::load()) {
printf("load %s error\r\n", libssl_path);
return 1;
}
2019-12-20 15:08:36 +08:00
#else
# ifdef __APPLE__
2020-01-02 23:29:06 +08:00
const char* libssl_path = "../libpolarssl.dylib";
acl::polarssl_conf::set_libpath(libssl_path);
2019-12-20 15:08:36 +08:00
# else
2020-01-02 23:29:06 +08:00
const char* libssl_path = "../libpolarssl.so";
acl::polarssl_conf::set_libpath(libssl_path);
2019-12-20 15:08:36 +08:00
# endif
2020-01-02 23:29:06 +08:00
if (!acl::polarssl_conf::load()) {
printf("load %s error\r\n", libssl_path);
return 1;
}
2019-12-20 15:08:36 +08:00
#endif
// <20><>ʼ<EFBFBD><CABC><EFBFBD><EFBFBD>
2019-12-20 15:08:36 +08:00
if (argc >= 2 && strcmp(argv[1], "alone") == 0) {
#ifdef USE_MBEDTLS
acl::mbedtls_verify_t verify_mode;
#else
2014-11-19 00:25:21 +08:00
acl::polarssl_verify_t verify_mode;
2019-12-20 15:08:36 +08:00
#endif
2014-11-19 00:25:21 +08:00
const char* crt_file, *key_file;
2019-12-20 15:08:36 +08:00
if (argc >= 3) {
2014-11-19 00:25:21 +08:00
crt_file = argv[2];
2019-12-20 15:08:36 +08:00
} else {
2014-11-19 00:25:21 +08:00
crt_file = NULL;
2019-12-20 15:08:36 +08:00
}
if (argc >= 4) {
2014-11-19 00:25:21 +08:00
key_file = argv[3];
2019-12-20 15:08:36 +08:00
} else {
2014-11-19 00:25:21 +08:00
key_file = NULL;
2019-12-20 15:08:36 +08:00
}
#ifdef USE_MBEDTLS
if (argc >= 5) {
if (strcasecmp(argv[4], "req") == 0) {
verify_mode = acl::MBEDTLS_VERIFY_REQ;
} else if (strcasecmp(argv[4], "opt") == 0) {
verify_mode = acl::MBEDTLS_VERIFY_OPT;
} else {
verify_mode = acl::MBEDTLS_VERIFY_NONE;
}
} else {
verify_mode = acl::MBEDTLS_VERIFY_NONE;
}
#else
if (argc >= 5) {
if (strcasecmp(argv[4], "req") == 0) {
2014-11-19 00:25:21 +08:00
verify_mode = acl::POLARSSL_VERIFY_REQ;
2019-12-20 15:08:36 +08:00
} else if (strcasecmp(argv[4], "opt") == 0) {
2014-11-19 00:25:21 +08:00
verify_mode = acl::POLARSSL_VERIFY_OPT;
2019-12-20 15:08:36 +08:00
} else {
2014-11-19 00:25:21 +08:00
verify_mode = acl::POLARSSL_VERIFY_NONE;
2019-12-20 15:08:36 +08:00
}
} else {
2014-11-19 00:25:21 +08:00
verify_mode = acl::POLARSSL_VERIFY_NONE;
2019-12-20 15:08:36 +08:00
}
#endif
2014-11-19 00:25:21 +08:00
do_alone(crt_file, key_file, verify_mode);
2019-12-20 15:08:36 +08:00
} else if (argc >= 2) {
2014-11-19 00:25:21 +08:00
printf("usage: %s alone cert_file key_file verify[none|req|opt]\r\n", argv[0]);
2019-12-20 15:08:36 +08:00
} else {
2014-11-19 00:25:21 +08:00
do_cgi();
2019-12-20 15:08:36 +08:00
}
2014-11-19 00:25:21 +08:00
return 0;
}