HttpParser.swift 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. //
  2. // HttpParser.swift
  3. //
  4. // Created by Damian Kolakowski on 05/06/14.
  5. // Copyright (c) 2014 Damian Kołakowski. All rights reserved.
  6. //
  7. import Foundation
  8. class HttpParser {
  9. class func err(reason:String) -> NSError {
  10. return NSError(domain: "HTTP_PARSER", code: 0, userInfo:[NSLocalizedFailureReasonErrorKey : reason])
  11. }
  12. func nextHttpRequest(socket: CInt, error:NSErrorPointer = nil) -> HttpRequest? {
  13. if let statusLine = nextLine(socket, error: error) {
  14. let statusTokens = split(statusLine, { $0 == " " })
  15. println(statusTokens)
  16. if ( statusTokens.count < 3 ) {
  17. if error != nil { error.memory = HttpParser.err("Invalid status line: \(statusLine)") }
  18. return nil
  19. }
  20. let method = statusTokens[0]
  21. let path = statusTokens[1]
  22. let urlParams = extractUrlParams(path)
  23. // TODO extract query parameters
  24. if let headers = nextHeaders(socket, error: error) {
  25. // TODO detect content-type and handle:
  26. // 'application/x-www-form-urlencoded' -> Dictionary
  27. // 'multipart' -> Dictionary
  28. if let contentSize = headers["content-length"]?.toInt() {
  29. let body = nextBody(socket, size: contentSize, error: error)
  30. return HttpRequest(url: path, urlParams: urlParams, method: method, headers: headers, body: body, capturedUrlGroups: [])
  31. }
  32. return HttpRequest(url: path, urlParams: urlParams, method: method, headers: headers, body: nil, capturedUrlGroups: [])
  33. }
  34. }
  35. return nil
  36. }
  37. private func extractUrlParams(url: String) -> [(String, String)] {
  38. if let query = split(url, { $0 == "?" }).last {
  39. return map(split(query, { $0 == "&" }), { (param:String) -> (String, String) in
  40. let tokens = split(param, { $0 == "=" })
  41. return tokens.count >= 2 ? (tokens.first!, tokens.last!) : ("", "")
  42. })
  43. }
  44. return []
  45. }
  46. private func nextBody(socket: CInt, size: Int , error:NSErrorPointer) -> String? {
  47. var body = ""
  48. var counter = 0;
  49. while ( counter < size ) {
  50. let c = nextUInt8(socket)
  51. if ( c < 0 ) {
  52. if error != nil { error.memory = HttpParser.err("IO error while reading body") }
  53. return nil
  54. }
  55. body.append(UnicodeScalar(c))
  56. counter++;
  57. }
  58. return body
  59. }
  60. private func nextHeaders(socket: CInt, error:NSErrorPointer) -> Dictionary<String, String>? {
  61. var headers = Dictionary<String, String>()
  62. while let headerLine = nextLine(socket, error: error) {
  63. if ( headerLine.isEmpty ) {
  64. return headers
  65. }
  66. let headerTokens = split(headerLine, { $0 == ":" })
  67. if ( headerTokens.count >= 2 ) {
  68. // RFC 2616 - "Hypertext Transfer Protocol -- HTTP/1.1", paragraph 4.2, "Message Headers":
  69. // "Each header field consists of a name followed by a colon (":") and the field value. Field names are case-insensitive."
  70. // We can keep lower case version.
  71. let headerName = headerTokens[0].lowercaseString
  72. let headerValue = headerTokens[1].stringByTrimmingCharactersInSet(NSCharacterSet.whitespaceAndNewlineCharacterSet())
  73. if ( !headerName.isEmpty && !headerValue.isEmpty ) {
  74. headers.updateValue(headerValue, forKey: headerName)
  75. }
  76. }
  77. }
  78. return nil
  79. }
  80. private func nextUInt8(socket: CInt) -> Int {
  81. var buffer = [UInt8](count: 1, repeatedValue: 0);
  82. let next = recv(socket, &buffer, UInt(buffer.count), 0)
  83. if next <= 0 { return next }
  84. return Int(buffer[0])
  85. }
  86. private func nextLine(socket: CInt, error:NSErrorPointer) -> String? {
  87. var characters: String = ""
  88. var n = 0
  89. do {
  90. n = nextUInt8(socket)
  91. if ( n > 13 /* CR */ ) { characters.append(Character(UnicodeScalar(n))) }
  92. } while ( n > 0 && n != 10 /* NL */)
  93. if ( n == -1 && characters.isEmpty ) {
  94. if error != nil { error.memory = Socket.socketLastError("recv(...) failed.") }
  95. return nil
  96. }
  97. return characters
  98. }
  99. func supportsKeepAlive(headers: Dictionary<String, String>) -> Bool {
  100. if let value = headers["connection"] {
  101. return "keep-alive" == value.stringByTrimmingCharactersInSet(NSCharacterSet.whitespaceAndNewlineCharacterSet()).lowercaseString
  102. }
  103. return false
  104. }
  105. }