Skip to content

Commit 1beab00

Browse files
committed
libunit-wasm: Allow to set the HTTP response status
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 <[email protected]>
1 parent 44442b8 commit 1beab00

File tree

3 files changed

+160
-2
lines changed

3 files changed

+160
-2
lines changed

API-C.md

Lines changed: 108 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ C Library for creating WebAssembly modules for use with NGINX Unit.
1212
* [Misc](#misc)
1313
3. [Types](#types)
1414
4. [Enums](#enums)
15+
* [luw_srb_flags_t](#luw_srb_flags_t)
16+
* [luw_http_status_t](#luw_http_status_t)
1517
5. [Structs](#structs)
1618
6. [Function Handlers](#function-handlers)
1719
* [Optional](#optional)
@@ -48,6 +50,7 @@ C Library for creating WebAssembly modules for use with NGINX Unit.
4850
* [luw_req_buf_append](#luw_req_buf_append)
4951
* [luw_mem_fill_buf_from_req](#luw_mem_fill_buf_from_req)
5052
* [luw_mem_reset](#luw_mem_reset)
53+
* [luw_http_set_response_status](#luw_http_set_response_status)
5154
* [luw_http_send_response](#luw_http_send_response)
5255
* [luw_http_init_headers](#luw_http_init_headers)
5356
* [luw_http_add_header](#luw_http_add_header)
@@ -112,6 +115,8 @@ typedef int8_t s8;
112115

113116
## Enums
114117

118+
### luw_srb_flags_t
119+
115120
```C
116121
typedef enum {
117122
LUW_SRB_NONE = 0x00,
@@ -124,6 +129,54 @@ typedef enum {
124129
} luw_srb_flags_t;
125130
```
126131

132+
### luw_http_status_t
133+
134+
```C
135+
typedef enum {
136+
LUW_HTTP_CONTINUE = 100,
137+
LUW_HTTP_SWITCHING_PROTOCOLS = 101,
138+
139+
LUW_HTTP_OK = 200,
140+
LUW_HTTP_CREATED = 201,
141+
LUW_HTTP_ACCEPTED = 202,
142+
LUW_HTTP_NO_CONTENT = 204,
143+
144+
LUW_HTTP_MULTIPLE_CHOICES = 300,
145+
LUW_HTTP_MOVED_PERMANENTLY = 301,
146+
LUW_HTTP_FOUND = 302,
147+
LUW_HTTP_SEE_OTHER = 303,
148+
LUW_HTTP_NOT_MODIFIED = 304,
149+
LUW_HTTP_TEMPORARY_REDIRECT = 307,
150+
LUW_HTTP_PERMANENT_REDIRECT = 308,
151+
152+
LUW_HTTP_BAD_REQUEST = 400,
153+
LUW_HTTP_UNAUTHORIZED = 401,
154+
LUW_HTTP_FORBIDDEN = 403,
155+
LUW_HTTP_NOT_FOUND = 404,
156+
LUW_HTTP_METHOD_NOT_ALLOWED = 405,
157+
LUW_HTTP_NOT_ACCEPTABLE = 406,
158+
LUW_HTTP_REQUEST_TIMEOUT = 408,
159+
LUW_HTTP_CONFLICT = 409,
160+
LUW_HTTP_GONE = 410,
161+
LUW_HTTP_LENGTH_REQUIRED = 411,
162+
LUW_HTTP_PAYLOAD_TOO_LARGE = 413,
163+
LUW_HTTP_URI_TOO_LONG = 414,
164+
LUW_HTTP_UNSUPPORTED_MEDIA_TYPE = 415,
165+
LUW_HTTP_UPGRADE_REQUIRED = 426,
166+
LUW_HTTP_TOO_MANY_REQUESTS = 429,
167+
LUW_HTTP_REQUEST_HEADER_FIELDS_TOO_LARGE = 431,
168+
169+
/* Proposed by RFC 7725 */
170+
LUW_HTTP_UNAVAILABLE_FOR_LEGAL_REASONS = 451,
171+
172+
LUW_HTTP_INTERNAL_SERVER_ERROR = 500,
173+
LUW_HTTP_NOT_IMPLEMENTED = 501,
174+
LUW_HTTP_BAD_GATEWAY = 502,
175+
LUW_HTTP_SERVICE_UNAVAILABLE = 503,
176+
LUW_HTTP_GATEWAY_TIMEOUT = 504,
177+
} luw_http_status_t;
178+
```
179+
127180
## Structs
128181

129182
```C
@@ -264,8 +317,14 @@ returned by luw_malloc_handler().
264317
265318
This memory will contain a *struct luw_req*.
266319
267-
It returns an int, this is currently ignored but will likely be used to
268-
indicate a HTTP status code.
320+
It returns an int. This should nearly always be _0_.
321+
322+
If you wish to indicate a '500 Internal Server Error', for example if some
323+
internal API has failed or an OS level error occurred, then you can simply
324+
return _-1_, _if_ you have haven't already _sent_ any response or headers.
325+
326+
You can still return 0 _and_ set the HTTP response status to 500 using
327+
[luw_http_set_resp_status](#luw_http_set_resp_status).
269328
270329
#### luw_malloc_handler
271330
@@ -850,6 +909,53 @@ void luw_mem_reset(luw_ctx_t *ctx);
850909
This function resets the response buffer size and the number of response
851910
headers back to 0.
852911
912+
### luw_http_set_response_status
913+
914+
```C
915+
void luw_http_set_response_status(luw_http_status_t status);
916+
```
917+
918+
This function is used to set the HTTP response status. It takes one of the
919+
[luw_http_status_t](#luw_http_status_t) enum values.
920+
921+
It should be called before any calls to *luw_http_send_response()* or
922+
*luw_http_send_headers()*.
923+
924+
If you don't call this function the response status defaults to '200 OK'.
925+
926+
If you wish to error out with a '500 Internal Server Error', you don't need to
927+
call this function. Simply returning _-1_ from the request_handler function
928+
will indicate this error.
929+
930+
E.g
931+
932+
Send a '403 Forbidden'
933+
934+
```C
935+
/* ... */
936+
luw_http_set_response_status(LUW_HTTP_FORBIDDEN);
937+
luw_http_send_response(ctx); /* Doesn't require any body */
938+
luw_http_response_end();
939+
/* ... */
940+
return 0;
941+
```
942+
943+
Send a '307 Temporary Re-direct'
944+
945+
```C
946+
/* ... */
947+
luw_http_set_response_status(LUW_HTTP_TEMPORARY_REDIRECT);
948+
949+
luw_http_init_headers(ctx, 1, 0);
950+
luw_http_add_header(ctx, "Location", "https://example.com/");
951+
luw_http_send_headers(ctx);
952+
luw_http_response_end();
953+
/* ... */
954+
return 0;
955+
```
956+
957+
_Version: 0.3.0_
958+
853959
### luw_http_send_response
854960

855961
```C

src/c/include/unit/unit-wasm.h

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,50 @@ typedef int16_t s16;
4040
typedef uint8_t u8;
4141
typedef int8_t s8;
4242

43+
typedef enum {
44+
LUW_HTTP_CONTINUE = 100,
45+
LUW_HTTP_SWITCHING_PROTOCOLS = 101,
46+
47+
LUW_HTTP_OK = 200,
48+
LUW_HTTP_CREATED = 201,
49+
LUW_HTTP_ACCEPTED = 202,
50+
LUW_HTTP_NO_CONTENT = 204,
51+
52+
LUW_HTTP_MULTIPLE_CHOICES = 300,
53+
LUW_HTTP_MOVED_PERMANENTLY = 301,
54+
LUW_HTTP_FOUND = 302,
55+
LUW_HTTP_SEE_OTHER = 303,
56+
LUW_HTTP_NOT_MODIFIED = 304,
57+
LUW_HTTP_TEMPORARY_REDIRECT = 307,
58+
LUW_HTTP_PERMANENT_REDIRECT = 308,
59+
60+
LUW_HTTP_BAD_REQUEST = 400,
61+
LUW_HTTP_UNAUTHORIZED = 401,
62+
LUW_HTTP_FORBIDDEN = 403,
63+
LUW_HTTP_NOT_FOUND = 404,
64+
LUW_HTTP_METHOD_NOT_ALLOWED = 405,
65+
LUW_HTTP_NOT_ACCEPTABLE = 406,
66+
LUW_HTTP_REQUEST_TIMEOUT = 408,
67+
LUW_HTTP_CONFLICT = 409,
68+
LUW_HTTP_GONE = 410,
69+
LUW_HTTP_LENGTH_REQUIRED = 411,
70+
LUW_HTTP_PAYLOAD_TOO_LARGE = 413,
71+
LUW_HTTP_URI_TOO_LONG = 414,
72+
LUW_HTTP_UNSUPPORTED_MEDIA_TYPE = 415,
73+
LUW_HTTP_UPGRADE_REQUIRED = 426,
74+
LUW_HTTP_TOO_MANY_REQUESTS = 429,
75+
LUW_HTTP_REQUEST_HEADER_FIELDS_TOO_LARGE = 431,
76+
77+
/* Proposed by RFC 7725 */
78+
LUW_HTTP_UNAVAILABLE_FOR_LEGAL_REASONS = 451,
79+
80+
LUW_HTTP_INTERNAL_SERVER_ERROR = 500,
81+
LUW_HTTP_NOT_IMPLEMENTED = 501,
82+
LUW_HTTP_BAD_GATEWAY = 502,
83+
LUW_HTTP_SERVICE_UNAVAILABLE = 503,
84+
LUW_HTTP_GATEWAY_TIMEOUT = 504,
85+
} luw_http_status_t;
86+
4387
struct luw_hdr_field {
4488
u32 name_off;
4589
u32 name_len;
@@ -151,6 +195,8 @@ __attribute__((import_module("env"), import_name("nxt_wasm_send_headers")))
151195
void nxt_wasm_send_headers(u32 offset);
152196
__attribute__((import_module("env"), import_name("nxt_wasm_send_response")))
153197
void nxt_wasm_send_response(u32 offset);
198+
__attribute__((import_module("env"), import_name("nxt_wasm_set_resp_status")))
199+
void nxt_wasm_set_resp_status(u32 status);
154200

155201
extern void luw_module_init_handler(void);
156202
extern void luw_module_end_handler(void);
@@ -192,6 +238,7 @@ extern size_t luw_mem_writep_data(luw_ctx_t *ctx, const u8 *src, size_t size);
192238
extern void luw_req_buf_append(luw_ctx_t *ctx, const u8 *src);
193239
extern size_t luw_mem_fill_buf_from_req(luw_ctx_t *ctx, size_t from);
194240
extern void luw_mem_reset(luw_ctx_t *ctx);
241+
extern void luw_http_set_response_status(luw_http_status_t status);
195242
extern void luw_http_send_response(const luw_ctx_t *ctx);
196243
extern void luw_http_init_headers(luw_ctx_t *ctx, size_t nr, size_t offset);
197244
extern void luw_http_add_header(luw_ctx_t *ctx, const char *name,

src/c/libunit-wasm.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -336,6 +336,11 @@ void luw_mem_reset(luw_ctx_t *ctx)
336336
ctx->resp_hdr_idx = -1;
337337
}
338338

339+
void luw_http_set_response_status(luw_http_status_t status)
340+
{
341+
nxt_wasm_set_resp_status(status);
342+
}
343+
339344
void luw_http_send_response(const luw_ctx_t *ctx)
340345
{
341346
nxt_wasm_send_response(ctx->resp_offset);

0 commit comments

Comments
 (0)