2
0

ws.c 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 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. #if defined(USE_WEBSOCKETS) && !defined(CURL_DISABLE_HTTP)
  27. #include "urldata.h"
  28. #include "bufq.h"
  29. #include "dynbuf.h"
  30. #include "rand.h"
  31. #include "curl_base64.h"
  32. #include "connect.h"
  33. #include "sendf.h"
  34. #include "multiif.h"
  35. #include "ws.h"
  36. #include "easyif.h"
  37. #include "transfer.h"
  38. #include "nonblock.h"
  39. /* The last 3 #include files should be in this order */
  40. #include "curl_printf.h"
  41. #include "curl_memory.h"
  42. #include "memdebug.h"
  43. #define WSBIT_FIN 0x80
  44. #define WSBIT_OPCODE_CONT 0
  45. #define WSBIT_OPCODE_TEXT (1)
  46. #define WSBIT_OPCODE_BIN (2)
  47. #define WSBIT_OPCODE_CLOSE (8)
  48. #define WSBIT_OPCODE_PING (9)
  49. #define WSBIT_OPCODE_PONG (0xa)
  50. #define WSBIT_OPCODE_MASK (0xf)
  51. #define WSBIT_MASK 0x80
  52. /* buffer dimensioning */
  53. #define WS_CHUNK_SIZE 65535
  54. #define WS_CHUNK_COUNT 2
  55. struct ws_frame_meta {
  56. char proto_opcode;
  57. int flags;
  58. const char *name;
  59. };
  60. static struct ws_frame_meta WS_FRAMES[] = {
  61. { WSBIT_OPCODE_CONT, CURLWS_CONT, "CONT" },
  62. { WSBIT_OPCODE_TEXT, CURLWS_TEXT, "TEXT" },
  63. { WSBIT_OPCODE_BIN, CURLWS_BINARY, "BIN" },
  64. { WSBIT_OPCODE_CLOSE, CURLWS_CLOSE, "CLOSE" },
  65. { WSBIT_OPCODE_PING, CURLWS_PING, "PING" },
  66. { WSBIT_OPCODE_PONG, CURLWS_PONG, "PONG" },
  67. };
  68. static const char *ws_frame_name_of_op(unsigned char proto_opcode)
  69. {
  70. unsigned char opcode = proto_opcode & WSBIT_OPCODE_MASK;
  71. size_t i;
  72. for(i = 0; i < sizeof(WS_FRAMES)/sizeof(WS_FRAMES[0]); ++i) {
  73. if(WS_FRAMES[i].proto_opcode == opcode)
  74. return WS_FRAMES[i].name;
  75. }
  76. return "???";
  77. }
  78. static int ws_frame_op2flags(unsigned char proto_opcode)
  79. {
  80. unsigned char opcode = proto_opcode & WSBIT_OPCODE_MASK;
  81. size_t i;
  82. for(i = 0; i < sizeof(WS_FRAMES)/sizeof(WS_FRAMES[0]); ++i) {
  83. if(WS_FRAMES[i].proto_opcode == opcode)
  84. return WS_FRAMES[i].flags;
  85. }
  86. return 0;
  87. }
  88. static unsigned char ws_frame_flags2op(int flags)
  89. {
  90. size_t i;
  91. for(i = 0; i < sizeof(WS_FRAMES)/sizeof(WS_FRAMES[0]); ++i) {
  92. if(WS_FRAMES[i].flags & flags)
  93. return WS_FRAMES[i].proto_opcode;
  94. }
  95. return 0;
  96. }
  97. static void ws_dec_info(struct ws_decoder *dec, struct Curl_easy *data,
  98. const char *msg)
  99. {
  100. switch(dec->head_len) {
  101. case 0:
  102. break;
  103. case 1:
  104. CURL_TRC_WRITE(data, "websocket, decoded %s [%s%s]", msg,
  105. ws_frame_name_of_op(dec->head[0]),
  106. (dec->head[0] & WSBIT_FIN)? "" : " NON-FINAL");
  107. break;
  108. default:
  109. if(dec->head_len < dec->head_total) {
  110. CURL_TRC_WRITE(data, "websocket, decoded %s [%s%s](%d/%d)", msg,
  111. ws_frame_name_of_op(dec->head[0]),
  112. (dec->head[0] & WSBIT_FIN)? "" : " NON-FINAL",
  113. dec->head_len, dec->head_total);
  114. }
  115. else {
  116. CURL_TRC_WRITE(data, "websocket, decoded %s [%s%s payload=%"
  117. CURL_FORMAT_CURL_OFF_T "/%" CURL_FORMAT_CURL_OFF_T "]",
  118. msg, ws_frame_name_of_op(dec->head[0]),
  119. (dec->head[0] & WSBIT_FIN)? "" : " NON-FINAL",
  120. dec->payload_offset, dec->payload_len);
  121. }
  122. break;
  123. }
  124. }
  125. typedef ssize_t ws_write_payload(const unsigned char *buf, size_t buflen,
  126. int frame_age, int frame_flags,
  127. curl_off_t payload_offset,
  128. curl_off_t payload_len,
  129. void *userp,
  130. CURLcode *err);
  131. static void ws_dec_reset(struct ws_decoder *dec)
  132. {
  133. dec->frame_age = 0;
  134. dec->frame_flags = 0;
  135. dec->payload_offset = 0;
  136. dec->payload_len = 0;
  137. dec->head_len = dec->head_total = 0;
  138. dec->state = WS_DEC_INIT;
  139. }
  140. static void ws_dec_init(struct ws_decoder *dec)
  141. {
  142. ws_dec_reset(dec);
  143. }
  144. static CURLcode ws_dec_read_head(struct ws_decoder *dec,
  145. struct Curl_easy *data,
  146. struct bufq *inraw)
  147. {
  148. const unsigned char *inbuf;
  149. size_t inlen;
  150. while(Curl_bufq_peek(inraw, &inbuf, &inlen)) {
  151. if(dec->head_len == 0) {
  152. dec->head[0] = *inbuf;
  153. Curl_bufq_skip(inraw, 1);
  154. dec->frame_flags = ws_frame_op2flags(dec->head[0]);
  155. if(!dec->frame_flags) {
  156. failf(data, "WS: unknown opcode: %x", dec->head[0]);
  157. ws_dec_reset(dec);
  158. return CURLE_RECV_ERROR;
  159. }
  160. dec->head_len = 1;
  161. /* ws_dec_info(dec, data, "seeing opcode"); */
  162. continue;
  163. }
  164. else if(dec->head_len == 1) {
  165. dec->head[1] = *inbuf;
  166. Curl_bufq_skip(inraw, 1);
  167. dec->head_len = 2;
  168. if(dec->head[1] & WSBIT_MASK) {
  169. /* A client MUST close a connection if it detects a masked frame. */
  170. failf(data, "WS: masked input frame");
  171. ws_dec_reset(dec);
  172. return CURLE_RECV_ERROR;
  173. }
  174. /* How long is the frame head? */
  175. if(dec->head[1] == 126) {
  176. dec->head_total = 4;
  177. continue;
  178. }
  179. else if(dec->head[1] == 127) {
  180. dec->head_total = 10;
  181. continue;
  182. }
  183. else {
  184. dec->head_total = 2;
  185. }
  186. }
  187. if(dec->head_len < dec->head_total) {
  188. dec->head[dec->head_len] = *inbuf;
  189. Curl_bufq_skip(inraw, 1);
  190. ++dec->head_len;
  191. if(dec->head_len < dec->head_total) {
  192. /* ws_dec_info(dec, data, "decoding head"); */
  193. continue;
  194. }
  195. }
  196. /* got the complete frame head */
  197. DEBUGASSERT(dec->head_len == dec->head_total);
  198. switch(dec->head_total) {
  199. case 2:
  200. dec->payload_len = dec->head[1];
  201. break;
  202. case 4:
  203. dec->payload_len = (dec->head[2] << 8) | dec->head[3];
  204. break;
  205. case 10:
  206. if(dec->head[2] > 127) {
  207. failf(data, "WS: frame length longer than 64 signed not supported");
  208. return CURLE_RECV_ERROR;
  209. }
  210. dec->payload_len = ((curl_off_t)dec->head[2] << 56) |
  211. (curl_off_t)dec->head[3] << 48 |
  212. (curl_off_t)dec->head[4] << 40 |
  213. (curl_off_t)dec->head[5] << 32 |
  214. (curl_off_t)dec->head[6] << 24 |
  215. (curl_off_t)dec->head[7] << 16 |
  216. (curl_off_t)dec->head[8] << 8 |
  217. dec->head[9];
  218. break;
  219. default:
  220. /* this should never happen */
  221. DEBUGASSERT(0);
  222. failf(data, "WS: unexpected frame header length");
  223. return CURLE_RECV_ERROR;
  224. }
  225. dec->frame_age = 0;
  226. dec->payload_offset = 0;
  227. ws_dec_info(dec, data, "decoded");
  228. return CURLE_OK;
  229. }
  230. return CURLE_AGAIN;
  231. }
  232. static CURLcode ws_dec_pass_payload(struct ws_decoder *dec,
  233. struct Curl_easy *data,
  234. struct bufq *inraw,
  235. ws_write_payload *write_payload,
  236. void *write_ctx)
  237. {
  238. const unsigned char *inbuf;
  239. size_t inlen;
  240. ssize_t nwritten;
  241. CURLcode result;
  242. curl_off_t remain = dec->payload_len - dec->payload_offset;
  243. (void)data;
  244. while(remain && Curl_bufq_peek(inraw, &inbuf, &inlen)) {
  245. if((curl_off_t)inlen > remain)
  246. inlen = (size_t)remain;
  247. nwritten = write_payload(inbuf, inlen, dec->frame_age, dec->frame_flags,
  248. dec->payload_offset, dec->payload_len,
  249. write_ctx, &result);
  250. if(nwritten < 0)
  251. return result;
  252. Curl_bufq_skip(inraw, (size_t)nwritten);
  253. dec->payload_offset += (curl_off_t)nwritten;
  254. remain = dec->payload_len - dec->payload_offset;
  255. CURL_TRC_WRITE(data, "websocket, passed %zd bytes payload, %"
  256. CURL_FORMAT_CURL_OFF_T " remain", nwritten, remain);
  257. }
  258. return remain? CURLE_AGAIN : CURLE_OK;
  259. }
  260. static CURLcode ws_dec_pass(struct ws_decoder *dec,
  261. struct Curl_easy *data,
  262. struct bufq *inraw,
  263. ws_write_payload *write_payload,
  264. void *write_ctx)
  265. {
  266. CURLcode result;
  267. if(Curl_bufq_is_empty(inraw))
  268. return CURLE_AGAIN;
  269. switch(dec->state) {
  270. case WS_DEC_INIT:
  271. ws_dec_reset(dec);
  272. dec->state = WS_DEC_HEAD;
  273. FALLTHROUGH();
  274. case WS_DEC_HEAD:
  275. result = ws_dec_read_head(dec, data, inraw);
  276. if(result) {
  277. if(result != CURLE_AGAIN) {
  278. infof(data, "WS: decode error %d", (int)result);
  279. break; /* real error */
  280. }
  281. /* incomplete ws frame head */
  282. DEBUGASSERT(Curl_bufq_is_empty(inraw));
  283. break;
  284. }
  285. /* head parsing done */
  286. dec->state = WS_DEC_PAYLOAD;
  287. if(dec->payload_len == 0) {
  288. ssize_t nwritten;
  289. const unsigned char tmp = '\0';
  290. /* special case of a 0 length frame, need to write once */
  291. nwritten = write_payload(&tmp, 0, dec->frame_age, dec->frame_flags,
  292. 0, 0, write_ctx, &result);
  293. if(nwritten < 0)
  294. return result;
  295. dec->state = WS_DEC_INIT;
  296. break;
  297. }
  298. FALLTHROUGH();
  299. case WS_DEC_PAYLOAD:
  300. result = ws_dec_pass_payload(dec, data, inraw, write_payload, write_ctx);
  301. ws_dec_info(dec, data, "passing");
  302. if(result)
  303. return result;
  304. /* paylod parsing done */
  305. dec->state = WS_DEC_INIT;
  306. break;
  307. default:
  308. /* we covered all enums above, but some code analyzers are whimps */
  309. result = CURLE_FAILED_INIT;
  310. }
  311. return result;
  312. }
  313. static void update_meta(struct websocket *ws,
  314. int frame_age, int frame_flags,
  315. curl_off_t payload_offset,
  316. curl_off_t payload_len,
  317. size_t cur_len)
  318. {
  319. ws->frame.age = frame_age;
  320. ws->frame.flags = frame_flags;
  321. ws->frame.offset = payload_offset;
  322. ws->frame.len = cur_len;
  323. ws->frame.bytesleft = (payload_len - payload_offset - cur_len);
  324. }
  325. /* WebSockets decoding client writer */
  326. struct ws_cw_ctx {
  327. struct Curl_cwriter super;
  328. struct bufq buf;
  329. };
  330. static CURLcode ws_cw_init(struct Curl_easy *data,
  331. struct Curl_cwriter *writer)
  332. {
  333. struct ws_cw_ctx *ctx = writer->ctx;
  334. (void)data;
  335. Curl_bufq_init2(&ctx->buf, WS_CHUNK_SIZE, 1, BUFQ_OPT_SOFT_LIMIT);
  336. return CURLE_OK;
  337. }
  338. static void ws_cw_close(struct Curl_easy *data, struct Curl_cwriter *writer)
  339. {
  340. struct ws_cw_ctx *ctx = writer->ctx;
  341. (void) data;
  342. Curl_bufq_free(&ctx->buf);
  343. }
  344. struct ws_cw_dec_ctx {
  345. struct Curl_easy *data;
  346. struct websocket *ws;
  347. struct Curl_cwriter *next_writer;
  348. int cw_type;
  349. };
  350. static ssize_t ws_cw_dec_next(const unsigned char *buf, size_t buflen,
  351. int frame_age, int frame_flags,
  352. curl_off_t payload_offset,
  353. curl_off_t payload_len,
  354. void *user_data,
  355. CURLcode *err)
  356. {
  357. struct ws_cw_dec_ctx *ctx = user_data;
  358. struct Curl_easy *data = ctx->data;
  359. struct websocket *ws = ctx->ws;
  360. curl_off_t remain = (payload_len - (payload_offset + buflen));
  361. (void)frame_age;
  362. if((frame_flags & CURLWS_PING) && !remain) {
  363. /* auto-respond to PINGs, only works for single-frame payloads atm */
  364. size_t bytes;
  365. infof(data, "WS: auto-respond to PING with a PONG");
  366. /* send back the exact same content as a PONG */
  367. *err = curl_ws_send(data, buf, buflen, &bytes, 0, CURLWS_PONG);
  368. if(*err)
  369. return -1;
  370. }
  371. else if(buflen || !remain) {
  372. /* forward the decoded frame to the next client writer. */
  373. update_meta(ws, frame_age, frame_flags, payload_offset,
  374. payload_len, buflen);
  375. *err = Curl_cwriter_write(data, ctx->next_writer, ctx->cw_type,
  376. (const char *)buf, buflen);
  377. if(*err)
  378. return -1;
  379. }
  380. *err = CURLE_OK;
  381. return (ssize_t)buflen;
  382. }
  383. static CURLcode ws_cw_write(struct Curl_easy *data,
  384. struct Curl_cwriter *writer, int type,
  385. const char *buf, size_t nbytes)
  386. {
  387. struct ws_cw_ctx *ctx = writer->ctx;
  388. struct websocket *ws;
  389. CURLcode result;
  390. if(!(type & CLIENTWRITE_BODY) || data->set.ws_raw_mode)
  391. return Curl_cwriter_write(data, writer->next, type, buf, nbytes);
  392. ws = data->conn->proto.ws;
  393. if(!ws) {
  394. failf(data, "WS: not a websocket transfer");
  395. return CURLE_FAILED_INIT;
  396. }
  397. if(nbytes) {
  398. ssize_t nwritten;
  399. nwritten = Curl_bufq_write(&ctx->buf, (const unsigned char *)buf,
  400. nbytes, &result);
  401. if(nwritten < 0) {
  402. infof(data, "WS: error adding data to buffer %d", result);
  403. return result;
  404. }
  405. }
  406. while(!Curl_bufq_is_empty(&ctx->buf)) {
  407. struct ws_cw_dec_ctx pass_ctx;
  408. pass_ctx.data = data;
  409. pass_ctx.ws = ws;
  410. pass_ctx.next_writer = writer->next;
  411. pass_ctx.cw_type = type;
  412. result = ws_dec_pass(&ws->dec, data, &ctx->buf,
  413. ws_cw_dec_next, &pass_ctx);
  414. if(result == CURLE_AGAIN) {
  415. /* insufficient amount of data, keep it for later.
  416. * we pretend to have written all since we have a copy */
  417. CURL_TRC_WRITE(data, "websocket, buffered incomplete frame head");
  418. return CURLE_OK;
  419. }
  420. else if(result) {
  421. infof(data, "WS: decode error %d", (int)result);
  422. return result;
  423. }
  424. }
  425. if((type & CLIENTWRITE_EOS) && !Curl_bufq_is_empty(&ctx->buf)) {
  426. infof(data, "WS: decode ending with %zd frame bytes remaining",
  427. Curl_bufq_len(&ctx->buf));
  428. return CURLE_RECV_ERROR;
  429. }
  430. return CURLE_OK;
  431. }
  432. /* WebSocket payload decoding client writer. */
  433. static const struct Curl_cwtype ws_cw_decode = {
  434. "ws-decode",
  435. NULL,
  436. ws_cw_init,
  437. ws_cw_write,
  438. ws_cw_close,
  439. sizeof(struct ws_cw_ctx)
  440. };
  441. static void ws_enc_info(struct ws_encoder *enc, struct Curl_easy *data,
  442. const char *msg)
  443. {
  444. infof(data, "WS-ENC: %s [%s%s%s payload=%" CURL_FORMAT_CURL_OFF_T
  445. "/%" CURL_FORMAT_CURL_OFF_T "]",
  446. msg, ws_frame_name_of_op(enc->firstbyte),
  447. (enc->firstbyte & WSBIT_OPCODE_MASK) == WSBIT_OPCODE_CONT ?
  448. " CONT" : "",
  449. (enc->firstbyte & WSBIT_FIN)? "" : " NON-FIN",
  450. enc->payload_len - enc->payload_remain, enc->payload_len);
  451. }
  452. static void ws_enc_reset(struct ws_encoder *enc)
  453. {
  454. enc->payload_remain = 0;
  455. enc->xori = 0;
  456. enc->contfragment = FALSE;
  457. }
  458. static void ws_enc_init(struct ws_encoder *enc)
  459. {
  460. ws_enc_reset(enc);
  461. }
  462. /***
  463. RFC 6455 Section 5.2
  464. 0 1 2 3
  465. 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
  466. +-+-+-+-+-------+-+-------------+-------------------------------+
  467. |F|R|R|R| opcode|M| Payload len | Extended payload length |
  468. |I|S|S|S| (4) |A| (7) | (16/64) |
  469. |N|V|V|V| |S| | (if payload len==126/127) |
  470. | |1|2|3| |K| | |
  471. +-+-+-+-+-------+-+-------------+ - - - - - - - - - - - - - - - +
  472. | Extended payload length continued, if payload len == 127 |
  473. + - - - - - - - - - - - - - - - +-------------------------------+
  474. | |Masking-key, if MASK set to 1 |
  475. +-------------------------------+-------------------------------+
  476. | Masking-key (continued) | Payload Data |
  477. +-------------------------------- - - - - - - - - - - - - - - - +
  478. : Payload Data continued ... :
  479. + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - +
  480. | Payload Data continued ... |
  481. +---------------------------------------------------------------+
  482. */
  483. static ssize_t ws_enc_write_head(struct Curl_easy *data,
  484. struct ws_encoder *enc,
  485. unsigned int flags,
  486. curl_off_t payload_len,
  487. struct bufq *out,
  488. CURLcode *err)
  489. {
  490. unsigned char firstbyte = 0;
  491. unsigned char opcode;
  492. unsigned char head[14];
  493. size_t hlen;
  494. ssize_t n;
  495. if(payload_len < 0) {
  496. failf(data, "WS: starting new frame with negative payload length %"
  497. CURL_FORMAT_CURL_OFF_T, payload_len);
  498. *err = CURLE_SEND_ERROR;
  499. return -1;
  500. }
  501. if(enc->payload_remain > 0) {
  502. /* trying to write a new frame before the previous one is finished */
  503. failf(data, "WS: starting new frame with %zd bytes from last one"
  504. "remaining to be sent", (ssize_t)enc->payload_remain);
  505. *err = CURLE_SEND_ERROR;
  506. return -1;
  507. }
  508. opcode = ws_frame_flags2op(flags);
  509. if(!opcode) {
  510. failf(data, "WS: provided flags not recognized '%x'", flags);
  511. *err = CURLE_SEND_ERROR;
  512. return -1;
  513. }
  514. if(!(flags & CURLWS_CONT)) {
  515. if(!enc->contfragment)
  516. /* not marked as continuing, this is the final fragment */
  517. firstbyte |= WSBIT_FIN | opcode;
  518. else
  519. /* marked as continuing, this is the final fragment; set CONT
  520. opcode and FIN bit */
  521. firstbyte |= WSBIT_FIN | WSBIT_OPCODE_CONT;
  522. enc->contfragment = FALSE;
  523. }
  524. else if(enc->contfragment) {
  525. /* the previous fragment was not a final one and this isn't either, keep a
  526. CONT opcode and no FIN bit */
  527. firstbyte |= WSBIT_OPCODE_CONT;
  528. }
  529. else {
  530. firstbyte = opcode;
  531. enc->contfragment = TRUE;
  532. }
  533. head[0] = enc->firstbyte = firstbyte;
  534. if(payload_len > 65535) {
  535. head[1] = 127 | WSBIT_MASK;
  536. head[2] = (unsigned char)((payload_len >> 56) & 0xff);
  537. head[3] = (unsigned char)((payload_len >> 48) & 0xff);
  538. head[4] = (unsigned char)((payload_len >> 40) & 0xff);
  539. head[5] = (unsigned char)((payload_len >> 32) & 0xff);
  540. head[6] = (unsigned char)((payload_len >> 24) & 0xff);
  541. head[7] = (unsigned char)((payload_len >> 16) & 0xff);
  542. head[8] = (unsigned char)((payload_len >> 8) & 0xff);
  543. head[9] = (unsigned char)(payload_len & 0xff);
  544. hlen = 10;
  545. }
  546. else if(payload_len >= 126) {
  547. head[1] = 126 | WSBIT_MASK;
  548. head[2] = (unsigned char)((payload_len >> 8) & 0xff);
  549. head[3] = (unsigned char)(payload_len & 0xff);
  550. hlen = 4;
  551. }
  552. else {
  553. head[1] = (unsigned char)payload_len | WSBIT_MASK;
  554. hlen = 2;
  555. }
  556. enc->payload_remain = enc->payload_len = payload_len;
  557. ws_enc_info(enc, data, "sending");
  558. /* add 4 bytes mask */
  559. memcpy(&head[hlen], &enc->mask, 4);
  560. hlen += 4;
  561. /* reset for payload to come */
  562. enc->xori = 0;
  563. n = Curl_bufq_write(out, head, hlen, err);
  564. if(n < 0)
  565. return -1;
  566. if((size_t)n != hlen) {
  567. /* We use a bufq with SOFT_LIMIT, writing should always succeed */
  568. DEBUGASSERT(0);
  569. *err = CURLE_SEND_ERROR;
  570. return -1;
  571. }
  572. return n;
  573. }
  574. static ssize_t ws_enc_write_payload(struct ws_encoder *enc,
  575. struct Curl_easy *data,
  576. const unsigned char *buf, size_t buflen,
  577. struct bufq *out, CURLcode *err)
  578. {
  579. ssize_t n;
  580. size_t i, len;
  581. if(Curl_bufq_is_full(out)) {
  582. *err = CURLE_AGAIN;
  583. return -1;
  584. }
  585. /* not the most performant way to do this */
  586. len = buflen;
  587. if((curl_off_t)len > enc->payload_remain)
  588. len = (size_t)enc->payload_remain;
  589. for(i = 0; i < len; ++i) {
  590. unsigned char c = buf[i] ^ enc->mask[enc->xori];
  591. n = Curl_bufq_write(out, &c, 1, err);
  592. if(n < 0) {
  593. if((*err != CURLE_AGAIN) || !i)
  594. return -1;
  595. break;
  596. }
  597. enc->xori++;
  598. enc->xori &= 3;
  599. }
  600. enc->payload_remain -= (curl_off_t)i;
  601. ws_enc_info(enc, data, "buffered");
  602. return (ssize_t)i;
  603. }
  604. struct wsfield {
  605. const char *name;
  606. const char *val;
  607. };
  608. CURLcode Curl_ws_request(struct Curl_easy *data, REQTYPE *req)
  609. {
  610. unsigned int i;
  611. CURLcode result = CURLE_OK;
  612. unsigned char rand[16];
  613. char *randstr;
  614. size_t randlen;
  615. char keyval[40];
  616. struct SingleRequest *k = &data->req;
  617. struct wsfield heads[]= {
  618. {
  619. /* The request MUST contain an |Upgrade| header field whose value
  620. MUST include the "websocket" keyword. */
  621. "Upgrade:", "websocket"
  622. },
  623. {
  624. /* The request MUST contain a |Connection| header field whose value
  625. MUST include the "Upgrade" token. */
  626. "Connection:", "Upgrade",
  627. },
  628. {
  629. /* The request MUST include a header field with the name
  630. |Sec-WebSocket-Version|. The value of this header field MUST be
  631. 13. */
  632. "Sec-WebSocket-Version:", "13",
  633. },
  634. {
  635. /* The request MUST include a header field with the name
  636. |Sec-WebSocket-Key|. The value of this header field MUST be a nonce
  637. consisting of a randomly selected 16-byte value that has been
  638. base64-encoded (see Section 4 of [RFC4648]). The nonce MUST be
  639. selected randomly for each connection. */
  640. "Sec-WebSocket-Key:", NULL,
  641. }
  642. };
  643. heads[3].val = &keyval[0];
  644. /* 16 bytes random */
  645. result = Curl_rand(data, (unsigned char *)rand, sizeof(rand));
  646. if(result)
  647. return result;
  648. result = Curl_base64_encode((char *)rand, sizeof(rand), &randstr, &randlen);
  649. if(result)
  650. return result;
  651. DEBUGASSERT(randlen < sizeof(keyval));
  652. if(randlen >= sizeof(keyval))
  653. return CURLE_FAILED_INIT;
  654. strcpy(keyval, randstr);
  655. free(randstr);
  656. for(i = 0; !result && (i < sizeof(heads)/sizeof(heads[0])); i++) {
  657. if(!Curl_checkheaders(data, STRCONST(heads[i].name))) {
  658. #ifdef USE_HYPER
  659. char field[128];
  660. msnprintf(field, sizeof(field), "%s %s", heads[i].name,
  661. heads[i].val);
  662. result = Curl_hyper_header(data, req, field);
  663. #else
  664. (void)data;
  665. result = Curl_dyn_addf(req, "%s %s\r\n", heads[i].name,
  666. heads[i].val);
  667. #endif
  668. }
  669. }
  670. k->upgr101 = UPGR101_WS;
  671. return result;
  672. }
  673. /*
  674. * 'nread' is number of bytes of websocket data already in the buffer at
  675. * 'mem'.
  676. */
  677. CURLcode Curl_ws_accept(struct Curl_easy *data,
  678. const char *mem, size_t nread)
  679. {
  680. struct SingleRequest *k = &data->req;
  681. struct websocket *ws;
  682. struct Curl_cwriter *ws_dec_writer;
  683. CURLcode result;
  684. DEBUGASSERT(data->conn);
  685. ws = data->conn->proto.ws;
  686. if(!ws) {
  687. size_t chunk_size = WS_CHUNK_SIZE;
  688. ws = calloc(1, sizeof(*ws));
  689. if(!ws)
  690. return CURLE_OUT_OF_MEMORY;
  691. data->conn->proto.ws = ws;
  692. #ifdef DEBUGBUILD
  693. {
  694. char *p = getenv("CURL_WS_CHUNK_SIZE");
  695. if(p) {
  696. long l = strtol(p, NULL, 10);
  697. if(l > 0 && l <= (1*1024*1024)) {
  698. chunk_size = (size_t)l;
  699. }
  700. }
  701. }
  702. #endif
  703. DEBUGF(infof(data, "WS, using chunk size %zu", chunk_size));
  704. Curl_bufq_init2(&ws->recvbuf, chunk_size, WS_CHUNK_COUNT,
  705. BUFQ_OPT_SOFT_LIMIT);
  706. Curl_bufq_init2(&ws->sendbuf, chunk_size, WS_CHUNK_COUNT,
  707. BUFQ_OPT_SOFT_LIMIT);
  708. ws_dec_init(&ws->dec);
  709. ws_enc_init(&ws->enc);
  710. }
  711. else {
  712. Curl_bufq_reset(&ws->recvbuf);
  713. ws_dec_reset(&ws->dec);
  714. ws_enc_reset(&ws->enc);
  715. }
  716. /* Verify the Sec-WebSocket-Accept response.
  717. The sent value is the base64 encoded version of a SHA-1 hash done on the
  718. |Sec-WebSocket-Key| header field concatenated with
  719. the string "258EAFA5-E914-47DA-95CA-C5AB0DC85B11".
  720. */
  721. /* If the response includes a |Sec-WebSocket-Extensions| header field and
  722. this header field indicates the use of an extension that was not present
  723. in the client's handshake (the server has indicated an extension not
  724. requested by the client), the client MUST Fail the WebSocket Connection.
  725. */
  726. /* If the response includes a |Sec-WebSocket-Protocol| header field
  727. and this header field indicates the use of a subprotocol that was
  728. not present in the client's handshake (the server has indicated a
  729. subprotocol not requested by the client), the client MUST Fail
  730. the WebSocket Connection. */
  731. /* 4 bytes random */
  732. result = Curl_rand(data, (unsigned char *)&ws->enc.mask,
  733. sizeof(ws->enc.mask));
  734. if(result)
  735. return result;
  736. infof(data, "Received 101, switch to WebSocket; mask %02x%02x%02x%02x",
  737. ws->enc.mask[0], ws->enc.mask[1], ws->enc.mask[2], ws->enc.mask[3]);
  738. /* Install our client writer that decodes WS frames payload */
  739. result = Curl_cwriter_create(&ws_dec_writer, data, &ws_cw_decode,
  740. CURL_CW_CONTENT_DECODE);
  741. if(result)
  742. return result;
  743. result = Curl_cwriter_add(data, ws_dec_writer);
  744. if(result) {
  745. Curl_cwriter_free(data, ws_dec_writer);
  746. return result;
  747. }
  748. if(data->set.connect_only) {
  749. ssize_t nwritten;
  750. /* In CONNECT_ONLY setup, the payloads from `mem` need to be received
  751. * when using `curl_ws_recv` later on after this transfer is already
  752. * marked as DONE. */
  753. nwritten = Curl_bufq_write(&ws->recvbuf, (const unsigned char *)mem,
  754. nread, &result);
  755. if(nwritten < 0)
  756. return result;
  757. infof(data, "%zu bytes websocket payload", nread);
  758. }
  759. else { /* !connect_only */
  760. /* And pass any additional data to the writers */
  761. if(nread) {
  762. result = Curl_client_write(data, CLIENTWRITE_BODY, (char *)mem, nread);
  763. }
  764. }
  765. k->upgr101 = UPGR101_RECEIVED;
  766. return result;
  767. }
  768. struct ws_collect {
  769. struct Curl_easy *data;
  770. unsigned char *buffer;
  771. size_t buflen;
  772. size_t bufidx;
  773. int frame_age;
  774. int frame_flags;
  775. curl_off_t payload_offset;
  776. curl_off_t payload_len;
  777. bool written;
  778. };
  779. static ssize_t ws_client_collect(const unsigned char *buf, size_t buflen,
  780. int frame_age, int frame_flags,
  781. curl_off_t payload_offset,
  782. curl_off_t payload_len,
  783. void *userp,
  784. CURLcode *err)
  785. {
  786. struct ws_collect *ctx = userp;
  787. size_t nwritten;
  788. curl_off_t remain = (payload_len - (payload_offset + buflen));
  789. if(!ctx->bufidx) {
  790. /* first write */
  791. ctx->frame_age = frame_age;
  792. ctx->frame_flags = frame_flags;
  793. ctx->payload_offset = payload_offset;
  794. ctx->payload_len = payload_len;
  795. }
  796. if((frame_flags & CURLWS_PING) && !remain) {
  797. /* auto-respond to PINGs, only works for single-frame payloads atm */
  798. size_t bytes;
  799. infof(ctx->data, "WS: auto-respond to PING with a PONG");
  800. /* send back the exact same content as a PONG */
  801. *err = curl_ws_send(ctx->data, buf, buflen, &bytes, 0, CURLWS_PONG);
  802. if(*err)
  803. return -1;
  804. nwritten = bytes;
  805. }
  806. else {
  807. ctx->written = TRUE;
  808. DEBUGASSERT(ctx->buflen >= ctx->bufidx);
  809. nwritten = CURLMIN(buflen, ctx->buflen - ctx->bufidx);
  810. if(!nwritten) {
  811. if(!buflen) { /* 0 length write, we accept that */
  812. *err = CURLE_OK;
  813. return 0;
  814. }
  815. *err = CURLE_AGAIN; /* no more space */
  816. return -1;
  817. }
  818. *err = CURLE_OK;
  819. memcpy(ctx->buffer + ctx->bufidx, buf, nwritten);
  820. ctx->bufidx += nwritten;
  821. }
  822. return nwritten;
  823. }
  824. static ssize_t nw_in_recv(void *reader_ctx,
  825. unsigned char *buf, size_t buflen,
  826. CURLcode *err)
  827. {
  828. struct Curl_easy *data = reader_ctx;
  829. size_t nread;
  830. *err = curl_easy_recv(data, buf, buflen, &nread);
  831. if(*err)
  832. return -1;
  833. return (ssize_t)nread;
  834. }
  835. CURL_EXTERN CURLcode curl_ws_recv(struct Curl_easy *data, void *buffer,
  836. size_t buflen, size_t *nread,
  837. const struct curl_ws_frame **metap)
  838. {
  839. struct connectdata *conn = data->conn;
  840. struct websocket *ws;
  841. bool done = FALSE; /* not filled passed buffer yet */
  842. struct ws_collect ctx;
  843. CURLcode result;
  844. if(!conn) {
  845. /* Unhappy hack with lifetimes of transfers and connection */
  846. if(!data->set.connect_only) {
  847. failf(data, "CONNECT_ONLY is required");
  848. return CURLE_UNSUPPORTED_PROTOCOL;
  849. }
  850. Curl_getconnectinfo(data, &conn);
  851. if(!conn) {
  852. failf(data, "connection not found");
  853. return CURLE_BAD_FUNCTION_ARGUMENT;
  854. }
  855. }
  856. ws = conn->proto.ws;
  857. if(!ws) {
  858. failf(data, "connection is not setup for websocket");
  859. return CURLE_BAD_FUNCTION_ARGUMENT;
  860. }
  861. *nread = 0;
  862. *metap = NULL;
  863. memset(&ctx, 0, sizeof(ctx));
  864. ctx.data = data;
  865. ctx.buffer = buffer;
  866. ctx.buflen = buflen;
  867. while(!done) {
  868. /* receive more when our buffer is empty */
  869. if(Curl_bufq_is_empty(&ws->recvbuf)) {
  870. ssize_t n = Curl_bufq_slurp(&ws->recvbuf, nw_in_recv, data, &result);
  871. if(n < 0) {
  872. return result;
  873. }
  874. else if(n == 0) {
  875. /* connection closed */
  876. infof(data, "connection expectedly closed?");
  877. return CURLE_GOT_NOTHING;
  878. }
  879. DEBUGF(infof(data, "curl_ws_recv, added %zu bytes from network",
  880. Curl_bufq_len(&ws->recvbuf)));
  881. }
  882. result = ws_dec_pass(&ws->dec, data, &ws->recvbuf,
  883. ws_client_collect, &ctx);
  884. if(result == CURLE_AGAIN) {
  885. if(!ctx.written) {
  886. ws_dec_info(&ws->dec, data, "need more input");
  887. continue; /* nothing written, try more input */
  888. }
  889. done = TRUE;
  890. break;
  891. }
  892. else if(result) {
  893. return result;
  894. }
  895. else if(ctx.written) {
  896. /* The decoded frame is passed back to our caller.
  897. * There are frames like PING were we auto-respond to and
  898. * that we do not return. For these `ctx.written` is not set. */
  899. done = TRUE;
  900. break;
  901. }
  902. }
  903. /* update frame information to be passed back */
  904. update_meta(ws, ctx.frame_age, ctx.frame_flags, ctx.payload_offset,
  905. ctx.payload_len, ctx.bufidx);
  906. *metap = &ws->frame;
  907. *nread = ws->frame.len;
  908. /* infof(data, "curl_ws_recv(len=%zu) -> %zu bytes (frame at %"
  909. CURL_FORMAT_CURL_OFF_T ", %" CURL_FORMAT_CURL_OFF_T " left)",
  910. buflen, *nread, ws->frame.offset, ws->frame.bytesleft); */
  911. return CURLE_OK;
  912. }
  913. static CURLcode ws_flush(struct Curl_easy *data, struct websocket *ws,
  914. bool complete)
  915. {
  916. if(!Curl_bufq_is_empty(&ws->sendbuf)) {
  917. CURLcode result;
  918. const unsigned char *out;
  919. size_t outlen, n;
  920. while(Curl_bufq_peek(&ws->sendbuf, &out, &outlen)) {
  921. if(data->set.connect_only)
  922. result = Curl_senddata(data, out, outlen, &n);
  923. else {
  924. result = Curl_xfer_send(data, out, outlen, &n);
  925. if(!result && !n && outlen)
  926. result = CURLE_AGAIN;
  927. }
  928. if(result) {
  929. if(result == CURLE_AGAIN) {
  930. if(!complete) {
  931. infof(data, "WS: flush EAGAIN, %zu bytes remain in buffer",
  932. Curl_bufq_len(&ws->sendbuf));
  933. return result;
  934. }
  935. /* TODO: the current design does not allow for buffered writes.
  936. * We need to flush the buffer now. There is no ws_flush() later */
  937. n = 0;
  938. continue;
  939. }
  940. else if(result) {
  941. failf(data, "WS: flush, write error %d", result);
  942. return result;
  943. }
  944. }
  945. else {
  946. infof(data, "WS: flushed %zu bytes", n);
  947. Curl_bufq_skip(&ws->sendbuf, n);
  948. }
  949. }
  950. }
  951. return CURLE_OK;
  952. }
  953. CURL_EXTERN CURLcode curl_ws_send(CURL *data, const void *buffer,
  954. size_t buflen, size_t *sent,
  955. curl_off_t fragsize,
  956. unsigned int flags)
  957. {
  958. struct websocket *ws;
  959. ssize_t n;
  960. size_t nwritten, space;
  961. CURLcode result;
  962. *sent = 0;
  963. if(!data->conn && data->set.connect_only) {
  964. result = Curl_connect_only_attach(data);
  965. if(result)
  966. return result;
  967. }
  968. if(!data->conn) {
  969. failf(data, "No associated connection");
  970. return CURLE_SEND_ERROR;
  971. }
  972. if(!data->conn->proto.ws) {
  973. failf(data, "Not a websocket transfer");
  974. return CURLE_SEND_ERROR;
  975. }
  976. ws = data->conn->proto.ws;
  977. if(data->set.ws_raw_mode) {
  978. if(fragsize || flags) {
  979. DEBUGF(infof(data, "ws_send: "
  980. "fragsize and flags cannot be non-zero in raw mode"));
  981. return CURLE_BAD_FUNCTION_ARGUMENT;
  982. }
  983. if(!buflen)
  984. /* nothing to do */
  985. return CURLE_OK;
  986. /* raw mode sends exactly what was requested, and this is from within
  987. the write callback */
  988. if(Curl_is_in_callback(data)) {
  989. result = Curl_xfer_send(data, buffer, buflen, &nwritten);
  990. }
  991. else
  992. result = Curl_senddata(data, buffer, buflen, &nwritten);
  993. infof(data, "WS: wanted to send %zu bytes, sent %zu bytes",
  994. buflen, nwritten);
  995. *sent = nwritten;
  996. return result;
  997. }
  998. /* Not RAW mode, buf we do the frame encoding */
  999. result = ws_flush(data, ws, FALSE);
  1000. if(result)
  1001. return result;
  1002. /* TODO: the current design does not allow partial writes, afaict.
  1003. * It is not clear how the application is supposed to react. */
  1004. space = Curl_bufq_space(&ws->sendbuf);
  1005. DEBUGF(infof(data, "curl_ws_send(len=%zu), sendbuf len=%zu space %zu",
  1006. buflen, Curl_bufq_len(&ws->sendbuf), space));
  1007. if(space < 14)
  1008. return CURLE_AGAIN;
  1009. if(flags & CURLWS_OFFSET) {
  1010. if(fragsize) {
  1011. /* a frame series 'fragsize' bytes big, this is the first */
  1012. n = ws_enc_write_head(data, &ws->enc, flags, fragsize,
  1013. &ws->sendbuf, &result);
  1014. if(n < 0)
  1015. return result;
  1016. }
  1017. else {
  1018. if((curl_off_t)buflen > ws->enc.payload_remain) {
  1019. infof(data, "WS: unaligned frame size (sending %zu instead of %"
  1020. CURL_FORMAT_CURL_OFF_T ")",
  1021. buflen, ws->enc.payload_remain);
  1022. }
  1023. }
  1024. }
  1025. else if(!ws->enc.payload_remain) {
  1026. n = ws_enc_write_head(data, &ws->enc, flags, (curl_off_t)buflen,
  1027. &ws->sendbuf, &result);
  1028. if(n < 0)
  1029. return result;
  1030. }
  1031. n = ws_enc_write_payload(&ws->enc, data,
  1032. buffer, buflen, &ws->sendbuf, &result);
  1033. if(n < 0)
  1034. return result;
  1035. *sent = (size_t)n;
  1036. return ws_flush(data, ws, TRUE);
  1037. }
  1038. static void ws_free(struct connectdata *conn)
  1039. {
  1040. if(conn && conn->proto.ws) {
  1041. Curl_bufq_free(&conn->proto.ws->recvbuf);
  1042. Curl_bufq_free(&conn->proto.ws->sendbuf);
  1043. Curl_safefree(conn->proto.ws);
  1044. }
  1045. }
  1046. static CURLcode ws_setup_conn(struct Curl_easy *data,
  1047. struct connectdata *conn)
  1048. {
  1049. /* websockets is 1.1 only (for now) */
  1050. data->state.httpwant = CURL_HTTP_VERSION_1_1;
  1051. return Curl_http_setup_conn(data, conn);
  1052. }
  1053. static CURLcode ws_disconnect(struct Curl_easy *data,
  1054. struct connectdata *conn,
  1055. bool dead_connection)
  1056. {
  1057. (void)data;
  1058. (void)dead_connection;
  1059. ws_free(conn);
  1060. return CURLE_OK;
  1061. }
  1062. CURL_EXTERN const struct curl_ws_frame *curl_ws_meta(struct Curl_easy *data)
  1063. {
  1064. /* we only return something for websocket, called from within the callback
  1065. when not using raw mode */
  1066. if(GOOD_EASY_HANDLE(data) && Curl_is_in_callback(data) && data->conn &&
  1067. data->conn->proto.ws && !data->set.ws_raw_mode)
  1068. return &data->conn->proto.ws->frame;
  1069. return NULL;
  1070. }
  1071. const struct Curl_handler Curl_handler_ws = {
  1072. "WS", /* scheme */
  1073. ws_setup_conn, /* setup_connection */
  1074. Curl_http, /* do_it */
  1075. Curl_http_done, /* done */
  1076. ZERO_NULL, /* do_more */
  1077. Curl_http_connect, /* connect_it */
  1078. ZERO_NULL, /* connecting */
  1079. ZERO_NULL, /* doing */
  1080. ZERO_NULL, /* proto_getsock */
  1081. Curl_http_getsock_do, /* doing_getsock */
  1082. ZERO_NULL, /* domore_getsock */
  1083. ZERO_NULL, /* perform_getsock */
  1084. ws_disconnect, /* disconnect */
  1085. Curl_http_write_resp, /* write_resp */
  1086. Curl_http_write_resp_hd, /* write_resp_hd */
  1087. ZERO_NULL, /* connection_check */
  1088. ZERO_NULL, /* attach connection */
  1089. PORT_HTTP, /* defport */
  1090. CURLPROTO_WS, /* protocol */
  1091. CURLPROTO_HTTP, /* family */
  1092. PROTOPT_CREDSPERREQUEST | /* flags */
  1093. PROTOPT_USERPWDCTRL
  1094. };
  1095. #ifdef USE_SSL
  1096. const struct Curl_handler Curl_handler_wss = {
  1097. "WSS", /* scheme */
  1098. ws_setup_conn, /* setup_connection */
  1099. Curl_http, /* do_it */
  1100. Curl_http_done, /* done */
  1101. ZERO_NULL, /* do_more */
  1102. Curl_http_connect, /* connect_it */
  1103. NULL, /* connecting */
  1104. ZERO_NULL, /* doing */
  1105. NULL, /* proto_getsock */
  1106. Curl_http_getsock_do, /* doing_getsock */
  1107. ZERO_NULL, /* domore_getsock */
  1108. ZERO_NULL, /* perform_getsock */
  1109. ws_disconnect, /* disconnect */
  1110. Curl_http_write_resp, /* write_resp */
  1111. Curl_http_write_resp_hd, /* write_resp_hd */
  1112. ZERO_NULL, /* connection_check */
  1113. ZERO_NULL, /* attach connection */
  1114. PORT_HTTPS, /* defport */
  1115. CURLPROTO_WSS, /* protocol */
  1116. CURLPROTO_HTTP, /* family */
  1117. PROTOPT_SSL | PROTOPT_CREDSPERREQUEST | /* flags */
  1118. PROTOPT_USERPWDCTRL
  1119. };
  1120. #endif
  1121. #else
  1122. CURL_EXTERN CURLcode curl_ws_recv(CURL *curl, void *buffer, size_t buflen,
  1123. size_t *nread,
  1124. const struct curl_ws_frame **metap)
  1125. {
  1126. (void)curl;
  1127. (void)buffer;
  1128. (void)buflen;
  1129. (void)nread;
  1130. (void)metap;
  1131. return CURLE_NOT_BUILT_IN;
  1132. }
  1133. CURL_EXTERN CURLcode curl_ws_send(CURL *curl, const void *buffer,
  1134. size_t buflen, size_t *sent,
  1135. curl_off_t fragsize,
  1136. unsigned int flags)
  1137. {
  1138. (void)curl;
  1139. (void)buffer;
  1140. (void)buflen;
  1141. (void)sent;
  1142. (void)fragsize;
  1143. (void)flags;
  1144. return CURLE_NOT_BUILT_IN;
  1145. }
  1146. CURL_EXTERN const struct curl_ws_frame *curl_ws_meta(struct Curl_easy *data)
  1147. {
  1148. (void)data;
  1149. return NULL;
  1150. }
  1151. #endif /* USE_WEBSOCKETS */