HttpRequest.swift 1.1 KB

123456789101112131415161718192021222324252627282930313233
  1. //
  2. // HttpRequest.swift
  3. // Swifter
  4. // Copyright (c) 2015 Damian Kołakowski. All rights reserved.
  5. //
  6. import Foundation
  7. public struct HttpRequest {
  8. public let url: String
  9. public let urlParams: [(String, String)]
  10. public let method: String
  11. public let headers: [String: String]
  12. public let body: String?
  13. public var capturedUrlGroups: [String]
  14. public var address: String?
  15. public func parseForm() -> [(String, String)] {
  16. if let body = body {
  17. return body.componentsSeparatedByString("&").map { (param:String) -> (String, String) in
  18. let tokens = param.componentsSeparatedByString("=")
  19. if tokens.count >= 2 {
  20. let key = tokens[0].stringByReplacingOccurrencesOfString("+", withString: " ").stringByRemovingPercentEncoding
  21. let value = tokens[1].stringByReplacingOccurrencesOfString("+", withString: " ").stringByRemovingPercentEncoding
  22. if let key = key, value = value { return (key, value) }
  23. }
  24. return ("","")
  25. }
  26. }
  27. return []
  28. }
  29. }