Skip to content

Commit 6c57a5c

Browse files
authored
chore: URLSessionHTTPClient logging & code cleanup (#856)
1 parent dc7e56c commit 6c57a5c

File tree

1 file changed

+38
-16
lines changed

1 file changed

+38
-16
lines changed

Diff for: Sources/ClientRuntime/Networking/Http/URLSession/URLSessionHTTPClient.swift

+38-16
Original file line numberDiff line numberDiff line change
@@ -198,15 +198,13 @@ public final class URLSessionHTTPClient: HTTPClient {
198198
guard let tlsOptions = tlsOptions, tlsOptions.useSelfSignedCertificate,
199199
let certFile = tlsOptions.certificate,
200200
let serverTrust = challenge.protectionSpace.serverTrust else {
201-
logger.info(
202-
"Either TLSOptions not set or missing values! Using default trust store."
203-
)
201+
logger.debug("Either TLSOptions not set or missing values! Using default trust store.")
204202
completionHandler(.performDefaultHandling, nil)
205203
return
206204
}
207205

208206
guard let customRoot = Bundle.main.certificate(named: certFile) else {
209-
logger.info("Certificate not found! Using default trust store.")
207+
logger.debug("Certificate not found! Using default trust store.")
210208
completionHandler(.performDefaultHandling, nil)
211209
return
212210
}
@@ -232,9 +230,7 @@ public final class URLSessionHTTPClient: HTTPClient {
232230
guard let tlsOptions, tlsOptions.useProvidedKeystore,
233231
let keystoreName = tlsOptions.pkcs12Path,
234232
let keystorePasword = tlsOptions.pkcs12Password else {
235-
logger.info(
236-
"Either TLSOptions not set or missing values! Using default keystore."
237-
)
233+
logger.debug("Either TLSOptions not set or missing values! Using default keystore.")
238234
completionHandler(.performDefaultHandling, nil)
239235
return
240236
}
@@ -275,8 +271,11 @@ public final class URLSessionHTTPClient: HTTPClient {
275271
/// This callback is made as soon as the initial response + headers is complete.
276272
/// Response body data may continue to stream in after this callback is received.
277273
func urlSession(
278-
_ session: URLSession, dataTask: URLSessionDataTask, didReceive response: URLResponse
279-
) async -> URLSession.ResponseDisposition {
274+
_ session: URLSession,
275+
dataTask: URLSessionDataTask,
276+
didReceive response: URLResponse,
277+
completionHandler: @escaping (URLSession.ResponseDisposition) -> Void
278+
) {
280279
logger.debug("urlSession(_:dataTask:didReceive response:) called")
281280
storage.modify(dataTask) { connection in
282281
guard let httpResponse = response as? HTTPURLResponse else {
@@ -295,7 +294,7 @@ public final class URLSessionHTTPClient: HTTPClient {
295294
let response = HTTPResponse(headers: headers, body: body, statusCode: statusCode)
296295
connection.resume(returning: response)
297296
}
298-
return .allow
297+
completionHandler(.allow)
299298
}
300299

301300
/// Called when the task needs a new `InputStream` to continue streaming the request body.
@@ -348,10 +347,12 @@ public final class URLSessionHTTPClient: HTTPClient {
348347
/// If the error is returned prior to the initial response, the request fails with an error.
349348
/// If the error is returned after the initial response, the error is used to fail the response stream.
350349
func urlSession(_ session: URLSession, task: URLSessionTask, didCompleteWithError error: Error?) {
350+
let httpMethod = task.originalRequest?.httpMethod ?? ""
351+
let url = task.originalRequest?.url?.absoluteString ?? ""
351352
if let error {
352-
logger.error("urlSession(_:task:didCompleteWithError:) failed. Error: \(error.localizedDescription)")
353+
logger.error("URLRequest(\(httpMethod) \(url)) failed with error: \(error)")
353354
} else {
354-
logger.debug("urlSession(_:task:didCompleteWithError:) called. Success")
355+
logger.debug("URLRequest(\(httpMethod) \(url)) succeeded")
355356
}
356357

357358
// This connection is complete. No further data will be sent, and none will be received.
@@ -557,8 +558,10 @@ public final class URLSessionHTTPClient: HTTPClient {
557558
delegate.storage.set(connection, for: dataTask)
558559

559560
// Start the HTTP connection and start streaming the request body data, if needed
560-
logger.info("start URLRequest(\(urlRequest.url?.absoluteString ?? "")) called")
561-
logger.info(" body is \(streamBridge != nil ? "InputStream" : "Data")")
561+
let httpMethod = urlRequest.httpMethod ?? ""
562+
let url = urlRequest.url?.absoluteString ?? ""
563+
logger.debug("URLRequest(\(httpMethod) \(url)) started")
564+
logBodyDescription(body)
562565
dataTask.resume()
563566
Task { [streamBridge] in
564567
await streamBridge?.open()
@@ -582,7 +585,7 @@ public final class URLSessionHTTPClient: HTTPClient {
582585
/// Create a `URLRequest` for the Smithy operation to be performed.
583586
/// - Parameters:
584587
/// - request: The SDK-native, signed `HTTPRequest` ready to be transmitted.
585-
/// - httpBodyStream: A Foundation `InputStream` carrying the HTTP body for this request.
588+
/// - body: A `Body` with either a stream bridge or data for this request's body.
586589
/// - Returns: A `URLRequest` ready to be transmitted by `URLSession` for this operation.
587590
private func makeURLRequest(from request: HTTPRequest, body: Body) throws -> URLRequest {
588591
var components = URLComponents()
@@ -592,7 +595,7 @@ public final class URLSessionHTTPClient: HTTPClient {
592595
components.percentEncodedPath = request.destination.path
593596
if let queryItems = request.queryItems, !queryItems.isEmpty {
594597
components.percentEncodedQueryItems = queryItems.map {
595-
Foundation.URLQueryItem(name: $0.name, value: $0.value)
598+
URLQueryItem(name: $0.name, value: $0.value)
596599
}
597600
}
598601
guard let url = components.url else { throw URLSessionHTTPClientError.incompleteHTTPRequest }
@@ -622,6 +625,25 @@ public final class URLSessionHTTPClient: HTTPClient {
622625
}
623626
}
624627

628+
private func logBodyDescription(_ body: Body) {
629+
switch body {
630+
case .stream(let stream):
631+
let lengthString: String
632+
if let length = stream.readableStream.length {
633+
lengthString = "\(length) bytes"
634+
} else {
635+
lengthString = "unknown length"
636+
}
637+
logger.debug("body is InputStream (\(lengthString))")
638+
case .data(let data):
639+
if let data {
640+
logger.debug("body is Data (\(data.count) bytes)")
641+
} else {
642+
logger.debug("body is empty")
643+
}
644+
}
645+
}
646+
625647
private static func makeServerAddress(sessionTask: URLSessionTask) -> String {
626648
let url = sessionTask.originalRequest?.url
627649
let host = url?.host ?? "unknown"

0 commit comments

Comments
 (0)