Skip to content

Commit 58806e4

Browse files
authored
header name constants changed from &str to HeaderName (#839)
1 parent 2ac449c commit 58806e4

File tree

96 files changed

+637
-667
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

96 files changed

+637
-667
lines changed

sdk/core/src/constants.rs

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,20 @@ pub mod resource_manager_endpoint {
1717
///
1818
/// <https://developer.mozilla.org/docs/Web/HTTP/Headers/Content-Type>
1919
pub mod content_type {
20+
use crate::headers::HeaderValue;
21+
2022
// Form content types
2123
// https://www.w3.org/TR/html401/interact/forms.html#h-17.13.4
2224

23-
pub const MULTIPART_FORM_DATA: &str = "multipart/form-data";
24-
pub const APPLICATION_X_WWW_FORM_URLENCODED: &str = "application/x-www-form-urlencoded";
25+
pub const MULTIPART_FORM_DATA: HeaderValue = HeaderValue::from_static("multipart/form-data");
26+
pub const APPLICATION_X_WWW_FORM_URLENCODED: HeaderValue =
27+
HeaderValue::from_static("application/x-www-form-urlencoded");
2528

26-
pub const APPLICATION_XML: &str = "application/xml";
27-
pub const APPLICATION_JSON: &str = "application/json";
28-
pub const APPLICATION_OCTET_STREAM: &str = "application/octet-stream";
29+
pub const APPLICATION_XML: HeaderValue = HeaderValue::from_static("application/xml");
30+
pub const APPLICATION_JSON: HeaderValue = HeaderValue::from_static("application/json");
31+
pub const APPLICATION_OCTET_STREAM: HeaderValue =
32+
HeaderValue::from_static("application/octet-stream");
33+
pub const TEXT_PLAIN: HeaderValue = HeaderValue::from_static("text/plain");
2934
}
3035

3136
/// Constants related to the Content-Type header

sdk/core/src/error/http_error.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::Response;
1+
use crate::{headers, Response};
22
use bytes::Bytes;
33
use std::collections::HashMap;
44

@@ -72,7 +72,7 @@ impl std::error::Error for HttpError {}
7272
///
7373
/// For more info, see [here](https://github.com/microsoft/api-guidelines/blob/vNext/azure/Guidelines.md#handling-errors)
7474
fn get_error_code_from_header(response: &Response) -> Option<String> {
75-
response.headers().get_as_string("x-ms-error-code")
75+
response.headers().get_as_string(&headers::ERROR_CODE)
7676
}
7777

7878
/// Gets the error code if it's present in the body

sdk/core/src/error/hyperium_http.rs

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
11
use super::{Error, ErrorKind};
2-
3-
use http::header::{InvalidHeaderName, InvalidHeaderValue};
42
use http::method::InvalidMethod;
53
use http::status::InvalidStatusCode;
64
use http::uri::{InvalidUri, InvalidUriParts};
@@ -11,18 +9,6 @@ impl From<http::Error> for super::Error {
119
}
1210
}
1311

14-
impl From<InvalidHeaderName> for super::Error {
15-
fn from(err: InvalidHeaderName) -> super::Error {
16-
Error::new(ErrorKind::DataConversion, err)
17-
}
18-
}
19-
20-
impl From<InvalidHeaderValue> for super::Error {
21-
fn from(err: InvalidHeaderValue) -> super::Error {
22-
Error::new(ErrorKind::DataConversion, err)
23-
}
24-
}
25-
2612
impl From<InvalidMethod> for super::Error {
2713
fn from(err: InvalidMethod) -> super::Error {
2814
Error::new(ErrorKind::DataConversion, err)

sdk/core/src/headers/mod.rs

Lines changed: 119 additions & 131 deletions
Large diffs are not rendered by default.

sdk/core/src/headers/utilities.rs

Lines changed: 30 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -2,31 +2,29 @@ use super::*;
22
use crate::error::{Error, ErrorKind};
33
use crate::request_options::LeaseId;
44
use crate::{RequestId, SessionToken};
5-
65
use chrono::{DateTime, FixedOffset, Utc};
7-
use http::header::{DATE, ETAG, LAST_MODIFIED, SERVER};
86
use std::str::FromStr;
97

108
pub fn get_option_str_from_headers<'a>(
119
headers: &'a Headers,
12-
key: &str,
10+
key: &HeaderName,
1311
) -> crate::Result<Option<&'a str>> {
14-
let h = match headers.get(key.to_owned()) {
12+
let h = match headers.get(key) {
1513
Some(h) => h,
1614
None => return Ok(None),
1715
};
1816
Ok(Some(h.as_str()))
1917
}
2018

21-
pub fn get_str_from_headers<'a>(headers: &'a Headers, key: &str) -> crate::Result<&'a str> {
19+
pub fn get_str_from_headers<'a>(headers: &'a Headers, key: &HeaderName) -> crate::Result<&'a str> {
2220
get_option_str_from_headers(headers, key)?.ok_or_else(|| {
2321
Error::with_message(ErrorKind::DataConversion, || {
24-
format!("could not find '{key}' in headers")
22+
format!("could not find '{key:?}' in headers")
2523
})
2624
})
2725
}
2826

