[swift-server-dev] HTTP Parser

Helge Heß me at helgehess.eu
Fri Nov 4 16:38:54 CDT 2016


On 4 Nov 2016, at 21:34, Brent Royal-Gordon <brent at architechies.com> wrote:
> Here's my question: Can we have our HTTP parser use Foundation's existing `URLRequest` and `HTTPURLResponse` types as Swift-facing outputs and inputs?

I did that in a Swift 2 GCD (and http-parser) based iOS HTTP proxy for a customer project. That is, parse stuff into NSURLRequest and NSHTTPURLResponse. I think in general this is the right approach to do this, but I also ran into a few issues. E.g. one was that NSURLRequest takes an NSURL, hence it can’t be used as-is for a `CONNECT host:port …` request.

The other thing I dislike about the two is that the API isn’t symmetric. E.g. in NSURLRequest you grab the headers via 

  var allHTTPHeaderFields: [String : String]?

whereas in NSHTTPURLResponse you do:

  var allHeaderFields: [AnyHashable : Any]

NSURLRequest has

  func value(forHTTPHeaderField: String)

which NSHTTPURLResponse doesn’t have, etc.

But I suppose stuff like that should be fixed in Foundation too. And it isn’t a blocker, I ended up writing extensions for such.
And maybe that is the right solution for Swift-Server: Have protocols for that and let the two be just one implementation of them. E.g. a libcurl C request struct could be another. 


Having said that: there is one detail which may be hard for the API as it may tie into the way the I/O is done: the body streams. In my project I just used the two classes for the headers only.

hh



More information about the swift-server-dev mailing list