Parcourir la source

“parse” error replaced by “httpError”.
Added sample handler for HTTP POST method.

Damian Kołakowski il y a 9 ans
Parent
commit
f93c22d909
4 fichiers modifiés avec 23 ajouts et 10 suppressions
  1. 0 1
      Sources/Error.swift
  2. 1 3
      Sources/Http+Misc.swift
  3. 6 6
      Sources/Http.swift
  4. 16 0
      XCode/SwifterSampleOSX/main.swift

+ 0 - 1
Sources/Error.swift

@@ -9,7 +9,6 @@ import Foundation
 
 public enum SwifterError: Error {
     
-    case parse(String)
     case async(String)
     case socketCreation(String)
     case setReUseAddr(String)

+ 1 - 3
Sources/Http+Misc.swift

@@ -1,14 +1,12 @@
 //
-//  HttpPost.swift
+//  Http+Misc.swift
 //  Swifter
 //
-//  Created by Damian Kolakowski on 24/02/2017.
 //  Copyright © 2017 Damian Kołakowski. All rights reserved.
 //
 
 import Foundation
 
-
 extension Request {
     
     public func hasToken(_ token: String, forHeader headerName: String) -> Bool {

+ 6 - 6
Sources/Http.swift

@@ -114,7 +114,7 @@ public class HttpIncomingDataPorcessor: Hashable, IncomingDataProcessor {
         case .waitingForHeaders:
             
             guard self.buffer.count + chunk.count < 4096 else {
-                throw SwifterError.parse("Headers size exceeds the limit.")
+                throw SwifterError.httpError("Headers size exceeds the limit.")
             }
             
             var iterator = chunk.makeIterator()
@@ -137,7 +137,7 @@ public class HttpIncomingDataPorcessor: Hashable, IncomingDataProcessor {
         case .waitingForBody:
             
             guard self.request.body.count + chunk.count <= request.contentLength else {
-                throw SwifterError.parse("Peer sent more data then required ('Content-Length' = \(request.contentLength).")
+                throw SwifterError.httpError("Peer sent more data then required ('Content-Length' = \(request.contentLength).")
             }
             
             request.body.append(contentsOf: chunk)
@@ -170,7 +170,7 @@ public class HttpIncomingDataPorcessor: Hashable, IncomingDataProcessor {
         } else if requestLineTokens[2] == [0x48, 0x54,  0x54,  0x50, 0x2f, 0x31, 0x2e, 0x31] {
             request.httpVersion = .http11
         } else {
-            throw SwifterError.parse("Invalid http version: \(requestLineTokens[2])")
+            throw SwifterError.httpError("Invalid http version: \(requestLineTokens[2])")
         }
         
         request.headers = lines
@@ -190,19 +190,19 @@ public class HttpIncomingDataPorcessor: Hashable, IncomingDataProcessor {
             .filter({ $0.0 == "content-length" })
             .first {
             guard let contentLength = Int(value) else {
-                throw SwifterError.parse("Invalid 'Content-Length' header value \(value).")
+                throw SwifterError.httpError("Invalid 'Content-Length' header value \(value).")
             }
             request.contentLength = contentLength
         }
         
         guard let method = String(bytes: requestLineTokens[0], encoding: .ascii) else {
-            throw SwifterError.parse("Invalid 'method' value \(requestLineTokens[0]).")
+            throw SwifterError.httpError("Invalid 'method' value \(requestLineTokens[0]).")
         }
         
         request.method = method
         
         guard let path = String(bytes: requestLineTokens[1], encoding: .ascii) else {
-            throw SwifterError.parse("Invalid 'path' value \(requestLineTokens[1]).")
+            throw SwifterError.httpError("Invalid 'path' value \(requestLineTokens[1]).")
         }
         
         let queryComponents = path.components(separatedBy: "?")

+ 16 - 0
XCode/SwifterSampleOSX/main.swift

@@ -44,6 +44,22 @@ server.get("/background") { _, _, closure in
     
 }
 
+server.post("/post") { _, request, responder in
+    
+    let post = request.parseUrlencodedForm()
+    
+    responder(html(200) {
+        "body" ~ {
+            "h4" ~ "You sent: "
+            "ul" ~ {
+                post.forEach { item in
+                    "li" ~ "\(item.0) -> \(item.1)"
+                }
+            }
+        }
+    })
+}
+
 while true {
     try server.loop()
 }