tinywebd

changeset 4:9e054c002489

fixed the header parsing bugs
author John Tsiombikas <nuclear@member.fsf.org>
date Thu, 16 Apr 2015 17:34:15 +0300
parents 852a745503cf
children def49a046566
files src/http.c src/main.c
diffstat 2 files changed, 19 insertions(+), 19 deletions(-) [+]
line diff
     1.1 --- a/src/http.c	Thu Apr 16 15:20:16 2015 +0300
     1.2 +++ b/src/http.c	Thu Apr 16 17:34:15 2015 +0300
     1.3 @@ -146,11 +146,12 @@
     1.4  
     1.5  	startln = buf;
     1.6  	endln = buf;
     1.7 -	for(i=1; i<hdr->body_offset; i++) {
     1.8 +	for(i=1; i<hdr->body_offset - 2; i++) {
     1.9  		if(buf[i] == '\n' && buf[i - 1] == '\r') {
    1.10  			int linesz;
    1.11 +
    1.12  			endln = buf + i - 1;
    1.13 -			linesz = endln - startln - 1;
    1.14 +			linesz = endln - startln;
    1.15  
    1.16  			if(startln > buf) {	/* skip first line */
    1.17  				int idx = hdr->num_hdrfields++;
     2.1 --- a/src/main.c	Thu Apr 16 15:20:16 2015 +0300
     2.2 +++ b/src/main.c	Thu Apr 16 17:34:15 2015 +0300
     2.3 @@ -99,6 +99,7 @@
     2.4  				c = c->next;
     2.5  			}
     2.6  		}
     2.7 +		clist = dummy.next;
     2.8  	}
     2.9  
    2.10  	return 0;	/* unreachable */
    2.11 @@ -168,26 +169,24 @@
    2.12  	int rdsz, status;
    2.13  
    2.14  	while((rdsz = recv(c->s, buf, sizeof buf, 0)) > 0) {
    2.15 -		if(c->rcvbuf) {
    2.16 -			char *newbuf;
    2.17 -			int newsz = c->bufsz + rdsz;
    2.18 -			if(newsz > MAX_REQ_LENGTH) {
    2.19 -				respond_error(c, 413);
    2.20 -				return -1;
    2.21 -			}
    2.22 +		char *newbuf;
    2.23 +		int newsz = c->bufsz + rdsz;
    2.24 +		if(newsz > MAX_REQ_LENGTH) {
    2.25 +			respond_error(c, 413);
    2.26 +			return -1;
    2.27 +		}
    2.28  
    2.29 -			if(!(newbuf = realloc(buf, newsz + 1))) {
    2.30 -				fprintf(stderr, "failed to allocate %d byte buffer\n", newsz);
    2.31 -				respond_error(c, 503);
    2.32 -				return -1;
    2.33 -			}
    2.34 +		if(!(newbuf = realloc(c->rcvbuf, newsz + 1))) {
    2.35 +			fprintf(stderr, "failed to allocate %d byte buffer\n", newsz);
    2.36 +			respond_error(c, 503);
    2.37 +			return -1;
    2.38 +		}
    2.39  
    2.40 -			memcpy(newbuf + c->bufsz, buf, rdsz);
    2.41 -			newbuf[newsz] = 0;
    2.42 +		memcpy(newbuf + c->bufsz, buf, rdsz);
    2.43 +		newbuf[newsz] = 0;
    2.44  
    2.45 -			c->rcvbuf = newbuf;
    2.46 -			c->bufsz = newsz;
    2.47 -		}
    2.48 +		c->rcvbuf = newbuf;
    2.49 +		c->bufsz = newsz;
    2.50  	}
    2.51  
    2.52  	if((status = http_parse_header(&hdr, c->rcvbuf, c->bufsz)) != HTTP_HDR_OK) {