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

Decode headers as latin1/UTF-8, show real reason phrase #377

Merged
merged 3 commits into from
Sep 30, 2024

Conversation

blyxxyz
Copy link
Collaborator

@blyxxyz blyxxyz commented Jul 4, 2024

image

External changes:

  • We now print the actual reason phrase sent by the server instead of guessing it from the status code. That is, if servers reply with "200 Wonderful" instead of "200 OK" then we show that. This is especially useful for status codes that xh doesn't recognize.

  • Header values are now decoded as latin1, with the UTF-8 decoding also shown if applicable. (Resolves IRI output #323.)

  • A new FAQ file with an entry that explains header value encoding. Header output now hyperlinks to this entry when relevant and if supported by the terminal.

Under the hood we now color headers manually. It's still hooked up to the .tmTheme files but not to the .sublime-syntax file. This lets us highlight the latin1 header values differently. In the future we could use the same approach to optimize JSON highlighting.

I'm unsure about the position of the hyperlink. Currently it's the text "UTF-8" in <latin1 value> (UTF-8: <utf-8 value>). But that means it's only shown if the value can be decoded as UTF-8. An alternative is to turn the latin1 value itself into a hyperlink, but that's confusing if the value itself is already a URL (which is a common case for the Location header).

I also don't feel that our text is quite distinct enough from the header value in the default ansi theme. Though the hyperlink does help to set it apart.

We should use a stable URL that keeps working in the future. I linked to FAQ.md on this repo's master branch (https://github.com/ducaale/xh/blob/master/FAQ.md#header-value-encoding, won't work until it's merged) but maybe there's a better option?

External changes:

- We now print the actual reason phrase sent by the server instead
  of guessing it from the status code. That is, if servers reply with
  "200 Wonderful" instead of "200 OK" then we show that. This is
  especially useful for status codes that xh doesn't recognize.

- Header values are now decoded as latin1, with the UTF-8 decoding
  also shown if applicable.

- A new FAQ file with an entry that explains header value encoding.
  Header output now hyperlinks to this entry when relevant and if
  supported by the terminal.

Under the hood we now color headers manually. It's still hooked up to
the `.tmTheme` files but not to the `.sublime-syntax` file. This lets
us highlight the latin1 header values differently. In the future we
could use the same approach to optimize JSON highlighting.

I'm unsure about the position of the hyperlink. Currently it's the
text "UTF-8" in `<latin1 value> (UTF-8: <utf-8 value>)`. But that
means it's only shown if the value can be decoded as UTF-8. An
alternative is to turn the latin1 value itself into a hyperlink, but
that's confusing if the value itself is already a URL (which is a
common case for the `Location` header).

I also don't feel that our text is quite distinct enough from the
header value in the default `ansi` theme. Though the hyperlink does
help to set it apart.
@blyxxyz
Copy link
Collaborator Author

blyxxyz commented Jul 6, 2024

(Sorry for the big diff for such a small feature! I'm hoping it'll be useful for other things in the future.)

@ducaale
Copy link
Owner

ducaale commented Sep 25, 2024

Apologies for taking so long to review this 😬. I’ll make sure to leave a review by this weekend.

Copy link
Owner

@ducaale ducaale left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good! Loving the new FAQ.md file

I'm unsure about the position of the hyperlink. Currently it's the text "UTF-8" in (UTF-8: ). But that means it's only shown if the value can be decoded as UTF-8.

I think linking the FAQ file is only necessary when xh prints extra information that wasn't part of the request/response, e.g (UTF-8: <utf-8 value>)

<key>settings</key>
<dict>
<key>foreground</key>
<string>#01000000</string>
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure what color would work best. But since this is rarely shown, defaulting to Ansi-red sound good 👍

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we rename this file to formatting/mod.rs?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done!

Copy link
Owner

@ducaale ducaale left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Approved 🎉

@ducaale ducaale merged commit cca9ad0 into ducaale:master Sep 30, 2024
9 checks passed
@blyxxyz blyxxyz deleted the smart-header-decode branch October 1, 2024 06:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

IRI output
2 participants