Debian Codegraph @

source/http_message.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 "io.h"
8 #include <aws/http/request_response.h>
9
10 static const char *s_capsule_name_http_message = "aws_http_message";
11
12 /**
13 * Bind aws_http_message to HttpRequest/HttpResponse.
14 *
15 * This binding acts differently than most. Instead of keeping the native type in
16 * sync with the Python type at all times, we lazily sync their contents only when we must.
17 * We do this to reduce the impact of repeatedly crossing the language barrier.
18 */
19 struct http_message_binding {
20 struct aws_http_message *native;
21 };
22
23 static void s_http_message_capsule_destructor(PyObject *capsule) {
24 struct http_message_binding *message = PyCapsule_GetPointer(capsule, s_capsule_name_http_message);
25
26 /* Note that destructor may be cleaning up a message that failed part-way through initialization */
27 aws_http_message_release(message->native);
28
29 aws_mem_release(aws_py_get_allocator(), message);
30 }
31
32 static struct http_message_binding *s_binding_from_capsule(PyObject *capsule) {
33 return PyCapsule_GetPointer(capsule, s_capsule_name_http_message);
34 }
35
36 struct aws_http_message *aws_py_get_http_message(PyObject *http_message) {
37 AWS_PY_RETURN_NATIVE_FROM_BINDING(
38 http_message, s_capsule_name_http_message, "HttpMessageBase", http_message_binding);
39 }
40
41 PyObject *aws_py_http_message_new_request_from_native(struct aws_http_message *request) {
42 struct aws_allocator *alloc = aws_py_get_allocator();
43 struct http_message_binding *binding = aws_mem_calloc(alloc, 1, sizeof(struct http_message_binding));
44 if (!binding) {
45 return PyErr_AwsLastError();
46 }
47
48 /* From hereon, we need to clean up if errors occur.
49 * Fortunately, the capsule destructors will clean up anything stored inside them. */
50 PyObject *py_capsule = PyCapsule_New(binding, s_capsule_name_http_message, s_http_message_capsule_destructor);
51 if (!py_capsule) {
52 goto error;
53 }
54
55 binding->native = request;
56 aws_http_message_acquire(binding->native);
57
58 return py_capsule;
59
60 error:
61 if (py_capsule) {
62 Py_DECREF(py_capsule);
63 } else {
64 aws_mem_release(alloc, binding);
65 }
66 return NULL;
67 }
68
69 PyObject *aws_py_http_message_new_request(PyObject *self, PyObject *args) {
70 (void)self;
71
72 PyObject *py_headers;
73 if (!PyArg_ParseTuple(args, "O", &py_headers)) {
74 return NULL;
75 }
76
77 struct aws_http_headers *headers = aws_py_get_http_headers(py_headers);
78 if (!headers) {
79 return NULL;
80 }
81
82 struct aws_http_message *request = aws_http_message_new_request_with_headers(aws_py_get_allocator(), headers);
83 if (!request) {
84 return PyErr_AwsLastError();
85 }
86
87 PyObject *py_capsule = aws_py_http_message_new_request_from_native(request);
88 if (!py_capsule) {
89 aws_http_message_release(request);
90 return NULL;
91 }
92
93 /* The capsule has its own reference to the request now,
94 * release the reference we got for creating it */
95 aws_http_message_release(request);
96
97 return py_capsule;
98 }
99
100 static struct http_message_binding *s_get_binding_from_capsule_arg(PyObject *self, PyObject *args) {
101 (void)self;
102 PyObject *py_capsule;
103 if (!PyArg_ParseTuple(args, "O", &py_capsule)) {
104 return NULL;
105 }
106 return s_binding_from_capsule(py_capsule);
107 }
108
109 PyObject *aws_py_http_message_get_request_method(PyObject *self, PyObject *args) {
110 struct http_message_binding *binding = s_get_binding_from_capsule_arg(self, args);
111 if (!binding) {
112 return NULL;
113 }
114
115 struct aws_byte_cursor method;
116 if (aws_http_message_get_request_method(binding->native, &method)) {
117 Py_RETURN_NONE;
118 }
119
120 return PyUnicode_FromAwsByteCursor(&method);
121 }
122
123 PyObject *aws_py_http_message_set_request_method(PyObject *self, PyObject *args) {
124 (void)self;
125
126 PyObject *py_capsule;
127 struct aws_byte_cursor method;
128 if (!PyArg_ParseTuple(args, "Os#", &py_capsule, &method.ptr, &method.len)) {
129 return NULL;
130 }
131
132 struct http_message_binding *binding = s_binding_from_capsule(py_capsule);
133 if (!binding) {
134 return NULL;
135 }
136
137 if (aws_http_message_set_request_method(binding->native, method)) {
138 return PyErr_AwsLastError();
139 }
140
141 Py_RETURN_NONE;
142 }
143
144 PyObject *aws_py_http_message_get_request_path(PyObject *self, PyObject *args) {
145 struct http_message_binding *binding = s_get_binding_from_capsule_arg(self, args);
146 if (!binding) {
147 return NULL;
148 }
149
150 struct aws_byte_cursor path;
151 if (aws_http_message_get_request_path(binding->native, &path)) {
152 Py_RETURN_NONE;
153 }
154 return PyUnicode_FromAwsByteCursor(&path);
155 }
156
157 PyObject *aws_py_http_message_set_request_path(PyObject *self, PyObject *args) {
158 (void)self;
159
160 PyObject *py_capsule;
161 struct aws_byte_cursor path;
162 if (!PyArg_ParseTuple(args, "Os#", &py_capsule, &path.ptr, &path.len)) {
163 return NULL;
164 }
165
166 struct http_message_binding *binding = s_binding_from_capsule(py_capsule);
167 if (!binding) {
168 return NULL;
169 }
170
171 if (aws_http_message_set_request_path(binding->native, path)) {
172 return PyErr_AwsLastError();
173 }
174
175 Py_RETURN_NONE;
176 }
177
178 PyObject *aws_py_http_message_set_body_stream(PyObject *self, PyObject *args) {
179 (void)self;
180
181 PyObject *py_capsule;
182 PyObject *py_stream;
183 if (!PyArg_ParseTuple(args, "OO", &py_capsule, &py_stream)) {
184 return NULL;
185 }
186
187 struct http_message_binding *binding = s_binding_from_capsule(py_capsule);
188 if (!binding) {
189 return NULL;
190 }
191
192 struct aws_input_stream *stream = NULL;
193 if (py_stream != Py_None) {
194 stream = aws_py_get_input_stream(py_stream);
195 if (!stream) {
196 return PyErr_AwsLastError();
197 }
198 }
199
200 aws_http_message_set_body_stream(binding->native, stream);
201
202 Py_RETURN_NONE;
203 }