Skip to content

Commit

Permalink
Adapt to minor breaking change in websocket API (#243)
Browse files Browse the repository at this point in the history
Adapt to changes from awslabs/aws-c-http#409
  • Loading branch information
graebm committed Dec 23, 2022
1 parent c2bc310 commit ac51334
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 38 deletions.
29 changes: 11 additions & 18 deletions source/client.c
Original file line number Diff line number Diff line change
Expand Up @@ -1158,18 +1158,10 @@ static void s_on_websocket_shutdown(struct aws_websocket *websocket, int error_c
}
}

static void s_on_websocket_setup(
struct aws_websocket *websocket,
int error_code,
int handshake_response_status,
const struct aws_http_header *handshake_response_header_array,
size_t num_handshake_response_headers,
void *user_data) {

(void)handshake_response_status;
static void s_on_websocket_setup(const struct aws_websocket_on_connection_setup_data *setup, void *user_data) {

/* Setup callback contract is: if error_code is non-zero then websocket is NULL. */
AWS_FATAL_ASSERT((error_code != 0) == (websocket == NULL));
AWS_FATAL_ASSERT((setup->error_code != 0) == (setup->websocket == NULL));

struct aws_mqtt_client_connection *connection = user_data;
struct aws_channel *channel = NULL;
Expand All @@ -1179,13 +1171,13 @@ static void s_on_websocket_setup(
connection->websocket.handshake_request = NULL;
}

if (websocket) {
channel = aws_websocket_get_channel(websocket);
if (setup->websocket) {
channel = aws_websocket_get_channel(setup->websocket);
AWS_FATAL_ASSERT(channel);
AWS_FATAL_ASSERT(aws_channel_get_event_loop(channel) == connection->loop);

/* Websocket must be "converted" before the MQTT handler can be installed next to it. */
if (aws_websocket_convert_to_midchannel_handler(websocket)) {
if (aws_websocket_convert_to_midchannel_handler(setup->websocket)) {
AWS_LOGF_ERROR(
AWS_LS_MQTT_CLIENT,
"id=%p: Failed converting websocket, error %d (%s)",
Expand All @@ -1203,8 +1195,8 @@ static void s_on_websocket_setup(

if (connection->websocket.handshake_validator(
connection,
handshake_response_header_array,
num_handshake_response_headers,
setup->handshake_response_header_array,
setup->num_handshake_response_headers,
connection->websocket.handshake_validator_ud)) {

AWS_LOGF_ERROR(
Expand All @@ -1224,7 +1216,7 @@ static void s_on_websocket_setup(
}

/* Call into the channel-setup callback, the rest of the logic is the same. */
s_mqtt_client_init(connection->client->bootstrap, error_code, channel, connection);
s_mqtt_client_init(connection->client->bootstrap, setup->error_code, channel, connection);
}

static aws_mqtt_transform_websocket_handshake_complete_fn s_websocket_handshake_transform_complete; /* fwd declare */
Expand Down Expand Up @@ -1323,9 +1315,10 @@ static void s_websocket_handshake_transform_complete(
/* Success */
return;

error:
error:;
/* Proceed to next step, telling it that we failed. */
s_on_websocket_setup(NULL, error_code, -1, NULL, 0, connection);
struct aws_websocket_on_connection_setup_data websocket_setup = {.error_code = error_code};
s_on_websocket_setup(&websocket_setup, connection);
}

#else /* AWS_MQTT_WITH_WEBSOCKETS */
Expand Down
28 changes: 9 additions & 19 deletions source/v5/mqtt5_client.c
Original file line number Diff line number Diff line change
Expand Up @@ -836,32 +836,22 @@ static void s_on_websocket_shutdown(struct aws_websocket *websocket, int error_c
}
}

static void s_on_websocket_setup(
struct aws_websocket *websocket,
int error_code,
int handshake_response_status,
const struct aws_http_header *handshake_response_header_array,
size_t num_handshake_response_headers,
void *user_data) {

(void)handshake_response_status;
(void)handshake_response_header_array;
(void)num_handshake_response_headers;
static void s_on_websocket_setup(const struct aws_websocket_on_connection_setup_data *setup, void *user_data) {

struct aws_mqtt5_client *client = user_data;
client->handshake = aws_http_message_release(client->handshake);

/* Setup callback contract is: if error_code is non-zero then websocket is NULL. */
AWS_FATAL_ASSERT((error_code != 0) == (websocket == NULL));
AWS_FATAL_ASSERT((setup->error_code != 0) == (setup->websocket == NULL));

struct aws_channel *channel = NULL;

if (websocket) {
channel = aws_websocket_get_channel(websocket);
if (setup->websocket) {
channel = aws_websocket_get_channel(setup->websocket);
AWS_ASSERT(channel);

/* Websocket must be "converted" before the MQTT handler can be installed next to it. */
if (aws_websocket_convert_to_midchannel_handler(websocket)) {
if (aws_websocket_convert_to_midchannel_handler(setup->websocket)) {
AWS_LOGF_ERROR(
AWS_LS_MQTT5_CLIENT,
"id=%p: Failed converting websocket, error %d (%s)",
Expand All @@ -875,7 +865,7 @@ static void s_on_websocket_setup(
}

/* Call into the channel-setup callback, the rest of the logic is the same. */
s_mqtt5_client_setup(client->config->bootstrap, error_code, channel, client);
s_mqtt5_client_setup(client->config->bootstrap, setup->error_code, channel, client);
}

struct aws_mqtt5_websocket_transform_complete_task {
Expand Down Expand Up @@ -936,9 +926,9 @@ void s_websocket_transform_complete_task_fn(struct aws_task *task, void *arg, en
}
}

error:

s_on_websocket_setup(NULL, error_code, -1, NULL, 0, client);
error:;
struct aws_websocket_on_connection_setup_data websocket_setup = {.error_code = error_code};
s_on_websocket_setup(&websocket_setup, client);

done:

Expand Down
3 changes: 2 additions & 1 deletion tests/v5/mqtt5_client_tests.c
Original file line number Diff line number Diff line change
Expand Up @@ -680,7 +680,8 @@ void s_websocket_channel_async_failure_task_fn(struct aws_task *task, void *arg,
struct websocket_channel_failure_wrapper *wrapper = arg;
struct aws_websocket_client_connection_options *options = &wrapper->websocket_options;

(*wrapper->websocket_options.on_connection_setup)(NULL, AWS_ERROR_INVALID_STATE, 0, NULL, 0, options->user_data);
struct aws_websocket_on_connection_setup_data websocket_setup = {.error_code = AWS_ERROR_INVALID_STATE};
(*wrapper->websocket_options.on_connection_setup)(&websocket_setup, options->user_data);
}

static int s_mqtt5_client_test_asynchronous_websocket_failure_fn(
Expand Down

0 comments on commit ac51334

Please sign in to comment.