Skip to content

Commit 055a757

Browse files
authored
Update README.md and Jazzy annotations (swift-server#43)
1 parent 6b5b27e commit 055a757

File tree

7 files changed

+84
-2
lines changed

7 files changed

+84
-2
lines changed

.jazzy.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ custom_categories:
1818

1919
- name: HTTP Server
2020
children:
21+
- HTTPServer
22+
- HTTPServing
2123
- HTTPRequestHandling
2224
- HTTPRequestHandler
2325

README.md

Lines changed: 73 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,74 @@
1-
# SwiftServerHttp
1+
# Swift Server Project HTTP APIs
22

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>

Sources/HTTP/HTTPCommon.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import Foundation
1212
/// - Parameter req: the incoming HTTP request.
1313
/// - Parameter res: a writer providing functions to create an HTTP reponse to the request.
1414
/// - Returns HTTPBodyProcessing: a enum that either discards the request data, or provides a closure to process it.
15+
/// - See: `HTTPRequestHandling` for more information
1516
public typealias HTTPRequestHandler = (HTTPRequest, HTTPResponseWriter) -> HTTPBodyProcessing
1617

1718
/// Class protocol containing the HTTPRequestHandler that responds to the incoming HTTP requests.

Sources/HTTP/HTTPServer.swift

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,21 +28,26 @@ public protocol HTTPServing : class {
2828
public class HTTPServer: HTTPServing {
2929
private let server = PoCSocketSimpleServer()
3030

31+
/// Create an instance of the server. This needs to be followed with a call to `start()`
3132
public init() {
3233
}
3334

35+
/// Start the HTTP server on the given `port`, using `handler` to process incoming requests
3436
public func start(port: Int = 0, handler: @escaping HTTPRequestHandler) throws {
3537
try server.start(handler: handler)
3638
}
3739

40+
/// Stop the server
3841
public func stop() {
3942
server.stop()
4043
}
4144

45+
/// The port the server is listening on
4246
public var port: Int {
4347
return server.port
4448
}
4549

50+
/// The number of current connections
4651
public var connectionCount: Int {
4752
return server.connectionCount
4853
}

Sources/HTTP/PoCSocket/PoCSocket.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import Foundation
1010
import Dispatch
1111

12+
///:nodoc:
1213
public enum PoCSocketError: Error {
1314
case SocketOSError(errno: Int32)
1415
case InvalidSocketError

Sources/HTTP/PoCSocket/PoCSocketConnectionListener.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import Foundation
1010
import Dispatch
1111

12+
///:nodoc:
1213
public class PoCSocketConnectionListener: ParserConnecting {
1314

1415
///socket(2) wrapper object

Sources/HTTP/PoCSocket/PoCSocketSimpleServer.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import Foundation
1313
// MARK: Server
1414

1515
/// An HTTP server that listens for connections on a TCP socket and spawns Listeners to handle them.
16+
///:nodoc:
1617
public class PoCSocketSimpleServer: CurrentConnectionCounting {
1718
/// PoCSocket to listen on for connections
1819
private let serverSocket: PoCSocket = PoCSocket()

0 commit comments

Comments
 (0)