Browse Source

Add new API function mg_send_file_body

bel2125 7 years ago
parent
commit
d504fa233d
5 changed files with 68 additions and 3 deletions
  1. 1 0
      docs/APIReference.md
  2. 4 1
      docs/api/mg_send_file.md
  3. 28 0
      docs/api/mg_send_file_body.md
  4. 20 1
      include/civetweb.h
  5. 15 1
      src/civetweb.c

+ 1 - 0
docs/APIReference.md

@@ -110,6 +110,7 @@ The content of both structures is not defined in the interface - they are only u
 * [`mg_printf( conn, fmt, ... );`](api/mg_printf.md)
 * [`mg_read( conn, buf, len );`](api/mg_read.md)
 * [`mg_send_chunk( conn, buf, len );`](api/mg_send_chunk.md)
+* [`mg_send_file( conn, path );`](api/mg_send_file_body.md)
 * [`mg_set_user_connection_data( conn, data );`](api/mg_set_user_connection_data.md)
 * [`mg_start_thread( f, p );`](api/mg_start_thread.md)
 * [`mg_store_body( conn, path );`](api/mg_store_body.md)

+ 4 - 1
docs/api/mg_send_file.md

@@ -19,7 +19,10 @@ The function `mg_send_file()` sends the contents of a file over a connection to
 
 ### See Also
 
-* [`mg_printf();`](mg_printf.md)
+* [`mg_send_file_body();`](mg_send_file_body.md)
 * [`mg_send_mime_file();`](mg_send_mime_file.md)
 * [`mg_send_mime_file2();`](mg_send_mime_file2.md)
+* [`mg_printf();`](mg_printf.md)
 * [`mg_write();`](mg_write.md)
+
+

+ 28 - 0
docs/api/mg_send_file_body.md

@@ -0,0 +1,28 @@
+# Civetweb API Reference
+
+### `mg_send_file_body( conn, path );`
+
+### Parameters
+
+| Parameter | Type | Description |
+| :--- | :--- | :--- |
+|**`conn`**|`struct mg_connection *`|The connection over which the file must be sent|
+|**`path`**|`const char *`|The full path and filename of the file|
+
+### Return Value
+
+| Type | Description |
+|`int`| An integer indicating success (>=0) or failure (<0) |
+
+### Description
+
+The function `mg_send_file_file()` sends the contents of a file over a connection to the remote peer without adding any HTTP headers. The code must send all required HTTP response headers before using this function.
+
+### See Also
+
+* [`mg_send_file();`](mg_send_file.md)
+* [`mg_send_mime_file();`](mg_send_mime_file.md)
+* [`mg_send_mime_file2();`](mg_send_mime_file2.md)
+* [`mg_printf();`](mg_printf.md)
+* [`mg_write();`](mg_write.md)
+

+ 20 - 1
include/civetweb.h

@@ -875,10 +875,29 @@ CIVETWEB_API int mg_send_chunk(struct mg_connection *conn,
                                unsigned int chunk_len);
 
 
-/* Send contents of the entire file together with HTTP headers. */
+/* Send contents of the entire file together with HTTP headers.
+ * Parameters:
+ *   conn: Current connection information.
+ *   path: Full path to the file to send.
+ * This function has been superseded by mg_send_mime_file
+ */
 CIVETWEB_API void mg_send_file(struct mg_connection *conn, const char *path);
 
 
+/* Send contents of the file without HTTP headers.
+ * The code must send a valid HTTP response header before using this function.
+ *
+ * Parameters:
+ *   conn: Current connection information.
+ *   path: Full path to the file to send.
+ *
+ * Return:
+ *   < 0   Error
+*/
+CIVETWEB_API int mg_send_file_body(struct mg_connection *conn,
+                                   const char *path);
+
+
 /* Send HTTP error reply. */
 CIVETWEB_API int mg_send_http_error(struct mg_connection *conn,
                                     int status_code,

+ 15 - 1
src/civetweb.c

@@ -9713,6 +9713,20 @@ handle_static_file_request(struct mg_connection *conn,
 }
 
 
+int
+mg_send_file_body(struct mg_connection *conn, const char *path)
+{
+	struct mg_file file = STRUCT_FILE_INITIALIZER;
+	if (!mg_fopen(conn, path, MG_FOPEN_MODE_READ, &file)) {
+		return -1;
+	}
+	fclose_on_exec(&file.access, conn);
+	send_file_data(conn, &file, 0, INT64_MAX);
+	(void)mg_fclose(&file.access); /* Ignore errors for readonly files */
+	return 0;                      /* >= 0 for OK */
+}
+
+
 #if !defined(NO_CACHING)
 /* Return True if we should reply 304 Not Modified. */
 static int
@@ -9767,7 +9781,7 @@ handle_not_modified_static_file_request(struct mg_connection *conn,
 void
 mg_send_file(struct mg_connection *conn, const char *path)
 {
-	mg_send_mime_file(conn, path, NULL);
+	mg_send_mime_file2(conn, path, NULL, NULL);
 }