Skip to content

Commit 57ded00

Browse files
authored
Update proxy-aware samples with new proxy options (#274)
* Update proxy-aware samples with new proxy options
1 parent df16ccb commit 57ded00

File tree

3 files changed

+41
-34
lines changed

3 files changed

+41
-34
lines changed

samples/mqtt/basic_pub_sub/main.cpp

Lines changed: 25 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,8 @@ static void s_printHelp()
4444
stdout,
4545
"signing_region: used for websocket signer it should only be specific if websockets are used. (required for "
4646
"websockets)\n");
47-
fprintf(stdout, "proxy_host: if you want to use a proxy with websockets, specify the host here (optional).\n");
48-
fprintf(
49-
stdout, "proxy_port: defaults to 8080 is proxy_host is set. Set this to any value you'd like (optional).\n");
47+
fprintf(stdout, "proxy_host: host name of the http proxy to use (optional).\n");
48+
fprintf(stdout, "proxy_port: port of the http proxy to use (optional).\n");
5049

5150
fprintf(stdout, " x509: Use the x509 credentials provider while using websockets (optional)\n");
5251
fprintf(stdout, " x509_role_alias: Role alias to use with the x509 credentials provider (required for x509)\n");
@@ -155,15 +154,19 @@ int main(int argc, char *argv[])
155154
}
156155
useWebSocket = true;
157156
signingRegion = s_getCmdOption(argv, argv + argc, "--signing_region");
157+
}
158158

159-
if (s_cmdOptionExists(argv, argv + argc, "--proxy_host"))
160-
{
161-
proxyHost = s_getCmdOption(argv, argv + argc, "--proxy_host");
162-
}
159+
if (s_cmdOptionExists(argv, argv + argc, "--proxy_host"))
160+
{
161+
proxyHost = s_getCmdOption(argv, argv + argc, "--proxy_host");
162+
}
163163

164-
if (s_cmdOptionExists(argv, argv + argc, "--proxy_port"))
164+
if (s_cmdOptionExists(argv, argv + argc, "--proxy_port"))
165+
{
166+
int port = atoi(s_getCmdOption(argv, argv + argc, "--proxy_port"));
167+
if (port > 0 && port <= UINT16_MAX)
165168
{
166-
proxyPort = static_cast<uint16_t>(atoi(s_getCmdOption(argv, argv + argc, "--proxy_port")));
169+
proxyPort = static_cast<uint16_t>(port);
167170
}
168171
}
169172

@@ -265,6 +268,14 @@ int main(int argc, char *argv[])
265268
exit(-1);
266269
}
267270

271+
Aws::Crt::Http::HttpClientConnectionProxyOptions proxyOptions;
272+
if (!proxyHost.empty())
273+
{
274+
proxyOptions.HostName = proxyHost;
275+
proxyOptions.Port = proxyPort;
276+
proxyOptions.AuthType = Aws::Crt::Http::AwsHttpProxyAuthenticationType::None;
277+
}
278+
268279
Aws::Crt::Io::TlsContext x509TlsCtx;
269280
Aws::Iot::MqttClientConnectionConfigBuilder builder;
270281

@@ -275,15 +286,6 @@ int main(int argc, char *argv[])
275286
else if (useWebSocket)
276287
{
277288
std::shared_ptr<Aws::Crt::Auth::ICredentialsProvider> provider = nullptr;
278-
279-
Aws::Crt::Http::HttpClientConnectionProxyOptions proxyOptions;
280-
if (!proxyHost.empty())
281-
{
282-
proxyOptions.HostName = proxyHost;
283-
proxyOptions.Port = proxyPort;
284-
proxyOptions.AuthType = Aws::Crt::Http::AwsHttpProxyAuthenticationType::None;
285-
}
286-
287289
if (useX509)
288290
{
289291
Aws::Crt::Io::TlsContextOptions tlsCtxOptions =
@@ -351,18 +353,18 @@ int main(int argc, char *argv[])
351353
}
352354

353355
Aws::Iot::WebsocketConfig config(signingRegion, provider);
354-
if (!proxyHost.empty())
355-
{
356-
config.ProxyOptions = proxyOptions;
357-
}
358-
359356
builder = Aws::Iot::MqttClientConnectionConfigBuilder(config);
360357
}
361358
else
362359
{
363360
s_printHelp();
364361
}
365362

363+
if (!proxyHost.empty())
364+
{
365+
builder.WithHttpProxyOptions(proxyOptions);
366+
}
367+
366368
if (!caFile.empty())
367369
{
368370
builder.WithCertificateAuthority(caFile.c_str());

samples/mqtt/raw_pub_sub/main.cpp

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include <algorithm>
1212
#include <aws/crt/UUID.h>
1313
#include <condition_variable>
14+
#include <cstdint>
1415
#include <iostream>
1516
#include <mutex>
1617

@@ -36,9 +37,8 @@ static void s_printHelp()
3637
" in your trust store, set this.\n");
3738
fprintf(stdout, "\tIt's the path to a CA file in PEM format\n");
3839
fprintf(stdout, "use_websocket: if specified, uses a websocket over https (optional)\n");
39-
fprintf(stdout, "proxy_host: if you want to use a proxy with websockets, specify the host here (optional).\n");
40-
fprintf(
41-
stdout, "proxy_port: defaults to 8080 is proxy_host is set. Set this to any value you'd like (optional).\n");
40+
fprintf(stdout, "proxy_host: host name of the http proxy to use (optional).\n");
41+
fprintf(stdout, "proxy_port: port of the http proxy to use (optional).\n");
4242
fprintf(stdout, "user_name: User name to send with mqtt connect.\n");
4343
fprintf(stdout, "password: Password to send with mqtt connect.\n");
4444
fprintf(stdout, "protocol_name: (optional) defaults to x-amzn-mqtt-ca.\n");
@@ -124,14 +124,19 @@ int main(int argc, char *argv[])
124124
{
125125
protocolName = "http/1.1";
126126
useWebSocket = true;
127-
if (s_cmdOptionExists(argv, argv + argc, "--proxy_host"))
128-
{
129-
proxyHost = s_getCmdOption(argv, argv + argc, "--proxy_host");
130-
}
127+
}
131128

132-
if (s_cmdOptionExists(argv, argv + argc, "--proxy_port"))
129+
if (s_cmdOptionExists(argv, argv + argc, "--proxy_host"))
130+
{
131+
proxyHost = s_getCmdOption(argv, argv + argc, "--proxy_host");
132+
}
133+
134+
if (s_cmdOptionExists(argv, argv + argc, "--proxy_port"))
135+
{
136+
int port = atoi(s_getCmdOption(argv, argv + argc, "--proxy_port"));
137+
if (port > 0 && port <= UINT16_MAX)
133138
{
134-
proxyPort = static_cast<uint16_t>(atoi(s_getCmdOption(argv, argv + argc, "--proxy_port")));
139+
proxyPort = static_cast<uint16_t>(port);
135140
}
136141
}
137142

@@ -274,7 +279,7 @@ int main(int argc, char *argv[])
274279
Http::HttpClientConnectionProxyOptions proxyOptions;
275280
proxyOptions.HostName = proxyHost;
276281
proxyOptions.Port = proxyPort;
277-
connection->SetWebsocketProxyOptions(proxyOptions);
282+
connection->SetHttpProxyOptions(proxyOptions);
278283
}
279284

280285
/*

0 commit comments

Comments
 (0)