Skip to content
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

support http2-prior-knowledge #356

Merged
merged 1 commit into from
Mar 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion completions/_xh
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ none\:"Disable both coloring and formatting"))' \
'--cert-key=[A private key file to use with --cert]:FILE:_files' \
'--ssl=[Force a particular TLS version]:VERSION:(auto tls1 tls1.1 tls1.2 tls1.3)' \
'--default-scheme=[The default scheme to use if not specified in the URL]:SCHEME: ' \
'--http-version=[HTTP version to use]:VERSION:(1.0 1.1 2)' \
'--http-version=[HTTP version to use]:VERSION:(1.0 1.1 2 2-prior-knowledge)' \
'*--resolve=[Override DNS resolution for specific domain to a custom IP]:HOST:ADDRESS: ' \
'--interface=[Bind to a network interface or local IP address]:NAME: ' \
'-j[(default) Serialize data items from the command line as a JSON object]' \
Expand Down
2 changes: 1 addition & 1 deletion completions/xh.bash
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ _xh() {
return 0
;;
--http-version)
COMPREPLY=($(compgen -W "1.0 1.1 2" -- "${cur}"))
COMPREPLY=($(compgen -W "1.0 1.1 2 2-prior-knowledge" -- "${cur}"))
return 0
;;
--resolve)
Expand Down
2 changes: 1 addition & 1 deletion completions/xh.fish
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ complete -c xh -l cert -d 'Use a client side certificate for SSL' -r -F
complete -c xh -l cert-key -d 'A private key file to use with --cert' -r -F
complete -c xh -l ssl -d 'Force a particular TLS version' -r -f -a "{auto '',tls1 '',tls1.1 '',tls1.2 '',tls1.3 ''}"
complete -c xh -l default-scheme -d 'The default scheme to use if not specified in the URL' -r
complete -c xh -l http-version -d 'HTTP version to use' -r -f -a "{1.0 '',1.1 '',2 ''}"
complete -c xh -l http-version -d 'HTTP version to use' -r -f -a "{1.0 '',1.1 '',2 '',2-prior-knowledge ''}"
complete -c xh -l resolve -d 'Override DNS resolution for specific domain to a custom IP' -r
complete -c xh -l interface -d 'Bind to a network interface or local IP address' -r
complete -c xh -s j -l json -d '(default) Serialize data items from the command line as a JSON object'
Expand Down
4 changes: 2 additions & 2 deletions doc/xh.1
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
.TH XH 1 2024-01-28 0.21.0 "User Commands"
.TH XH 1 2024-03-22 0.21.0 "User Commands"

.SH NAME
xh \- Friendly and fast tool for sending HTTP requests
Expand Down Expand Up @@ -278,7 +278,7 @@ Make HTTPS requests if not specified in the URL.
\fB\-\-http\-version\fR=\fIVERSION\fR
HTTP version to use.

[possible values: 1.0, 1.1, 2]
[possible values: 1.0, 1.1, 2, 2\-prior\-knowledge]
.TP 4
\fB\-\-resolve\fR=\fIHOST:ADDRESS\fR
Override DNS resolution for specific domain to a custom IP.
Expand Down
2 changes: 2 additions & 0 deletions src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1280,6 +1280,8 @@ pub enum HttpVersion {
Http11,
#[clap(name = "2")]
Http2,
#[clap(name = "2-prior-knowledge")]
Http2PriorKnowledge,
}

/// HTTPie uses Python's str.decode(). That one's very accepting of different spellings.
Expand Down
8 changes: 7 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,10 @@ fn run(args: Cli) -> Result<i32> {
client = client.http1_only();
}

if matches!(args.http_version, Some(HttpVersion::Http2PriorKnowledge)) {
client = client.http2_prior_knowledge();
}

let cookie_jar = Arc::new(reqwest_cookie_store::CookieStoreMutex::default());
client = client.cookie_provider(cookie_jar.clone());

Expand Down Expand Up @@ -370,7 +374,9 @@ fn run(args: Cli) -> Result<i32> {
request_builder = match args.http_version {
Some(HttpVersion::Http10) => request_builder.version(reqwest::Version::HTTP_10),
Some(HttpVersion::Http11) => request_builder.version(reqwest::Version::HTTP_11),
Some(HttpVersion::Http2) => request_builder.version(reqwest::Version::HTTP_2),
Some(HttpVersion::Http2 | HttpVersion::Http2PriorKnowledge) => {
request_builder.version(reqwest::Version::HTTP_2)
}
None => request_builder,
};

Expand Down
1 change: 1 addition & 0 deletions src/to_curl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,7 @@ pub fn translate(args: Cli) -> Result<Command> {
HttpVersion::Http10 => cmd.arg("--http1.0"),
HttpVersion::Http11 => cmd.arg("--http1.1"),
HttpVersion::Http2 => cmd.arg("--http2"),
HttpVersion::Http2PriorKnowledge => cmd.arg("--http2-prior-knowledge"),
}
}

Expand Down
14 changes: 14 additions & 0 deletions tests/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3053,6 +3053,20 @@ fn http2() {
.stdout(contains("HTTP/2.0 200 OK"));
}

#[cfg(feature = "online-tests")]
#[test]
fn http2_prior_knowledge() {
get_command()
.args([
"--print=hH",
"--http-version=2-prior-knowledge",
"http://x.com",
])
.assert()
.stdout(contains("GET / HTTP/2.0"))
.stdout(contains("HTTP/2.0 "));
}

#[test]
fn override_response_charset() {
let server = server::http(|_req| async move {
Expand Down
Loading