<feed xmlns='http://www.w3.org/2005/Atom'>
<title>unit-wasm.git/src/c/include/unit, branch v0.4.0</title>
<subtitle>Development libraries (C &amp; Rust) for writing WebAssembly modules for NGINX Unit</subtitle>
<link rel='alternate' type='text/html' href='https://git.sigsegv.uk/unit-wasm.git/'/>
<entry>
<title>unit-wasm 0.4.0</title>
<updated>2025-05-14T19:46:09+00:00</updated>
<author>
<name>Andrew Clayton</name>
<email>a.clayton@nginx.com</email>
</author>
<published>2025-05-14T19:46:09+00:00</published>
<link rel='alternate' type='text/html' href='https://git.sigsegv.uk/unit-wasm.git/commit/?id=7ef2f181304f19aba59066a35c72e1a239a4218d'/>
<id>7ef2f181304f19aba59066a35c72e1a239a4218d</id>
<content type='text'>
Signed-off-by: Andrew Clayton &lt;a.clayton@nginx.com&gt;
</content>
<content type='xhtml'>
<div xmlns='http://www.w3.org/1999/xhtml'>
<pre>
Signed-off-by: Andrew Clayton &lt;a.clayton@nginx.com&gt;
</pre>
</div>
</content>
</entry>
<entry>
<title>unit-wasm 0.3.0</title>
<updated>2023-10-19T18:35:12+00:00</updated>
<author>
<name>Andrew Clayton</name>
<email>a.clayton@nginx.com</email>
</author>
<published>2023-10-19T18:35:12+00:00</published>
<link rel='alternate' type='text/html' href='https://git.sigsegv.uk/unit-wasm.git/commit/?id=01c43784ec53aa1ff22aca7e7ae6f18b4591b514'/>
<id>01c43784ec53aa1ff22aca7e7ae6f18b4591b514</id>
<content type='text'>
Signed-off-by: Andrew Clayton &lt;a.clayton@nginx.com&gt;
</content>
<content type='xhtml'>
<div xmlns='http://www.w3.org/1999/xhtml'>
<pre>
Signed-off-by: Andrew Clayton &lt;a.clayton@nginx.com&gt;
</pre>
</div>
</content>
</entry>
<entry>
<title>libunit-wasm: Add a luw_mem_splice_file() function</title>
<updated>2023-09-25T16:39:43+00:00</updated>
<author>
<name>Andrew Clayton</name>
<email>a.clayton@nginx.com</email>
</author>
<published>2023-09-21T16:14:17+00:00</published>
<link rel='alternate' type='text/html' href='https://git.sigsegv.uk/unit-wasm.git/commit/?id=43c8e44d33572b818cdd0a5945e494c5510ab24f'/>
<id>43c8e44d33572b818cdd0a5945e494c5510ab24f</id>
<content type='text'>
This is inspired by the likes of splice(2) and sendfile(2) in that it
takes data from one place and puts it in another.

This function write(2)'s the request data straight from the shared
memory to a given file (referenced by its file descriptor).

This is an alternative to using luw_req_buf_copy() and avoids an extra
copying of the request data.

E.g

  /* In the request_handler */
  if (total_bytes_wrote == 0) {
          luw_init_ctx(&amp;ctx, addr, 0);
          luw_set_req_buf(&amp;ctx, &amp;request_buf, LUW_SRB_NONE);

          fd = open("/var/tmp/large-file.dat", O_CREAT|O_TRUNC|O_WRONLY,
                    0666);
  }

  total_bytes_wrote += luw_mem_splice_file(addr, fd);
  if (total_bytes_wrote == luw_get_http_content_len(&amp;ctx)) {
          close(fd);
          total_bytes_wrote = 0;
          luw_http_response_end();
  }

NOTE:

We include a typedef definition for ssize_t in unit-wasm.h, to avoid
having a dependency on the wasi-sysroot when generating the rust
bindings.

ssize_t is defined in sys/types.h which is provided by libc and not the
compiler.

Signed-off-by: Andrew Clayton &lt;a.clayton@nginx.com&gt;
</content>
<content type='xhtml'>
<div xmlns='http://www.w3.org/1999/xhtml'>
<pre>
This is inspired by the likes of splice(2) and sendfile(2) in that it
takes data from one place and puts it in another.

