src/server/util/io.c

changeset 688
02360295ec58
parent 687
4bded456b4a7
equal deleted inserted replaced
687:4bded456b4a7 688:02360295ec58
627 * >0 chunk header length 627 * >0 chunk header length
628 */ 628 */
629 int http_stream_parse_chunk_header(char *str, int len, WSBool first, int64_t *chunklen) { 629 int http_stream_parse_chunk_header(char *str, int len, WSBool first, int64_t *chunklen) {
630 char *hdr_start = NULL; 630 char *hdr_start = NULL;
631 char *hdr_end = NULL; 631 char *hdr_end = NULL;
632 char *chk_end = str + len;
632 int i = 0; 633 int i = 0;
633 if(first) { 634 if(first) {
634 hdr_start = str; 635 hdr_start = str;
635 } else { 636 } else {
636 if(len < 3) { 637 if(len < 3) {
658 return 0; // incomplete 659 return 0; // incomplete
659 } 660 }
660 661
661 if(*hdr_end == '\r') { 662 if(*hdr_end == '\r') {
662 // we also need '\n' 663 // we also need '\n'
664 if(hdr_end + 1 >= chk_end) {
665 return 0; // incomplete
666 }
667
663 if(hdr_end[1] != '\n') { 668 if(hdr_end[1] != '\n') {
664 return -1; 669 return -1;
665 } 670 }
666 i++; // '\n' found 671 i++; // '\n' found
667 } 672 }
715 if(st->read_eof) { 720 if(st->read_eof) {
716 return 0; 721 return 0;
717 } 722 }
718 723
719 char *rbuf = buf; // buffer pos 724 char *rbuf = buf; // buffer pos
720 size_t rd = 0; // number of bytes read 725 ssize_t rd = 0; // number of bytes read
721 size_t rbuflen = nbytes; // number of bytes until end of buf 726 size_t rbuflen = nbytes; // number of bytes until end of buf
722 WSBool perform_io = WS_TRUE; // we do only 1 read before we abort 727 WSBool perform_io = WS_TRUE; // we do only 1 read before we abort
723 while(rd < nbytes && (perform_io || (st->max_read - st->read) > 0)) { 728 while(rd < nbytes && (perform_io || (st->max_read - st->read) > 0)) {
724 // how many bytes are available in the current chunk 729 // how many bytes are available in the current chunk
725 size_t chunk_available = st->max_read - st->read; 730 size_t chunk_available = st->max_read - st->read;

mercurial