Skip to content

Commit 07a1bf0

Browse files
authored
fix: utilize endpoint ports (#311)
1 parent 820f2fe commit 07a1bf0

File tree

6 files changed

+58
-6
lines changed

6 files changed

+58
-6
lines changed

aws-runtime/auth/common/src/aws/sdk/kotlin/runtime/auth/Presigner.kt

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,13 @@ public suspend fun createPresignedRequest(serviceConfig: ServicePresignConfig, r
8888
expirationInSeconds = requestConfig.durationSeconds
8989
}
9090

91-
val unsignedUrl = Url(Protocol.HTTPS, endpoint.hostname, path = requestConfig.path, parameters = requestConfig.queryString)
91+
val unsignedUrl = Url(
92+
scheme = Protocol.HTTPS,
93+
host = endpoint.hostname,
94+
port = endpoint.port ?: Protocol.HTTPS.defaultPort,
95+
path = requestConfig.path,
96+
parameters = requestConfig.queryString,
97+
)
9298

9399
val request = CrtHttpRequest(
94100
requestConfig.method.name,
@@ -105,9 +111,10 @@ public suspend fun createPresignedRequest(serviceConfig: ServicePresignConfig, r
105111
url = Url(
106112
scheme = Protocol.HTTPS,
107113
host = endpoint.hostname,
114+
port = endpoint.port ?: Protocol.HTTPS.defaultPort,
108115
path = signedRequest.path(),
109116
parameters = signedRequest.queryParameters() ?: QueryParameters.Empty,
110-
encodeParameters = false
117+
encodeParameters = false,
111118
),
112119
headers = signedRequest.headers.toSdkHeaders(),
113120
body = HttpBody.Empty

aws-runtime/http-client-engine-crt/common/src/aws/sdk/kotlin/runtime/http/engine/crt/CrtHttpEngine.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ public class CrtHttpEngine(public val config: HttpClientEngineConfig) : HttpClie
8282
}
8383

8484
private suspend fun getManagerForUri(uri: Uri): HttpClientConnectionManager = mutex.withLock {
85-
connManagers.getOrPut(uri.host) {
85+
connManagers.getOrPut(uri.authority) {
8686
HttpClientConnectionManager(options.apply { this.uri = uri }.build())
8787
}
8888
}

aws-runtime/http-client-engine-crt/jvm/test/aws/sdk/kotlin/runtime/http/engine/crt/AsyncStressTest.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ class AsyncStressTest : TestWithLocalServer() {
4848
scheme = Protocol.HTTP
4949
method = HttpMethod.GET
5050
host = testHost
51+
port = serverPort
5152
path = "/largeResponse"
5253
}
5354
}
@@ -82,6 +83,7 @@ class AsyncStressTest : TestWithLocalServer() {
8283
scheme = Protocol.HTTP
8384
method = HttpMethod.GET
8485
host = testHost
86+
port = serverPort
8587
path = "/largeResponse"
8688
}
8789
}

aws-runtime/http-client-engine-crt/jvm/test/aws/sdk/kotlin/runtime/http/engine/crt/TestWithLocalServer.kt

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,7 @@ import kotlin.test.BeforeTest
1414

1515
public abstract class TestWithLocalServer {
1616
protected val serverPort: Int = ServerSocket(0).use { it.localPort }
17-
protected val testHost: String = "localhost:$serverPort"
18-
protected val testUrl: String = "http://$testHost"
17+
protected val testHost: String = "localhost"
1918

2019
public abstract val server: ApplicationEngine
2120

@@ -29,7 +28,7 @@ public abstract class TestWithLocalServer {
2928
attempt++
3029
try {
3130
server.start()
32-
logger.info { "test server listening on: $testHost" }
31+
logger.info { "test server listening on: $testHost:$serverPort" }
3332
break
3433
} catch (cause: Throwable) {
3534
if (attempt >= 10) throw cause

aws-runtime/protocols/http/common/src/aws/sdk/kotlin/runtime/http/middleware/ServiceEndpointResolver.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ public class ServiceEndpointResolver(
5555
val hostname = "${hostPrefix}${endpoint.hostname}"
5656
req.subject.url.scheme = Protocol.parse(endpoint.protocol)
5757
req.subject.url.host = hostname
58+
req.subject.url.port = endpoint.port
5859
req.subject.headers["Host"] = hostname
5960

6061
endpoint.signingName?.let {

aws-runtime/protocols/http/common/test/aws/sdk/kotlin/runtime/http/middleware/ServiceEndpointResolverTest.kt

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ class ServiceEndpointResolverTest {
3333
assertEquals(expectedHost, request.url.host)
3434
assertEquals(expectedHost, request.headers["Host"])
3535
assertEquals("https", request.url.scheme.protocolName)
36+
assertEquals(443, request.url.port)
3637
val resp = HttpResponse(HttpStatusCode.fromValue(200), Headers.Empty, HttpBody.Empty)
3738
val now = Instant.now()
3839
return HttpCall(request, resp, now, now)
@@ -63,6 +64,48 @@ class ServiceEndpointResolverTest {
6364
assertNotNull(response)
6465
}
6566

67+
@Test
68+
fun `it sets the port when present`(): Unit = runSuspendTest {
69+
val expectedProtocol = "https"
70+
val expectedHost = "test.com"
71+
val expectedPort = 8080
72+
73+
val mockEngine = object : HttpClientEngineBase("test") {
74+
override suspend fun roundTrip(request: HttpRequest): HttpCall {
75+
assertEquals(expectedHost, request.url.host)
76+
assertEquals(expectedHost, request.headers["Host"])
77+
assertEquals(expectedProtocol, request.url.scheme.protocolName)
78+
assertEquals(expectedPort, request.url.port)
79+
val resp = HttpResponse(HttpStatusCode.fromValue(200), Headers.Empty, HttpBody.Empty)
80+
val now = Instant.now()
81+
return HttpCall(request, resp, now, now)
82+
}
83+
}
84+
85+
val client = sdkHttpClient(mockEngine)
86+
87+
val op = SdkHttpOperation.build<Unit, HttpResponse> {
88+
serializer = UnitSerializer
89+
deserializer = IdentityDeserializer
90+
context {
91+
service = "TestService"
92+
operationName = "testOperation"
93+
}
94+
}
95+
96+
op.install(ServiceEndpointResolver) {
97+
serviceId = "TestService"
98+
resolver = object : EndpointResolver {
99+
override suspend fun resolve(service: String, region: String): Endpoint =
100+
Endpoint(expectedHost, expectedProtocol, port = expectedPort)
101+
}
102+
}
103+
104+
op.context[AwsClientOption.Region] = "us-east-1"
105+
val response = op.roundTrip(client, Unit)
106+
assertNotNull(response)
107+
}
108+
66109
@Test
67110
fun `it prepends hostPrefix when present`(): Unit = runSuspendTest {
68111
val expectedHost = "prefix.test.com"

0 commit comments

Comments
 (0)