<html><head><meta http-equiv="Content-Type" content="text/html charset=utf-8"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class="">We could, but I’m not sure it would be more performant. The parser still has to scan the string to figure out how much data there is (find ‘\r\n’, etc). This is complicated by the fact that there are already emoji in URLs, among other issues. So, if the data coming off the wire is parsed as a string I’d prefer to keep it as such. I actually wasn’t sure about using Uint for status and could be for using strings for everything.<div class=""><br class=""></div><div class="">Also, bytes do not fare well with asynchronous APIs as all of the bytes may not be in a continuous buffer (this is what dispatch_data was made to handle)<div class=""><br class=""><div><blockquote type="cite" class=""><div class="">On Apr 10, 2017, at 2:35 AM, Joy Keys &lt;<a href="mailto:proyb7@gmail.com" class="">proyb7@gmail.com</a>&gt; wrote:</div><br class="Apple-interchange-newline"><div class="">I would like to know what if we could use bytes to&nbsp;accept input&nbsp;instead of UInt and&nbsp;String for performance?<div class=""><div class=""><br class="">On Monday, April 10, 2017, George Leontiev via swift-server-dev &lt;<a href="javascript:_e(%7B%7D,'cvml','swift-server-dev@swift.org');" target="_blank" class="">swift-server-dev@swift.org</a>&gt; wrote:<br class=""><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div style="word-wrap:break-word" class=""><div class="">I’ve fleshed out my earlier suggestion.</div><div class=""><br class=""></div><div class="">For responses coming back from a server or requests going to a server I like what Helge referred to as a “parser delegate” (naming suggestions welcome) because it allows us to parse the request or response exactly as it comes of the wire, and make no assumptions about how the user wants that data. Also, this API can be used with both an async API (built on top of dispatch) but could also be leveraged to create a synchronous API.&nbsp;</div><div class=""><br class=""></div><div class="">In terms of utility, I like that clients can provide their own HTTPResponseDecoders which can decode to the specific type they are expecting from that endpoint. Servers, on the other hand, can perform their routing logic while processing that path and use the results of that to more appropriately decode the rest of the request.&nbsp;</div><div class=""><br class=""></div><div class="">As I and Helge mentioned, there is overhead to using a protocol cross-module, but the benefit is that we no longer have to have a catch-all type to represent a decoded request or response; this allows users who care about performance to encode their concrete types as efficiently as they can. We can also have a catch-all type provided by the standard library which leaves all the fields as strings (indeed, it can be backed by the actual request data and just point to ranges) and would benefit from full-module optimization. Even better, we can make an opinionated concrete implementation (which can use enums for stata and headers and enforce things like the ContentType header value should always be a MediaType, etc.) but allow specialized applications which need more exotic semantics to fall back to implementing their own type (instead of having `.custom(…)` enum cases)</div><div class=""><br class=""></div><div class="">Note: I specifically omitted the body implementation and we may need something like Johannes’ &nbsp;*Writer API to support sending chunked things.</div><div class=""><br class=""></div><div class="">- George</div><div class=""><br class=""></div>Code:<div class=""><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(0,132,0)" class=""><span class="">// MARK: - Stubs</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;min-height:13px" class=""><span class=""></span><br class=""></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(186,45,162)" class=""><span class="">public</span><span style="" class=""> </span><span class="">protocol</span><span style="" class=""> HTTPBody {</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(0,132,0)" class=""><span style="" class="">&nbsp; &nbsp; </span><span class="">// API omitted.</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(0,132,0)" class=""><span style="" class="">&nbsp; &nbsp; </span><span class="">// There are decisions about how to handle streaming bodies which are outside the scope of this example.</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo" class=""><span class="">}</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;min-height:13px" class=""><span class=""></span><br class=""></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(0,132,0)" class=""><span class="">// Definition omitted</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo" class=""><span style="color:#ba2da2" class="">public</span><span class=""> </span><span style="color:#ba2da2" class="">typealias</span><span class=""> HTTPHeader = </span><span style="color:#703daa" class="">Void</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;min-height:13px" class=""><span class=""></span><br class=""></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo" class=""><span style="color:#ba2da2" class="">public</span><span class=""> </span><span style="color:#ba2da2" class="">struct</span><span class=""> PathIterator: </span><span style="color:#703daa" class="">IteratorProtocol</span><span class=""> {</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; min-height: 13px;" class=""><span class="">&nbsp;&nbsp; &nbsp;</span><br class=""></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo" class=""><span class="">&nbsp; &nbsp; </span><span style="color:#ba2da2" class="">func</span><span class=""> next() -&gt; </span><span style="color:#703daa" class="">String</span><span class=""> {</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo" class=""><span class="">&nbsp; &nbsp; &nbsp; &nbsp; </span><span style="color:#3e1e81" class="">fatalError</span><span class="">()</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo" class=""><span class="">&nbsp; &nbsp; }</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; min-height: 13px;" class=""><span class="">&nbsp;&nbsp; &nbsp;</span><br class=""></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo" class=""><span class="">}</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;min-height:13px" class=""><span class=""></span><br class=""></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(0,132,0)" class=""><span class="">// MARK: - Body Decoder</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;min-height:13px" class=""><span class=""></span><br class=""></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo" class=""><span style="color:#ba2da2" class="">public</span><span class=""> </span><span style="color:#ba2da2" class="">protocol</span><span class=""> HTTPBodyDecoder {</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; min-height: 13px;" class=""><span class="">&nbsp;&nbsp; &nbsp;</span><br class=""></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo" class=""><span class="">&nbsp; &nbsp; </span><span style="color:#ba2da2" class="">mutating</span><span class=""> </span><span style="color:#ba2da2" class="">func</span><span class=""> decodeTrailingHeader(</span><span style="color:#ba2da2" class="">_</span><span class=""> key: </span><span style="color:#703daa" class="">String</span><span class="">, value: </span><span style="color:#703daa" class="">String</span><span class="">) </span><span style="color:#ba2da2" class="">throws</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; min-height: 13px;" class=""><span class="">&nbsp;&nbsp; &nbsp;</span><br class=""></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo" class=""><span class="">&nbsp; &nbsp; </span><span style="color:#ba2da2" class="">mutating</span><span class=""> </span><span style="color:#ba2da2" class="">func</span><span class=""> decodeBodyChunk(</span><span style="color:#ba2da2" class="">_</span><span class=""> data: </span><span style="color:#703daa" class="">Data</span><span class="">) </span><span style="color:#ba2da2" class="">throws</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; min-height: 13px;" class=""><span class="">&nbsp;&nbsp; &nbsp;</span><br class=""></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo" class=""><span class="">}</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;min-height:13px" class=""><span class=""></span><br class=""></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(0,132,0)" class=""><span class="">// MARK: - Server API</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;min-height:13px" class=""><span class=""></span><br class=""></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(0,132,0)" class=""><span class="">// A type which can be created from an HTTP Request as it is coming off the wire. Suggestions on naming are welcome!</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo" class=""><span style="color:#ba2da2" class="">public</span><span class=""> </span><span style="color:#ba2da2" class="">protocol</span><span class=""> HTTPRequestDecoder: </span><span style="color:#4f8187" class="">HTTPBodyDecoder</span><span class=""> {</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; min-height: 13px;" class=""><span class="">&nbsp;&nbsp; &nbsp;</span><br class=""></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(186,45,162)" class=""><span style="" class="">&nbsp; &nbsp; </span><span class="">associatedtype</span><span style="" class=""> Request</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; min-height: 13px;" class=""><span class="">&nbsp;&nbsp; &nbsp;</span><br class=""></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(0,132,0)" class=""><span style="" class="">&nbsp; &nbsp; </span><span class="">// Thrown exceptions terminate decoding</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; min-height: 13px;" class=""><span class="">&nbsp;&nbsp; &nbsp;</span><br class=""></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(0,132,0)" class=""><span style="" class="">&nbsp; &nbsp; </span><span class="">// Called before any other methods</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo" class=""><span class="">&nbsp; &nbsp; </span><span style="color:#ba2da2" class="">mutating</span><span class=""> </span><span style="color:#ba2da2" class="">func</span><span class=""> decodeMethod(</span><span style="color:#ba2da2" class="">_</span><span class=""> value: </span><span style="color:#703daa" class="">String</span><span class="">) </span><span style="color:#ba2da2" class="">throws</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; min-height: 13px;" class=""><span class="">&nbsp;&nbsp; &nbsp;</span><br class=""></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(0,132,0)" class=""><span style="" class="">&nbsp; &nbsp; </span><span class="">// Called after `decodeMethod` and before any other methods. Types conforming to `HTTPRequestDecoder` can use this method to walk the requested path and prepare to handle the rest of the request appropriately. Implementation does not need to exhaust the iterator.</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo" class=""><span class="">&nbsp; &nbsp; </span><span style="color:#ba2da2" class="">mutating</span><span class=""> </span><span style="color:#ba2da2" class="">func</span><span class=""> decodePath(</span><span style="color:#ba2da2" class="">_</span><span class=""> iterator: </span><span style="color:#ba2da2" class="">inout</span><span class=""> </span><span style="color:#4f8187" class="">PathIterator</span><span class="">) </span><span style="color:#ba2da2" class="">throws</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; min-height: 13px;" class=""><span class="">&nbsp;&nbsp; &nbsp;</span><br class=""></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(0,132,0)" class=""><span style="" class="">&nbsp; &nbsp; </span><span class="">// Called after all of the above methods, and before any methods below.</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo" class=""><span class="">&nbsp; &nbsp; </span><span style="color:#ba2da2" class="">mutating</span><span class=""> </span><span style="color:#ba2da2" class="">func</span><span class=""> decodeVersion(</span><span style="color:#ba2da2" class="">_</span><span class=""> value: </span><span style="color:#703daa" class="">String</span><span class="">) </span><span style="color:#ba2da2" class="">throws</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; min-height: 13px;" class=""><span class="">&nbsp;&nbsp; &nbsp;</span><br class=""></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(0,132,0)" class=""><span style="" class="">&nbsp; &nbsp; </span><span class="">// Called after all of the above methods and before any methods below. May be called multiple times</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo" class=""><span class="">&nbsp; &nbsp; </span><span style="color:#ba2da2" class="">mutating</span><span class=""> </span><span style="color:#ba2da2" class="">func</span><span class=""> decodeHeader(</span><span style="color:#ba2da2" class="">_</span><span class=""> key: </span><span style="color:#703daa" class="">String</span><span class="">, value: </span><span style="color:#703daa" class="">String</span><span class="">) </span><span style="color:#ba2da2" class="">throws</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; min-height: 13px;" class=""><span class="">&nbsp;&nbsp; &nbsp;</span><br class=""></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(0,132,0)" class=""><span style="" class="">&nbsp; &nbsp; </span><span class="">// HTTPBodyDecoder methods conceptually appear here</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; min-height: 13px;" class=""><span class="">&nbsp;&nbsp; &nbsp;</span><br class=""></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(0,132,0)" class=""><span style="" class="">&nbsp; &nbsp; </span><span class="">// Returns an object to be used to represent the request</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo" class=""><span class="">&nbsp; &nbsp; </span><span style="color:#ba2da2" class="">func</span><span class=""> decoded() </span><span style="color:#ba2da2" class="">throws</span><span class=""> -&gt; Request</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; min-height: 13px;" class=""><span class="">&nbsp;&nbsp; &nbsp;</span><br class=""></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo" class=""><span class="">}</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;min-height:13px" class=""><span class=""></span><br class=""></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(79,129,135)" class=""><span style="color:#ba2da2" class="">public</span><span style="" class=""> </span><span style="color:#ba2da2" class="">extension</span><span style="" class=""> </span><span class="">HTTPRequestDecoder</span><span style="" class=""> {</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; min-height: 13px;" class=""><span class="">&nbsp;&nbsp; &nbsp;</span><br class=""></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo" class=""><span class="">&nbsp; &nbsp; </span><span style="color:#ba2da2" class="">mutating</span><span class=""> </span><span style="color:#ba2da2" class="">func</span><span class=""> decodeVersion(</span><span style="color:#ba2da2" class="">_</span><span class=""> value: </span><span style="color:#703daa" class="">String</span><span class="">) </span><span style="color:#ba2da2" class="">throws</span><span class=""> {</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(0,132,0)" class=""><span style="" class="">&nbsp; &nbsp; &nbsp; &nbsp; </span><span class="">// Implementing decodeVersion is not necessary</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo" class=""><span class="">&nbsp; &nbsp; }</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; min-height: 13px;" class=""><span class="">&nbsp;&nbsp; &nbsp;</span><br class=""></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo" class=""><span class="">}</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;min-height:13px" class=""><span class=""></span><br class=""></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(0,132,0)" class=""><span class="">/**</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(0,132,0)" class=""><span class="">&nbsp;A type which can be represented as an HTTP Response</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(0,132,0)" class=""><span class="">&nbsp;*/</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo" class=""><span style="color:#ba2da2" class="">public</span><span class=""> </span><span style="color:#ba2da2" class="">protocol</span><span class=""> HTTPResponse {</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo" class=""><span class="">&nbsp; &nbsp; </span><span style="color:#ba2da2" class="">var</span><span class=""> statusCode: </span><span style="color:#703daa" class="">UInt</span><span class=""> { </span><span style="color:#ba2da2" class="">get</span><span class=""> }</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo" class=""><span class="">&nbsp; &nbsp; </span><span style="color:#ba2da2" class="">var</span><span class=""> headers: [</span><span style="color:#4f8187" class="">HTTPHeader</span><span class="">] { </span><span style="color:#ba2da2" class="">get</span><span class=""> }</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo" class=""><span class="">&nbsp; &nbsp; </span><span style="color:#ba2da2" class="">var</span><span class=""> body: </span><span style="color:#4f8187" class="">HTTPBody</span><span class=""> { </span><span style="color:#ba2da2" class="">get</span><span class=""> }</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo" class=""><span class="">}</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;min-height:13px" class=""><span class=""></span><br class=""></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(0,132,0)" class=""><span class="">// MARK: - Client API</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;min-height:13px" class=""><span class=""></span><br class=""></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(0,132,0)" class=""><span class="">/**</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(0,132,0)" class=""><span class="">&nbsp;A type which can be represented as an HTTP Request</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(0,132,0)" class=""><span class="">&nbsp;*/</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo" class=""><span style="color:#ba2da2" class="">public</span><span class=""> </span><span style="color:#ba2da2" class="">protocol</span><span class=""> HTTPRequest {</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo" class=""><span class="">&nbsp; &nbsp; </span><span style="color:#ba2da2" class="">var</span><span class=""> method: </span><span style="color:#703daa" class="">String</span><span class=""> { </span><span style="color:#ba2da2" class="">get</span><span class=""> }</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; min-height: 13px;" class=""><span class="">&nbsp;&nbsp; &nbsp;</span><br class=""></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(0,132,0)" class=""><span style="" class="">&nbsp; &nbsp; </span><span class="">// Note that the target host a part of an HTTPRequest</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo" class=""><span class="">&nbsp; &nbsp; </span><span style="color:#ba2da2" class="">var</span><span class=""> path: </span><span style="color:#703daa" class="">String</span><span class=""> { </span><span style="color:#ba2da2" class="">get</span><span class=""> }</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo" class=""><span class="">&nbsp; &nbsp; </span><span style="color:#ba2da2" class="">var</span><span class=""> headers: [</span><span style="color:#4f8187" class="">HTTPHeader</span><span class="">] { </span><span style="color:#ba2da2" class="">get</span><span class=""> }</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo" class=""><span class="">&nbsp; &nbsp; </span><span style="color:#ba2da2" class="">var</span><span class=""> body: </span><span style="color:#4f8187" class="">HTTPBody</span><span class=""> { </span><span style="color:#ba2da2" class="">get</span><span class=""> }</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo" class=""><span class="">}</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;min-height:13px" class=""><span class=""></span><br class=""></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo" class=""><span style="color:#ba2da2" class="">public</span><span class=""> </span><span style="color:#ba2da2" class="">protocol</span><span class=""> HTTPResponseDecoder: </span><span style="color:#4f8187" class="">HTTPBodyDecoder</span><span class=""> {</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; min-height: 13px;" class=""><span class="">&nbsp;&nbsp; &nbsp;</span><br class=""></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(186,45,162)" class=""><span style="" class="">&nbsp; &nbsp; </span><span class="">associatedtype</span><span style="" class=""> Response</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; min-height: 13px;" class=""><span class="">&nbsp;&nbsp; &nbsp;</span><br class=""></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(0,132,0)" class=""><span style="" class="">&nbsp; &nbsp; </span><span class="">// Thrown exceptions terminate decoding</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; min-height: 13px;" class=""><span class="">&nbsp;&nbsp; &nbsp;</span><br class=""></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(0,132,0)" class=""><span style="" class="">&nbsp; &nbsp; </span><span class="">// Called before any other methods</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo" class=""><span class="">&nbsp; &nbsp; </span><span style="color:#ba2da2" class="">mutating</span><span class=""> </span><span style="color:#ba2da2" class="">func</span><span class=""> decodeVersion(</span><span style="color:#ba2da2" class="">_</span><span class=""> value: </span><span style="color:#703daa" class="">String</span><span class="">) </span><span style="color:#ba2da2" class="">throws</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; min-height: 13px;" class=""><span class="">&nbsp;&nbsp; &nbsp;</span><br class=""></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(0,132,0)" class=""><span style="" class="">&nbsp; &nbsp; </span><span class="">// Called after `decodeVersion` and before any other methods</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo" class=""><span class="">&nbsp; &nbsp; </span><span style="color:#ba2da2" class="">mutating</span><span class=""> </span><span style="color:#ba2da2" class="">func</span><span class=""> decodeStatus(</span><span style="color:#ba2da2" class="">_</span><span class=""> value: </span><span style="color:#703daa" class="">UInt</span><span class="">) </span><span style="color:#ba2da2" class="">throws</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; min-height: 13px;" class=""><span class="">&nbsp;&nbsp; &nbsp;</span><br class=""></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(0,132,0)" class=""><span style="" class="">&nbsp; &nbsp; </span><span class="">// Called after `decodeVersion` and before any methods below</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo" class=""><span class="">&nbsp; &nbsp; </span><span style="color:#ba2da2" class="">mutating</span><span class=""> </span><span style="color:#ba2da2" class="">func</span><span class=""> decodeStatusText(</span><span style="color:#ba2da2" class="">_</span><span class=""> value: </span><span style="color:#703daa" class="">String</span><span class="">) </span><span style="color:#ba2da2" class="">throws</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; min-height: 13px;" class=""><span class="">&nbsp;&nbsp; &nbsp;</span><br class=""></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(0,132,0)" class=""><span style="" class="">&nbsp; &nbsp; </span><span class="">// Called after all of the above methods and before any methods below. May be called multiple times</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo" class=""><span class="">&nbsp; &nbsp; </span><span style="color:#ba2da2" class="">mutating</span><span class=""> </span><span style="color:#ba2da2" class="">func</span><span class=""> decodeHeader(</span><span style="color:#ba2da2" class="">_</span><span class=""> key: </span><span style="color:#703daa" class="">String</span><span class="">, value: </span><span style="color:#703daa" class="">String</span><span class="">) </span><span style="color:#ba2da2" class="">throws</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; min-height: 13px;" class=""><span class="">&nbsp;&nbsp; &nbsp;</span><br class=""></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(0,132,0)" class=""><span style="" class="">&nbsp; &nbsp; </span><span class="">// HTTPBodyDecoder methods conceptually appear here</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; min-height: 13px;" class=""><span class="">&nbsp;&nbsp; &nbsp;</span><br class=""></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(0,132,0)" class=""><span style="" class="">&nbsp; &nbsp; </span><span class="">// Returns an object to be used to represent the response</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo" class=""><span class="">&nbsp; &nbsp; </span><span style="color:#ba2da2" class="">func</span><span class=""> decoded() </span><span style="color:#ba2da2" class="">throws</span><span class=""> -&gt; Response</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; min-height: 13px;" class=""><span class="">&nbsp;&nbsp; &nbsp;</span><br class=""></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo" class=""><span class="">}</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;min-height:13px" class=""><span class=""></span><br class=""></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(0,132,0)" class=""><span class="">// MARK: - Implementing concrete types.</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;min-height:13px" class=""><span class=""></span><br class=""></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(0,132,0)" class=""><span class="">// We can provide come concrete implementations to handle *most* cases. For instance, a concrete type could only support IANA registered stata (as an enum) while a separate concrete type can allow for custom stata.</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;min-height:13px" class=""><span class=""></span><br class=""></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(0,132,0)" class=""><span class="">// Here is an example for a very simple server</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;min-height:13px" class=""><span class=""></span><br class=""></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo" class=""><span style="color:#ba2da2" class="">public</span><span class=""> </span><span style="color:#ba2da2" class="">enum</span><span class=""> GeorgeMethod: </span><span style="color:#703daa" class="">String</span><span class=""> {</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo" class=""><span class="">&nbsp; &nbsp; </span><span style="color:#ba2da2" class="">case</span><span class=""> get, post</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo" class=""><span class="">}</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;min-height:13px" class=""><span class=""></span><br class=""></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo" class=""><span style="color:#ba2da2" class="">public</span><span class=""> </span><span style="color:#ba2da2" class="">struct</span><span class=""> GeorgeHTTPRequest {</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; min-height: 13px;" class=""><span class="">&nbsp;&nbsp; &nbsp;</span><br class=""></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo" class=""><span class="">&nbsp; &nbsp; </span><span style="color:#ba2da2" class="">let</span><span class=""> status: </span><span style="color:#703daa" class="">GeorgeStatus</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; min-height: 13px;" class=""><span class="">&nbsp;&nbsp; &nbsp;</span><br class=""></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(0,132,0)" class=""><span style="" class="">&nbsp; &nbsp; </span><span class="">// Implementation omitted</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; min-height: 13px;" class=""><span class="">&nbsp;&nbsp; &nbsp;</span><br class=""></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo" class=""><span class="">}</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;min-height:13px" class=""><span class=""></span><br class=""></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(0,132,0)" class=""><span class="">// We can provide convenience types/protocols for generating decoders from types like my simple enum type above</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo" class=""><span style="color:#ba2da2" class="">public</span><span class=""> </span><span style="color:#ba2da2" class="">struct</span><span class=""> GeorgeHTTPRequestDecoder: </span><span style="color:#4f8187" class="">HTTPRequestDecoder</span><span class=""> {</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; min-height: 13px;" class=""><span class="">&nbsp;&nbsp; &nbsp;</span><br class=""></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(0,132,0)" class=""><span style="" class="">&nbsp; &nbsp; </span><span class="">// Implementation omitted</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; min-height: 13px;" class=""><span class="">&nbsp;&nbsp; &nbsp;</span><br class=""></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo" class=""><span class="">&nbsp; &nbsp; </span><span style="color:#ba2da2" class="">func</span><span class=""> decode() </span><span style="color:#ba2da2" class="">throws</span><span class=""> -&gt; </span><span style="color:#4f8187" class="">GeorgeHTTPRequest</span><span class=""> {</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo" class=""><span class="">&nbsp; &nbsp; &nbsp; &nbsp; </span><span style="color:#3e1e81" class="">fatalError</span><span class="">()</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo" class=""><span class="">&nbsp; &nbsp; }</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; min-height: 13px;" class=""><span class="">&nbsp;&nbsp; &nbsp;</span><br class=""></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo" class=""><span class="">}</span></div><div class=""><span class=""><br class=""></span></div><div class=""><span class=""><br class=""></span></div><div class=""><blockquote type="cite" class=""><div class="">On Apr 9, 2017, at 4:50 AM, Helge Heß via swift-server-dev &lt;<a class="">swift-server-dev@swift.org</a>&gt; wrote:</div><br class=""><div class=""><div class="">Hi,<br class=""><br class="">On 9. Apr 2017, at 04:12, George Leontiev via swift-server-dev &lt;<a class="">swift-server-dev@swift.org</a>&gt; wrote:<br class=""><blockquote type="cite" class="">Earlier in this thread, I think someone dismissed protocols/generics because “at some point, we need to implement a concrete type”.<br class=""></blockquote><br class="">I don’t think anything was ‘dismissed’. The sole meaning was that there will be a Swift Server module and that this should/will come with a concrete type (so that people can have a minimal running http server out of the box by just using that module). You can still have protocols or whatever you chose.<br class=""><br class=""><br class=""><blockquote type="cite" class="">I think we can leverage the Swift type system to have something much better.<br class=""><br class="">I recommend having protocols for HTTP{Request,Response}{Encodab<wbr class="">le,Decoder} types<br class=""></blockquote><br class="">&lt;snip&gt; protocols &lt;snip&gt;<br class=""><br class="">Your `HTTPRequestEncodable` is in a way the reverse of Johannes’ HTTPResponseWriter. The advantage of the latter is that it also doesn’t enforce a specific response type and neither requires a type at all. W/ your approach there always has to be a type implementing the protocol.<br class=""><br class="">I’m not entirely sure why you would call it `HTTPRequestEncodable`. I would just call it `HTTPRequest`? To me an `Encodable` has a method which provides another value (the encoded representation). Like this:<br class=""><br class=""> &nbsp;protocol HTTPRequestEncodable {<br class=""> &nbsp;&nbsp;&nbsp;func encodeAsHTTPRequest() -&gt; HTTPRequest<br class=""> &nbsp;}<br class=""><br class="">which may still be useful. But extra indirections :-)<br class=""><br class=""><br class="">Your `HTTPRequestDecoder` is like a parser delegate, kinda like the peer to the `HTTPResponseWriter`. Could be done like this.<br class="">Though if you provide an “Encodable” I suppose the assumption would be that you also provide a “Decodable” too ...<br class=""><br class=""><br class=""><blockquote type="cite" class="">I can flesh out the APIs more if people are interested, but I think this is sufficient to communicate the idea, and I’d love to hear more feedback. <br class=""></blockquote><br class="">A complete proposal like Johannes’ would be more useful. Or maybe even better a revision of Johannes’ proposal with your ideas in it.<br class=""><br class=""><br class=""><blockquote type="cite" class="">There is a performance overhead to using protocols/generics, but I believe these can be effectively mediated by the compiler.<br class=""></blockquote><br class="">I don’t see how you would eliminate the protocol overhead cross-module. But I think it should be small enough (after all the proposal also uses Strings ;-).<br class=""><br class="">hh<br class=""><br class="">______________________________<wbr class="">_________________<br class="">swift-server-dev mailing list<br class=""><a class="">swift-server-dev@swift.org</a><br class=""><a href="https://lists.swift.org/mailman/listinfo/swift-server-dev" target="_blank" class="">https://lists.swift.org/mailma<wbr class="">n/listinfo/swift-server-dev</a><br class=""></div></div></blockquote></div><br class=""></div></div></blockquote></div>
</div>
</div></blockquote></div><br class=""></div></div></body></html>