You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

123 lines
4.2 KiB

2 years ago
  1. //////////////////////////////////////////////////////////////////////////////////////////////////
  2. //
  3. // FoundationHTTPHandler.swift
  4. // Starscream
  5. //
  6. // Created by Dalton Cherry on 1/25/19.
  7. // Copyright © 2019 Vluxe. All rights reserved.
  8. //
  9. // Licensed under the Apache License, Version 2.0 (the "License");
  10. // you may not use this file except in compliance with the License.
  11. // You may obtain a copy of the License at
  12. //
  13. // http://www.apache.org/licenses/LICENSE-2.0
  14. //
  15. // Unless required by applicable law or agreed to in writing, software
  16. // distributed under the License is distributed on an "AS IS" BASIS,
  17. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  18. // See the License for the specific language governing permissions and
  19. // limitations under the License.
  20. //
  21. //////////////////////////////////////////////////////////////////////////////////////////////////
  22. import Foundation
  23. #if os(watchOS)
  24. public typealias FoundationHTTPHandler = StringHTTPHandler
  25. #else
  26. public class FoundationHTTPHandler: HTTPHandler {
  27. var buffer = Data()
  28. weak var delegate: HTTPHandlerDelegate?
  29. public init() {
  30. }
  31. public func convert(request: URLRequest) -> Data {
  32. let msg = CFHTTPMessageCreateRequest(kCFAllocatorDefault, request.httpMethod! as CFString,
  33. request.url! as CFURL, kCFHTTPVersion1_1).takeRetainedValue()
  34. if let headers = request.allHTTPHeaderFields {
  35. for (aKey, aValue) in headers {
  36. CFHTTPMessageSetHeaderFieldValue(msg, aKey as CFString, aValue as CFString)
  37. }
  38. }
  39. if let body = request.httpBody {
  40. CFHTTPMessageSetBody(msg, body as CFData)
  41. }
  42. guard let data = CFHTTPMessageCopySerializedMessage(msg) else {
  43. return Data()
  44. }
  45. return data.takeRetainedValue() as Data
  46. }
  47. public func parse(data: Data) -> Int {
  48. let offset = findEndOfHTTP(data: data)
  49. if offset > 0 {
  50. buffer.append(data.subdata(in: 0..<offset))
  51. } else {
  52. buffer.append(data)
  53. }
  54. if parseContent(data: buffer) {
  55. buffer = Data()
  56. }
  57. return offset
  58. }
  59. //returns true when the buffer should be cleared
  60. func parseContent(data: Data) -> Bool {
  61. var pointer = [UInt8]()
  62. data.withUnsafeBytes { pointer.append(contentsOf: $0) }
  63. let response = CFHTTPMessageCreateEmpty(kCFAllocatorDefault, false).takeRetainedValue()
  64. if !CFHTTPMessageAppendBytes(response, pointer, data.count) {
  65. return false //not enough data, wait for more
  66. }
  67. if !CFHTTPMessageIsHeaderComplete(response) {
  68. return false //not enough data, wait for more
  69. }
  70. let code = CFHTTPMessageGetResponseStatusCode(response)
  71. if code != HTTPWSHeader.switchProtocolCode {
  72. delegate?.didReceiveHTTP(event: .failure(HTTPUpgradeError.notAnUpgrade(code)))
  73. return true
  74. }
  75. if let cfHeaders = CFHTTPMessageCopyAllHeaderFields(response) {
  76. let nsHeaders = cfHeaders.takeRetainedValue() as NSDictionary
  77. var headers = [String: String]()
  78. for (key, value) in nsHeaders {
  79. if let key = key as? String, let value = value as? String {
  80. headers[key] = value
  81. }
  82. }
  83. delegate?.didReceiveHTTP(event: .success(headers))
  84. return true
  85. }
  86. delegate?.didReceiveHTTP(event: .failure(HTTPUpgradeError.invalidData))
  87. return true
  88. }
  89. public func register(delegate: HTTPHandlerDelegate) {
  90. self.delegate = delegate
  91. }
  92. private func findEndOfHTTP(data: Data) -> Int {
  93. let endBytes = [UInt8(ascii: "\r"), UInt8(ascii: "\n"), UInt8(ascii: "\r"), UInt8(ascii: "\n")]
  94. var pointer = [UInt8]()
  95. data.withUnsafeBytes { pointer.append(contentsOf: $0) }
  96. var k = 0
  97. for i in 0..<data.count {
  98. if pointer[i] == endBytes[k] {
  99. k += 1
  100. if k == 4 {
  101. return i + 1
  102. }
  103. } else {
  104. k = 0
  105. }
  106. }
  107. return -1
  108. }
  109. }
  110. #endif