HttpParser.swift 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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. guard let questionMark = url.index(of: "?") else {
  31. return []
  32. }
  33. let queryStart = url.index(after: questionMark)
  34. guard url.endIndex > queryStart else {
  35. return []
  36. }
  37. let query = String(url[queryStart..<url.endIndex])
  38. return query.components(separatedBy: "&")
  39. .reduce([(String, String)]()) { (c, s) -> [(String, String)] in
  40. guard let nameEndIndex = s.index(of: "=") else {
  41. return c
  42. }
  43. guard let name = String(s[s.startIndex..<nameEndIndex]).removingPercentEncoding else {
  44. return c
  45. }
  46. let valueStartIndex = s.index(nameEndIndex, offsetBy: 1)
  47. guard valueStartIndex < s.endIndex else {
  48. return c + [(name, "")]
  49. }
  50. guard let value = String(s[valueStartIndex..<s.endIndex]).removingPercentEncoding else {
  51. return c + [(name, "")]
  52. }
  53. return c + [(name, value)]
  54. }
  55. }
  56. private func readBody(_ socket: Socket, size: Int) throws -> [UInt8] {
  57. var body = [UInt8]()
  58. for _ in 0..<size { body.append(try socket.read()) }
  59. return body
  60. }
  61. private func readHeaders(_ socket: Socket) throws -> [String: String] {
  62. var headers = [String: String]()
  63. while case let headerLine = try socket.readLine() , !headerLine.isEmpty {
  64. let headerTokens = headerLine.split(separator: ":", maxSplits: 1, omittingEmptySubsequences: true).map(String.init)
  65. if let name = headerTokens.first, let value = headerTokens.last {
  66. headers[name.lowercased()] = value.trimmingCharacters(in: .whitespaces)
  67. }
  68. }
  69. return headers
  70. }
  71. func supportsKeepAlive(_ headers: [String: String]) -> Bool {
  72. if let value = headers["connection"] {
  73. return "keep-alive" == value.trimmingCharacters(in: .whitespaces)
  74. }
  75. return false
  76. }
  77. }