This function write(2)'s the request data straight from the shared
memory to a given file (referenced by its file descriptor).

This is an alternative to using luw_req_buf_copy() and avoids an extra
copying of the request data.

E.g

  /* In the request_handler */
  if (total_bytes_wrote == 0) {
          luw_init_ctx(&amp;ctx, addr, 0);
          luw_set_req_buf(&amp;ctx, &amp;request_buf, LUW_SRB_NONE);

          fd = open("/var/tmp/large-file.dat", O_CREAT|O_TRUNC|O_WRONLY,
                    0666);
  }

  total_bytes_wrote += luw_mem_splice_file(addr, fd);
  if (total_bytes_wrote == luw_get_http_content_len(&amp;ctx)) {
          close(fd);
          total_bytes_wrote = 0;
          luw_http_response_end();
  }

NOTE:

We include a typedef definition for ssize_t in unit-wasm.h, to avoid
having a dependency on the wasi-sysroot when generating the rust
bindings.

ssize_t is defined in sys/types.h which is provided by libc and not the
compiler.

Signed-off-by: Andrew Clayton &lt;a.clayton@nginx.com&gt;
</pre>
</div>
</content>
</entry>
<entry>
<title>libunit-wasm: Add a luw_req_buf_copy() function</title>
<updated>2023-09-25T16:36:24+00:00</updated>
<author>
<name>Andrew Clayton</name>
<email>a.clayton@nginx.com</email>
</author>
<published>2023-09-14T20:58:58+00:00</published>
<link rel='alternate' type='text/html' href='https://git.sigsegv.uk/unit-wasm.git/commit/?id=1dd1b34194573661b10015fbc60d6910dbea8fcc'/>
<id>1dd1b34194573661b10015fbc60d6910dbea8fcc</id>
<content type='text'>
This is analogous to luw_req_buf_append() but rather than appending
request data to the buffer it simply overwrites what's currently there.

This is needed to take advantage of the new ability to receive &gt;4GiB
requests/payloads.

On a new request you would call luw_init_ctx(), luw_set_req_buf() &amp;
open(2).

On subsequent calls to the request_handler (for this same HTTP
request/upload) you would call this new function and then write out the
data to a file.

E.g

  /* In the request_handler */
  if (total_bytes_wrote == 0) {
          luw_init_ctx(&amp;ctx, addr, 0);
          luw_set_req_buf(&amp;ctx, &amp;request_buf, LUW_SRB_NONE);

          fd = open("/var/tmp/large-file.dat", O_CREAT|O_TRUNC|O_WRONLY,
                    0666);
  } else {
          luw_req_buf_copy(&amp;ctx, addr);
  }

  buf = luw_get_http_content(&amp;ctx);
  bytes_wrote = write(fd, buf, luw_get_http_content_sent(&amp;ctx));
  total_bytes_wrote += bytes_wrote;
  if (total_bytes_wrote == luw_get_http_content_len(&amp;ctx)) {
          close(fd);
          total_bytes_wrote = 0;
          luw_http_response_end();
  }

Signed-off-by: Andrew Clayton &lt;a.clayton@nginx.com&gt;
</content>
<content type='xhtml'>
<div xmlns='http://www.w3.org/1999/xhtml'>
<pre>
This is analogous to luw_req_buf_append() but rather than appending
request data to the buffer it simply overwrites what's currently there.

This is needed to take advantage of the new ability to receive &gt;4GiB
requests/payloads.

On a new request you would call luw_init_ctx(), luw_set_req_buf() &amp;
open(2).

On subsequent calls to the request_handler (for this same HTTP
request/upload) you would call this new function and then write out the
data to a file.

E.g

  /* In the request_handler */
  if (total_bytes_wrote == 0) {
          luw_init_ctx(&amp;ctx, addr, 0);
          luw_set_req_buf(&amp;ctx, &amp;request_buf, LUW_SRB_NONE);

          fd = open("/var/tmp/large-file.dat", O_CREAT|O_TRUNC|O_WRONLY,
                    0666);
  } else {
          luw_req_buf_copy(&amp;ctx, addr);
  }

  buf = luw_get_http_content(&amp;ctx);
  bytes_wrote = write(fd, buf, luw_get_http_content_sent(&amp;ctx));
  total_bytes_wrote += bytes_wrote;
  if (total_bytes_wrote == luw_get_http_content_len(&amp;ctx)) {
          close(fd);
          total_bytes_wrote = 0;
          luw_http_response_end();
  }

