mirror of
https://github.com/cesanta/mongoose.git
synced 2026-10-23 11:44:53 +00:00
Update http server example to showcase SSL
This commit is contained in:
1 parent
82a378e519
commit
04450ec659
7 files changed
+120
-9
No files matched your search
@@ -1,9 +1,14 @@
|
||||
// Copyright (c) 2020 Cesanta Software Limited
|
||||
// All rights reserved
|
||||
//
|
||||
// To enable SSL/TLS,
|
||||
// 1. Change s_listen_on from http:// to https://
|
||||
// 2. make MBEDTLS_DIR=/path/to/your/mbedtls/installation
|
||||
// 3. curl -k https://127.0.0.1:8000
|
||||
|
||||
#include "mongoose.h"
|
||||
|
||||
static const char *s_listen_on = "http://localhost:8000";
|
||||
static const char *s_listen_on = "https://localhost:8000";
|
||||
static const char *s_web_directory = ".";
|
||||
|
||||
// This RESTful server implements the following endpoints:
|
||||
@@ -11,7 +16,15 @@ static const char *s_web_directory = ".";
|
||||
// /api/f2/:id - wildcard example, respond with JSON string {"result": "URI"}
|
||||
// any other URI serves static files from s_web_directory
|
||||
static void fn(struct mg_connection *c, int ev, void *ev_data, void *fn_data) {
|
||||
if (ev == MG_EV_HTTP_MSG) {
|
||||
if (ev == MG_EV_ACCEPT && mg_url_is_ssl(s_listen_on)) {
|
||||
// If s_listen_on URL is https://, tell listening connection to use TLS
|
||||
struct mg_tls_opts opts = {
|
||||
//.ca = "ca.pem", // Uncomment to enable two-way SSL
|
||||
.cert = "server.pem", // Certificate PEM file
|
||||
.certkey = "server.pem", // This pem conains both cert and key
|
||||
};
|
||||
mg_tls_init(c, &opts);
|
||||
} else if (ev == MG_EV_HTTP_MSG) {
|
||||
struct mg_http_message *hm = (struct mg_http_message *) ev_data;
|
||||
if (mg_http_match_uri(hm, "/api/f1")) {
|
||||
mg_http_reply(c, 200, "", "{\"result\": %d}\n", 123); // Serve REST
|
||||
@@ -27,6 +40,7 @@ static void fn(struct mg_connection *c, int ev, void *ev_data, void *fn_data) {
|
||||
|
||||
int main(void) {
|
||||
struct mg_mgr mgr; // Event manager
|
||||
mg_log_set("2"); // Set to 3 to enable debug
|
||||
mg_mgr_init(&mgr); // Initialise event manager
|
||||
mg_http_listen(&mgr, s_listen_on, fn, NULL); // Create HTTP listener
|
||||
for (;;) mg_mgr_poll(&mgr, 1000); // Infinite event loop
|
||||
|
||||
Reference in new issue
Block a user