Bläddra i källkod

Merge pull request #332 from ioxp/master

Added optional additional_headers for mg_send_mime_file
bel2125 8 år sedan
förälder
incheckning
eafa23ea46
2 ändrade filer med 42 tillägg och 4 borttagningar
  1. 15 0
      include/civetweb.h
  2. 27 4
      src/civetweb.c

+ 15 - 0
include/civetweb.h

@@ -623,6 +623,21 @@ CIVETWEB_API void mg_send_mime_file(struct mg_connection *conn,
                                     const char *path,
                                     const char *mime_type);
 
+/* Send contents of the entire file together with HTTP headers.
+   Parameters:
+     conn: Current connection information.
+     path: Full path to the file to send.
+     mime_type: Content-Type for file.  NULL will cause the type to be
+                looked up by the file extension.
+     additional_headers: Additional custom header fields appended to the header.
+                         Each header must start with an X- to ensure it is not included twice.
+                         NULL does not append anything.
+*/
+CIVETWEB_API void mg_send_mime_file2(struct mg_connection *conn,
+                                    const char *path,
+                                    const char *mime_type,
+                                    const char* additional_headers);
+
 /* Store body data into a file. */
 CIVETWEB_API long long mg_store_body(struct mg_connection *conn,
                                      const char *path);

+ 27 - 4
src/civetweb.c

@@ -6886,7 +6886,8 @@ static void
 handle_static_file_request(struct mg_connection *conn,
                            const char *path,
                            struct file *filep,
-                           const char *mime_type)
+                           const char *mime_type,
+                           const char *additional_headers)
 {
 	char date[64], lm[64], etag[64];
 	char range[128]; /* large enough, so there will be no overflow */
@@ -7015,7 +7016,7 @@ handle_static_file_request(struct mg_connection *conn,
 	                "Content-Length: %" INT64_FMT "\r\n"
 	                "Connection: %s\r\n"
 	                "Accept-Ranges: bytes\r\n"
-	                "%s%s\r\n",
+	                "%s%s",
 	                lm,
 	                etag,
 	                (int)mime_vec.len,
@@ -7025,6 +7026,18 @@ handle_static_file_request(struct mg_connection *conn,
 	                range,
 	                encoding);
 
+    /* The previous code must not add any header starting with X- to make
+     * sure no one of the additional_headers is included twice */
+
+    if (additional_headers != NULL) {
+        (void)mg_printf(conn,
+                        "%.*s\r\n\r\n",
+                        (int)strlen(additional_headers),
+                        additional_headers);
+    } else {
+        (void)mg_printf(conn, "\r\n");
+    }
+
 	if (strcmp(conn->request_info.request_method, "HEAD") != 0) {
 		send_file_data(conn, filep, r1, cl);
 	}
@@ -7079,6 +7092,16 @@ mg_send_mime_file(struct mg_connection *conn,
                   const char *path,
                   const char *mime_type)
 {
+	mg_send_mime_file2(conn, path, mime_type, NULL);
+}
+
+
+void
+mg_send_mime_file2(struct mg_connection *conn,
+                  const char *path,
+                  const char *mime_type,
+                  const char* additional_headers)
+{
 	struct file file = STRUCT_FILE_INITIALIZER;
 	if (mg_stat(conn, path, &file)) {
 		if (file.is_directory) {
@@ -7095,7 +7118,7 @@ mg_send_mime_file(struct mg_connection *conn,
 				                "Error: Directory listing denied");
 			}
 		} else {
-			handle_static_file_request(conn, path, &file, mime_type);
+			handle_static_file_request(conn, path, &file, mime_type, additional_headers);
 		}
 	} else {
 		send_http_error(conn, 404, "%s", "Error: File not found");
@@ -10644,7 +10667,7 @@ handle_file_based_request(struct mg_connection *conn,
 		handle_not_modified_static_file_request(conn, file);
 #endif /* !NO_CACHING */
 	} else {
-		handle_static_file_request(conn, path, file, NULL);
+		handle_static_file_request(conn, path, file, NULL, NULL);
 	}
 }