src/server/util/io.c

branch
webdav
changeset 335
5234c57b8759
parent 334
a55491f66003
child 336
fb75473fecca
equal deleted inserted replaced
334:a55491f66003 335:5234c57b8759
269 http->readbuf = buffer; 269 http->readbuf = buffer;
270 http->bufsize = bufsize; 270 http->bufsize = bufsize;
271 http->buflen = *cursize; // TODO: store buflen as pointer 271 http->buflen = *cursize; // TODO: store buflen as pointer
272 http->bufpos = pos; 272 http->bufpos = pos;
273 http->chunk_buf_pos = 0; 273 http->chunk_buf_pos = 0;
274 http->remaining_len = 0;
275 http->remaining_pos = 0;
276 return 0; 274 return 0;
277 } 275 }
278 276
279 int httpstream_enable_chunked_write(IOStream *st) { 277 int httpstream_enable_chunked_write(IOStream *st) {
280 if(st->write != (io_write_f)net_http_write) { 278 if(st->write != (io_write_f)net_http_write) {
362 ssize_t r = 0; 360 ssize_t r = 0;
363 361
364 //memset(buf, 'x', nbytes); 362 //memset(buf, 'x', nbytes);
365 //char *orig_buf = buf; 363 //char *orig_buf = buf;
366 364
367 // remaining bytes from the chunkbuf
368 /*
369 if(st->remaining_len > 0) {
370 size_t cplen = st->remaining_len > nbytes ? nbytes : st->remaining_len;
371 WSBool ret = FALSE;
372 if(read_data) {
373 // if we read data (and not a chunk header), we limit the
374 // amount of bytes we copy
375 size_t chunk_available = st->max_read - st->read;
376 if(cplen > chunk_available) {
377 cplen = chunk_available;
378 ret = TRUE;
379 }
380 st->read += cplen;
381 }
382 memcpy(buf, &st->remaining_buf[st->remaining_pos], cplen);
383 st->remaining_pos += cplen;
384 st->remaining_len -= cplen;
385 buf += cplen;
386 nbytes -= cplen;
387 r += cplen;
388 if(st->remaining_len == 0) {
389 st->remaining_pos = 0;
390 }
391 if(ret) {
392 return r;
393 }
394 }
395 */
396
397 // copy available data from st->readbuf to buf 365 // copy available data from st->readbuf to buf
398 int pos = *st->bufpos; 366 int pos = *st->bufpos;
399 size_t buf_available = st->buflen - pos; 367 size_t buf_available = st->buflen - pos;
400 if(buf_available) { 368 if(buf_available) {
401 size_t cplen = buf_available > nbytes ? nbytes : buf_available; 369 size_t cplen = buf_available > nbytes ? nbytes : buf_available;
411 r += cplen; 379 r += cplen;
412 buf += cplen; 380 buf += cplen;
413 nbytes -= cplen; 381 nbytes -= cplen;
414 } 382 }
415 383
384 // maybe perform IO and refill the read buffer
385 // if we read data (read_data == true), make sure not to perform IO,
386 // when a chunk is completed
387 //
388 // if we read a chunk header (read_data == false) it is very important
389 // to not perform IO, if we have previously copied data from readbuf
390 // this ensures we never override non-chunk-header data
416 if(*perform_io && ((read_data && nbytes > 0 && st->max_read - st->read) || (!read_data && r == 0))) { 391 if(*perform_io && ((read_data && nbytes > 0 && st->max_read - st->read) || (!read_data && r == 0))) {
417 if(st->buflen - *st->bufpos > 0) { 392 if(st->buflen - *st->bufpos > 0) {
418 printf("todo: fix, should not happen, remove later\n"); 393 printf("todo: fix, should not happen, remove later\n");
419 } 394 }
420 // fill buffer again 395 // fill buffer again
568 st->read_eof = WS_TRUE; 543 st->read_eof = WS_TRUE;
569 return -1; 544 return -1;
570 } else if(ret > 0) { 545 } else if(ret > 0) {
571 st->max_read = chunklen; 546 st->max_read = chunklen;
572 st->read = 0; 547 st->read = 0;
573 st->remaining_len = chunkbuf_len - ret; 548 int remaining_len = chunkbuf_len - ret;
574 if(st->remaining_len > 0) { 549 if(remaining_len > 0) {
575 //memcpy(st->remaining_buf, st->chunk_buf, HTTP_STREAM_CBUF_SIZE); 550 // we have read more into chunk_buf than the chunk_header
576 *st->bufpos -= st->remaining_len; 551 // it is save to just move bufpos back
577 //st->remaining_pos = ret; 552 *st->bufpos -= remaining_len;
578 } else {
579 st->remaining_pos = 0;
580 } 553 }
581 //st->remaining_len = chunkbuf_len - ret; 554 //st->remaining_len = chunkbuf_len - ret;
582 st->chunk_buf_pos = 0; 555 st->chunk_buf_pos = 0;
583 556
584 if(chunklen == 0) { 557 if(chunklen == 0) {

mercurial