关于mongoose由http转换成https在Linux下笔记(2)
重新梳理一下mongoose的源码程序,mongoose的源码只有mongoose.c和mongoose.h两个文件。
但是在mongoose6.4的版本上mongoose.h里已经定义了使用ssl的宏定义,用户只需要在 #define MG_ENABLE_SSL //使能https
mongoose的提交发布的网址:https://github.com/cesanta/mongoose/releases 这个网址可以查看发布的历史,
点击箭头进入找到源码,
然后按照github的例子进行配置始终编译不过,github的源码如下
文件名称叫 simplest_web_server_ssl.c
/*
* Copyright (c) 2016 Cesanta Software Limited
* All rights reserved
*/
#if MG_ENABLE_SSL
/*
* This example starts an SSL web server on https://localhost:8443/
*
* Please note that the certificate used is a self-signed one and will not be
* recognised as valid. You should expect an SSL error and will need to
* explicitly allow the browser to proceed.
*/
#include "mongoose.h"
static const char *s_http_port = "8443";
static const char *s_ssl_cert = "server.pem";
static const char *s_ssl_key = "server.key";
static struct mg_serve_http_opts s_http_server_opts;
static void ev_handler(struct mg_connection *nc, int ev, void *p) {
if (ev == MG_EV_HTTP_REQUEST) {
mg_serve_http(nc, (struct http_message *) p, s_http_server_opts);
}
}
int main(void) {
struct mg_mgr mgr;
struct mg_connection *nc;
struct mg_bind_opts bind_opts;
const char *err;
mg_mgr_init(&mgr, NULL);
memset(&bind_opts, 0, sizeof(bind_opts));
bind_opts.ssl_cert = s_ssl_cert;
bind_opts.ssl_key = s_ssl_key;
bind_opts.error_string = &err;
printf("Starting SSL server on port %s, cert from %s, key from %s\n",
s_http_port, bind_opts.ssl_cert, bind_opts.ssl_key);
nc = mg_bind_opt(&mgr, s_http_port, ev_handler, bind_opts);
if (nc == NULL) {
printf("Failed to create listener: %s\n", err);
return 1;
}
// Set up HTTP server parameters
mg_set_protocol_http_websocket(nc);
s_http_server_opts.document_root = "."; // Serve current directory
s_http_server_opts.enable_directory_listing = "yes";
for (;;) {
mg_mgr_poll(&mgr, 1000);
}
mg_mgr_free(&mgr);
return 0;
}
#else
int main(void) {
return 0;
}
#endif /* MG_ENABLE_SSL */
但是,以上代码是6.6的版本下的https,我使用的是mongoose6.4 有些地方是不兼容的,
所以下载6.4的版本时,也是宏定义一个#define MG_ENABLE_SSL //使能https
但是在main里边要这么写
int main()
{
struct mg_mgr mgr;
struct mg_connection *nc;
//https by lide
struct mg_mgr mgrs;
struct mg_connection *c;
struct mg_bind_opts bind_opts;
(void) signal(SIGCHLD, signal_handler);
(void) signal(SIGTERM, signal_handler);
(void) signal(SIGINT, signal_handler);
(void) signal(SIGTSTP, signal_handler);
if (signal(SIGSEGV, dump_backtrace) == SIG_ERR)
printf("can't catch SIGSEGV");
memset(&bind_opts, 0, sizeof(bind_opts));
bind_opts.ssl_cert = "/home/linux/http/server.pem";
//bind_opts.ssl_key = "/home/linux/http/key.pem";
// Use bind_opts to specify SSL certificate & key file
mg_mgr_init(&mgr, NULL);
nc = mg_bind_opt(&mgr, "443", ev_handler, bind_opts);
/* Set up HTTP server parameters */
mg_set_protocol_http_websocket(nc);
// Set up HTTP server parameters
mg_set_protocol_http_websocket(nc);
s_http_server_opts.document_root = "."; // Serve current directory
s_http_server_opts.enable_directory_listing = "yes";
while (1) {
mg_mgr_poll(&mgr, 1000);
}
mg_mgr_free(&mgr);
return 0;
}