Signed-off-by: Andrew Clayton &lt;a.clayton@nginx.com&gt;
</pre>
</div>
</content>
</entry>
<entry>
<title>libunit-wasm: Allow uploads larger than 4GiB</title>
<updated>2023-09-25T16:36:23+00:00</updated>
<author>
<name>Andrew Clayton</name>
<email>a.clayton@nginx.com</email>
</author>
<published>2023-09-14T20:43:52+00:00</published>
<link rel='alternate' type='text/html' href='https://git.sigsegv.uk/unit-wasm.git/commit/?id=ee64ca584081b812d9254ec116e1a060483071ec'/>
<id>ee64ca584081b812d9254ec116e1a060483071ec</id>
<content type='text'>
Currently Wasm modules are limited to a 32bit address space (until at
least the memory64 work is completed). All the counters etc in the
request structure were u32's. Which matched with the 32bit memory
limitation.

However there is really no need to not allow &gt;4GiB uploads that can be
saved off to disk or some such.

To do this we need to increase the -&gt;content_len &amp; -&gt;total_content_sent
members to u64's and also adjust the return types of
(luw,uwr}_get_http_content_len() and
{luw,uwr}_get_http_total_content_sent() similarly.

However because we need the request structure to have the exact same
layout on 32bit (for Wasm modules) as it does on 64bit we need to re-jig
the order of some of these members and add a four-byte padding member.

Thus the request structure now looks like on 32bit (as shown by
pahole(1))

  struct luw_req {
          u32                        method_off;           /*     0     4 */
          u32                        method_len;           /*     4     4 */
          u32                        version_off;          /*     8     4 */
          u32                        version_len;          /*    12     4 */
          u32                        path_off;             /*    16     4 */
          u32                        path_len;             /*    20     4 */
          u32                        query_off;            /*    24     4 */
          u32                        query_len;            /*    28     4 */
          u32                        remote_off;           /*    32     4 */
          u32                        remote_len;           /*    36     4 */
          u32                        local_addr_off;       /*    40     4 */
          u32                        local_addr_len;       /*    44     4 */
          u32                        local_port_off;       /*    48     4 */
          u32                        local_port_len;       /*    52     4 */
          u32                        server_name_off;      /*    56     4 */
          u32                        server_name_len;      /*    60     4 */
          /* --- cacheline 1 boundary (64 bytes) --- */
          u64                        content_len;          /*    64     8 */
          u64                        total_content_sent;   /*    72     8 */
          u32                        content_sent;         /*    80     4 */
          u32                        content_off;          /*    84     4 */
          u32                        request_size;         /*    88     4 */
          u32                        nr_fields;            /*    92     4 */
          u32                        tls;                  /*    96     4 */
          char                       __pad[4];             /*   100     4 */
          struct luw_hdr_field       fields[];             /*   104     0 */

          /* size: 104, cachelines: 2, members: 25 */
          /* last cacheline: 40 bytes */
  };

and the same structure (taken from Unit) compiled as 64bit

  struct nxt_wasm_request_s {
          uint32_t                   method_off;           /*     0     4 */
          uint32_t                   method_len;           /*     4     4 */
          uint32_t                   version_off;          /*     8     4 */
          uint32_t                   version_len;          /*    12     4 */
          uint32_t                   path_off;             /*    16     4 */
          uint32_t                   path_len;             /*    20     4 */
          uint32_t                   query_off;            /*    24     4 */
          uint32_t                   query_len;            /*    28     4 */
          uint32_t                   remote_off;           /*    32     4 */
          uint32_t                   remote_len;           /*    36     4 */
          uint32_t                   local_addr_off;       /*    40     4 */
          uint32_t                   local_addr_len;       /*    44     4 */
          uint32_t                   local_port_off;       /*    48     4 */
          uint32_t                   local_port_len;       /*    52     4 */
          uint32_t                   server_name_off;      /*    56     4 */
          uint32_t                   server_name_len;      /*    60     4 */
          /* --- cacheline 1 boundary (64 bytes) --- */
          uint64_t                   content_len;          /*    64     8 */
          uint64_t                   total_content_sent;   /*    72     8 */
          uint32_t                   content_sent;         /*    80     4 */
          uint32_t                   content_off;          /*    84     4 */
          uint32_t                   request_size;         /*    88     4 */
          uint32_t                   nfields;              /*    92     4 */
          uint32_t                   tls;                  /*    96     4 */
          char                       __pad[4];             /*   100     4 */
          nxt_wasm_http_field_t      fields[];             /*   104     0 */

          /* size: 104, cachelines: 2, members: 25 */
          /* last cacheline: 40 bytes */
  };

