HttpRequest.swift 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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 var url: String = ""
  9. public var queryParams: [(String, String)] = []
  10. public var method: String = ""
  11. public var headers: [String: String] = [:]
  12. public var body: [UInt8] = []
  13. public var address: String? = ""
  14. public var params: [String: String] = [:]
  15. public func parseUrlencodedForm() -> [(String, String)] {
  16. guard let contentTypeHeader = headers["content-type"] else {
  17. return []
  18. }
  19. let contentTypeHeaderTokens = contentTypeHeader.split(";").map { $0.trim() }
  20. guard let contentType = contentTypeHeaderTokens.first where contentType == "application/x-www-form-urlencoded" else {
  21. return []
  22. }
  23. return UInt8ArrayToUTF8String(body).split("&").map { (param: String) -> (String, String) in
  24. let tokens = param.split("=")
  25. if let name = tokens.first, value = tokens.last where tokens.count == 2 {
  26. return (name.replace("+", new: " ").removePercentEncoding(),
  27. value.replace("+", new: " ").removePercentEncoding())
  28. }
  29. return ("","")
  30. }
  31. }
  32. public struct MultiPart {
  33. public let headers: [String: String]
  34. public let body: [UInt8]
  35. }
  36. public func parseMultiPartFormData() -> [MultiPart] {
  37. guard let contentTypeHeader = headers["content-type"] else {
  38. return []
  39. }
  40. let contentTypeHeaderTokens = contentTypeHeader.split(";").map { $0.trim() }
  41. guard let contentType = contentTypeHeaderTokens.first where contentType == "multipart/form-data" else {
  42. return []
  43. }
  44. var boundary: String? = nil
  45. contentTypeHeaderTokens.forEach({
  46. let tokens = $0.split("=")
  47. if let key = tokens.first where key == "boundary" && tokens.count == 2 {
  48. boundary = tokens.last
  49. }
  50. })
  51. if let boundary = boundary where boundary.utf8.count > 0 {
  52. return parseMultiPartFormData(body, boundary: "--\(boundary)")
  53. }
  54. return []
  55. }
  56. private func parseMultiPartFormData(data: [UInt8], boundary: String) -> [MultiPart] {
  57. var generator = data.generate()
  58. var result = [MultiPart]()
  59. while let part = nextMultiPart(&generator, boundary: boundary, isFirst: result.isEmpty) {
  60. result.append(part)
  61. }
  62. return result
  63. }
  64. private func nextMultiPart(inout generator: IndexingGenerator<[UInt8]>, boundary: String, isFirst: Bool) -> MultiPart? {
  65. if isFirst {
  66. guard nextMultiPartLine(&generator) == boundary else {
  67. return nil
  68. }
  69. } else {
  70. nextMultiPartLine(&generator)
  71. }
  72. var headers = [String: String]()
  73. while let line = nextMultiPartLine(&generator) where !line.isEmpty {
  74. let tokens = line.split(":")
  75. if let name = tokens.first, value = tokens.last where tokens.count == 2 {
  76. headers[name.lowercaseString] = value.trim()
  77. }
  78. }
  79. guard let body = nextMultiPartBody(&generator, boundary: boundary) else {
  80. return nil
  81. }
  82. return MultiPart(headers: headers, body: body)
  83. }
  84. private func nextMultiPartLine(inout generator: IndexingGenerator<[UInt8]>) -> String? {
  85. var result = String()
  86. while let value = generator.next() {
  87. if value > Constants.CR {
  88. result.append(Character(UnicodeScalar(value)))
  89. }
  90. if value == Constants.NL {
  91. break
  92. }
  93. }
  94. return result
  95. }
  96. private func nextMultiPartBody(inout generator: IndexingGenerator<[UInt8]>, boundary: String) -> [UInt8]? {
  97. var body = [UInt8]()
  98. let boundaryArray = [UInt8](boundary.utf8)
  99. var matchOffset = 0;
  100. while let x = generator.next() {
  101. matchOffset = ( x == boundaryArray[matchOffset] ? matchOffset + 1 : 0 )
  102. body.append(x)
  103. if matchOffset == boundaryArray.count {
  104. body.removeRange(Range<Int>(start: body.count-matchOffset, end: body.count))
  105. if body.last == Constants.NL {
  106. body.removeLast()
  107. if body.last == Constants.CR {
  108. body.removeLast()
  109. }
  110. }
  111. return body
  112. }
  113. }
  114. return nil
  115. }
  116. }