-
Notifications
You must be signed in to change notification settings - Fork 522
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move method between Request / Response.
This is a preliminary work for issue #628.
- Loading branch information
Showing
9 changed files
with
277 additions
and
66 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
/* | ||
* Hurl (https://hurl.dev) | ||
* Copyright (C) 2022 Orange | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
* | ||
*/ | ||
use crate::http::Header; | ||
|
||
/// Debug log HTTP request headers. | ||
pub fn log_headers_out(headers: &[Header]) { | ||
for header in headers { | ||
eprintln!("> {}", header); | ||
} | ||
} | ||
|
||
/// Debug log HTTP response headers. | ||
pub fn log_headers_in(headers: &[Header]) { | ||
for header in headers { | ||
eprintln!("< {}", header); | ||
} | ||
} | ||
|
||
/// Debug log text. | ||
pub fn log_text(text: &str) { | ||
text.split('\n').for_each(|l| eprintln!("* {}", l)) | ||
} | ||
|
||
/// Debug log bytes with a maximum size. | ||
/// | ||
/// # Arguments | ||
/// | ||
/// * `bytes`- the bytes to log | ||
/// * `max` - The maximum number if bytes to log | ||
pub fn log_bytes(bytes: &[u8], max: usize) { | ||
let bytes = if bytes.len() > max { | ||
&bytes[..max] | ||
} else { | ||
bytes | ||
}; | ||
eprintln!("* Bytes <{}...>", hex::encode(bytes)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
/* | ||
* Hurl (https://hurl.dev) | ||
* Copyright (C) 2022 Orange | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
* | ||
*/ | ||
|
||
/// Returns true if binary data with this `content_type` can be decoded as text. | ||
pub fn is_kind_of_text(content_type: &str) -> bool { | ||
content_type.contains("text/") | ||
|| content_type.contains("application/json") | ||
|| content_type.contains("application/xml") | ||
|| content_type.contains("application/x-www-form-urlencoded") | ||
} | ||
|
||
/// Returns true if this `content_type` is HTML. | ||
pub fn is_html(content_type: &str) -> bool { | ||
content_type.starts_with("text/html") | ||
} | ||
|
||
/// Extracts charset from mime-type String | ||
pub fn charset(mime_type: &str) -> Option<String> { | ||
mime_type | ||
.find("charset=") | ||
.map(|index| mime_type[(index + 8)..].to_string()) | ||
} | ||
|
||
#[cfg(test)] | ||
pub mod tests { | ||
use super::*; | ||
|
||
#[test] | ||
pub fn test_charset() { | ||
assert_eq!( | ||
charset("text/plain; charset=utf-8"), | ||
Some("utf-8".to_string()) | ||
); | ||
assert_eq!( | ||
charset("text/plain; charset=ISO-8859-1"), | ||
Some("ISO-8859-1".to_string()) | ||
); | ||
assert_eq!(charset("text/plain;"), None); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
/* | ||
* Hurl (https://hurl.dev) | ||
* Copyright (C) 2022 Orange | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
* | ||
*/ | ||
|
||
use crate::http::{debug, mimetype, Request}; | ||
|
||
impl Request { | ||
/// Log request headers. | ||
pub fn log_headers(&self) { | ||
debug::log_headers_out(&self.headers) | ||
} | ||
|
||
/// Log request body. | ||
pub fn log_body(&self) { | ||
debug::log_text("Request:"); | ||
|
||
// We try to decode the HTTP body as text if the response has a text kind content type. | ||
// If it ok, we print each line of the body in debug format. Otherwise, we | ||
// print the body first 64 bytes. | ||
if let Some(content_type) = self.content_type() { | ||
if !mimetype::is_kind_of_text(&content_type) { | ||
debug::log_bytes(&self.body, 64); | ||
return; | ||
} | ||
} | ||
match self.text() { | ||
Ok(text) => debug::log_text(&text), | ||
Err(_) => debug::log_bytes(&self.body, 64), | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
/* | ||
* Hurl (https://hurl.dev) | ||
* Copyright (C) 2022 Orange | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
* | ||
*/ | ||
|
||
use crate::http::{mimetype, HttpError, Request}; | ||
use encoding::{DecoderTrap, EncodingRef}; | ||
|
||
impl Request { | ||
/// Returns character encoding of the HTTP request. | ||
fn character_encoding(&self) -> Result<EncodingRef, HttpError> { | ||
match self.content_type() { | ||
Some(content_type) => match mimetype::charset(&content_type) { | ||
Some(charset) => { | ||
match encoding::label::encoding_from_whatwg_label(charset.as_str()) { | ||
None => Err(HttpError::InvalidCharset { charset }), | ||
Some(enc) => Ok(enc), | ||
} | ||
} | ||
None => Ok(encoding::all::UTF_8), | ||
}, | ||
None => Ok(encoding::all::UTF_8), | ||
} | ||
} | ||
|
||
/// Returns response body as text. | ||
pub fn text(&self) -> Result<String, HttpError> { | ||
let encoding = self.character_encoding()?; | ||
match encoding.decode(&self.body, DecoderTrap::Strict) { | ||
Ok(s) => Ok(s), | ||
Err(_) => Err(HttpError::InvalidDecoding { | ||
charset: encoding.name().to_string(), | ||
}), | ||
} | ||
} | ||
} |
Oops, something went wrong.