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 HTTP headers parsing #297

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

alexkunin
Copy link
Contributor

This is far from the final implementation. More of a conversation starter.

Few notes:

  1. It uses http.ReadResponse to properly parse headers.
  2. Due to p.1, headers are re-ordered, their names are canonicalized.
  3. Body is also parsed by http.ReadResponse because that's how it works.
  4. Special handling of curl's status line HTTP/2 (no minor version) is included.
  5. User will see an array with 3 values: status line, headers object, parsed body. I mean, fx is JSON viewer, so, we convert things to JSON and view them.
  6. Headers with multiple values are presented as arrays.
  7. No JavaScript part whatsoever for now, Go part requires lots of attention.

Things to consider:

  1. What if body is not a valid JSON? Currently it will fail. But we could show it as a string instead.
  2. Body is just brute-force parsed as JSON, Content-Type and other related headers are ignored. What if it's not application/json, or not UTF-8? Or if it's YAML?
  3. http.ReadResponse might be picky about Content-Length header (especially wrong values). Just believe it will work fine? Make it ignore content length? Show some warning?
  4. Apparently, if we want to see original header names (non-canonicalized), in the original order, we should find an alternative to http.ReadResponse. Is this really important? Maybe users will want a CLI option (or hotkey) to switch modes?
  5. Often field values are valid JSON, should we do something about it? E.g.:
report-to: {"group":"heroku-nel","max_age":3600,"endpoints":[{"url":"https://nel.heroku.com/reports?ts=1710712651&sid=c4c9725f-1ab0-44d8-820f-430df2718e11&s=jGCtfLAslGLM1QUWfRhiMIJr5mcM29gDwJ5JsYNYRuA%3D"}]}
reporting-endpoints: heroku-nel=https://nel.heroku.com/reports?ts=1710712651&sid=c4c9725f-1ab0-44d8-820f-430df2718e11&s=jGCtfLAslGLM1QUWfRhiMIJr5mcM29gDwJ5JsYNYRuA%3D
nel: {"report_to":"heroku-nel","max_age":3600,"success_fraction":0.005,"failure_fraction":0.05,"response_headers":["Via"]}

closes #290

@antonmedv
Copy link
Owner

I think displaying in this format will be much cooler:

image

http.go Outdated
import (
"bufio"
"bytes"
"encoding/json"
Copy link
Owner

Choose a reason for hiding this comment

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

And let's use our own parser jsonx

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I was using encoding/json because I needed to manipulate JSON (mostly combine), not only parse. But I agree, having two separate parsers is a bit excessive.

@alexkunin
Copy link
Contributor Author

I think displaying in this format will be much cooler:

I don't disagree, but this format rises questions:

  1. Is it interactable?
  2. If yes, how yanking should work?
  3. If no, what if terminal size is too small to show all headers?

I'd say the following will pose least problems:

  1. Make it interactable: let it render as current JSON structure (object + strings or arrays of strings as values), but don't display any brackets or indent. This should be rather easy.
  2. On yank:
  • if cursor is inside headers, work with headers JSON only
  • if cursor inside body, work with body JSON only

So, effectively:

  1. Need to implement ability to have two consecutive JSON documents.
  2. Add special document renderer without brackets.

@antonmedv
Copy link
Owner

Is it interactable?

Yes.

If yes, how yanking should work?

I guess this will be just a key/value pair.

If no, what if terminal size is too small to show all headers?

Wrapping and scrolling.

Need to implement ability to have two consecutive JSON documents.

Yes. This is actually number one thing what is needed.

Add special document renderer without brackets.

This is easy.

@alexkunin
Copy link
Contributor Author

alexkunin commented Mar 23, 2024

This update is much more finished:

  1. Simple regexp-based HTTP response parser (basically, everything is retained as close to original as possible):
    • original order of headers retained
    • header names are displayed as-is (no canonicalization)
    • multiline header values are preserved (and later displayed) as separate lines
    • multiple instances of same header are not combined
    • all of this as a neat small package with own tests
  2. New keys in theme (currently header name has same color as object key, and header name + status have same color as string). Note: that differs from your example screenshot because it's somewhat hard for me to come up with colors for every theme available. Anyhow, that's the smallest of problems, considering how large this PR is now.
  3. Yanking/searching should work fine.
  4. Got rid of encoding/json.

Re multiple documents support, I've tried that approach at first, but it requires too many changes in lots of places. So, instead I'm just adding some new nodes at the start to display headers (along with new Node.Kind field which simplifies things alot).

@antonmedv
Copy link
Owner

Awesome! Let me review and test this PR before merging.

@alexkunin
Copy link
Contributor Author

Yay! But FYI, currently I have no idea what JavaScript part of the project does, so, this PR so far was about Go code only.

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.

Display http headers
2 participants