|
| 1 | +//===----------------------------------------------------------------------===// |
| 2 | +// |
| 3 | +// This source file is part of the AsyncHTTPClient open source project |
| 4 | +// |
| 5 | +// Copyright (c) 2021 Apple Inc. and the AsyncHTTPClient project authors |
| 6 | +// Licensed under Apache License v2.0 |
| 7 | +// |
| 8 | +// See LICENSE.txt for license information |
| 9 | +// See CONTRIBUTORS.txt for the list of AsyncHTTPClient project authors |
| 10 | +// |
| 11 | +// SPDX-License-Identifier: Apache-2.0 |
| 12 | +// |
| 13 | +//===----------------------------------------------------------------------===// |
| 14 | + |
| 15 | +import struct Foundation.URL |
| 16 | +import NIOHTTP1 |
| 17 | + |
| 18 | +@available(macOS 12.0, iOS 15.0, watchOS 8.0, tvOS 15.0, *) |
| 19 | +extension AsyncRequest { |
| 20 | + |
| 21 | + struct ValidationResult { |
| 22 | + let requestFramingMetadata: RequestFramingMetadata |
| 23 | + let poolKey: ConnectionPool.Key |
| 24 | + let head: HTTPRequestHead |
| 25 | + } |
| 26 | + |
| 27 | + func validate() throws -> ValidationResult { |
| 28 | + |
| 29 | + guard let url = URL(string: self.url) else { |
| 30 | + throw HTTPClientError.invalidURL |
| 31 | + } |
| 32 | + |
| 33 | + guard let urlScheme = url.scheme?.lowercased() else { |
| 34 | + throw HTTPClientError.emptyScheme |
| 35 | + } |
| 36 | + |
| 37 | + let kind = try HTTPClient.Request.Kind(forScheme: urlScheme) |
| 38 | + let useTLS: Bool = urlScheme == "https" || urlScheme == "https+unix" |
| 39 | + |
| 40 | + let poolKey = try ConnectionPool.Key( |
| 41 | + scheme: .init(string: urlScheme), |
| 42 | + host: kind.hostFromURL(url), |
| 43 | + port: url.port ?? (useTLS ? 443 : 80), |
| 44 | + unixPath: kind.socketPathFromURL(url), |
| 45 | + tlsConfiguration: nil |
| 46 | + ) |
| 47 | + |
| 48 | + try self.headers.validateFieldNames() |
| 49 | + |
| 50 | + var head = HTTPRequestHead(version: .http1_1, method: self.method, uri: url.uri, headers: self.headers) |
| 51 | + |
| 52 | + // if no host header was set, let's pick |
| 53 | + if !head.headers.contains(name: "host") { |
| 54 | + guard let urlHost = url.host else { |
| 55 | + throw HTTPClientError.emptyHost |
| 56 | + } |
| 57 | + head.headers.add(name: "host", value: urlHost) |
| 58 | + } |
| 59 | + |
| 60 | + let encodings = head.headers[canonicalForm: "Transfer-Encoding"].map { $0.lowercased() } |
| 61 | + if encodings.contains("identity") { |
| 62 | + throw HTTPClientError.identityCodingIncorrectlyPresent |
| 63 | + } |
| 64 | + |
| 65 | + head.headers.remove(name: "Transfer-Encoding") |
| 66 | + |
| 67 | + guard let body = self.body else { |
| 68 | + head.headers.remove(name: "Content-Length") |
| 69 | + // if we don't have a body we might not need to send the Content-Length field |
| 70 | + // https://tools.ietf.org/html/rfc7230#section-3.3.2 |
| 71 | + switch method { |
| 72 | + case .GET, .HEAD, .DELETE, .CONNECT, .TRACE: |
| 73 | + // A user agent SHOULD NOT send a Content-Length header field when the request |
| 74 | + // message does not contain a payload body and the method semantics do not |
| 75 | + // anticipate such a body. |
| 76 | + return ValidationResult( |
| 77 | + requestFramingMetadata: .init(connectionClose: !head.isKeepAlive, body: .none), |
| 78 | + poolKey: poolKey, |
| 79 | + head: head |
| 80 | + ) |
| 81 | + default: |
| 82 | + // A user agent SHOULD send a Content-Length in a request message when |
| 83 | + // no Transfer-Encoding is sent and the request method defines a meaning |
| 84 | + // for an enclosed payload body. |
| 85 | + head.headers.add(name: "Content-Length", value: "0") |
| 86 | + return ValidationResult( |
| 87 | + requestFramingMetadata: .init(connectionClose: !head.isKeepAlive, body: .none), |
| 88 | + poolKey: poolKey, |
| 89 | + head: head |
| 90 | + ) |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | + if case .TRACE = method { |
| 95 | + // A client MUST NOT send a message body in a TRACE request. |
| 96 | + // https://tools.ietf.org/html/rfc7230#section-4.3.8 |
| 97 | + throw HTTPClientError.traceRequestWithBody |
| 98 | + } |
| 99 | + |
| 100 | + guard (encodings.lazy.filter { $0 == "chunked" }.count <= 1) else { |
| 101 | + throw HTTPClientError.chunkedSpecifiedMultipleTimes |
| 102 | + } |
| 103 | + |
| 104 | + if encodings.isEmpty { |
| 105 | + switch self.body { |
| 106 | + case .some(.byteBuffer(let byteBuffer)): |
| 107 | + head.headers.add(name: "content-length", value: "\(byteBuffer.readableBytes)") |
| 108 | + case .some(bytes(let sequence)): |
| 109 | + // if we have a content length header, we assume this was set correctly |
| 110 | + if head.headers.contains(name: "content-length") { |
| 111 | + |
| 112 | + } else { |
| 113 | + head.headers.add(name: "transfer-encoding", value: "chunked") |
| 114 | + } |
| 115 | + |
| 116 | + } |
| 117 | + |
| 118 | + |
| 119 | + } |
| 120 | + } |
| 121 | +} |
0 commit comments