PMHTTP icon indicating copy to clipboard operation
PMHTTP copied to clipboard

Preserve header case

Open lilyball opened this issue 8 years ago • 1 comments

Our current mechanism for handling request headers is to normalize header names when inserting or looking them up. This means that you can say

request.headerFields["content-type"] = "application/json"
assert(request.headerFields["Content-Type"] == "application/json")

But this has two downsides:

  1. There's a (very small) performance cost for doing this. Probably negligible, but it'd still be nicer to avoid it.
  2. It doesn't preserve the case of assigned headers.

Headers are case-insensitive (which is why we do this), so not preserving the case has no functionality impacts, but it may be visible if header names are ever exposed to the user.

Note that we're already using a custom type to represent the headers, but we expose a var dictionary: [String: String] { get } property which is why we normalize instead of using a data structure that does case-insensitive comparisons. Solving this problem means doing one of the following:

  1. Calculate the dictionary value when accessing the property. This could be memoized so it's only recalculated if anything changes, but it's still not great.
  2. Get rid of the property entirely.
  3. Publicly expose CaseInsensitiveASCIIString and change the property to be [CaseInsensitiveASCIIString: String], though if we did this we'd want to extend CaseInsensitiveASCIIString to do unicode comparisons. It's also not obj-c–compatible.
  4. Define our own NSDictionary subclass that does case-insensitive comparisons and use obj-c bridging in order to use that as our data structure.

lilyball avatar Apr 19 '16 22:04 lilyball

We'd want to still normalize headers defined by the HTTP/1.1 standard, it's only custom headers that should preserve case.

lilyball avatar Apr 19 '16 22:04 lilyball