Skip to content

Commit b163831

Browse files
committed
API-C.md: Update for libunit-wasm updates
The types of some of the member of the luw_req structure increased to 64bits to allow for uploads larger than 4GiB. Two new functions were added luw_req_buf_copy() Like luw_req_buf_append() but just copies the data over what's already there. luw_mem_splice_file() This write(2)'s the request data directly from the shared memory to a given file. Signed-off-by: Andrew Clayton <[email protected]>
1 parent f8927bd commit b163831

File tree

1 file changed

+98
-6
lines changed

1 file changed

+98
-6
lines changed

API-C.md

+98-6
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@ C Library for creating WebAssembly modules for use with NGINX Unit.
4848
* [luw_mem_writep](#luw_mem_writep)
4949
* [luw_mem_writep_data](#luw_mem_writep_data)
5050
* [luw_req_buf_append](#luw_req_buf_append)
51+
* [luw_req_buf_copy](#luw_req_buf_copy)
52+
* [luw_mem_splice_file](#luw_mem_splice_file)
5153
* [luw_mem_fill_buf_from_req](#luw_mem_fill_buf_from_req)
5254
* [luw_mem_reset](#luw_mem_reset)
5355
* [luw_http_set_response_status](#luw_http_set_response_status)
@@ -207,17 +209,19 @@ struct luw_req {
207209
u32 server_name_off;
208210
u32 server_name_len;
209211

210-
u32 content_off;
211-
u32 content_len;
212+
u64 content_len;
213+
u64 total_content_sent;
212214
u32 content_sent;
213-
u32 total_content_sent;
215+
u32 content_off;
214216

215217
u32 request_size;
216218

217219
u32 nr_fields;
218220

219221
u32 tls;
220222

223+
char __pad[4];
224+
221225
struct luw_hdr_field fields[];
222226
};
223227
```
@@ -701,11 +705,12 @@ This function returns a pointer to the start of the request body.
701705
### luw_get_http_content_len
702706
703707
```C
704-
size_t luw_get_http_content_len(const luw_ctx_t *ctx);
708+
u64 luw_get_http_content_len(const luw_ctx_t *ctx);
705709
```
706710

707711
This function returns the size of the overall content. I.e Content-Length.
708712

713+
Prior to version 0.3.0 it returned a size_t
709714

710715
### luw_get_http_content_sent
711716

@@ -720,14 +725,14 @@ split over several calls to luw_request_handler().
720725
### luw_get_http_total_content_sent
721726
722727
```C
723-
size_t luw_get_http_total_content_sent(const luw_ctx_t *ctx);
728+
u64 luw_get_http_total_content_sent(const luw_ctx_t *ctx);
724729
```
725730

726731
This function returns the total length of the content that was sent to the
727732
WebAssembly module so far. Remember, a single HTTP request may be split over
728733
several calls to luw_request_handler().
729734

730-
_Version: 0.2.0_
735+
_Version: 0.2.0_ Prior to 0.3.0 it returned a size_t
731736

732737
### luw_http_is_tls
733738

@@ -867,6 +872,93 @@ int luw_request_handler(u8 *addr)
867872
}
868873
```
869874

875+
### luw_req_buf_copy
876+
877+
```C
878+
void luw_req_buf_copy(luw_ctx_t *ctx, const u8 *src);
879+
```
880+
881+
This function is analogous to [luw_req_buf_append](#luw_req_buf_append) but
882+
rather than appending the request data contained in _src_ to the previously
883+
setup *request_buffer* with luw_set_req_buf(), it simply overwrites what's
884+
currently there.
885+
886+
This function could be used to handle large requests/uploads that you want to
887+
save out to disk or some such and can't buffer it all in memory.
888+
889+
Example
890+
891+
```C
892+
int luw_request_handler(u8 *addr)
893+
{
894+
const u8 *buf;
895+
ssize_t bytes_wrote;
896+
897+
if (total_bytes_wrote == 0) {
898+
luw_init_ctx(&ctx, addr, 0);
899+
luw_set_req_buf(&ctx, &request_buf, LUW_SRB_NONE);
900+
901+
fd = open("/var/tmp/large-file.dat", O_CREAT|O_TRUNC|O_WRONLY,
902+
0666);
903+
} else {
904+
luw_req_buf_copy(&ctx, addr);
905+
}
906+
907+
buf = luw_get_http_content(&ctx);
908+
bytes_wrote = write(fd, buf, luw_get_http_content_sent(&ctx));
909+
if (bytes_wrote == -1)
910+
return -1;
911+
912+
total_bytes_wrote += bytes_wrote;
913+
if (total_bytes_wrote == luw_get_http_content_len(&ctx))
914+
luw_http_response_end();
915+
916+
return 0;
917+
}
918+
```
919+
920+
_Version: 0.3.0_
921+
922+
### luw_mem_splice_file
923+
924+
```C
925+
ssize_t luw_mem_splice_file(const u8 *src, int fd);
926+
```
927+
928+
This function write(2)'s the request data directly from the shared memory
929+
(_src_) to the file represented by the given file-descriptor (_fd_).
930+
931+
This can be used as an alternative to [luw_req_buf_copy](#luw_req_buf_copy)
932+
and avoids an extra copying of the request data.
933+
934+
Example
935+
936+
```C
937+
int luw_request_handler(u8 *addr) {
938+
ssize_t bytes_wrote;
939+
940+
if (total_bytes_wrote == 0) {
941+
luw_init_ctx(&ctx, addr, 0);
942+
luw_set_req_buf(&ctx, &request_buf, LUW_SRB_NONE);
943+
944+
fd = open("/var/tmp/large-file.dat", O_CREAT|O_TRUNC|O_WRONLY,
945+
0666);
946+
}
947+
948+
bytes_wrote = luw_mem_splice_file(addr, fd);
949+
if (bytes_wrote == -1)
950+
return -1;
951+
952+
total_bytes_wrote += bytes_wrote;
953+
if (total_bytes_wrote == luw_get_http_content_len(&ctx))
954+
luw_http_response_end();
955+
956+
return 0;
957+
}
958+
```
959+
960+
_Version: 0.3.0_
961+
870962
### luw_mem_fill_buf_from_req
871963

872964
```C

0 commit comments

Comments
 (0)