29-
pub fn get_option_from_headers<T>(headers: &Headers, key: &str) -> crate::Result<Option<T>>
27+
pub fn get_option_from_headers<T>(headers: &Headers, key: &HeaderName) -> crate::Result<Option<T>>
3028
where
3129
T: std::str::FromStr + 'static,
3230
T::Err: std::error::Error + Send + Sync,
@@ -41,15 +39,15 @@ where
4139
ErrorKind::DataConversion,
4240
e,
4341
format!(
44-
"failed to parse header '{}' as {:?}",
42+
"failed to parse header '{:?}' as {:?}",
4543
key,
4644
std::any::TypeId::of::<T>()
4745
),
4846
)
4947
})?))
5048
}
5149

52-
pub fn get_from_headers<T>(headers: &Headers, key: &str) -> crate::Result<T>
50+
pub fn get_from_headers<T>(headers: &Headers, key: &HeaderName) -> crate::Result<T>
5351
where
5452
T: std::str::FromStr + 'static,
5553
T::Err: std::error::Error + Send + Sync,
@@ -59,7 +57,7 @@ where
5957
ErrorKind::DataConversion,
6058
e,
6159
format!(
62-
"failed to parse header '{}' as {:?}",
60+
"failed to parse header '{:?}' as {:?}",
6361
key,
6462
std::any::TypeId::of::<T>()
6563
),
@@ -104,36 +102,36 @@ where
104102
}
105103

106104
pub fn lease_id_from_headers(headers: &Headers) -> crate::Result<LeaseId> {
107-
get_from_headers(headers, LEASE_ID)
105+
get_from_headers(headers, &LEASE_ID)
108106
}
109107

110108
pub fn request_id_from_headers(headers: &Headers) -> crate::Result<RequestId> {
111-
get_from_headers(headers, REQUEST_ID)
109+
get_from_headers(headers, &REQUEST_ID)
112110
}
113111

114112
pub fn client_request_id_from_headers_optional(headers: &Headers) -> Option<String> {
115-
get_option_from_headers(headers, CLIENT_REQUEST_ID)
113+
get_option_from_headers(headers, &CLIENT_REQUEST_ID)
116114
.ok()
117115
.flatten()
118116
}
119117

120118
pub fn last_modified_from_headers_optional(
121119
headers: &Headers,
122120
) -> crate::Result<Option<DateTime<Utc>>> {
123-
get_option_from_headers(headers, LAST_MODIFIED.as_str())
121+
get_option_from_headers(headers, &LAST_MODIFIED)
124122
}
125123

126124
pub fn date_from_headers(headers: &Headers) -> crate::Result<DateTime<Utc>> {
127-
rfc2822_from_headers_mandatory(headers, DATE.as_str())
125+
rfc2822_from_headers_mandatory(headers, &DATE)
128126
}
129127

130128
pub fn last_modified_from_headers(headers: &Headers) -> crate::Result<DateTime<Utc>> {
131-
rfc2822_from_headers_mandatory(headers, LAST_MODIFIED.as_str())
129+
rfc2822_from_headers_mandatory(headers, &LAST_MODIFIED)
132130
}
133131

