forked from PerfectlySoft/Perfect
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWebResponse.swift
More file actions
414 lines (339 loc) · 12.1 KB
/
Copy pathWebResponse.swift
File metadata and controls
414 lines (339 loc) · 12.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
//
// WebResponse.swift
// PerfectLib
//
// Created by Kyle Jessup on 7/6/15.
// Copyright (C) 2015 PerfectlySoft, Inc.
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version, as supplemented by the
// Perfect Additional Terms.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License, as supplemented by the
// Perfect Additional Terms, for more details.
//
// You should have received a copy of the GNU Affero General Public License
// and the Perfect Additional Terms that immediately follow the terms and
// conditions of the GNU Affero General Public License along with this
// program. If not, see <http://www.perfect.org/AGPL_3_0_With_Perfect_Additional_Terms.txt>.
//
/// This class bundles together the values which will be used to set a cookie in the outgoing response
public struct Cookie {
public let name: String?
public let value: String?
public let domain: String?
public let expires: String?
public let expiresIn: Double // seconds from now. may be negative. 0.0 means no expiry (session cookie)
public let path: String?
public let secure: Bool?
public let httpOnly: Bool?
public init(name: String?,
value: String?,
domain: String?,
expires: String?,
expiresIn: Double,
path: String?,
secure: Bool?,
httpOnly: Bool?) {
self.name = name
self.value = value
self.domain = domain
self.expires = expires
self.expiresIn = expiresIn
self.path = path
self.secure = secure
self.httpOnly = httpOnly
}
}
/*
class MustacheCacheItem {
let modificationDate: Int
let template: MustacheTemplate
init(modificationDate: Int, template: MustacheTemplate) {
self.modificationDate = modificationDate
self.template = template
}
}
let mustacheTemplateCache = RWLockCache<String, MustacheCacheItem>()
*/
/// Represents an outgoing web response. Handles the following tasks:
/// - Management of sessions
/// - Collecting HTTP response headers & cookies.
/// - Locating the response template file, parsing it, evaluating it and returning the resulting data.
/// - Provides access to the WebRequest object.
public class WebResponse {
var connection: WebConnection
/// The WebRequest for this response
public var request: WebRequest
/// The output encoding for a textual response. Defaults to UTF-8.
public var outputEncoding = "UTF-8"
var headersArray = [(String, String)]()
var cookiesArray = [Cookie]()
var includeStack = [String]()
var appStatus = 0
var appMessage = ""
var bodyData = [UInt8]()
var sessions = Dictionary<String, SessionManager>()
public var requestCompletedCallback: () -> () = {}
internal init(_ c: WebConnection, request: WebRequest) {
self.connection = c
self.request = request
}
/// Set the response status code and message. For example, 200, "OK".
public func setStatus(code: Int, message: String) {
self.connection.setStatus(code, msg: message)
}
/// Get the response status code and message.
public func getStatus() -> (Int, String) {
return self.connection.getStatus()
}
/// Adds the cookie object to the response
public func addCookie(cookie: Cookie) {
self.cookiesArray.append(cookie)
}
public func appendBodyBytes(bytes: [UInt8]) {
self.bodyData.appendContentsOf(bytes)
}
public func appendBodyString(string: String) {
self.bodyData.appendContentsOf([UInt8](string.utf8))
}
func respond(completion: () -> ()) {
self.requestCompletedCallback = { [weak self] in
self?.doSessionHeaders()
self?.sendResponse()
self?.commitSessions()
completion()
}
doMainBody()
}
/// !FIX! needs to pull key from possible request param
func getSessionKey(name: String) -> String {
// ...
for (cName, cValue) in self.request.cookies {
if name == cName {
return cValue
}
}
return SessionManager.generateSessionKey()
}
/// Provides access to the indicated `SessionManager` object.
/// If the session does not exist it is created. If it does exist, the existing object is returned.
public func getSession(named: String) -> SessionManager {
if let s = self.sessions[named] {
return s
}
let s = SessionManager(SessionConfiguration(named, id: getSessionKey(perfectSessionNamePrefix + named)))
self.sessions[named] = s
return s
}
/// Provides access to the indicated `SessionManager` object using the given `SessionConfiguration` data.
/// - throws: If the session already exists, `PerfectError.APIError` is thrown.
public func getSession(named: String, withConfiguration: SessionConfiguration) throws -> SessionManager {
guard self.sessions[named] == nil else {
throw PerfectError.APIError("WebResponse getSession withConfiguration: session was already initialized")
}
let s = SessionManager(SessionConfiguration(named, id: getSessionKey(perfectSessionNamePrefix + named), copyFrom: withConfiguration))
self.sessions[named] = s
return s
}
/// Discards a previously started session. The session will not be propagated and any changes to the session's variables will be discarded.
public func abandonSession(named: String) {
self.sessions.removeValueForKey(named)
}
/// Perform a 302 redirect to the given url
public func redirectTo(url: String) {
self.setStatus(302, message: "FOUND")
self.replaceHeader("Location", value: url)
}
/// Add an outgoing HTTP header
public func addHeader(name: String, value: String) {
self.headersArray.append( (name, value) )
}
/// Set a HTTP header, replacing all existing instances of said header
public func replaceHeader(name: String, value: String) {
for i in 0..<self.headersArray.count {
if self.headersArray[i].0 == name {
self.headersArray.removeAtIndex(i)
}
}
self.addHeader(name, value: value)
}
// directly called by the WebSockets impl
func sendResponse() {
for (key, value) in headersArray {
connection.writeHeaderLine(key + ": " + value)
}
// cookies
if self.cookiesArray.count > 0 {
let standardDateFormat = "';expires='E, dd-LLL-yyyy HH:mm:ss 'GMT'"
let now = ICU.getNow()
for cookie in self.cookiesArray {
var cookieLine = "Set-Cookie: "
cookieLine.appendContentsOf(cookie.name!.stringByEncodingURL)
cookieLine.appendContentsOf("=")
cookieLine.appendContentsOf(cookie.value!.stringByEncodingURL)
if cookie.expiresIn != 0.0 {
let formattedDate = try! ICU.formatDate(now + ICU.secondsToICUDate(Int(cookie.expiresIn)*60),
format: standardDateFormat, timezone: "GMT")
cookieLine.appendContentsOf(formattedDate)
}
if let path = cookie.path {
cookieLine.appendContentsOf("; path=" + path)
}
if let domain = cookie.domain {
cookieLine.appendContentsOf("; domain=" + domain)
}
if let secure = cookie.secure {
if secure == true {
cookieLine.appendContentsOf("; secure")
}
}
if let httpOnly = cookie.httpOnly {
if httpOnly == true {
cookieLine.appendContentsOf("; HttpOnly")
}
}
// etc...
connection.writeHeaderLine(cookieLine)
}
}
connection.writeHeaderLine("Content-Length: \(bodyData.count)")
connection.writeBodyBytes(bodyData)
}
private func doMainBody() {
do {
return try include(request.pathInfo(), local: false)
} catch PerfectError.FileError(let code, let msg) {
print("File exception \(code) \(msg)")
self.setStatus(code == 404 ? Int(code) : 500, message: msg)
self.bodyData = [UInt8]("File exception \(code) \(msg)".utf8)
} catch MustacheError.SyntaxError(let msg) {
print("MustacheError.SyntaxError \(msg)")
self.setStatus(500, message: msg)
self.bodyData = [UInt8]("Mustache syntax error \(msg)".utf8)
} catch MustacheError.EvaluationError(let msg) {
print("MustacheError.EvaluationError exception \(msg)")
self.setStatus(500, message: msg)
self.bodyData = [UInt8]("Mustache evaluation error \(msg)".utf8)
} catch let e {
print("Unexpected exception \(e)")
}
self.requestCompletedCallback()
}
func doSessionHeaders() {
for (_, session) in self.sessions {
session.initializeForResponse(self)
}
}
func commitSessions() {
for (name, session) in self.sessions {
do {
try session.commit()
} catch let e {
LogManager.logMessage("Exception while committing session \(name) \(e)")
}
}
}
func includeVirtual(path: String) throws {
guard let handler = PageHandlerRegistry.getRequestHandler(self) else {
throw PerfectError.FileError(404, "The path \(path) had no associated handler")
}
handler.handleRequest(self.request, response: self)
}
func include(path: String, local: Bool) throws {
var fullPath = path
if let decodedPath = path.stringByDecodingURL {
fullPath = decodedPath
}
if !path.hasPrefix("/") {
fullPath = makeNonRelative(path, local: local)
}
fullPath = request.documentRoot + fullPath
let file = File(fullPath)
if PageHandlerRegistry.hasGlobalHandler() && (!path.hasSuffix("."+mustacheExtension) || !file.exists()) {
return try self.includeVirtual(path)
}
if !path.hasSuffix("."+mustacheExtension) {
throw PerfectError.FileError(404, "The file \(path) was not a mustache template file")
}
do {
guard file.exists() else {
throw PerfectError.FileError(404, "Not Found")
}
try file.openRead()
defer { file.close() }
let bytes = try file.readSomeBytes(file.size())
let parser = MustacheParser()
let str = UTF8Encoding.encode(bytes)
let template = try parser.parse(str)
let context = MustacheEvaluationContext(webResponse: self)
context.filePath = fullPath
let collector = MustacheEvaluationOutputCollector()
template.templateName = path
try template.evaluatePragmas(context, collector: collector)
template.evaluate(context, collector: collector)
let fullString = collector.asString()
self.bodyData += Array(fullString.utf8)
}
self.requestCompletedCallback()
}
private func makeNonRelative(path: String, local: Bool = false) -> String {
if includeStack.count == 0 {
return "/" + path
}
if local {
return includeStack.last!.stringByDeletingLastPathComponent + "/" + path
}
return request.pathInfo().stringByDeletingLastPathComponent + "/" + path
}
// WARNING NOTE Using the RWLockCache, even just for read access seems to bring out some sort of bug in the
// ARC system. Therefore, this function is not currently called.
// !FIX! track this problem down
/*
func includeCached(path: String, local: Bool = false) throws {
if !path.hasSuffix("."+mustacheExtension) {
throw PerfectError.FileError(404, "The file \(path) was not a mustache template file")
}
var fullPath = path
if !path.hasPrefix("/") {
fullPath = makeNonRelative(path, local: local)
}
fullPath = request.documentRoot + fullPath
do {
let file = File(fullPath)
guard file.exists() else {
throw PerfectError.FileError(404, "Not Found")
}
let diskModTime = file.modificationTime()
var cacheItem = mustacheTemplateCache.valueForKey(fullPath)//, validatorCallback: { (value) -> Bool in
// return diskModTime == value.modificationDate
// })
if cacheItem == nil {
print("REPLACING")
try file.openRead()
defer { file.close() }
let bytes = try file.readSomeBytes(file.size())
let parser = MustacheParser()
let str = UTF8Encoding.encode(bytes)
let template = try parser.parse(str)
cacheItem = MustacheCacheItem(modificationDate: diskModTime, template: template)
mustacheTemplateCache.setValueForKey(fullPath, value: cacheItem!)
}
let template = cacheItem!.template
let context = MustacheEvaluationContext(webResponse: self)
context.filePath = fullPath
let collector = MustacheEvaluationOutputCollector()
template.templateName = path
try template.evaluatePragmas(context, collector: collector)
template.evaluate(context, collector: collector)
let fullString = collector.asString()
self.bodyData += Array(fullString.utf8)
}
}
*/
}