-
Notifications
You must be signed in to change notification settings - Fork 84
/
Copy pathaccess_control_request_method.rs
79 lines (71 loc) · 2.3 KB
/
access_control_request_method.rs
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
use crate::core::{Decodable, Encodable, Named};
use crate::{HeaderName, HeaderValue};
use http::Method;
/// `Access-Control-Request-Method` header, part of
/// [CORS](http://www.w3.org/TR/cors/#access-control-request-method-request-header)
///
/// The `Access-Control-Request-Method` header indicates which method will be
/// used in the actual request as part of the preflight request.
/// # ABNF
///
/// ```text
/// Access-Control-Request-Method: \"Access-Control-Request-Method\" \":\" Method
/// ```
///
/// # Example values
/// * `GET`
///
/// # Examples
///
/// ```
/// # extern crate headers;
/// extern crate http;
/// use headers::AccessControlRequestMethod;
/// use http::Method;
///
/// let req_method = AccessControlRequestMethod::from(Method::GET);
/// ```
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
pub struct AccessControlRequestMethod(Method);
impl Named for AccessControlRequestMethod {
fn name() -> &'static HeaderName {
&::http::header::ACCESS_CONTROL_REQUEST_METHOD
}
}
impl Decodable for AccessControlRequestMethod {
fn decode<'i, I: Iterator<Item = &'i HeaderValue>>(values: &mut I) -> Result<Self, ::Error> {
values
.next()
.and_then(|value| Method::from_bytes(value.as_bytes()).ok())
.map(AccessControlRequestMethod)
.ok_or_else(::Error::invalid)
}
}
impl Encodable for AccessControlRequestMethod {
fn encode<E: Extend<::HeaderValue>>(&self, values: &mut E) {
// For the more common methods, try to use a static string.
let s = match self.0 {
Method::GET => "GET",
Method::POST => "POST",
Method::PUT => "PUT",
Method::DELETE => "DELETE",
_ => {
let val = HeaderValue::from_str(self.0.as_ref())
.expect("Methods are also valid HeaderValues");
values.extend(::std::iter::once(val));
return;
}
};
values.extend(::std::iter::once(HeaderValue::from_static(s)));
}
}
impl From<Method> for AccessControlRequestMethod {
fn from(method: Method) -> AccessControlRequestMethod {
AccessControlRequestMethod(method)
}
}
impl From<AccessControlRequestMethod> for Method {
fn from(method: AccessControlRequestMethod) -> Method {
method.0
}
}