ws.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 1998 - 2022, Daniel Stenberg, <daniel@haxx.se>, et al.
  9. *
  10. * This software is licensed as described in the file COPYING, which
  11. * you should have received as part of this distribution. The terms
  12. * are also available at https://curl.se/docs/copyright.html.
  13. *
  14. * You may opt to use, copy, modify, merge, publish, distribute and/or sell
  15. * copies of the Software, and permit persons to whom the Software is
  16. * furnished to do so, under the terms of the COPYING file.
  17. *
  18. * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
  19. * KIND, either express or implied.
  20. *
  21. * SPDX-License-Identifier: curl
  22. *
  23. ***************************************************************************/
  24. #include "curl_setup.h"
  25. #include <curl/curl.h>
  26. #ifdef USE_WEBSOCKETS
  27. #include "urldata.h"
  28. #include "dynbuf.h"
  29. #include "rand.h"
  30. #include "curl_base64.h"
  31. #include "sendf.h"
  32. #include "multiif.h"
  33. #include "ws.h"
  34. #include "easyif.h"
  35. #include "transfer.h"
  36. #include "nonblock.h"
  37. /* The last 3 #include files should be in this order */
  38. #include "curl_printf.h"
  39. #include "curl_memory.h"
  40. #include "memdebug.h"
  41. struct wsfield {
  42. const char *name;
  43. const char *val;
  44. };
  45. CURLcode Curl_ws_request(struct Curl_easy *data, REQTYPE *req)
  46. {
  47. unsigned int i;
  48. CURLcode result = CURLE_OK;
  49. unsigned char rand[16];
  50. char *randstr;
  51. size_t randlen;
  52. char keyval[40];
  53. struct SingleRequest *k = &data->req;
  54. struct wsfield heads[]= {
  55. {
  56. /* The request MUST contain an |Upgrade| header field whose value
  57. MUST include the "websocket" keyword. */
  58. "Upgrade:", "websocket"
  59. },
  60. {
  61. /* The request MUST contain a |Connection| header field whose value
  62. MUST include the "Upgrade" token. */
  63. "Connection:", "Upgrade",
  64. },
  65. {
  66. /* The request MUST include a header field with the name
  67. |Sec-WebSocket-Version|. The value of this header field MUST be
  68. 13. */
  69. "Sec-WebSocket-Version:", "13",
  70. },
  71. {
  72. /* The request MUST include a header field with the name
  73. |Sec-WebSocket-Key|. The value of this header field MUST be a nonce
  74. consisting of a randomly selected 16-byte value that has been
  75. base64-encoded (see Section 4 of [RFC4648]). The nonce MUST be
  76. selected randomly for each connection. */
  77. "Sec-WebSocket-Key:", NULL,
  78. }
  79. };
  80. heads[3].val = &keyval[0];
  81. /* 16 bytes random */
  82. result = Curl_rand(data, (unsigned char *)rand, sizeof(rand));
  83. if(result)
  84. return result;
  85. result = Curl_base64_encode((char *)rand, sizeof(rand), &randstr, &randlen);
  86. if(result)
  87. return result;
  88. DEBUGASSERT(randlen < sizeof(keyval));
  89. if(randlen >= sizeof(keyval))
  90. return CURLE_FAILED_INIT;
  91. strcpy(keyval, randstr);
  92. free(randstr);
  93. for(i = 0; !result && (i < sizeof(heads)/sizeof(heads[0])); i++) {
  94. if(!Curl_checkheaders(data, STRCONST(heads[i].name))) {
  95. #ifdef USE_HYPER
  96. char field[128];
  97. msnprintf(field, sizeof(field), "%s %s", heads[i].name,
  98. heads[i].val);
  99. result = Curl_hyper_header(data, req, field);
  100. #else
  101. (void)data;
  102. result = Curl_dyn_addf(req, "%s %s\r\n", heads[i].name,
  103. heads[i].val);
  104. #endif
  105. }
  106. }
  107. k->upgr101 = UPGR101_WS;
  108. Curl_dyn_init(&data->req.p.http->ws.buf, MAX_WS_SIZE * 2);
  109. return result;
  110. }
  111. CURLcode Curl_ws_accept(struct Curl_easy *data)
  112. {
  113. struct SingleRequest *k = &data->req;
  114. struct HTTP *ws = data->req.p.http;
  115. struct connectdata *conn = data->conn;
  116. struct websocket *wsp = &data->req.p.http->ws;
  117. CURLcode result;
  118. /* Verify the Sec-WebSocket-Accept response.
  119. The sent value is the base64 encoded version of a SHA-1 hash done on the
  120. |Sec-WebSocket-Key| header field concatenated with
  121. the string "258EAFA5-E914-47DA-95CA-C5AB0DC85B11".
  122. */
  123. /* If the response includes a |Sec-WebSocket-Extensions| header field and
  124. this header field indicates the use of an extension that was not present
  125. in the client's handshake (the server has indicated an extension not
  126. requested by the client), the client MUST Fail the WebSocket Connection.
  127. */
  128. /* If the response includes a |Sec-WebSocket-Protocol| header field
  129. and this header field indicates the use of a subprotocol that was
  130. not present in the client's handshake (the server has indicated a
  131. subprotocol not requested by the client), the client MUST Fail
  132. the WebSocket Connection. */
  133. /* 4 bytes random */
  134. result = Curl_rand(data, (unsigned char *)&ws->ws.mask, sizeof(ws->ws.mask));
  135. if(result)
  136. return result;
  137. infof(data, "Received 101, switch to WebSocket; mask %02x%02x%02x%02x",
  138. ws->ws.mask[0], ws->ws.mask[1], ws->ws.mask[2], ws->ws.mask[3]);
  139. k->upgr101 = UPGR101_RECEIVED;
  140. if(data->set.connect_only)
  141. /* switch off non-blocking sockets */
  142. (void)curlx_nonblock(conn->sock[FIRSTSOCKET], FALSE);
  143. wsp->oleft = 0;
  144. return result;
  145. }
  146. #define WSBIT_FIN 0x80
  147. #define WSBIT_OPCODE_CONT 0
  148. #define WSBIT_OPCODE_TEXT (1)
  149. #define WSBIT_OPCODE_BIN (2)
  150. #define WSBIT_OPCODE_CLOSE (8)
  151. #define WSBIT_OPCODE_PING (9)
  152. #define WSBIT_OPCODE_PONG (0xa)
  153. #define WSBIT_OPCODE_MASK (0xf)
  154. #define WSBIT_MASK 0x80
  155. /* remove the spent bytes from the beginning of the buffer as that part has
  156. now been delivered to the application */
  157. static void ws_decode_clear(struct Curl_easy *data)
  158. {
  159. struct websocket *wsp = &data->req.p.http->ws;
  160. size_t spent = wsp->usedbuf;
  161. size_t len = Curl_dyn_len(&wsp->buf);
  162. size_t keep = len - spent;
  163. DEBUGASSERT(len >= spent);
  164. Curl_dyn_tail(&wsp->buf, keep);
  165. }
  166. /* ws_decode() decodes a binary frame into structured WebSocket data,
  167. wpkt - the incoming raw data. If NULL, work on the already buffered data.
  168. ilen - the size of the provided data, perhaps too little, perhaps too much
  169. out - stored pointed to extracted data
  170. olen - stored length of the extracted data
  171. oleft - number of unread bytes pending to that belongs to this frame
  172. more - if there is more data in there
  173. flags - stored bitmask about the frame
  174. Returns CURLE_AGAIN if there is only a partial frame in the buffer. Then it
  175. stores the first part in the ->extra buffer to be used in the next call
  176. when more data is provided.
  177. */
  178. static CURLcode ws_decode(struct Curl_easy *data,
  179. unsigned char *wpkt, size_t ilen,
  180. unsigned char **out, size_t *olen,
  181. curl_off_t *oleft,
  182. bool *more,
  183. unsigned int *flags)
  184. {
  185. bool fin;
  186. unsigned char opcode;
  187. curl_off_t total;
  188. size_t dataindex = 2;
  189. curl_off_t plen; /* size of data in the buffer */
  190. curl_off_t payloadsize;
  191. struct websocket *wsp = &data->req.p.http->ws;
  192. unsigned char *p;
  193. CURLcode result;
  194. *olen = 0;
  195. /* add the incoming bytes, if any */
  196. if(wpkt) {
  197. result = Curl_dyn_addn(&wsp->buf, wpkt, ilen);
  198. if(result)
  199. return result;
  200. }
  201. plen = Curl_dyn_len(&wsp->buf);
  202. if(plen < 2) {
  203. /* the smallest possible frame is two bytes */
  204. infof(data, "WS: plen == %u, EAGAIN", (int)plen);
  205. return CURLE_AGAIN;
  206. }
  207. p = Curl_dyn_uptr(&wsp->buf);
  208. fin = p[0] & WSBIT_FIN;
  209. opcode = p[0] & WSBIT_OPCODE_MASK;
  210. infof(data, "WS:%d received FIN bit %u", __LINE__, (int)fin);
  211. *flags = 0;
  212. switch(opcode) {
  213. case WSBIT_OPCODE_CONT:
  214. if(!fin)
  215. *flags |= CURLWS_CONT;
  216. infof(data, "WS: received OPCODE CONT");
  217. break;
  218. case WSBIT_OPCODE_TEXT:
  219. infof(data, "WS: received OPCODE TEXT");
  220. *flags |= CURLWS_TEXT;
  221. break;
  222. case WSBIT_OPCODE_BIN:
  223. infof(data, "WS: received OPCODE BINARY");
  224. *flags |= CURLWS_BINARY;
  225. break;
  226. case WSBIT_OPCODE_CLOSE:
  227. infof(data, "WS: received OPCODE CLOSE");
  228. *flags |= CURLWS_CLOSE;
  229. break;
  230. case WSBIT_OPCODE_PING:
  231. infof(data, "WS: received OPCODE PING");
  232. *flags |= CURLWS_PING;
  233. break;
  234. case WSBIT_OPCODE_PONG:
  235. infof(data, "WS: received OPCODE PONG");
  236. *flags |= CURLWS_PONG;
  237. break;
  238. }
  239. if(p[1] & WSBIT_MASK) {
  240. /* A client MUST close a connection if it detects a masked frame. */
  241. failf(data, "WS: masked input frame");
  242. return CURLE_RECV_ERROR;
  243. }
  244. payloadsize = p[1];
  245. if(payloadsize == 126) {
  246. if(plen < 4) {
  247. infof(data, "WS:%d plen == %u, EAGAIN", __LINE__, (int)plen);
  248. return CURLE_AGAIN; /* not enough data available */
  249. }
  250. payloadsize = (p[2] << 8) | p[3];
  251. dataindex += 2;
  252. }
  253. else if(payloadsize == 127) {
  254. /* 64 bit payload size */
  255. if(plen < 10)
  256. return CURLE_AGAIN;
  257. if(p[2] & 80) {
  258. failf(data, "WS: too large frame");
  259. return CURLE_RECV_ERROR;
  260. }
  261. dataindex += 8;
  262. payloadsize = ((curl_off_t)p[2] << 56) |
  263. (curl_off_t)p[3] << 48 |
  264. (curl_off_t)p[4] << 40 |
  265. (curl_off_t)p[5] << 32 |
  266. (curl_off_t)p[6] << 24 |
  267. (curl_off_t)p[7] << 16 |
  268. (curl_off_t)p[8] << 8 |
  269. p[9];
  270. }
  271. total = dataindex + payloadsize;
  272. if(total > plen) {
  273. /* deliver a partial frame */
  274. *oleft = total - dataindex;
  275. payloadsize = total - dataindex;
  276. }
  277. else {
  278. *oleft = 0;
  279. if(plen > total)
  280. /* there is another fragment after */
  281. *more = TRUE;
  282. }
  283. /* point to the payload */
  284. *out = &p[dataindex];
  285. /* return the payload length */
  286. *olen = payloadsize;
  287. /* number of bytes "used" from the buffer */
  288. wsp->usedbuf = dataindex + payloadsize;
  289. infof(data, "WS: received %zu bytes payload (%zu left)",
  290. payloadsize, *oleft);
  291. return CURLE_OK;
  292. }
  293. /* Curl_ws_writecb() is the write callback for websocket traffic. The
  294. websocket data is provided to this raw, in chunks. This function should
  295. handle/decode the data and call the "real" underlying callback accordingly.
  296. */
  297. size_t Curl_ws_writecb(char *buffer, size_t size /* 1 */,
  298. size_t nitems, void *userp)
  299. {
  300. struct HTTP *ws = (struct HTTP *)userp;
  301. struct Curl_easy *data = ws->ws.data;
  302. void *writebody_ptr = data->set.out;
  303. if(data->set.ws_raw_mode)
  304. return data->set.fwrite_func(buffer, size, nitems, writebody_ptr);
  305. else if(nitems) {
  306. unsigned char *frame = NULL;
  307. size_t flen = 0;
  308. size_t wrote = 0;
  309. CURLcode result;
  310. bool more; /* there's is more to parse in the buffer */
  311. curl_off_t oleft;
  312. decode:
  313. more = FALSE;
  314. oleft = ws->ws.frame.bytesleft;
  315. if(!oleft) {
  316. unsigned int recvflags;
  317. result = ws_decode(data, (unsigned char *)buffer, nitems,
  318. &frame, &flen, &oleft, &more, &recvflags);
  319. if(result == CURLE_AGAIN)
  320. /* insufficient amount of data, keep it for later */
  321. return nitems;
  322. else if(result) {
  323. infof(data, "WS: decode error %d", (int)result);
  324. return nitems - 1;
  325. }
  326. /* Store details about the frame to be reachable with curl_ws_meta()
  327. from within the write callback */
  328. ws->ws.frame.age = 0;
  329. ws->ws.frame.offset = 0;
  330. ws->ws.frame.flags = recvflags;
  331. ws->ws.frame.bytesleft = oleft;
  332. }
  333. else {
  334. if(nitems > (size_t)ws->ws.frame.bytesleft) {
  335. nitems = ws->ws.frame.bytesleft;
  336. more = TRUE;
  337. }
  338. else
  339. more = FALSE;
  340. ws->ws.frame.offset += nitems;
  341. ws->ws.frame.bytesleft -= nitems;
  342. frame = (unsigned char *)buffer;
  343. flen = nitems;
  344. }
  345. if((ws->ws.frame.flags & CURLWS_PING) && !oleft) {
  346. /* auto-respond to PINGs, only works for single-frame payloads atm */
  347. size_t bytes;
  348. infof(data, "WS: auto-respond to PING with a PONG");
  349. DEBUGASSERT(frame);
  350. /* send back the exact same content as a PONG */
  351. result = curl_ws_send(data, frame, flen, &bytes, 0, CURLWS_PONG);
  352. if(result)
  353. return result;
  354. }
  355. else {
  356. /* deliver the decoded frame to the user callback */
  357. Curl_set_in_callback(data, true);
  358. wrote = data->set.fwrite_func((char *)frame, 1, flen, writebody_ptr);
  359. Curl_set_in_callback(data, false);
  360. if(wrote != flen)
  361. return 0;
  362. }
  363. if(oleft)
  364. ws->ws.frame.offset += flen;
  365. /* the websocket frame has been delivered */
  366. ws_decode_clear(data);
  367. if(more) {
  368. /* there's more websocket data to deal with in the buffer */
  369. buffer = NULL; /* the buffer as been drained already */
  370. goto decode;
  371. }
  372. }
  373. return nitems;
  374. }
  375. CURL_EXTERN CURLcode curl_ws_recv(struct Curl_easy *data, void *buffer,
  376. size_t buflen, size_t *nread,
  377. struct curl_ws_frame **metap)
  378. {
  379. size_t bytes;
  380. CURLcode result;
  381. struct websocket *wsp = &data->req.p.http->ws;
  382. *nread = 0;
  383. *metap = NULL;
  384. /* get a download buffer */
  385. result = Curl_preconnect(data);
  386. if(result)
  387. return result;
  388. do {
  389. bool drain = FALSE; /* if there is pending buffered data to drain */
  390. char *inbuf = data->state.buffer;
  391. bytes = wsp->stillbuffer;
  392. if(!bytes) {
  393. result = curl_easy_recv(data, data->state.buffer,
  394. data->set.buffer_size, &bytes);
  395. if(result)
  396. return result;
  397. }
  398. else {
  399. /* the pending bytes can be found here */
  400. inbuf = wsp->stillb;
  401. drain = TRUE;
  402. }
  403. if(bytes) {
  404. unsigned char *out;
  405. size_t olen;
  406. bool more;
  407. unsigned int recvflags;
  408. curl_off_t oleft = wsp->frame.bytesleft;
  409. infof(data, "WS: got %u websocket bytes to decode", (int)bytes);
  410. if(!oleft && !drain) {
  411. result = ws_decode(data, (unsigned char *)inbuf, bytes,
  412. &out, &olen, &oleft, &more, &recvflags);
  413. if(result == CURLE_AGAIN)
  414. /* a packet fragment only */
  415. break;
  416. else if(result)
  417. return result;
  418. wsp->frame.offset = 0;
  419. wsp->frame.bytesleft = oleft;
  420. wsp->frame.flags = recvflags;
  421. }
  422. else {
  423. olen = oleft;
  424. out = (unsigned char *)wsp->stillb;
  425. recvflags = wsp->frame.flags;
  426. if((curl_off_t)buflen < oleft)
  427. /* there is still data left after this */
  428. wsp->frame.bytesleft -= buflen;
  429. else
  430. wsp->frame.bytesleft = 0;
  431. }
  432. /* auto-respond to PINGs */
  433. if((recvflags & CURLWS_PING) && !oleft) {
  434. infof(data, "WS: auto-respond to PING with a PONG");
  435. /* send back the exact same content as a PONG */
  436. result = curl_ws_send(data, out, olen, &bytes, 0, CURLWS_PONG);
  437. if(result)
  438. return result;
  439. }
  440. else {
  441. if(olen < buflen) {
  442. /* copy the payload to the user buffer */
  443. memcpy(buffer, out, olen);
  444. *nread = olen;
  445. if(!oleft)
  446. /* websocket frame has been delivered */
  447. ws_decode_clear(data);
  448. }
  449. else {
  450. /* copy a partial payload */
  451. memcpy(buffer, out, buflen);
  452. *nread = buflen;
  453. /* remember what is left and where */
  454. wsp->stillbuffer = olen - buflen;
  455. wsp->stillb = (char *)buffer + buflen;
  456. }
  457. wsp->frame.offset += *nread;
  458. }
  459. }
  460. else
  461. *nread = bytes;
  462. break;
  463. } while(1);
  464. *metap = &wsp->frame;
  465. return CURLE_OK;
  466. }
  467. static void ws_xor(struct Curl_easy *data,
  468. const unsigned char *source,
  469. unsigned char *dest,
  470. size_t len)
  471. {
  472. struct websocket *wsp = &data->req.p.http->ws;
  473. size_t i;
  474. /* append payload after the mask, XOR appropriately */
  475. for(i = 0; i < len; i++) {
  476. dest[i] = source[i] ^ wsp->mask[wsp->xori];
  477. wsp->xori++;
  478. wsp->xori &= 3;
  479. }
  480. }
  481. /***
  482. RFC 6455 Section 5.2
  483. 0 1 2 3
  484. 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
  485. +-+-+-+-+-------+-+-------------+-------------------------------+
  486. |F|R|R|R| opcode|M| Payload len | Extended payload length |
  487. |I|S|S|S| (4) |A| (7) | (16/64) |
  488. |N|V|V|V| |S| | (if payload len==126/127) |
  489. | |1|2|3| |K| | |
  490. +-+-+-+-+-------+-+-------------+ - - - - - - - - - - - - - - - +
  491. | Extended payload length continued, if payload len == 127 |
  492. + - - - - - - - - - - - - - - - +-------------------------------+
  493. | |Masking-key, if MASK set to 1 |
  494. +-------------------------------+-------------------------------+
  495. | Masking-key (continued) | Payload Data |
  496. +-------------------------------- - - - - - - - - - - - - - - - +
  497. : Payload Data continued ... :
  498. + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - +
  499. | Payload Data continued ... |
  500. +---------------------------------------------------------------+
  501. */
  502. static size_t ws_packethead(struct Curl_easy *data,
  503. size_t len, unsigned int flags)
  504. {
  505. struct HTTP *ws = data->req.p.http;
  506. unsigned char *out = (unsigned char *)data->state.ulbuf;
  507. unsigned char firstbyte = 0;
  508. int outi;
  509. unsigned char opcode;
  510. if(flags & CURLWS_TEXT) {
  511. opcode = WSBIT_OPCODE_TEXT;
  512. infof(data, "WS: send OPCODE TEXT");
  513. }
  514. else if(flags & CURLWS_CLOSE) {
  515. opcode = WSBIT_OPCODE_CLOSE;
  516. infof(data, "WS: send OPCODE CLOSE");
  517. }
  518. else if(flags & CURLWS_PING) {
  519. opcode = WSBIT_OPCODE_PING;
  520. infof(data, "WS: send OPCODE PING");
  521. }
  522. else if(flags & CURLWS_PONG) {
  523. opcode = WSBIT_OPCODE_PONG;
  524. infof(data, "WS: send OPCODE PONG");
  525. }
  526. else {
  527. opcode = WSBIT_OPCODE_BIN;
  528. infof(data, "WS: send OPCODE BINARY");
  529. }
  530. if(!(flags & CURLWS_CONT)) {
  531. /* if not marked as continuing, assume this is the final fragment */
  532. firstbyte |= WSBIT_FIN | opcode;
  533. ws->ws.contfragment = FALSE;
  534. }
  535. else if(ws->ws.contfragment) {
  536. /* the previous fragment was not a final one and this isn't either, keep a
  537. CONT opcode and no FIN bit */
  538. firstbyte |= WSBIT_OPCODE_CONT;
  539. }
  540. else {
  541. ws->ws.contfragment = TRUE;
  542. }
  543. out[0] = firstbyte;
  544. if(len > 65535) {
  545. out[1] = 127 | WSBIT_MASK;
  546. out[2] = (len >> 8) & 0xff;
  547. out[3] = len & 0xff;
  548. outi = 10;
  549. }
  550. else if(len > 126) {
  551. out[1] = 126 | WSBIT_MASK;
  552. out[2] = (len >> 8) & 0xff;
  553. out[3] = len & 0xff;
  554. outi = 4;
  555. }
  556. else {
  557. out[1] = (unsigned char)len | WSBIT_MASK;
  558. outi = 2;
  559. }
  560. infof(data, "WS: send FIN bit %u (byte %02x)",
  561. firstbyte & WSBIT_FIN ? 1 : 0,
  562. firstbyte);
  563. infof(data, "WS: send payload len %u", (int)len);
  564. /* 4 bytes mask */
  565. memcpy(&out[outi], &ws->ws.mask, 4);
  566. if(data->set.upload_buffer_size < (len + 10))
  567. return 0;
  568. /* pass over the mask */
  569. outi += 4;
  570. ws->ws.xori = 0;
  571. /* return packet size */
  572. return outi;
  573. }
  574. CURL_EXTERN CURLcode curl_ws_send(struct Curl_easy *data, const void *buffer,
  575. size_t buflen, size_t *sent,
  576. curl_off_t totalsize,
  577. unsigned int sendflags)
  578. {
  579. CURLcode result;
  580. size_t headlen;
  581. char *out;
  582. ssize_t written;
  583. struct websocket *wsp = &data->req.p.http->ws;
  584. if(!data->set.ws_raw_mode) {
  585. result = Curl_get_upload_buffer(data);
  586. if(result)
  587. return result;
  588. }
  589. else {
  590. if(totalsize || sendflags)
  591. return CURLE_BAD_FUNCTION_ARGUMENT;
  592. }
  593. if(data->set.ws_raw_mode) {
  594. if(!buflen)
  595. /* nothing to do */
  596. return CURLE_OK;
  597. /* raw mode sends exactly what was requested, and this is from within
  598. the write callback */
  599. if(Curl_is_in_callback(data))
  600. result = Curl_write(data, data->conn->writesockfd, buffer, buflen,
  601. &written);
  602. else
  603. result = Curl_senddata(data, buffer, buflen, &written);
  604. infof(data, "WS: wanted to send %zu bytes, sent %zu bytes",
  605. buflen, written);
  606. *sent = written;
  607. return result;
  608. }
  609. if(buflen > (data->set.upload_buffer_size - 10))
  610. /* don't do more than this in one go */
  611. buflen = data->set.upload_buffer_size - 10;
  612. if(sendflags & CURLWS_OFFSET) {
  613. if(totalsize) {
  614. /* a frame series 'totalsize' bytes big, this is the first */
  615. headlen = ws_packethead(data, totalsize, sendflags);
  616. wsp->sleft = totalsize - buflen;
  617. }
  618. else {
  619. headlen = 0;
  620. if((curl_off_t)buflen > wsp->sleft) {
  621. infof(data, "WS: unaligned frame size (sending %zu instead of %zu)",
  622. buflen, wsp->sleft);
  623. wsp->sleft = 0;
  624. }
  625. else
  626. wsp->sleft -= buflen;
  627. }
  628. }
  629. else
  630. headlen = ws_packethead(data, buflen, sendflags);
  631. /* headlen is the size of the frame header */
  632. out = data->state.ulbuf;
  633. if(buflen)
  634. /* for PING and PONG etc there might not be a payload */
  635. ws_xor(data, buffer, (unsigned char *)out + headlen, buflen);
  636. if(data->set.connect_only)
  637. result = Curl_senddata(data, out, buflen + headlen, &written);
  638. else
  639. result = Curl_write(data, data->conn->writesockfd, out,
  640. buflen + headlen, &written);
  641. infof(data, "WS: wanted to send %zu bytes, sent %zu bytes",
  642. headlen + buflen, written);
  643. *sent = written;
  644. return result;
  645. }
  646. void Curl_ws_done(struct Curl_easy *data)
  647. {
  648. struct websocket *wsp = &data->req.p.http->ws;
  649. DEBUGASSERT(wsp);
  650. Curl_dyn_free(&wsp->buf);
  651. }
  652. CURL_EXTERN struct curl_ws_frame *curl_ws_meta(struct Curl_easy *data)
  653. {
  654. /* we only return something for websocket, called from within the callback
  655. when not using raw mode */
  656. if(GOOD_EASY_HANDLE(data) && Curl_is_in_callback(data) && data->req.p.http &&
  657. !data->set.ws_raw_mode)
  658. return &data->req.p.http->ws.frame;
  659. return NULL;
  660. }
  661. #else
  662. CURL_EXTERN CURLcode curl_ws_recv(CURL *curl, void *buffer, size_t buflen,
  663. size_t *nread,
  664. struct curl_ws_frame **metap)
  665. {
  666. (void)curl;
  667. (void)buffer;
  668. (void)buflen;
  669. (void)nread;
  670. (void)metap;
  671. return CURLE_OK;
  672. }
  673. CURL_EXTERN CURLcode curl_ws_send(CURL *curl, const void *buffer,
  674. size_t buflen, size_t *sent,
  675. curl_off_t framesize,
  676. unsigned int sendflags)
  677. {
  678. (void)curl;
  679. (void)buffer;
  680. (void)buflen;
  681. (void)sent;
  682. (void)framesize;
  683. (void)sendflags;
  684. return CURLE_OK;
  685. }
  686. CURL_EXTERN struct curl_ws_frame *curl_ws_meta(struct Curl_easy *data)
  687. {
  688. (void)data;
  689. return NULL;
  690. }
  691. #endif /* USE_WEBSOCKETS */