134132
pub fn rfc2822_from_headers_mandatory(
135133
headers: &Headers,
136-
header_name: &str,
134+
header_name: &HeaderName,
137135
) -> crate::Result<DateTime<Utc>> {
138136
let date = get_str_from_headers(headers, header_name)?;
139137
utc_date_from_rfc2822(date)
@@ -147,63 +145,63 @@ pub fn utc_date_from_rfc2822(date: &str) -> crate::Result<DateTime<Utc>> {
147145
pub fn continuation_token_from_headers_optional(
148146
headers: &Headers,
149147
) -> crate::Result<Option<String>> {
150-
Ok(get_option_str_from_headers(headers, CONTINUATION)?.map(String::from))
148+
Ok(get_option_str_from_headers(headers, &CONTINUATION)?.map(String::from))
151149
}
152150

153151
pub fn sku_name_from_headers(headers: &Headers) -> crate::Result<String> {
154-
Ok(get_str_from_headers(headers, SKU_NAME)?.to_owned())
152+
Ok(get_str_from_headers(headers, &SKU_NAME)?.to_owned())
155153
}
156154

157155
pub fn account_kind_from_headers(headers: &Headers) -> crate::Result<String> {
158-
Ok(get_str_from_headers(headers, ACCOUNT_KIND)?.to_owned())
156+
Ok(get_str_from_headers(headers, &ACCOUNT_KIND)?.to_owned())
159157
}
160158

161159
pub fn etag_from_headers_optional(headers: &Headers) -> crate::Result<Option<String>> {
162-
Ok(get_option_str_from_headers(headers, ETAG.as_str())?.map(String::from))
160+
Ok(get_option_str_from_headers(headers, &ETAG)?.map(String::from))
163161
}
164162

165163
pub fn etag_from_headers(headers: &Headers) -> crate::Result<String> {
166-
Ok(get_str_from_headers(headers, ETAG.as_str())?.to_owned())
164+
Ok(get_str_from_headers(headers, &ETAG)?.to_owned())
167165
}
168166

169167
pub fn lease_time_from_headers(headers: &Headers) -> crate::Result<u8> {
170-
get_from_headers(headers, LEASE_TIME)
168+
get_from_headers(headers, &LEASE_TIME)
171169
}
172170

173171
#[cfg(not(feature = "azurite_workaround"))]
174172
pub fn delete_type_permanent_from_headers(headers: &Headers) -> crate::Result<bool> {
175-
get_from_headers(headers, DELETE_TYPE_PERMANENT)
173+
get_from_headers(headers, &DELETE_TYPE_PERMANENT)
176174
}
177175

178176
#[cfg(feature = "azurite_workaround")]
179177
pub fn delete_type_permanent_from_headers(headers: &Headers) -> crate::Result<Option<bool>> {
180-
get_option_from_headers(headers, DELETE_TYPE_PERMANENT)
178+
get_option_from_headers(headers, &DELETE_TYPE_PERMANENT)
181179
}
182180

183181
pub fn sequence_number_from_headers(headers: &Headers) -> crate::Result<u64> {
184-
get_from_headers(headers, BLOB_SEQUENCE_NUMBER)
182+
get_from_headers(headers, &BLOB_SEQUENCE_NUMBER)
185183
}
186184

187185
pub fn session_token_from_headers(headers: &Headers) -> crate::Result<SessionToken> {
188-
get_str_from_headers(headers, SESSION_TOKEN).map(ToOwned::to_owned)
186+
get_str_from_headers(headers, &SESSION_TOKEN).map(ToOwned::to_owned)
189187
}
190188

191189
pub fn server_from_headers(headers: &Headers) -> crate::Result<&str> {
192-
get_str_from_headers(headers, SERVER.as_str())
190+
get_str_from_headers(headers, &SERVER)
193191
}
194192

195193
pub fn version_from_headers(headers: &Headers) -> crate::Result<&str> {
196-
get_str_from_headers(headers, VERSION)
194+
get_str_from_headers(headers, &VERSION)
197195
}
198196

199197
pub fn request_server_encrypted_from_headers(headers: &Headers) -> crate::Result<bool> {
200-
get_from_headers(headers, REQUEST_SERVER_ENCRYPTED)
198+
get_from_headers(headers, &REQUEST_SERVER_ENCRYPTED)
201199
}
202200

203201
pub fn content_type_from_headers(headers: &Headers) -> crate::Result<&str> {
204-
get_str_from_headers(headers, http::header::CONTENT_TYPE.as_str())
202+
get_str_from_headers(headers, &CONTENT_TYPE)
205203
}
206204

207205
pub fn item_count_from_headers(headers: &Headers) -> crate::Result<u32> {
208-
get_from_headers(headers, ITEM_COUNT)
206+
get_from_headers(headers, &ITEM_COUNT)
209207
}

sdk/core/src/http_client.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ impl HttpClient for reqwest::Client {
5353
let url = request.url().clone();
5454
let mut reqwest_request = self.request(request.method().clone(), url);
5555
for (name, value) in request.headers().iter() {
56-
reqwest_request = reqwest_request.header(name, value);
56+
reqwest_request = reqwest_request.header(name.as_str(), value.as_str());
5757
}
5858

5959
let body = request.body().clone();

sdk/core/src/mock/mock_response.rs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
11
use bytes::Bytes;
2-
use http::{header, StatusCode};
2+
use http::StatusCode;
33
use serde::{Deserialize, Serialize};
44
use std::collections::BTreeMap;
55

6-
use crate::{collect_pinned_stream, error, headers::Headers, BytesStream, Response};
6+
use crate::{
7+
collect_pinned_stream, error,
8+
headers::{HeaderName, HeaderValue, Headers},
9+
BytesStream, Response,
10+
};
711

812
#[derive(Debug, Clone, PartialEq)]
913
pub(crate) struct MockResponse {
@@ -63,8 +67,8 @@ impl<'de> Deserialize<'de> for MockResponse {
6367
let r = SerializedMockResponse::deserialize(deserializer)?;
6468
let mut headers = Headers::new();
6569
for (n, v) in r.headers.iter() {
66-
let name = header::HeaderName::from_lowercase(n.as_bytes()).map_err(Error::custom)?;
67-
let value = header::HeaderValue::from_str(&v).map_err(Error::custom)?;
70+
let name = HeaderName::from(n.to_owned());
71+
let value = HeaderValue::from(v.to_owned());
6872
headers.insert(name, value);
6973
}
7074
let body = Bytes::from(base64::decode(r.body).map_err(Error::custom)?);

sdk/core/src/mock/mock_transaction.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,7 @@ impl MockTransaction {
5757
"the transaction location '{}' does not exist",
5858
path.canonicalize().unwrap_or(path).display()
5959
)
60-
})
61-
.into());
60+
}));
6261
}
6362
}
6463

