2
0

ws.c 38 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271
  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. free(randstr);
  654. return CURLE_FAILED_INIT;
  655. }
  656. strcpy(keyval, randstr);
  657. free(randstr);
  658. for(i = 0; !result && (i < sizeof(heads)/sizeof(heads[0])); i++) {
  659. if(!Curl_checkheaders(data, STRCONST(heads[i].name))) {
  660. #ifdef USE_HYPER
  661. char field[128];
  662. msnprintf(field, sizeof(field), "%s %s", heads[i].name,
  663. heads[i].val);
  664. result = Curl_hyper_header(data, req, field);
  665. #else
  666. (void)data;
  667. result = Curl_dyn_addf(req, "%s %s\r\n", heads[i].name,
  668. heads[i].val);
  669. #endif
  670. }
  671. }
  672. k->upgr101 = UPGR101_WS;
  673. return result;
  674. }
  675. /*
  676. * 'nread' is number of bytes of websocket data already in the buffer at
  677. * 'mem'.
  678. */
  679. CURLcode Curl_ws_accept(struct Curl_easy *data,
  680. const char *mem, size_t nread)
  681. {
  682. struct SingleRequest *k = &data->req;
  683. struct websocket *ws;
  684. struct Curl_cwriter *ws_dec_writer;
  685. CURLcode result;
  686. DEBUGASSERT(data->conn);
  687. ws = data->conn->proto.ws;
  688. if(!ws) {
  689. size_t chunk_size = WS_CHUNK_SIZE;
  690. ws = calloc(1, sizeof(*ws));
  691. if(!ws)
  692. return CURLE_OUT_OF_MEMORY;
  693. data->conn->proto.ws = ws;
  694. #ifdef DEBUGBUILD
  695. {
  696. char *p = getenv("CURL_WS_CHUNK_SIZE");
  697. if(p) {
  698. long l = strtol(p, NULL, 10);
  699. if(l > 0 && l <= (1*1024*1024)) {
  700. chunk_size = (size_t)l;
  701. }
  702. }
  703. }
  704. #endif
  705. DEBUGF(infof(data, "WS, using chunk size %zu", chunk_size));
  706. Curl_bufq_init2(&ws->recvbuf, chunk_size, WS_CHUNK_COUNT,
  707. BUFQ_OPT_SOFT_LIMIT);
  708. Curl_bufq_init2(&ws->sendbuf, chunk_size, WS_CHUNK_COUNT,
  709. BUFQ_OPT_SOFT_LIMIT);
  710. ws_dec_init(&ws->dec);
  711. ws_enc_init(&ws->enc);
  712. }
  713. else {
  714. Curl_bufq_reset(&ws->recvbuf);
  715. ws_dec_reset(&ws->dec);
  716. ws_enc_reset(&ws->enc);
  717. }
  718. /* Verify the Sec-WebSocket-Accept response.
  719. The sent value is the base64 encoded version of a SHA-1 hash done on the
  720. |Sec-WebSocket-Key| header field concatenated with
  721. the string "258EAFA5-E914-47DA-95CA-C5AB0DC85B11".
  722. */
  723. /* If the response includes a |Sec-WebSocket-Extensions| header field and
  724. this header field indicates the use of an extension that was not present
  725. in the client's handshake (the server has indicated an extension not
  726. requested by the client), the client MUST Fail the WebSocket Connection.
  727. */
  728. /* If the response includes a |Sec-WebSocket-Protocol| header field
  729. and this header field indicates the use of a subprotocol that was
  730. not present in the client's handshake (the server has indicated a
  731. subprotocol not requested by the client), the client MUST Fail
  732. the WebSocket Connection. */
  733. /* 4 bytes random */
  734. result = Curl_rand(data, (unsigned char *)&ws->enc.mask,
  735. sizeof(ws->enc.mask));
  736. if(result)
  737. return result;
  738. infof(data, "Received 101, switch to WebSocket; mask %02x%02x%02x%02x",
  739. ws->enc.mask[0], ws->enc.mask[1], ws->enc.mask[2], ws->enc.mask[3]);
  740. /* Install our client writer that decodes WS frames payload */
  741. result = Curl_cwriter_create(&ws_dec_writer, data, &ws_cw_decode,
  742. CURL_CW_CONTENT_DECODE);
  743. if(result)
  744. return result;
  745. result = Curl_cwriter_add(data, ws_dec_writer);
  746. if(result) {
  747. Curl_cwriter_free(data, ws_dec_writer);
  748. return result;
  749. }
  750. if(data->set.connect_only) {
  751. ssize_t nwritten;
  752. /* In CONNECT_ONLY setup, the payloads from `mem` need to be received
  753. * when using `curl_ws_recv` later on after this transfer is already
  754. * marked as DONE. */
  755. nwritten = Curl_bufq_write(&ws->recvbuf, (const unsigned char *)mem,
  756. nread, &result);
  757. if(nwritten < 0)
  758. return result;
  759. infof(data, "%zu bytes websocket payload", nread);
  760. }
  761. else { /* !connect_only */
  762. /* And pass any additional data to the writers */
  763. if(nread) {
  764. result = Curl_client_write(data, CLIENTWRITE_BODY, (char *)mem, nread);
  765. }
  766. }
  767. k->upgr101 = UPGR101_RECEIVED;
  768. return result;
  769. }
  770. struct ws_collect {
  771. struct Curl_easy *data;
  772. unsigned char *buffer;
  773. size_t buflen;
  774. size_t bufidx;
  775. int frame_age;
  776. int frame_flags;
  777. curl_off_t payload_offset;
  778. curl_off_t payload_len;
  779. bool written;
  780. };
  781. static ssize_t ws_client_collect(const unsigned char *buf, size_t buflen,
  782. int frame_age, int frame_flags,
  783. curl_off_t payload_offset,
  784. curl_off_t payload_len,
  785. void *userp,
  786. CURLcode *err)
  787. {
  788. struct ws_collect *ctx = userp;
  789. size_t nwritten;
  790. curl_off_t remain = (payload_len - (payload_offset + buflen));
  791. if(!ctx->bufidx) {
  792. /* first write */
  793. ctx->frame_age = frame_age;
  794. ctx->frame_flags = frame_flags;
  795. ctx->payload_offset = payload_offset;
  796. ctx->payload_len = payload_len;
  797. }
  798. if((frame_flags & CURLWS_PING) && !remain) {
  799. /* auto-respond to PINGs, only works for single-frame payloads atm */
  800. size_t bytes;
  801. infof(ctx->data, "WS: auto-respond to PING with a PONG");
  802. /* send back the exact same content as a PONG */
  803. *err = curl_ws_send(ctx->data, buf, buflen, &bytes, 0, CURLWS_PONG);
  804. if(*err)
  805. return -1;
  806. nwritten = bytes;
  807. }
  808. else {
  809. ctx->written = TRUE;
  810. DEBUGASSERT(ctx->buflen >= ctx->bufidx);
  811. nwritten = CURLMIN(buflen, ctx->buflen - ctx->bufidx);
  812. if(!nwritten) {
  813. if(!buflen) { /* 0 length write, we accept that */
  814. *err = CURLE_OK;
  815. return 0;
  816. }
  817. *err = CURLE_AGAIN; /* no more space */
  818. return -1;
  819. }
  820. *err = CURLE_OK;
  821. memcpy(ctx->buffer + ctx->bufidx, buf, nwritten);
  822. ctx->bufidx += nwritten;
  823. }
  824. return nwritten;
  825. }
  826. static ssize_t nw_in_recv(void *reader_ctx,
  827. unsigned char *buf, size_t buflen,
  828. CURLcode *err)
  829. {
  830. struct Curl_easy *data = reader_ctx;
  831. size_t nread;
  832. *err = curl_easy_recv(data, buf, buflen, &nread);
  833. if(*err)
  834. return -1;
  835. return (ssize_t)nread;
  836. }
  837. CURL_EXTERN CURLcode curl_ws_recv(struct Curl_easy *data, void *buffer,
  838. size_t buflen, size_t *nread,
  839. const struct curl_ws_frame **metap)
  840. {
  841. struct connectdata *conn = data->conn;
  842. struct websocket *ws;
  843. bool done = FALSE; /* not filled passed buffer yet */
  844. struct ws_collect ctx;
  845. CURLcode result;
  846. if(!conn) {
  847. /* Unhappy hack with lifetimes of transfers and connection */
  848. if(!data->set.connect_only) {
  849. failf(data, "CONNECT_ONLY is required");
  850. return CURLE_UNSUPPORTED_PROTOCOL;
  851. }
  852. Curl_getconnectinfo(data, &conn);
  853. if(!conn) {
  854. failf(data, "connection not found");
  855. return CURLE_BAD_FUNCTION_ARGUMENT;
  856. }
  857. }
  858. ws = conn->proto.ws;
  859. if(!ws) {
  860. failf(data, "connection is not setup for websocket");
  861. return CURLE_BAD_FUNCTION_ARGUMENT;
  862. }
  863. *nread = 0;
  864. *metap = NULL;
  865. memset(&ctx, 0, sizeof(ctx));
  866. ctx.data = data;
  867. ctx.buffer = buffer;
  868. ctx.buflen = buflen;
  869. while(!done) {
  870. /* receive more when our buffer is empty */
  871. if(Curl_bufq_is_empty(&ws->recvbuf)) {
  872. ssize_t n = Curl_bufq_slurp(&ws->recvbuf, nw_in_recv, data, &result);
  873. if(n < 0) {
  874. return result;
  875. }
  876. else if(n == 0) {
  877. /* connection closed */
  878. infof(data, "connection expectedly closed?");
  879. return CURLE_GOT_NOTHING;
  880. }
  881. DEBUGF(infof(data, "curl_ws_recv, added %zu bytes from network",
  882. Curl_bufq_len(&ws->recvbuf)));
  883. }
  884. result = ws_dec_pass(&ws->dec, data, &ws->recvbuf,
  885. ws_client_collect, &ctx);
  886. if(result == CURLE_AGAIN) {
  887. if(!ctx.written) {
  888. ws_dec_info(&ws->dec, data, "need more input");
  889. continue; /* nothing written, try more input */
  890. }
  891. done = TRUE;
  892. break;
  893. }
  894. else if(result) {
  895. return result;
  896. }
  897. else if(ctx.written) {
  898. /* The decoded frame is passed back to our caller.
  899. * There are frames like PING were we auto-respond to and
  900. * that we do not return. For these `ctx.written` is not set. */
  901. done = TRUE;
  902. break;
  903. }
  904. }
  905. /* update frame information to be passed back */
  906. update_meta(ws, ctx.frame_age, ctx.frame_flags, ctx.payload_offset,
  907. ctx.payload_len, ctx.bufidx);
  908. *metap = &ws->frame;
  909. *nread = ws->frame.len;
  910. /* infof(data, "curl_ws_recv(len=%zu) -> %zu bytes (frame at %"
  911. CURL_FORMAT_CURL_OFF_T ", %" CURL_FORMAT_CURL_OFF_T " left)",
  912. buflen, *nread, ws->frame.offset, ws->frame.bytesleft); */
  913. return CURLE_OK;
  914. }
  915. static CURLcode ws_flush(struct Curl_easy *data, struct websocket *ws,
  916. bool complete)
  917. {
  918. if(!Curl_bufq_is_empty(&ws->sendbuf)) {
  919. CURLcode result;
  920. const unsigned char *out;
  921. size_t outlen, n;
  922. while(Curl_bufq_peek(&ws->sendbuf, &out, &outlen)) {
  923. if(data->set.connect_only)
  924. result = Curl_senddata(data, out, outlen, &n);
  925. else {
  926. result = Curl_xfer_send(data, out, outlen, &n);
  927. if(!result && !n && outlen)
  928. result = CURLE_AGAIN;
  929. }
  930. if(result) {
  931. if(result == CURLE_AGAIN) {
  932. if(!complete) {
  933. infof(data, "WS: flush EAGAIN, %zu bytes remain in buffer",
  934. Curl_bufq_len(&ws->sendbuf));
  935. return result;
  936. }
  937. /* TODO: the current design does not allow for buffered writes.
  938. * We need to flush the buffer now. There is no ws_flush() later */
  939. n = 0;
  940. continue;
  941. }
  942. else if(result) {
  943. failf(data, "WS: flush, write error %d", result);
  944. return result;
  945. }
  946. }
  947. else {
  948. infof(data, "WS: flushed %zu bytes", n);
  949. Curl_bufq_skip(&ws->sendbuf, n);
  950. }
  951. }
  952. }
  953. return CURLE_OK;
  954. }
  955. CURL_EXTERN CURLcode curl_ws_send(CURL *data, const void *buffer,
  956. size_t buflen, size_t *sent,
  957. curl_off_t fragsize,
  958. unsigned int flags)
  959. {
  960. struct websocket *ws;
  961. ssize_t n;
  962. size_t nwritten, space;
  963. CURLcode result;
  964. *sent = 0;
  965. if(!data->conn && data->set.connect_only) {
  966. result = Curl_connect_only_attach(data);
  967. if(result)
  968. return result;
  969. }
  970. if(!data->conn) {
  971. failf(data, "No associated connection");
  972. return CURLE_SEND_ERROR;
  973. }
  974. if(!data->conn->proto.ws) {
  975. failf(data, "Not a websocket transfer");
  976. return CURLE_SEND_ERROR;
  977. }
  978. ws = data->conn->proto.ws;
  979. if(data->set.ws_raw_mode) {
  980. if(fragsize || flags) {
  981. DEBUGF(infof(data, "ws_send: "
  982. "fragsize and flags cannot be non-zero in raw mode"));
  983. return CURLE_BAD_FUNCTION_ARGUMENT;
  984. }
  985. if(!buflen)
  986. /* nothing to do */
  987. return CURLE_OK;
  988. /* raw mode sends exactly what was requested, and this is from within
  989. the write callback */
  990. if(Curl_is_in_callback(data)) {
  991. result = Curl_xfer_send(data, buffer, buflen, &nwritten);
  992. }
  993. else
  994. result = Curl_senddata(data, buffer, buflen, &nwritten);
  995. infof(data, "WS: wanted to send %zu bytes, sent %zu bytes",
  996. buflen, nwritten);
  997. *sent = nwritten;
  998. return result;
  999. }
  1000. /* Not RAW mode, buf we do the frame encoding */
  1001. result = ws_flush(data, ws, FALSE);
  1002. if(result)
  1003. return result;
  1004. /* TODO: the current design does not allow partial writes, afaict.
  1005. * It is not clear how the application is supposed to react. */
  1006. space = Curl_bufq_space(&ws->sendbuf);
  1007. DEBUGF(infof(data, "curl_ws_send(len=%zu), sendbuf len=%zu space %zu",
  1008. buflen, Curl_bufq_len(&ws->sendbuf), space));
  1009. if(space < 14)
  1010. return CURLE_AGAIN;
  1011. if(flags & CURLWS_OFFSET) {
  1012. if(fragsize) {
  1013. /* a frame series 'fragsize' bytes big, this is the first */
  1014. n = ws_enc_write_head(data, &ws->enc, flags, fragsize,
  1015. &ws->sendbuf, &result);
  1016. if(n < 0)
  1017. return result;
  1018. }
  1019. else {
  1020. if((curl_off_t)buflen > ws->enc.payload_remain) {
  1021. infof(data, "WS: unaligned frame size (sending %zu instead of %"
  1022. CURL_FORMAT_CURL_OFF_T ")",
  1023. buflen, ws->enc.payload_remain);
  1024. }
  1025. }
  1026. }
  1027. else if(!ws->enc.payload_remain) {
  1028. n = ws_enc_write_head(data, &ws->enc, flags, (curl_off_t)buflen,
  1029. &ws->sendbuf, &result);
  1030. if(n < 0)
  1031. return result;
  1032. }
  1033. n = ws_enc_write_payload(&ws->enc, data,
  1034. buffer, buflen, &ws->sendbuf, &result);
  1035. if(n < 0)
  1036. return result;
  1037. *sent = (size_t)n;
  1038. return ws_flush(data, ws, TRUE);
  1039. }
  1040. static void ws_free(struct connectdata *conn)
  1041. {
  1042. if(conn && conn->proto.ws) {
  1043. Curl_bufq_free(&conn->proto.ws->recvbuf);
  1044. Curl_bufq_free(&conn->proto.ws->sendbuf);
  1045. Curl_safefree(conn->proto.ws);
  1046. }
  1047. }
  1048. static CURLcode ws_setup_conn(struct Curl_easy *data,
  1049. struct connectdata *conn)
  1050. {
  1051. /* websockets is 1.1 only (for now) */
  1052. data->state.httpwant = CURL_HTTP_VERSION_1_1;
  1053. return Curl_http_setup_conn(data, conn);
  1054. }
  1055. static CURLcode ws_disconnect(struct Curl_easy *data,
  1056. struct connectdata *conn,
  1057. bool dead_connection)
  1058. {
  1059. (void)data;
  1060. (void)dead_connection;
  1061. ws_free(conn);
  1062. return CURLE_OK;
  1063. }
  1064. CURL_EXTERN const struct curl_ws_frame *curl_ws_meta(struct Curl_easy *data)
  1065. {
  1066. /* we only return something for websocket, called from within the callback
  1067. when not using raw mode */
  1068. if(GOOD_EASY_HANDLE(data) && Curl_is_in_callback(data) && data->conn &&
  1069. data->conn->proto.ws && !data->set.ws_raw_mode)
  1070. return &data->conn->proto.ws->frame;
  1071. return NULL;
  1072. }
  1073. const struct Curl_handler Curl_handler_ws = {
  1074. "WS", /* scheme */
  1075. ws_setup_conn, /* setup_connection */
  1076. Curl_http, /* do_it */
  1077. Curl_http_done, /* done */
  1078. ZERO_NULL, /* do_more */
  1079. Curl_http_connect, /* connect_it */
  1080. ZERO_NULL, /* connecting */
  1081. ZERO_NULL, /* doing */
  1082. ZERO_NULL, /* proto_getsock */
  1083. Curl_http_getsock_do, /* doing_getsock */
  1084. ZERO_NULL, /* domore_getsock */
  1085. ZERO_NULL, /* perform_getsock */
  1086. ws_disconnect, /* disconnect */
  1087. Curl_http_write_resp, /* write_resp */
  1088. Curl_http_write_resp_hd, /* write_resp_hd */
  1089. ZERO_NULL, /* connection_check */
  1090. ZERO_NULL, /* attach connection */
  1091. PORT_HTTP, /* defport */
  1092. CURLPROTO_WS, /* protocol */
  1093. CURLPROTO_HTTP, /* family */
  1094. PROTOPT_CREDSPERREQUEST | /* flags */
  1095. PROTOPT_USERPWDCTRL
  1096. };
  1097. #ifdef USE_SSL
  1098. const struct Curl_handler Curl_handler_wss = {
  1099. "WSS", /* scheme */
  1100. ws_setup_conn, /* setup_connection */
  1101. Curl_http, /* do_it */
  1102. Curl_http_done, /* done */
  1103. ZERO_NULL, /* do_more */
  1104. Curl_http_connect, /* connect_it */
  1105. NULL, /* connecting */
  1106. ZERO_NULL, /* doing */
  1107. NULL, /* proto_getsock */
  1108. Curl_http_getsock_do, /* doing_getsock */
  1109. ZERO_NULL, /* domore_getsock */
  1110. ZERO_NULL, /* perform_getsock */
  1111. ws_disconnect, /* disconnect */
  1112. Curl_http_write_resp, /* write_resp */
  1113. Curl_http_write_resp_hd, /* write_resp_hd */
  1114. ZERO_NULL, /* connection_check */
  1115. ZERO_NULL, /* attach connection */
  1116. PORT_HTTPS, /* defport */
  1117. CURLPROTO_WSS, /* protocol */
  1118. CURLPROTO_HTTP, /* family */
  1119. PROTOPT_SSL | PROTOPT_CREDSPERREQUEST | /* flags */
  1120. PROTOPT_USERPWDCTRL
  1121. };
  1122. #endif
  1123. #else
  1124. CURL_EXTERN CURLcode curl_ws_recv(CURL *curl, void *buffer, size_t buflen,
  1125. size_t *nread,
  1126. const struct curl_ws_frame **metap)
  1127. {
  1128. (void)curl;
  1129. (void)buffer;
  1130. (void)buflen;
  1131. (void)nread;
  1132. (void)metap;
  1133. return CURLE_NOT_BUILT_IN;
  1134. }
  1135. CURL_EXTERN CURLcode curl_ws_send(CURL *curl, const void *buffer,
  1136. size_t buflen, size_t *sent,
  1137. curl_off_t fragsize,
  1138. unsigned int flags)
  1139. {
  1140. (void)curl;
  1141. (void)buffer;
  1142. (void)buflen;
  1143. (void)sent;
  1144. (void)fragsize;
  1145. (void)flags;
  1146. return CURLE_NOT_BUILT_IN;
  1147. }
  1148. CURL_EXTERN const struct curl_ws_frame *curl_ws_meta(struct Curl_easy *data)
  1149. {
  1150. (void)data;
  1151. return NULL;
  1152. }
  1153. #endif /* USE_WEBSOCKETS */