Просмотр исходного кода

Adds response body to any http response (#476)

Co-authored-by: Michael Enger <michaelenger@users.noreply.github.com>
mtgto 4 лет назад
Родитель
Сommit
c11dbd1569
4 измененных файлов с 12 добавлено и 13 удалено
  1. 2 1
      CHANGELOG.md
  2. 7 7
      Xcode/Sources/Files.swift
  3. 2 4
      Xcode/Sources/HttpResponse.swift
  4. 1 1
      Xcode/Sources/HttpServerIO.swift

+ 2 - 1
CHANGELOG.md

@@ -19,11 +19,12 @@ All notable changes to this project will be documented in this file. Changes not
 # [Unreleased]
 
 ## Added
+
 - Add custom headers into .ok HttpResponse. ([#500](https://github.com/httpswift/swifter/pull/500) by [@yuri-qualtie](https://github.com/yuri-qualtie)
 - Add support for using `**` as a catch-all at the end of a route. ([#479](https://github.com/httpswift/swifter/pull/479)) by [@michaelenger](https://github.com/michaelenger)
 - Set `Content-Type` to HttpBody and Text HttpResponse. ([#474](https://github.com/httpswift/swifter/pull/474)) by [@mtgto](https://github.com/mtgto)
-
 - The `shareFile` function now sets `Content-Type` and `Content-Length` headers like `shareFilesFromDirectory`. ([#493](https://github.com/httpswift/swifter/pull/493)) by [@jcrate](https://github.com/jcrate)
+- Adds response body to any http response. ([#476](https://github.com/httpswift/swifter/pull/476) by [@mtgto](https://github.com/mtgto)
 
 ## Fixed
 

+ 7 - 7
Xcode/Sources/Files.swift

@@ -22,14 +22,14 @@ public func shareFile(_ path: String) -> ((HttpRequest) -> HttpResponse) {
                 file.close()
             })
         }
-        return .notFound
+        return .notFound()
     }
 }
 
 public func shareFilesFromDirectory(_ directoryPath: String, defaults: [String] = ["index.html", "default.html"]) -> ((HttpRequest) -> HttpResponse) {
     return { request in
         guard let fileRelativePath = request.params.first else {
-            return .notFound
+            return .notFound()
         }
         if fileRelativePath.value.isEmpty {
             for path in defaults {
@@ -57,19 +57,19 @@ public func shareFilesFromDirectory(_ directoryPath: String, defaults: [String]
                 file.close()
             })
         }
-        return .notFound
+        return .notFound()
     }
 }
 
 public func directoryBrowser(_ dir: String) -> ((HttpRequest) -> HttpResponse) {
     return { request in
         guard let (_, value) = request.params.first else {
-            return HttpResponse.notFound
+            return .notFound()
         }
         let filePath = dir + String.pathSeparator + value
         do {
             guard try filePath.exists() else {
-                return .notFound
+                return .notFound()
             }
             if try filePath.directory() {
                 var files = try filePath.files()
@@ -92,7 +92,7 @@ public func directoryBrowser(_ dir: String) -> ((HttpRequest) -> HttpResponse) {
                     }(request)
             } else {
                 guard let file = try? filePath.openForReading() else {
-                    return .notFound
+                    return .notFound()
                 }
                 return .raw(200, "OK", [:], { writer in
                     try? writer.write(file)
@@ -100,7 +100,7 @@ public func directoryBrowser(_ dir: String) -> ((HttpRequest) -> HttpResponse) {
                 })
             }
         } catch {
-            return HttpResponse.internalServerError
+            return HttpResponse.internalServerError(.text("Internal Server Error"))
         }
     }
 }

+ 2 - 4
Xcode/Sources/HttpResponse.swift

@@ -83,9 +83,7 @@ public enum HttpResponse {
     case ok(HttpResponseBody, [String: String] = [:]), created, accepted
     case movedPermanently(String)
     case movedTemporarily(String)
-    case badRequest(HttpResponseBody?), unauthorized, forbidden, notFound, notAcceptable
-    case tooManyRequests
-    case internalServerError
+    case badRequest(HttpResponseBody?), unauthorized(HttpResponseBody?), forbidden(HttpResponseBody?), notFound(HttpResponseBody? = nil), notAcceptable(HttpResponseBody?), tooManyRequests(HttpResponseBody?), internalServerError(HttpResponseBody?)
     case raw(Int, String, [String: String]?, ((HttpResponseBodyWriter) throws -> Void)? )
 
     public var statusCode: Int {
@@ -162,7 +160,7 @@ public enum HttpResponse {
     func content() -> (length: Int, write: ((HttpResponseBodyWriter) throws -> Void)?) {
         switch self {
         case .ok(let body, _)          : return body.content()
-        case .badRequest(let body)     : return body?.content() ?? (-1, nil)
+        case .badRequest(let body), .unauthorized(let body), .forbidden(let body), .notFound(let body), .tooManyRequests(let body), .internalServerError(let body) : return body?.content() ?? (-1, nil)
         case .raw(_, _, _, let writer) : return (-1, writer)
         default                        : return (-1, nil)
         }

+ 1 - 1
Xcode/Sources/HttpServerIO.swift

@@ -112,7 +112,7 @@ open class HttpServerIO {
     }
 
     open func dispatch(_ request: HttpRequest) -> ([String: String], (HttpRequest) -> HttpResponse) {
-        return ([:], { _ in HttpResponse.notFound })
+        return ([:], { _ in HttpResponse.notFound(nil) })
     }
 
     private func handleConnection(_ socket: Socket) {