1
0

HttpParser.swift 3.0 KB

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