Skip to content

Commit c11dbd1

Browse files
mtgtomichaelenger
andauthored
Adds response body to any http response (#476)
Co-authored-by: Michael Enger <[email protected]>
1 parent c62e382 commit c11dbd1

File tree

4 files changed

+12
-13
lines changed

4 files changed

+12
-13
lines changed

CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,12 @@ All notable changes to this project will be documented in this file. Changes not
1919
# [Unreleased]
2020

2121
## Added
22+
2223
- Add custom headers into .ok HttpResponse. ([#500](https://github.com/httpswift/swifter/pull/500) by [@yuri-qualtie](https://github.com/yuri-qualtie)
2324
- 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)
2425
- Set `Content-Type` to HttpBody and Text HttpResponse. ([#474](https://github.com/httpswift/swifter/pull/474)) by [@mtgto](https://github.com/mtgto)
25-
2626
- 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)
27+
- Adds response body to any http response. ([#476](https://github.com/httpswift/swifter/pull/476) by [@mtgto](https://github.com/mtgto)
2728

2829
## Fixed
2930

Xcode/Sources/Files.swift

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,14 @@ public func shareFile(_ path: String) -> ((HttpRequest) -> HttpResponse) {
2222
file.close()
2323
})
2424
}
25-
return .notFound
25+
return .notFound()
2626
}
2727
}
2828

2929
public func shareFilesFromDirectory(_ directoryPath: String, defaults: [String] = ["index.html", "default.html"]) -> ((HttpRequest) -> HttpResponse) {
3030
return { request in
3131
guard let fileRelativePath = request.params.first else {
32-
return .notFound
32+
return .notFound()
3333
}
3434
if fileRelativePath.value.isEmpty {
3535
for path in defaults {
@@ -57,19 +57,19 @@ public func shareFilesFromDirectory(_ directoryPath: String, defaults: [String]
5757
file.close()
5858
})
5959
}
60-
return .notFound
60+
return .notFound()
6161
}
6262
}
6363

6464
public func directoryBrowser(_ dir: String) -> ((HttpRequest) -> HttpResponse) {
6565
return { request in
6666
guard let (_, value) = request.params.first else {
67-
return HttpResponse.notFound
67+
return .notFound()
6868
}
6969
let filePath = dir + String.pathSeparator + value
7070
do {
7171
guard try filePath.exists() else {
72-
return .notFound
72+
return .notFound()
7373
}
7474
if try filePath.directory() {
7575
var files = try filePath.files()
@@ -92,15 +92,15 @@ public func directoryBrowser(_ dir: String) -> ((HttpRequest) -> HttpResponse) {
9292
}(request)
9393
} else {
9494
guard let file = try? filePath.openForReading() else {
95-
return .notFound
95+
return .notFound()
9696
}
9797
return .raw(200, "OK", [:], { writer in
9898
try? writer.write(file)
9999
file.close()
100100
})
101101
}
102102
} catch {
103-
return HttpResponse.internalServerError
103+
return HttpResponse.internalServerError(.text("Internal Server Error"))
104104
}
105105
}
106106
}

Xcode/Sources/HttpResponse.swift

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -83,9 +83,7 @@ public enum HttpResponse {
8383
case ok(HttpResponseBody, [String: String] = [:]), created, accepted
8484
case movedPermanently(String)
8585
case movedTemporarily(String)
86-
case badRequest(HttpResponseBody?), unauthorized, forbidden, notFound, notAcceptable
87-
case tooManyRequests
88-
case internalServerError
86+
case badRequest(HttpResponseBody?), unauthorized(HttpResponseBody?), forbidden(HttpResponseBody?), notFound(HttpResponseBody? = nil), notAcceptable(HttpResponseBody?), tooManyRequests(HttpResponseBody?), internalServerError(HttpResponseBody?)
8987
case raw(Int, String, [String: String]?, ((HttpResponseBodyWriter) throws -> Void)? )
9088

9189
public var statusCode: Int {
@@ -162,7 +160,7 @@ public enum HttpResponse {
162160
func content() -> (length: Int, write: ((HttpResponseBodyWriter) throws -> Void)?) {
163161
switch self {
164162
case .ok(let body, _) : return body.content()
165-
case .badRequest(let body) : return body?.content() ?? (-1, nil)
163+
case .badRequest(let body), .unauthorized(let body), .forbidden(let body), .notFound(let body), .tooManyRequests(let body), .internalServerError(let body) : return body?.content() ?? (-1, nil)
166164
case .raw(_, _, _, let writer) : return (-1, writer)
167165
default : return (-1, nil)
168166
}

Xcode/Sources/HttpServerIO.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ open class HttpServerIO {
112112
}
113113

114114
open func dispatch(_ request: HttpRequest) -> ([String: String], (HttpRequest) -> HttpResponse) {
115-
return ([:], { _ in HttpResponse.notFound })
115+
return ([:], { _ in HttpResponse.notFound(nil) })
116116
}
117117

118118
private func handleConnection(_ socket: Socket) {

0 commit comments

Comments
 (0)