We can see the structures have the same layout, same size and no
padding.

We need the __pad member as otherwise I saw gcc and clang on Alpine
Linux automatically add the 'packed' attribute to the structure which
made the two structures not match.

Link: &lt;https://github.com/WebAssembly/memory64&gt;
Signed-off-by: Andrew Clayton &lt;a.clayton@nginx.com&gt;
</content>
<content type='xhtml'>
<div xmlns='http://www.w3.org/1999/xhtml'>
<pre>
Currently Wasm modules are limited to a 32bit address space (until at
least the memory64 work is completed). All the counters etc in the
request structure were u32's. Which matched with the 32bit memory
limitation.

However there is really no need to not allow &gt;4GiB uploads that can be
saved off to disk or some such.

To do this we need to increase the -&gt;content_len &amp; -&gt;total_content_sent
members to u64's and also adjust the return types of
(luw,uwr}_get_http_content_len() and
{luw,uwr}_get_http_total_content_sent() similarly.

However because we need the request structure to have the exact same
layout on 32bit (for Wasm modules) as it does on 64bit we need to re-jig
the order of some of these members and add a four-byte padding member.

Thus the request structure now looks like on 32bit (as shown by
pahole(1))

  struct luw_req {
          u32                        method_off;           /*     0     4 */
          u32                        method_len;           /*     4     4 */
          u32                        version_off;          /*     8     4 */
          u32                        version_len;          /*    12     4 */
          u32                        path_off;             /*    16     4 */
          u32                        path_len;             /*    20     4 */
          u32                        query_off;            /*    24     4 */
          u32                        query_len;            /*    28     4 */
          u32                        remote_off;           /*    32     4 */
          u32                        remote_len;           /*    36     4 */
          u32                        local_addr_off;       /*    40     4 */
          u32                        local_addr_len;       /*    44     4 */
          u32                        local_port_off;       /*    48     4 */
          u32                        local_port_len;       /*    52     4 */
          u32                        server_name_off;      /*    56     4 */
          u32                        server_name_len;      /*    60     4 */
          /* --- cacheline 1 boundary (64 bytes) --- */
          u64                        content_len;          /*    64     8 */
          u64                        total_content_sent;   /*    72     8 */
          u32                        content_sent;         /*    80     4 */
          u32                        content_off;          /*    84     4 */
          u32                        request_size;         /*    88     4 */
          u32                        nr_fields;            /*    92     4 */
          u32                        tls;                  /*    96     4 */
          char                       __pad[4];             /*   100     4 */
          struct luw_hdr_field       fields[];             /*   104     0 */

          /* size: 104, cachelines: 2, members: 25 */
          /* last cacheline: 40 bytes */
  };

