|
1 | | -# SwiftServerHttp |
| 1 | +# Swift Server Project HTTP APIs |
2 | 2 |
|
3 | | -Sample prototype implementation of @weissi's HTTP Sketch v2 from https://lists.swift.org/pipermail/swift-server-dev/Week-of-Mon-20170403/000422.html for discussion. |
| 3 | +This is an early implementation of the Swift Server Project's HTTP APIs. This provides simple HTTP server on which rich web application frameworks can be built. |
| 4 | + |
| 5 | +## Getting Started |
| 6 | + |
| 7 | + |
| 8 | +### Hello World |
| 9 | +The following code implements a very simple "Hello World!" server: |
| 10 | + |
| 11 | +```swift |
| 12 | +import Foundation |
| 13 | +import HTTP |
| 14 | + |
| 15 | +func hello(request: HTTPRequest, response: HTTPResponseWriter ) -> HTTPBodyProcessing { |
| 16 | + response.writeHeader(status: .ok) |
| 17 | + response.writeBody("Hello, World!") |
| 18 | + response.done() |
| 19 | + return .discardBody |
| 20 | +} |
| 21 | + |
| 22 | +let server = HTTPServer() |
| 23 | +try! server.start(port: 8080, handler: hello) |
| 24 | + |
| 25 | +CFRunLoopRun() |
| 26 | +``` |
| 27 | + |
| 28 | +The `hello()` function receives a `HTTPRequest` that describes the request and a `HTTPResponseWriter` used to write a response. |
| 29 | + |
| 30 | +Data that is received as part of the request body is made available to the closure that is returned by the `hello()` function. In the "Hello World!" example the request body is not used, so `.discardBody` is returned. |
| 31 | + |
| 32 | +### Echo Server |
| 33 | +The following code implements a very simple Echo server that responds with the contents of the incoming request: |
| 34 | + |
| 35 | +```swift |
| 36 | +import Foundation |
| 37 | +import HTTP |
| 38 | + |
| 39 | +func echo(request: HTTPRequest, response: HTTPResponseWriter ) -> HTTPBodyProcessing { |
| 40 | + response.writeHeader(status: .ok) |
| 41 | + return .processBody { (chunk, stop) in |
| 42 | + switch chunk { |
| 43 | + case .chunk(let data, let finishedProcessing): |
| 44 | + response.writeBody(data) { _ in |
| 45 | + finishedProcessing() |
| 46 | + } |
| 47 | + case .end: |
| 48 | + response.done() |
| 49 | + default: |
| 50 | + stop = true |
| 51 | + response.abort() |
| 52 | + } |
| 53 | + } |
| 54 | +} |
| 55 | + |
| 56 | +let server = HTTPServer() |
| 57 | +try! server.start(port: 8080, handler: echo) |
| 58 | + |
| 59 | +CFRunLoopRun() |
| 60 | +``` |
| 61 | +As the Echo server needs to process the request body data and return it in the reponse, the `echo()` function returns a `.processBody` closure. This closure is called with `.chunk` when data is available for processing from the request, and `.end` when no more data is available. |
| 62 | + |
| 63 | +Once any data chunk has been processed, `finishedProcessing()` should be called to signify that it has been handled. |
| 64 | + |
| 65 | +When the response is complete, `response.done()` should be called. |
| 66 | + |
| 67 | +## API Documentation |
| 68 | +Full Jazzy documentation of the API is available here: |
| 69 | +<https://swift-server.github.io/http/> |
| 70 | + |
| 71 | + |
| 72 | +## Acknowledgements |
| 73 | +This project is based on an inital proposal from @weissi on the swift-server-dev mailing list: |
| 74 | +<https://lists.swift.org/pipermail/swift-server-dev/Week-of-Mon-20170403/000422.html> |
0 commit comments