| 1 |
#include "pycurl.h" |
| 2 |
|
| 3 |
#ifdef HAVE_CURL_WEBSOCKETS |
| 4 |
|
| 5 |
#include <errno.h> |
| 6 |
|
| 7 |
/* Build a WsFrame namedtuple snapshot from a libcurl frame struct. |
| 8 |
Returns None if meta is NULL, a new reference to a WsFrame otherwise. |
| 9 |
The libcurl-owned pointer is never stored past this call. */ |
| 10 |
static PyObject * |
| 11 |
build_ws_frame(const struct curl_ws_frame *meta) |
| 12 |
{ |
| 13 |
if (meta == NULL) { |
| 14 |
Py_RETURN_NONE; |
| 15 |
} |
| 16 |
return PyObject_CallFunction(ws_frame_type, "iiKKK", |
| 17 |
meta->age, |
| 18 |
meta->flags, |
| 19 |
(unsigned long long)meta->offset, |
| 20 |
(unsigned long long)meta->bytesleft, |
| 21 |
(unsigned long long)meta->len); |
| 22 |
} |
| 23 |
|
| 24 |
static int |
| 25 |
check_ws_curl_state(const CurlObject *self, int allow_write_callback, |
| 26 |
const char *name) |
| 27 |
{ |
| 28 |
PyThreadState *callback_state; |
| 29 |
|
| 30 |
assert_curl_state(self); |
| 31 |
if (self->handle == NULL) { |
| 32 |
PyErr_Format(ErrorObject, "cannot invoke %s() - no curl handle", name); |
| 33 |
return -1; |
| 34 |
} |
| 35 |
callback_state = pycurl_get_thread_state(self); |
| 36 |
if (callback_state != NULL) { |
| 37 |
if (allow_write_callback |
| 38 |
&& self->ws_write_cb_running |
| 39 |
&& PyThreadState_Get() == callback_state) { |
| 40 |
return 0; |
| 41 |
} |
| 42 |
if (allow_write_callback) { |
| 43 |
PyErr_Format(ErrorObject, |
| 44 |
"cannot invoke %s() - perform() is currently running " |
| 45 |
"outside WRITEFUNCTION callback", name); |
| 46 |
} else { |
| 47 |
PyErr_Format(ErrorObject, |
| 48 |
"cannot invoke %s() - perform() is currently running", name); |
| 49 |
} |
| 50 |
return -1; |
| 51 |
} |
| 52 |
return 0; |
| 53 |
} |
| 54 |
|
| 55 |
static int |
| 56 |
is_valid_ws_close_code(long code) |
| 57 |
{ |
| 58 |
switch (code) { |
| 59 |
case 1000: |
| 60 |
case 1001: |
| 61 |
case 1002: |
| 62 |
case 1003: |
| 63 |
case 1007: |
| 64 |
case 1008: |
| 65 |
case 1009: |
| 66 |
case 1010: |
| 67 |
case 1011: |
| 68 |
case 1012: |
| 69 |
case 1013: |
| 70 |
case 1014: |
| 71 |
return 1; |
| 72 |
default: |
| 73 |
return code >= 3000 && code <= 4999; |
| 74 |
} |
| 75 |
} |
| 76 |
|
| 77 |
static int |
| 78 |
validate_ws_close_reason_utf8(const char *reason_ptr, Py_ssize_t reason_len) |
| 79 |
{ |
| 80 |
PyObject *decoded; |
| 81 |
|
| 82 |
if (reason_len == 0) { |
| 83 |
return 0; |
| 84 |
} |
| 85 |
decoded = PyUnicode_DecodeUTF8(reason_ptr, reason_len, "strict"); |
| 86 |
if (decoded == NULL) { |
| 87 |
return -1; |
| 88 |
} |
| 89 |
Py_DECREF(decoded); |
| 90 |
return 0; |
| 91 |
} |
| 92 |
|
| 93 |
/* --------------- ws_send --------------- */ |
| 94 |
|
| 95 |
PYCURL_INTERNAL PyObject * |
| 96 |
do_curl_ws_send(CurlObject *self, PyObject *args, PyObject *kwds) |
| 97 |
{ |
| 98 |
static char *kwlist[] = {"data", "flags", "fragsize", "encoding", NULL}; |
| 99 |
PyObject *data_obj = NULL; |
| 100 |
PyObject *flags_obj = Py_None; |
| 101 |
Py_ssize_t fragsize = 0; |
| 102 |
const char *encoding = "utf-8"; |
| 103 |
unsigned int flags; |
| 104 |
int flags_explicit; |
| 105 |
int data_is_str; |
| 106 |
PyObject *encoded_bytes = NULL; /* owned if we UTF-8 encoded */ |
| 107 |
Py_buffer sendbuf; |
| 108 |
int have_buffer = 0; |
| 109 |
size_t sent = 0; |
| 110 |
CURLcode res; |
| 111 |
PyObject *result = NULL; |
| 112 |
PyThreadState *saved_state; |
| 113 |
|
| 114 |
if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|Ons:ws_send", kwlist, |
| 115 |
&data_obj, &flags_obj, &fragsize, &encoding)) { |
| 116 |
return NULL; |
| 117 |
} |
| 118 |
if (fragsize < 0) { |
| 119 |
PyErr_SetString(PyExc_ValueError, "negative fragsize in ws_send"); |
| 120 |
return NULL; |
| 121 |
} |
| 122 |
|
| 123 |
flags_explicit = (flags_obj != Py_None); |
| 124 |
if (flags_explicit) { |
| 125 |
long long f = PyLong_AsLongLong(flags_obj); |
| 126 |
if (f == -1 && PyErr_Occurred()) { |
| 127 |
return NULL; |
| 128 |
} |
| 129 |
if (f < 0) { |
| 130 |
PyErr_SetString(PyExc_ValueError, "flags must be non-negative"); |
| 131 |
return NULL; |
| 132 |
} |
| 133 |
if ((unsigned long long)f > (unsigned long long)UINT_MAX) { |
| 134 |
PyErr_SetString(PyExc_OverflowError, |
| 135 |
"flags out of range for curl_ws_send"); |
| 136 |
return NULL; |
| 137 |
} |
| 138 |
flags = (unsigned int)f; |
| 139 |
} else { |
| 140 |
flags = 0; |
| 141 |
} |
| 142 |
|
| 143 |
if (flags_explicit |
| 144 |
&& (flags & CURLWS_TEXT) |
| 145 |
&& (flags & CURLWS_BINARY)) { |
| 146 |
PyErr_SetString(PyExc_ValueError, |
| 147 |
"ws_send: cannot set both WS_TEXT and WS_BINARY"); |
| 148 |
return NULL; |
| 149 |
} |
| 150 |
|
| 151 |
if (flags_explicit) { |
| 152 |
unsigned int control_bits = |
| 153 |
flags & (CURLWS_PING | CURLWS_PONG | CURLWS_CLOSE); |
| 154 |
if (control_bits && (flags & CURLWS_CONT)) { |
| 155 |
PyErr_SetString(PyExc_ValueError, |
| 156 |
"ws_send: control frames cannot be fragmented " |
| 157 |
"(WS_CONT forbidden with WS_PING/WS_PONG/WS_CLOSE)"); |
| 158 |
return NULL; |
| 159 |
} |
| 160 |
if (control_bits & (control_bits - 1)) { |
| 161 |
PyErr_SetString(PyExc_ValueError, |
| 162 |
"ws_send: only one of WS_PING / WS_PONG / WS_CLOSE may be set"); |
| 163 |
return NULL; |
| 164 |
} |
| 165 |
} |
| 166 |
|
| 167 |
if (data_obj == Py_None) { |
| 168 |
PyErr_SetString(PyExc_TypeError, |
| 169 |
"ws_send: data must be str or bytes-like, not NoneType " |
| 170 |
"(use b\"\" for an empty payload)"); |
| 171 |
return NULL; |
| 172 |
} |
| 173 |
|
| 174 |
data_is_str = PyUnicode_Check(data_obj); |
| 175 |
if (data_is_str) { |
| 176 |
if (flags_explicit && (flags & CURLWS_BINARY)) { |
| 177 |
PyErr_SetString(PyExc_TypeError, |
| 178 |
"ws_send: str data is incompatible with WS_BINARY; " |
| 179 |
"pass bytes-like data for binary frames"); |
| 180 |
return NULL; |
| 181 |
} |
| 182 |
if (flags_explicit && (flags & CURLWS_CLOSE)) { |
| 183 |
PyErr_SetString(PyExc_TypeError, |
| 184 |
"ws_send: str data is incompatible with WS_CLOSE; " |
| 185 |
"use ws_close() or pass a bytes-like payload"); |
| 186 |
return NULL; |
| 187 |
} |
| 188 |
if (!flags_explicit) { |
| 189 |
flags = CURLWS_TEXT; |
| 190 |
} |
| 191 |
encoded_bytes = PyUnicode_AsEncodedString(data_obj, encoding, "strict"); |
| 192 |
if (encoded_bytes == NULL) { |
| 193 |
return NULL; |
| 194 |
} |
| 195 |
if (PyObject_GetBuffer(encoded_bytes, &sendbuf, PyBUF_SIMPLE) != 0) { |
| 196 |
Py_DECREF(encoded_bytes); |
| 197 |
return NULL; |
| 198 |
} |
| 199 |
have_buffer = 1; |
| 200 |
} else { |
| 201 |
/* bytes-like */ |
| 202 |
if (PyObject_GetBuffer(data_obj, &sendbuf, PyBUF_SIMPLE) != 0) { |
| 203 |
return NULL; |
| 204 |
} |
| 205 |
have_buffer = 1; |
| 206 |
if (!flags_explicit) { |
| 207 |
flags = CURLWS_BINARY; |
| 208 |
} |
| 209 |
} |
| 210 |
|
| 211 |
if (check_ws_curl_state(self, 1, "ws_send") != 0) { |
| 212 |
goto cleanup; |
| 213 |
} |
| 214 |
|
| 215 |
/* Preserve the thread state across the libcurl call so nested |
| 216 |
* Python callbacks (e.g. if we're already inside WRITEFUNCTION) |
| 217 |
* retain their thread context. Mirrors do_curl_pause. */ |
| 218 |
if (self->multi_stack != NULL) { |
| 219 |
saved_state = self->multi_stack->state; |
| 220 |
} else { |
| 221 |
saved_state = self->state; |
| 222 |
} |
| 223 |
|
| 224 |
PYCURL_BEGIN_ALLOW_THREADS_EASY |
| 225 |
res = curl_ws_send(self->handle, sendbuf.buf, (size_t)sendbuf.len, |
| 226 |
&sent, (curl_off_t)fragsize, flags); |
| 227 |
PYCURL_END_ALLOW_THREADS_EASY |
| 228 |
|
| 229 |
if (self->multi_stack != NULL) { |
| 230 |
self->multi_stack->state = saved_state; |
| 231 |
} else { |
| 232 |
self->state = saved_state; |
| 233 |
} |
| 234 |
|
| 235 |
if (check_pending_python_exception_or_signal() != 0) { |
| 236 |
goto cleanup; |
| 237 |
} |
| 238 |
if (check_easy_recv_send_result(self, res) != 0) { |
| 239 |
goto cleanup; |
| 240 |
} |
| 241 |
result = PyLong_FromSsize_t((Py_ssize_t)sent); |
| 242 |
|
| 243 |
cleanup: |
| 244 |
if (have_buffer) { |
| 245 |
PyBuffer_Release(&sendbuf); |
| 246 |
} |
| 247 |
Py_XDECREF(encoded_bytes); |
| 248 |
return result; |
| 249 |
} |
| 250 |
|
| 251 |
/* --------------- ws_recv --------------- */ |
| 252 |
|
| 253 |
PYCURL_INTERNAL PyObject * |
| 254 |
do_curl_ws_recv(CurlObject *self, PyObject *args) |
| 255 |
{ |
| 256 |
Py_ssize_t buflen; |
| 257 |
size_t recvd = 0; |
| 258 |
const struct curl_ws_frame *meta = NULL; |
| 259 |
CURLcode res; |
| 260 |
PyObject *data = NULL, *frame = NULL, *tuple = NULL; |
| 261 |
#if PY_VERSION_HEX >= 0x030F0000 |
| 262 |
PyBytesWriter *writer = NULL; |
| 263 |
void *buf = NULL; |
| 264 |
#else |
| 265 |
PyObject *bytes_obj = NULL; |
| 266 |
char *buf = NULL; |
| 267 |
#endif |
| 268 |
|
| 269 |
if (!PyArg_ParseTuple(args, "n:ws_recv", &buflen)) { |
| 270 |
return NULL; |
| 271 |
} |
| 272 |
if (buflen < 0) { |
| 273 |
PyErr_SetString(PyExc_ValueError, "negative buffersize in ws_recv"); |
| 274 |
return NULL; |
| 275 |
} |
| 276 |
if (check_curl_state(self, 1 | 2, "ws_recv") != 0) { |
| 277 |
return NULL; |
| 278 |
} |
| 279 |
#if PY_VERSION_HEX >= 0x030F0000 |
| 280 |
if (buflen > 0) { |
| 281 |
writer = PyBytesWriter_Create(buflen); |
| 282 |
if (writer == NULL) return NULL; |
| 283 |
buf = PyBytesWriter_GetData(writer); |
| 284 |
} |
| 285 |
#else |
| 286 |
if (buflen > 0) { |
| 287 |
bytes_obj = PyBytes_FromStringAndSize(NULL, buflen); |
| 288 |
if (bytes_obj == NULL) return NULL; |
| 289 |
buf = PyBytes_AS_STRING(bytes_obj); |
| 290 |
} |
| 291 |
#endif |
| 292 |
|
| 293 |
PYCURL_BEGIN_ALLOW_THREADS |
| 294 |
res = curl_ws_recv(self->handle, buf, (size_t)buflen, &recvd, &meta); |
| 295 |
PYCURL_END_ALLOW_THREADS |
| 296 |
|
| 297 |
if (check_pending_python_exception_or_signal() != 0) { |
| 298 |
#if PY_VERSION_HEX >= 0x030F0000 |
| 299 |
if (writer != NULL) { |
| 300 |
PyBytesWriter_Discard(writer); |
| 301 |
} |
| 302 |
#else |
| 303 |
Py_XDECREF(bytes_obj); |
| 304 |
#endif |
| 305 |
return NULL; |
| 306 |
} |
| 307 |
if (check_easy_recv_send_result(self, res) != 0) { |
| 308 |
#if PY_VERSION_HEX >= 0x030F0000 |
| 309 |
if (writer != NULL) { |
| 310 |
PyBytesWriter_Discard(writer); |
| 311 |
} |
| 312 |
#else |
| 313 |
Py_XDECREF(bytes_obj); |
| 314 |
#endif |
| 315 |
return NULL; |
| 316 |
} |
| 317 |
|
| 318 |
#if PY_VERSION_HEX >= 0x030F0000 |
| 319 |
if (writer != NULL) { |
| 320 |
data = PyBytesWriter_FinishWithSize(writer, (Py_ssize_t)recvd); |
| 321 |
if (data == NULL) return NULL; |
| 322 |
} else { |
| 323 |
data = PyBytes_FromStringAndSize("", 0); |
| 324 |
if (data == NULL) return NULL; |
| 325 |
} |
| 326 |
#else |
| 327 |
if (bytes_obj == NULL) { |
| 328 |
data = PyBytes_FromStringAndSize("", 0); |
| 329 |
if (data == NULL) return NULL; |
| 330 |
} else if ((Py_ssize_t)recvd == buflen) { |
| 331 |
data = bytes_obj; |
| 332 |
} else { |
| 333 |
data = PyBytes_FromStringAndSize(buf, (Py_ssize_t)recvd); |
| 334 |
Py_DECREF(bytes_obj); |
| 335 |
if (data == NULL) return NULL; |
| 336 |
} |
| 337 |
#endif |
| 338 |
|
| 339 |
frame = build_ws_frame(meta); |
| 340 |
if (frame == NULL) { Py_DECREF(data); return NULL; } |
| 341 |
tuple = PyTuple_Pack(2, data, frame); |
| 342 |
Py_DECREF(data); Py_DECREF(frame); |
| 343 |
return tuple; |
| 344 |
} |
| 345 |
|
| 346 |
/* --------------- ws_recv_into --------------- */ |
| 347 |
|
| 348 |
PYCURL_INTERNAL PyObject * |
| 349 |
do_curl_ws_recv_into(CurlObject *self, PyObject *args, PyObject *kwds) |
| 350 |
{ |
| 351 |
static char *kwlist[] = {"buffer", "nbytes", NULL}; |
| 352 |
Py_buffer recvbuf; |
| 353 |
Py_ssize_t buflen; |
| 354 |
Py_ssize_t recvlen = 0; |
| 355 |
size_t recvd = 0; |
| 356 |
const struct curl_ws_frame *meta = NULL; |
| 357 |
CURLcode res; |
| 358 |
PyObject *frame, *tuple; |
| 359 |
|
| 360 |
if (!PyArg_ParseTupleAndKeywords(args, kwds, "w*|n:ws_recv_into", kwlist, |
| 361 |
&recvbuf, &recvlen)) { |
| 362 |
return NULL; |
| 363 |
} |
| 364 |
buflen = recvbuf.len; |
| 365 |
if (recvlen < 0) { |
| 366 |
PyBuffer_Release(&recvbuf); |
| 367 |
PyErr_SetString(PyExc_ValueError, "negative buffersize in ws_recv_into"); |
| 368 |
return NULL; |
| 369 |
} |
| 370 |
/* nbytes=0 (the default) means "use the full buffer". Either way, |
| 371 |
* recvlen must not exceed the buffer itself. */ |
| 372 |
if (recvlen == 0) { |
| 373 |
recvlen = buflen; |
| 374 |
} |
| 375 |
if (recvlen > buflen) { |
| 376 |
PyBuffer_Release(&recvbuf); |
| 377 |
PyErr_SetString(PyExc_ValueError, "buffer too small for requested bytes"); |
| 378 |
return NULL; |
| 379 |
} |
| 380 |
if (check_curl_state(self, 1 | 2, "ws_recv_into") != 0) { |
| 381 |
PyBuffer_Release(&recvbuf); |
| 382 |
return NULL; |
| 383 |
} |
| 384 |
PYCURL_BEGIN_ALLOW_THREADS |
| 385 |
res = curl_ws_recv(self->handle, |
| 386 |
recvlen > 0 ? recvbuf.buf : NULL, |
| 387 |
(size_t)recvlen, &recvd, &meta); |
| 388 |
PYCURL_END_ALLOW_THREADS |
| 389 |
|
| 390 |
PyBuffer_Release(&recvbuf); |
| 391 |
|
| 392 |
if (check_pending_python_exception_or_signal() != 0) { |
| 393 |
return NULL; |
| 394 |
} |
| 395 |
if (check_easy_recv_send_result(self, res) != 0) { |
| 396 |
return NULL; |
| 397 |
} |
| 398 |
|
| 399 |
frame = build_ws_frame(meta); |
| 400 |
if (frame == NULL) return NULL; |
| 401 |
tuple = Py_BuildValue("(nN)", (Py_ssize_t)recvd, frame); |
| 402 |
return tuple; |
| 403 |
} |
| 404 |
|
| 405 |
/* --------------- ws_meta --------------- */ |
| 406 |
|
| 407 |
PYCURL_INTERNAL PyObject * |
| 408 |
do_curl_ws_meta(CurlObject *self, PyObject *Py_UNUSED(ignored)) |
| 409 |
{ |
| 410 |
const struct curl_ws_frame *meta; |
| 411 |
|
| 412 |
if (check_curl_state(self, 1, "ws_meta") != 0) { |
| 413 |
return NULL; |
| 414 |
} |
| 415 |
meta = curl_ws_meta(self->handle); |
| 416 |
return build_ws_frame(meta); |
| 417 |
} |
| 418 |
|
| 419 |
/* --------------- ws_close --------------- */ |
| 420 |
|
| 421 |
PYCURL_INTERNAL PyObject * |
| 422 |
do_curl_ws_close(CurlObject *self, PyObject *args, PyObject *kwds) |
| 423 |
{ |
| 424 |
static char *kwlist[] = {"code", "reason", "encoding", NULL}; |
| 425 |
PyObject *code_obj = Py_None; |
| 426 |
PyObject *reason_obj = Py_None; |
| 427 |
const char *encoding = "utf-8"; |
| 428 |
int code = 0; |
| 429 |
int have_code = 0; |
| 430 |
PyObject *encoded_reason = NULL; /* owned if we UTF-8 encoded */ |
| 431 |
Py_buffer reason_buf; |
| 432 |
int have_reason_buf = 0; |
| 433 |
const char *reason_ptr = NULL; |
| 434 |
Py_ssize_t reason_len = 0; |
| 435 |
unsigned char payload[125]; |
| 436 |
Py_ssize_t payload_len = 0; |
| 437 |
size_t sent = 0; |
| 438 |
CURLcode res; |
| 439 |
PyObject *result = NULL; |
| 440 |
PyThreadState *saved_state; |
| 441 |
|
| 442 |
if (!PyArg_ParseTupleAndKeywords(args, kwds, "|OOs:ws_close", kwlist, |
| 443 |
&code_obj, &reason_obj, &encoding)) { |
| 444 |
return NULL; |
| 445 |
} |
| 446 |
|
| 447 |
if (code_obj != Py_None) { |
| 448 |
long c = PyLong_AsLong(code_obj); |
| 449 |
if (c == -1 && PyErr_Occurred()) { |
| 450 |
return NULL; |
| 451 |
} |
| 452 |
if (!is_valid_ws_close_code(c)) { |
| 453 |
PyErr_SetString(PyExc_ValueError, |
| 454 |
"ws_close: code must be a valid wire close status code"); |
| 455 |
return NULL; |
| 456 |
} |
| 457 |
code = (int)c; |
| 458 |
have_code = 1; |
| 459 |
} |
| 460 |
|
| 461 |
if (reason_obj != Py_None) { |
| 462 |
if (!have_code) { |
| 463 |
PyErr_SetString(PyExc_ValueError, |
| 464 |
"ws_close: reason requires code (RFC 6455 §5.5.1)"); |
| 465 |
return NULL; |
| 466 |
} |
| 467 |
if (PyUnicode_Check(reason_obj)) { |
| 468 |
encoded_reason = PyUnicode_AsEncodedString( |
| 469 |
reason_obj, encoding, "strict"); |
| 470 |
if (encoded_reason == NULL) { |
| 471 |
return NULL; |
| 472 |
} |
| 473 |
if (PyObject_GetBuffer(encoded_reason, &reason_buf, |
| 474 |
PyBUF_SIMPLE) != 0) { |
| 475 |
Py_DECREF(encoded_reason); |
| 476 |
return NULL; |
| 477 |
} |
| 478 |
} else { |
| 479 |
if (PyObject_GetBuffer(reason_obj, &reason_buf, |
| 480 |
PyBUF_SIMPLE) != 0) { |
| 481 |
return NULL; |
| 482 |
} |
| 483 |
} |
| 484 |
have_reason_buf = 1; |
| 485 |
reason_ptr = (const char *)reason_buf.buf; |
| 486 |
reason_len = reason_buf.len; |
| 487 |
} |
| 488 |
|
| 489 |
if (validate_ws_close_reason_utf8(reason_ptr, reason_len) != 0) { |
| 490 |
goto cleanup; |
| 491 |
} |
| 492 |
|
| 493 |
/* Total payload: 2 bytes for code (if any) + reason bytes. |
| 494 |
* RFC 6455 §5.5 caps control-frame payloads at 125 bytes. */ |
| 495 |
if (have_code) { |
| 496 |
payload_len = 2 + reason_len; |
| 497 |
} else { |
| 498 |
payload_len = 0; /* empty close */ |
| 499 |
} |
| 500 |
if (payload_len > (Py_ssize_t)sizeof(payload)) { |
| 501 |
PyErr_Format(PyExc_ValueError, |
| 502 |
"ws_close: payload too large (%zd bytes, max 125 including " |
| 503 |
"2-byte status code)", payload_len); |
| 504 |
goto cleanup; |
| 505 |
} |
| 506 |
|
| 507 |
if (have_code) { |
| 508 |
payload[0] = (unsigned char)((code >> 8) & 0xff); |
| 509 |
payload[1] = (unsigned char)(code & 0xff); |
| 510 |
if (reason_len > 0) { |
| 511 |
memcpy(payload + 2, reason_ptr, (size_t)reason_len); |
| 512 |
} |
| 513 |
} |
| 514 |
|
| 515 |
if (check_ws_curl_state(self, 1, "ws_close") != 0) { |
| 516 |
goto cleanup; |
| 517 |
} |
| 518 |
|
| 519 |
if (self->multi_stack != NULL) { |
| 520 |
saved_state = self->multi_stack->state; |
| 521 |
} else { |
| 522 |
saved_state = self->state; |
| 523 |
} |
| 524 |
|
| 525 |
PYCURL_BEGIN_ALLOW_THREADS_EASY |
| 526 |
res = curl_ws_send(self->handle, payload, (size_t)payload_len, |
| 527 |
&sent, (curl_off_t)0, CURLWS_CLOSE); |
| 528 |
PYCURL_END_ALLOW_THREADS_EASY |
| 529 |
|
| 530 |
if (self->multi_stack != NULL) { |
| 531 |
self->multi_stack->state = saved_state; |
| 532 |
} else { |
| 533 |
self->state = saved_state; |
| 534 |
} |
| 535 |
|
| 536 |
if (check_pending_python_exception_or_signal() != 0) { |
| 537 |
goto cleanup; |
| 538 |
} |
| 539 |
if (check_easy_recv_send_result(self, res) != 0) { |
| 540 |
goto cleanup; |
| 541 |
} |
| 542 |
result = PyLong_FromSsize_t((Py_ssize_t)sent); |
| 543 |
|
| 544 |
cleanup: |
| 545 |
if (have_reason_buf) { |
| 546 |
PyBuffer_Release(&reason_buf); |
| 547 |
} |
| 548 |
Py_XDECREF(encoded_reason); |
| 549 |
return result; |
| 550 |
} |
| 551 |
|
| 552 |
#endif /* HAVE_CURL_WEBSOCKETS */ |