[swift-server-dev] HTTP Parser

steve algernon salgernon at apple.com
Mon Nov 7 04:37:24 CST 2016


> On Nov 4, 2016, at 1:34 PM, Brent Royal-Gordon via swift-server-dev <swift-server-dev at swift.org> 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?  Or are these types too-tightly designed for an HTTP client's needs, and we need different types for an HTTP server? Or are there minor modifications the Foundation team is willing to make in Apple Foundation to extend them for our use case? If we *can* use these types on both client and server, I think that would be ideal—it would allow people to reuse their knowledge and potentially a little bit of code.

NSHTTPURLRequest and NHTTPURLResponse should be written in terms of a richer HTTP message object.  I would suggest NSHTTPMessage => { NSHTTPRequestMessage, NSHTTPResponseMessage } with a class method to parse and construct.

(Currently, NSURLRequest carries a reference to any payload, while NSURLSession expects the payload to be separate from the request header.  NSURLResponse doesn't provide the response as a data, and I don't like having the two not be reflexive.)

There's two reasons for this: (1) The current API doesn't allow for correct behavior for multiple HTTP Headers. If you Set-Cookie: a=x and Set-Cookie: b=x, asking for the Set-Cookie header will give you SetCookie value of "a=x; b=x", which is not a valid cookie.  There may be other headers for which this combining is incorrect.

(2) CFHTTPMessageRef in CFNetwork implements the underlying behavior here, but that object is not exposed in Foundation.  I don't know if CFHTTPMessage ever made it out into the CFNetwork open source drop of, what 2003? but it could do with a refresh.  

That being said, the header parsing is pretty trivial.  (Except where it isn't, eg multipart-mixed-replace and chunked-encoding Trailers)

--sma

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-server-dev/attachments/20161107/f9ec2831/attachment.html>


More information about the swift-server-dev mailing list