From bafc30bec6c1b2659ae1554030c910e05705f9d3 Mon Sep 17 00:00:00 2001 From: Deomid Ryabkov Date: Thu, 13 Oct 2016 20:55:08 +0300 Subject: [PATCH] Change from using #ifdef to #if for features tests "#if FOO" still works with simple -DFOO, but gives more flexibility. Specifically, if user expressed no preference (FOO is not defined), we can apply reasonable defaults (this is the legitimate use of ifdef). In short, from now on, please use #if MG_ENABLE_FOO instead of #ifdef MG_ENABLE_FOO Since we are all used to #ifdef, this change also adds a precommit check to police this. Specifically, in *.h and *.c files that are Copyright Cesanta, "ifdef" and "if defined()" are not allowed to be used with macros that contain ENABLE or DISABLE, unless the like also contains "ifdef-ok". Hence, if you are sure you want to use ifdef, use this: #ifdef MG_ENABLE_FOO /* ifdef-ok */ PUBLISHED_FROM=9be829448f53cff575d6cae8b9945fb12531c15a --- docs/c-api/net.h/struct_mg_bind_opts.md | 2 +- docs/c-api/net.h/struct_mg_connect_opts.md | 2 +- docs/c-api/net.h/struct_mg_connection.md | 2 +- docs/c-api/net.h/struct_mg_mgr.md | 4 +- examples/CC3200/cs_dbg.h | 4 +- examples/ESP8266_RTOS/Makefile | 2 +- examples/ESP8266_RTOS/user/Makefile | 4 +- examples/MSP432/ccs/MSP432_MG_hello/cs_dbg.h | 4 +- examples/load_balancer/load_balancer.c | 6 +- examples/restful_server/restful_server.c | 10 +- .../simplest_web_server_ssl.c | 2 +- mongoose.c | 295 ++++++++++-------- mongoose.h | 251 +++++++++++---- 13 files changed, 380 insertions(+), 208 deletions(-) diff --git a/docs/c-api/net.h/struct_mg_bind_opts.md b/docs/c-api/net.h/struct_mg_bind_opts.md index 74a8758f..50c0010b 100644 --- a/docs/c-api/net.h/struct_mg_bind_opts.md +++ b/docs/c-api/net.h/struct_mg_bind_opts.md @@ -7,7 +7,7 @@ signature: | void *user_data; /* Initial value for connection's user_data */ unsigned int flags; /* Extra connection flags */ const char **error_string; /* Placeholder for the error string */ - #ifdef MG_ENABLE_SSL + #if MG_ENABLE_SSL /* SSL settings. */ const char *ssl_cert; /* Server certificate to present to clients */ const char *ssl_key; /* Private key corresponding to the certificate. diff --git a/docs/c-api/net.h/struct_mg_connect_opts.md b/docs/c-api/net.h/struct_mg_connect_opts.md index c41370ee..4bb9ea66 100644 --- a/docs/c-api/net.h/struct_mg_connect_opts.md +++ b/docs/c-api/net.h/struct_mg_connect_opts.md @@ -7,7 +7,7 @@ signature: | void *user_data; /* Initial value for connection's user_data */ unsigned int flags; /* Extra connection flags */ const char **error_string; /* Placeholder for the error string */ - #ifdef MG_ENABLE_SSL + #if MG_ENABLE_SSL /* SSL settings. */ const char *ssl_cert; /* Client certificate to present to the server */ const char *ssl_key; /* Private key corresponding to the certificate. diff --git a/docs/c-api/net.h/struct_mg_connection.md b/docs/c-api/net.h/struct_mg_connection.md index 76f767f8..682aa85b 100644 --- a/docs/c-api/net.h/struct_mg_connection.md +++ b/docs/c-api/net.h/struct_mg_connection.md @@ -14,7 +14,7 @@ signature: | size_t recv_mbuf_limit; /* Max size of recv buffer */ struct mbuf recv_mbuf; /* Received data */ struct mbuf send_mbuf; /* Data scheduled for sending */ - #if defined(MG_ENABLE_SSL) + #if MG_ENABLE_SSL #if !defined(MG_SOCKET_SIMPLELINK) SSL *ssl; SSL_CTX *ssl_ctx; diff --git a/docs/c-api/net.h/struct_mg_mgr.md b/docs/c-api/net.h/struct_mg_mgr.md index 5adfe6e8..eac8075f 100644 --- a/docs/c-api/net.h/struct_mg_mgr.md +++ b/docs/c-api/net.h/struct_mg_mgr.md @@ -6,12 +6,12 @@ signature: | struct mg_mgr { struct mg_connection *active_connections; const char *hexdump_file; /* Debug hexdump file path */ - #ifndef MG_DISABLE_SOCKETPAIR + #if !MG_DISABLE_SOCKETPAIR sock_t ctl[2]; /* Socketpair for mg_broadcast() */ #endif void *user_data; /* User data */ void *mgr_data; /* Implementation-specific event manager's data. */ - #ifdef MG_ENABLE_JAVASCRIPT + #if MG_ENABLE_JAVASCRIPT struct v7 *v7; #endif }; diff --git a/examples/CC3200/cs_dbg.h b/examples/CC3200/cs_dbg.h index 219ef4d4..813413d5 100644 --- a/examples/CC3200/cs_dbg.h +++ b/examples/CC3200/cs_dbg.h @@ -6,7 +6,7 @@ #ifndef CS_COMMON_CS_DBG_H_ #define CS_COMMON_CS_DBG_H_ -#ifndef CS_DISABLE_STDIO +#if !CS_DISABLE_STDIO #include #endif @@ -28,7 +28,7 @@ enum cs_log_level { void cs_log_set_level(enum cs_log_level level); -#ifndef CS_DISABLE_STDIO +#if !CS_DISABLE_STDIO void cs_log_set_file(FILE *file); diff --git a/examples/ESP8266_RTOS/Makefile b/examples/ESP8266_RTOS/Makefile index 0317ff87..2d0b16a4 100644 --- a/examples/ESP8266_RTOS/Makefile +++ b/examples/ESP8266_RTOS/Makefile @@ -27,7 +27,7 @@ endif # } PDIR LDDIR = $(SDK_PATH)/ld -CCFLAGS += -Os +CCFLAGS += -Os -Wno-undef TARGET_LDFLAGS = \ -nostdlib \ diff --git a/examples/ESP8266_RTOS/user/Makefile b/examples/ESP8266_RTOS/user/Makefile index 85700bed..532f357a 100644 --- a/examples/ESP8266_RTOS/user/Makefile +++ b/examples/ESP8266_RTOS/user/Makefile @@ -24,8 +24,8 @@ endif # for a subtree within the makefile rooted therein # DEFINES += -DCS_PLATFORM=3 \ - -DMG_NO_BSD_SOCKETS \ - -DMG_DISABLE_FILESYSTEM \ + -DMG_NO_BSD_SOCKETS=1 \ + -DMG_DISABLE_FILESYSTEM=1 \ -DRTOS_SDK -DMG_LWIP -DLWIP_TIMEVAL_PRIVATE=0 \ -DMG_INTERNAL= diff --git a/examples/MSP432/ccs/MSP432_MG_hello/cs_dbg.h b/examples/MSP432/ccs/MSP432_MG_hello/cs_dbg.h index 219ef4d4..813413d5 100644 --- a/examples/MSP432/ccs/MSP432_MG_hello/cs_dbg.h +++ b/examples/MSP432/ccs/MSP432_MG_hello/cs_dbg.h @@ -6,7 +6,7 @@ #ifndef CS_COMMON_CS_DBG_H_ #define CS_COMMON_CS_DBG_H_ -#ifndef CS_DISABLE_STDIO +#if !CS_DISABLE_STDIO #include #endif @@ -28,7 +28,7 @@ enum cs_log_level { void cs_log_set_level(enum cs_log_level level); -#ifndef CS_DISABLE_STDIO +#if !CS_DISABLE_STDIO void cs_log_set_file(FILE *file); diff --git a/examples/load_balancer/load_balancer.c b/examples/load_balancer/load_balancer.c index 1dcbd42d..52760bdd 100644 --- a/examples/load_balancer/load_balancer.c +++ b/examples/load_balancer/load_balancer.c @@ -60,7 +60,7 @@ static int s_num_vhost_backends = 0, s_num_default_backends = 0; static int s_sig_num = 0; static int s_backend_keepalive = 0; static FILE *s_log_file = NULL; -#ifdef MG_ENABLE_SSL +#if MG_ENABLE_SSL const char *s_ssl_cert = NULL; #endif @@ -206,7 +206,7 @@ static void forward(struct conn_data *conn, struct http_message *hm, for (i = 0; i < MG_MAX_HTTP_HEADERS && hm->header_names[i].len > 0; i++) { struct mg_str hn = hm->header_names[i]; struct mg_str hv = hm->header_values[i]; -#ifdef MG_ENABLE_SSL +#if MG_ENABLE_SSL /* * If we terminate SSL and backend redirects to local HTTP port, * strip protocol to let client use HTTPS. @@ -598,7 +598,7 @@ int main(int argc, char *argv[]) { vhost = NULL; redirect = 0; i += 2; -#ifdef MG_ENABLE_SSL +#if MG_ENABLE_SSL } else if (strcmp(argv[i], "-s") == 0 && i + 1 < argc) { s_ssl_cert = argv[++i]; #endif diff --git a/examples/restful_server/restful_server.c b/examples/restful_server/restful_server.c index b3d6fd63..9419755a 100644 --- a/examples/restful_server/restful_server.c +++ b/examples/restful_server/restful_server.c @@ -53,7 +53,7 @@ int main(int argc, char *argv[]) { int i; char *cp; const char *err_str; -#ifdef MG_ENABLE_SSL +#if MG_ENABLE_SSL const char *ssl_cert = NULL; #endif @@ -75,7 +75,7 @@ int main(int argc, char *argv[]) { s_http_port = argv[++i]; } else if (strcmp(argv[i], "-a") == 0 && i + 1 < argc) { s_http_server_opts.auth_domain = argv[++i]; -#ifdef MG_ENABLE_JAVASCRIPT +#if MG_ENABLE_JAVASCRIPT } else if (strcmp(argv[i], "-j") == 0 && i + 1 < argc) { const char *init_file = argv[++i]; mg_enable_javascript(&mgr, v7_create(), init_file); @@ -86,11 +86,11 @@ int main(int argc, char *argv[]) { s_http_server_opts.per_directory_auth_file = argv[++i]; } else if (strcmp(argv[i], "-r") == 0 && i + 1 < argc) { s_http_server_opts.url_rewrites = argv[++i]; -#ifndef MG_DISABLE_CGI +#if !MG_DISABLE_CGI } else if (strcmp(argv[i], "-i") == 0 && i + 1 < argc) { s_http_server_opts.cgi_interpreter = argv[++i]; #endif -#ifdef MG_ENABLE_SSL +#if MG_ENABLE_SSL } else if (strcmp(argv[i], "-s") == 0 && i + 1 < argc) { ssl_cert = argv[++i]; #endif @@ -103,7 +103,7 @@ int main(int argc, char *argv[]) { /* Set HTTP server options */ memset(&bind_opts, 0, sizeof(bind_opts)); bind_opts.error_string = &err_str; -#ifdef MG_ENABLE_SSL +#if MG_ENABLE_SSL if (ssl_cert != NULL) { bind_opts.ssl_cert = ssl_cert; } diff --git a/examples/simplest_web_server_ssl/simplest_web_server_ssl.c b/examples/simplest_web_server_ssl/simplest_web_server_ssl.c index 658935ab..d96faf05 100644 --- a/examples/simplest_web_server_ssl/simplest_web_server_ssl.c +++ b/examples/simplest_web_server_ssl/simplest_web_server_ssl.c @@ -3,7 +3,7 @@ * All rights reserved */ -#ifdef MG_ENABLE_SSL +#if MG_ENABLE_SSL /* * This example starts an SSL web server on https://localhost:8443/ * diff --git a/mongoose.c b/mongoose.c index 2d03840f..7efbf2bc 100644 --- a/mongoose.c +++ b/mongoose.c @@ -74,7 +74,7 @@ MG_INTERNAL void mg_remove_conn(struct mg_connection *c); MG_INTERNAL struct mg_connection *mg_create_connection( struct mg_mgr *mgr, mg_event_handler_t callback, struct mg_add_sock_opts opts); -#ifndef MG_DISABLE_FILESYSTEM +#if !MG_DISABLE_FILESYSTEM MG_INTERNAL int mg_uri_to_local_path(struct http_message *hm, const struct mg_serve_http_opts *opts, char **local_path, @@ -100,7 +100,7 @@ MG_INTERNAL size_t mg_handle_chunked(struct mg_connection *nc, struct http_message *hm, char *buf, size_t blen); -#ifndef MG_DISABLE_FILESYSTEM +#if !MG_DISABLE_FILESYSTEM MG_INTERNAL time_t mg_parse_date_string(const char *datetime); MG_INTERNAL int mg_is_not_modified(struct http_message *hm, cs_stat_t *st); #endif @@ -110,7 +110,7 @@ struct ctl_msg { char message[MG_CTL_MSG_MESSAGE_SIZE]; }; -#ifndef MG_DISABLE_MQTT +#if !MG_DISABLE_MQTT struct mg_mqtt_message; MG_INTERNAL int parse_mqtt(struct mbuf *io, struct mg_mqtt_message *mm); #endif @@ -136,6 +136,18 @@ extern void *(*test_calloc)(size_t count, size_t size); #define CS_COMMON_CS_DBG_H_ #ifndef CS_DISABLE_STDIO +#define CS_DISABLE_STDIO 0 +#endif + +#ifndef CS_ENABLE_DEBUG +#define CS_ENABLE_DEBUG 0 +#endif + +#ifndef CS_LOG_TS_DIFF +#define CS_LOG_TS_DIFF 0 +#endif + +#if !CS_DISABLE_STDIO #include #endif @@ -157,7 +169,7 @@ enum cs_log_level { void cs_log_set_level(enum cs_log_level level); -#ifndef CS_DISABLE_STDIO +#if !CS_DISABLE_STDIO void cs_log_set_file(FILE *file); @@ -213,13 +225,13 @@ void cs_log_printf(const char *fmt, ...); /* Amalgamated: #include "common/cs_time.h" */ enum cs_log_level cs_log_level = -#ifdef CS_ENABLE_DEBUG +#if CS_ENABLE_DEBUG LL_VERBOSE_DEBUG; #else LL_ERROR; #endif -#ifndef CS_DISABLE_STDIO +#if !CS_DISABLE_STDIO FILE *cs_log_file = NULL; @@ -256,7 +268,7 @@ void cs_log_set_file(FILE *file) { void cs_log_set_level(enum cs_log_level level) { cs_log_level = level; -#if defined(CS_LOG_TS_DIFF) && !defined(CS_DISABLE_STDIO) +#if CS_LOG_TS_DIFF && !CS_DISABLE_STDIO cs_log_ts = cs_time(); #endif } @@ -271,8 +283,11 @@ void cs_log_set_level(enum cs_log_level level) { #ifndef EXCLUDE_COMMON /* Amalgamated: #include "common/base64.h" */ + #include +/* Amalgamated: #include "common/cs_dbg.h" */ + /* ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/ */ #define NUM_UPPERCASES ('Z' - 'A' + 1) @@ -386,7 +401,7 @@ void cs_base64_encode(const unsigned char *src, int src_len, char *dst) { #undef BASE64_OUT #undef BASE64_FLUSH -#ifndef CS_DISABLE_STDIO +#if !CS_DISABLE_STDIO #define BASE64_OUT(ch) \ do { \ fprintf(f, "%c", (ch)); \ @@ -478,7 +493,11 @@ int cs_base64_decode(const unsigned char *s, int len, char *dst) { extern "C" { #endif /* __cplusplus */ -#ifdef CS_ENABLE_SPIFFS +#ifndef CS_ENABLE_SPIFFS +#define CS_ENABLE_SPIFFS 0 +#endif + +#if CS_ENABLE_SPIFFS #include @@ -507,7 +526,7 @@ typedef struct DIR { } DIR; #endif -#if defined(_WIN32) || defined(CS_ENABLE_SPIFFS) +#if defined(_WIN32) || CS_ENABLE_SPIFFS DIR *opendir(const char *dir_name); int closedir(DIR *dir); struct dirent *readdir(DIR *dir); @@ -610,7 +629,7 @@ struct dirent *readdir(DIR *dir) { } #endif -#ifdef CS_ENABLE_SPIFFS +#if CS_ENABLE_SPIFFS DIR *opendir(const char *dir_name) { DIR *dir = NULL; @@ -734,11 +753,10 @@ double cs_time(void) { * will fill a supplied 16-byte array with the digest. */ -#if !defined(DISABLE_MD5) && !defined(EXCLUDE_COMMON) - /* Amalgamated: #include "common/md5.h" */ -#ifndef CS_ENABLE_NATIVE_MD5 +#if !DISABLE_MD5 && !defined(EXCLUDE_COMMON) + static void byteReverse(unsigned char *buf, unsigned longs) { /* Forrest: MD5 expect LITTLE_ENDIAN, swap if BIG_ENDIAN */ #if BYTE_ORDER == BIG_ENDIAN @@ -922,7 +940,6 @@ void MD5_Final(unsigned char digest[16], MD5_CTX *ctx) { memcpy(digest, ctx->buf, 16); memset((char *) ctx, 0, sizeof(*ctx)); } -#endif /* CS_ENABLE_NATIVE_MD5 */ /* * Stringify binary data. Output buffer size must be 2 * size_of_input + 1 @@ -1131,7 +1148,7 @@ int mg_strcmp(const struct mg_str str1, const struct mg_str str2) { /* Copyright(c) By Steve Reid */ /* 100% Public Domain */ -#if !defined(DISABLE_SHA1) && !defined(EXCLUDE_COMMON) +#if !DISABLE_SHA1 && !defined(EXCLUDE_COMMON) /* Amalgamated: #include "common/sha1.h" */ @@ -1392,6 +1409,10 @@ void cs_hmac_sha1(const unsigned char *key, size_t keylen, /* Amalgamated: #include "common/platform.h" */ /* Amalgamated: #include "common/str_util.h" */ +#ifndef C_DISABLE_BUILTIN_SNPRINTF +#define C_DISABLE_BUILTIN_SNPRINTF 0 +#endif + size_t c_strnlen(const char *s, size_t maxlen) { size_t l = 0; for (; l < maxlen && s[l] != '\0'; l++) { @@ -1407,7 +1428,7 @@ size_t c_strnlen(const char *s, size_t maxlen) { #define C_SNPRINTF_FLAG_ZERO 1 -#ifdef C_DISABLE_BUILTIN_SNPRINTF +#if C_DISABLE_BUILTIN_SNPRINTF int c_vsnprintf(char *buf, size_t buf_size, const char *fmt, va_list ap) { return vsnprintf(buf, buf_size, fmt, ap); } @@ -1729,7 +1750,7 @@ MG_INTERNAL void mg_call(struct mg_connection *nc, ev_handler == nc->handler ? "user" : "proto", ev, ev_data, nc->flags, (int) nc->recv_mbuf.len, (int) nc->send_mbuf.len)); -#if !defined(NO_LIBC) && !defined(MG_DISABLE_HEXDUMP) +#if !defined(NO_LIBC) && !MG_DISABLE_HEXDUMP /* LCOV_EXCL_START */ if (nc->mgr->hexdump_file != NULL && ev != MG_EV_POLL && ev != MG_EV_SEND /* handled separately */) { @@ -1786,7 +1807,7 @@ static void mg_destroy_conn(struct mg_connection *conn, int destroy_if) { if (conn->proto_data != NULL && conn->proto_data_destructor != NULL) { conn->proto_data_destructor(conn->proto_data); } -#if defined(MG_ENABLE_SSL) && !defined(MG_SOCKET_SIMPLELINK) +#if MG_ENABLE_SSL && !defined(MG_SOCKET_SIMPLELINK) if (conn->ssl != NULL) SSL_free(conn->ssl); if (conn->ssl_ctx != NULL) SSL_CTX_free(conn->ssl_ctx); #endif @@ -1807,7 +1828,7 @@ void mg_close_conn(struct mg_connection *conn) { void mg_mgr_init(struct mg_mgr *m, void *user_data) { memset(m, 0, sizeof(*m)); -#ifndef MG_DISABLE_SOCKETPAIR +#if !MG_DISABLE_SOCKETPAIR m->ctl[0] = m->ctl[1] = INVALID_SOCKET; #endif m->user_data = user_data; @@ -1823,7 +1844,7 @@ void mg_mgr_init(struct mg_mgr *m, void *user_data) { signal(SIGPIPE, SIG_IGN); #endif -#if defined(MG_ENABLE_SSL) && !defined(MG_SOCKET_SIMPLELINK) +#if MG_ENABLE_SSL && !defined(MG_SOCKET_SIMPLELINK) { static int init_done; if (!init_done) { @@ -1838,7 +1859,7 @@ void mg_mgr_init(struct mg_mgr *m, void *user_data) { DBG(("init mgr=%p", m)); } -#ifdef MG_ENABLE_JAVASCRIPT +#if MG_ENABLE_JAVASCRIPT static enum v7_err mg_send_js(struct v7 *v7, v7_val_t *res) { v7_val_t arg0 = v7_arg(v7, 0); v7_val_t arg1 = v7_arg(v7, 1); @@ -1872,7 +1893,7 @@ void mg_mgr_free(struct mg_mgr *m) { /* Do one last poll, see https://github.com/cesanta/mongoose/issues/286 */ mg_mgr_poll(m, 0); -#ifndef MG_DISABLE_SOCKETPAIR +#if !MG_DISABLE_SOCKETPAIR if (m->ctl[0] != INVALID_SOCKET) closesocket(m->ctl[0]); if (m->ctl[1] != INVALID_SOCKET) closesocket(m->ctl[1]); m->ctl[0] = m->ctl[1] = INVALID_SOCKET; @@ -1909,10 +1930,10 @@ int mg_printf(struct mg_connection *conn, const char *fmt, ...) { return len; } -#ifndef MG_DISABLE_SYNC_RESOLVER +#if !MG_DISABLE_SYNC_RESOLVER /* TODO(lsm): use non-blocking resolver */ static int mg_resolve2(const char *host, struct in_addr *ina) { -#ifdef MG_ENABLE_GETADDRINFO +#if MG_ENABLE_GETADDRINFO int rv = 0; struct addrinfo hints, *servinfo, *p; struct sockaddr_in *h = NULL; @@ -2003,7 +2024,7 @@ MG_INTERNAL int mg_parse_address(const char *str, union socket_address *sa, int *proto, char *host, size_t host_len) { unsigned int a, b, c, d, port = 0; int ch, len = 0; -#ifdef MG_ENABLE_IPV6 +#if MG_ENABLE_IPV6 char buf[100]; #endif @@ -2029,14 +2050,14 @@ MG_INTERNAL int mg_parse_address(const char *str, union socket_address *sa, sa->sin.sin_addr.s_addr = htonl(((uint32_t) a << 24) | ((uint32_t) b << 16) | c << 8 | d); sa->sin.sin_port = htons((uint16_t) port); -#ifdef MG_ENABLE_IPV6 +#if MG_ENABLE_IPV6 } else if (sscanf(str, "[%99[^]]]:%u%n", buf, &port, &len) == 2 && inet_pton(AF_INET6, buf, &sa->sin6.sin6_addr)) { /* IPv6 address, e.g. [3ffe:2a00:100:7031::1]:8080 */ sa->sin6.sin6_family = AF_INET6; sa->sin.sin_port = htons((uint16_t) port); #endif -#ifndef MG_DISABLE_RESOLVER +#if !MG_DISABLE_RESOLVER } else if (strlen(str) < host_len && sscanf(str, "%[^ :]:%u%n", host, &port, &len) == 2) { sa->sin.sin_port = htons((uint16_t) port); @@ -2051,7 +2072,7 @@ MG_INTERNAL int mg_parse_address(const char *str, union socket_address *sa, return 0; } -#ifndef MG_DISABLE_SYNC_RESOLVER +#if !MG_DISABLE_SYNC_RESOLVER if (!mg_resolve2(host, &sa->sin.sin_addr)) { return -1; } @@ -2072,7 +2093,7 @@ MG_INTERNAL int mg_parse_address(const char *str, union socket_address *sa, return port < 0xffffUL && (ch == '\0' || ch == ',' || isspace(ch)) ? len : -1; } -#ifdef MG_ENABLE_SSL +#if MG_ENABLE_SSL #ifndef MG_SOCKET_SIMPLELINK /* @@ -2080,7 +2101,7 @@ MG_INTERNAL int mg_parse_address(const char *str, union socket_address *sa, * https://github.com/cesanta/mongoose/blob/master/scripts/generate_ssl_certificates.sh */ -#ifndef MG_DISABLE_PFS +#if !MG_DISABLE_PFS /* * Cipher suite options used for TLS negotiation. * https://wiki.mozilla.org/Security/Server_Side_TLS#Recommended_configurations @@ -2159,7 +2180,7 @@ static int mg_use_cert(SSL_CTX *ctx, const char *cert, const char *key) { SSL_CTX_use_PrivateKey_file(ctx, key, 1) == 0) { return -2; } else { -#ifndef MG_DISABLE_PFS +#if !MG_DISABLE_PFS BIO *bio = NULL; DH *dh = NULL; @@ -2242,7 +2263,7 @@ static const char *mg_set_ssl2(struct mg_connection *nc, const char *cert, result = "SSL_new() failed"; } -#ifndef MG_DISABLE_PFS +#if !MG_DISABLE_PFS SSL_CTX_set_cipher_list(nc->ssl_ctx, mg_s_cipher_list); #endif @@ -2293,7 +2314,7 @@ void mg_send(struct mg_connection *nc, const void *buf, int len) { } else { mg_if_tcp_send(nc, buf, len); } -#if !defined(NO_LIBC) && !defined(MG_DISABLE_HEXDUMP) +#if !defined(NO_LIBC) && !MG_DISABLE_HEXDUMP if (nc->mgr && nc->mgr->hexdump_file != NULL) { mg_hexdump_connection(nc, nc->mgr->hexdump_file, buf, len, MG_EV_SEND); } @@ -2411,7 +2432,7 @@ void mg_if_connect_cb(struct mg_connection *nc, int err) { mg_call(nc, NULL, MG_EV_CONNECT, &err); } -#ifndef MG_DISABLE_RESOLVER +#if !MG_DISABLE_RESOLVER /* * Callback for the async resolver on mg_connect_opt() call. * Main task of this function is to trigger MG_EV_CONNECT event with @@ -2465,7 +2486,7 @@ struct mg_connection *mg_connect(struct mg_mgr *mgr, const char *address, return mg_connect_opt(mgr, address, callback, opts); } -#ifdef MG_ENABLE_SSL +#if MG_ENABLE_SSL static void mg_set_ssl_server_name(struct mg_connection *nc, const char *server_name) { DBG(("%p '%s'", nc, server_name)); @@ -2507,7 +2528,7 @@ struct mg_connection *mg_connect_opt(struct mg_mgr *mgr, const char *address, nc->flags |= (proto == SOCK_DGRAM) ? MG_F_UDP : 0; nc->user_data = opts.user_data; -#ifdef MG_ENABLE_SSL +#if MG_ENABLE_SSL LOG(LL_DEBUG, ("%p %s %s,%s,%s", nc, address, (opts.ssl_cert ? opts.ssl_cert : "-"), (opts.ssl_key ? opts.ssl_key : "-"), @@ -2529,7 +2550,7 @@ struct mg_connection *mg_connect_opt(struct mg_mgr *mgr, const char *address, #endif /* MG_ENABLE_SSL */ if (rc == 0) { -#ifndef MG_DISABLE_RESOLVER +#if !MG_DISABLE_RESOLVER /* * DNS resolution is required for host. * mg_parse_address() fills port in nc->sa, which we pass to resolve_cb() @@ -2546,7 +2567,7 @@ struct mg_connection *mg_connect_opt(struct mg_mgr *mgr, const char *address, } nc->priv_2 = dns_conn; nc->flags |= MG_F_RESOLVING; -#ifdef MG_ENABLE_SSL +#if MG_ENABLE_SSL if (opts.ssl_ca_cert != NULL && opts.ssl_server_name == NULL) { mg_set_ssl_server_name(nc, host); } @@ -2595,7 +2616,7 @@ struct mg_connection *mg_bind_opt(struct mg_mgr *mgr, const char *address, nc->flags |= MG_F_LISTENING; if (proto == SOCK_DGRAM) nc->flags |= MG_F_UDP; -#ifdef MG_ENABLE_SSL +#if MG_ENABLE_SSL DBG(("%p %s %s %s %s", nc, address, (opts.ssl_cert ? opts.ssl_cert : ""), (opts.ssl_key ? opts.ssl_key : ""), (opts.ssl_ca_cert ? opts.ssl_ca_cert : ""))); @@ -2631,7 +2652,7 @@ struct mg_connection *mg_next(struct mg_mgr *s, struct mg_connection *conn) { return conn == NULL ? s->active_connections : conn->next; } -#ifndef MG_DISABLE_SOCKETPAIR +#if !MG_DISABLE_SOCKETPAIR void mg_broadcast(struct mg_mgr *mgr, mg_event_handler_t cb, void *data, size_t len) { struct ctl_msg ctl_msg; @@ -2752,7 +2773,7 @@ double mg_time(void) { * All rights reserved */ -#if !defined(MG_DISABLE_SOCKET_IF) && !defined(MG_SOCKET_SIMPLELINK) +#if !MG_DISABLE_SOCKET_IF && !defined(MG_SOCKET_SIMPLELINK) /* Amalgamated: #include "mongoose/src/internal.h" */ /* Amalgamated: #include "mongoose/src/util.h" */ @@ -2762,7 +2783,7 @@ double mg_time(void) { static sock_t mg_open_listening_socket(union socket_address *sa, int type, int proto); -#if defined(MG_ENABLE_SSL) +#if MG_ENABLE_SSL static void mg_ssl_begin(struct mg_connection *nc); static int mg_ssl_err(struct mg_connection *conn, int res); #endif @@ -2881,7 +2902,7 @@ static int mg_accept_conn(struct mg_connection *lc) { DBG(("%p conn from %s:%d", nc, inet_ntoa(sa.sin.sin_addr), ntohs(sa.sin.sin_port))); mg_sock_set(nc, sock); -#if defined(MG_ENABLE_SSL) +#if MG_ENABLE_SSL if (lc->ssl_ctx != NULL) { nc->ssl = SSL_new(lc->ssl_ctx); if (nc->ssl == NULL || SSL_set_fd(nc->ssl, sock) != 1) { @@ -2966,7 +2987,7 @@ static void mg_write_to_socket(struct mg_connection *nc) { return; } -#if defined(MG_ENABLE_SSL) +#if MG_ENABLE_SSL if (nc->ssl != NULL) { if (nc->flags & MG_F_SSL_HANDSHAKE_DONE) { n = SSL_write(nc->ssl, io->buf, io->len); @@ -3019,7 +3040,7 @@ static void mg_handle_tcp_read(struct mg_connection *conn) { return; } -#if defined(MG_ENABLE_SSL) +#if MG_ENABLE_SSL if (conn->ssl != NULL) { if (conn->flags & MG_F_SSL_HANDSHAKE_DONE) { /* SSL library may have more bytes ready to read then we ask to read. @@ -3087,7 +3108,7 @@ static void mg_handle_udp_read(struct mg_connection *nc) { mg_if_recv_udp_cb(nc, buf, n, &sa, sa_len); } -#if defined(MG_ENABLE_SSL) +#if MG_ENABLE_SSL static int mg_ssl_err(struct mg_connection *conn, int res) { int ssl_err = SSL_get_error(conn->ssl, res); DBG(("%p %d -> %d", conn, res, ssl_err)); @@ -3130,7 +3151,7 @@ static void mg_ssl_begin(struct mg_connection *nc) { } } } -#endif /* defined(MG_ENABLE_SSL) */ +#endif /* MG_ENABLE_SSL */ #define _MG_F_FD_CAN_READ 1 #define _MG_F_FD_CAN_WRITE 1 << 1 @@ -3156,7 +3177,7 @@ void mg_mgr_handle_conn(struct mg_connection *nc, int fd_flags, double now) { */ err = nc->err; #endif -#if defined(MG_ENABLE_SSL) +#if MG_ENABLE_SSL if (nc->ssl != NULL && err == 0) { SSL_set_fd(nc->ssl, nc->sock); mg_ssl_begin(nc); @@ -3203,7 +3224,7 @@ void mg_mgr_handle_conn(struct mg_connection *nc, int fd_flags, double now) { (int) nc->recv_mbuf.len, (int) nc->send_mbuf.len)); } -#ifndef MG_DISABLE_SOCKETPAIR +#if !MG_DISABLE_SOCKETPAIR static void mg_mgr_handle_ctl_sock(struct mg_mgr *mgr) { struct ctl_msg ctl_msg; int len = @@ -3231,7 +3252,7 @@ void mg_sock_set(struct mg_connection *nc, sock_t sock) { void mg_ev_mgr_init(struct mg_mgr *mgr) { (void) mgr; DBG(("%p using select()", mgr)); -#ifndef MG_DISABLE_SOCKETPAIR +#if !MG_DISABLE_SOCKETPAIR do { mg_socketpair(mgr->ctl, SOCK_DGRAM); } while (mgr->ctl[0] == INVALID_SOCKET); @@ -3278,7 +3299,7 @@ time_t mg_mgr_poll(struct mg_mgr *mgr, int timeout_ms) { FD_ZERO(&read_set); FD_ZERO(&write_set); FD_ZERO(&err_set); -#ifndef MG_DISABLE_SOCKETPAIR +#if !MG_DISABLE_SOCKETPAIR mg_add_to_set(mgr->ctl[1], &read_set, &max_fd); #endif @@ -3348,7 +3369,7 @@ time_t mg_mgr_poll(struct mg_mgr *mgr, int timeout_ms) { DBG(("select @ %ld num_ev=%d of %d, timeout=%d", (long) now, num_ev, num_fds, timeout_ms)); -#ifndef MG_DISABLE_SOCKETPAIR +#if !MG_DISABLE_SOCKETPAIR if (num_ev > 0 && mgr->ctl[1] != INVALID_SOCKET && FD_ISSET(mgr->ctl[1], &read_set)) { mg_mgr_handle_ctl_sock(mgr); @@ -3386,7 +3407,7 @@ time_t mg_mgr_poll(struct mg_mgr *mgr, int timeout_ms) { return (time_t) now; } -#ifndef MG_DISABLE_SOCKETPAIR +#if !MG_DISABLE_SOCKETPAIR int mg_socketpair(sock_t sp[2], int sock_type) { union socket_address sa; sock_t sock; @@ -3452,7 +3473,7 @@ void mg_if_get_conn_addr(struct mg_connection *nc, int remote, mg_sock_get_addr(nc->sock, remote, sa); } -#endif /* !defined(MG_DISABLE_SOCKET_IF) && !defined(MG_SOCKET_SIMPLELINK) */ +#endif /* !MG_DISABLE_SOCKET_IF && !defined(MG_SOCKET_SIMPLELINK) */ #ifdef MG_MODULE_LINES #line 1 "mongoose/src/multithreading.c" #endif @@ -3464,7 +3485,7 @@ void mg_if_get_conn_addr(struct mg_connection *nc, int remote, /* Amalgamated: #include "mongoose/src/internal.h" */ /* Amalgamated: #include "mongoose/src/util.h" */ -#ifdef MG_ENABLE_THREADS +#if MG_ENABLE_THREADS static void multithreaded_ev_handler(struct mg_connection *c, int ev, void *p); @@ -3770,19 +3791,19 @@ int mg_normalize_uri_path(const struct mg_str *in, struct mg_str *out) { * All rights reserved */ -#ifndef MG_DISABLE_HTTP +#if !MG_DISABLE_HTTP /* Amalgamated: #include "mongoose/src/internal.h" */ /* Amalgamated: #include "mongoose/src/util.h" */ /* Amalgamated: #include "common/sha1.h" */ /* Amalgamated: #include "common/md5.h" */ -#ifndef MG_DISABLE_HTTP_WEBSOCKET +#if !MG_DISABLE_HTTP_WEBSOCKET #define MG_WS_NO_HOST_HEADER_MAGIC ((char *) 0x1) #endif /* CGI requires socketpair. */ -#if defined(MG_DISABLE_SOCKETPAIR) && !defined(MG_DISABLE_CGI) +#if MG_DISABLE_SOCKETPAIR && !MG_DISABLE_CGI #define MG_DISABLE_CGI 1 #endif @@ -3835,13 +3856,13 @@ struct mg_http_multipart_stream { }; struct mg_http_proto_data { -#ifndef MG_DISABLE_FILESYSTEM +#if !MG_DISABLE_FILESYSTEM struct mg_http_proto_data_file file; #endif -#ifndef MG_DISABLE_CGI +#if !MG_DISABLE_CGI struct mg_http_proto_data_cgi cgi; #endif -#ifdef MG_ENABLE_HTTP_STREAMING_MULTIPART +#if MG_ENABLE_HTTP_STREAMING_MULTIPART struct mg_http_multipart_stream mp_stream; #endif struct mg_http_proto_data_chuncked chunk; @@ -3861,7 +3882,7 @@ static struct mg_http_proto_data *mg_http_get_proto_data( return (struct mg_http_proto_data *) c->proto_data; } -#ifdef MG_ENABLE_HTTP_STREAMING_MULTIPART +#if MG_ENABLE_HTTP_STREAMING_MULTIPART static void mg_http_free_proto_data_mp_stream( struct mg_http_multipart_stream *mp) { free((void *) mp->boundary); @@ -3871,7 +3892,7 @@ static void mg_http_free_proto_data_mp_stream( } #endif -#ifndef MG_DISABLE_FILESYSTEM +#if !MG_DISABLE_FILESYSTEM static void mg_http_free_proto_data_file(struct mg_http_proto_data_file *d) { if (d != NULL) { if (d->fp != NULL) { @@ -3882,7 +3903,7 @@ static void mg_http_free_proto_data_file(struct mg_http_proto_data_file *d) { } #endif -#ifndef MG_DISABLE_CGI +#if !MG_DISABLE_CGI static void mg_http_free_proto_data_cgi(struct mg_http_proto_data_cgi *d) { if (d != NULL) { if (d->cgi_nc != NULL) d->cgi_nc->flags |= MG_F_CLOSE_IMMEDIATELY; @@ -3906,13 +3927,13 @@ static void mg_http_free_proto_data_endpoints(struct mg_http_endpoint **ep) { static void mg_http_conn_destructor(void *proto_data) { struct mg_http_proto_data *pd = (struct mg_http_proto_data *) proto_data; -#ifndef MG_DISABLE_FILESYSTEM +#if !MG_DISABLE_FILESYSTEM mg_http_free_proto_data_file(&pd->file); #endif -#ifndef MG_DISABLE_CGI +#if !MG_DISABLE_CGI mg_http_free_proto_data_cgi(&pd->cgi); #endif -#ifdef MG_ENABLE_HTTP_STREAMING_MULTIPART +#if MG_ENABLE_HTTP_STREAMING_MULTIPART mg_http_free_proto_data_mp_stream(&pd->mp_stream); #endif mg_http_free_proto_data_endpoints(&pd->endpoints); @@ -3937,7 +3958,7 @@ struct mg_cgi_env_block { int nvars; /* Number of variables in envp[] */ }; -#ifndef MG_DISABLE_FILESYSTEM +#if !MG_DISABLE_FILESYSTEM #define MIME_ENTRY(_ext, _type) \ { _ext, sizeof(_ext) - 1, _type } @@ -3996,7 +4017,7 @@ static const struct { MIME_ENTRY("bmp", "image/bmp"), {NULL, 0, NULL}}; -#ifndef MG_DISABLE_DAV +#if !MG_DISABLE_DAV static int mg_mkdir(const char *path, uint32_t mode) { #ifndef _WIN32 return mkdir(path, mode); @@ -4178,7 +4199,7 @@ struct mg_str *mg_get_http_header(struct http_message *hm, const char *name) { return NULL; } -#ifndef MG_DISABLE_HTTP_WEBSOCKET +#if !MG_DISABLE_HTTP_WEBSOCKET static int mg_is_ws_fragment(unsigned char flags) { return (flags & 0x80) == 0 || (flags & 0x0f) == 0; @@ -4304,7 +4325,7 @@ static uint32_t mg_ws_random_mask(void) { * mongoose use cases and thus can be disabled, e.g. when porting to a platform * that lacks rand(). */ -#ifdef MG_DISABLE_WS_RANDOM_MASK +#if MG_DISABLE_WS_RANDOM_MASK mask = 0xefbeadde; /* generated with a random number generator, I swear */ #else if (sizeof(long) >= 4) { @@ -4475,7 +4496,7 @@ static void mg_ws_handshake(struct mg_connection *nc, #endif /* MG_DISABLE_HTTP_WEBSOCKET */ -#ifndef MG_DISABLE_FILESYSTEM +#if !MG_DISABLE_FILESYSTEM static void mg_http_transfer_file_data(struct mg_connection *nc) { struct mg_http_proto_data *pd = mg_http_get_proto_data(nc); char buf[MG_MAX_HTTP_SEND_MBUF]; @@ -4514,7 +4535,7 @@ static void mg_http_transfer_file_data(struct mg_connection *nc) { mg_http_free_proto_data_file(&pd->file); } } -#ifndef MG_DISABLE_CGI +#if !MG_DISABLE_CGI else if (pd->cgi.cgi_nc != NULL) { /* This is POST data that needs to be forwarded to the CGI process */ if (pd->cgi.cgi_nc != NULL) { @@ -4664,7 +4685,7 @@ static void mg_http_call_endpoint_handler(struct mg_connection *nc, int ev, hm); } -#ifdef MG_ENABLE_HTTP_STREAMING_MULTIPART +#if MG_ENABLE_HTTP_STREAMING_MULTIPART static void mg_http_multipart_continue(struct mg_connection *nc); static void mg_http_multipart_begin(struct mg_connection *nc, @@ -4697,11 +4718,11 @@ void mg_http_handler(struct mg_connection *nc, int ev, void *ev_data) { struct mbuf *io = &nc->recv_mbuf; int req_len; const int is_req = (nc->listener != NULL); -#ifndef MG_DISABLE_HTTP_WEBSOCKET +#if !MG_DISABLE_HTTP_WEBSOCKET struct mg_str *vec; #endif if (ev == MG_EV_CLOSE) { -#ifdef MG_ENABLE_HTTP_STREAMING_MULTIPART +#if MG_ENABLE_HTTP_STREAMING_MULTIPART if (pd->mp_stream.boundary != NULL) { /* * Multipart message is in progress, but we get close @@ -4729,7 +4750,7 @@ void mg_http_handler(struct mg_connection *nc, int ev, void *ev_data) { } } -#ifndef MG_DISABLE_FILESYSTEM +#if !MG_DISABLE_FILESYSTEM if (pd->file.fp != NULL) { mg_http_transfer_file_data(nc); } @@ -4740,7 +4761,7 @@ void mg_http_handler(struct mg_connection *nc, int ev, void *ev_data) { if (ev == MG_EV_RECV) { struct mg_str *s; -#ifdef MG_ENABLE_HTTP_STREAMING_MULTIPART +#if MG_ENABLE_HTTP_STREAMING_MULTIPART if (pd->mp_stream.boundary != NULL) { mg_http_multipart_continue(nc); return; @@ -4755,7 +4776,7 @@ void mg_http_handler(struct mg_connection *nc, int ev, void *ev_data) { mg_handle_chunked(nc, hm, io->buf + req_len, io->len - req_len); } -#ifdef MG_ENABLE_HTTP_STREAMING_MULTIPART +#if MG_ENABLE_HTTP_STREAMING_MULTIPART if (req_len > 0 && (s = mg_get_http_header(hm, "Content-Type")) != NULL && s->len >= 9 && strncmp(s->p, "multipart", 9) == 0) { mg_http_multipart_begin(nc, hm, req_len); @@ -4772,7 +4793,7 @@ void mg_http_handler(struct mg_connection *nc, int ev, void *ev_data) { } else if (req_len == 0) { /* Do nothing, request is not yet fully buffered */ } -#ifndef MG_DISABLE_HTTP_WEBSOCKET +#if !MG_DISABLE_HTTP_WEBSOCKET else if (nc->listener == NULL && mg_get_http_header(hm, "Sec-WebSocket-Accept")) { /* We're websocket client, got handshake response from server. */ @@ -4805,7 +4826,7 @@ void mg_http_handler(struct mg_connection *nc, int ev, void *ev_data) { /* Whole HTTP message is fully buffered, call event handler */ -#ifdef MG_ENABLE_JAVASCRIPT +#if MG_ENABLE_JAVASCRIPT v7_val_t v1, v2, headers, req, args, res; struct v7 *v7 = nc->mgr->v7; const char *ev_name = trigger_ev == MG_EV_HTTP_REPLY ? "onsnd" : "onrcv"; @@ -4865,7 +4886,7 @@ static size_t mg_get_line_len(const char *buf, size_t buf_len) { return len == buf_len ? 0 : len + 1; } -#ifdef MG_ENABLE_HTTP_STREAMING_MULTIPART +#if MG_ENABLE_HTTP_STREAMING_MULTIPART static void mg_http_multipart_begin(struct mg_connection *nc, struct http_message *hm, int req_len) { struct mg_http_proto_data *pd = mg_http_get_proto_data(nc); @@ -5254,7 +5275,7 @@ void mg_set_protocol_http_websocket(struct mg_connection *nc) { nc->proto_handler = mg_http_handler; } -#ifndef MG_DISABLE_HTTP_WEBSOCKET +#if !MG_DISABLE_HTTP_WEBSOCKET void mg_send_websocket_handshake2(struct mg_connection *nc, const char *path, const char *host, const char *protocol, @@ -5373,7 +5394,7 @@ void mg_send_head(struct mg_connection *c, int status_code, mg_send(c, "\r\n", 2); } -#ifdef MG_DISABLE_FILESYSTEM +#if MG_DISABLE_FILESYSTEM void mg_serve_http(struct mg_connection *nc, struct http_message *hm, struct mg_serve_http_opts opts) { mg_send_head(nc, 501, 0, NULL); @@ -5388,7 +5409,7 @@ static void mg_http_send_error(struct mg_connection *nc, int code, mg_send(nc, reason, strlen(reason)); nc->flags |= MG_F_SEND_AND_CLOSE; } -#ifndef MG_DISABLE_SSI +#if !MG_DISABLE_SSI static void mg_send_ssi_file(struct mg_connection *nc, struct http_message *hm, const char *path, FILE *fp, int include_level, const struct mg_serve_http_opts *opts); @@ -5447,7 +5468,7 @@ static void mg_do_ssi_include(struct mg_connection *nc, struct http_message *hm, } } -#ifndef MG_DISABLE_POPEN +#if !MG_DISABLE_POPEN static void do_ssi_exec(struct mg_connection *nc, char *tag) { char cmd[BUFSIZ]; FILE *fp; @@ -5473,7 +5494,7 @@ static void mg_send_ssi_file(struct mg_connection *nc, struct http_message *hm, static const struct mg_str btag = MG_MK_STR("