| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
#include "auth.h" |
| 7 |
|
| 8 |
#include "http.h" |
| 9 |
|
| 10 |
#include <aws/auth/signable.h> |
| 11 |
#include <aws/auth/signing.h> |
| 12 |
#include <aws/auth/signing_result.h> |
| 13 |
|
| 14 |
|
| 15 |
struct async_signing_data { |
| 16 |
PyObject *py_http_request; |
| 17 |
struct aws_http_message *http_request; |
| 18 |
PyObject *py_signing_config; |
| 19 |
PyObject *py_on_complete; |
| 20 |
struct aws_signable *signable; |
| 21 |
}; |
| 22 |
|
| 23 |
static void s_async_signing_data_destroy(struct async_signing_data *async_data) { |
| 24 |
if (async_data) { |
| 25 |
Py_XDECREF(async_data->py_http_request); |
| 26 |
Py_XDECREF(async_data->py_signing_config); |
| 27 |
Py_XDECREF(async_data->py_on_complete); |
| 28 |
aws_signable_destroy(async_data->signable); |
| 29 |
aws_mem_release(aws_py_get_allocator(), async_data); |
| 30 |
} |
| 31 |
} |
| 32 |
|
| 33 |
static void s_signing_complete(struct aws_signing_result *signing_result, int error_code, void *userdata) { |
| 34 |
struct async_signing_data *async_data = userdata; |
| 35 |
|
| 36 |
if (!error_code) { |
| 37 |
struct aws_allocator *allocator = aws_py_get_allocator(); |
| 38 |
|
| 39 |
if (aws_apply_signing_result_to_http_request(async_data->http_request, allocator, signing_result)) { |
| 40 |
error_code = aws_last_error(); |
| 41 |
} |
| 42 |
} |
| 43 |
|
| 44 |
|
| 45 |
PyGILState_STATE state; |
| 46 |
if (aws_py_gilstate_ensure(&state)) { |
| 47 |
return; |
| 48 |
} |
| 49 |
|
| 50 |
PyObject *py_result = PyObject_CallFunction(async_data->py_on_complete, "(i)", error_code); |
| 51 |
if (py_result) { |
| 52 |
Py_DECREF(py_result); |
| 53 |
} else { |
| 54 |
PyErr_WriteUnraisable(PyErr_Occurred()); |
| 55 |
} |
| 56 |
|
| 57 |
s_async_signing_data_destroy(async_data); |
| 58 |
|
| 59 |
PyGILState_Release(state); |
| 60 |
|
| 61 |
} |
| 62 |
|
| 63 |
PyObject *aws_py_sign_request_aws(PyObject *self, PyObject *args) { |
| 64 |
(void)self; |
| 65 |
|
| 66 |
PyObject *py_http_request; |
| 67 |
PyObject *py_signing_config; |
| 68 |
PyObject *py_on_complete; |
| 69 |
if (!PyArg_ParseTuple(args, "OOO", &py_http_request, &py_signing_config, &py_on_complete)) { |
| 70 |
return NULL; |
| 71 |
} |
| 72 |
|
| 73 |
struct aws_http_message *http_request = aws_py_get_http_message(py_http_request); |
| 74 |
if (!http_request) { |
| 75 |
return NULL; |
| 76 |
} |
| 77 |
|
| 78 |
struct aws_signing_config_aws *signing_config = aws_py_get_signing_config(py_signing_config); |
| 79 |
if (!signing_config) { |
| 80 |
return NULL; |
| 81 |
} |
| 82 |
|
| 83 |
AWS_FATAL_ASSERT(py_on_complete != Py_None); |
| 84 |
|
| 85 |
struct aws_allocator *alloc = aws_py_get_allocator(); |
| 86 |
|
| 87 |
struct async_signing_data *async_data = aws_mem_calloc(alloc, 1, sizeof(struct async_signing_data)); |
| 88 |
if (!async_data) { |
| 89 |
return PyErr_AwsLastError(); |
| 90 |
} |
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
async_data->py_http_request = py_http_request; |
| 96 |
Py_INCREF(async_data->py_http_request); |
| 97 |
|
| 98 |
async_data->http_request = http_request; |
| 99 |
|
| 100 |
async_data->py_signing_config = py_signing_config; |
| 101 |
Py_INCREF(async_data->py_signing_config); |
| 102 |
|
| 103 |
async_data->py_on_complete = py_on_complete; |
| 104 |
Py_INCREF(async_data->py_on_complete); |
| 105 |
|
| 106 |
async_data->signable = aws_signable_new_http_request(aws_py_get_allocator(), http_request); |
| 107 |
if (!async_data->signable) { |
| 108 |
PyErr_SetAwsLastError(); |
| 109 |
goto error; |
| 110 |
} |
| 111 |
|
| 112 |
if (aws_sign_request_aws( |
| 113 |
alloc, |
| 114 |
async_data->signable, |
| 115 |
(struct aws_signing_config_base *)signing_config, |
| 116 |
s_signing_complete, |
| 117 |
async_data)) { |
| 118 |
PyErr_SetAwsLastError(); |
| 119 |
goto error; |
| 120 |
} |
| 121 |
|
| 122 |
Py_RETURN_NONE; |
| 123 |
|
| 124 |
error: |
| 125 |
s_async_signing_data_destroy(async_data); |
| 126 |
return NULL; |
| 127 |
} |