Skip to content

Commit f93c22d

Browse files
“parse” error replaced by “httpError”.
Added sample handler for HTTP POST method.
1 parent 9affc5b commit f93c22d

4 files changed

Lines changed: 23 additions & 10 deletions

File tree

Sources/Error.swift

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import Foundation
99

1010
public enum SwifterError: Error {
1111

12-
case parse(String)
1312
case async(String)
1413
case socketCreation(String)
1514
case setReUseAddr(String)

Sources/Http+Misc.swift

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,12 @@
11
//
2-
// HttpPost.swift
2+
// Http+Misc.swift
33
// Swifter
44
//
5-
// Created by Damian Kolakowski on 24/02/2017.
65
// Copyright © 2017 Damian Kołakowski. All rights reserved.
76
//
87

98
import Foundation
109

11-
1210
extension Request {
1311

1412
public func hasToken(_ token: String, forHeader headerName: String) -> Bool {

Sources/Http.swift

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ public class HttpIncomingDataPorcessor: Hashable, IncomingDataProcessor {
114114
case .waitingForHeaders:
115115

116116
guard self.buffer.count + chunk.count < 4096 else {
117-
throw SwifterError.parse("Headers size exceeds the limit.")
117+
throw SwifterError.httpError("Headers size exceeds the limit.")
118118
}
119119

120120
var iterator = chunk.makeIterator()
@@ -137,7 +137,7 @@ public class HttpIncomingDataPorcessor: Hashable, IncomingDataProcessor {
137137
case .waitingForBody:
138138

139139
guard self.request.body.count + chunk.count <= request.contentLength else {
140-
throw SwifterError.parse("Peer sent more data then required ('Content-Length' = \(request.contentLength).")
140+
throw SwifterError.httpError("Peer sent more data then required ('Content-Length' = \(request.contentLength).")
141141
}
142142

143143
request.body.append(contentsOf: chunk)
@@ -170,7 +170,7 @@ public class HttpIncomingDataPorcessor: Hashable, IncomingDataProcessor {
170170
} else if requestLineTokens[2] == [0x48, 0x54, 0x54, 0x50, 0x2f, 0x31, 0x2e, 0x31] {
171171
request.httpVersion = .http11
172172
} else {
173-
throw SwifterError.parse("Invalid http version: \(requestLineTokens[2])")
173+
throw SwifterError.httpError("Invalid http version: \(requestLineTokens[2])")
174174
}
175175

176176
request.headers = lines
@@ -190,19 +190,19 @@ public class HttpIncomingDataPorcessor: Hashable, IncomingDataProcessor {
190190
.filter({ $0.0 == "content-length" })
191191
.first {
192192
guard let contentLength = Int(value) else {
193-
throw SwifterError.parse("Invalid 'Content-Length' header value \(value).")
193+
throw SwifterError.httpError("Invalid 'Content-Length' header value \(value).")
194194
}
195195
request.contentLength = contentLength
196196
}
197197

198198
guard let method = String(bytes: requestLineTokens[0], encoding: .ascii) else {
199-
throw SwifterError.parse("Invalid 'method' value \(requestLineTokens[0]).")
199+
throw SwifterError.httpError("Invalid 'method' value \(requestLineTokens[0]).")
200200
}
201201

202202
request.method = method
203203

204204
guard let path = String(bytes: requestLineTokens[1], encoding: .ascii) else {
205-
throw SwifterError.parse("Invalid 'path' value \(requestLineTokens[1]).")
205+
throw SwifterError.httpError("Invalid 'path' value \(requestLineTokens[1]).")
206206
}
207207

208208
let queryComponents = path.components(separatedBy: "?")

XCode/SwifterSampleOSX/main.swift

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,22 @@ server.get("/background") { _, _, closure in
4444

4545
}
4646

47+
server.post("/post") { _, request, responder in
48+
49+
let post = request.parseUrlencodedForm()
50+
51+
responder(html(200) {
52+
"body" ~ {
53+
"h4" ~ "You sent: "
54+
"ul" ~ {
55+
post.forEach { item in
56+
"li" ~ "\(item.0) -> \(item.1)"
57+
}
58+
}
59+
}
60+
})
61+
}
62+
4763
while true {
4864
try server.loop()
4965
}

0 commit comments

Comments
 (0)