1
0

HttpParser.swift 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. //
  2. // HttpParser.swift
  3. // Swifter
  4. //
  5. // Copyright (c) 2014-2016 Damian Kołakowski. All rights reserved.
  6. //
  7. import Foundation
  8. enum HttpParserError: Error {
  9. case invalidStatusLine(String)
  10. }
  11. public class HttpParser {
  12. public init() { }
  13. public func readHttpRequest(_ socket: Socket) throws -> HttpRequest {
  14. let statusLine = try socket.readLine()
  15. let statusLineTokens = statusLine.components(separatedBy: " ")
  16. if statusLineTokens.count < 3 {
  17. throw HttpParserError.invalidStatusLine(statusLine)
  18. }
  19. let request = HttpRequest()
  20. request.method = statusLineTokens[0]
  21. request.path = statusLineTokens[1]
  22. request.queryParams = extractQueryParams(request.path)
  23. request.headers = try readHeaders(socket)
  24. if let contentLength = request.headers["content-length"], let contentLengthValue = Int(contentLength) {
  25. request.body = try readBody(socket, size: contentLengthValue)
  26. }
  27. return request
  28. }
  29. private func extractQueryParams(_ url: String) -> [(String, String)] {
  30. #if compiler(>=5.0)
  31. guard let questionMarkIndex = url.firstIndex(of: "?") else {
  32. return []
  33. }
  34. #else
  35. guard let questionMarkIndex = url.index(of: "?") else {
  36. return []
  37. }
  38. #endif
  39. let queryStart = url.index(after: questionMarkIndex)
  40. guard url.endIndex > queryStart else { return [] }
  41. #if swift(>=4.0)
  42. let query = String(url[queryStart..<url.endIndex])
  43. #else
  44. guard let query = String(url[queryStart..<url.endIndex]) else { return [] }
  45. #endif
  46. return query.components(separatedBy: "&")
  47. .reduce([(String, String)]()) { (result, stringValue) -> [(String, String)] in
  48. #if compiler(>=5.0)
  49. guard let nameEndIndex = stringValue.firstIndex(of: "=") else {
  50. return result
  51. }
  52. #else
  53. guard let nameEndIndex = stringValue.index(of: "=") else {
  54. return result
  55. }
  56. #endif
  57. guard let name = String(stringValue[stringValue.startIndex..<nameEndIndex]).removingPercentEncoding else {
  58. return result
  59. }
  60. let valueStartIndex = stringValue.index(nameEndIndex, offsetBy: 1)
  61. guard valueStartIndex < stringValue.endIndex else {
  62. return result + [(name, "")]
  63. }
  64. guard let value = String(stringValue[valueStartIndex..<stringValue.endIndex]).removingPercentEncoding else {
  65. return result + [(name, "")]
  66. }
  67. return result + [(name, value)]
  68. }
  69. }
  70. private func readBody(_ socket: Socket, size: Int) throws -> [UInt8] {
  71. return try socket.read(length: size)
  72. }
  73. private func readHeaders(_ socket: Socket) throws -> [String: String] {
  74. var headers = [String: String]()
  75. while case let headerLine = try socket.readLine(), !headerLine.isEmpty {
  76. let headerTokens = headerLine.split(separator: ":", maxSplits: 1, omittingEmptySubsequences: true).map(String.init)
  77. if let name = headerTokens.first, let value = headerTokens.last {
  78. headers[name.lowercased()] = value.trimmingCharacters(in: .whitespaces)
  79. }
  80. }
  81. return headers
  82. }
  83. func supportsKeepAlive(_ headers: [String: String]) -> Bool {
  84. if let value = headers["connection"] {
  85. return "keep-alive" == value.trimmingCharacters(in: .whitespaces)
  86. }
  87. return false
  88. }
  89. }