Skip to content

Commit 294037a

Browse files
committed
improve architecture
1 parent cb139df commit 294037a

22 files changed

+238
-202
lines changed

Aeon Tests/AeonTests.swift

+1-15
Original file line numberDiff line numberDiff line change
@@ -26,19 +26,5 @@ import XCTest
2626
import Aeon
2727

2828
class AeonTests: XCTestCase {
29-
func testExample() {
30-
func respond(request: HTTPRequest, completion: HTTPResponse -> Void) {
31-
completion(
32-
HTTPResponse(
33-
statusCode: 200,
34-
reasonPhrase: "OK",
35-
headers: [:],
36-
body: []
37-
)
38-
)
39-
}
40-
41-
let server = HTTPServer(port: 8080, respond: respond)
42-
server.start()
43-
}
29+
func testExample() {}
4430
}

Aeon.xcodeproj/project.pbxproj

+111-103
Large diffs are not rendered by default.

Aeon/HTTPParser.swift

+2-2
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@
2222
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
2323
// SOFTWARE.
2424

25-
struct HTTPParser : HTTPRequestParserType {
26-
func parseRequest(client: StreamType, completion: (request: HTTPRequest?, error: ErrorType?) -> Void) {
25+
public struct HTTPParser : HTTPRequestParserType {
26+
public func parseRequest(client: TCPStreamType, completion: (request: HTTPRequest?, error: ErrorType?) -> Void) {
2727
let parser = HTTPRequestParser { request in
2828
completion(request: request, error: nil)
2929
}

Aeon/HTTPSerializer.swift

+2-2
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@
2222
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
2323
// SOFTWARE.
2424

25-
struct HTTPSerializer : HTTPResponseSerializerType {
26-
func serializeResponse(client: StreamType, response: HTTPResponse, completion: (error: ErrorType?) -> Void) {
25+
public struct HTTPSerializer : HTTPResponseSerializerType {
26+
public func serializeResponse(client: TCPStreamType, response: HTTPResponse, completion: (error: ErrorType?) -> Void) {
2727
var string = "HTTP/\(response.majorVersion).\(response.minorVersion) \(response.statusCode) \(response.reasonPhrase)\r\n"
2828

2929
for (name, value) in response.headers {

Aeon/HTTPServer.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
// SOFTWARE.
2424

2525
public struct HTTPServer : HTTPServerType {
26-
public let server: ServerType
26+
public let server: TCPServerType
2727
public let parser: HTTPRequestParserType = HTTPParser()
2828
public let responder: HTTPResponderType
2929
public let serializer: HTTPResponseSerializerType = HTTPSerializer()

Aeon/TCPServer.swift

+2-2
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,11 @@
2222
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
2323
// SOFTWARE.
2424

25-
struct TCPServer : ServerType {
25+
struct TCPServer : TCPServerType {
2626
let port: Int
2727
let semaphore = Semaphore(resourceCount: 0)
2828

29-
func acceptClient(completion: (stream: StreamType?, error: ErrorType?) -> Void) {
29+
func acceptClient(completion: (stream: TCPStreamType?, error: ErrorType?) -> Void) {
3030
do {
3131
let ip = try IP(port: port)
3232
let socket = try TCPServerSocket(ip: ip, backlog: 128)

Aeon/TCPStream.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
2323
// SOFTWARE.
2424

25-
final class TCPStream : StreamType {
25+
final class TCPStream : TCPStreamType {
2626
let socket: TCPClientSocket
2727
let channel: IOChannel
2828

Luminescence/Method/HTTPMethod.swift renamed to HTTPRequest/HTTPMethod.swift

+1-42
Original file line numberDiff line numberDiff line change
@@ -66,47 +66,6 @@ public enum HTTPMethod {
6666
case UNKNOWN
6767
}
6868

69-
extension HTTPMethod {
70-
init(code: Int) {
71-
switch code {
72-
case 00: self = DELETE
73-
case 01: self = GET
74-
case 02: self = HEAD
75-
case 03: self = POST
76-
case 04: self = PUT
77-
case 05: self = CONNECT
78-
case 06: self = OPTIONS
79-
case 07: self = TRACE
80-
case 08: self = COPY
81-
case 09: self = LOCK
82-
case 10: self = MKCOL
83-
case 11: self = MOVE
84-
case 12: self = PROPFIND
85-
case 13: self = PROPPATCH
86-
case 14: self = SEARCH
87-
case 15: self = UNLOCK
88-
case 16: self = BIND
89-
case 17: self = REBIND
90-
case 18: self = UNBIND
91-
case 19: self = ACL
92-
case 20: self = REPORT
93-
case 21: self = MKACTIVITY
94-
case 22: self = CHECKOUT
95-
case 23: self = MERGE
96-
case 24: self = MSEARCH
97-
case 25: self = NOTIFY
98-
case 26: self = SUBSCRIBE
99-
case 27: self = UNSUBSCRIBE
100-
case 28: self = PATCH
101-
case 29: self = PURGE
102-
case 30: self = MKCALENDAR
103-
case 31: self = LINK
104-
case 32: self = UNLINK
105-
default: self = UNKNOWN
106-
}
107-
}
108-
}
109-
11069
extension HTTPMethod : CustomStringConvertible {
11170
public var description: String {
11271
switch self {
@@ -146,4 +105,4 @@ extension HTTPMethod : CustomStringConvertible {
146105
default: return "UNKNOWN"
147106
}
148107
}
149-
}
108+
}

HTTPServerType/HTTPResponderType.swift renamed to HTTPResponderType/HTTPResponderType.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// ResponderType.swift
1+
// HTTPResponderType.swift
22
//
33
// The MIT License (MIT)
44
//

HTTPServerType/HTTPRequestParserType.swift

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// RequestParserType.swift
1+
// HTTPRequestParserType.swift
22
//
33
// The MIT License (MIT)
44
//
@@ -23,5 +23,5 @@
2323
// SOFTWARE.
2424

2525
public protocol HTTPRequestParserType {
26-
func parseRequest(client: StreamType, completion: (request: HTTPRequest?, error: ErrorType?) -> Void)
26+
func parseRequest(client: TCPStreamType, completion: (request: HTTPRequest?, error: ErrorType?) -> Void)
2727
}

HTTPServerType/HTTPResponseSerializerType.swift

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// ResponseSerializerType.swift
1+
// HTTPResponseSerializerType.swift
22
//
33
// The MIT License (MIT)
44
//
@@ -23,5 +23,5 @@
2323
// SOFTWARE.
2424

2525
public protocol HTTPResponseSerializerType {
26-
func serializeResponse(client: StreamType, response: HTTPResponse, completion: (error: ErrorType?) -> Void)
26+
func serializeResponse(client: TCPStreamType, response: HTTPResponse, completion: (error: ErrorType?) -> Void)
2727
}

HTTPServerType/HTTPServerType.swift

+3-7
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,10 @@
2323
// SOFTWARE.
2424

2525
public protocol HTTPServerType {
26-
var server: ServerType { get }
26+
var server: TCPServerType { get }
2727
var parser: HTTPRequestParserType { get }
2828
var responder: HTTPResponderType { get }
29-
var serializer: HTTPResponseSerializerType { get }
29+
var serializer: HTTPResponseSerializerType { get }
3030
}
3131

3232
extension HTTPServerType {
@@ -46,7 +46,7 @@ extension HTTPServerType {
4646
failure(error)
4747
client.close()
4848
} else {
49-
if !self.keepAlive(request) {
49+
if !request.keepAlive {
5050
client.close()
5151
}
5252
}
@@ -61,10 +61,6 @@ extension HTTPServerType {
6161
public func stop() {
6262
server.stop()
6363
}
64-
65-
private func keepAlive(request: HTTPRequest) -> Bool {
66-
return request.keepAlive
67-
}
6864

6965
private static func defaultFailureHandler(error: ErrorType) -> Void {
7066
print("Error: \(error)")

HTTPServerType/TCPServerType.swift

+2-2
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
2323
// SOFTWARE.
2424

25-
public protocol ServerType {
26-
func acceptClient(completion: (stream: StreamType?, error: ErrorType?) -> Void)
25+
public protocol TCPServerType {
26+
func acceptClient(completion: (stream: TCPStreamType?, error: ErrorType?) -> Void)
2727
func stop()
2828
}

HTTPServerType/TCPStreamType.swift

+2-2
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@
2222
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
2323
// SOFTWARE.
2424

25-
public protocol StreamType {
25+
public protocol TCPStreamType {
2626
func close()
2727
func receive(completion: (data: [Int8], error: ErrorType?) -> Void)
2828
func send(data: [Int8], completion: (error: ErrorType?) -> Void)
29-
}
29+
}
+64
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
// HTTPMethod+Luminescence.swift
2+
//
3+
// The MIT License (MIT)
4+
//
5+
// Copyright (c) 2015 Zewo
6+
//
7+
// Permission is hereby granted, free of charge, to any person obtaining a copy
8+
// of this software and associated documentation files (the "Software"), to deal
9+
// in the Software without restriction, including without limitation the rights
10+
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11+
// copies of the Software, and to permit persons to whom the Software is
12+
// furnished to do so, subject to the following conditions:
13+
//
14+
// The above copyright notice and this permission notice shall be included in all
15+
// copies or substantial portions of the Software.
16+
//
17+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18+
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19+
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20+
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21+
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22+
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23+
// SOFTWARE.
24+
25+
extension HTTPMethod {
26+
init(code: Int) {
27+
switch code {
28+
case 00: self = DELETE
29+
case 01: self = GET
30+
case 02: self = HEAD
31+
case 03: self = POST
32+
case 04: self = PUT
33+
case 05: self = CONNECT
34+
case 06: self = OPTIONS
35+
case 07: self = TRACE
36+
case 08: self = COPY
37+
case 09: self = LOCK
38+
case 10: self = MKCOL
39+
case 11: self = MOVE
40+
case 12: self = PROPFIND
41+
case 13: self = PROPPATCH
42+
case 14: self = SEARCH
43+
case 15: self = UNLOCK
44+
case 16: self = BIND
45+
case 17: self = REBIND
46+
case 18: self = UNBIND
47+
case 19: self = ACL
48+
case 20: self = REPORT
49+
case 21: self = MKACTIVITY
50+
case 22: self = CHECKOUT
51+
case 23: self = MERGE
52+
case 24: self = MSEARCH
53+
case 25: self = NOTIFY
54+
case 26: self = SUBSCRIBE
55+
case 27: self = UNSUBSCRIBE
56+
case 28: self = PATCH
57+
case 29: self = PURGE
58+
case 30: self = MKCALENDAR
59+
case 31: self = LINK
60+
case 32: self = UNLINK
61+
default: self = UNKNOWN
62+
}
63+
}
64+
}

Luminescence/URI/URI.swift renamed to Luminescence/URI+Luminescence.swift

+3-18
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// URI.swift
1+
// URI+Luminescence.swift
22
//
33
// The MIT License (MIT)
44
//
@@ -24,21 +24,6 @@
2424

2525
import Incandescence
2626

27-
public struct URIUserInfo {
28-
public let username: String
29-
public let password: String
30-
}
31-
32-
public struct URI {
33-
public let scheme: String?
34-
public let userInfo: URIUserInfo?
35-
public let host: String?
36-
public let port: Int?
37-
public let path: String?
38-
public let query: [String: String]
39-
public let fragment: String?
40-
}
41-
4227
extension URI {
4328
public init(string: String) {
4429
let u = parse_uri(string)
@@ -92,9 +77,9 @@ extension URI {
9277
return string[string.startIndex.advancedBy(Int(start)) ..< string.startIndex.advancedBy(Int(end))]
9378
}
9479

95-
@inline(__always) private static func parseUserInfoString(userInfoString: String) -> URIUserInfo? {
80+
@inline(__always) private static func parseUserInfoString(userInfoString: String) -> URI.UserInfo? {
9681
let userInfoElements = userInfoString.characters.split{$0 == ":"}.map(String.init)
97-
return URIUserInfo(
82+
return URI.UserInfo(
9883
username: userInfoElements[0],
9984
password: userInfoElements[1]
10085
)

URI/URI.swift

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
// URI.swift
2+
//
3+
// The MIT License (MIT)
4+
//
5+
// Copyright (c) 2015 Zewo
6+
//
7+
// Permission is hereby granted, free of charge, to any person obtaining a copy
8+
// of this software and associated documentation files (the "Software"), to deal
9+
// in the Software without restriction, including without limitation the rights
10+
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11+
// copies of the Software, and to permit persons to whom the Software is
12+
// furnished to do so, subject to the following conditions:
13+
//
14+
// The above copyright notice and this permission notice shall be included in all
15+
// copies or substantial portions of the Software.
16+
//
17+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18+
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19+
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20+
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21+
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22+
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23+
// SOFTWARE.
24+
25+
public struct URI {
26+
public struct UserInfo {
27+
public let username: String
28+
public let password: String
29+
}
30+
31+
public let scheme: String?
32+
public let userInfo: UserInfo?
33+
public let host: String?
34+
public let port: Int?
35+
public let path: String?
36+
public let query: [String : String]
37+
public let fragment: String?
38+
}

0 commit comments

Comments
 (0)