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

Add a flag for forcing a specific http version #161

Merged
merged 18 commits into from
Oct 14, 2021
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
593 changes: 263 additions & 330 deletions Cargo.lock

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ exclude = ["assets/xhs", "assets/xhs.1.gz"]
anyhow = "1.0.38"
atty = "0.2"
base64 = "0.13"
bytes = "1.0.1"
cookie_crate = { version = "0.15", package = "cookie" }
cookie_store = { version = "0.15.0" }
reqwest_cookie_store = { version = "0.2.0" }
dirs = "3.0.1"
encoding_rs = "0.8.28"
encoding_rs_io = "0.1.7"
Expand All @@ -43,7 +43,7 @@ jsonxf = "1.1.0"
url = "2.2.2"

[dependencies.reqwest]
version = "0.11.4"
version = "0.11.5"
default-features = false
features = ["rustls-tls", "json", "gzip", "brotli", "deflate", "multipart", "blocking", "socks", "cookies"]

Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ OPTIONS:
--cert-key <FILE> A private key file to use with --cert
--native-tls Use the system TLS library instead of rustls (if enabled at compile time)
--https Make HTTPS requests if not specified in the URL
--http-version <VERSION> HTTP version to use [possible values: 1, 1.0, 1.1, 2]
-I, --ignore-stdin Do not attempt to read stdin
--curl Print a translation to a `curl` command
--curl-long Use the long versions of curl's flags
Expand Down Expand Up @@ -170,7 +171,6 @@ xh -d httpbin.org/json -o res.json
### Disadvantages

- Not all of HTTPie's features are implemented. ([#4](https://github.com/ducaale/xh/issues/4))
- HTTP/2 cannot be disabled. ([#68](https://github.com/ducaale/xh/issues/68))
- Header names are not case-sensitive.
- No plugin system.
- General immaturity. HTTPie is old and well-tested.
Expand Down
24 changes: 24 additions & 0 deletions src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,10 @@ pub struct Cli {
#[structopt(long)]
pub https: bool,

/// HTTP version to use
#[structopt(long, value_name = "VERSION", possible_values = &["1", "1.0", "1.1", "2"])]
pub http_version: Option<HttpVersion>,

/// Do not attempt to read stdin.
#[structopt(short = "I", long)]
pub ignore_stdin: bool,
Expand Down Expand Up @@ -319,6 +323,7 @@ const NEGATION_FLAGS: &[&str] = &[
"--no-headers",
"--no-history-print",
"--no-https",
"--no-http-version",
"--no-ignore-netrc",
"--no-ignore-stdin",
"--no-json",
Expand Down Expand Up @@ -932,6 +937,25 @@ impl Default for BodyType {
}
}

#[derive(Debug)]
pub enum HttpVersion {
Http10,
Http11,
Http2,
}

impl FromStr for HttpVersion {
type Err = Error;
fn from_str(version: &str) -> Result<HttpVersion> {
match version {
"1.0" => Ok(HttpVersion::Http10),
"1" | "1.1" => Ok(HttpVersion::Http11),
"2" => Ok(HttpVersion::Http2),
_ => unreachable!(),
}
}
}

/// Based on the function used by clap to abort
fn safe_exit() -> ! {
let _ = std::io::stdout().lock().flush();
Expand Down
31 changes: 27 additions & 4 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ mod request_items;
mod session;
mod to_curl;
mod utils;
mod vendored;

use std::env;
use std::fs::File;
Expand All @@ -28,12 +29,13 @@ use reqwest::header::{

use crate::auth::{auth_from_netrc, parse_auth, read_netrc};
use crate::buffer::Buffer;
use crate::cli::{BodyType, Cli, Print, Proxy, Verify};
use crate::cli::{BodyType, Cli, HttpVersion, Print, Proxy, Verify};
use crate::download::{download_file, get_file_size};
use crate::printer::Printer;
use crate::request_items::{Body, FORM_CONTENT_TYPE, JSON_ACCEPT, JSON_CONTENT_TYPE};
use crate::session::Session;
use crate::utils::{test_mode, test_pretend_term};
use crate::vendored::reqwest_cookie_store;

fn get_user_agent() -> &'static str {
if test_mode() {
Expand Down Expand Up @@ -218,9 +220,18 @@ fn run(args: Cli) -> Result<i32> {
}?);
}

if matches!(
args.http_version,
Some(HttpVersion::Http10) | Some(HttpVersion::Http11)
) {
client = client.http1_only();
}

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

let client = client.build()?;

let mut session = match &args.session {
Some(name_or_path) => Some(
Session::load_session(&args.url, name_or_path.clone(), args.is_session_read_only)
Expand Down Expand Up @@ -256,18 +267,30 @@ fn run(args: Cli) -> Result<i32> {
}
}

let client = client.build()?;

let mut request = {
let mut request_builder = client
.request(method, args.url.clone())
.header(
ACCEPT_ENCODING,
HeaderValue::from_static("gzip, deflate, br"),
)
.header(CONNECTION, HeaderValue::from_static("keep-alive"))
.header(USER_AGENT, get_user_agent());

if matches!(
args.http_version,
Some(HttpVersion::Http10) | Some(HttpVersion::Http11) | None
) {
request_builder =
request_builder.header(CONNECTION, HeaderValue::from_static("keep-alive"));
}

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),
None => request_builder,
};

request_builder = match body {
Body::Form(body) => request_builder.form(&body),
Body::Multipart(body) => request_builder.multipart(body),
Expand Down
2 changes: 1 addition & 1 deletion src/printer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -312,7 +312,7 @@ impl Printer {
let method = request.method();
let url = request.url();
let query_string = url.query().map_or(String::from(""), |q| ["?", q].concat());
let version = reqwest::Version::HTTP_11;
let version = request.version();
let mut headers = request.headers().clone();

headers
Expand Down
9 changes: 8 additions & 1 deletion src/to_curl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use anyhow::{anyhow, Result};
use reqwest::Method;

use crate::{
cli::{Cli, Verify},
cli::{Cli, HttpVersion, Verify},
request_items::{Body, RequestItem, FORM_CONTENT_TYPE, JSON_ACCEPT, JSON_CONTENT_TYPE},
};

Expand Down Expand Up @@ -185,6 +185,13 @@ pub fn translate(args: Cli) -> Result<Command> {
}
}
}
if let Some(http_version) = args.http_version {
match http_version {
HttpVersion::Http10 => cmd.push("--http1.0"),
HttpVersion::Http11 => cmd.push("--http1.1"),
HttpVersion::Http2 => cmd.push("--http2"),
}
}

if args.method == Some(Method::HEAD) {
cmd.flag("-I", "--head");
Expand Down
Loading