and the same structure (taken from Unit) compiled as 64bit

  struct nxt_wasm_request_s {
          uint32_t                   method_off;           /*     0     4 */
          uint32_t                   method_len;           /*     4     4 */
          uint32_t                   version_off;          /*     8     4 */
          uint32_t                   version_len;          /*    12     4 */
          uint32_t                   path_off;             /*    16     4 */
          uint32_t                   path_len;             /*    20     4 */
          uint32_t                   query_off;            /*    24     4 */
          uint32_t                   query_len;            /*    28     4 */
          uint32_t                   remote_off;           /*    32     4 */
          uint32_t                   remote_len;           /*    36     4 */
          uint32_t                   local_addr_off;       /*    40     4 */
          uint32_t                   local_addr_len;       /*    44     4 */
          uint32_t                   local_port_off;       /*    48     4 */
          uint32_t                   local_port_len;       /*    52     4 */
          uint32_t                   server_name_off;      /*    56     4 */
          uint32_t                   server_name_len;      /*    60     4 */
          /* --- cacheline 1 boundary (64 bytes) --- */
          uint64_t                   content_len;          /*    64     8 */
          uint64_t                   total_content_sent;   /*    72     8 */
          uint32_t                   content_sent;         /*    80     4 */
          uint32_t                   content_off;          /*    84     4 */
          uint32_t                   request_size;         /*    88     4 */
          uint32_t                   nfields;              /*    92     4 */
          uint32_t                   tls;                  /*    96     4 */
          char                       __pad[4];             /*   100     4 */
          nxt_wasm_http_field_t      fields[];             /*   104     0 */

          /* size: 104, cachelines: 2, members: 25 */
          /* last cacheline: 40 bytes */
  };

We can see the structures have the same layout, same size and no
padding.

We need the __pad member as otherwise I saw gcc and clang on Alpine
Linux automatically add the 'packed' attribute to the structure which
made the two structures not match.

Link: &lt;https://github.com/WebAssembly/memory64&gt;
Signed-off-by: Andrew Clayton &lt;a.clayton@nginx.com&gt;
</pre>
</div>
</content>
</entry>
<entry>
<title>libunit-wasm: Allow to set the HTTP response status</title>
<updated>2023-09-12T16:21:11+00:00</updated>
<author>
<name>Andrew Clayton</name>
<email>a.clayton@nginx.com</email>
</author>
<published>2023-09-09T00:57:09+00:00</published>
<link rel='alternate' type='text/html' href='https://git.sigsegv.uk/unit-wasm.git/commit/?id=1beab00acdebc1263f83d393742271d31031bde3'/>
<id>1beab00acdebc1263f83d393742271d31031bde3</id>
<content type='text'>
This adds a new luw_http_set_response_status() function that takes one
of the luw_http_status_t response status codes.

This function should be called before any calls to
luw_http_send_headers() or luw_http_send_response().

This function calls into Unit via a new function import,
nxt_wasm_set_resp_status().

Signed-off-by: Andrew Clayton &lt;a.clayton@nginx.com&gt;
</content>
<content type='xhtml'>
<div xmlns='http://www.w3.org/1999/xhtml'>
<pre>
This adds a new luw_http_set_response_status() function that takes one
of the luw_http_status_t response status codes.

This function should be called before any calls to
luw_http_send_headers() or luw_http_send_response().

This function calls into Unit via a new function import,
nxt_wasm_set_resp_status().

Signed-off-by: Andrew Clayton &lt;a.clayton@nginx.com&gt;
</pre>
</div>
</content>
</entry>
<entry>
<title>unit-wasm 0.2.0</title>
<updated>2023-08-30T13:56:03+00:00</updated>
<author>
<name>Andrew Clayton</name>
<email>a.clayton@nginx.com</email>
</author>
<published>2023-08-30T13:56:03+00:00</published>
<link rel='alternate' type='text/html' href='https://git.sigsegv.uk/unit-wasm.git/commit/?id=d81bc9290edc8d1eb59ced024499359ccd4da77a'/>
<id>d81bc9290edc8d1eb59ced024499359ccd4da77a</id>
<content type='text'>
Signed-off-by: Andrew Clayton &lt;a.clayton@nginx.com&gt;
</content>
<content type='xhtml'>
<div xmlns='http://www.w3.org/1999/xhtml'>
<pre>
Signed-off-by: Andrew Clayton &lt;a.clayton@nginx.com&gt;
</pre>
</div>
</content>
</entry>
<entry>
<title>libunit-wasm: Add a luw_get_http_total_content_sent() function</title>
<updated>2023-08-29T22:12:56+00:00</updated>
<author>
<name>Andrew Clayton</name>
<email>a.clayton@nginx.com</email>
</author>
<published>2023-08-29T21:22:01+00:00</published>
<link rel='alternate' type='text/html' href='https://git.sigsegv.uk/unit-wasm.git/commit/?id=59cb1e8000e1b18e317cb1c19185eb3a1b5d4e9f'/>
<id>59cb1e8000e1b18e317cb1c19185eb3a1b5d4e9f</id>
<content type='text'>
This function returns the total amount of content that the Wasm module
has received so far.

