Skip to content

Commit

Permalink
chore: bump aws-crt-swift to 0.5.6 (#518)
Browse files Browse the repository at this point in the history
Co-authored-by: Ed Paulosky <eeppaauu@amazon.com>
  • Loading branch information
Ganesh Jangir and epau authored Jan 31, 2023
1 parent b040af1 commit 5b797bb
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 40 deletions.
2 changes: 1 addition & 1 deletion Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ let package = Package(
.library(name: "SmithyTestUtil", targets: ["SmithyTestUtil"])
],
dependencies: [
.package(url: "https://github.com/awslabs/aws-crt-swift.git", .exact("0.5.4")),
.package(url: "https://github.com/awslabs/aws-crt-swift.git", .exact("0.5.6")),
.package(url: "https://github.com/apple/swift-log.git", from: "1.0.0"),
.package(url: "https://github.com/MaxDesiatov/XMLCoder.git", from: "0.13.0")
],
Expand Down
2 changes: 1 addition & 1 deletion Sources/ClientRuntime/Networking/ClientError.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import AwsCommonRuntimeKit

public enum ClientError: Error, Equatable {
case networkError(Error)
case crtError(CRTError)
case crtError(CommonRunTimeError)
case pathCreationFailed(String)
case queryItemCreationFailed(String)
case serializationFailed(String)
Expand Down
56 changes: 26 additions & 30 deletions Sources/ClientRuntime/Networking/Http/CRT/CRTClientEngine.swift
Original file line number Diff line number Diff line change
Expand Up @@ -103,39 +103,35 @@ public class CRTClientEngine: HttpClientEngine {
let crtRequest = try request.toHttpRequest()
let streamReader: StreamReader = DataStreamReader()

let makeStatusCode: (HTTPStream) -> HttpStatusCode = { stream in
guard
let statusCodeInt = try? stream.statusCode(),
let statusCode = HttpStatusCode(rawValue: statusCodeInt)
else { return .notFound }
return statusCode
}

let requestOptions = HTTPRequestOptions(request: crtRequest) { [self] (stream, _, httpHeaders) in
logger.debug("headers were received")
response.statusCode = makeStatusCode(stream)
response.headers.addAll(httpHeaders: httpHeaders)
} onIncomingHeadersBlockDone: { [self] (stream, _) in
logger.debug("header block is done")
response.statusCode = makeStatusCode(stream)
} onIncomingBody: { [self] (stream, data) in
logger.debug("incoming data")
response.statusCode = makeStatusCode(stream)
let byteBuffer = ByteBuffer(data: data)
let makeStatusCode: (UInt32) -> HttpStatusCode = { statusCode in
HttpStatusCode(rawValue: Int(statusCode)) ?? .notFound
}

let requestOptions = HTTPRequestOptions(request: crtRequest) { statusCode, headers in
response.statusCode = makeStatusCode(statusCode)
response.headers.addAll(headers: Headers(httpHeaders: headers))
} onResponse: { statusCode, headers in
response.statusCode = makeStatusCode(statusCode)
response.headers.addAll(headers: Headers(httpHeaders: headers))
} onIncomingBody: { bodyChunk in
let byteBuffer = ByteBuffer(data: bodyChunk)
streamReader.write(buffer: byteBuffer)
} onStreamComplete: { [self] (stream, error) in
logger.debug("stream completed")
if let error = error, error.code != 0 {
logger.error("Response encountered an error: \(error)")
continuation.resume(throwing: CommonRunTimeError.crtError(error))
return
} onTrailer: { headers in
response.headers.addAll(headers: Headers(httpHeaders: headers))
} onStreamComplete: { result in
streamReader.hasFinishedWriting = true
switch result {
case .success(let statusCode):
response.statusCode = makeStatusCode(statusCode)
continuation.resume(returning: response)
case .failure(let error):
self.logger.error("Response encountered an error: \(error)")
streamReader.onError(error: .crtError(error))
continuation.resume(throwing: error)
}

response.body = .stream(.reader(streamReader))
response.statusCode = makeStatusCode(stream)

continuation.resume(returning: response)
}

response.body = .stream(.reader(streamReader))
return requestOptions
}
}
23 changes: 17 additions & 6 deletions Sources/ClientRuntime/Networking/Http/SdkHttpRequest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -70,18 +70,29 @@ extension SdkHttpRequest: CustomDebugStringConvertible, CustomStringConvertible
}

extension SdkHttpRequestBuilder {
public func update(from crtRequest: HTTPRequest, originalRequest: SdkHttpRequest) -> SdkHttpRequestBuilder {

/// Update the builder with the values from the CRT request
/// - Parameters:
/// - crtRequest: the CRT request, this can be either a `HTTPRequest` or a `HTTP2Request`
/// - originalRequest: the SDK request that is used to hold the original values
/// - Returns: the builder
public func update(from crtRequest: HTTPRequestBase, originalRequest: SdkHttpRequest) -> SdkHttpRequestBuilder {
headers = convertSignedHeadersToHeaders(crtRequest: crtRequest)
methodType = originalRequest.method
host = originalRequest.endpoint.host
let pathAndQueryItems = URLComponents(string: crtRequest.path)
path = pathAndQueryItems?.path ?? "/"
queryItems = pathAndQueryItems?.percentEncodedQueryItems ?? [URLQueryItem]()

if let crtRequest = crtRequest as? HTTPRequest {
let pathAndQueryItems = URLComponents(string: crtRequest.path)
path = pathAndQueryItems?.path ?? "/"
queryItems = pathAndQueryItems?.percentEncodedQueryItems ?? [URLQueryItem]()
} else if crtRequest as? HTTP2Request != nil {
assertionFailure("HTTP2Request not supported")
} else {
assertionFailure("Unknown request type")
}
return self
}

func convertSignedHeadersToHeaders(crtRequest: HTTPRequest) -> Headers {
func convertSignedHeadersToHeaders(crtRequest: HTTPRequestBase) -> Headers {
return Headers(httpHeaders: crtRequest.getHeaders())
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ open class HttpResponseTestBase: XCTestCase {

return HttpResponse(headers: internalHeaders,
body: content,
statusCode: HttpStatusCode(rawValue: code) ?? HttpStatusCode.badRequest)
statusCode: HttpStatusCode(rawValue: Int(code)) ?? HttpStatusCode.badRequest)

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ class ClientErrorTests: XCTestCase {
}

func test_waiterErrorType_returnsNilForCRTError() async throws {
let crtError = CRTError(code: 2)
let crtError = CommonRunTimeError.crtError(.init(code: 2))
let subject = ClientError.crtError(crtError)
XCTAssertNil(subject.waiterErrorType)
}
Expand Down

0 comments on commit 5b797bb

Please sign in to comment.