<div><br></div><div>Thanks George on bytes. I have seen the recently Techempower benchmark shown minihttp is performant in JSON and plain text is an interesting finding whether if it is something we haven&#39;t explore.</div><div><a href="https://github.com/tokio-rs/tokio-minihttp">https://github.com/tokio-rs/tokio-minihttp</a><br></div><div><br></div><div>Check out on JSON and Plaintext </div><div><a href="https://www.techempower.com/benchmarks/previews/round14/#section=data-r14&amp;hw=ph&amp;test=json">https://www.techempower.com/benchmarks/previews/round14/#section=data-r14&amp;hw=ph&amp;test=json</a><br></div><div><br></div><br><br>On Monday, April 10, 2017, George Leontiev &lt;<a href="mailto:georgeleontiev@gmail.com">georgeleontiev@gmail.com</a>&gt; wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div style="word-wrap:break-word">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><br></div><div>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><br><div><blockquote type="cite"><div>On Apr 10, 2017, at 2:35 AM, Joy Keys &lt;<a href="javascript:_e(%7B%7D,&#39;cvml&#39;,&#39;proyb7@gmail.com&#39;);" target="_blank">proyb7@gmail.com</a>&gt; wrote:</div><br><div>I would like to know what if we could use bytes to accept input instead of UInt and String for performance?<div><div><br>On Monday, April 10, 2017, George Leontiev via swift-server-dev &lt;<a>swift-server-dev@swift.org</a>&gt; wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div style="word-wrap:break-word"><div>I’ve fleshed out my earlier suggestion.</div><div><br></div><div>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. </div><div><br></div><div>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. </div><div><br></div><div>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><br></div><div>Note: I specifically omitted the body implementation and we may need something like Johannes’  *Writer API to support sending chunked things.</div><div><br></div><div>- George</div><div><br></div>Code:<div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(0,132,0)"><span>// MARK: - Stubs</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;min-height:13px"><span></span><br></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(186,45,162)"><span>public</span><span> </span><span>protocol</span><span> HTTPBody {</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(0,132,0)"><span>    </span><span>// API omitted.</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(0,132,0)"><span>    </span><span>// 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"><span>}</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;min-height:13px"><span></span><br></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(0,132,0)"><span>// Definition omitted</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span style="color:#ba2da2">public</span><span> </span><span style="color:#ba2da2">typealias</span><span> HTTPHeader = </span><span style="color:#703daa">Void</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;min-height:13px"><span></span><br></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span style="color:#ba2da2">public</span><span> </span><span style="color:#ba2da2">struct</span><span> PathIterator: </span><span style="color:#703daa">IteratorProtocol</span><span> {</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;min-height:13px"><span>    </span><br></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span>    </span><span style="color:#ba2da2">func</span><span> next() -&gt; </span><span style="color:#703daa">String</span><span> {</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span>        </span><span style="color:#3e1e81">fatalError</span><span>()</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span>    }</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;min-height:13px"><span>    </span><br></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span>}</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;min-height:13px"><span></span><br></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(0,132,0)"><span>// MARK: - Body Decoder</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;min-height:13px"><span></span><br></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span style="color:#ba2da2">public</span><span> </span><span style="color:#ba2da2">protocol</span><span> HTTPBodyDecoder {</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;min-height:13px"><span>    </span><br></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span>    </span><span style="color:#ba2da2">mutating</span><span> </span><span style="color:#ba2da2">func</span><span> decodeTrailingHeader(</span><span style="color:#ba2da2">_</span><span> key: </span><span style="color:#703daa">String</span><span>, value: </span><span style="color:#703daa">String</span><span>) </span><span style="color:#ba2da2">throws</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;min-height:13px"><span>    </span><br></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span>    </span><span style="color:#ba2da2">mutating</span><span> </span><span style="color:#ba2da2">func</span><span> decodeBodyChunk(</span><span style="color:#ba2da2">_</span><span> data: </span><span style="color:#703daa">Data</span><span>) </span><span style="color:#ba2da2">throws</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;min-height:13px"><span>    </span><br></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span>}</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;min-height:13px"><span></span><br></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(0,132,0)"><span>// MARK: - Server API</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;min-height:13px"><span></span><br></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(0,132,0)"><span>// 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"><span style="color:#ba2da2">public</span><span> </span><span style="color:#ba2da2">protocol</span><span> HTTPRequestDecoder: </span><span style="color:#4f8187">HTTPBodyDecoder</span><span> {</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;min-height:13px"><span>    </span><br></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(186,45,162)"><span>    </span><span>associatedtype</span><span> Request</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;min-height:13px"><span>    </span><br></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(0,132,0)"><span>    </span><span>// Thrown exceptions terminate decoding</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;min-height:13px"><span>    </span><br></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(0,132,0)"><span>    </span><span>// Called before any other methods</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span>    </span><span style="color:#ba2da2">mutating</span><span> </span><span style="color:#ba2da2">func</span><span> decodeMethod(</span><span style="color:#ba2da2">_</span><span> value: </span><span style="color:#703daa">String</span><span>) </span><span style="color:#ba2da2">throws</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;min-height:13px"><span>    </span><br></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(0,132,0)"><span>    </span><span>// 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"><span>    </span><span style="color:#ba2da2">mutating</span><span> </span><span style="color:#ba2da2">func</span><span> decodePath(</span><span style="color:#ba2da2">_</span><span> iterator: </span><span style="color:#ba2da2">inout</span><span> </span><span style="color:#4f8187">PathIterator</span><span>) </span><span style="color:#ba2da2">throws</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;min-height:13px"><span>    </span><br></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(0,132,0)"><span>    </span><span>// 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"><span>    </span><span style="color:#ba2da2">mutating</span><span> </span><span style="color:#ba2da2">func</span><span> decodeVersion(</span><span style="color:#ba2da2">_</span><span> value: </span><span style="color:#703daa">String</span><span>) </span><span style="color:#ba2da2">throws</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;min-height:13px"><span>    </span><br></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(0,132,0)"><span>    </span><span>// 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"><span>    </span><span style="color:#ba2da2">mutating</span><span> </span><span style="color:#ba2da2">func</span><span> decodeHeader(</span><span style="color:#ba2da2">_</span><span> key: </span><span style="color:#703daa">String</span><span>, value: </span><span style="color:#703daa">String</span><span>) </span><span style="color:#ba2da2">throws</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;min-height:13px"><span>    </span><br></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(0,132,0)"><span>    </span><span>// HTTPBodyDecoder methods conceptually appear here</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;min-height:13px"><span>    </span><br></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(0,132,0)"><span>    </span><span>// 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"><span>    </span><span style="color:#ba2da2">func</span><span> decoded() </span><span style="color:#ba2da2">throws</span><span> -&gt; Request</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;min-height:13px"><span>    </span><br></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span>}</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;min-height:13px"><span></span><br></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(79,129,135)"><span style="color:#ba2da2">public</span><span> </span><span style="color:#ba2da2">extension</span><span> </span><span>HTTPRequestDecoder</span><span> {</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;min-height:13px"><span>    </span><br></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span>    </span><span style="color:#ba2da2">mutating</span><span> </span><span style="color:#ba2da2">func</span><span> decodeVersion(</span><span style="color:#ba2da2">_</span><span> value: </span><span style="color:#703daa">String</span><span>) </span><span style="color:#ba2da2">throws</span><span> {</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(0,132,0)"><span>        </span><span>// Implementing decodeVersion is not necessary</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span>    }</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;min-height:13px"><span>    </span><br></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span>}</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;min-height:13px"><span></span><br></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(0,132,0)"><span>/**</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(0,132,0)"><span> 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)"><span> */</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span style="color:#ba2da2">public</span><span> </span><span style="color:#ba2da2">protocol</span><span> HTTPResponse {</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span>    </span><span style="color:#ba2da2">var</span><span> statusCode: </span><span style="color:#703daa">UInt</span><span> { </span><span style="color:#ba2da2">get</span><span> }</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span>    </span><span style="color:#ba2da2">var</span><span> headers: [</span><span style="color:#4f8187">HTTPHeader</span><span>] { </span><span style="color:#ba2da2">get</span><span> }</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span>    </span><span style="color:#ba2da2">var</span><span> body: </span><span style="color:#4f8187">HTTPBody</span><span> { </span><span style="color:#ba2da2">get</span><span> }</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span>}</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;min-height:13px"><span></span><br></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(0,132,0)"><span>// MARK: - Client API</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;min-height:13px"><span></span><br></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(0,132,0)"><span>/**</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(0,132,0)"><span> 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)"><span> */</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span style="color:#ba2da2">public</span><span> </span><span style="color:#ba2da2">protocol</span><span> HTTPRequest {</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span>    </span><span style="color:#ba2da2">var</span><span> method: </span><span style="color:#703daa">String</span><span> { </span><span style="color:#ba2da2">get</span><span> }</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;min-height:13px"><span>    </span><br></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(0,132,0)"><span>    </span><span>// 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"><span>    </span><span style="color:#ba2da2">var</span><span> path: </span><span style="color:#703daa">String</span><span> { </span><span style="color:#ba2da2">get</span><span> }</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span>    </span><span style="color:#ba2da2">var</span><span> headers: [</span><span style="color:#4f8187">HTTPHeader</span><span>] { </span><span style="color:#ba2da2">get</span><span> }</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span>    </span><span style="color:#ba2da2">var</span><span> body: </span><span style="color:#4f8187">HTTPBody</span><span> { </span><span style="color:#ba2da2">get</span><span> }</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span>}</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;min-height:13px"><span></span><br></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span style="color:#ba2da2">public</span><span> </span><span style="color:#ba2da2">protocol</span><span> HTTPResponseDecoder: </span><span style="color:#4f8187">HTTPBodyDecoder</span><span> {</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;min-height:13px"><span>    </span><br></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(186,45,162)"><span>    </span><span>associatedtype</span><span> Response</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;min-height:13px"><span>    </span><br></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(0,132,0)"><span>    </span><span>// Thrown exceptions terminate decoding</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;min-height:13px"><span>    </span><br></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(0,132,0)"><span>    </span><span>// Called before any other methods</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span>    </span><span style="color:#ba2da2">mutating</span><span> </span><span style="color:#ba2da2">func</span><span> decodeVersion(</span><span style="color:#ba2da2">_</span><span> value: </span><span style="color:#703daa">String</span><span>) </span><span style="color:#ba2da2">throws</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;min-height:13px"><span>    </span><br></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(0,132,0)"><span>    </span><span>// Called after `decodeVersion` and before any other methods</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span>    </span><span style="color:#ba2da2">mutating</span><span> </span><span style="color:#ba2da2">func</span><span> decodeStatus(</span><span style="color:#ba2da2">_</span><span> value: </span><span style="color:#703daa">UInt</span><span>) </span><span style="color:#ba2da2">throws</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;min-height:13px"><span>    </span><br></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(0,132,0)"><span>    </span><span>// Called after `decodeVersion` and before any methods below</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span>    </span><span style="color:#ba2da2">mutating</span><span> </span><span style="color:#ba2da2">func</span><span> decodeStatusText(</span><span style="color:#ba2da2">_</span><span> value: </span><span style="color:#703daa">String</span><span>) </span><span style="color:#ba2da2">throws</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;min-height:13px"><span>    </span><br></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(0,132,0)"><span>    </span><span>// 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"><span>    </span><span style="color:#ba2da2">mutating</span><span> </span><span style="color:#ba2da2">func</span><span> decodeHeader(</span><span style="color:#ba2da2">_</span><span> key: </span><span style="color:#703daa">String</span><span>, value: </span><span style="color:#703daa">String</span><span>) </span><span style="color:#ba2da2">throws</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;min-height:13px"><span>    </span><br></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(0,132,0)"><span>    </span><span>// HTTPBodyDecoder methods conceptually appear here</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;min-height:13px"><span>    </span><br></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(0,132,0)"><span>    </span><span>// 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"><span>    </span><span style="color:#ba2da2">func</span><span> decoded() </span><span style="color:#ba2da2">throws</span><span> -&gt; Response</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;min-height:13px"><span>    </span><br></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span>}</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;min-height:13px"><span></span><br></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(0,132,0)"><span>// MARK: - Implementing concrete types.</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;min-height:13px"><span></span><br></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(0,132,0)"><span>// 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"><span></span><br></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(0,132,0)"><span>// 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"><span></span><br></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span style="color:#ba2da2">public</span><span> </span><span style="color:#ba2da2">enum</span><span> GeorgeMethod: </span><span style="color:#703daa">String</span><span> {</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span>    </span><span style="color:#ba2da2">case</span><span> get, post</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span>}</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;min-height:13px"><span></span><br></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span style="color:#ba2da2">public</span><span> </span><span style="color:#ba2da2">struct</span><span> GeorgeHTTPRequest {</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;min-height:13px"><span>    </span><br></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span>    </span><span style="color:#ba2da2">let</span><span> status: </span><span style="color:#703daa">GeorgeStatus</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;min-height:13px"><span>    </span><br></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(0,132,0)"><span>    </span><span>// Implementation omitted</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;min-height:13px"><span>    </span><br></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span>}</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;min-height:13px"><span></span><br></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(0,132,0)"><span>// 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"><span style="color:#ba2da2">public</span><span> </span><span style="color:#ba2da2">struct</span><span> GeorgeHTTPRequestDecoder: </span><span style="color:#4f8187">HTTPRequestDecoder</span><span> {</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;min-height:13px"><span>    </span><br></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(0,132,0)"><span>    </span><span>// Implementation omitted</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;min-height:13px"><span>    </span><br></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span>    </span><span style="color:#ba2da2">func</span><span> decode() </span><span style="color:#ba2da2">throws</span><span> -&gt; </span><span style="color:#4f8187">GeorgeHTTPRequest</span><span> {</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span>        </span><span style="color:#3e1e81">fatalError</span><span>()</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span>    }</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;min-height:13px"><span>    </span><br></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span>}</span></div><div><span><br></span></div><div><span><br></span></div><div><blockquote type="cite"><div>On Apr 9, 2017, at 4:50 AM, Helge Heß via swift-server-dev &lt;<a>swift-server-dev@swift.org</a>&gt; wrote:</div><br><div><div>Hi,<br><br>On 9. Apr 2017, at 04:12, George Leontiev via swift-server-dev &lt;<a>swift-server-dev@swift.org</a>&gt; wrote:<br><blockquote type="cite">Earlier in this thread, I think someone dismissed protocols/generics because “at some point, we need to implement a concrete type”.<br></blockquote><br>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><br><br><blockquote type="cite">I think we can leverage the Swift type system to have something much better.<br><br>I recommend having protocols for HTTP{Request,Response}{Encodab<wbr>le,Decoder} types<br></blockquote><br>&lt;snip&gt; protocols &lt;snip&gt;<br><br>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><br>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><br>  protocol HTTPRequestEncodable {<br>    func encodeAsHTTPRequest() -&gt; HTTPRequest<br>  }<br><br>which may still be useful. But extra indirections :-)<br><br><br>Your `HTTPRequestDecoder` is like a parser delegate, kinda like the peer to the `HTTPResponseWriter`. Could be done like this.<br>Though if you provide an “Encodable” I suppose the assumption would be that you also provide a “Decodable” too ...<br><br><br><blockquote type="cite">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></blockquote><br>A complete proposal like Johannes’ would be more useful. Or maybe even better a revision of Johannes’ proposal with your ideas in it.<br><br><br><blockquote type="cite">There is a performance overhead to using protocols/generics, but I believe these can be effectively mediated by the compiler.<br></blockquote><br>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><br>hh<br><br>______________________________<wbr>_________________<br>swift-server-dev mailing list<br><a>swift-server-dev@swift.org</a><br><a href="https://lists.swift.org/mailman/listinfo/swift-server-dev" target="_blank">https://lists.swift.org/mailma<wbr>n/listinfo/swift-server-dev</a><br></div></div></blockquote></div><br></div></div></blockquote></div>
</div>
</div></blockquote></div><br></div></div></div></blockquote>