This will be used in the rusty API's uwr_get_http_content_str() function
which returns the body content as a string. Due to the body content not
being null-terminated we need to know how much data to use for the
string, for which we are currently using uwr_get_http_content_len(),
which could in some cases return too much if a request is being split
over multiple calls the module.

Signed-off-by: Andrew Clayton &lt;a.clayton@nginx.com&gt;
</content>
<content type='xhtml'>
<div xmlns='http://www.w3.org/1999/xhtml'>
<pre>
This function returns the total amount of content that the Wasm module
has received so far.

This will be used in the rusty API's uwr_get_http_content_str() function
which returns the body content as a string. Due to the body content not
being null-terminated we need to know how much data to use for the
string, for which we are currently using uwr_get_http_content_len(),
which could in some cases return too much if a request is being split
over multiple calls the module.

Signed-off-by: Andrew Clayton &lt;a.clayton@nginx.com&gt;
</pre>
</div>
</content>
</entry>
<entry>
<title>libunit-wasm: Pass ctx into luw_http_hdr_get_value() as const</title>
<updated>2023-08-28T19:44:23+00:00</updated>
<author>
<name>Andrew Clayton</name>
<email>a.clayton@nginx.com</email>
</author>
<published>2023-08-28T11:37:01+00:00</published>
<link rel='alternate' type='text/html' href='https://git.sigsegv.uk/unit-wasm.git/commit/?id=8dce3b870b923db810d232e408c530f938f75d80'/>
<id>8dce3b870b923db810d232e408c530f938f75d80</id>
<content type='text'>
Technically the context pointer can be passed in const even though we
then un-const it passing it to the luw_foreach_http_hdr() macro.

Signed-off-by: Andrew Clayton &lt;a.clayton@nginx.com&gt;
</content>
<content type='xhtml'>
<div xmlns='http://www.w3.org/1999/xhtml'>
<pre>
Technically the context pointer can be passed in const even though we
then un-const it passing it to the luw_foreach_http_hdr() macro.

Signed-off-by: Andrew Clayton &lt;a.clayton@nginx.com&gt;
</pre>
</div>
</content>
</entry>
<entry>
<title>libunit-wasm: Remove the idx argument from luw_http_add_header()</title>
<updated>2023-08-28T15:20:26+00:00</updated>
<author>
<name>Andrew Clayton</name>
<email>a.clayton@nginx.com</email>
</author>
<published>2023-08-28T15:11:15+00:00</published>
<link rel='alternate' type='text/html' href='https://git.sigsegv.uk/unit-wasm.git/commit/?id=da5d9dc03b14b4d0f1ce0ce3ff093f387e148706'/>
<id>da5d9dc03b14b4d0f1ce0ce3ff093f387e148706</id>
<content type='text'>
This was used to specify the index of the response header being added,
starting at 0 and incrementing by one for each header.

Instead of having the programmer specify this, track it internally.

We add an extra check in luw_http_add_header() to make sure we aren't
trying to add more headers than we said with luw_http_init_headers(), if
we are, simply return.

This updates the API-C.md and the various examples and 'rusty' API
wrapper.

Suggested-by: Liam Crilly &lt;liam@nginx.com&gt;
Signed-off-by: Andrew Clayton &lt;a.clayton@nginx.com&gt;
</content>
<content type='xhtml'>
<div xmlns='http://www.w3.org/1999/xhtml'>
<pre>
This was used to specify the index of the response header being added,
starting at 0 and incrementing by one for each header.

Instead of having the programmer specify this, track it internally.

We add an extra check in luw_http_add_header() to make sure we aren't
trying to add more headers than we said with luw_http_init_headers(), if
we are, simply return.

This updates the API-C.md and the various examples and 'rusty' API
wrapper.

Suggested-by: Liam Crilly &lt;liam@nginx.com&gt;
Signed-off-by: Andrew Clayton &lt;a.clayton@nginx.com&gt;
</pre>
</div>
</content>
</entry>
</feed>
