@@ -12,6 +12,8 @@ C Library for creating WebAssembly modules for use with NGINX Unit.
12
12
* [ Misc] ( #misc )
13
13
3 . [ Types] ( #types )
14
14
4 . [ Enums] ( #enums )
15
+ * [ luw_srb_flags_t] ( #luw_srb_flags_t )
16
+ * [ luw_http_status_t] ( #luw_http_status_t )
15
17
5 . [ Structs] ( #structs )
16
18
6 . [ Function Handlers] ( #function-handlers )
17
19
* [ Optional] ( #optional )
@@ -48,6 +50,7 @@ C Library for creating WebAssembly modules for use with NGINX Unit.
48
50
* [ luw_req_buf_append] ( #luw_req_buf_append )
49
51
* [ luw_mem_fill_buf_from_req] ( #luw_mem_fill_buf_from_req )
50
52
* [ luw_mem_reset] ( #luw_mem_reset )
53
+ * [ luw_http_set_response_status] ( #luw_http_set_response_status )
51
54
* [ luw_http_send_response] ( #luw_http_send_response )
52
55
* [ luw_http_init_headers] ( #luw_http_init_headers )
53
56
* [ luw_http_add_header] ( #luw_http_add_header )
@@ -112,6 +115,8 @@ typedef int8_t s8;
112
115
113
116
## Enums
114
117
118
+ ### luw_srb_flags_t
119
+
115
120
```C
116
121
typedef enum {
117
122
LUW_SRB_NONE = 0x00,
@@ -124,6 +129,54 @@ typedef enum {
124
129
} luw_srb_flags_t ;
125
130
```
126
131
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
+
127
180
## Structs
128
181
129
182
``` C
@@ -264,8 +317,14 @@ returned by luw_malloc_handler().
264
317
265
318
This memory will contain a *struct luw_req*.
266
319
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).
269
328
270
329
#### luw_malloc_handler
271
330
@@ -850,6 +909,53 @@ void luw_mem_reset(luw_ctx_t *ctx);
850
909
This function resets the response buffer size and the number of response
851
910
headers back to 0.
852
911
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
+
853
959
### luw_http_send_response
854
960
855
961
``` C
0 commit comments