1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602
|
/**
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
* SPDX-License-Identifier: Apache-2.0.
*/
#include "websocket.h"
#include "http.h"
#include "io.h"
#include <aws/http/proxy.h>
#include <aws/http/request_response.h>
#include <aws/http/websocket.h>
#include <aws/io/socket.h>
static const char *s_websocket_capsule_name = "aws_websocket";
static void s_websocket_on_connection_setup(
const struct aws_websocket_on_connection_setup_data *setup,
void *user_data);
static void s_websocket_on_connection_shutdown(struct aws_websocket *websocket, int error_code, void *user_data);
static bool s_websocket_on_incoming_frame_begin(
struct aws_websocket *websocket,
const struct aws_websocket_incoming_frame *frame,
void *user_data);
static bool s_websocket_on_incoming_frame_payload(
struct aws_websocket *websocket,
const struct aws_websocket_incoming_frame *frame,
struct aws_byte_cursor data,
void *user_data);
static bool s_websocket_on_incoming_frame_complete(
struct aws_websocket *websocket,
const struct aws_websocket_incoming_frame *frame,
int error_code,
void *user_data);
/* When WebSocket._binding is GC'd, release the native websocket pointer.
* It will close (if necessary) on its way to the grave */
static void s_websocket_capsule_destructor(PyObject *capsule) {
struct aws_websocket *websocket = PyCapsule_GetPointer(capsule, s_websocket_capsule_name);
aws_websocket_release(websocket);
}
/* Kick off websocket connection.
* WebSocket._binding does not get returned from this function,
* it gets delivered later via the _WebSocketCore._on_connection_setup() callback */
PyObject *aws_py_websocket_client_connect(PyObject *self, PyObject *args) {
(void)self;
struct aws_byte_cursor host; /* s# */
uint16_t port; /* H */
PyObject *handshake_request_py; /* O */
PyObject *bootstrap_py; /* O */
PyObject *socket_options_py; /* O */
PyObject *tls_options_py; /* O */
PyObject *proxy_options_py; /* O */
int manage_read_window; /* p - boolean predicate */
Py_ssize_t initial_read_window; /* n */
PyObject *websocket_core_py; /* O */
if (!PyArg_ParseTuple(
args,
"s#HOOOOOpnO",
&host.ptr,
&host.len,
&port,
&handshake_request_py,
&bootstrap_py,
&socket_options_py,
&tls_options_py,
&proxy_options_py,
&manage_read_window,
&initial_read_window,
&websocket_core_py)) {
return NULL;
}
/* First, wrangle args that don't require any cleanup if things go wrong... */
/* required bootstrap */
struct aws_client_bootstrap *bootstrap = aws_py_get_client_bootstrap(bootstrap_py);
if (bootstrap == NULL) {
return NULL;
}
/* required socket_options */
struct aws_socket_options socket_options;
if (aws_py_socket_options_init(&socket_options, socket_options_py) == false) {
return NULL;
}
/* optional tls_options */
struct aws_tls_connection_options *tls_options = NULL;
if (tls_options_py != Py_None) {
tls_options = aws_py_get_tls_connection_options(tls_options_py);
if (tls_options == NULL) {
return NULL;
}
}
/* optional proxy_options */
bool has_proxy_options = proxy_options_py != Py_None;
struct aws_http_proxy_options proxy_options;
if (has_proxy_options) {
if (aws_py_http_proxy_options_init(&proxy_options, proxy_options_py) == false) {
return NULL;
}
}
/* required handshake_request */
struct aws_http_message *handshake_request = aws_py_get_http_message(handshake_request_py);
if (handshake_request == NULL) {
return NULL;
}
/* From hereon, we need to clean up if errors occur... */
/* keep _WebSocketCore alive for lifetime of aws_websocket */
Py_INCREF(websocket_core_py);
struct aws_websocket_client_connection_options options = {
.allocator = aws_py_get_allocator(),
.bootstrap = bootstrap,
.socket_options = &socket_options,
.tls_options = tls_options,
.proxy_options = has_proxy_options ? &proxy_options : NULL,
.host = host,
.port = port,
.handshake_request = handshake_request,
.initial_window_size = (size_t)initial_read_window, /* already checked it was non-negative out in python */
.user_data = websocket_core_py,
.on_connection_setup = s_websocket_on_connection_setup,
.on_connection_shutdown = s_websocket_on_connection_shutdown,
.on_incoming_frame_begin = s_websocket_on_incoming_frame_begin,
.on_incoming_frame_payload = s_websocket_on_incoming_frame_payload,
.on_incoming_frame_complete = s_websocket_on_incoming_frame_complete,
.manual_window_management = manage_read_window != 0,
};
if (aws_websocket_client_connect(&options) != AWS_OP_SUCCESS) {
PyErr_SetAwsLastError();
goto error;
}
/* Success! */
Py_RETURN_NONE;
error:
Py_DECREF(websocket_core_py);
return NULL;
}
/* Completion callback for websocket_client_connect().
* Wrangle args and fire _WebSocketCore._on_connection_setup().
*
* DIATRIBE ON ERROR HANDLING:
* Wrangling args from C->Python takes a lot of function calls that could THEORETICALLY fail.
* But we MUST fire the completion callback or the user's code would just hang.
*
* Attempting to handle all theoretical arg-wrangling errors would add a TON of complexity, such as:
* - switch callback to report failure instead of success
* - cleanup half-initialized resources (shut down websocket, cleanup half-initialized lists)
* - ability to report pure python exception, in addition to C error_code, in callback
* - suppress further callbacks from C as websocket shuts down,
* to maintain contract of "if init fails, then no further callbacks"
*
* I'm making a judgement call to just make these theoretical failures fatal.
* If there's a bug, it will be glaringly obvious, and simple to fix.
* This seems better than complicating things with tons of error-handling code
* that we can't actually check (and so may not actually work).
*/
static void s_websocket_on_connection_setup(
const struct aws_websocket_on_connection_setup_data *setup,
void *user_data) {
/* sanity check: websocket XOR error_code is set. both cannot be set. both cannot be unset */
AWS_FATAL_ASSERT((setup->websocket != NULL) ^ (setup->error_code != 0));
/* userdata is _WebSocketCore */
PyObject *websocket_core_py = user_data;
/*************** GIL ACQUIRE ***************/
PyGILState_STATE state = PyGILState_Ensure();
PyObject *websocket_binding_py = NULL;
if (setup->websocket) {
websocket_binding_py =
PyCapsule_New(setup->websocket, s_websocket_capsule_name, s_websocket_capsule_destructor);
AWS_FATAL_ASSERT(websocket_binding_py && "capsule allocation failed");
}
/* Any of the handshake_response variables could be NULL */
PyObject *status_code_py = NULL;
if (setup->handshake_response_status != NULL) {
status_code_py = PyLong_FromLong(*setup->handshake_response_status);
AWS_FATAL_ASSERT(status_code_py && "status code allocation failed");
}
PyObject *headers_py = NULL;
if (setup->handshake_response_header_array != NULL) {
headers_py = PyList_New((Py_ssize_t)setup->num_handshake_response_headers);
AWS_FATAL_ASSERT(headers_py && "header list allocation failed");
for (size_t i = 0; i < setup->num_handshake_response_headers; ++i) {
const struct aws_http_header *header_i = &setup->handshake_response_header_array[i];
PyObject *tuple_py = PyTuple_New(2);
AWS_FATAL_ASSERT(tuple_py && "header tuple allocation failed");
PyObject *name_py = PyUnicode_FromAwsByteCursor(&header_i->name);
AWS_FATAL_ASSERT(name_py && "header name wrangling failed");
PyTuple_SET_ITEM(tuple_py, 0, name_py);
PyObject *value_py = PyUnicode_FromAwsByteCursor(&header_i->value);
AWS_FATAL_ASSERT(value_py && "header value wrangling failed");
PyTuple_SET_ITEM(tuple_py, 1, value_py);
PyList_SET_ITEM(headers_py, i, tuple_py);
}
}
PyObject *body_py = NULL;
if (setup->handshake_response_body != NULL) {
/* AWS APIs are fine with NULL as the address of a 0-length array,
* but python APIs requires that it be non-NULL */
const char *ptr = setup->handshake_response_body->ptr ? (const char *)setup->handshake_response_body->ptr : "";
body_py = PyBytes_FromStringAndSize(ptr, (Py_ssize_t)setup->handshake_response_body->len);
AWS_FATAL_ASSERT(body_py && "response body allocation failed");
}
PyObject *result = PyObject_CallMethod(
websocket_core_py,
"_on_connection_setup",
"(iOOOO)",
/* i */ setup->error_code,
/* O */ websocket_binding_py ? websocket_binding_py : Py_None,
/* O */ status_code_py ? status_code_py : Py_None,
/* O */ headers_py ? headers_py : Py_None,
/* O */ body_py ? body_py : Py_None);
if (result) {
Py_DECREF(result);
} else {
/* _WebSocketCore._on_connection_setup() runs the user's callback in a try/except
* So any exception that leaks out is an unexpected bug in our code.
* Make it fatal, we have no graceful way to deal with this. */
PyErr_WriteUnraisable(websocket_core_py);
AWS_FATAL_ASSERT(0 && "Failed to invoke WebSocket on_connection_setup callback");
}
Py_XDECREF(websocket_binding_py);
Py_XDECREF(status_code_py);
Py_XDECREF(headers_py);
Py_XDECREF(body_py);
/* If setup failed, there will be no further callbacks, so release _WebSocketCore */
if (setup->error_code != 0) {
Py_DECREF(websocket_core_py);
}
PyGILState_Release(state);
/*************** GIL RELEASE ***************/
}
static void s_websocket_on_connection_shutdown(struct aws_websocket *websocket, int error_code, void *user_data) {
(void)websocket;
/* userdata is _WebSocketCore */
PyObject *websocket_core_py = user_data;
/*************** GIL ACQUIRE ***************/
PyGILState_STATE state = PyGILState_Ensure();
PyObject *result = PyObject_CallMethod(websocket_core_py, "_on_connection_shutdown", "(i)", error_code);
if (result) {
Py_DECREF(result);
} else {
/* _WebSocketCore._on_connection_shutdown() runs the user's callback in a try/except.
* So any exception that leaks out is an unexpected bug in our code.
* Make it fatal, we have no graceful way to deal with this. */
PyErr_WriteUnraisable(websocket_core_py);
AWS_FATAL_ASSERT(0 && "Failed to invoke WebSocket on_connection_shutdown callback");
}
/* Release _WebSocketCore, there will be no further callbacks */
Py_DECREF(websocket_core_py);
PyGILState_Release(state);
/*************** GIL RELEASE ***************/
}
static bool s_websocket_on_incoming_frame_begin(
struct aws_websocket *websocket,
const struct aws_websocket_incoming_frame *frame,
void *user_data) {
(void)websocket;
/* userdata is _WebSocketCore */
PyObject *websocket_core_py = user_data;
/*************** GIL ACQUIRE ***************/
PyGILState_STATE state = PyGILState_Ensure();
PyObject *result = PyObject_CallMethod(
websocket_core_py,
"_on_incoming_frame_begin",
"(iKO)",
frame->opcode,
frame->payload_length,
frame->fin ? Py_True : Py_False);
/* If the user's callback raises an exception, we catch it and return False to C... */
if (result == NULL) {
/* ... so any exception that leaks out is an unexpected bug in our code.
* Make it fatal, we have no graceful way to deal with this. */
PyErr_WriteUnraisable(websocket_core_py);
AWS_FATAL_ASSERT(0 && "Failed to invoke WebSocket on_incoming_frame_begin callback");
}
bool success = PyObject_IsTrue(result);
Py_DECREF(result);
PyGILState_Release(state);
/*************** GIL RELEASE ***************/
return success;
}
static bool s_websocket_on_incoming_frame_payload(
struct aws_websocket *websocket,
const struct aws_websocket_incoming_frame *frame,
struct aws_byte_cursor data,
void *user_data) {
(void)websocket;
(void)frame;
/* userdata is _WebSocketCore */
PyObject *websocket_core_py = user_data;
/*************** GIL ACQUIRE ***************/
PyGILState_STATE state = PyGILState_Ensure();
PyObject *result = PyObject_CallMethod(websocket_core_py, "_on_incoming_frame_payload", "(y#)", data.ptr, data.len);
/* If the user's callback raises an exception, we catch it and return False to C... */
if (result == NULL) {
/* ... so any exception that leaks out is an unexpected bug in our code.
* Make it fatal, we have no graceful way to deal with this. */
PyErr_WriteUnraisable(websocket_core_py);
AWS_FATAL_ASSERT(0 && "Failed to invoke WebSocket on_incoming_frame_payload callback");
}
bool success = PyObject_IsTrue(result);
Py_DECREF(result);
PyGILState_Release(state);
/*************** GIL RELEASE ***************/
return success;
}
static bool s_websocket_on_incoming_frame_complete(
struct aws_websocket *websocket,
const struct aws_websocket_incoming_frame *frame,
int error_code,
void *user_data) {
(void)websocket;
(void)frame;
/* userdata is _WebSocketCore */
PyObject *websocket_core_py = user_data;
/*************** GIL ACQUIRE ***************/
PyGILState_STATE state = PyGILState_Ensure();
PyObject *result = PyObject_CallMethod(websocket_core_py, "_on_incoming_frame_complete", "(i)", error_code);
/* If the user's callback raises an exception, we catch it and return False to C... */
if (result == NULL) {
/* ... so any exception that leaks out is an unexpected bug in our code.
* Make it fatal, we have no graceful way to deal with this. */
PyErr_WriteUnraisable(websocket_core_py);
AWS_FATAL_ASSERT(0 && "Failed to invoke WebSocket on_incoming_frame_complete callback");
}
bool success = PyObject_IsTrue(result);
Py_DECREF(result);
PyGILState_Release(state);
/*************** GIL RELEASE ***************/
return success;
}
PyObject *aws_py_websocket_close(PyObject *self, PyObject *args) {
(void)self;
PyObject *binding_py; /* O */
if (!PyArg_ParseTuple(args, "O", &binding_py)) {
return NULL;
}
struct aws_websocket *websocket = PyCapsule_GetPointer(binding_py, s_websocket_capsule_name);
if (!websocket) {
return NULL;
}
aws_websocket_close(websocket, false /*free_scarce_resources_immediately*/);
Py_RETURN_NONE;
}
/**
* This stays alive for the duration of a send_frame operation.
* It streams the payload data, and fires the completion callback.
*/
struct websocket_send_op {
/* Py_buffer lets us hold onto Python data and read it without holding the GIL */
Py_buffer payload_buffer;
/* this cursor tracks our progress streaming the payload */
struct aws_byte_cursor payload_cursor;
PyObject *on_complete_py;
};
static void s_websocket_send_op_destroy(struct websocket_send_op *send_op) {
if (send_op == NULL) {
return;
}
if (send_op->payload_buffer.buf != NULL) {
PyBuffer_Release(&send_op->payload_buffer);
}
Py_XDECREF(send_op->on_complete_py);
aws_mem_release(aws_py_get_allocator(), send_op);
}
static bool s_websocket_stream_outgoing_payload(
struct aws_websocket *websocket,
struct aws_byte_buf *out_buf,
void *user_data) {
(void)websocket;
struct websocket_send_op *send_op = user_data;
aws_byte_buf_write_to_capacity(out_buf, &send_op->payload_cursor);
return true;
}
static void s_websocket_on_send_frame_complete(struct aws_websocket *websocket, int error_code, void *user_data) {
(void)websocket;
struct websocket_send_op *send_op = user_data;
/*************** GIL ACQUIRE ***************/
PyGILState_STATE state = PyGILState_Ensure();
PyObject *result = PyObject_CallFunction(send_op->on_complete_py, "(i)", error_code);
if (result) {
Py_DECREF(result);
} else {
/* WebSocket.send_frame.on_complete() runs the user's callback in a try/except.
* So any exception that leaks out is an unexpected bug in our code.
* Make it fatal, we have no graceful way to deal with this. */
PyErr_WriteUnraisable(send_op->on_complete_py);
AWS_FATAL_ASSERT(0 && "Failed to invoke WebSocket.send_frame()'s on_complete callback");
}
s_websocket_send_op_destroy(send_op);
PyGILState_Release(state);
/*************** GIL RELEASE ***************/
}
PyObject *aws_py_websocket_send_frame(PyObject *self, PyObject *args) {
(void)self;
PyObject *binding_py; /* O */
uint8_t opcode; /* b */
Py_buffer payload_buffer; /* z* */
int fin; /* p - boolean predicate */
PyObject *on_complete_py; /* O */
if (!PyArg_ParseTuple(args, "Obz*pO", &binding_py, &opcode, &payload_buffer, &fin, &on_complete_py)) {
return NULL;
}
/* From hereon, we need to clean up if errors occur (Py_buffers must always be released) ... */
struct websocket_send_op *send_op = aws_mem_calloc(aws_py_get_allocator(), 1, sizeof(struct websocket_send_op));
send_op->payload_buffer = payload_buffer;
send_op->payload_cursor = aws_byte_cursor_from_array(payload_buffer.buf, payload_buffer.len);
Py_INCREF(on_complete_py);
send_op->on_complete_py = on_complete_py;
struct aws_websocket *websocket = PyCapsule_GetPointer(binding_py, s_websocket_capsule_name);
if (!websocket) {
goto error;
}
struct aws_websocket_send_frame_options options = {
.payload_length = (uint64_t)payload_buffer.len,
.user_data = send_op,
.stream_outgoing_payload = s_websocket_stream_outgoing_payload,
.on_complete = s_websocket_on_send_frame_complete,
.opcode = opcode,
.fin = fin,
};
if (aws_websocket_send_frame(websocket, &options)) {
PyErr_SetAwsLastError();
goto error;
}
/* Success! */
Py_RETURN_NONE;
error:
s_websocket_send_op_destroy(send_op);
return NULL;
}
PyObject *aws_py_websocket_increment_read_window(PyObject *self, PyObject *args) {
(void)self;
PyObject *binding_py; /* O */
Py_ssize_t size; /* n */
if (!PyArg_ParseTuple(args, "On", &binding_py, &size)) {
return NULL;
}
struct aws_websocket *websocket = PyCapsule_GetPointer(binding_py, s_websocket_capsule_name);
if (!websocket) {
return NULL;
}
/* already checked that size was non-negative out in python */
aws_websocket_increment_read_window(websocket, (size_t)size);
Py_RETURN_NONE;
}
PyObject *aws_py_websocket_create_handshake_request(PyObject *self, PyObject *args) {
(void)self;
struct aws_byte_cursor host; /* s# */
struct aws_byte_cursor path; /* s# */
if (!PyArg_ParseTuple(args, "s#s#", &host.ptr, &host.len, &path.ptr, &path.len)) {
return NULL;
}
/* This function will return a tuple containing:
* 1) the binding for an HttpRequest
* 2) the binding for an HttpHeaders */
bool success = false;
struct aws_http_message *request = NULL;
PyObject *tuple_py = NULL;
request = aws_http_message_new_websocket_handshake_request(aws_py_get_allocator(), path, host);
if (!request) {
PyErr_SetAwsLastError();
goto cleanup;
}
tuple_py = PyTuple_New(2);
if (!tuple_py) {
goto cleanup;
}
PyObject *request_binding_py = aws_py_http_message_new_request_from_native(request);
if (!request_binding_py) {
goto cleanup;
}
PyTuple_SET_ITEM(tuple_py, 0, request_binding_py); /* steals reference to request_binding_py */
PyObject *headers_binding_py = aws_py_http_headers_new_from_native(aws_http_message_get_headers(request));
if (!headers_binding_py) {
goto cleanup;
}
PyTuple_SET_ITEM(tuple_py, 1, headers_binding_py); /* steals reference to headers_binding_py */
/* Success! */
success = true;
cleanup:
aws_http_message_release(request);
if (success) {
return tuple_py;
} else {
Py_XDECREF(tuple_py);
return NULL;
}
}
|