Debian Codegraph @

source/http_headers.c

raw symbols SCIP
1 /**
2 * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3 * SPDX-License-Identifier: Apache-2.0.
4 */
5 #include "http.h"
6
7 #include <aws/http/request_response.h>
8
9 static const char *s_capsule_name_headers = "aws_http_headers";
10
11 /* HttpHeaders._binding capsule contains raw aws_http_headers struct.
12 * There is no intermediate binding struct */
13
14 static struct aws_http_headers *s_headers_from_capsule(PyObject *py_capsule) {
15 return PyCapsule_GetPointer(py_capsule, s_capsule_name_headers);
16 }
17
18 /* Runs when GC destroys the capsule */
19 static void s_headers_capsule_destructor(PyObject *py_capsule) {
20 struct aws_http_headers *headers = s_headers_from_capsule(py_capsule);
21 aws_http_headers_release(headers);
22 }
23
24 struct aws_http_headers *aws_py_get_http_headers(PyObject *http_headers) {
25 return aws_py_get_binding(http_headers, s_capsule_name_headers, "HttpHeaders");
26 }
27
28 PyObject *aws_py_http_headers_new_from_native(struct aws_http_headers *headers) {
29 PyObject *py_capsule = PyCapsule_New(headers, s_capsule_name_headers, s_headers_capsule_destructor);
30 if (!py_capsule) {
31 return NULL;
32 }
33
34 /* Acquire hold so aws_http_headers object lives at least as long as the binding */
35 aws_http_headers_acquire(headers);
36 return py_capsule;
37 }
38
39 PyObject *aws_py_http_headers_new(PyObject *self, PyObject *args) {
40 (void)self;
41 (void)args;
42
43 struct aws_http_headers *headers = aws_http_headers_new(aws_py_get_allocator());
44 if (!headers) {
45 return PyErr_AwsLastError();
46 }
47
48 PyObject *py_capsule = PyCapsule_New(headers, s_capsule_name_headers, s_headers_capsule_destructor);
49 if (!py_capsule) {
50 aws_http_headers_release(headers);
51 return NULL;
52 }
53
54 return py_capsule;
55 }
56
57 /**
58 * Common start to methods whose first argument is the capsule.
59 * FMT: string for PyArg_ParseTuple(). DO NOT pass the "O" for the capsule.
60 * ...: varargs for PyArg_ParseTuple(). DO NOT pass &capsule.
61 *
62 * on error, returns NULL from function with python exception set
63 * on success, creates and sets local variable: aws_http_headers *headers = ...;
64 */
65 #define S_HEADERS_METHOD_START(FMT, ...) \
66 (void)self; \
67 PyObject *py_capsule; \
68 if (!PyArg_ParseTuple(args, "O" FMT, &py_capsule, __VA_ARGS__)) { \
69 return NULL; \
70 } \
71 struct aws_http_headers *headers = s_headers_from_capsule(py_capsule); \
72 if (!headers) { \
73 return NULL; \
74 }
75
76 PyObject *aws_py_http_headers_add(PyObject *self, PyObject *args) {
77 struct aws_byte_cursor name;
78 struct aws_byte_cursor value;
79 S_HEADERS_METHOD_START("s#s#", &name.ptr, &name.len, &value.ptr, &value.len);
80
81 if (aws_http_headers_add(headers, name, value)) {
82 return PyErr_AwsLastError();
83 }
84
85 Py_RETURN_NONE;
86 }
87
88 static bool s_py_http_headers_add_pair(PyObject *py_pair, struct aws_http_headers *headers) {
89
90 const char *type_errmsg = "List of (name,value) pairs expected.";
91 if (!PyTuple_Check(py_pair) || PyTuple_Size(py_pair) != 2) {
92 PyErr_SetString(PyExc_TypeError, type_errmsg);
93 return false;
94 }
95
96 struct aws_byte_cursor name = aws_byte_cursor_from_pyunicode(PyTuple_GetItem(py_pair, 0) /* Borrowed reference */);
97 struct aws_byte_cursor value = aws_byte_cursor_from_pyunicode(PyTuple_GetItem(py_pair, 1) /* Borrowed reference */);
98 if (!name.ptr || !value.ptr) {
99 PyErr_SetString(PyExc_TypeError, type_errmsg);
100 return false;
101 }
102
103 if (aws_http_headers_add(headers, name, value)) {
104 PyErr_SetAwsLastError();
105 return false;
106 }
107
108 return true;
109 }
110
111 PyObject *aws_py_http_headers_add_pairs(PyObject *self, PyObject *args) {
112 PyObject *py_pairs;
113 S_HEADERS_METHOD_START("O", &py_pairs);
114 bool success = false;
115
116 const char *type_errmsg = "List of (name,value) pairs expected.";
117 PyObject *py_sequence = PySequence_Fast(py_pairs, type_errmsg); /* new reference */
118 if (!py_sequence) {
119 return NULL;
120 }
121
122 const Py_ssize_t count = PySequence_Size(py_pairs);
123 for (Py_ssize_t i = 0; i < count; ++i) {
124 PyObject *py_pair = PySequence_GetItem(py_sequence, i); /* New Reference */
125 bool success = s_py_http_headers_add_pair(py_pair, headers);
126 Py_DECREF(py_pair);
127 if (!success) {
128 goto done;
129 }
130 }
131
132 success = true;
133 done:
134 Py_DECREF(py_sequence);
135 if (success) {
136 Py_RETURN_NONE;
137 }
138 return NULL;
139 }
140
141 PyObject *aws_py_http_headers_set(PyObject *self, PyObject *args) {
142 struct aws_byte_cursor name;
143 struct aws_byte_cursor value;
144 S_HEADERS_METHOD_START("s#s#", &name.ptr, &name.len, &value.ptr, &value.len);
145
146 if (aws_http_headers_set(headers, name, value)) {
147 return PyErr_AwsLastError();
148 }
149
150 Py_RETURN_NONE;
151 }
152
153 PyObject *aws_py_http_headers_get(PyObject *self, PyObject *args) {
154 struct aws_byte_cursor name;
155 PyObject *py_default;
156 S_HEADERS_METHOD_START("s#O", &name.ptr, &name.len, &py_default);
157
158 struct aws_byte_cursor value;
159 if (aws_http_headers_get(headers, name, &value)) {
160 /* C API raises error if name not found, but Python API returns a default value */
161 Py_INCREF(py_default);
162 return py_default;
163 }
164
165 return PyUnicode_FromAwsByteCursor(&value);
166 }
167
168 static PyObject *s_py_tuple_from_header(struct aws_http_header header) {
169 PyObject *py_name = NULL;
170 PyObject *py_value = NULL;
171 PyObject *py_pair = NULL;
172
173 py_name = PyUnicode_FromAwsByteCursor(&header.name);
174 if (!py_name) {
175 goto error;
176 }
177
178 py_value = PyUnicode_FromAwsByteCursor(&header.value);
179 if (!py_value) {
180 goto error;
181 }
182
183 py_pair = PyTuple_New(2);
184 if (!py_pair) {
185 goto error;
186 }
187
188 PyTuple_SetItem(py_pair, 0, py_name); /* Steals a reference */
189 PyTuple_SetItem(py_pair, 1, py_value); /* Steals a reference */
190 return py_pair;
191
192 error:
193 Py_XDECREF(py_name);
194 Py_XDECREF(py_value);
195 Py_XDECREF(py_pair);
196 return NULL;
197 }
198
199 PyObject *aws_py_http_headers_get_index(PyObject *self, PyObject *args) {
200 Py_ssize_t index;
201 S_HEADERS_METHOD_START("n", &index);
202
203 struct aws_http_header header;
204 if (aws_http_headers_get_index(headers, index, &header)) {
205 return PyErr_AwsLastError();
206 }
207
208 return s_py_tuple_from_header(header);
209 }
210
211 PyObject *aws_py_http_headers_count(PyObject *self, PyObject *args) {
212 (void)self;
213 PyObject *py_capsule;
214 if (!PyArg_ParseTuple(args, "O", &py_capsule)) {
215 return NULL;
216 }
217
218 struct aws_http_headers *headers = s_headers_from_capsule(py_capsule);
219 if (!headers) {
220 return NULL;
221 }
222
223 return PyLong_FromSize_t(aws_http_headers_count(headers));
224 }
225
226 PyObject *aws_py_http_headers_remove(PyObject *self, PyObject *args) {
227 struct aws_byte_cursor name;
228 S_HEADERS_METHOD_START("s#", &name.ptr, &name.len);
229
230 if (aws_http_headers_erase(headers, name)) {
231 PyErr_SetString(PyExc_KeyError, "HttpHeaders.remove(name): name not found");
232 return NULL;
233 }
234
235 Py_RETURN_NONE;
236 }
237
238 PyObject *aws_py_http_headers_remove_value(PyObject *self, PyObject *args) {
239 struct aws_byte_cursor name;
240 struct aws_byte_cursor value;
241 S_HEADERS_METHOD_START("s#s#", &name.ptr, &name.len, &value.ptr, &value.len);
242
243 if (aws_http_headers_erase_value(headers, name, value)) {
244 PyErr_SetString(PyExc_ValueError, "HttpHeaders.remove_value(name,value): value not found");
245 return NULL;
246 }
247
248 Py_RETURN_NONE;
249 }
250
251 PyObject *aws_py_http_headers_clear(PyObject *self, PyObject *args) {
252 (void)self;
253 PyObject *py_capsule;
254 if (!PyArg_ParseTuple(args, "O", &py_capsule)) {
255 return NULL;
256 }
257
258 struct aws_http_headers *headers = s_headers_from_capsule(py_capsule);
259 if (!headers) {
260 return NULL;
261 }
262
263 aws_http_headers_clear(headers);
264
265 Py_RETURN_NONE;
266 }