|
| 1 | +use super::*; |
| 2 | +use crate::errors::{check_status_extract_body_2, AzureError}; |
| 3 | +use crate::lease::LeaseId; |
| 4 | +use crate::util::HeaderMapExt; |
| 5 | +use crate::{Consistency, RequestId, SessionToken}; |
| 6 | + |
| 7 | +use chrono::{DateTime, Utc}; |
| 8 | +use http::status::StatusCode; |
| 9 | +use http::HeaderMap; |
| 10 | +use hyper::header::{HeaderName, DATE, ETAG, LAST_MODIFIED}; |
| 11 | +use hyper::{Body, Client, Request}; |
| 12 | +use uuid::Uuid; |
| 13 | + |
| 14 | +use std::convert::TryFrom; |
| 15 | + |
| 16 | +pub fn lease_id_from_headers(headers: &HeaderMap) -> Result<LeaseId, AzureError> { |
| 17 | + let lease_id = headers |
| 18 | + .get_as_str(LEASE_ID) |
| 19 | + .ok_or_else(|| AzureError::HeaderNotFound(LEASE_ID.to_owned()))?; |
| 20 | + Ok(Uuid::parse_str(lease_id)?) |
| 21 | +} |
| 22 | + |
| 23 | +pub fn request_id_from_headers(headers: &HeaderMap) -> Result<RequestId, AzureError> { |
| 24 | + let request_id = headers |
| 25 | + .get_as_str(REQUEST_ID) |
| 26 | + .ok_or_else(|| AzureError::HeaderNotFound(REQUEST_ID.to_owned()))?; |
| 27 | + Ok(Uuid::parse_str(request_id)?) |
| 28 | +} |
| 29 | + |
| 30 | +pub fn client_request_id_from_headers_optional(headers: &HeaderMap) -> Option<String> { |
| 31 | + headers.get_as_str(CLIENT_REQUEST_ID).map(|s| s.to_owned()) |
| 32 | +} |
| 33 | + |
| 34 | +pub fn content_md5_from_headers_optional( |
| 35 | + headers: &HeaderMap, |
| 36 | +) -> Result<Option<[u8; 16]>, AzureError> { |
| 37 | + if headers.contains_key(CONTENT_MD5) { |
| 38 | + Ok(Some(content_md5_from_headers(headers)?)) |
| 39 | + } else { |
| 40 | + Ok(None) |
| 41 | + } |
| 42 | +} |
| 43 | + |
| 44 | +#[derive(Debug, Clone)] |
| 45 | +pub struct CommonStorageResponseHeaders { |
| 46 | + pub request_id: RequestId, |
| 47 | + pub client_request_id: Option<String>, |
| 48 | + pub version: String, |
| 49 | + pub date: DateTime<Utc>, |
| 50 | + pub server: String, |
| 51 | +} |
| 52 | + |
| 53 | +impl TryFrom<&HeaderMap> for CommonStorageResponseHeaders { |
| 54 | + type Error = AzureError; |
| 55 | + |
| 56 | + fn try_from(headers: &HeaderMap) -> Result<Self, Self::Error> { |
| 57 | + Ok(Self { |
| 58 | + request_id: request_id_from_headers(headers)?, |
| 59 | + client_request_id: client_request_id_from_headers_optional(headers), |
| 60 | + version: version_from_headers(headers)?.to_owned(), |
| 61 | + date: date_from_headers(headers)?, |
| 62 | + server: server_from_headers(headers)?.to_owned(), |
| 63 | + }) |
| 64 | + } |
| 65 | +} |
| 66 | + |
| 67 | +pub fn content_md5_from_headers(headers: &HeaderMap) -> Result<[u8; 16], AzureError> { |
| 68 | + let content_md5 = headers |
| 69 | + .get(CONTENT_MD5) |
| 70 | + .ok_or_else(|| AzureError::HeaderNotFound(CONTENT_MD5.to_owned()))? |
| 71 | + .to_str()?; |
| 72 | + |
| 73 | + let content_md5_vec = base64::decode(&content_md5)?; |
| 74 | + |
| 75 | + if content_md5_vec.len() != 16 { |
| 76 | + return Err(AzureError::DigestNot16BytesLong( |
| 77 | + content_md5_vec.len() as u64 |
| 78 | + )); |
| 79 | + } |
| 80 | + let mut content_md5 = [0; 16]; |
| 81 | + content_md5.copy_from_slice(&content_md5_vec[0..16]); |
| 82 | + |
| 83 | + trace!("content_md5 == {:?}", content_md5); |
| 84 | + Ok(content_md5) |
| 85 | +} |
| 86 | + |
| 87 | +pub fn content_crc64_from_headers_optional( |
| 88 | + headers: &HeaderMap, |
| 89 | +) -> Result<Option<[u8; 8]>, AzureError> { |
| 90 | + if headers.contains_key(CONTENT_CRC64) { |
| 91 | + Ok(Some(content_crc64_from_headers(headers)?)) |
| 92 | + } else { |
| 93 | + Ok(None) |
| 94 | + } |
| 95 | +} |
| 96 | + |
| 97 | +pub fn content_crc64_from_headers(headers: &HeaderMap) -> Result<[u8; 8], AzureError> { |
| 98 | + let content_crc64 = headers |
| 99 | + .get(CONTENT_CRC64) |
| 100 | + .ok_or_else(|| AzureError::HeaderNotFound(CONTENT_CRC64.to_owned()))? |
| 101 | + .to_str()?; |
| 102 | + |
| 103 | + let content_crc64_vec = base64::decode(&content_crc64)?; |
| 104 | + |
| 105 | + if content_crc64_vec.len() != 8 { |
| 106 | + return Err(AzureError::CRC64Not8BytesLong( |
| 107 | + content_crc64_vec.len() as u64 |
| 108 | + )); |
| 109 | + } |
| 110 | + let mut content_crc64 = [0; 8]; |
| 111 | + content_crc64.copy_from_slice(&content_crc64_vec[0..8]); |
| 112 | + |
| 113 | + trace!("content_crc64 == {:?}", content_crc64); |
| 114 | + Ok(content_crc64) |
| 115 | +} |
| 116 | + |
| 117 | +pub fn consistency_from_headers(headers: &HeaderMap) -> Result<Consistency, AzureError> { |
| 118 | + if let Some(content_crc64) = content_crc64_from_headers_optional(headers)? { |
| 119 | + return Ok(Consistency::Crc64(content_crc64)); |
| 120 | + } else if let Some(content_md5) = content_md5_from_headers_optional(headers)? { |
| 121 | + return Ok(Consistency::Md5(content_md5)); |
| 122 | + } |
| 123 | + |
| 124 | + Err(AzureError::HeadersNotFound(vec![ |
| 125 | + CONTENT_CRC64.to_owned(), |
| 126 | + CONTENT_MD5.to_owned(), |
| 127 | + ])) |
| 128 | +} |
| 129 | + |
| 130 | +pub fn last_modified_from_headers_optional( |
| 131 | + headers: &HeaderMap, |
| 132 | +) -> Result<Option<DateTime<Utc>>, AzureError> { |
| 133 | + if headers.contains_key(LAST_MODIFIED) { |
| 134 | + Ok(Some(last_modified_from_headers(headers)?)) |
| 135 | + } else { |
| 136 | + Ok(None) |
| 137 | + } |
| 138 | +} |
| 139 | + |
| 140 | +pub fn last_modified_from_headers(headers: &HeaderMap) -> Result<DateTime<Utc>, AzureError> { |
| 141 | + let last_modified = headers |
| 142 | + .get(LAST_MODIFIED) |
| 143 | + .ok_or_else(|| { |
| 144 | + static LM: HeaderName = LAST_MODIFIED; |
| 145 | + AzureError::HeaderNotFound(LM.as_str().to_owned()) |
| 146 | + })? |
| 147 | + .to_str()?; |
| 148 | + let last_modified = DateTime::parse_from_rfc2822(last_modified)?; |
| 149 | + let last_modified = DateTime::from_utc(last_modified.naive_utc(), Utc); |
| 150 | + |
| 151 | + trace!("last_modified == {:?}", last_modified); |
| 152 | + Ok(last_modified) |
| 153 | +} |
| 154 | + |
| 155 | +pub fn continuation_token_from_headers_optional( |
| 156 | + headers: &HeaderMap, |
| 157 | +) -> Result<Option<String>, AzureError> { |
| 158 | + if let Some(hc) = headers.get(HEADER_CONTINUATION) { |
| 159 | + Ok(Some(hc.to_str()?.to_owned())) |
| 160 | + } else { |
| 161 | + Ok(None) |
| 162 | + } |
| 163 | +} |
| 164 | + |
| 165 | +#[inline] |
| 166 | +pub fn utc_date_from_rfc2822(date: &str) -> Result<DateTime<Utc>, AzureError> { |
| 167 | + let date = DateTime::parse_from_rfc2822(date)?; |
| 168 | + Ok(DateTime::from_utc(date.naive_utc(), Utc)) |
| 169 | +} |
| 170 | + |
| 171 | +pub fn date_from_headers(headers: &HeaderMap) -> Result<DateTime<Utc>, AzureError> { |
| 172 | + let date = headers |
| 173 | + .get(DATE) |
| 174 | + .ok_or_else(|| { |
| 175 | + static D: HeaderName = DATE; |
| 176 | + AzureError::HeaderNotFound(D.as_str().to_owned()) |
| 177 | + })? |
| 178 | + .to_str()?; |
| 179 | + let date = DateTime::parse_from_rfc2822(date)?; |
| 180 | + let date = DateTime::from_utc(date.naive_utc(), Utc); |
| 181 | + |
| 182 | + trace!("date == {:?}", date); |
| 183 | + Ok(date) |
| 184 | +} |
| 185 | + |
| 186 | +pub fn sku_name_from_headers(headers: &HeaderMap) -> Result<String, AzureError> { |
| 187 | + let sku_name = headers |
| 188 | + .get(SKU_NAME) |
| 189 | + .ok_or_else(|| AzureError::HeaderNotFound(SKU_NAME.to_owned()))? |
| 190 | + .to_str()?; |
| 191 | + trace!("sku_name == {:?}", sku_name); |
| 192 | + Ok(sku_name.to_owned()) |
| 193 | +} |
| 194 | + |
| 195 | +pub fn account_kind_from_headers(headers: &HeaderMap) -> Result<String, AzureError> { |
| 196 | + let account_kind = headers |
| 197 | + .get(ACCOUNT_KIND) |
| 198 | + .ok_or_else(|| AzureError::HeaderNotFound(ACCOUNT_KIND.to_owned()))? |
| 199 | + .to_str()?; |
| 200 | + trace!("account_kind == {:?}", account_kind); |
| 201 | + Ok(account_kind.to_owned()) |
| 202 | +} |
| 203 | + |
| 204 | +pub fn etag_from_headers_optional(headers: &HeaderMap) -> Result<Option<String>, AzureError> { |
| 205 | + if headers.contains_key(ETAG) { |
| 206 | + Ok(Some(etag_from_headers(headers)?)) |
| 207 | + } else { |
| 208 | + Ok(None) |
| 209 | + } |
| 210 | +} |
| 211 | + |
| 212 | +pub fn etag_from_headers(headers: &HeaderMap) -> Result<String, AzureError> { |
| 213 | + let etag = headers |
| 214 | + .get(ETAG) |
| 215 | + .ok_or_else(|| { |
| 216 | + static E: HeaderName = ETAG; |
| 217 | + AzureError::HeaderNotFound(E.as_str().to_owned()) |
| 218 | + })? |
| 219 | + .to_str()? |
| 220 | + .to_owned(); |
| 221 | + |
| 222 | + trace!("etag == {:?}", etag); |
| 223 | + Ok(etag) |
| 224 | +} |
| 225 | + |
| 226 | +pub fn lease_time_from_headers(headers: &HeaderMap) -> Result<u8, AzureError> { |
| 227 | + let lease_time = headers |
| 228 | + .get(LEASE_TIME) |
| 229 | + .ok_or_else(|| AzureError::HeaderNotFound(LEASE_TIME.to_owned()))? |
| 230 | + .to_str()?; |
| 231 | + |
| 232 | + let lease_time = lease_time.parse::<u8>()?; |
| 233 | + |
| 234 | + trace!("lease_time == {:?}", lease_time); |
| 235 | + Ok(lease_time) |
| 236 | +} |
| 237 | + |
| 238 | +pub fn delete_type_permanent_from_headers(headers: &HeaderMap) -> Result<bool, AzureError> { |
| 239 | + let delete_type_permanent = headers |
| 240 | + .get(DELETE_TYPE_PERMANENT) |
| 241 | + .ok_or_else(|| AzureError::HeaderNotFound(DELETE_TYPE_PERMANENT.to_owned()))? |
| 242 | + .to_str()?; |
| 243 | + |
| 244 | + let delete_type_permanent = delete_type_permanent.parse::<bool>()?; |
| 245 | + |
| 246 | + trace!("delete_type_permanent == {:?}", delete_type_permanent); |
| 247 | + Ok(delete_type_permanent) |
| 248 | +} |
| 249 | + |
| 250 | +pub fn sequence_number_from_headers(headers: &HeaderMap) -> Result<u64, AzureError> { |
| 251 | + let sequence_number = headers |
| 252 | + .get(BLOB_SEQUENCE_NUMBER) |
| 253 | + .ok_or_else(|| AzureError::HeaderNotFound(BLOB_SEQUENCE_NUMBER.to_owned()))? |
| 254 | + .to_str()?; |
| 255 | + |
| 256 | + let sequence_number = sequence_number.parse::<u64>()?; |
| 257 | + |
| 258 | + trace!("sequence_number == {:?}", sequence_number); |
| 259 | + Ok(sequence_number) |
| 260 | +} |
| 261 | + |
| 262 | +pub fn session_token_from_headers(headers: &HeaderMap) -> Result<SessionToken, AzureError> { |
| 263 | + Ok(headers |
| 264 | + .get(SESSION_TOKEN) |
| 265 | + .ok_or_else(|| AzureError::HeaderNotFound(SESSION_TOKEN.to_owned()))? |
| 266 | + .to_str()? |
| 267 | + .to_owned()) |
| 268 | +} |
| 269 | + |
| 270 | +pub fn server_from_headers(headers: &HeaderMap) -> Result<&str, AzureError> { |
| 271 | + Ok(headers |
| 272 | + .get(SERVER) |
| 273 | + .ok_or_else(|| AzureError::HeaderNotFound(SERVER.to_owned()))? |
| 274 | + .to_str()?) |
| 275 | +} |
| 276 | + |
| 277 | +pub fn version_from_headers(headers: &HeaderMap) -> Result<&str, AzureError> { |
| 278 | + Ok(headers |
| 279 | + .get(VERSION) |
| 280 | + .ok_or_else(|| AzureError::HeaderNotFound(VERSION.to_owned()))? |
| 281 | + .to_str()?) |
| 282 | +} |
| 283 | + |
| 284 | +pub fn request_server_encrypted_from_headers(headers: &HeaderMap) -> Result<bool, AzureError> { |
| 285 | + let request_server_encrypted = headers |
| 286 | + .get(REQUEST_SERVER_ENCRYPTED) |
| 287 | + .ok_or_else(|| AzureError::HeaderNotFound(REQUEST_SERVER_ENCRYPTED.to_owned()))? |
| 288 | + .to_str()?; |
| 289 | + |
| 290 | + let request_server_encrypted = request_server_encrypted.parse::<bool>()?; |
| 291 | + |
| 292 | + trace!("request_server_encrypted == {:?}", request_server_encrypted); |
| 293 | + Ok(request_server_encrypted) |
| 294 | +} |
| 295 | + |
| 296 | +pub async fn perform_http_request( |
| 297 | + client: &Client<hyper_rustls::HttpsConnector<hyper::client::HttpConnector>>, |
| 298 | + req: Request<Body>, |
| 299 | + expected_status: StatusCode, |
| 300 | +) -> Result<String, AzureError> { |
| 301 | + debug!("req == {:?}", req); |
| 302 | + let res = client.request(req).await?; |
| 303 | + check_status_extract_body_2(res, expected_status).await |
| 304 | +} |
0 commit comments