@@ -48,6 +48,8 @@ C Library for creating WebAssembly modules for use with NGINX Unit.
48
48
* [ luw_mem_writep] ( #luw_mem_writep )
49
49
* [ luw_mem_writep_data] ( #luw_mem_writep_data )
50
50
* [ 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 )
51
53
* [ luw_mem_fill_buf_from_req] ( #luw_mem_fill_buf_from_req )
52
54
* [ luw_mem_reset] ( #luw_mem_reset )
53
55
* [ luw_http_set_response_status] ( #luw_http_set_response_status )
@@ -207,17 +209,19 @@ struct luw_req {
207
209
u32 server_name_off;
208
210
u32 server_name_len;
209
211
210
- u32 content_off ;
211
- u32 content_len ;
212
+ u64 content_len ;
213
+ u64 total_content_sent ;
212
214
u32 content_sent;
213
- u32 total_content_sent ;
215
+ u32 content_off ;
214
216
215
217
u32 request_size;
216
218
217
219
u32 nr_fields;
218
220
219
221
u32 tls;
220
222
223
+ char __pad[4];
224
+
221
225
struct luw_hdr_field fields[];
222
226
};
223
227
```
@@ -701,11 +705,12 @@ This function returns a pointer to the start of the request body.
701
705
### luw_get_http_content_len
702
706
703
707
```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);
705
709
```
706
710
707
711
This function returns the size of the overall content. I.e Content-Length.
708
712
713
+ Prior to version 0.3.0 it returned a size_t
709
714
710
715
### luw_get_http_content_sent
711
716
@@ -720,14 +725,14 @@ split over several calls to luw_request_handler().
720
725
### luw_get_http_total_content_sent
721
726
722
727
```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);
724
729
```
725
730
726
731
This function returns the total length of the content that was sent to the
727
732
WebAssembly module so far. Remember, a single HTTP request may be split over
728
733
several calls to luw_request_handler().
729
734
730
- _ Version: 0.2.0_
735
+ _ Version: 0.2.0_ Prior to 0.3.0 it returned a size_t
731
736
732
737
### luw_http_is_tls
733
738
@@ -867,6 +872,93 @@ int luw_request_handler(u8 *addr)
867
872
}
868
873
```
869
874
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
+
870
962
### luw_mem_fill_buf_from_req
871
963
872
964
``` C
0 commit comments