Skip to content

Commit

Permalink
Apply suggested changes
Browse files Browse the repository at this point in the history
  • Loading branch information
zuisong committed Apr 11, 2024
1 parent 38724cc commit 3c985f0
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 39 deletions.
26 changes: 2 additions & 24 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ use cookie_store::{CookieStore, RawCookie};
#[cfg(feature = "network-interface")]
use network_interface::{NetworkInterface, NetworkInterfaceConfig};
use redirect::RedirectFollower;
use reqwest::blocking::{Client, Response};
use reqwest::blocking::Client;
use reqwest::header::{
HeaderValue, ACCEPT, ACCEPT_ENCODING, CONNECTION, CONTENT_TYPE, COOKIE, RANGE, USER_AGENT,
};
Expand Down Expand Up @@ -502,13 +502,7 @@ fn run(args: Cli) -> Result<i32> {
.format_options
.iter()
.fold(FormatOptions::default(), FormatOptions::merge);
let mut printer = Printer::new(
pretty,
theme,
args.stream.unwrap_or(false),
buffer,
format_options,
);
let mut printer = Printer::new(pretty, theme, args.stream, buffer, format_options);

let response_charset = args.response_charset;
let response_mime = args.response_mime.as_deref();
Expand All @@ -529,11 +523,6 @@ fn run(args: Cli) -> Result<i32> {
if history_print.response_headers {
printer.print_response_headers(prev_response)?;
}
if args.stream.is_none()
&& is_text_event_stream_body(prev_response) == Some(true)
{
printer.set_stream(true)
}
if history_print.response_body {
printer.print_response_body(
prev_response,
Expand All @@ -548,7 +537,6 @@ fn run(args: Cli) -> Result<i32> {
if history_print.request_headers {
printer.print_request_headers(next_request, &*cookie_jar)?;
}

if history_print.request_body {
printer.print_request_body(next_request)?;
}
Expand Down Expand Up @@ -593,9 +581,6 @@ fn run(args: Cli) -> Result<i32> {
}
} else {
if print.response_body {
if args.stream.is_none() && is_text_event_stream_body(&response) == Some(true) {
printer.set_stream(true)
}
printer.print_response_body(&mut response, response_charset, response_mime)?;
if print.response_meta {
printer.print_separator()?;
Expand All @@ -616,10 +601,3 @@ fn run(args: Cli) -> Result<i32> {

Ok(exit_code)
}

fn is_text_event_stream_body(response: &Response) -> Option<bool> {
let content_type = response.headers().get(CONTENT_TYPE)?.to_str().ok()?;
let m: mime::Mime = content_type.parse().ok()?;
let is_stream = m.type_() == mime::TEXT && m.subtype() == mime::EVENT_STREAM;
is_stream.into()
}
52 changes: 37 additions & 15 deletions src/printer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,19 +114,15 @@ pub struct Printer {
sort_headers: bool,
color: bool,
theme: Theme,
stream: bool,
stream: Option<bool>,
buffer: Buffer,
}

impl Printer {
pub fn set_stream(&mut self, stream: bool) {
self.stream = stream
}

pub fn new(
pretty: Pretty,
theme: Theme,
stream: bool,
stream: impl Into<Option<bool>>,
buffer: Buffer,
format_options: FormatOptions,
) -> Self {
Expand All @@ -135,7 +131,7 @@ impl Printer {
json_indent_level: format_options.json_indent.unwrap_or(4),
sort_headers: format_options.headers_sort.unwrap_or(pretty.format()),
color: pretty.color(),
stream,
stream: stream.into(),
theme,
buffer,
}
Expand Down Expand Up @@ -451,6 +447,9 @@ impl Printer {
let compression_type = get_compression_type(response.headers());
let mut body = decompress(response, compression_type);

// Automatically activate stream mode when it hasn't been set by the user and the content type is stream
let stream = self.stream.unwrap_or(content_type.is_stream());

if !self.buffer.is_terminal() {
if (self.color || self.format_json) && content_type.is_text() {
// The user explicitly asked for formatting even though this is
Expand All @@ -465,7 +464,7 @@ impl Printer {
// force UTF-8 output without coloring or formatting
// Unconditionally decoding is not an option because the body
// might not be text at all
if self.stream {
if stream {
self.print_body_stream(
content_type,
&mut decode_stream(&mut body, encoding, &url)?,
Expand All @@ -476,14 +475,14 @@ impl Printer {
let text = decode_blob_unconditional(&buf, encoding, &url);
self.print_body_text(content_type, &text)?;
}
} else if self.stream {
} else if stream {
copy_largebuf(&mut body, &mut self.buffer, true)?;
} else {
let mut buf = Vec::new();
body.read_to_end(&mut buf)?;
self.buffer.print(&buf)?;
}
} else if self.stream {
} else if stream {
match self
.print_body_stream(content_type, &mut decode_stream(&mut body, encoding, &url)?)
{
Expand Down Expand Up @@ -542,15 +541,36 @@ enum ContentType {
Text,
UrlencodedForm,
Multipart,
EventStream,
Unknown,
}

impl ContentType {
fn is_text(&self) -> bool {
!matches!(
self,
ContentType::Unknown | ContentType::UrlencodedForm | ContentType::Multipart
)
match self {
ContentType::Unknown | ContentType::UrlencodedForm | ContentType::Multipart => false,
ContentType::Json
| ContentType::Html
| ContentType::Xml
| ContentType::JavaScript
| ContentType::Css
| ContentType::Text
| ContentType::EventStream => true,
}
}
fn is_stream(&self) -> bool {
match self {
ContentType::EventStream => true,
ContentType::Json
| ContentType::Html
| ContentType::Xml
| ContentType::JavaScript
| ContentType::Css
| ContentType::Text
| ContentType::UrlencodedForm
| ContentType::Multipart
| ContentType::Unknown => false,
}
}
}

Expand All @@ -570,6 +590,8 @@ impl From<&str> for ContentType {
ContentType::JavaScript
} else if content_type.contains("css") {
ContentType::Css
} else if content_type.contains("event-stream") {
ContentType::EventStream
} else if content_type.contains("text") {
// We later check if this one's JSON
// HTTPie checks for "json", "javascript" and "text" in one place:
Expand Down Expand Up @@ -823,7 +845,7 @@ mod tests {
sort_headers: false,
color: false,
theme: Theme::Auto,
stream: false,
stream: false.into(),
buffer: Buffer::new(false, None, false).unwrap(),
};

Expand Down

0 comments on commit 3c985f0

Please sign in to comment.