-
-
Notifications
You must be signed in to change notification settings - Fork 138
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add support for custom headers in input processing #1561
Open
mre
wants to merge
2
commits into
master
Choose a base branch
from
1441-always-send-headers
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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 |
---|---|---|
|
@@ -3,6 +3,7 @@ use crate::{utils, ErrorKind, Result}; | |
use async_stream::try_stream; | ||
use futures::stream::Stream; | ||
use glob::glob_with; | ||
use http::HeaderMap; | ||
use ignore::WalkBuilder; | ||
use reqwest::Url; | ||
use serde::{Deserialize, Serialize}; | ||
|
@@ -105,14 +106,16 @@ impl Display for InputSource { | |
} | ||
|
||
/// Lychee Input with optional file hint for parsing | ||
#[derive(Clone, Debug, PartialEq, Eq, Hash)] | ||
#[derive(Clone, Debug, PartialEq, Eq)] | ||
pub struct Input { | ||
/// Origin of input | ||
pub source: InputSource, | ||
/// Hint to indicate which extractor to use | ||
pub file_type_hint: Option<FileType>, | ||
/// Excluded paths that will be skipped when reading content | ||
pub excluded_paths: Option<Vec<PathBuf>>, | ||
/// Custom headers to be used when fetching remote URLs | ||
pub headers: reqwest::header::HeaderMap, | ||
} | ||
|
||
impl Input { | ||
|
@@ -129,6 +132,7 @@ impl Input { | |
file_type_hint: Option<FileType>, | ||
glob_ignore_case: bool, | ||
excluded_paths: Option<Vec<PathBuf>>, | ||
headers: reqwest::header::HeaderMap, | ||
) -> Result<Self> { | ||
let source = if value == STDIN { | ||
InputSource::Stdin | ||
|
@@ -194,9 +198,20 @@ impl Input { | |
source, | ||
file_type_hint, | ||
excluded_paths, | ||
headers, | ||
}) | ||
} | ||
|
||
/// Convenience constructor with sane defaults | ||
/// | ||
/// # Errors | ||
/// | ||
/// Returns an error if the input does not exist (i.e. invalid path) | ||
/// and the input cannot be parsed as a URL. | ||
pub fn from_value(value: &str) -> Result<Self> { | ||
Self::new(value, None, false, None, HeaderMap::new()) | ||
} | ||
|
||
/// Retrieve the contents from the input | ||
/// | ||
/// # Errors | ||
|
@@ -424,6 +439,8 @@ fn is_excluded_path(excluded_paths: &[PathBuf], path: &PathBuf) -> bool { | |
|
||
#[cfg(test)] | ||
mod tests { | ||
use http::HeaderMap; | ||
|
||
use super::*; | ||
|
||
#[test] | ||
|
@@ -434,14 +451,15 @@ mod tests { | |
assert!(path.exists()); | ||
assert!(path.is_relative()); | ||
|
||
let input = Input::new(test_file, None, false, None); | ||
let input = Input::new(test_file, None, false, None, HeaderMap::new()); | ||
assert!(input.is_ok()); | ||
assert!(matches!( | ||
input, | ||
Ok(Input { | ||
source: InputSource::FsPath(PathBuf { .. }), | ||
file_type_hint: None, | ||
excluded_paths: None | ||
excluded_paths: None, | ||
headers: _, | ||
}) | ||
)); | ||
} | ||
|
@@ -454,7 +472,7 @@ mod tests { | |
assert!(!path.exists()); | ||
assert!(path.is_relative()); | ||
|
||
let input = Input::new(test_file, None, false, None); | ||
let input = Input::from_value(test_file); | ||
assert!(input.is_err()); | ||
assert!(matches!(input, Err(ErrorKind::InvalidFile(PathBuf { .. })))); | ||
} | ||
|
@@ -497,7 +515,7 @@ mod tests { | |
|
||
#[test] | ||
fn test_url_without_scheme() { | ||
let input = Input::new("example.com", None, false, None); | ||
let input = Input::from_value("example.com"); | ||
assert_eq!( | ||
input.unwrap().source.to_string(), | ||
String::from("http://example.com/") | ||
|
@@ -508,7 +526,7 @@ mod tests { | |
#[cfg(windows)] | ||
#[test] | ||
fn test_windows_style_filepath_not_existing() { | ||
let input = Input::new("C:\\example\\project\\here", None, false, None); | ||
let input = Input::from_value("C:\\example\\project\\here"); | ||
assert!(input.is_err()); | ||
let input = input.unwrap_err(); | ||
|
||
|
@@ -528,7 +546,7 @@ mod tests { | |
let dir = temp_dir(); | ||
let file = NamedTempFile::new_in(dir).unwrap(); | ||
let path = file.path(); | ||
let input = Input::new(path.to_str().unwrap(), None, false, None).unwrap(); | ||
let input = Input::from_value(path.to_str().unwrap()).unwrap(); | ||
|
||
match input.source { | ||
InputSource::FsPath(_) => (), | ||
|
@@ -540,33 +558,28 @@ mod tests { | |
fn test_url_scheme_check_succeeding() { | ||
// Valid http and https URLs | ||
assert!(matches!( | ||
Input::new("http://example.com", None, false, None), | ||
Input::from_value("http://example.com"), | ||
Ok(Input { | ||
source: InputSource::RemoteUrl(_), | ||
.. | ||
}) | ||
)); | ||
assert!(matches!( | ||
Input::new("https://example.com", None, false, None), | ||
Input::from_value("https://example.com"), | ||
Ok(Input { | ||
source: InputSource::RemoteUrl(_), | ||
.. | ||
}) | ||
)); | ||
assert!(matches!( | ||
Input::new( | ||
"http://subdomain.example.com/path?query=value", | ||
None, | ||
false, | ||
None | ||
), | ||
Input::from_value("http://subdomain.example.com/path?query=value",), | ||
Ok(Input { | ||
source: InputSource::RemoteUrl(_), | ||
.. | ||
}) | ||
)); | ||
assert!(matches!( | ||
Input::new("https://example.com:8080", None, false, None), | ||
Input::from_value("https://example.com:8080"), | ||
Ok(Input { | ||
source: InputSource::RemoteUrl(_), | ||
.. | ||
|
@@ -578,19 +591,19 @@ mod tests { | |
fn test_url_scheme_check_failing() { | ||
// Invalid schemes | ||
assert!(matches!( | ||
Input::new("ftp://example.com", None, false, None), | ||
Input::from_value("ftp://example.com"), | ||
Err(ErrorKind::InvalidFile(_)) | ||
)); | ||
assert!(matches!( | ||
Input::new("httpx://example.com", None, false, None), | ||
Input::from_value("httpx://example.com"), | ||
Err(ErrorKind::InvalidFile(_)) | ||
)); | ||
assert!(matches!( | ||
Input::new("file:///path/to/file", None, false, None), | ||
Input::from_value("file:///path/to/file"), | ||
Err(ErrorKind::InvalidFile(_)) | ||
)); | ||
assert!(matches!( | ||
Input::new("mailto:[email protected]", None, false, None), | ||
Input::from_value("mailto:[email protected]"), | ||
Err(ErrorKind::InvalidFile(_)) | ||
)); | ||
} | ||
|
@@ -599,19 +612,19 @@ mod tests { | |
fn test_non_url_inputs() { | ||
// Non-URL inputs | ||
assert!(matches!( | ||
Input::new("./local/path", None, false, None), | ||
Input::from_value("./local/path"), | ||
Err(ErrorKind::InvalidFile(_)) | ||
)); | ||
assert!(matches!( | ||
Input::new("*.md", None, false, None), | ||
Input::from_value("*.md"), | ||
Ok(Input { | ||
source: InputSource::FsGlob { .. }, | ||
.. | ||
}) | ||
)); | ||
// Assuming the current directory exists | ||
assert!(matches!( | ||
Input::new(".", None, false, None), | ||
Input::from_value("."), | ||
Ok(Input { | ||
source: InputSource::FsPath(_), | ||
.. | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is not ideal because we parse the headers twice: here and in
lychee-bin/src/client
. We should probably move the parsing to the Clap config.