1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393 |
- #include "curl_setup.h"
- #include <curl/curl.h>
- #if !defined(CURL_DISABLE_WEBSOCKETS) && !defined(CURL_DISABLE_HTTP)
- #include "urldata.h"
- #include "bufq.h"
- #include "dynbuf.h"
- #include "rand.h"
- #include "curl_base64.h"
- #include "connect.h"
- #include "sendf.h"
- #include "multiif.h"
- #include "ws.h"
- #include "easyif.h"
- #include "transfer.h"
- #include "select.h"
- #include "nonblock.h"
- #include "curl_printf.h"
- #include "curl_memory.h"
- #include "memdebug.h"
- #define WSBIT_FIN 0x80
- #define WSBIT_OPCODE_CONT 0
- #define WSBIT_OPCODE_TEXT (1)
- #define WSBIT_OPCODE_BIN (2)
- #define WSBIT_OPCODE_CLOSE (8)
- #define WSBIT_OPCODE_PING (9)
- #define WSBIT_OPCODE_PONG (0xa)
- #define WSBIT_OPCODE_MASK (0xf)
- #define WSBIT_MASK 0x80
- #define WS_CHUNK_SIZE 65535
- #define WS_CHUNK_COUNT 2
- struct ws_frame_meta {
- char proto_opcode;
- int flags;
- const char *name;
- };
- static struct ws_frame_meta WS_FRAMES[] = {
- { WSBIT_OPCODE_CONT, CURLWS_CONT, "CONT" },
- { WSBIT_OPCODE_TEXT, CURLWS_TEXT, "TEXT" },
- { WSBIT_OPCODE_BIN, CURLWS_BINARY, "BIN" },
- { WSBIT_OPCODE_CLOSE, CURLWS_CLOSE, "CLOSE" },
- { WSBIT_OPCODE_PING, CURLWS_PING, "PING" },
- { WSBIT_OPCODE_PONG, CURLWS_PONG, "PONG" },
- };
- static const char *ws_frame_name_of_op(unsigned char proto_opcode)
- {
- unsigned char opcode = proto_opcode & WSBIT_OPCODE_MASK;
- size_t i;
- for(i = 0; i < sizeof(WS_FRAMES)/sizeof(WS_FRAMES[0]); ++i) {
- if(WS_FRAMES[i].proto_opcode == opcode)
- return WS_FRAMES[i].name;
- }
- return "???";
- }
- static int ws_frame_op2flags(unsigned char proto_opcode)
- {
- unsigned char opcode = proto_opcode & WSBIT_OPCODE_MASK;
- size_t i;
- for(i = 0; i < sizeof(WS_FRAMES)/sizeof(WS_FRAMES[0]); ++i) {
- if(WS_FRAMES[i].proto_opcode == opcode)
- return WS_FRAMES[i].flags;
- }
- return 0;
- }
- static unsigned char ws_frame_flags2op(int flags)
- {
- size_t i;
- for(i = 0; i < sizeof(WS_FRAMES)/sizeof(WS_FRAMES[0]); ++i) {
- if(WS_FRAMES[i].flags & flags)
- return (unsigned char)WS_FRAMES[i].proto_opcode;
- }
- return 0;
- }
- static void ws_dec_info(struct ws_decoder *dec, struct Curl_easy *data,
- const char *msg)
- {
- switch(dec->head_len) {
- case 0:
- break;
- case 1:
- CURL_TRC_WRITE(data, "websocket, decoded %s [%s%s]", msg,
- ws_frame_name_of_op(dec->head[0]),
- (dec->head[0] & WSBIT_FIN) ? "" : " NON-FINAL");
- break;
- default:
- if(dec->head_len < dec->head_total) {
- CURL_TRC_WRITE(data, "websocket, decoded %s [%s%s](%d/%d)", msg,
- ws_frame_name_of_op(dec->head[0]),
- (dec->head[0] & WSBIT_FIN) ? "" : " NON-FINAL",
- dec->head_len, dec->head_total);
- }
- else {
- CURL_TRC_WRITE(data, "websocket, decoded %s [%s%s payload=%"
- FMT_OFF_T "/%" FMT_OFF_T "]",
- msg, ws_frame_name_of_op(dec->head[0]),
- (dec->head[0] & WSBIT_FIN) ? "" : " NON-FINAL",
- dec->payload_offset, dec->payload_len);
- }
- break;
- }
- }
- static CURLcode ws_send_raw_blocking(CURL *data, struct websocket *ws,
- const char *buffer, size_t buflen);
- typedef ssize_t ws_write_payload(const unsigned char *buf, size_t buflen,
- int frame_age, int frame_flags,
- curl_off_t payload_offset,
- curl_off_t payload_len,
- void *userp,
- CURLcode *err);
- static void ws_dec_reset(struct ws_decoder *dec)
- {
- dec->frame_age = 0;
- dec->frame_flags = 0;
- dec->payload_offset = 0;
- dec->payload_len = 0;
- dec->head_len = dec->head_total = 0;
- dec->state = WS_DEC_INIT;
- }
- static void ws_dec_init(struct ws_decoder *dec)
- {
- ws_dec_reset(dec);
- }
- static CURLcode ws_dec_read_head(struct ws_decoder *dec,
- struct Curl_easy *data,
- struct bufq *inraw)
- {
- const unsigned char *inbuf;
- size_t inlen;
- while(Curl_bufq_peek(inraw, &inbuf, &inlen)) {
- if(dec->head_len == 0) {
- dec->head[0] = *inbuf;
- Curl_bufq_skip(inraw, 1);
- dec->frame_flags = ws_frame_op2flags(dec->head[0]);
- if(!dec->frame_flags) {
- failf(data, "WS: unknown opcode: %x", dec->head[0]);
- ws_dec_reset(dec);
- return CURLE_RECV_ERROR;
- }
- dec->head_len = 1;
-
- continue;
- }
- else if(dec->head_len == 1) {
- dec->head[1] = *inbuf;
- Curl_bufq_skip(inraw, 1);
- dec->head_len = 2;
- if(dec->head[1] & WSBIT_MASK) {
-
- failf(data, "WS: masked input frame");
- ws_dec_reset(dec);
- return CURLE_RECV_ERROR;
- }
-
- if(dec->head[1] == 126) {
- dec->head_total = 4;
- continue;
- }
- else if(dec->head[1] == 127) {
- dec->head_total = 10;
- continue;
- }
- else {
- dec->head_total = 2;
- }
- }
- if(dec->head_len < dec->head_total) {
- dec->head[dec->head_len] = *inbuf;
- Curl_bufq_skip(inraw, 1);
- ++dec->head_len;
- if(dec->head_len < dec->head_total) {
-
- continue;
- }
- }
-
- DEBUGASSERT(dec->head_len == dec->head_total);
- switch(dec->head_total) {
- case 2:
- dec->payload_len = dec->head[1];
- break;
- case 4:
- dec->payload_len = (dec->head[2] << 8) | dec->head[3];
- break;
- case 10:
- if(dec->head[2] > 127) {
- failf(data, "WS: frame length longer than 64 signed not supported");
- return CURLE_RECV_ERROR;
- }
- dec->payload_len = ((curl_off_t)dec->head[2] << 56) |
- (curl_off_t)dec->head[3] << 48 |
- (curl_off_t)dec->head[4] << 40 |
- (curl_off_t)dec->head[5] << 32 |
- (curl_off_t)dec->head[6] << 24 |
- (curl_off_t)dec->head[7] << 16 |
- (curl_off_t)dec->head[8] << 8 |
- dec->head[9];
- break;
- default:
-
- DEBUGASSERT(0);
- failf(data, "WS: unexpected frame header length");
- return CURLE_RECV_ERROR;
- }
- dec->frame_age = 0;
- dec->payload_offset = 0;
- ws_dec_info(dec, data, "decoded");
- return CURLE_OK;
- }
- return CURLE_AGAIN;
- }
- static CURLcode ws_dec_pass_payload(struct ws_decoder *dec,
- struct Curl_easy *data,
- struct bufq *inraw,
- ws_write_payload *write_payload,
- void *write_ctx)
- {
- const unsigned char *inbuf;
- size_t inlen;
- ssize_t nwritten;
- CURLcode result;
- curl_off_t remain = dec->payload_len - dec->payload_offset;
- (void)data;
- while(remain && Curl_bufq_peek(inraw, &inbuf, &inlen)) {
- if((curl_off_t)inlen > remain)
- inlen = (size_t)remain;
- nwritten = write_payload(inbuf, inlen, dec->frame_age, dec->frame_flags,
- dec->payload_offset, dec->payload_len,
- write_ctx, &result);
- if(nwritten < 0)
- return result;
- Curl_bufq_skip(inraw, (size_t)nwritten);
- dec->payload_offset += (curl_off_t)nwritten;
- remain = dec->payload_len - dec->payload_offset;
- CURL_TRC_WRITE(data, "websocket, passed %zd bytes payload, %"
- FMT_OFF_T " remain", nwritten, remain);
- }
- return remain ? CURLE_AGAIN : CURLE_OK;
- }
- static CURLcode ws_dec_pass(struct ws_decoder *dec,
- struct Curl_easy *data,
- struct bufq *inraw,
- ws_write_payload *write_payload,
- void *write_ctx)
- {
- CURLcode result;
- if(Curl_bufq_is_empty(inraw))
- return CURLE_AGAIN;
- switch(dec->state) {
- case WS_DEC_INIT:
- ws_dec_reset(dec);
- dec->state = WS_DEC_HEAD;
- FALLTHROUGH();
- case WS_DEC_HEAD:
- result = ws_dec_read_head(dec, data, inraw);
- if(result) {
- if(result != CURLE_AGAIN) {
- infof(data, "WS: decode error %d", (int)result);
- break;
- }
-
- DEBUGASSERT(Curl_bufq_is_empty(inraw));
- break;
- }
-
- dec->state = WS_DEC_PAYLOAD;
- if(dec->payload_len == 0) {
- ssize_t nwritten;
- const unsigned char tmp = '\0';
-
- nwritten = write_payload(&tmp, 0, dec->frame_age, dec->frame_flags,
- 0, 0, write_ctx, &result);
- if(nwritten < 0)
- return result;
- dec->state = WS_DEC_INIT;
- break;
- }
- FALLTHROUGH();
- case WS_DEC_PAYLOAD:
- result = ws_dec_pass_payload(dec, data, inraw, write_payload, write_ctx);
- ws_dec_info(dec, data, "passing");
- if(result)
- return result;
-
- dec->state = WS_DEC_INIT;
- break;
- default:
-
- result = CURLE_FAILED_INIT;
- }
- return result;
- }
- static void update_meta(struct websocket *ws,
- int frame_age, int frame_flags,
- curl_off_t payload_offset,
- curl_off_t payload_len,
- size_t cur_len)
- {
- ws->frame.age = frame_age;
- ws->frame.flags = frame_flags;
- ws->frame.offset = payload_offset;
- ws->frame.len = cur_len;
- ws->frame.bytesleft = (payload_len - payload_offset - cur_len);
- }
- struct ws_cw_ctx {
- struct Curl_cwriter super;
- struct bufq buf;
- };
- static CURLcode ws_cw_init(struct Curl_easy *data,
- struct Curl_cwriter *writer)
- {
- struct ws_cw_ctx *ctx = writer->ctx;
- (void)data;
- Curl_bufq_init2(&ctx->buf, WS_CHUNK_SIZE, 1, BUFQ_OPT_SOFT_LIMIT);
- return CURLE_OK;
- }
- static void ws_cw_close(struct Curl_easy *data, struct Curl_cwriter *writer)
- {
- struct ws_cw_ctx *ctx = writer->ctx;
- (void) data;
- Curl_bufq_free(&ctx->buf);
- }
- struct ws_cw_dec_ctx {
- struct Curl_easy *data;
- struct websocket *ws;
- struct Curl_cwriter *next_writer;
- int cw_type;
- };
- static ssize_t ws_cw_dec_next(const unsigned char *buf, size_t buflen,
- int frame_age, int frame_flags,
- curl_off_t payload_offset,
- curl_off_t payload_len,
- void *user_data,
- CURLcode *err)
- {
- struct ws_cw_dec_ctx *ctx = user_data;
- struct Curl_easy *data = ctx->data;
- struct websocket *ws = ctx->ws;
- curl_off_t remain = (payload_len - (payload_offset + buflen));
- (void)frame_age;
- if((frame_flags & CURLWS_PING) && !remain) {
-
- size_t bytes;
- infof(data, "WS: auto-respond to PING with a PONG");
-
- *err = curl_ws_send(data, buf, buflen, &bytes, 0, CURLWS_PONG);
- if(*err)
- return -1;
- }
- else if(buflen || !remain) {
-
- update_meta(ws, frame_age, frame_flags, payload_offset,
- payload_len, buflen);
- *err = Curl_cwriter_write(data, ctx->next_writer, ctx->cw_type,
- (const char *)buf, buflen);
- if(*err)
- return -1;
- }
- *err = CURLE_OK;
- return (ssize_t)buflen;
- }
- static CURLcode ws_cw_write(struct Curl_easy *data,
- struct Curl_cwriter *writer, int type,
- const char *buf, size_t nbytes)
- {
- struct ws_cw_ctx *ctx = writer->ctx;
- struct websocket *ws;
- CURLcode result;
- if(!(type & CLIENTWRITE_BODY) || data->set.ws_raw_mode)
- return Curl_cwriter_write(data, writer->next, type, buf, nbytes);
- ws = data->conn->proto.ws;
- if(!ws) {
- failf(data, "WS: not a websocket transfer");
- return CURLE_FAILED_INIT;
- }
- if(nbytes) {
- ssize_t nwritten;
- nwritten = Curl_bufq_write(&ctx->buf, (const unsigned char *)buf,
- nbytes, &result);
- if(nwritten < 0) {
- infof(data, "WS: error adding data to buffer %d", result);
- return result;
- }
- }
- while(!Curl_bufq_is_empty(&ctx->buf)) {
- struct ws_cw_dec_ctx pass_ctx;
- pass_ctx.data = data;
- pass_ctx.ws = ws;
- pass_ctx.next_writer = writer->next;
- pass_ctx.cw_type = type;
- result = ws_dec_pass(&ws->dec, data, &ctx->buf,
- ws_cw_dec_next, &pass_ctx);
- if(result == CURLE_AGAIN) {
-
- CURL_TRC_WRITE(data, "websocket, buffered incomplete frame head");
- return CURLE_OK;
- }
- else if(result) {
- infof(data, "WS: decode error %d", (int)result);
- return result;
- }
- }
- if((type & CLIENTWRITE_EOS) && !Curl_bufq_is_empty(&ctx->buf)) {
- infof(data, "WS: decode ending with %zd frame bytes remaining",
- Curl_bufq_len(&ctx->buf));
- return CURLE_RECV_ERROR;
- }
- return CURLE_OK;
- }
- static const struct Curl_cwtype ws_cw_decode = {
- "ws-decode",
- NULL,
- ws_cw_init,
- ws_cw_write,
- ws_cw_close,
- sizeof(struct ws_cw_ctx)
- };
- static void ws_enc_info(struct ws_encoder *enc, struct Curl_easy *data,
- const char *msg)
- {
- infof(data, "WS-ENC: %s [%s%s%s payload=%" FMT_OFF_T "/%" FMT_OFF_T "]",
- msg, ws_frame_name_of_op(enc->firstbyte),
- (enc->firstbyte & WSBIT_OPCODE_MASK) == WSBIT_OPCODE_CONT ?
- " CONT" : "",
- (enc->firstbyte & WSBIT_FIN) ? "" : " NON-FIN",
- enc->payload_len - enc->payload_remain, enc->payload_len);
- }
- static void ws_enc_reset(struct ws_encoder *enc)
- {
- enc->payload_remain = 0;
- enc->xori = 0;
- enc->contfragment = FALSE;
- }
- static void ws_enc_init(struct ws_encoder *enc)
- {
- ws_enc_reset(enc);
- }
- static ssize_t ws_enc_write_head(struct Curl_easy *data,
- struct ws_encoder *enc,
- unsigned int flags,
- curl_off_t payload_len,
- struct bufq *out,
- CURLcode *err)
- {
- unsigned char firstbyte = 0;
- unsigned char opcode;
- unsigned char head[14];
- size_t hlen;
- ssize_t n;
- if(payload_len < 0) {
- failf(data, "WS: starting new frame with negative payload length %"
- FMT_OFF_T, payload_len);
- *err = CURLE_SEND_ERROR;
- return -1;
- }
- if(enc->payload_remain > 0) {
-
- failf(data, "WS: starting new frame with %zd bytes from last one "
- "remaining to be sent", (ssize_t)enc->payload_remain);
- *err = CURLE_SEND_ERROR;
- return -1;
- }
- opcode = ws_frame_flags2op((int)flags & ~CURLWS_CONT);
- if(!opcode) {
- failf(data, "WS: provided flags not recognized '%x'", flags);
- *err = CURLE_SEND_ERROR;
- return -1;
- }
- if(!(flags & CURLWS_CONT)) {
- if(!enc->contfragment)
-
- firstbyte |= WSBIT_FIN | opcode;
- else
-
- firstbyte |= WSBIT_FIN | WSBIT_OPCODE_CONT;
- enc->contfragment = FALSE;
- }
- else if(enc->contfragment) {
-
- firstbyte |= WSBIT_OPCODE_CONT;
- }
- else {
- firstbyte = opcode;
- enc->contfragment = TRUE;
- }
- head[0] = enc->firstbyte = firstbyte;
- if(payload_len > 65535) {
- head[1] = 127 | WSBIT_MASK;
- head[2] = (unsigned char)((payload_len >> 56) & 0xff);
- head[3] = (unsigned char)((payload_len >> 48) & 0xff);
- head[4] = (unsigned char)((payload_len >> 40) & 0xff);
- head[5] = (unsigned char)((payload_len >> 32) & 0xff);
- head[6] = (unsigned char)((payload_len >> 24) & 0xff);
- head[7] = (unsigned char)((payload_len >> 16) & 0xff);
- head[8] = (unsigned char)((payload_len >> 8) & 0xff);
- head[9] = (unsigned char)(payload_len & 0xff);
- hlen = 10;
- }
- else if(payload_len >= 126) {
- head[1] = 126 | WSBIT_MASK;
- head[2] = (unsigned char)((payload_len >> 8) & 0xff);
- head[3] = (unsigned char)(payload_len & 0xff);
- hlen = 4;
- }
- else {
- head[1] = (unsigned char)payload_len | WSBIT_MASK;
- hlen = 2;
- }
- enc->payload_remain = enc->payload_len = payload_len;
- ws_enc_info(enc, data, "sending");
-
- memcpy(&head[hlen], &enc->mask, 4);
- hlen += 4;
-
- enc->xori = 0;
- n = Curl_bufq_write(out, head, hlen, err);
- if(n < 0)
- return -1;
- if((size_t)n != hlen) {
-
- DEBUGASSERT(0);
- *err = CURLE_SEND_ERROR;
- return -1;
- }
- return n;
- }
- static ssize_t ws_enc_write_payload(struct ws_encoder *enc,
- struct Curl_easy *data,
- const unsigned char *buf, size_t buflen,
- struct bufq *out, CURLcode *err)
- {
- ssize_t n;
- size_t i, len;
- if(Curl_bufq_is_full(out)) {
- *err = CURLE_AGAIN;
- return -1;
- }
-
- len = buflen;
- if((curl_off_t)len > enc->payload_remain)
- len = (size_t)enc->payload_remain;
- for(i = 0; i < len; ++i) {
- unsigned char c = buf[i] ^ enc->mask[enc->xori];
- n = Curl_bufq_write(out, &c, 1, err);
- if(n < 0) {
- if((*err != CURLE_AGAIN) || !i)
- return -1;
- break;
- }
- enc->xori++;
- enc->xori &= 3;
- }
- enc->payload_remain -= (curl_off_t)i;
- ws_enc_info(enc, data, "buffered");
- return (ssize_t)i;
- }
- struct wsfield {
- const char *name;
- const char *val;
- };
- CURLcode Curl_ws_request(struct Curl_easy *data, REQTYPE *req)
- {
- unsigned int i;
- CURLcode result = CURLE_OK;
- unsigned char rand[16];
- char *randstr;
- size_t randlen;
- char keyval[40];
- struct SingleRequest *k = &data->req;
- struct wsfield heads[]= {
- {
-
- "Upgrade:", "websocket"
- },
- {
-
- "Connection:", "Upgrade",
- },
- {
-
- "Sec-WebSocket-Version:", "13",
- },
- {
-
- "Sec-WebSocket-Key:", NULL,
- }
- };
- heads[3].val = &keyval[0];
-
- result = Curl_rand(data, (unsigned char *)rand, sizeof(rand));
- if(result)
- return result;
- result = Curl_base64_encode((char *)rand, sizeof(rand), &randstr, &randlen);
- if(result)
- return result;
- DEBUGASSERT(randlen < sizeof(keyval));
- if(randlen >= sizeof(keyval)) {
- free(randstr);
- return CURLE_FAILED_INIT;
- }
- strcpy(keyval, randstr);
- free(randstr);
- for(i = 0; !result && (i < sizeof(heads)/sizeof(heads[0])); i++) {
- if(!Curl_checkheaders(data, STRCONST(heads[i].name))) {
- #ifdef USE_HYPER
- char field[128];
- msnprintf(field, sizeof(field), "%s %s", heads[i].name,
- heads[i].val);
- result = Curl_hyper_header(data, req, field);
- #else
- (void)data;
- result = Curl_dyn_addf(req, "%s %s\r\n", heads[i].name,
- heads[i].val);
- #endif
- }
- }
- k->upgr101 = UPGR101_WS;
- return result;
- }
- CURLcode Curl_ws_accept(struct Curl_easy *data,
- const char *mem, size_t nread)
- {
- struct SingleRequest *k = &data->req;
- struct websocket *ws;
- struct Curl_cwriter *ws_dec_writer;
- CURLcode result;
- DEBUGASSERT(data->conn);
- ws = data->conn->proto.ws;
- if(!ws) {
- size_t chunk_size = WS_CHUNK_SIZE;
- ws = calloc(1, sizeof(*ws));
- if(!ws)
- return CURLE_OUT_OF_MEMORY;
- data->conn->proto.ws = ws;
- #ifdef DEBUGBUILD
- {
- char *p = getenv("CURL_WS_CHUNK_SIZE");
- if(p) {
- long l = strtol(p, NULL, 10);
- if(l > 0 && l <= (1*1024*1024)) {
- chunk_size = (size_t)l;
- }
- }
- }
- #endif
- CURL_TRC_WS(data, "WS, using chunk size %zu", chunk_size);
- Curl_bufq_init2(&ws->recvbuf, chunk_size, WS_CHUNK_COUNT,
- BUFQ_OPT_SOFT_LIMIT);
- Curl_bufq_init2(&ws->sendbuf, chunk_size, WS_CHUNK_COUNT,
- BUFQ_OPT_SOFT_LIMIT);
- ws_dec_init(&ws->dec);
- ws_enc_init(&ws->enc);
- }
- else {
- Curl_bufq_reset(&ws->recvbuf);
- ws_dec_reset(&ws->dec);
- ws_enc_reset(&ws->enc);
- }
-
-
-
-
- result = Curl_rand(data, (unsigned char *)&ws->enc.mask,
- sizeof(ws->enc.mask));
- if(result)
- return result;
- infof(data, "Received 101, switch to WebSocket; mask %02x%02x%02x%02x",
- ws->enc.mask[0], ws->enc.mask[1], ws->enc.mask[2], ws->enc.mask[3]);
-
- result = Curl_cwriter_create(&ws_dec_writer, data, &ws_cw_decode,
- CURL_CW_CONTENT_DECODE);
- if(result)
- return result;
- result = Curl_cwriter_add(data, ws_dec_writer);
- if(result) {
- Curl_cwriter_free(data, ws_dec_writer);
- return result;
- }
- if(data->set.connect_only) {
- ssize_t nwritten;
-
- nwritten = Curl_bufq_write(&ws->recvbuf, (const unsigned char *)mem,
- nread, &result);
- if(nwritten < 0)
- return result;
- infof(data, "%zu bytes websocket payload", nread);
- }
- else {
-
- if(nread) {
- result = Curl_client_write(data, CLIENTWRITE_BODY, (char *)mem, nread);
- }
- }
- k->upgr101 = UPGR101_RECEIVED;
- return result;
- }
- struct ws_collect {
- struct Curl_easy *data;
- unsigned char *buffer;
- size_t buflen;
- size_t bufidx;
- int frame_age;
- int frame_flags;
- curl_off_t payload_offset;
- curl_off_t payload_len;
- bool written;
- };
- static ssize_t ws_client_collect(const unsigned char *buf, size_t buflen,
- int frame_age, int frame_flags,
- curl_off_t payload_offset,
- curl_off_t payload_len,
- void *userp,
- CURLcode *err)
- {
- struct ws_collect *ctx = userp;
- size_t nwritten;
- curl_off_t remain = (payload_len - (payload_offset + buflen));
- if(!ctx->bufidx) {
-
- ctx->frame_age = frame_age;
- ctx->frame_flags = frame_flags;
- ctx->payload_offset = payload_offset;
- ctx->payload_len = payload_len;
- }
- if((frame_flags & CURLWS_PING) && !remain) {
-
- size_t bytes;
- infof(ctx->data, "WS: auto-respond to PING with a PONG");
-
- *err = curl_ws_send(ctx->data, buf, buflen, &bytes, 0, CURLWS_PONG);
- if(*err)
- return -1;
- nwritten = bytes;
- }
- else {
- ctx->written = TRUE;
- DEBUGASSERT(ctx->buflen >= ctx->bufidx);
- nwritten = CURLMIN(buflen, ctx->buflen - ctx->bufidx);
- if(!nwritten) {
- if(!buflen) {
- *err = CURLE_OK;
- return 0;
- }
- *err = CURLE_AGAIN;
- return -1;
- }
- *err = CURLE_OK;
- memcpy(ctx->buffer + ctx->bufidx, buf, nwritten);
- ctx->bufidx += nwritten;
- }
- return nwritten;
- }
- static ssize_t nw_in_recv(void *reader_ctx,
- unsigned char *buf, size_t buflen,
- CURLcode *err)
- {
- struct Curl_easy *data = reader_ctx;
- size_t nread;
- *err = curl_easy_recv(data, buf, buflen, &nread);
- if(*err)
- return -1;
- return (ssize_t)nread;
- }
- CURL_EXTERN CURLcode curl_ws_recv(CURL *d, void *buffer,
- size_t buflen, size_t *nread,
- const struct curl_ws_frame **metap)
- {
- struct Curl_easy *data = d;
- struct connectdata *conn = data->conn;
- struct websocket *ws;
- struct ws_collect ctx;
- *nread = 0;
- *metap = NULL;
- if(!conn) {
-
- if(!data->set.connect_only) {
- failf(data, "CONNECT_ONLY is required");
- return CURLE_UNSUPPORTED_PROTOCOL;
- }
- Curl_getconnectinfo(data, &conn);
- if(!conn) {
- failf(data, "connection not found");
- return CURLE_BAD_FUNCTION_ARGUMENT;
- }
- }
- ws = conn->proto.ws;
- if(!ws) {
- failf(data, "connection is not setup for websocket");
- return CURLE_BAD_FUNCTION_ARGUMENT;
- }
- memset(&ctx, 0, sizeof(ctx));
- ctx.data = data;
- ctx.buffer = buffer;
- ctx.buflen = buflen;
- while(1) {
- CURLcode result;
-
- if(Curl_bufq_is_empty(&ws->recvbuf)) {
- ssize_t n = Curl_bufq_slurp(&ws->recvbuf, nw_in_recv, data, &result);
- if(n < 0) {
- return result;
- }
- else if(n == 0) {
-
- infof(data, "connection expectedly closed?");
- return CURLE_GOT_NOTHING;
- }
- CURL_TRC_WS(data, "curl_ws_recv, added %zu bytes from network",
- Curl_bufq_len(&ws->recvbuf));
- }
- result = ws_dec_pass(&ws->dec, data, &ws->recvbuf,
- ws_client_collect, &ctx);
- if(result == CURLE_AGAIN) {
- if(!ctx.written) {
- ws_dec_info(&ws->dec, data, "need more input");
- continue;
- }
- break;
- }
- else if(result) {
- return result;
- }
- else if(ctx.written) {
-
- break;
- }
- }
-
- update_meta(ws, ctx.frame_age, ctx.frame_flags, ctx.payload_offset,
- ctx.payload_len, ctx.bufidx);
- *metap = &ws->frame;
- *nread = ws->frame.len;
- CURL_TRC_WS(data, "curl_ws_recv(len=%zu) -> %zu bytes (frame at %"
- FMT_OFF_T ", %" FMT_OFF_T " left)",
- buflen, *nread, ws->frame.offset, ws->frame.bytesleft);
- return CURLE_OK;
- }
- static CURLcode ws_flush(struct Curl_easy *data, struct websocket *ws,
- bool blocking)
- {
- if(!Curl_bufq_is_empty(&ws->sendbuf)) {
- CURLcode result;
- const unsigned char *out;
- size_t outlen, n;
- while(Curl_bufq_peek(&ws->sendbuf, &out, &outlen)) {
- if(blocking) {
- result = ws_send_raw_blocking(data, ws, (char *)out, outlen);
- n = result ? 0 : outlen;
- }
- else if(data->set.connect_only || Curl_is_in_callback(data))
- result = Curl_senddata(data, out, outlen, &n);
- else {
- result = Curl_xfer_send(data, out, outlen, FALSE, &n);
- if(!result && !n && outlen)
- result = CURLE_AGAIN;
- }
- if(result == CURLE_AGAIN) {
- CURL_TRC_WS(data, "flush EAGAIN, %zu bytes remain in buffer",
- Curl_bufq_len(&ws->sendbuf));
- return result;
- }
- else if(result) {
- failf(data, "WS: flush, write error %d", result);
- return result;
- }
- else {
- infof(data, "WS: flushed %zu bytes", n);
- Curl_bufq_skip(&ws->sendbuf, n);
- }
- }
- }
- return CURLE_OK;
- }
- static CURLcode ws_send_raw_blocking(CURL *d, struct websocket *ws,
- const char *buffer, size_t buflen)
- {
- CURLcode result = CURLE_OK;
- size_t nwritten;
- struct Curl_easy *data = d;
- (void)ws;
- while(buflen) {
- result = Curl_xfer_send(data, buffer, buflen, FALSE, &nwritten);
- if(result)
- return result;
- DEBUGASSERT(nwritten <= buflen);
- buffer += nwritten;
- buflen -= nwritten;
- if(buflen) {
- curl_socket_t sock = data->conn->sock[FIRSTSOCKET];
- timediff_t left_ms;
- int ev;
- CURL_TRC_WS(data, "ws_send_raw_blocking() partial, %zu left to send",
- buflen);
- left_ms = Curl_timeleft(data, NULL, FALSE);
- if(left_ms < 0) {
- failf(data, "Timeout waiting for socket becoming writable");
- return CURLE_SEND_ERROR;
- }
-
- if(sock == CURL_SOCKET_BAD)
- return CURLE_SEND_ERROR;
- ev = Curl_socket_check(CURL_SOCKET_BAD, CURL_SOCKET_BAD, sock,
- left_ms ? left_ms : 500);
- if(ev < 0) {
- failf(data, "Error while waiting for socket becoming writable");
- return CURLE_SEND_ERROR;
- }
- }
- }
- return result;
- }
- static CURLcode ws_send_raw(struct Curl_easy *data, const void *buffer,
- size_t buflen, size_t *pnwritten)
- {
- struct websocket *ws = data->conn->proto.ws;
- CURLcode result;
- if(!ws) {
- failf(data, "Not a websocket transfer");
- return CURLE_SEND_ERROR;
- }
- if(!buflen)
- return CURLE_OK;
- if(Curl_is_in_callback(data)) {
-
- result = ws_flush(data, ws, TRUE);
- if(result)
- return result;
- result = ws_send_raw_blocking(data, ws, buffer, buflen);
- }
- else {
-
- result = ws_flush(data, ws, FALSE);
- if(result)
- return result;
- result = Curl_senddata(data, buffer, buflen, pnwritten);
- }
- CURL_TRC_WS(data, "ws_send_raw(len=%zu) -> %d, %zu",
- buflen, result, *pnwritten);
- return result;
- }
- CURL_EXTERN CURLcode curl_ws_send(CURL *d, const void *buffer,
- size_t buflen, size_t *sent,
- curl_off_t fragsize,
- unsigned int flags)
- {
- struct websocket *ws;
- ssize_t n;
- size_t space, payload_added;
- CURLcode result;
- struct Curl_easy *data = d;
- CURL_TRC_WS(data, "curl_ws_send(len=%zu, fragsize=%" FMT_OFF_T
- ", flags=%x), raw=%d",
- buflen, fragsize, flags, data->set.ws_raw_mode);
- *sent = 0;
- if(!data->conn && data->set.connect_only) {
- result = Curl_connect_only_attach(data);
- if(result)
- goto out;
- }
- if(!data->conn) {
- failf(data, "No associated connection");
- result = CURLE_SEND_ERROR;
- goto out;
- }
- if(!data->conn->proto.ws) {
- failf(data, "Not a websocket transfer");
- result = CURLE_SEND_ERROR;
- goto out;
- }
- ws = data->conn->proto.ws;
-
- result = ws_flush(data, ws, FALSE);
- if(result)
- goto out;
- if(data->set.ws_raw_mode) {
-
- if(fragsize || flags) {
- failf(data, "ws_send, raw mode: fragsize and flags cannot be non-zero");
- return CURLE_BAD_FUNCTION_ARGUMENT;
- }
- result = ws_send_raw(data, buffer, buflen, sent);
- goto out;
- }
-
- space = Curl_bufq_space(&ws->sendbuf);
- CURL_TRC_WS(data, "curl_ws_send(len=%zu), sendbuf=%zu space_left=%zu",
- buflen, Curl_bufq_len(&ws->sendbuf), space);
- if(space < 14) {
- result = CURLE_AGAIN;
- goto out;
- }
- if(flags & CURLWS_OFFSET) {
- if(fragsize) {
-
- n = ws_enc_write_head(data, &ws->enc, flags, fragsize,
- &ws->sendbuf, &result);
- if(n < 0)
- goto out;
- }
- else {
- if((curl_off_t)buflen > ws->enc.payload_remain) {
- infof(data, "WS: unaligned frame size (sending %zu instead of %"
- FMT_OFF_T ")",
- buflen, ws->enc.payload_remain);
- }
- }
- }
- else if(!ws->enc.payload_remain) {
- n = ws_enc_write_head(data, &ws->enc, flags, (curl_off_t)buflen,
- &ws->sendbuf, &result);
- if(n < 0)
- goto out;
- }
- n = ws_enc_write_payload(&ws->enc, data,
- buffer, buflen, &ws->sendbuf, &result);
- if(n < 0)
- goto out;
- payload_added = (size_t)n;
- while(!result && (buflen || !Curl_bufq_is_empty(&ws->sendbuf))) {
-
- result = ws_flush(data, ws, Curl_is_in_callback(data));
- if(!result) {
- DEBUGASSERT(payload_added <= buflen);
-
- *sent += payload_added;
- buffer = (const char *)buffer + payload_added;
- buflen -= payload_added;
- payload_added = 0;
- if(buflen) {
- n = ws_enc_write_payload(&ws->enc, data,
- buffer, buflen, &ws->sendbuf, &result);
- if(n < 0)
- goto out;
- payload_added = Curl_bufq_len(&ws->sendbuf);
- }
- }
- else if(result == CURLE_AGAIN) {
-
- if(payload_added < buflen) {
-
- *sent = payload_added;
- result = CURLE_OK;
- goto out;
- }
- else if(!buflen) {
-
- *sent = (size_t)n;
- result = ws_flush(data, ws, TRUE);
- goto out;
- }
-
- *sent = payload_added - 1;
- result = Curl_bufq_unwrite(&ws->sendbuf, 1);
- if(!result)
- result = CURLE_AGAIN;
- }
- }
- out:
- CURL_TRC_WS(data, "curl_ws_send(len=%zu, fragsize=%" FMT_OFF_T
- ", flags=%x, raw=%d) -> %d, %zu",
- buflen, fragsize, flags, data->set.ws_raw_mode, result, *sent);
- return result;
- }
- static void ws_free(struct connectdata *conn)
- {
- if(conn && conn->proto.ws) {
- Curl_bufq_free(&conn->proto.ws->recvbuf);
- Curl_bufq_free(&conn->proto.ws->sendbuf);
- Curl_safefree(conn->proto.ws);
- }
- }
- static CURLcode ws_setup_conn(struct Curl_easy *data,
- struct connectdata *conn)
- {
-
- data->state.httpwant = CURL_HTTP_VERSION_1_1;
- return Curl_http_setup_conn(data, conn);
- }
- static CURLcode ws_disconnect(struct Curl_easy *data,
- struct connectdata *conn,
- bool dead_connection)
- {
- (void)data;
- (void)dead_connection;
- ws_free(conn);
- return CURLE_OK;
- }
- CURL_EXTERN const struct curl_ws_frame *curl_ws_meta(CURL *d)
- {
-
- struct Curl_easy *data = d;
- if(GOOD_EASY_HANDLE(data) && Curl_is_in_callback(data) && data->conn &&
- data->conn->proto.ws && !data->set.ws_raw_mode)
- return &data->conn->proto.ws->frame;
- return NULL;
- }
- const struct Curl_handler Curl_handler_ws = {
- "WS",
- ws_setup_conn,
- Curl_http,
- Curl_http_done,
- ZERO_NULL,
- Curl_http_connect,
- ZERO_NULL,
- ZERO_NULL,
- ZERO_NULL,
- Curl_http_getsock_do,
- ZERO_NULL,
- ZERO_NULL,
- ws_disconnect,
- Curl_http_write_resp,
- Curl_http_write_resp_hd,
- ZERO_NULL,
- ZERO_NULL,
- PORT_HTTP,
- CURLPROTO_WS,
- CURLPROTO_HTTP,
- PROTOPT_CREDSPERREQUEST |
- PROTOPT_USERPWDCTRL
- };
- #ifdef USE_SSL
- const struct Curl_handler Curl_handler_wss = {
- "WSS",
- ws_setup_conn,
- Curl_http,
- Curl_http_done,
- ZERO_NULL,
- Curl_http_connect,
- NULL,
- ZERO_NULL,
- NULL,
- Curl_http_getsock_do,
- ZERO_NULL,
- ZERO_NULL,
- ws_disconnect,
- Curl_http_write_resp,
- Curl_http_write_resp_hd,
- ZERO_NULL,
- ZERO_NULL,
- PORT_HTTPS,
- CURLPROTO_WSS,
- CURLPROTO_HTTP,
- PROTOPT_SSL | PROTOPT_CREDSPERREQUEST |
- PROTOPT_USERPWDCTRL
- };
- #endif
- #else
- CURL_EXTERN CURLcode curl_ws_recv(CURL *curl, void *buffer, size_t buflen,
- size_t *nread,
- const struct curl_ws_frame **metap)
- {
- (void)curl;
- (void)buffer;
- (void)buflen;
- (void)nread;
- (void)metap;
- return CURLE_NOT_BUILT_IN;
- }
- CURL_EXTERN CURLcode curl_ws_send(CURL *curl, const void *buffer,
- size_t buflen, size_t *sent,
- curl_off_t fragsize,
- unsigned int flags)
- {
- (void)curl;
- (void)buffer;
- (void)buflen;
- (void)sent;
- (void)fragsize;
- (void)flags;
- return CURLE_NOT_BUILT_IN;
- }
- CURL_EXTERN const struct curl_ws_frame *curl_ws_meta(CURL *data)
- {
- (void)data;
- return NULL;
- }
- #endif
|