This repository was archived by the owner on Sep 22, 2025. It is now read-only.
feat: define a headers type #68
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
I have been thinking a lot about headers. Currently, there is a relatively major issue that
ServiceContext.request_headershas different type than others.The biggest challenge of headers is that 99% of users treat them as a mapping from string to single string, but for HTTP protocol compatibility we must still support the <1% that may need multiple values for a single key. So my goal with exposing headers is to make it as easy as possible for the 99% while also supporting the rest. We know we support the rest because conformance tests require it too.
This means I want to stop using
Mappingbecause it can only have single value, or multi value, but not handle them both cleanly.I looked at some popular libraries like multidict. But it seems like overkill to me, notably it reimplements hashtables completely and has known bugs because of it. It's still ok but I wanted to avoid it.
We shouldn't use
httpx.Headerson server side since it's a client library.So I went ahead and implemented my own
Headersclass (so great for Go / typescript to have them in stdlib 😅 ). It is almost completelly just delegating to adict[str, str]for the 99% case, while also keeping an extra dict in case extra values are needed. As almost all usage delegates todict, possibly with an additional failingis Nonecheck, I think the performance should be quite high and avoid reimplementing the dict. It won't be as high as another approach when multiple values are actually used, but they should rarely be used.To avoid exporting multiple headers types, I have replaced
RequestHeaderswith an inline type that should cover the common use case and uncommon use case minimally.How does that sound?
Note, this is the last big API change I have in mind so probably a good chance to release after.