[swift-server-dev] HTTP API v0.1.0

Vladimir.S svabox at gmail.com
Tue Oct 3 12:55:09 CDT 2017


On 02.10.2017 19:30, Chris Bailey via swift-server-dev wrote:
> We spent some time updating and validating the HTTP API, along with creating a full 
> set of API docs etc. In order to make it easier for the wider community to build 
> applications that use it to test out the API and provide feedback, I've just tagged 
> v0.1.0.

Thank you Chris and others for all of this job, I believe HTTP API is very important 
for Swift future.

Have to say that I'm not a server-side developer, just interested in this subject and 
periodically looking into the swift-server-dev at swift.org mailing list.

If it is possible, I'd like to clarify for myself some points and provide my 
non-authoritative opinion based on my first look on examples of 'Hello World' and 
echo servers.
For me they look a little non-intuitive, not easy to understand what is going on and 
why we need all of this code.

For example, the 'echo' server written in Node.js I googled fast:

var http = require('http');

http.createServer(function(request,response){
	response.writeHead(200);

	request.on('data',function(message){
  		response.write(message);
	});

	request.on('end',function(){
  		response.end();
  	});
}).listen(8080);


All is clear from the first sight.
Is it possible/reasonable to compose the Swift code in similar way, for example 
something like as written below?

func hello(http: HTTPContext) {
	http.response.write(header: .status(.ok))
	http.response.write(body: "Hello, World")
	http.response.end()
	
	http.request.discard()
}

func echo(http: HTTPContext) {
	http.response.write(header: .status(.ok))

	http.request.process { message in
		switch message {
		case .chunk(let data):
			http.response.write(body: data) {
				http.request.nextMessage()
			}

// or this(don't know implementation details):
//		case .chunk(let chunk):
//			http.response.write(body: chunk.data) {
//				chunk.next()
//			}
		
		case .end:
			http.response.end()
		
		default:
			http.response.abort()
			http.request.abort() // notifies that processing finished
		}
	}
}

Sorry if my thoughts are naive/fool but thank you for reply :-)

Vladimir.

> 
> You can see that here:
>          HTTP 0.1.0 https://github.com/swift-server/http/tree/0.1.0
> I've also submitted a PR to add the HTTP API into the set servers/frameworks being 
> tested as part of the "Which is the fastest?" benchmark.
> 
> In parallel, Gelareh Taban has been working on adding providing a TLS API, and HTTPS 
> support to the HTTP project. You can see her incubator work here:
>          HTTPS incubator: https://github.com/gtaban/http
>          TLS incubator:        https://github.com/gtaban/TLSService.git 
> <https://github.com/gtaban/TLSService.git>
> 
> The aim is to validate and include the TLS work in v0.2.0, along with feedback from 
> the wider community on the existing API surface, fixing bugs, and working on 
> implementing outstanding items like trailer support, backpressure, etc.
> 
> Chris
> Unless stated otherwise above:
> IBM United Kingdom Limited - Registered in England and Wales with number 741598.
> Registered office: PO Box 41, North Harbour, Portsmouth, Hampshire PO6 3AU
> 
> 
> _______________________________________________
> swift-server-dev mailing list
> swift-server-dev at swift.org
> https://lists.swift.org/mailman/listinfo/swift-server-dev
> 


More information about the swift-server-dev mailing list