Add support for cacert entry option.

This commit is contained in:
jcamiel 2022-08-12 17:14:49 +02:00
parent 81c1233623
commit aae808b0b0
No known key found for this signature in database
GPG Key ID: 07FF11CFD55356CC
10 changed files with 199 additions and 23 deletions

View File

@ -1,3 +1,4 @@
curl 'https://localhost:8001/hello' --insecure curl 'https://localhost:8001/hello' --insecure
curl 'https://localhost:8001/hello' --insecure curl 'https://localhost:8001/hello' --insecure
curl 'https://localhost:8001/hello' --cacert ssl/cert.pem

View File

@ -13,4 +13,12 @@ insecure: false
insecure: true insecure: true
HTTP/* 200 HTTP/* 200
```Hello World!``` ```Hello World!```
GET https://localhost:8001/hello
[Options]
cacert: ssl/cert.pem # with a custom certificate
HTTP/* 200
```Hello World!```

View File

@ -0,0 +1,7 @@
error: Parsing filename
--> tests_error_parser/options_cacert.hurl:3:9
|
3 | cacert: # missing filename !
| ^ expecting a filename
|

View File

@ -0,0 +1 @@
2

View File

@ -0,0 +1,5 @@
GET http://localhost:8000
[Options]
cacert: # missing filename !
HTTP/* *

View File

@ -249,9 +249,16 @@ fn get_entry_options(
for section in &entry.request.sections { for section in &entry.request.sections {
if let SectionValue::Options(options) = &section.value { if let SectionValue::Options(options) = &section.value {
for option in options { for option in options {
let EntryOption::Insecure(insecure_option) = option; match option {
client_options.insecure = insecure_option.value; EntryOption::Insecure(option) => {
logger.debug(format!("insecure: {}", client_options.insecure).as_str()); client_options.insecure = option.value;
logger.debug(format!("insecure: {}", client_options.insecure).as_str());
}
EntryOption::CaCertificate(option) => {
client_options.cacert_file = Some(option.filename.value.clone());
logger.debug(format!("cacert: {}", option.filename.value).as_str());
}
}
} }
} }
} }

View File

@ -668,6 +668,7 @@ pub struct Variable {
#[derive(Clone, Debug, PartialEq, Eq)] #[derive(Clone, Debug, PartialEq, Eq)]
pub enum EntryOption { pub enum EntryOption {
Insecure(InsecureOption), Insecure(InsecureOption),
CaCertificate(CaCertificateOption),
} }
#[derive(Clone, Debug, PartialEq, Eq)] #[derive(Clone, Debug, PartialEq, Eq)]
@ -679,3 +680,13 @@ pub struct InsecureOption {
pub value: bool, pub value: bool,
pub line_terminator0: LineTerminator, pub line_terminator0: LineTerminator,
} }
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct CaCertificateOption {
pub line_terminators: Vec<LineTerminator>,
pub space0: Whitespace,
pub space1: Whitespace,
pub space2: Whitespace,
pub filename: Filename,
pub line_terminator0: LineTerminator,
}

View File

@ -233,6 +233,7 @@ impl Htmlable for EntryOption {
fn to_html(&self) -> String { fn to_html(&self) -> String {
match self { match self {
EntryOption::Insecure(option) => option.to_html(), EntryOption::Insecure(option) => option.to_html(),
EntryOption::CaCertificate(option) => option.to_html(),
} }
} }
} }
@ -254,6 +255,23 @@ impl Htmlable for InsecureOption {
} }
} }
impl Htmlable for CaCertificateOption {
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\">cacert</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(self.filename.to_html().as_str());
buffer.push_str("</span>");
buffer.push_str(self.line_terminator0.to_html().as_str());
buffer
}
}
impl Htmlable for MultipartParam { impl Htmlable for MultipartParam {
fn to_html(&self) -> String { fn to_html(&self) -> String {
match self { match self {

View File

@ -368,8 +368,25 @@ fn option_insecure(reader: &mut Reader) -> ParseResult<'static, EntryOption> {
} }
fn option_cacert(reader: &mut Reader) -> ParseResult<'static, EntryOption> { fn option_cacert(reader: &mut Reader) -> ParseResult<'static, EntryOption> {
let line_terminators = optional_line_terminators(reader)?;
let space0 = zero_or_more_spaces(reader)?;
try_literal("cacert", reader)?; try_literal("cacert", reader)?;
unimplemented!() let space1 = zero_or_more_spaces(reader)?;
try_literal(":", reader)?;
let space2 = zero_or_more_spaces(reader)?;
let f = filename::parse(reader)?;
let line_terminator0 = line_terminator(reader)?;
let option = CaCertificateOption {
line_terminators,
space0,
space1,
space2,
filename: f,
line_terminator0,
};
Ok(EntryOption::CaCertificate(option))
} }
#[cfg(test)] #[cfg(test)]
@ -533,28 +550,28 @@ mod tests {
value: "".to_string(), value: "".to_string(),
source_info: SourceInfo { source_info: SourceInfo {
start: Pos { line: 1, column: 1 }, start: Pos { line: 1, column: 1 },
end: Pos { line: 1, column: 1 } end: Pos { line: 1, column: 1 },
} },
}, },
space1: Whitespace { space1: Whitespace {
value: "".to_string(), value: "".to_string(),
source_info: SourceInfo { source_info: SourceInfo {
start: Pos { line: 1, column: 9 }, start: Pos { line: 1, column: 9 },
end: Pos { line: 1, column: 9 } end: Pos { line: 1, column: 9 },
} },
}, },
space2: Whitespace { space2: Whitespace {
value: " ".to_string(), value: " ".to_string(),
source_info: SourceInfo { source_info: SourceInfo {
start: Pos { start: Pos {
line: 1, line: 1,
column: 10 column: 10,
}, },
end: Pos { end: Pos {
line: 1, line: 1,
column: 11 column: 11,
} },
} },
}, },
value: true, value: true,
line_terminator0: LineTerminator { line_terminator0: LineTerminator {
@ -563,13 +580,13 @@ mod tests {
source_info: SourceInfo { source_info: SourceInfo {
start: Pos { start: Pos {
line: 1, line: 1,
column: 15 column: 15,
}, },
end: Pos { end: Pos {
line: 1, line: 1,
column: 15 column: 15,
} },
} },
}, },
comment: None, comment: None,
newline: Whitespace { newline: Whitespace {
@ -577,15 +594,15 @@ mod tests {
source_info: SourceInfo { source_info: SourceInfo {
start: Pos { start: Pos {
line: 1, line: 1,
column: 15 column: 15,
}, },
end: Pos { end: Pos {
line: 1, line: 1,
column: 15 column: 15,
} },
} },
} },
} },
}) })
); );
} }
@ -597,6 +614,85 @@ mod tests {
assert_eq!(error.recoverable, false) assert_eq!(error.recoverable, false)
} }
#[test]
fn test_option_cacert() {
let mut reader = Reader::init("cacert: /home/foo/cert.pem");
let option = option_cacert(&mut reader).unwrap();
assert_eq!(
option,
EntryOption::CaCertificate(CaCertificateOption {
line_terminators: vec![],
space0: Whitespace {
value: "".to_string(),
source_info: SourceInfo {
start: Pos { line: 1, column: 1 },
end: Pos { line: 1, column: 1 },
},
},
space1: Whitespace {
value: "".to_string(),
source_info: SourceInfo {
start: Pos { line: 1, column: 7 },
end: Pos { line: 1, column: 7 },
},
},
space2: Whitespace {
value: " ".to_string(),
source_info: SourceInfo {
start: Pos { line: 1, column: 8 },
end: Pos { line: 1, column: 9 },
},
},
filename: Filename {
value: "/home/foo/cert.pem".to_string(),
source_info: SourceInfo {
start: Pos { line: 1, column: 9 },
end: Pos {
line: 1,
column: 27
}
}
},
line_terminator0: LineTerminator {
space0: Whitespace {
value: "".to_string(),
source_info: SourceInfo {
start: Pos {
line: 1,
column: 27,
},
end: Pos {
line: 1,
column: 27,
},
},
},
comment: None,
newline: Whitespace {
value: "".to_string(),
source_info: SourceInfo {
start: Pos {
line: 1,
column: 27,
},
end: Pos {
line: 1,
column: 27,
},
},
},
},
})
);
}
#[test]
fn test_option_cacert_error() {
let mut reader = Reader::init("cacert: ###");
let error = option_cacert(&mut reader).err().unwrap();
assert_eq!(error.recoverable, false)
}
#[test] #[test]
fn test_cookie_error() { fn test_cookie_error() {
let mut reader = Reader::init("Foo: {{Bar"); let mut reader = Reader::init("Foo: {{Bar");

View File

@ -819,6 +819,7 @@ impl Tokenizable for EntryOption {
fn tokenize(&self) -> Vec<Token> { fn tokenize(&self) -> Vec<Token> {
match self { match self {
EntryOption::Insecure(option) => option.tokenize(), EntryOption::Insecure(option) => option.tokenize(),
EntryOption::CaCertificate(option) => option.tokenize(),
} }
} }
} }
@ -843,3 +844,24 @@ impl Tokenizable for InsecureOption {
tokens tokens
} }
} }
impl Tokenizable for CaCertificateOption {
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("cacert".to_string()));
tokens.append(&mut self.space1.tokenize());
tokens.push(Token::Colon(String::from(":")));
tokens.append(&mut self.space2.tokenize());
tokens.append(&mut self.filename.tokenize());
tokens.append(&mut self.line_terminator0.tokenize());
tokens
}
}