Skip to content

Commit cce7107

Browse files
authored
H1b (#56)
* Update for h1b API changes * Lambda capture scope crash * Options updates, formatting * Update CRT version dependency
1 parent b1fb3a5 commit cce7107

File tree

8 files changed

+262
-167
lines changed

8 files changed

+262
-167
lines changed

aws-common-runtime/CMakeLists.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,5 +18,5 @@ set(AWS_DEPS_DOWNLOAD_DIR "${AWS_DEPS_BUILD_DIR}/downloads" CACHE PATH "Dependen
1818
message("install dir ${AWS_DEPS_INSTALL_DIR}")
1919

2020
set(AWS_CRT_CPP_URL "https://github.com/awslabs/aws-crt-cpp.git")
21-
set(AWS_CRT_CPP_SHA "v0.4.6")
21+
set(AWS_CRT_CPP_SHA "v0.5.3")
2222
include(BuildAwsCrtCpp)

codebuild/common-posix.sh

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ fi
1717

1818
# build aws-crt-cpp
1919
pushd $BUILD_PATH
20-
git clone --branch v0.4.6 https://github.com/awslabs/aws-crt-cpp.git
20+
git clone --branch v0.5.3 https://github.com/awslabs/aws-crt-cpp.git
2121
cd aws-crt-cpp
2222
cmake $CMAKE_ARGS -DBUILD_DEPS=ON ./
2323
cmake --build . --target install

codebuild/common-windows.bat

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ mkdir %INSTALL_DIR%
88
@rem build aws-crt-cpp
99
mkdir %BUILDS_DIR%\aws-crt-cpp-build
1010
cd %BUILDS_DIR%\aws-crt-cpp-build
11-
git clone --branch v0.4.6 https://github.com/awslabs/aws-crt-cpp.git
11+
git clone --branch v0.5.3 https://github.com/awslabs/aws-crt-cpp.git
1212
cmake %CMAKE_ARGS% -DCMAKE_INSTALL_PREFIX="%INSTALL_DIR%" -DCMAKE_PREFIX_PATH="%INSTALL_DIR%" -DCMAKE_BUILD_TYPE="Release" -DBUILD_DEPS=ON aws-crt-cpp || goto error
1313
cmake --build . --target install || goto error
1414

discovery/include/aws/discovery/DiscoveryClient.h

+51-11
Original file line numberDiff line numberDiff line change
@@ -22,26 +22,66 @@ namespace Aws
2222
{
2323
using OnDiscoverResponse = std::function<void(DiscoverResponse *, int errorCode, int httpResponseCode)>;
2424

25-
struct DiscoveryClientConfig
25+
class AWS_DISCOVERY_API DiscoveryClientConfig
2626
{
27+
public:
2728
DiscoveryClientConfig() noexcept;
28-
Crt::Allocator *allocator;
29-
Crt::Io::ClientBootstrap *bootstrap;
30-
Crt::Io::TlsContext *tlsContext;
31-
Crt::Io::SocketOptions *socketOptions;
32-
Crt::String region;
33-
size_t maxConnections;
34-
// TODO: when supported add proxy configuration.
29+
DiscoveryClientConfig(const DiscoveryClientConfig &rhs) = default;
30+
DiscoveryClientConfig(DiscoveryClientConfig &&rhs) = default;
31+
32+
DiscoveryClientConfig &operator=(const DiscoveryClientConfig &rhs) = default;
33+
DiscoveryClientConfig &operator=(DiscoveryClientConfig &&rhs) = default;
34+
35+
~DiscoveryClientConfig() = default;
36+
37+
/**
38+
* The client bootstrap to use for setting up and tearing down connections.
39+
* Required.
40+
*/
41+
Crt::Io::ClientBootstrap *Bootstrap;
42+
43+
/**
44+
* The TLS options for all http connections made by this client.
45+
* Optional.
46+
*/
47+
Crt::Optional<Crt::Io::TlsContext> TlsContext;
48+
49+
/**
50+
* The socket options of the connections made by the client.
51+
* Required.
52+
*/
53+
Crt::Io::SocketOptions SocketOptions;
54+
55+
/**
56+
* The value of the Aws region to connect to.
57+
* Required.
58+
*/
59+
Crt::String Region;
60+
61+
/**
62+
* The maximum number of concurrent connections allowed
63+
*/
64+
size_t MaxConnections;
65+
66+
/**
67+
* The proxy options for all http connections made by this client.
68+
* Optional.
69+
*/
70+
Crt::Optional<Crt::Http::HttpClientConnectionProxyOptions> ProxyOptions;
3571
};
3672

37-
class DiscoveryClient final
73+
class AWS_DISCOVERY_API DiscoveryClient final
3874
{
3975
public:
40-
DiscoveryClient(const DiscoveryClientConfig &) noexcept;
41-
4276
bool Discover(const Crt::String &thingName, const OnDiscoverResponse &onDiscoverResponse) noexcept;
4377

78+
static std::shared_ptr<DiscoveryClient> CreateClient(
79+
const DiscoveryClientConfig &config,
80+
Crt::Allocator *allocator = Crt::DefaultAllocator());
81+
4482
private:
83+
DiscoveryClient(const DiscoveryClientConfig &config, Crt::Allocator *allocator) noexcept;
84+
4585
std::shared_ptr<Crt::Http::HttpClientConnectionManager> m_connectionManager;
4686
Crt::String m_hostName;
4787
Crt::Allocator *m_allocator;

discovery/source/DiscoveryClient.cpp

+107-73
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@
1313
*/
1414
#include <aws/discovery/DiscoveryClient.h>
1515

16+
#include <aws/crt/Api.h>
1617
#include <aws/crt/Types.h>
18+
#include <aws/crt/http/HttpRequestResponse.h>
1719
#include <aws/crt/io/TlsOptions.h>
1820
#include <aws/crt/io/Uri.h>
1921

@@ -22,23 +24,23 @@ namespace Aws
2224
namespace Discovery
2325
{
2426
DiscoveryClientConfig::DiscoveryClientConfig() noexcept
25-
: allocator(Crt::DefaultAllocator()), bootstrap(nullptr), tlsContext(nullptr), socketOptions(nullptr),
26-
region("us-east-1"), maxConnections(2)
27+
: Bootstrap(nullptr), TlsContext(), SocketOptions(), Region(), MaxConnections(2), ProxyOptions()
2728
{
2829
}
2930

30-
DiscoveryClient::DiscoveryClient(const Aws::Discovery::DiscoveryClientConfig &clientConfig) noexcept
31+
DiscoveryClient::DiscoveryClient(
32+
const Aws::Discovery::DiscoveryClientConfig &clientConfig,
33+
Crt::Allocator *allocator) noexcept
3134
{
32-
AWS_ASSERT(clientConfig.tlsContext);
33-
AWS_ASSERT(clientConfig.bootstrap);
34-
AWS_ASSERT(clientConfig.socketOptions);
35+
AWS_FATAL_ASSERT(clientConfig.TlsContext);
36+
AWS_FATAL_ASSERT(clientConfig.Bootstrap);
3537

36-
m_allocator = clientConfig.allocator;
38+
m_allocator = allocator;
3739

3840
Crt::StringStream ss;
39-
ss << "greengrass-ats.iot." << clientConfig.region << ".amazonaws.com";
41+
ss << "greengrass-ats.iot." << clientConfig.Region << ".amazonaws.com";
4042

41-
Crt::Io::TlsConnectionOptions tlsConnectionOptions = clientConfig.tlsContext->NewConnectionOptions();
43+
Crt::Io::TlsConnectionOptions tlsConnectionOptions = clientConfig.TlsContext->NewConnectionOptions();
4244
uint16_t port = 443;
4345

4446
if (Crt::Io::TlsContextOptions::IsAlpnSupported())
@@ -54,17 +56,38 @@ namespace Aws
5456
Crt::ByteCursor serverName = Crt::ByteCursorFromCString(m_hostName.c_str());
5557
tlsConnectionOptions.SetServerName(serverName);
5658

59+
Crt::Http::HttpClientConnectionOptions connectionOptions;
60+
connectionOptions.SocketOptions = clientConfig.SocketOptions;
61+
connectionOptions.Bootstrap = clientConfig.Bootstrap;
62+
connectionOptions.TlsOptions = tlsConnectionOptions;
63+
connectionOptions.HostName = Crt::String((const char *)serverName.ptr, serverName.len);
64+
connectionOptions.Port = port;
65+
if (clientConfig.ProxyOptions)
66+
{
67+
connectionOptions.ProxyOptions = *clientConfig.ProxyOptions;
68+
}
69+
5770
Crt::Http::HttpClientConnectionManagerOptions connectionManagerOptions;
58-
connectionManagerOptions.socketOptions = clientConfig.socketOptions;
59-
connectionManagerOptions.bootstrap = clientConfig.bootstrap;
60-
connectionManagerOptions.tlsConnectionOptions = &tlsConnectionOptions;
61-
connectionManagerOptions.maxConnections = clientConfig.maxConnections;
62-
connectionManagerOptions.initialWindowSize = SIZE_MAX;
63-
connectionManagerOptions.hostName = serverName;
64-
connectionManagerOptions.port = port;
65-
66-
m_connectionManager = Crt::Http::HttpClientConnectionManager::NewClientConnectionManager(
67-
connectionManagerOptions, clientConfig.allocator);
71+
connectionManagerOptions.ConnectionOptions = connectionOptions;
72+
connectionManagerOptions.MaxConnections = clientConfig.MaxConnections;
73+
74+
m_connectionManager =
75+
Crt::Http::HttpClientConnectionManager::NewClientConnectionManager(connectionManagerOptions, allocator);
76+
}
77+
78+
std::shared_ptr<DiscoveryClient> DiscoveryClient::CreateClient(
79+
const Aws::Discovery::DiscoveryClientConfig &clientConfig,
80+
Crt::Allocator *allocator)
81+
{
82+
auto *toSeat = static_cast<DiscoveryClient *>(aws_mem_acquire(allocator, sizeof(DiscoveryClient)));
83+
if (toSeat)
84+
{
85+
toSeat = new (toSeat) DiscoveryClient(clientConfig, allocator);
86+
return std::shared_ptr<DiscoveryClient>(
87+
toSeat, [allocator](DiscoveryClient *client) { Crt::Delete(client, allocator); });
88+
}
89+
90+
return nullptr;
6891
}
6992

7093
struct ClientCallbackContext
@@ -77,8 +100,7 @@ namespace Aws
77100
const Crt::String &thingName,
78101
const OnDiscoverResponse &onDiscoverResponse) noexcept
79102
{
80-
81-
auto *callbackContext = Crt::New<ClientCallbackContext>(m_allocator);
103+
auto callbackContext = Crt::MakeShared<ClientCallbackContext>(m_allocator);
82104
if (!callbackContext)
83105
{
84106
return false;
@@ -89,69 +111,81 @@ namespace Aws
89111
bool res = m_connectionManager->AcquireConnection(
90112
[this, callbackContext, thingName, onDiscoverResponse](
91113
std::shared_ptr<Crt::Http::HttpClientConnection> connection, int errorCode) {
92-
if (!errorCode)
114+
if (errorCode)
93115
{
94-
Crt::StringStream ss;
95-
ss << "/greengrass/discover/thing/" << thingName;
96-
Crt::String uriStr = ss.str();
97-
Crt::Http::HttpRequestOptions requestOptions;
98-
requestOptions.uri = Crt::ByteCursorFromCString(uriStr.c_str());
99-
requestOptions.method = Crt::ByteCursorFromCString("GET");
100-
101-
Crt::Http::HttpHeader hostNameHeader;
102-
hostNameHeader.name = Crt::ByteCursorFromCString("host");
103-
hostNameHeader.value = Crt::ByteCursorFromCString(m_hostName.c_str());
104-
105-
requestOptions.headerArray = &hostNameHeader;
106-
requestOptions.headerArrayLength = 1;
107-
108-
requestOptions.onStreamOutgoingBody = nullptr;
109-
requestOptions.onIncomingHeaders =
110-
[](Crt::Http::HttpStream &, const Crt::Http::HttpHeader *, std::size_t) {};
111-
requestOptions.onIncomingHeadersBlockDone =
112-
[callbackContext](Crt::Http::HttpStream &stream, bool) {
113-
callbackContext->responseCode = stream.GetResponseStatusCode();
114-
};
115-
requestOptions.onIncomingBody =
116-
[callbackContext](Crt::Http::HttpStream &, const Crt::ByteCursor &data, std::size_t &) {
117-
callbackContext->ss.write(reinterpret_cast<const char *>(data.ptr), data.len);
118-
};
119-
requestOptions.onStreamComplete = [this, connection, callbackContext, onDiscoverResponse](
120-
Crt::Http::HttpStream &stream, int errorCode) {
121-
if (!errorCode && callbackContext->responseCode == 200)
122-
{
123-
Crt::JsonObject jsonObject(callbackContext->ss.str());
124-
DiscoverResponse response(jsonObject.View());
125-
onDiscoverResponse(&response, AWS_ERROR_SUCCESS, callbackContext->responseCode);
126-
}
127-
else
128-
{
129-
if (!errorCode)
130-
{
131-
errorCode = AWS_ERROR_UNKNOWN;
132-
}
133-
onDiscoverResponse(nullptr, errorCode, callbackContext->responseCode);
134-
}
116+
onDiscoverResponse(nullptr, errorCode, 0);
117+
return;
118+
}
135119

136-
Crt::Delete(callbackContext, m_allocator);
137-
};
120+
auto request = Aws::Crt::MakeShared<Crt::Http::HttpRequest>(m_allocator, m_allocator);
121+
if (request == nullptr)
122+
{
123+
onDiscoverResponse(nullptr, Crt::LastErrorOrUnknown(), 0);
124+
return;
125+
}
138126

139-
if (!connection->NewClientStream(requestOptions))
140-
{
141-
onDiscoverResponse(nullptr, aws_last_error(), 0);
142-
Crt::Delete(callbackContext, m_allocator);
143-
}
127+
Crt::StringStream ss;
128+
ss << "/greengrass/discover/thing/" << thingName;
129+
Crt::String uriStr = ss.str();
130+
if (!request->SetMethod(Crt::ByteCursorFromCString("GET")))
131+
{
132+
onDiscoverResponse(nullptr, Crt::LastErrorOrUnknown(), 0);
133+
return;
134+
}
144135

136+
if (!request->SetPath(Crt::ByteCursorFromCString(uriStr.c_str())))
137+
{
138+
onDiscoverResponse(nullptr, Crt::LastErrorOrUnknown(), 0);
139+
return;
140+
}
141+
142+
Crt::Http::HttpHeader hostNameHeader;
143+
hostNameHeader.name = Crt::ByteCursorFromCString("host");
144+
hostNameHeader.value = Crt::ByteCursorFromCString(m_hostName.c_str());
145+
146+
if (!request->AddHeader(hostNameHeader))
147+
{
148+
onDiscoverResponse(nullptr, Crt::LastErrorOrUnknown(), 0);
145149
return;
146150
}
147151

148-
onDiscoverResponse(nullptr, errorCode, 0);
149-
Crt::Delete(callbackContext, m_allocator);
152+
Crt::Http::HttpRequestOptions requestOptions;
153+
requestOptions.request = request.get();
154+
requestOptions.onIncomingHeaders =
155+
[](Crt::Http::HttpStream &, const Crt::Http::HttpHeader *, std::size_t) {};
156+
requestOptions.onIncomingHeadersBlockDone = [callbackContext](Crt::Http::HttpStream &stream, bool) {
157+
callbackContext->responseCode = stream.GetResponseStatusCode();
158+
};
159+
requestOptions.onIncomingBody =
160+
[callbackContext](Crt::Http::HttpStream &, const Crt::ByteCursor &data) {
161+
callbackContext->ss.write(reinterpret_cast<const char *>(data.ptr), data.len);
162+
};
163+
requestOptions.onStreamComplete = [request, connection, callbackContext, onDiscoverResponse](
164+
Crt::Http::HttpStream &, int errorCode) {
165+
if (!errorCode && callbackContext->responseCode == 200)
166+
{
167+
Crt::JsonObject jsonObject(callbackContext->ss.str());
168+
DiscoverResponse response(jsonObject.View());
169+
onDiscoverResponse(&response, AWS_ERROR_SUCCESS, callbackContext->responseCode);
170+
}
171+
else
172+
{
173+
if (!errorCode)
174+
{
175+
errorCode = AWS_ERROR_UNKNOWN;
176+
}
177+
onDiscoverResponse(nullptr, errorCode, callbackContext->responseCode);
178+
}
179+
};
180+
181+
if (!connection->NewClientStream(requestOptions))
182+
{
183+
onDiscoverResponse(nullptr, Crt::LastErrorOrUnknown(), 0);
184+
}
150185
});
151186

152187
if (!res)
153188
{
154-
Crt::Delete(callbackContext, m_allocator);
155189
return false;
156190
}
157191

0 commit comments

Comments
 (0)