diff --git a/examples/http-restful-server/Makefile b/examples/http-restful-server/Makefile index da256b51..9302488d 100644 --- a/examples/http-restful-server/Makefile +++ b/examples/http-restful-server/Makefile @@ -1,9 +1,11 @@ PROG ?= example -ifeq "$(MBEDTLS_DIR)" "" -else +ifneq ($(MBEDTLS_DIR),) CFLAGS += -DMG_ENABLE_MBEDTLS=1 -I$(MBEDTLS_DIR)/include -I/usr/include CFLAGS += -L$(MBEDTLS_DIR)/lib -lmbedtls -lmbedcrypto -lmbedx509 +else ifneq ($(OPENSSL_DIR),) +CFLAGS += -DMG_ENABLE_OPENSSL=1 -I$(OPENSSL_DIR)/include +CFLAGS += -L$(OPENSSL_DIR)/lib -lssl -lcrypto endif all: $(PROG) diff --git a/examples/http-restful-server/main.c b/examples/http-restful-server/main.c index 2602b66b..48e17dfd 100644 --- a/examples/http-restful-server/main.c +++ b/examples/http-restful-server/main.c @@ -2,24 +2,26 @@ // 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/f1 - respond with JSON string {"result": 123} // /api/f2/:id - wildcard example, respond with JSON string {"result": "URI"} -// any other URI serves static files from s_web_directory +// any other URI serves static files from s_root_dir // // To enable SSL/TLS (using self-signed certificates in PEM files), -// 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 +// 1. make MBEDTLS_DIR=/path/to/your/mbedtls/installation +// 2. curl -k https://127.0.0.1:8443 #include "mongoose.h" -static const char *s_listen_on = "http://0.0.0.0:8000"; -static const char *s_web_directory = "."; +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 = "."; +// 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, void *fn_data) { - 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 + if (ev == MG_EV_ACCEPT && fn_data != NULL) { struct mg_tls_opts opts = { //.ca = "ca.pem", // Uncomment to enable two-way SSL .cert = "server.pem", // Certificate PEM file @@ -34,7 +36,7 @@ static void fn(struct mg_connection *c, int ev, void *ev_data, void *fn_data) { mg_http_reply(c, 200, "", "{\"result\": \"%.*s\"}\n", (int) hm->uri.len, hm->uri.ptr); } else { - struct mg_http_serve_opts opts = {.root_dir = s_web_directory}; + struct mg_http_serve_opts opts = {.root_dir = s_root_dir}; mg_http_serve_dir(c, ev_data, &opts); } } @@ -45,8 +47,9 @@ 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 + 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; }