Skip to content

Commit c9bf600

Browse files
authored
Merge pull request #500 from yuri-qualtie/stable
added custom headers into .ok response
2 parents 01e4421 + d76d004 commit c9bf600

File tree

3 files changed

+12
-8
lines changed

3 files changed

+12
-8
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ All notable changes to this project will be documented in this file. Changes not
1919
# [Unreleased]
2020

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

Xcode/Sources/DemoServer.swift

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ public func demoServer(_ publicDir: String) -> HttpServer {
3030
}
3131
}
3232

33-
server["/magic"] = { .ok(.htmlBody("You asked for " + $0.path)) }
33+
server["/magic"] = { .ok(.htmlBody("You asked for " + $0.path), ["XXX-Custom-Header": "value"]) }
3434

3535
server["/test/:param1/:param2"] = { request in
3636
scopes {
@@ -98,7 +98,7 @@ public func demoServer(_ publicDir: String) -> HttpServer {
9898
guard let name = multipart.name, let fileName = multipart.fileName else { continue }
9999
response += "Name: \(name) File name: \(fileName) Size: \(multipart.body.count)<br>"
100100
}
101-
return HttpResponse.ok(.htmlBody(response))
101+
return HttpResponse.ok(.htmlBody(response), ["XXX-Custom-Header": "value"])
102102
}
103103

104104
server.GET["/login"] = scopes {
@@ -136,7 +136,7 @@ public func demoServer(_ publicDir: String) -> HttpServer {
136136

137137
server.POST["/login"] = { request in
138138
let formFields = request.parseUrlencodedForm()
139-
return HttpResponse.ok(.htmlBody(formFields.map({ "\($0.0) = \($0.1)" }).joined(separator: "<br>")))
139+
return HttpResponse.ok(.htmlBody(formFields.map({ "\($0.0) = \($0.1)" }).joined(separator: "<br>")), ["XXX-Custom-Header": "value"])
140140
}
141141

142142
server["/demo"] = scopes {
@@ -165,11 +165,11 @@ public func demoServer(_ publicDir: String) -> HttpServer {
165165
server["/long"] = { _ in
166166
var longResponse = ""
167167
for index in 0..<1000 { longResponse += "(\(index)),->" }
168-
return .ok(.htmlBody(longResponse))
168+
return .ok(.htmlBody(longResponse), ["XXX-Custom-Header": "value"])
169169
}
170170

171171
server["/wildcard/*/test/*/:param"] = { request in
172-
return .ok(.htmlBody(request.path))
172+
return .ok(.htmlBody(request.path), ["XXX-Custom-Header": "value"])
173173
}
174174

175175
server["/stream"] = { _ in

Xcode/Sources/HttpResponse.swift

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ public enum HttpResponseBody {
8080
public enum HttpResponse {
8181

8282
case switchProtocols([String: String], (Socket) -> Void)
83-
case ok(HttpResponseBody), created, accepted
83+
case ok(HttpResponseBody, [String: String] = [:]), created, accepted
8484
case movedPermanently(String)
8585
case movedTemporarily(String)
8686
case badRequest(HttpResponseBody?), unauthorized, forbidden, notFound, notAcceptable
@@ -133,7 +133,10 @@ public enum HttpResponse {
133133
for (key, value) in switchHeaders {
134134
headers[key] = value
135135
}
136-
case .ok(let body):
136+
case .ok(let body, let customHeaders):
137+
for (key, value) in customHeaders {
138+
headers.updateValue(value, forKey: key)
139+
}
137140
switch body {
138141
case .json: headers["Content-Type"] = "application/json"
139142
case .html, .htmlBody: headers["Content-Type"] = "text/html"
@@ -158,7 +161,7 @@ public enum HttpResponse {
158161

159162
func content() -> (length: Int, write: ((HttpResponseBodyWriter) throws -> Void)?) {
160163
switch self {
161-
case .ok(let body) : return body.content()
164+
case .ok(let body, _) : return body.content()
162165
case .badRequest(let body) : return body?.content() ?? (-1, nil)
163166
case .raw(_, _, _, let writer) : return (-1, writer)
164167
default : return (-1, nil)

0 commit comments

Comments
 (0)