|
| 1 | +// Foundation/URLSession/Message.swift - URLSession & libcurl |
| 2 | +// |
| 3 | +// This source file is part of the Swift.org open source project |
| 4 | +// |
| 5 | +// Copyright (c) 2014 - 2017 Apple Inc. and the Swift project authors |
| 6 | +// Licensed under Apache License v2.0 with Runtime Library Exception |
| 7 | +// |
| 8 | +// See http://swift.org/LICENSE.txt for license information |
| 9 | +// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors |
| 10 | +// |
| 11 | +// ----------------------------------------------------------------------------- |
| 12 | +/// |
| 13 | +/// Common code for Header parsing |
| 14 | +/// |
| 15 | +/// - SeeAlso: https://curl.haxx.se/libcurl/c/ |
| 16 | +/// - SeeAlso: URLSession.swift |
| 17 | +/// |
| 18 | +// ----------------------------------------------------------------------------- |
| 19 | + |
| 20 | +import CoreFoundation |
| 21 | + |
| 22 | +extension _HTTPURLProtocol { |
| 23 | + /// An HTTP header being parsed. |
| 24 | + /// |
| 25 | + /// It can either be complete (i.e. the final CR LF CR LF has been |
| 26 | + /// received), or partial. |
| 27 | + internal enum _ParsedResponseHeader { |
| 28 | + case partial(_ResponseHeaderLines) |
| 29 | + case complete(_ResponseHeaderLines) |
| 30 | + init() { |
| 31 | + self = .partial(_ResponseHeaderLines()) |
| 32 | + } |
| 33 | + } |
| 34 | + /// A type safe wrapper around multiple lines of headers. |
| 35 | + /// |
| 36 | + /// This can be converted into an `HTTPURLResponse`. |
| 37 | + internal struct _ResponseHeaderLines { |
| 38 | + let lines: [String] |
| 39 | + init() { |
| 40 | + self.lines = [] |
| 41 | + } |
| 42 | + init(headerLines: [String]) { |
| 43 | + self.lines = headerLines |
| 44 | + } |
| 45 | + } |
| 46 | +} |
| 47 | + |
| 48 | +extension _HTTPURLProtocol._ParsedResponseHeader { |
| 49 | + /// Parse a header line passed by libcurl. |
| 50 | + /// |
| 51 | + /// These contain the <CRLF> ending and the final line contains nothing but |
| 52 | + /// that ending. |
| 53 | + /// - Returns: Returning nil indicates failure. Otherwise returns a new |
| 54 | + /// `ParsedResponseHeader` with the given line added. |
| 55 | + func byAppending(headerLine data: Data) -> _HTTPURLProtocol._ParsedResponseHeader? { |
| 56 | + // The buffer must end in CRLF |
| 57 | + guard |
| 58 | + 2 <= data.count && |
| 59 | + data[data.endIndex - 2] == _HTTPCharacters.CR && |
| 60 | + data[data.endIndex - 1] == _HTTPCharacters.LF |
| 61 | + else { return nil } |
| 62 | + let lineBuffer = data.subdata(in: Range(data.startIndex..<data.endIndex-2)) |
| 63 | + guard let line = String(data: lineBuffer, encoding: String.Encoding.utf8) else { return nil} |
| 64 | + return byAppending(headerLine: line) |
| 65 | + } |
| 66 | + /// Append a status line. |
| 67 | + /// |
| 68 | + /// If the line is empty, it marks the end of the header, and the result |
| 69 | + /// is a complete header. Otherwise it's a partial header. |
| 70 | + /// - Note: Appending a line to a complete header results in a partial |
| 71 | + /// header with just that line. |
| 72 | + private func byAppending(headerLine line: String) -> _HTTPURLProtocol._ParsedResponseHeader { |
| 73 | + if line.isEmpty { |
| 74 | + switch self { |
| 75 | + case .partial(let header): return .complete(header) |
| 76 | + case .complete: return .partial(_HTTPURLProtocol._ResponseHeaderLines()) |
| 77 | + } |
| 78 | + } else { |
| 79 | + let header = partialResponseHeader |
| 80 | + return .partial(header.byAppending(headerLine: line)) |
| 81 | + } |
| 82 | + } |
| 83 | + private var partialResponseHeader: _HTTPURLProtocol._ResponseHeaderLines { |
| 84 | + switch self { |
| 85 | + case .partial(let header): return header |
| 86 | + case .complete: return _HTTPURLProtocol._ResponseHeaderLines() |
| 87 | + } |
| 88 | + } |
| 89 | +} |
| 90 | + |
| 91 | +private extension _HTTPURLProtocol._ResponseHeaderLines { |
| 92 | + /// Returns a copy of the lines with the new line appended to it. |
| 93 | + func byAppending(headerLine line: String) -> _HTTPURLProtocol._ResponseHeaderLines { |
| 94 | + var l = self.lines |
| 95 | + l.append(line) |
| 96 | + return _HTTPURLProtocol._ResponseHeaderLines(headerLines: l) |
| 97 | + } |
| 98 | +} |
| 99 | + |
| 100 | +// Characters that we need for HTTP parsing: |
| 101 | +struct _HTTPCharacters { |
| 102 | + /// *Carriage Return* symbol |
| 103 | + static let CR: UInt8 = 0x0d |
| 104 | + /// *Line Feed* symbol |
| 105 | + static let LF: UInt8 = 0x0a |
| 106 | + /// *Space* symbol |
| 107 | + static let Space = UnicodeScalar(0x20) |
| 108 | + static let HorizontalTab = UnicodeScalar(0x09) |
| 109 | + static let Colon = UnicodeScalar(0x3a) |
| 110 | + /// *Separators* according to RFC 2616 |
| 111 | + static let Separators = NSCharacterSet(charactersIn: "()<>@,;:\\\"/[]?={} \t") |
| 112 | +} |
0 commit comments