Skip to content

Commit d09b5b5

Browse files
committed
put AWSClient2 back where it belongs, start implementing AWSv4 signatures
1 parent 02f70f3 commit d09b5b5

File tree

8 files changed

+503
-57
lines changed

8 files changed

+503
-57
lines changed

src/common/AWSClient2.cpp

Lines changed: 6 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,9 @@
1818
static const char* CANONICAL_FORM_POST_LINE = "POST\n/\n\n";
1919
static const int CANONICAL_FORM_POST_LINE_LEN = 8;
2020
static const char* HTTPS_REQUEST_POST_LINE =
21-
"POST https://%s/ HTTP/1.1\n";
21+
"POST https://%s.%s.%s/ HTTP/1.1\n";
2222
static const int HTTPS_REQUEST_POST_LINE_LEN = 28;
23-
static const char* HTTP_REQUEST_POST_LINE = "POST http://%s/ HTTP/1.1\n";
23+
static const char* HTTP_REQUEST_POST_LINE = "POST http://%s.%s.%s/ HTTP/1.1\n";
2424
static const int HTTP_REQUEST_POST_LINE_LEN = 27;
2525
static const char* TO_SIGN_TEMPLATE =
2626
"AWS4-HMAC-SHA256\n%sT%sZ\n%s/%s/%s/aws4_request\n%s";
@@ -62,11 +62,6 @@ void AWSClient2::setAWSEndpoint(const char * awsEndpoint) {
6262
this->awsEndpoint = new char[len]();
6363
strcpy(this->awsEndpoint, awsEndpoint);
6464
}
65-
void AWSClient2::setAWSDomain(const char * awsDomain) {
66-
int len = strlen(awsDomain) + 1;
67-
this->awsDomain = new char[len]();
68-
strcpy(this->awsDomain, awsDomain);
69-
}
7065
void AWSClient2::setAWSSecretKey(const char * awsSecKey) {
7166
int len = strlen(awsSecKey) + 1;
7267
this->awsSecKey = new char[len]();
@@ -125,16 +120,6 @@ void AWSClient2::initSignedHeaders() {
125120
headerLens[headersCreated++] = len;
126121
}
127122

128-
char* AWSClient2::createHostString(void) {
129-
if(awsDomain[0] != '\0') {
130-
return awsDomain;
131-
} else {
132-
char* host = new char[200]();
133-
sprintf(host, "%s.%s.%s", awsService, awsRegion, awsEndpoint);
134-
return host;
135-
}
136-
}
137-
138123
char* AWSClient2::createStringToSign(void) {
139124
SHA256* sha256 = new SHA256();
140125
char* hashed;
@@ -266,8 +251,8 @@ char* AWSClient2::headersToRequest() {
266251
httpS ? HTTPS_REQUEST_POST_LINE : HTTP_REQUEST_POST_LINE;
267252

268253
/* Calculate length of httpRequest string. */
269-
char* host = createHostString();
270-
int httpRequestLen = postLineLen + strlen(host);
254+
int httpRequestLen = postLineLen + strlen(awsService) + strlen(awsRegion)
255+
+ strlen(awsEndpoint);
271256
for (int i = 0; i < headersCreated; i++) {
272257
/* +1 for newline. */
273258
httpRequestLen += *(headerLens + i) + 1;
@@ -278,7 +263,8 @@ char* AWSClient2::headersToRequest() {
278263
/* Create and write to the httpRequest string. */
279264
char* httpRequest = new char[httpRequestLen + 1]();
280265
int httpRequestWritten = 0;
281-
httpRequestWritten += sprintf(httpRequest + httpRequestWritten, postLine, host);
266+
httpRequestWritten += sprintf(httpRequest + httpRequestWritten, postLine,
267+
awsService, awsRegion, awsEndpoint);
282268
for (int i = 0; i < headersCreated; i++) {
283269
httpRequestWritten += sprintf(httpRequest + httpRequestWritten, "%s\n",
284270
*(headers + i));

src/common/AWSClient2.h

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,13 @@
1212
#include "AWSFoundationalTypes.h"
1313

1414
/* Total number of headers. */
15-
static const int HEADER_COUNT2 = 7;
15+
static const int HEADER_COUNT = 7;
1616
/* Size of the awsDate string. */
17-
static const int AWS_DATE_LEN2 = 8;
17+
static const int AWS_DATE_LEN = 8;
1818
/* Size of the awsTime string. */
19-
static const int AWS_TIME_LEN2 = 6;
19+
static const int AWS_TIME_LEN = 6;
2020
/* Size of sha hashes and signatures in hexidecimal. */
21-
static const int HASH_HEX_LEN2 = 64;
21+
static const int HASH_HEX_LEN = 64;
2222

2323
/* Base class for an AWS Service Client. Creates http and https request in raw
2424
* http format or as a curl command. */
@@ -27,22 +27,20 @@ class AWSClient2 {
2727
char* awsRegion;
2828
/* Endpoint, eg. "amazonaws.com" in "kinesis.us-east-1.amazonaws.com". */
2929
char* awsEndpoint;
30-
/* Subdomain, eg. "A2MBBEONHC7LUH" in "A2MBBEONHC9LUG.iot.us-east-1.amazonaws.com". */
31-
char* awsDomain;
3230
/* The user's AWS Secret Key for accessing the AWS Resource. */
3331
char* awsSecKey;
3432
/* The user's AWS Access Key ID for accessing the AWS Resource. */
3533
char* awsKeyID;
3634
/* GMT date in yyyyMMdd format. */
37-
char awsDate[AWS_DATE_LEN2 + 1];
35+
char awsDate[AWS_DATE_LEN + 1];
3836
/* GMT time in HHmmss format. */
39-
char awsTime[AWS_TIME_LEN2 + 1];
37+
char awsTime[AWS_TIME_LEN + 1];
4038
/* Number of headers created. */
4139
int headersCreated;
4240
/* Array of the created http headers. */
43-
char* headers[HEADER_COUNT2];
41+
char* headers[HEADER_COUNT];
4442
/* Array of string lengths of the headers in the "headers" array. */
45-
int headerLens[HEADER_COUNT2];
43+
int headerLens[HEADER_COUNT];
4644
/* The payload of the httprequest to be created */
4745
MinimalString payload;
4846

@@ -78,8 +76,6 @@ class AWSClient2 {
7876
const char* awsService;
7977
/* Content type of payload, eg. "application/x-amz-json-1.1". */
8078
const char* contentType;
81-
// /* Generates the host based on subdomain, service, etc */
82-
// char* createHostString(void);
8379
/* Creates a raw http request, given the payload and current GMT date in
8480
* yyyyMMddHHmmss format. Should be exposed to user by extending class.
8581
* Returns 0 if client is unititialized. */
@@ -92,11 +88,8 @@ class AWSClient2 {
9288
public:
9389
/* Setters for values used by createRequest and createCurlRequest. Must
9490
* be set or create[Curl]Request will return null. */
95-
/* Generates the host based on subdomain, service, etc */
96-
char* createHostString(void);
9791
void setAWSRegion(const char * awsRegion);
9892
void setAWSEndpoint(const char * awsEndpoint);
99-
void setAWSDomain(const char * awsDomain);
10093
void setAWSSecretKey(const char * awsSecKey);
10194
void setAWSKeyID(const char * awsKeyID);
10295
void setHttpClient(IHttpClient* httpClient);

0 commit comments

Comments
 (0)