1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
// Copyright 2020 Oxide Computer Company
//! General-purpose HTTP-related facilities
use Bytes;
use BodyExt;
use Body as HttpBody;
use DeserializeOwned;
use HttpError;
use cratefrom_map;
use crateVariableSet;
/// header name for conveying request ids ("x-request-id")
pub const HEADER_REQUEST_ID: &str = "x-request-id";
/// MIME type for raw bytes
pub const CONTENT_TYPE_OCTET_STREAM: &str = "application/octet-stream";
/// MIME type for plain JSON data
pub const CONTENT_TYPE_JSON: &str = "application/json";
/// MIME type for newline-delimited JSON data
pub const CONTENT_TYPE_NDJSON: &str = "application/x-ndjson";
/// MIME type for form/urlencoded data
pub const CONTENT_TYPE_URL_ENCODED: &str = "application/x-www-form-urlencoded";
/// MIME type for multipart/form-data
pub const CONTENT_TYPE_MULTIPART_FORM_DATA: &str = "multipart/form-data";
/// Reads the rest of the body from the request, dropping all the bytes. This is
/// useful after encountering error conditions.
pub async
/// Given a set of variables (most immediately from a RequestContext, likely
/// generated by the HttpRouter when routing an incoming request), extract them
/// into an instance of type T. This is a convenience function that reports an
/// appropriate error when the extraction fails.
///
/// Note that if this function fails, either there was a type error (e.g., a path
/// parameter was supposed to be a UUID, but wasn't), in which case we should
/// report a 400-level error; or the caller attempted to extract a parameter
/// (using a field in T) that wasn't populated in `path_params`. This latter
/// case is a programmer error -- this invocation can never work with this type
/// for this HTTP handler. Ideally, we'd catch this at build time, but we don't
/// currently do that. However, we _do_ currently catch this at server startup
/// time, so this case should be impossible.
///
/// TODO-cleanup: It would be better to fail to build when the struct's
/// parameters don't match up precisely with the path parameters
/// TODO-cleanup It would also be nice to know if the struct could not possibly
/// be correctly constructed from path parameters because the struct contains
/// values that could not be represented in path parameters (e.g., nested
/// structs). One approach to doing this would be to skip serde altogether here
/// for `T` and instead define our own trait. We could define a "derive" macro
/// that would do something similar to serde, but only allows field values that
/// implement FromStr. Then we'd at least know at build time that the consumer
/// gave us a type that could conceivably be represented by the path parameters.
/// TODO-testing: Add automated tests.