Skip to content

Commit 69b952e

Browse files
authored
Breaking change to websocket API: setup callback can report body of response (#409)
**Issue:** If the server rejected a websocket connection, there was no way to see the body of the response. The response body often has useful information explaining why the connection was rejected. **Underlying cause:** The websocket code would close the connection immediately after receiving headers, because that was the simplest way to do things. **Description of changes:** Change the setup callback signature, so that the body can be reported. The setup code keeps the connection open while receiving a rejection, making a copy of the body, and ultimately reporting it in the setup callback.
1 parent 4e82c1e commit 69b952e

File tree

4 files changed

+419
-239
lines changed

4 files changed

+419
-239
lines changed

include/aws/http/websocket.h

+26-18
Original file line numberDiff line numberDiff line change
@@ -37,27 +37,35 @@ enum aws_websocket_opcode {
3737
#define AWS_WEBSOCKET_CLOSE_TIMEOUT 1000000000 // nanos -> 1 sec
3838

3939
/**
40-
* Called when websocket setup is complete.
40+
* Data passed to the websocket on_connection_setup callback.
41+
*
4142
* An error_code of zero indicates that setup was completely successful.
42-
* Called exactly once on the websocket's event-loop thread.
43+
* You own the websocket pointer now and must call aws_websocket_release() when you are done with it.
44+
* You can inspect the response headers, if you're interested.
4345
*
44-
* websocket: if successful, a valid pointer to the websocket, otherwise NULL.
45-
* error_code: the operation was completely successful if this value is zero.
46-
* handshake_response_status: The response status code of the HTTP handshake, 101 if successful,
47-
* -1 if the connection failed before a response was received.
48-
* handshake_response_header_array: Headers from the HTTP handshake response.
49-
* May be NULL if num_handshake_response_headers is 0.
50-
* Copy if necessary, this memory becomes invalid once the callback completes.
51-
* num_handshake_response_headers: Number of entries in handshake_response_header_array.
52-
* May be 0 if the response did not complete, or was invalid.
46+
* A non-zero error_code indicates that setup failed.
47+
* The websocket pointer will be NULL.
48+
* If the server sent a response, you can inspect its status-code, headers, and body,
49+
* but this data will NULL if setup failed before a full response could be received.
50+
* If you wish to persist data from the response make a deep copy.
51+
* The response data becomes invalid once the callback completes.
5352
*/
54-
typedef void(aws_websocket_on_connection_setup_fn)(
55-
struct aws_websocket *websocket,
56-
int error_code,
57-
int handshake_response_status,
58-
const struct aws_http_header *handshake_response_header_array,
59-
size_t num_handshake_response_headers,
60-
void *user_data);
53+
struct aws_websocket_on_connection_setup_data {
54+
int error_code;
55+
struct aws_websocket *websocket;
56+
const int *handshake_response_status;
57+
const struct aws_http_header *handshake_response_header_array;
58+
size_t num_handshake_response_headers;
59+
const struct aws_byte_cursor *handshake_response_body;
60+
};
61+
62+
/**
63+
* Called when websocket setup is complete.
64+
* Called exactly once on the websocket's event-loop thread.
65+
* See `aws_websocket_on_connection_setup_data`.
66+
*/
67+
typedef void(
68+
aws_websocket_on_connection_setup_fn)(const struct aws_websocket_on_connection_setup_data *setup, void *user_data);
6169

6270
/**
6371
* Called when the websocket has finished shutting down.

0 commit comments

Comments
 (0)