CustomResponse.swift 733 B

12345678910111213141516171819202122232425262728
  1. //
  2. // CustomResponse.swift
  3. // Swifter
  4. //
  5. // Created by Dawid Szymczak on 15/08/16.
  6. // Copyright © 2016 Damian Kołakowski. All rights reserved.
  7. //
  8. #if os(Linux)
  9. import Glibc
  10. #else
  11. import Foundation
  12. #endif
  13. public class CustomResponse: Response {
  14. public var closure: (ObjectIdentifier) -> (String)
  15. public init(contentObject: AnyObject, closure: (Any) throws -> String) {
  16. self.closure = closure
  17. super.init(contentObject: contentObject)
  18. }
  19. public override func content() -> (contentLength: Int, contentString: String) {
  20. let serialised = try closure(ObjectIdentifier(contentObject))
  21. let data = [UInt8](serialised.utf8)
  22. return (data.count, serialised)
  23. }
  24. }