| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
#include "http.h" |
| 6 |
|
| 7 |
#include "io.h" |
| 8 |
|
| 9 |
#include <aws/common/array_list.h> |
| 10 |
#include <aws/common/ref_count.h> |
| 11 |
#include <aws/http/connection.h> |
| 12 |
#include <aws/http/proxy.h> |
| 13 |
#include <aws/http/request_response.h> |
| 14 |
#include <aws/io/socket.h> |
| 15 |
|
| 16 |
static const char *s_capsule_name_http_connection = "aws_http_connection"; |
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
). |
| 25 |
|
| 26 |
struct http_connection_binding { |
| 27 |
struct aws_http_connection *native; |
| 28 |
|
| 29 |
PyObject *py_core; |
| 30 |
|
| 31 |
struct aws_ref_count ref_count; |
| 32 |
}; |
| 33 |
|
| 34 |
static void s_connection_destroy(void *user_data) { |
| 35 |
struct http_connection_binding *connection = user_data; |
| 36 |
Py_XDECREF(connection->py_core); |
| 37 |
|
| 38 |
aws_mem_release(aws_py_get_allocator(), connection); |
| 39 |
} |
| 40 |
|
| 41 |
struct aws_http_connection *aws_py_get_http_connection(PyObject *connection) { |
| 42 |
AWS_PY_RETURN_NATIVE_FROM_BINDING( |
| 43 |
connection, s_capsule_name_http_connection, "HttpConnectionBase", http_connection_binding); |
| 44 |
} |
| 45 |
|
| 46 |
static void s_connection_capsule_destructor(PyObject *capsule) { |
| 47 |
struct http_connection_binding *connection = PyCapsule_GetPointer(capsule, s_capsule_name_http_connection); |
| 48 |
|
| 49 |
aws_http_connection_release(connection->native); |
| 50 |
|
| 51 |
aws_ref_count_release(&connection->ref_count); |
| 52 |
} |
| 53 |
|
| 54 |
static void s_on_connection_shutdown(struct aws_http_connection *native_connection, int error_code, void *user_data) { |
| 55 |
(void)native_connection; |
| 56 |
struct http_connection_binding *connection = user_data; |
| 57 |
|
| 58 |
PyGILState_STATE state; |
| 59 |
if (aws_py_gilstate_ensure(&state)) { |
| 60 |
return; |
| 61 |
} |
| 62 |
|
| 63 |
|
| 64 |
PyObject *result = PyObject_CallMethod(connection->py_core, "_on_shutdown", "(i)", error_code); |
| 65 |
|
| 66 |
if (result) { |
| 67 |
Py_DECREF(result); |
| 68 |
} else { |
| 69 |
|
| 70 |
PyErr_WriteUnraisable(PyErr_Occurred()); |
| 71 |
} |
| 72 |
|
| 73 |
|
| 74 |
aws_ref_count_release(&connection->ref_count); |
| 75 |
|
| 76 |
PyGILState_Release(state); |
| 77 |
} |
| 78 |
|
| 79 |
static void s_on_client_connection_setup( |
| 80 |
struct aws_http_connection *native_connection, |
| 81 |
int error_code, |
| 82 |
void *user_data) { |
| 83 |
|
| 84 |
struct http_connection_binding *connection = user_data; |
| 85 |
AWS_FATAL_ASSERT((native_connection != NULL) ^ error_code); |
| 86 |
|
| 87 |
connection->native = native_connection; |
| 88 |
|
| 89 |
PyGILState_STATE state; |
| 90 |
if (aws_py_gilstate_ensure(&state)) { |
| 91 |
return; |
| 92 |
} |
| 93 |
enum aws_http_version http_version = AWS_HTTP_VERSION_UNKNOWN; |
| 94 |
|
| 95 |
PyObject *capsule = NULL; |
| 96 |
if (!error_code) { |
| 97 |
|
| 98 |
|
| 99 |
aws_ref_count_acquire(&connection->ref_count); |
| 100 |
|
| 101 |
capsule = PyCapsule_New(connection, s_capsule_name_http_connection, s_connection_capsule_destructor); |
| 102 |
if (!capsule) { |
| 103 |
error_code = AWS_ERROR_UNKNOWN; |
| 104 |
} |
| 105 |
http_version = aws_http_connection_get_version(native_connection); |
| 106 |
} |
| 107 |
|
| 108 |
PyObject *result = PyObject_CallMethod( |
| 109 |
connection->py_core, "_on_connection_setup", "(Oii)", capsule ? capsule : Py_None, error_code, http_version); |
| 110 |
|
| 111 |
if (result) { |
| 112 |
Py_DECREF(result); |
| 113 |
} else { |
| 114 |
|
| 115 |
PyErr_WriteUnraisable(PyErr_Occurred()); |
| 116 |
} |
| 117 |
|
| 118 |
if (native_connection) { |
| 119 |
if (!capsule) { |
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
aws_ref_count_release(&connection->ref_count); |
| 125 |
aws_http_connection_release(connection->native); |
| 126 |
} |
| 127 |
} else { |
| 128 |
|
| 129 |
|
| 130 |
aws_ref_count_release(&connection->ref_count); |
| 131 |
} |
| 132 |
|
| 133 |
Py_XDECREF(capsule); |
| 134 |
PyGILState_Release(state); |
| 135 |
} |
| 136 |
|
| 137 |
|
| 138 |
static int s_convert_http2_settings( |
| 139 |
PyObject *initial_settings_py, |
| 140 |
struct aws_allocator *allocator, |
| 141 |
struct aws_http2_setting *out_settings[], |
| 142 |
size_t *out_size) { |
| 143 |
Py_ssize_t py_list_size = PyList_Size(initial_settings_py); |
| 144 |
if (py_list_size == 0) { |
| 145 |
*out_size = 0; |
| 146 |
*out_settings = NULL; |
| 147 |
return AWS_OP_SUCCESS; |
| 148 |
} |
| 149 |
|
| 150 |
*out_settings = aws_mem_calloc(allocator, py_list_size, sizeof(struct aws_http2_setting)); |
| 151 |
PyObject *setting_py = NULL; |
| 152 |
bool strong_ref = false; |
| 153 |
|
| 154 |
for (Py_ssize_t i = 0; i < py_list_size; i++) { |
| 155 |
#ifdef Py_GIL_DISABLED |
| 156 |
setting_py = PyList_GetItemRef(initial_settings_py, i); |
| 157 |
strong_ref = true; |
| 158 |
#else |
| 159 |
setting_py = PyList_GetItem(initial_settings_py, i); |
| 160 |
|
| 161 |
#endif |
| 162 |
|
| 163 |
enum aws_http2_settings_id id = PyObject_GetAttrAsIntEnum(setting_py, "Http2Setting", "id"); |
| 164 |
if (PyErr_Occurred()) { |
| 165 |
goto error; |
| 166 |
} |
| 167 |
|
| 168 |
|
| 169 |
uint32_t value = PyObject_GetAttrAsUint32(setting_py, "Http2Setting", "value"); |
| 170 |
if (PyErr_Occurred()) { |
| 171 |
goto error; |
| 172 |
} |
| 173 |
(*out_settings)[i].id = id; |
| 174 |
(*out_settings)[i].value = value; |
| 175 |
if (strong_ref) { |
| 176 |
Py_DECREF(setting_py); |
| 177 |
} |
| 178 |
} |
| 179 |
|
| 180 |
*out_size = (size_t)py_list_size; |
| 181 |
return AWS_OP_SUCCESS; |
| 182 |
error: |
| 183 |
if (setting_py && strong_ref) { |
| 184 |
Py_DECREF(setting_py); |
| 185 |
} |
| 186 |
*out_size = 0; |
| 187 |
aws_mem_release(allocator, *out_settings); |
| 188 |
*out_settings = NULL; |
| 189 |
return AWS_OP_ERR; |
| 190 |
} |
| 191 |
|
| 192 |
static void s_http2_on_remote_settings_change( |
| 193 |
struct aws_http_connection *http2_connection, |
| 194 |
const struct aws_http2_setting *settings_array, |
| 195 |
size_t num_settings, |
| 196 |
void *user_data) { |
| 197 |
(void)http2_connection; |
| 198 |
struct http_connection_binding *connection = user_data; |
| 199 |
|
| 200 |
PyGILState_STATE state; |
| 201 |
if (aws_py_gilstate_ensure(&state)) { |
| 202 |
return; |
| 203 |
} |
| 204 |
|
| 205 |
PyObject *py_settings_list = PyList_New(num_settings); |
| 206 |
if (!py_settings_list) { |
| 207 |
PyErr_WriteUnraisable(PyErr_Occurred()); |
| 208 |
return; |
| 209 |
} |
| 210 |
for (size_t i = 0; i < num_settings; i++) { |
| 211 |
PyObject *tuple = Py_BuildValue("(iI)", settings_array[i].id, settings_array[i].value); |
| 212 |
if (!tuple) { |
| 213 |
PyErr_WriteUnraisable(PyErr_Occurred()); |
| 214 |
goto done; |
| 215 |
} |
| 216 |
PyList_SetItem(py_settings_list, i, tuple); |
| 217 |
} |
| 218 |
PyObject *result = PyObject_CallMethod(connection->py_core, "_on_remote_settings_changed", "(O)", py_settings_list); |
| 219 |
if (!result) { |
| 220 |
PyErr_WriteUnraisable(PyErr_Occurred()); |
| 221 |
goto done; |
| 222 |
} |
| 223 |
Py_DECREF(result); |
| 224 |
done: |
| 225 |
Py_XDECREF(py_settings_list); |
| 226 |
PyGILState_Release(state); |
| 227 |
} |
| 228 |
|
| 229 |
PyObject *aws_py_http_client_connection_new(PyObject *self, PyObject *args) { |
| 230 |
(void)self; |
| 231 |
|
| 232 |
struct aws_allocator *allocator = aws_py_get_allocator(); |
| 233 |
|
| 234 |
PyObject *bootstrap_py; |
| 235 |
const char *host_name; |
| 236 |
Py_ssize_t host_name_len; |
| 237 |
uint32_t port_number; |
| 238 |
PyObject *socket_options_py; |
| 239 |
PyObject *tls_options_py; |
| 240 |
PyObject *proxy_options_py; |
| 241 |
PyObject *initial_settings_py; |
| 242 |
PyObject *on_remote_settings_changed_py; |
| 243 |
PyObject *py_core; |
| 244 |
int manual_window_management = 0; |
| 245 |
PyObject *initial_window_size_py = Py_None; |
| 246 |
PyObject *read_buffer_capacity_py = Py_None; |
| 247 |
int conn_manual_window_management = 0; |
| 248 |
PyObject *conn_window_size_threshold_py = Py_None; |
| 249 |
PyObject *stream_window_size_threshold_py = Py_None; |
| 250 |
bool success = false; |
| 251 |
|
| 252 |
if (!PyArg_ParseTuple( |
| 253 |
args, |
| 254 |
"Os#IOOOOOOpOOpOO", |
| 255 |
&bootstrap_py, |
| 256 |
&host_name, |
| 257 |
&host_name_len, |
| 258 |
&port_number, |
| 259 |
&socket_options_py, |
| 260 |
&tls_options_py, |
| 261 |
&proxy_options_py, |
| 262 |
&initial_settings_py, |
| 263 |
&on_remote_settings_changed_py, |
| 264 |
&py_core, |
| 265 |
&manual_window_management, |
| 266 |
&initial_window_size_py, |
| 267 |
&read_buffer_capacity_py, |
| 268 |
&conn_manual_window_management, |
| 269 |
&conn_window_size_threshold_py, |
| 270 |
&stream_window_size_threshold_py)) { |
| 271 |
return NULL; |
| 272 |
} |
| 273 |
|
| 274 |
struct aws_client_bootstrap *bootstrap = aws_py_get_client_bootstrap(bootstrap_py); |
| 275 |
if (!bootstrap) { |
| 276 |
return NULL; |
| 277 |
} |
| 278 |
|
| 279 |
struct http_connection_binding *connection = aws_mem_calloc(allocator, 1, sizeof(struct http_connection_binding)); |
| 280 |
aws_ref_count_init(&connection->ref_count, connection, s_connection_destroy); |
| 281 |
|
| 282 |
struct aws_http2_setting *http2_settings = NULL; |
| 283 |
size_t http2_settings_count = 0; |
| 284 |
struct aws_http2_connection_options http2_options = {0}; |
| 285 |
|
| 286 |
struct aws_tls_connection_options *tls_options = NULL; |
| 287 |
if (tls_options_py != Py_None) { |
| 288 |
tls_options = aws_py_get_tls_connection_options(tls_options_py); |
| 289 |
if (!tls_options) { |
| 290 |
goto done; |
| 291 |
} |
| 292 |
} |
| 293 |
|
| 294 |
struct aws_socket_options socket_options; |
| 295 |
if (!aws_py_socket_options_init(&socket_options, socket_options_py)) { |
| 296 |
goto done; |
| 297 |
} |
| 298 |
|
| 299 |
if (initial_settings_py != Py_None) { |
| 300 |
|
| 301 |
if (s_convert_http2_settings(initial_settings_py, allocator, &http2_settings, &http2_settings_count)) { |
| 302 |
goto done; |
| 303 |
} |
| 304 |
http2_options.initial_settings_array = http2_settings; |
| 305 |
http2_options.num_initial_settings = http2_settings_count; |
| 306 |
} |
| 307 |
if (on_remote_settings_changed_py != Py_None) { |
| 308 |
http2_options.on_remote_settings_change = s_http2_on_remote_settings_change; |
| 309 |
} |
| 310 |
|
| 311 |
|
| 312 |
if (conn_manual_window_management) { |
| 313 |
http2_options.conn_manual_window_management = true; |
| 314 |
} |
| 315 |
if (conn_window_size_threshold_py != Py_None) { |
| 316 |
http2_options.conn_window_size_threshold_to_send_update = PyLong_AsUnsignedLong(conn_window_size_threshold_py); |
| 317 |
if (PyErr_Occurred()) { |
| 318 |
goto done; |
| 319 |
} |
| 320 |
} |
| 321 |
if (stream_window_size_threshold_py != Py_None) { |
| 322 |
http2_options.stream_window_size_threshold_to_send_update = |
| 323 |
PyLong_AsUnsignedLong(stream_window_size_threshold_py); |
| 324 |
if (PyErr_Occurred()) { |
| 325 |
goto done; |
| 326 |
} |
| 327 |
} |
| 328 |
|
| 329 |
|
| 330 |
struct aws_http_proxy_options proxy_options_storage; |
| 331 |
struct aws_http_proxy_options *proxy_options = NULL; |
| 332 |
if (proxy_options_py != Py_None) { |
| 333 |
proxy_options = &proxy_options_storage; |
| 334 |
if (!aws_py_http_proxy_options_init(proxy_options, proxy_options_py)) { |
| 335 |
goto done; |
| 336 |
} |
| 337 |
} |
| 338 |
|
| 339 |
|
| 340 |
struct aws_http1_connection_options http1_options = {0}; |
| 341 |
if (read_buffer_capacity_py != Py_None) { |
| 342 |
http1_options.read_buffer_capacity = PyLong_AsSize_t(read_buffer_capacity_py); |
| 343 |
if (PyErr_Occurred()) { |
| 344 |
goto done; |
| 345 |
} |
| 346 |
} |
| 347 |
|
| 348 |
struct aws_http_client_connection_options http_options = { |
| 349 |
.self_size = sizeof(http_options), |
| 350 |
.bootstrap = bootstrap, |
| 351 |
.tls_options = tls_options, |
| 352 |
.proxy_options = proxy_options, |
| 353 |
.allocator = allocator, |
| 354 |
.user_data = connection, |
| 355 |
.host_name = aws_byte_cursor_from_array((const uint8_t *)host_name, host_name_len), |
| 356 |
.port = port_number, |
| 357 |
.initial_window_size = 0, |
| 358 |
.socket_options = &socket_options, |
| 359 |
.on_setup = s_on_client_connection_setup, |
| 360 |
.on_shutdown = s_on_connection_shutdown, |
| 361 |
.http2_options = &http2_options, |
| 362 |
.http1_options = (read_buffer_capacity_py != Py_None) ? &http1_options : NULL, |
| 363 |
.manual_window_management = manual_window_management, |
| 364 |
}; |
| 365 |
|
| 366 |
|
| 367 |
if (initial_window_size_py != Py_None) { |
| 368 |
http_options.initial_window_size = PyLong_AsSize_t(initial_window_size_py); |
| 369 |
if (PyErr_Occurred()) { |
| 370 |
goto done; |
| 371 |
} |
| 372 |
} |
| 373 |
|
| 374 |
connection->py_core = py_core; |
| 375 |
Py_INCREF(connection->py_core); |
| 376 |
|
| 377 |
if (aws_http_client_connect(&http_options)) { |
| 378 |
PyErr_SetAwsLastError(); |
| 379 |
goto done; |
| 380 |
} |
| 381 |
success = true; |
| 382 |
|
| 383 |
done: |
| 384 |
if (http2_settings) { |
| 385 |
aws_mem_release(allocator, http2_settings); |
| 386 |
} |
| 387 |
if (!success) { |
| 388 |
aws_ref_count_release(&connection->ref_count); |
| 389 |
return NULL; |
| 390 |
} |
| 391 |
Py_RETURN_NONE; |
| 392 |
} |
| 393 |
|
| 394 |
PyObject *aws_py_http_connection_close(PyObject *self, PyObject *args) { |
| 395 |
(void)self; |
| 396 |
PyObject *capsule; |
| 397 |
if (!PyArg_ParseTuple(args, "O", &capsule)) { |
| 398 |
return NULL; |
| 399 |
} |
| 400 |
|
| 401 |
struct http_connection_binding *connection = PyCapsule_GetPointer(capsule, s_capsule_name_http_connection); |
| 402 |
if (!connection) { |
| 403 |
return NULL; |
| 404 |
} |
| 405 |
|
| 406 |
aws_http_connection_close(connection->native); |
| 407 |
Py_RETURN_NONE; |
| 408 |
} |
| 409 |
|
| 410 |
PyObject *aws_py_http_connection_is_open(PyObject *self, PyObject *args) { |
| 411 |
(void)self; |
| 412 |
PyObject *capsule; |
| 413 |
if (!PyArg_ParseTuple(args, "O", &capsule)) { |
| 414 |
return NULL; |
| 415 |
} |
| 416 |
|
| 417 |
struct http_connection_binding *connection = PyCapsule_GetPointer(capsule, s_capsule_name_http_connection); |
| 418 |
if (!connection) { |
| 419 |
return NULL; |
| 420 |
} |
| 421 |
|
| 422 |
if (aws_http_connection_is_open(connection->native)) { |
| 423 |
Py_RETURN_TRUE; |
| 424 |
} |
| 425 |
Py_RETURN_FALSE; |
| 426 |
} |
| 427 |
|
| 428 |
PyObject *aws_py_http2_connection_update_window(PyObject *self, PyObject *args) { |
| 429 |
(void)self; |
| 430 |
PyObject *capsule; |
| 431 |
size_t increment_size; |
| 432 |
if (!PyArg_ParseTuple(args, "On", &capsule, &increment_size)) { |
| 433 |
return NULL; |
| 434 |
} |
| 435 |
|
| 436 |
struct http_connection_binding *connection = PyCapsule_GetPointer(capsule, s_capsule_name_http_connection); |
| 437 |
if (!connection) { |
| 438 |
return NULL; |
| 439 |
} |
| 440 |
|
| 441 |
aws_http2_connection_update_window(connection->native, increment_size); |
| 442 |
|
| 443 |
Py_RETURN_NONE; |
| 444 |
} |
| 445 |
|
| 446 |
PyObject *aws_py_http_stream_update_window(PyObject *self, PyObject *args) { |
| 447 |
(void)self; |
| 448 |
PyObject *stream_capsule; |
| 449 |
uint32_t increment_size; |
| 450 |
if (!PyArg_ParseTuple(args, "OI", &stream_capsule, &increment_size)) { |
| 451 |
return NULL; |
| 452 |
} |
| 453 |
|
| 454 |
struct aws_http_stream *stream = aws_py_get_http_stream(stream_capsule); |
| 455 |
if (!stream) { |
| 456 |
return NULL; |
| 457 |
} |
| 458 |
|
| 459 |
aws_http_stream_update_window(stream, increment_size); |
| 460 |
|
| 461 |
Py_RETURN_NONE; |
| 462 |
} |