HttpParser.swift 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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.errorWithDomain("HttpParser", code: 0, userInfo:[NSLocalizedFailureReasonErrorKey : reason])
  11. }
  12. func nextHttpRequest(socket: CInt, error:NSErrorPointer = nil) -> (String, String, Dictionary<String, String>)? {
  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 { error.memory = HttpParser.err("Invalid status line: \(statusLine)") }
  18. return nil
  19. }
  20. let method = statusTokens[0]
  21. let path = statusTokens[1]
  22. if let headers = nextHeaders(socket, error: error) {
  23. return (path, method, headers)
  24. }
  25. }
  26. return nil
  27. }
  28. func nextHeaders(socket: CInt, error:NSErrorPointer) -> Dictionary<String, String>? {
  29. var headers = Dictionary<String, String>()
  30. while let headerLine = nextLine(socket, error: error) {
  31. if ( headerLine.isEmpty ) {
  32. return headers
  33. }
  34. let headerTokens = split(headerLine, { $0 == ":" })
  35. if ( headerTokens.count >= 2 ) {
  36. // RFC 2616 - "Hypertext Transfer Protocol -- HTTP/1.1", paragraph 4.2, "Message Headers":
  37. // "Each header field consists of a name followed by a colon (":") and the field value. Field names are case-insensitive."
  38. // We can keep lower case version.
  39. let headerName = headerTokens[0].lowercaseString
  40. let headerValue = headerTokens[1]
  41. if ( !headerName.isEmpty && !headerValue.isEmpty ) {
  42. headers.updateValue(headerValue, forKey: headerName)
  43. }
  44. }
  45. }
  46. return nil
  47. }
  48. func nextLine(socket: CInt, error:NSErrorPointer) -> String? {
  49. // TODO - read more bytes than one. It makes the server very slow.
  50. // TODO - check if there is a nicer way to manipulate bytes with Swift ( recv(...) -> String )
  51. var characters: String = ""
  52. var buff: UInt8[] = UInt8[](count: 1, repeatedValue: 0), n: Int = 1
  53. do {
  54. n = recv(socket, &buff, 1, 0);
  55. if ( n > 0 && buff[0] > 13 /* CR */ ) {
  56. characters += Character(UnicodeScalar(UInt32(buff[0])))
  57. }
  58. } while ( n > 0 && buff[0] != 10 /* NL */ )
  59. if ( n == -1 ) {
  60. if error { error.memory = Socket.socketRecentError("recv(...) failed.") }
  61. return nil
  62. }
  63. return characters
  64. }
  65. func supportsKeepAlive(headers: Dictionary<String, String>) -> Bool {
  66. if let value = headers["connection"] {
  67. return "keep-alive" == value.stringByTrimmingCharactersInSet(NSCharacterSet.whitespaceAndNewlineCharacterSet()).lowercaseString
  68. }
  69. return false
  70. }
  71. }