2020-12-10 13:26:05 +00:00
|
|
|
// Copyright (c) 2020 Cesanta Software Limited
|
|
|
|
// All rights reserved
|
2020-12-18 09:01:14 +00:00
|
|
|
//
|
2020-12-18 09:08:28 +00:00
|
|
|
// HTTP server example. This server serves both static and dynamic content.
|
2021-07-13 14:40:52 +01:00
|
|
|
// It opens two ports: plain HTTP on port 8000 and HTTP on port 8443.
|
2020-12-18 09:08:28 +00:00
|
|
|
// It implements the following endpoints:
|
2022-05-06 20:19:40 +01:00
|
|
|
// /api/stats - respond with free-formatted stats on current connections
|
2020-12-18 09:08:28 +00:00
|
|
|
// /api/f2/:id - wildcard example, respond with JSON string {"result": "URI"}
|
2021-07-13 14:40:52 +01:00
|
|
|
// any other URI serves static files from s_root_dir
|
2020-12-18 09:08:28 +00:00
|
|
|
//
|
|
|
|
// To enable SSL/TLS (using self-signed certificates in PEM files),
|
2023-02-21 12:07:48 -03:00
|
|
|
// 1. See https://mongoose.ws/tutorials/tls/#how-to-build
|
2021-07-13 14:40:52 +01:00
|
|
|
// 2. curl -k https://127.0.0.1:8443
|
2020-12-10 13:26:05 +00:00
|
|
|
|
|
|
|
#include "mongoose.h"
|
|
|
|
|
2021-07-13 14:40:52 +01:00
|
|
|
static const char *s_http_addr = "http://0.0.0.0:8000"; // HTTP port
|
|
|
|
static const char *s_https_addr = "https://0.0.0.0:8443"; // HTTPS port
|
|
|
|
static const char *s_root_dir = ".";
|
2020-12-10 13:26:05 +00:00
|
|
|
|
2024-07-26 19:31:15 +01:00
|
|
|
// Self signed certificates, see
|
|
|
|
// https://github.com/cesanta/mongoose/blob/master/test/certs/generate.sh
|
2023-07-25 18:41:41 -03:00
|
|
|
#ifdef TLS_TWOWAY
|
|
|
|
static const char *s_tls_ca =
|
|
|
|
"-----BEGIN CERTIFICATE-----\n"
|
2024-07-26 19:31:15 +01:00
|
|
|
"MIIBFTCBvAIJAMNTFtpfcq8NMAoGCCqGSM49BAMCMBMxETAPBgNVBAMMCE1vbmdv\n"
|
|
|
|
"b3NlMB4XDTI0MDUwNzE0MzczNloXDTM0MDUwNTE0MzczNlowEzERMA8GA1UEAwwI\n"
|
|
|
|
"TW9uZ29vc2UwWTATBgcqhkjOPQIBBggqhkjOPQMBBwNCAASuP+86T/rOWnGpEVhl\n"
|
|
|
|
"fxYZ+pjMbCmDZ+vdnP0rjoxudwRMRQCv5slRlDK7Lxue761sdvqxWr0Ma6TFGTNg\n"
|
|
|
|
"epsRMAoGCCqGSM49BAMCA0gAMEUCIQCwb2CxuAKm51s81S6BIoy1IcandXSohnqs\n"
|
|
|
|
"us64BAA7QgIgGGtUrpkgFSS0oPBlCUG6YPHFVw42vTfpTC0ySwAS0M4=\n"
|
2023-07-25 18:41:41 -03:00
|
|
|
"-----END CERTIFICATE-----\n";
|
|
|
|
#endif
|
|
|
|
static const char *s_tls_cert =
|
|
|
|
"-----BEGIN CERTIFICATE-----\n"
|
2024-07-26 19:31:15 +01:00
|
|
|
"MIIBMTCB2aADAgECAgkAluqkgeuV/zUwCgYIKoZIzj0EAwIwEzERMA8GA1UEAwwI\n"
|
|
|
|
"TW9uZ29vc2UwHhcNMjQwNTA3MTQzNzM2WhcNMzQwNTA1MTQzNzM2WjARMQ8wDQYD\n"
|
|
|
|
"VQQDDAZzZXJ2ZXIwWTATBgcqhkjOPQIBBggqhkjOPQMBBwNCAASo3oEiG+BuTt5y\n"
|
|
|
|
"ZRyfwNr0C+SP+4M0RG2pYkb2v+ivbpfi72NHkmXiF/kbHXtgmSrn/PeTqiA8M+mg\n"
|
|
|
|
"BhYjDX+zoxgwFjAUBgNVHREEDTALgglsb2NhbGhvc3QwCgYIKoZIzj0EAwIDRwAw\n"
|
|
|
|
"RAIgTXW9MITQSwzqbNTxUUdt9DcB+8pPUTbWZpiXcA26GMYCIBiYw+DSFMLHmkHF\n"
|
|
|
|
"+5U3NXW3gVCLN9ntD5DAx8LTG8sB\n"
|
2023-07-25 18:41:41 -03:00
|
|
|
"-----END CERTIFICATE-----\n";
|
|
|
|
|
|
|
|
static const char *s_tls_key =
|
2024-07-26 19:31:15 +01:00
|
|
|
"-----BEGIN EC PRIVATE KEY-----\n"
|
|
|
|
"MHcCAQEEIAVdo8UAScxG7jiuNY2UZESNX/KPH8qJ0u0gOMMsAzYWoAoGCCqGSM49\n"
|
|
|
|
"AwEHoUQDQgAEqN6BIhvgbk7ecmUcn8Da9Avkj/uDNERtqWJG9r/or26X4u9jR5Jl\n"
|
|
|
|
"4hf5Gx17YJkq5/z3k6ogPDPpoAYWIw1/sw==\n"
|
|
|
|
"-----END EC PRIVATE KEY-----\n";
|
2023-07-25 18:41:41 -03:00
|
|
|
|
2021-07-13 14:40:52 +01:00
|
|
|
// We use the same event handler function for HTTP and HTTPS connections
|
|
|
|
// fn_data is NULL for plain HTTP, and non-NULL for HTTPS
|
2024-01-08 17:34:34 -03:00
|
|
|
static void fn(struct mg_connection *c, int ev, void *ev_data) {
|
|
|
|
if (ev == MG_EV_ACCEPT && c->fn_data != NULL) {
|
2024-07-27 07:00:51 +01:00
|
|
|
struct mg_tls_opts opts;
|
|
|
|
memset(&opts, 0, sizeof(opts));
|
2023-09-17 09:07:57 +01:00
|
|
|
#ifdef TLS_TWOWAY
|
2024-07-27 07:00:51 +01:00
|
|
|
opts.ca = mg_str(s_tls_ca);
|
2023-09-17 09:07:57 +01:00
|
|
|
#endif
|
2024-07-27 07:00:51 +01:00
|
|
|
opts.cert = mg_str(s_tls_cert);
|
|
|
|
opts.key = mg_str(s_tls_key);
|
2023-09-17 09:07:57 +01:00
|
|
|
mg_tls_init(c, &opts);
|
|
|
|
}
|
2023-07-25 18:41:41 -03:00
|
|
|
if (ev == MG_EV_HTTP_MSG) {
|
2020-12-10 13:26:05 +00:00
|
|
|
struct mg_http_message *hm = (struct mg_http_message *) ev_data;
|
2024-04-17 16:13:10 -03:00
|
|
|
if (mg_match(hm->uri, mg_str("/api/stats"), NULL)) {
|
2024-07-27 07:00:51 +01:00
|
|
|
struct mg_connection *t;
|
2022-05-06 20:19:40 +01:00
|
|
|
// Print some statistics about currently established connections
|
|
|
|
mg_printf(c, "HTTP/1.1 200 OK\r\nTransfer-Encoding: chunked\r\n\r\n");
|
|
|
|
mg_http_printf_chunk(c, "ID PROTO TYPE LOCAL REMOTE\n");
|
2024-07-27 07:00:51 +01:00
|
|
|
for (t = c->mgr->conns; t != NULL; t = t->next) {
|
2023-01-30 16:59:14 -03:00
|
|
|
mg_http_printf_chunk(c, "%-3lu %4s %s %M %M\n", t->id,
|
2022-05-06 20:19:40 +01:00
|
|
|
t->is_udp ? "UDP" : "TCP",
|
|
|
|
t->is_listening ? "LISTENING"
|
|
|
|
: t->is_accepted ? "ACCEPTED "
|
|
|
|
: "CONNECTED",
|
2023-01-30 16:59:14 -03:00
|
|
|
mg_print_ip, &t->loc, mg_print_ip, &t->rem);
|
2022-05-06 20:19:40 +01:00
|
|
|
}
|
|
|
|
mg_http_printf_chunk(c, ""); // Don't forget the last empty chunk
|
2024-04-17 16:13:10 -03:00
|
|
|
} else if (mg_match(hm->uri, mg_str("/api/f2/*"), NULL)) {
|
2024-07-27 07:00:51 +01:00
|
|
|
mg_http_reply(c, 200, "", "{\"result\": \"%.*s\"}\n", hm->uri.len,
|
2024-03-15 07:42:24 +00:00
|
|
|
hm->uri.buf);
|
2020-12-10 13:26:05 +00:00
|
|
|
} else {
|
2024-07-27 07:01:48 +01:00
|
|
|
struct mg_http_serve_opts opts;
|
|
|
|
memset(&opts, 0, sizeof(opts));
|
|
|
|
opts.root_dir = s_root_dir;
|
2021-01-02 17:57:51 +00:00
|
|
|
mg_http_serve_dir(c, ev_data, &opts);
|
2020-12-10 13:26:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int main(void) {
|
|
|
|
struct mg_mgr mgr; // Event manager
|
2022-08-01 11:19:32 +01:00
|
|
|
mg_log_set(MG_LL_DEBUG); // Set log level
|
2020-12-10 13:26:05 +00:00
|
|
|
mg_mgr_init(&mgr); // Initialise event manager
|
2021-07-13 14:40:52 +01:00
|
|
|
mg_http_listen(&mgr, s_http_addr, fn, NULL); // Create HTTP listener
|
|
|
|
mg_http_listen(&mgr, s_https_addr, fn, (void *) 1); // HTTPS listener
|
|
|
|
for (;;) mg_mgr_poll(&mgr, 1000); // Infinite event loop
|
2020-12-10 13:26:05 +00:00
|
|
|
mg_mgr_free(&mgr);
|
|
|
|
return 0;
|
|
|
|
}
|