mirror of
https://github.com/cesanta/mongoose.git
synced 2024-12-28 07:28:13 +08:00
103 lines
4.5 KiB
C
103 lines
4.5 KiB
C
// Copyright (c) 2020 Cesanta Software Limited
|
|
// All rights reserved
|
|
//
|
|
// HTTP server example. This server serves both static and dynamic content.
|
|
// It opens two ports: plain HTTP on port 8000 and HTTP on port 8443.
|
|
// It implements the following endpoints:
|
|
// /api/stats - respond with free-formatted stats on current connections
|
|
// /api/f2/:id - wildcard example, respond with JSON string {"result": "URI"}
|
|
// any other URI serves static files from s_root_dir
|
|
//
|
|
// To enable SSL/TLS (using self-signed certificates in PEM files),
|
|
// 1. See https://mongoose.ws/tutorials/tls/#how-to-build
|
|
// 2. curl -k https://127.0.0.1:8443
|
|
|
|
#include "mongoose.h"
|
|
|
|
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 = ".";
|
|
|
|
// Self signed certificates, see
|
|
// https://github.com/cesanta/mongoose/blob/master/test/certs/generate.sh
|
|
#ifdef TLS_TWOWAY
|
|
static const char *s_tls_ca =
|
|
"-----BEGIN CERTIFICATE-----\n"
|
|
"MIIBFTCBvAIJAMNTFtpfcq8NMAoGCCqGSM49BAMCMBMxETAPBgNVBAMMCE1vbmdv\n"
|
|
"b3NlMB4XDTI0MDUwNzE0MzczNloXDTM0MDUwNTE0MzczNlowEzERMA8GA1UEAwwI\n"
|
|
"TW9uZ29vc2UwWTATBgcqhkjOPQIBBggqhkjOPQMBBwNCAASuP+86T/rOWnGpEVhl\n"
|
|
"fxYZ+pjMbCmDZ+vdnP0rjoxudwRMRQCv5slRlDK7Lxue761sdvqxWr0Ma6TFGTNg\n"
|
|
"epsRMAoGCCqGSM49BAMCA0gAMEUCIQCwb2CxuAKm51s81S6BIoy1IcandXSohnqs\n"
|
|
"us64BAA7QgIgGGtUrpkgFSS0oPBlCUG6YPHFVw42vTfpTC0ySwAS0M4=\n"
|
|
"-----END CERTIFICATE-----\n";
|
|
#endif
|
|
static const char *s_tls_cert =
|
|
"-----BEGIN CERTIFICATE-----\n"
|
|
"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"
|
|
"-----END CERTIFICATE-----\n";
|
|
|
|
static const char *s_tls_key =
|
|
"-----BEGIN EC PRIVATE KEY-----\n"
|
|
"MHcCAQEEIAVdo8UAScxG7jiuNY2UZESNX/KPH8qJ0u0gOMMsAzYWoAoGCCqGSM49\n"
|
|
"AwEHoUQDQgAEqN6BIhvgbk7ecmUcn8Da9Avkj/uDNERtqWJG9r/or26X4u9jR5Jl\n"
|
|
"4hf5Gx17YJkq5/z3k6ogPDPpoAYWIw1/sw==\n"
|
|
"-----END EC PRIVATE KEY-----\n";
|
|
|
|
// We use the same event handler function for HTTP and HTTPS connections
|
|
// fn_data is NULL for plain HTTP, and non-NULL for HTTPS
|
|
static void fn(struct mg_connection *c, int ev, void *ev_data) {
|
|
if (ev == MG_EV_ACCEPT && c->fn_data != NULL) {
|
|
struct mg_tls_opts opts;
|
|
memset(&opts, 0, sizeof(opts));
|
|
#ifdef TLS_TWOWAY
|
|
opts.ca = mg_str(s_tls_ca);
|
|
#endif
|
|
opts.cert = mg_str(s_tls_cert);
|
|
opts.key = mg_str(s_tls_key);
|
|
mg_tls_init(c, &opts);
|
|
}
|
|
if (ev == MG_EV_HTTP_MSG) {
|
|
struct mg_http_message *hm = (struct mg_http_message *) ev_data;
|
|
if (mg_match(hm->uri, mg_str("/api/stats"), NULL)) {
|
|
struct mg_connection *t;
|
|
// 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");
|
|
for (t = c->mgr->conns; t != NULL; t = t->next) {
|
|
mg_http_printf_chunk(c, "%-3lu %4s %s %M %M\n", t->id,
|
|
t->is_udp ? "UDP" : "TCP",
|
|
t->is_listening ? "LISTENING"
|
|
: t->is_accepted ? "ACCEPTED "
|
|
: "CONNECTED",
|
|
mg_print_ip, &t->loc, mg_print_ip, &t->rem);
|
|
}
|
|
mg_http_printf_chunk(c, ""); // Don't forget the last empty chunk
|
|
} else if (mg_match(hm->uri, mg_str("/api/f2/*"), NULL)) {
|
|
mg_http_reply(c, 200, "", "{\"result\": \"%.*s\"}\n", hm->uri.len,
|
|
hm->uri.buf);
|
|
} else {
|
|
struct mg_http_serve_opts opts;
|
|
memset(&opts, 0, sizeof(opts));
|
|
opts.root_dir = s_root_dir;
|
|
mg_http_serve_dir(c, ev_data, &opts);
|
|
}
|
|
}
|
|
}
|
|
|
|
int main(void) {
|
|
struct mg_mgr mgr; // Event manager
|
|
mg_log_set(MG_LL_DEBUG); // Set log level
|
|
mg_mgr_init(&mgr); // Initialise event manager
|
|
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
|
|
mg_mgr_free(&mgr);
|
|
return 0;
|
|
}
|