Debian Codegraph @

src/easyperform.c

raw
1 #include "pycurl.h"
2 #include <errno.h>
3
4 /* --------------- perform --------------- */
5
6 PYCURL_INTERNAL PyObject *
7 do_curl_perform(CurlObject *self, PyObject *Py_UNUSED(ignored))
8 {
9 int res;
10
11 if (check_curl_state(self, 1 | 2, "perform") != 0) {
12 return NULL;
13 }
14
15 PYCURL_BEGIN_ALLOW_THREADS
16 res = curl_easy_perform(self->handle);
17 PYCURL_END_ALLOW_THREADS
18
19 if (check_pending_python_exception_or_signal() != 0) {
20 return NULL;
21 }
22
23 if (res != CURLE_OK) {
24 CURLERROR_RETVAL();
25 }
26 Py_RETURN_NONE;
27 }
28
29
30 PYCURL_INTERNAL PyObject *
31 do_curl_perform_rb(CurlObject *self, PyObject *Py_UNUSED(ignored))
32 {
33 PyObject *v, *io;
34
35 /* NOTE: this tuple is never freed. */
36 static PyObject *empty_tuple = NULL;
37
38 if (empty_tuple == NULL) {
39 empty_tuple = PyTuple_New(0);
40 if (empty_tuple == NULL) {
41 return NULL;
42 }
43 }
44
45 io = PyObject_Call(bytesio, empty_tuple, NULL);
46 if (io == NULL) {
47 return NULL;
48 }
49
50 v = do_curl_setopt_filelike(self, CURLOPT_WRITEDATA, io);
51 if (v == NULL) {
52 Py_DECREF(io);
53 return NULL;
54 }
55
56 v = do_curl_perform(self, NULL);
57 if (v == NULL) {
58 Py_DECREF(io);
59 return NULL;
60 }
61
62 v = PyObject_CallMethod(io, "getvalue", NULL);
63 Py_DECREF(io);
64 return v;
65 }
66
67 PYCURL_INTERNAL PyObject *
68 do_curl_perform_rs(CurlObject *self, PyObject *Py_UNUSED(ignored))
69 {
70 PyObject *v, *decoded;
71
72 v = do_curl_perform_rb(self, NULL);
73 if (v == NULL) {
74 return NULL;
75 }
76
77 decoded = PyUnicode_FromEncodedObject(v, NULL, NULL);
78 Py_DECREF(v);
79 return decoded;
80 }
81
82
83 /* --------------- send/recv --------------- */
84
85 PYCURL_INTERNAL PyObject *
86 set_would_block_error(void)
87 {
88 errno = EAGAIN;
89 PyErr_SetFromErrno(PyExc_BlockingIOError);
90 return NULL;
91 }
92
93 PYCURL_INTERNAL int
94 check_easy_recv_send_result(CurlObject *self, CURLcode res)
95 {
96 if (res == CURLE_OK) {
97 return 0;
98 }
99 if (res == CURLE_AGAIN) {
100 set_would_block_error();
101 return -1;
102 }
103 create_and_set_error_object(self, (int)res);
104 return -1;
105 }
106
107 static int
108 perform_easy_send(CurlObject *self, const void *buf, size_t len, size_t *sent)
109 {
110 CURLcode res;
111
112 PYCURL_BEGIN_ALLOW_THREADS
113 res = curl_easy_send(self->handle, buf, len, sent);
114 PYCURL_END_ALLOW_THREADS
115
116 if (check_pending_python_exception_or_signal() != 0) {
117 return -1;
118 }
119
120 return check_easy_recv_send_result(self, res);
121 }
122
123 static int
124 perform_easy_recv(CurlObject *self, void *buf, size_t len, size_t *recvd)
125 {
126 CURLcode res;
127
128 PYCURL_BEGIN_ALLOW_THREADS
129 res = curl_easy_recv(self->handle, buf, len, recvd);
130 PYCURL_END_ALLOW_THREADS
131
132 if (check_pending_python_exception_or_signal() != 0) {
133 return -1;
134 }
135
136 return check_easy_recv_send_result(self, res);
137 }
138
139 PYCURL_INTERNAL PyObject *
140 do_curl_send(CurlObject *self, PyObject *args)
141 {
142 Py_buffer sendbuf;
143 size_t sent = 0;
144
145 if (!PyArg_ParseTuple(args, "y*:send", &sendbuf)) {
146 return NULL;
147 }
148 if (check_curl_state(self, 1 | 2, "send") != 0) {
149 PyBuffer_Release(&sendbuf);
150 return NULL;
151 }
152
153 if (perform_easy_send(self, sendbuf.buf, (size_t)sendbuf.len, &sent) != 0) {
154 PyBuffer_Release(&sendbuf);
155 return NULL;
156 }
157 PyBuffer_Release(&sendbuf);
158
159 return PyLong_FromSsize_t((Py_ssize_t)sent);
160 }
161
162 PYCURL_INTERNAL PyObject *
163 do_curl_recv(CurlObject *self, PyObject *args)
164 {
165 Py_ssize_t buflen;
166 size_t recvd = 0;
167 #if PY_VERSION_HEX >= 0x030F0000
168 /* PyBytesWriter is part of the public C API starting with Python 3.15. */
169 PyBytesWriter *writer;
170 #else
171 PyObject *bytes_obj;
172 char *buf = NULL;
173 PyObject *result = NULL;
174 #endif
175
176 if (!PyArg_ParseTuple(args, "n:recv", &buflen)) {
177 return NULL;
178 }
179 if (buflen < 0) {
180 PyErr_SetString(PyExc_ValueError, "negative buffersize in recv");
181 return NULL;
182 }
183 if (check_curl_state(self, 1 | 2, "recv") != 0) {
184 return NULL;
185 }
186 if (buflen == 0) {
187 return PyBytes_FromStringAndSize("", 0);
188 }
189
190 #if PY_VERSION_HEX >= 0x030F0000
191 writer = PyBytesWriter_Create(buflen);
192 if (writer == NULL) {
193 return NULL;
194 }
195
196 if (perform_easy_recv(self, PyBytesWriter_GetData(writer), (size_t)buflen, &recvd) != 0) {
197 PyBytesWriter_Discard(writer);
198 return NULL;
199 }
200
201 return PyBytesWriter_FinishWithSize(writer, (Py_ssize_t)recvd);
202 #else
203 bytes_obj = PyBytes_FromStringAndSize(NULL, buflen);
204 if (bytes_obj == NULL) {
205 return NULL;
206 }
207 buf = PyBytes_AS_STRING(bytes_obj);
208
209 if (perform_easy_recv(self, buf, (size_t)buflen, &recvd) != 0) {
210 Py_DECREF(bytes_obj);
211 return NULL;
212 }
213
214 if ((Py_ssize_t)recvd == buflen) {
215 return bytes_obj;
216 }
217
218 result = PyBytes_FromStringAndSize(buf, (Py_ssize_t)recvd);
219 Py_DECREF(bytes_obj);
220 return result;
221 #endif
222 }
223
224 PYCURL_INTERNAL PyObject *
225 do_curl_recv_into(CurlObject *self, PyObject *args, PyObject *kwds)
226 {
227 static char *kwlist[] = {"buffer", "nbytes", NULL};
228 Py_buffer recvbuf;
229 Py_ssize_t buflen;
230 Py_ssize_t recvlen = 0;
231 size_t recvd = 0;
232
233 if (!PyArg_ParseTupleAndKeywords(args, kwds, "w*|n:recv_into", kwlist,
234 &recvbuf, &recvlen)) {
235 return NULL;
236 }
237 buflen = recvbuf.len;
238 if (recvlen < 0) {
239 PyBuffer_Release(&recvbuf);
240 PyErr_SetString(PyExc_ValueError, "negative buffersize in recv_into");
241 return NULL;
242 }
243 if (recvlen == 0) {
244 recvlen = buflen;
245 }
246 if (recvlen > buflen) {
247 PyBuffer_Release(&recvbuf);
248 PyErr_SetString(PyExc_ValueError, "buffer too small for requested bytes");
249 return NULL;
250 }
251 if (check_curl_state(self, 1 | 2, "recv_into") != 0) {
252 PyBuffer_Release(&recvbuf);
253 return NULL;
254 }
255 if (recvlen == 0) {
256 PyBuffer_Release(&recvbuf);
257 return PyLong_FromLong(0);
258 }
259
260 if (perform_easy_recv(self, recvbuf.buf, (size_t)recvlen, &recvd) != 0) {
261 PyBuffer_Release(&recvbuf);
262 return NULL;
263 }
264 PyBuffer_Release(&recvbuf);
265
266 return PyLong_FromSize_t(recvd);
267 }
268
269
270 /* --------------- pause --------------- */
271
272
273 /* curl_easy_pause() can be called from inside a callback or outside */
274 static PyObject *
275 do_curl_pause_internal(CurlObject *self, int bitmask, const char *op_name)
276 {
277 CURLcode res;
278 PyThreadState *saved_state;
279
280 if (check_curl_state(self, 1, op_name) != 0) {
281 return NULL;
282 }
283
284 /* Save handle to current thread (used as context for python callbacks) */
285 if (self->multi_stack != NULL) {
286 saved_state = self->multi_stack->state;
287 } else {
288 saved_state = self->state;
289 }
290
291 /* We must allow threads here because unpausing a handle can cause
292 some of its callbacks to be invoked immediately, from inside
293 curl_easy_pause() */
294
295 PYCURL_BEGIN_ALLOW_THREADS_EASY
296 res = curl_easy_pause(self->handle, bitmask);
297 PYCURL_END_ALLOW_THREADS_EASY
298
299 /* Restore the thread-state to whatever it was on entry */
300 if (self->multi_stack != NULL) {
301 self->multi_stack->state = saved_state;
302 } else {
303 self->state = saved_state;
304 }
305
306 if (check_pending_python_exception_or_signal() != 0) {
307 return NULL;
308 }
309
310 if (res != CURLE_OK) {
311 CURLERROR_MSG("pause/unpause failed");
312 } else {
313 Py_RETURN_NONE;
314 }
315 }
316
317
318 PYCURL_INTERNAL PyObject *
319 do_curl_pause(CurlObject *self, PyObject *args)
320 {
321 int bitmask = CURLPAUSE_ALL;
322
323 if (!PyArg_ParseTuple(args, "|i:pause", &bitmask)) {
324 return NULL;
325 }
326
327 return do_curl_pause_internal(self, bitmask, "pause");
328 }
329
330
331 PYCURL_INTERNAL PyObject *
332 do_curl_unpause(CurlObject *self, PyObject *Py_UNUSED(ignored))
333 {
334 return do_curl_pause_internal(self, CURLPAUSE_CONT, "unpause");
335 }