… | |
… | |
350 | int amount = sizeof (inbuf) - inbuf_len; |
350 | int amount = sizeof (inbuf) - inbuf_len; |
351 | |
351 | |
352 | if (!amount) |
352 | if (!amount) |
353 | { |
353 | { |
354 | // input buffer full |
354 | // input buffer full |
355 | socket_ev.poll (socket_ev.poll () & ~EV_READ); |
355 | LOG (llevError, "input buffer overflow."); |
|
|
356 | destroy (); |
356 | return; |
357 | return; |
357 | } |
358 | } |
358 | |
359 | |
|
|
360 | if (ws_version) |
|
|
361 | { |
|
|
362 | if (ws_inbuf_len + 4096 > ws_inbuf_alloc) |
|
|
363 | ws_inbuf = (uint8 *)realloc (ws_inbuf, ws_inbuf_alloc += 4096); |
|
|
364 | |
|
|
365 | int len = read (fd, ws_inbuf + ws_inbuf_len, ws_inbuf_alloc - ws_inbuf_len); |
|
|
366 | |
|
|
367 | if (len > 0) |
|
|
368 | { |
|
|
369 | ws_inbuf_len += len; |
|
|
370 | |
|
|
371 | if (ws_inbuf_len < 2 + 4) // 6 is minimum length: op, len, mask |
|
|
372 | return; |
|
|
373 | |
|
|
374 | int d = 2; |
|
|
375 | int o = ws_inbuf [0] & 15; |
|
|
376 | int l = ws_inbuf [1] & 127; |
|
|
377 | |
|
|
378 | if (l == 126) |
|
|
379 | { |
|
|
380 | l = (ws_inbuf [2] << 8) | ws_inbuf [3]; |
|
|
381 | d += 2; |
|
|
382 | } |
|
|
383 | else if (l == 127) |
|
|
384 | { |
|
|
385 | if (ws_inbuf_len < 2 + 8) |
|
|
386 | return; |
|
|
387 | |
|
|
388 | // we don't do extra long frames, if a browser wants to send >2**32 bytes, |
|
|
389 | // there are bigger issues to fix. |
|
|
390 | l = (ws_inbuf [6] << 24) |
|
|
391 | | (ws_inbuf [7] << 16) |
|
|
392 | | (ws_inbuf [8] << 8) |
|
|
393 | | ws_inbuf [9]; |
|
|
394 | d += 8; |
|
|
395 | } |
|
|
396 | |
|
|
397 | // we only continue if we have a complete frame |
|
|
398 | if (ws_inbuf_len < d + 4 + l) |
|
|
399 | return; |
|
|
400 | |
|
|
401 | switch (o) |
|
|
402 | { |
|
|
403 | case 0: o = ws_inbuf_type; break; // continuation |
|
|
404 | case 1: ws_inbuf_type = 1; break; // utf-8 |
|
|
405 | case 2: ws_inbuf_type = 2; break; // binary |
|
|
406 | } |
|
|
407 | |
|
|
408 | if (l > amount) |
|
|
409 | { |
|
|
410 | // input buffer full |
|
|
411 | LOG (llevError, "input buffer overflow (ws)."); |
|
|
412 | destroy (); |
|
|
413 | return; |
|
|
414 | } |
|
|
415 | |
|
|
416 | for (int i = 0; i < l; ++i) |
|
|
417 | inbuf [inbuf_len + i] = ws_inbuf [d + 4 + i] ^ ws_inbuf [d + (i & 3)]; |
|
|
418 | |
|
|
419 | // remove frame |
|
|
420 | ws_inbuf_len -= d + 4 + l; |
|
|
421 | memmove (ws_inbuf, ws_inbuf + d + 4 + l, ws_inbuf_len); |
|
|
422 | |
|
|
423 | switch (o) |
|
|
424 | { |
|
|
425 | case 1: // utf-8 |
|
|
426 | //TODO |
|
|
427 | break; |
|
|
428 | case 2: // binary |
|
|
429 | break; |
|
|
430 | |
|
|
431 | case 9: // ping |
|
|
432 | { |
|
|
433 | // send pong - we assume ping messages are <64k |
|
|
434 | // as we can't handle >10k at the moment anyway. |
|
|
435 | uint8 hdr [] = { 0x8a, 126, l >> 8, l }; |
|
|
436 | send (hdr, sizeof (hdr)); |
|
|
437 | send (inbuf + inbuf_len, l); |
|
|
438 | } |
|
|
439 | return; |
|
|
440 | |
|
|
441 | case 10: // pong |
|
|
442 | return; |
|
|
443 | |
|
|
444 | case 8: // close |
|
|
445 | default: |
|
|
446 | destroy (); |
|
|
447 | return; |
|
|
448 | } |
|
|
449 | |
|
|
450 | amount = l; |
|
|
451 | } |
|
|
452 | else |
|
|
453 | amount = -1; |
|
|
454 | } |
|
|
455 | else |
359 | amount = read (fd, inbuf + inbuf_len, amount); |
456 | amount = read (fd, inbuf + inbuf_len, amount); |
360 | |
457 | |
361 | if (!amount) |
458 | if (!amount) |
362 | { |
459 | { |
363 | destroy (); |
460 | destroy (); |
364 | return; |
461 | return; |
… | |
… | |
455 | } |
552 | } |
456 | |
553 | |
457 | if (!sl.length ()) |
554 | if (!sl.length ()) |
458 | return; |
555 | return; |
459 | |
556 | |
|
|
557 | if (ws_version == 8) |
|
|
558 | { |
|
|
559 | uint8 hdr [4] = { 0x81, 126, sl.length () >> 8, sl.length () }; |
|
|
560 | // TODO: utf-8 encoding |
|
|
561 | send (hdr, 4); |
|
|
562 | send (sl.buf_ + sl.hdrlen, sl.cur - sl.buf_ - sl.hdrlen); |
|
|
563 | } |
|
|
564 | else if (ws_version == 13) |
|
|
565 | { |
|
|
566 | uint8 hdr [4] = { 0x82, 126, sl.length () >> 8, sl.length () }; |
|
|
567 | send (hdr, 4); |
|
|
568 | send (sl.buf_ + sl.hdrlen, sl.cur - sl.buf_ - sl.hdrlen); |
|
|
569 | } |
|
|
570 | else |
|
|
571 | { |
460 | assert (sl.hdrlen == 2); |
572 | assert (sl.hdrlen == 2); |
461 | |
573 | |
462 | sl.buf_ [0] = sl.length () >> 8; |
574 | sl.buf_ [0] = sl.length () >> 8; |
463 | sl.buf_ [1] = sl.length () ; |
575 | sl.buf_ [1] = sl.length () ; |
464 | |
576 | |
465 | send (sl.buf_, sl.length () + sl.hdrlen); |
577 | send (sl.buf_, sl.length () + sl.hdrlen); |
|
|
578 | } |
466 | } |
579 | } |
467 | |
580 | |
468 | void |
581 | void |
469 | client::send_packet (const char *buf, int len) |
582 | client::send_packet (const char *buf, int len) |
470 | { |
583 | { |