PMHTTP
PMHTTP copied to clipboard
Preserve header case
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:
- There's a (very small) performance cost for doing this. Probably negligible, but it'd still be nicer to avoid it.
- 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:
- 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.
- Get rid of the property entirely.
- Publicly expose
CaseInsensitiveASCIIString
and change the property to be[CaseInsensitiveASCIIString: String]
, though if we did this we'd want to extendCaseInsensitiveASCIIString
to do unicode comparisons. It's also not obj-c–compatible. - Define our own
NSDictionary
subclass that does case-insensitive comparisons and use obj-c bridging in order to use that as our data structure.
We'd want to still normalize headers defined by the HTTP/1.1 standard, it's only custom headers that should preserve case.