@@ -81,7 +80,7 @@ fn workspace_root() -> crate::Result<String> {
8180
)
8281
})?;
8382
let value = &output[index + key.len()..];
84-
let end = value.find("\"").ok_or_else(|| {
83+
let end = value.find('\"').ok_or_else(|| {
8584
Error::message(
8685
ErrorKind::MockFramework,
8786
"workspace_root value was malformed",

sdk/core/src/mock/player_policy.rs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,7 @@ impl Policy for MockTransportPlayerPolicy {
6565
// check if the passed request matches the one read from disk
6666
// We will ignore some headers that are bound to change every time
6767
// We'll probabily want to make the exclusion list dynamic at some point.
68-
const SKIPPED_HEADERS: &[&'static str] =
69-
&["Date", "x-ms-date", "authorization", "user-agent"];
68+
const SKIPPED_HEADERS: &[&str] = &["Date", "x-ms-date", "authorization", "user-agent"];
7069
let actual_headers = request
7170
.headers()
7271
.iter()
@@ -97,14 +96,14 @@ impl Policy for MockTransportPlayerPolicy {
9796
.iter()
9897
.find(|(h, _)| actual_header_key.as_str() == h.as_str())
9998
.ok_or_else(|| Error::with_message(ErrorKind::MockFramework, ||
100-
format!("received request have header {0} but it was not present in the read request",
99+
format!("received request have header '{0}' but it was not present in the read request",
101100
actual_header_key.as_str(),
102101
)))?;
103102

104103
if actual_header_value != expected_header_value {
105104
return Err(Error::with_message(ErrorKind::MockFramework, || {
106105
format!(
107-
"request header {0} value is different. Actual: {1}, Expected: {2}",
106+
"request header '{0}' value is different. Actual: {1}, Expected: {2}",
108107
actual_header_key.as_str().to_owned(),
109108
actual_header_value.as_str().to_owned(),
110109
expected_header_value.as_str().to_owned(),
@@ -124,12 +123,12 @@ impl Policy for MockTransportPlayerPolicy {
124123
}
125124

126125
let actual_body = match request.body() {
127-
crate::Body::Bytes(bytes) => &bytes as &[u8],
126+
crate::Body::Bytes(bytes) => bytes as &[u8],
128127
crate::Body::SeekableStream(_) => unimplemented!(),
129128
};
130129

131130
let expected_body = match expected_request.body() {
132-
crate::Body::Bytes(bytes) => &bytes as &[u8],
131+
crate::Body::Bytes(bytes) => bytes as &[u8],
133132
crate::Body::SeekableStream(_) => unimplemented!(),
134133
};
135134

sdk/core/src/policies/custom_headers_policy.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,7 @@ impl Policy for CustomHeadersPolicy {
3232
header_name,
3333
header_value
3434
);
35-
request
36-
.headers_mut()
37-
.insert(header_name.clone(), header_value.clone());
35+
request.insert_header(header_name.clone(), header_value.clone());
3836
});
3937
}
4038

0 commit comments

Comments
 (0)