ソースを参照

Add mg_send_chunk interface function

bel 8 年 前
コミット
db1acbfd0f
4 ファイル変更74 行追加38 行削除
  1. 27 0
      docs/api/mg_send_chunk.md
  2. 1 38
      examples/embedded_c/embedded_c.c
  3. 8 0
      include/civetweb.h
  4. 38 0
      src/civetweb.c

+ 27 - 0
docs/api/mg_send_chunk.md

@@ -0,0 +1,27 @@
+# Civetweb API Reference
+
+### `mg_send_chunk( conn, buf, len );`
+
+### Parameters
+
+| Parameter | Type | Description |
+| :--- | :--- | :--- |
+|**`conn`**|`struct mg_connection *`| A pointer to the connection to be used to send data |
+|**`chunk`**|`const void *`| A pointer to the data to be sent |
+|**`chunk_len`**|`size_t`| The number of bytes to be sent |
+
+### Return Value
+
+| Type | Description |
+| :--- | :--- |
+|`int`| An integer indicating the amount of bytes sent, or failure |
+
+### Description
+
+The function `mg_send_chunk()` can be used to send a block of data, if chunked transfer encoding is used. Only use this function after sending a complete HTTP request or response header with the "Transfer-Encoding: chunked" header field set.
+The function returns the amount of bytes sent in case of success, or **-1** in case of an error.
+
+### See Also
+
+* [`mg_write();`](mg_write.md)
+* [`mg_printf();`](mg_print.md)

+ 1 - 38
examples/embedded_c/embedded_c.c

@@ -462,43 +462,6 @@ CookieHandler(struct mg_connection *conn, void *cbdata)
 }
 
 
-static int
-send_chunk(struct mg_connection *conn,
-           const char *chunk,
-           unsigned int chunk_len)
-{
-	char lenbuf[16];
-	size_t lenbuf_len;
-	int ret;
-	int t;
-
-	/* First store the length information in a text buffer. */
-	sprintf(lenbuf, "%x\r\n", chunk_len);
-	lenbuf_len = strlen(lenbuf);
-
-	/* Then send length information, chunk and terminating \r\n. */
-	ret = mg_write(conn, lenbuf, lenbuf_len);
-	if (ret != (int)lenbuf_len) {
-		return -1;
-	}
-	t = ret;
-
-	ret = mg_write(conn, chunk, chunk_len);
-	if (ret != (int)chunk_len) {
-		return -1;
-	}
-	t += ret;
-
-	ret = mg_write(conn, "\r\n", 2);
-	if (ret != 2) {
-		return -1;
-	}
-	t += ret;
-
-	return t;
-}
-
-
 int
 PostResponser(struct mg_connection *conn, void *cbdata)
 {
@@ -542,7 +505,7 @@ PostResponser(struct mg_connection *conn, void *cbdata)
 	r = mg_read(conn, buf, sizeof(buf));
 	while (r > 0) {
 		r_total += r;
-		s = send_chunk(conn, buf, r);
+		s = mg_send_chunk(conn, buf, r);
 		if (r != s) {
 			/* Send error */
 			break;

+ 8 - 0
include/civetweb.h

@@ -684,6 +684,14 @@ CIVETWEB_API int mg_printf(struct mg_connection *,
                            ...) PRINTF_ARGS(2, 3);
 
 
+/* Send a part of the message body, if chunked transfer encoding is set.
+ * Only use this function after sending a complete HTTP request or response
+ * header with "Transfer-Encoding: chunked" set. */
+CIVETWEB_API int mg_send_chunk(struct mg_connection *conn,
+                               const char *chunk,
+                               unsigned int chunk_len);
+
+
 /* Send contents of the entire file together with HTTP headers. */
 CIVETWEB_API void mg_send_file(struct mg_connection *conn, const char *path);
 

+ 38 - 0
src/civetweb.c

@@ -5390,6 +5390,44 @@ mg_write(struct mg_connection *conn, const void *buf, size_t len)
 }
 
 
+/* Send a chunk, if "Transfer-Encoding: chunked" is used */
+int
+mg_send_chunk(struct mg_connection *conn,
+              const char *chunk,
+              unsigned int chunk_len)
+{
+	char lenbuf[16];
+	size_t lenbuf_len;
+	int ret;
+	int t;
+
+	/* First store the length information in a text buffer. */
+	sprintf(lenbuf, "%x\r\n", chunk_len);
+	lenbuf_len = strlen(lenbuf);
+
+	/* Then send length information, chunk and terminating \r\n. */
+	ret = mg_write(conn, lenbuf, lenbuf_len);
+	if (ret != (int)lenbuf_len) {
+		return -1;
+	}
+	t = ret;
+
+	ret = mg_write(conn, chunk, chunk_len);
+	if (ret != (int)chunk_len) {
+		return -1;
+	}
+	t += ret;
+
+	ret = mg_write(conn, "\r\n", 2);
+	if (ret != 2) {
+		return -1;
+	}
+	t += ret;
+
+	return t;
+}
+
+
 /* Alternative alloc_vprintf() for non-compliant C runtimes */
 static int
 alloc_vprintf2(char **buf, const char *fmt, va_list ap)