Add max-redirs entry option.

This commit is contained in:
jcamiel 2022-08-16 11:08:56 +02:00
parent a2d3f87640
commit 2a002f81a6
No known key found for this signature in database
GPG Key ID: 07FF11CFD55356CC
10 changed files with 108 additions and 1 deletions

View File

@ -123,7 +123,8 @@ option:
| ca-certificate-option
| verbose-option
| very-verbose-option
| follow-redirect-option)
| follow-redirect-option
| max-redirs-option)
insecure-option: "insecure" ":" boolean lt

View File

@ -0,0 +1,7 @@
error: HTTP connection
--> tests_failed/option_max_redirect.hurl:1:5
|
1 | GET http://localhost:8000/redirect/7
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ too many redirect
|

View File

@ -0,0 +1,2 @@
3

View File

@ -0,0 +1,9 @@
<pre><code class="language-hurl"><span class="hurl-entry"><span class="request"><span class="line"><span class="method">GET</span> <span class="url">http://localhost:8000/redirect/7</span></span>
<span class="line section-header">[Options]</span>
<span class="line"><span class="string">location</span><span>:</span> <span class="boolean">true</span></span>
<span class="line"><span class="string">max-redirs</span><span>:</span> <span class="number">5</span></span>
</span><span class="response"><span class="line"></span>
<span class="line"><span class="version">HTTP/1.0</span> <span class="number">200</span></span>
</span></span><span class="line"></span>
<span class="line"></span>
</code></pre>

View File

@ -0,0 +1,8 @@
GET http://localhost:8000/redirect/7
[Options]
location: true
max-redirs: 5
HTTP/1.0 200

View File

@ -255,6 +255,10 @@ pub fn get_entry_options(
client_options.follow_location = option.value;
logger.debug(format!("location: {}", option.value).as_str());
}
EntryOption::MaxRedirect(option) => {
client_options.max_redirect = Some(option.value);
logger.debug(format!("max-redirs: {}", option.value).as_str());
}
EntryOption::Verbose(option) => {
client_options.verbosity = if option.value {
Some(Verbosity::Verbose)

View File

@ -670,6 +670,7 @@ pub enum EntryOption {
CaCertificate(CaCertificateOption),
Insecure(InsecureOption),
FollowLocation(FollowLocationOption),
MaxRedirect(MaxRedirectOption),
Verbose(VerboseOption),
VeryVerbose(VeryVerboseOption),
}
@ -723,3 +724,13 @@ pub struct FollowLocationOption {
pub value: bool,
pub line_terminator0: LineTerminator,
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct MaxRedirectOption {
pub line_terminators: Vec<LineTerminator>,
pub space0: Whitespace,
pub space1: Whitespace,
pub space2: Whitespace,
pub value: usize,
pub line_terminator0: LineTerminator,
}

View File

@ -235,6 +235,7 @@ impl Htmlable for EntryOption {
EntryOption::CaCertificate(option) => option.to_html(),
EntryOption::Insecure(option) => option.to_html(),
EntryOption::FollowLocation(option) => option.to_html(),
EntryOption::MaxRedirect(option) => option.to_html(),
EntryOption::Verbose(option) => option.to_html(),
EntryOption::VeryVerbose(option) => option.to_html(),
}
@ -292,6 +293,23 @@ impl Htmlable for FollowLocationOption {
}
}
impl Htmlable for MaxRedirectOption {
fn to_html(&self) -> String {
let mut buffer = String::from("");
add_line_terminators(&mut buffer, self.line_terminators.clone());
buffer.push_str("<span class=\"line\">");
buffer.push_str(self.space0.to_html().as_str());
buffer.push_str("<span class=\"string\">max-redirs</span>");
buffer.push_str(self.space1.to_html().as_str());
buffer.push_str("<span>:</span>");
buffer.push_str(self.space2.to_html().as_str());
buffer.push_str(format!("<span class=\"number\">{}</span>", self.value).as_str());
buffer.push_str("</span>");
buffer.push_str(self.line_terminator0.to_html().as_str());
buffer
}
}
impl Htmlable for VerboseOption {
fn to_html(&self) -> String {
let mut buffer = String::from("");

View File

@ -347,6 +347,7 @@ fn option(reader: &mut Reader) -> ParseResult<'static, EntryOption> {
option_cacert,
option_insecure,
option_follow_location,
option_max_redirect,
option_verbose,
option_very_verbose,
],
@ -420,6 +421,30 @@ fn option_follow_location(reader: &mut Reader) -> ParseResult<'static, EntryOpti
Ok(EntryOption::FollowLocation(option))
}
fn option_max_redirect(reader: &mut Reader) -> ParseResult<'static, EntryOption> {
let line_terminators = optional_line_terminators(reader)?;
let space0 = zero_or_more_spaces(reader)?;
try_literal("max-redirs", reader)?;
let space1 = zero_or_more_spaces(reader)?;
try_literal(":", reader)?;
let space2 = zero_or_more_spaces(reader)?;
let value = nonrecover(natural, reader)?;
let line_terminator0 = line_terminator(reader)?;
// FIXME: try to not unwrap redirect value
// and returns an error if not possible
let option = MaxRedirectOption {
line_terminators,
space0,
space1,
space2,
value: usize::try_from(value).unwrap(),
line_terminator0,
};
Ok(EntryOption::MaxRedirect(option))
}
fn option_verbose(reader: &mut Reader) -> ParseResult<'static, EntryOption> {
let line_terminators = optional_line_terminators(reader)?;
let space0 = zero_or_more_spaces(reader)?;

View File

@ -821,6 +821,7 @@ impl Tokenizable for EntryOption {
EntryOption::CaCertificate(option) => option.tokenize(),
EntryOption::Insecure(option) => option.tokenize(),
EntryOption::FollowLocation(option) => option.tokenize(),
EntryOption::MaxRedirect(option) => option.tokenize(),
EntryOption::Verbose(option) => option.tokenize(),
EntryOption::VeryVerbose(option) => option.tokenize(),
}
@ -890,6 +891,27 @@ impl Tokenizable for FollowLocationOption {
}
}
impl Tokenizable for MaxRedirectOption {
fn tokenize(&self) -> Vec<Token> {
let mut tokens: Vec<Token> = vec![];
tokens.append(
&mut self
.line_terminators
.iter()
.flat_map(|e| e.tokenize())
.collect(),
);
tokens.append(&mut self.space0.tokenize());
tokens.push(Token::String("max-redirs".to_string()));
tokens.append(&mut self.space1.tokenize());
tokens.push(Token::Colon(String::from(":")));
tokens.append(&mut self.space2.tokenize());
tokens.push(Token::Number(self.value.to_string()));
tokens.append(&mut self.line_terminator0.tokenize());
tokens
}
}
impl Tokenizable for VerboseOption {
fn tokenize(&self) -> Vec<Token> {
let mut tokens: Vec<Token> = vec![];