@@ -40,6 +40,8 @@ and there isn't a real need to create wrappers specifically for them.
40
40
* [ Version] ( #version )
41
41
* [ String Conversion] ( #string-conversion )
42
42
* [ uwr_write_str!] ( #uwr_write_str )
43
+ 3 . [ Enums] ( #enums )
44
+ [ luw_http_status_t] ( #luw_http_status_t )
43
45
3 . [ Function Handlers] ( #function-handlers )
44
46
* [ Optional] ( #optional )
45
47
- [ luw_module_init_handler] ( #luw_module_init_handler )
@@ -76,6 +78,7 @@ and there isn't a real need to create wrappers specifically for them.
76
78
* [ uwr_req_buf_append] ( #uwr_req_buf_append )
77
79
* [ uwr_mem_fill_buf_from_req] ( #uwr_mem_fill_buf_from_req )
78
80
* [ uwr_mem_reset] ( #uwr_mem_reset )
81
+ * [ uwr_http_set_response_status] ( #uwr_http_set_response_status )
79
82
* [ uwr_http_send_response] ( #uwr_http_send_response )
80
83
* [ uwr_http_init_headers] ( #uwr_http_init_headers )
81
84
* [ uwr_http_add_header] ( #uwr_http_add_header )
@@ -159,6 +162,55 @@ It takes the luw_ctx_t context pointer, a string that will be run through the
159
162
[ format!()] ( https://doc.rust-lang.org/std/macro.format.html ) macro and any
160
163
optional arguments.
161
164
165
+ ## Enums
166
+
167
+ ### luw_http_status_t
168
+ ``` Rust
169
+ pub enum luw_http_status_t {
170
+ LUW_HTTP_CONTINUE = 100 ,
171
+ LUW_HTTP_SWITCHING_PROTOCOLS = 101 ,
172
+
173
+ LUW_HTTP_OK = 200 ,
174
+ LUW_HTTP_CREATED = 201 ,
175
+ LUW_HTTP_ACCEPTED = 202 ,
176
+ LUW_HTTP_NO_CONTENT = 204 ,
177
+
178
+ LUW_HTTP_MULTIPLE_CHOICES = 300 ,
179
+ LUW_HTTP_MOVED_PERMANENTLY = 301 ,
180
+ LUW_HTTP_FOUND = 302 ,
181
+ LUW_HTTP_SEE_OTHER = 303 ,
182
+ LUW_HTTP_NOT_MODIFIED = 304 ,
183
+ LUW_HTTP_TEMPORARY_REDIRECT = 307 ,
184
+ LUW_HTTP_PERMANENT_REDIRECT = 308 ,
185
+
186
+ LUW_HTTP_BAD_REQUEST = 400 ,
187
+ LUW_HTTP_UNAUTHORIZED = 401 ,
188
+ LUW_HTTP_FORBIDDEN = 403 ,
189
+ LUW_HTTP_NOT_FOUND = 404 ,
190
+ LUW_HTTP_METHOD_NOT_ALLOWED = 405 ,
191
+ LUW_HTTP_NOT_ACCEPTABLE = 406 ,
192
+ LUW_HTTP_REQUEST_TIMEOUT = 408 ,
193
+ LUW_HTTP_CONFLICT = 409 ,
194
+ LUW_HTTP_GONE = 410 ,
195
+ LUW_HTTP_LENGTH_REQUIRED = 411 ,
196
+ LUW_HTTP_PAYLOAD_TOO_LARGE = 413 ,
197
+ LUW_HTTP_URI_TOO_LONG = 414 ,
198
+ LUW_HTTP_UNSUPPORTED_MEDIA_TYPE = 415 ,
199
+ LUW_HTTP_UPGRADE_REQUIRED = 426 ,
200
+ LUW_HTTP_TOO_MANY_REQUESTS = 429 ,
201
+ LUW_HTTP_REQUEST_HEADER_FIELDS_TOO_LARGE = 431 ,
202
+
203
+ /* Proposed by RFC 7725 */
204
+ LUW_HTTP_UNAVAILABLE_FOR_LEGAL_REASONS = 451 ,
205
+
206
+ LUW_HTTP_INTERNAL_SERVER_ERROR = 500 ,
207
+ LUW_HTTP_NOT_IMPLEMENTED = 501 ,
208
+ LUW_HTTP_BAD_GATEWAY = 502 ,
209
+ LUW_HTTP_SERVICE_UNAVAILABLE = 503 ,
210
+ LUW_HTTP_GATEWAY_TIMEOUT = 504 ,
211
+ }
212
+ ```
213
+
162
214
## Function Handlers
163
215
164
216
These functions are exported from the WebAssembly module and are called from
@@ -198,8 +250,14 @@ You will need to provide your own implementation of this function.
198
250
It receives the base address of the shared memory. Essentially what is
199
251
returned by luw_malloc_handler().
200
252
201
- It returns an int, this is currently ignored but will likely be used to
202
- indicate a HTTP status code.
253
+ It returns an int. This should nearly always be _ 0_ .
254
+
255
+ If you wish to indicate a '500 Internal Server Error', for example if some
256
+ internal API has failed or an OS level error occurred, then you can simply
257
+ return _ -1_ , _ if_ you have haven't already _ sent_ any response or headers.
258
+
259
+ You can still return 0 _ and_ set the HTTP response status to 500 using
260
+ [ uwr_http_set_response_status] ( #uwr_http_set_response_status ) .
203
261
204
262
#### luw_malloc_handler
205
263
@@ -822,6 +880,53 @@ pub fn uwr_luw_mem_reset(ctx: *mut luw_ctx_t);
822
880
This function resets the response buffer size and the number of response
823
881
headers back to 0.
824
882
883
+ ### uwr_http_set_response_status
884
+
885
+ ```Rust
886
+ pub fn uwr_http_set_response_status(status: luw_http_status_t);
887
+ ```
888
+
889
+ This function is used to set the HTTP response status. It takes one of the
890
+ [luw_http_status_t](#luw_http_status_t) enum values.
891
+
892
+ It should be called before any calls to *uwr_http_send_response()* or
893
+ *uwr_http_send_headers()*.
894
+
895
+ If you don't call this function the response status defaults to '200 OK'.
896
+
897
+ If you wish to error out with a '500 Internal Server Error', you don't need to
898
+ call this function. Simply returning _-1_ from the request_handler function
899
+ will indicate this error.
900
+
901
+ E.g
902
+
903
+ Send a '403 Forbidden'
904
+
905
+ ```Rust
906
+ /* ... */
907
+ uwr_http_set_response_status(LUW_HTTP_FORBIDDEN);
908
+ uwr_http_send_response(ctx); /* Doesn't require any body */
909
+ uwr_http_response_end();
910
+ /* ... */
911
+ return 0;
912
+ ```
913
+
914
+ Send a '307 Temporary Re-direct'
915
+
916
+ ```Rust
917
+ /* ... */
918
+ uwr_http_set_response_status(LUW_HTTP_TEMPORARY_REDIRECT);
919
+
920
+ uwr_http_init_headers(ctx, 1, 0);
921
+ uwr_http_add_header(ctx, "Location", "https://example.com/");
922
+ uwr_http_send_headers(ctx);
923
+ uwr_http_response_end();
924
+ /* ... */
925
+ return 0;
926
+ ```
927
+
928
+ _Version: 0.3.0_
929
+
825
930
### uwr_http_send_response
826
931
827
932
```Rust
0 commit comments