Ver Fonte

Add a configuration for additional headers

bel2125 há 8 anos atrás
pai
commit
ff575773c7
2 ficheiros alterados com 18 adições e 2 exclusões
  1. 7 0
      docs/UserManual.md
  2. 11 2
      src/civetweb.c

+ 7 - 0
docs/UserManual.md

@@ -550,6 +550,13 @@ This option can be used to enable or disable the use of the Linux `sendfile` sys
 ### case\_sensitive `no`
 This option can be uset to enable case URLs for Windows servers. It is only available for Windows systems. Windows file systems are not case sensitive, but they still store the file name including case. If this option is set to `yes`, the comparison for URIs and Windows file names will be case sensitive.
 
+### additional\_header
+Send additional HTTP response header line for every request.
+The full header line including key and value must be specified, excluding the carriage return line feed.
+
+Example:
+"X-Frame-Options: SAMEORIGIN"
+
 
 # Lua Scripts and Lua Server Pages
 Pre-built Windows and Mac civetweb binaries have built-in Lua scripting

+ 11 - 2
src/civetweb.c

@@ -1905,6 +1905,7 @@ enum {
 #if defined(USE_LUA)
 	LUA_BACKGROUND_SCRIPT,
 #endif
+	ADDITIONAL_HEADER,
 
 	NUM_OPTIONS
 };
@@ -1998,17 +1999,21 @@ static struct mg_option config_options[] = {
 #if defined(USE_LUA)
     {"lua_background_script", CONFIG_TYPE_FILE, NULL},
 #endif
+    {"additional_header", CONFIG_TYPE_STRING, NULL},
 
     {NULL, CONFIG_TYPE_UNKNOWN, NULL}};
 
+
 /* Check if the config_options and the corresponding enum have compatible
  * sizes. */
 mg_static_assert((sizeof(config_options) / sizeof(config_options[0]))
                      == (NUM_OPTIONS + 1),
                  "config_options and enum not sync");
 
+
 enum { REQUEST_HANDLER, WEBSOCKET_HANDLER, AUTH_HANDLER };
 
+
 struct mg_handler_info {
 	/* Name/Pattern of the URI. */
 	char *uri;
@@ -2039,6 +2044,7 @@ struct mg_handler_info {
 	struct mg_handler_info *next;
 };
 
+
 struct mg_context {
 	volatile int stop_flag;        /* Should we stop event loop */
 	SSL_CTX *ssl_ctx;              /* SSL context */
@@ -3420,6 +3426,7 @@ static int
 send_additional_header(struct mg_connection *conn)
 {
 	int i = 0;
+	const char *header = conn->ctx->config[ADDITIONAL_HEADER];
 
 #if !defined(NO_SSL)
 	if (conn->ctx->config[STRICT_HTTPS_MAX_AGE]) {
@@ -3430,10 +3437,12 @@ send_additional_header(struct mg_connection *conn)
 			               (unsigned)max_age);
 		}
 	}
-#else
-	(void)conn; /* unused */
 #endif
 
+	if (header && header[0]) {
+		i += mg_printf(conn, "%s\r\n", header);
+	}
+
 	return i;
 }