Fossil SCM
Fix Range replies in cgi_reply(): answer 416 when the range starts past the end, stop the range at the end, and leave replies other than 206 whole. Parse the numbers with strtoll() so a value too large for an int is refused, not wrapped.
Commit
123583b517ea907d45893808a28299576f8cea7b6b5e2dac576f0380fd264c1b
Parent
210e9def6f68dd3…
1 file changed
+28
-9
+28
-9
| --- src/cgi.c | ||
| +++ src/cgi.c | ||
| @@ -490,14 +490,26 @@ | ||
| 490 | 490 | |
| 491 | 491 | if( g.fullHttpReply ){ |
| 492 | 492 | if( rangeEnd>0 |
| 493 | 493 | && iReplyStatus==200 |
| 494 | 494 | && fossil_strcmp(P("REQUEST_METHOD"),"GET")==0 |
| 495 | + && fossil_strcmp(zReplyMimeType,"application/x-fossil")!=0 | |
| 495 | 496 | ){ |
| 496 | - iReplyStatus = 206; | |
| 497 | - zReplyStatus = "Partial Content"; | |
| 497 | + int nContent = blob_size(&cgiContent[0]) + blob_size(&cgiContent[1]); | |
| 498 | + if( rangeStart>=nContent ){ | |
| 499 | + iReplyStatus = 416; | |
| 500 | + zReplyStatus = "Range Not Satisfiable"; | |
| 501 | + rangeEnd = nContent; | |
| 502 | + blob_reset(&cgiContent[0]); | |
| 503 | + blob_reset(&cgiContent[1]); | |
| 504 | + }else{ | |
| 505 | + iReplyStatus = 206; | |
| 506 | + zReplyStatus = "Partial Content"; | |
| 507 | + if( rangeEnd>nContent ) rangeEnd = nContent; | |
| 508 | + } | |
| 498 | 509 | } |
| 510 | + if( iReplyStatus!=206 ) rangeStart = 0; | |
| 499 | 511 | blob_appendf(&hdr, "HTTP/1.0 %d %s\r\n", iReplyStatus, zReplyStatus); |
| 500 | 512 | blob_appendf(&hdr, "Date: %s\r\n", cgi_rfc822_datestamp(time(0))); |
| 501 | 513 | blob_appendf(&hdr, "Connection: close\r\n"); |
| 502 | 514 | blob_appendf(&hdr, "X-UA-Compatible: IE=edge\r\n"); |
| 503 | 515 | }else{ |
| @@ -553,11 +565,11 @@ | ||
| 553 | 565 | if( fossil_strcmp(zReplyMimeType,"application/x-fossil")==0 ){ |
| 554 | 566 | cgi_combine_header_and_body(); |
| 555 | 567 | blob_compress(&cgiContent[0], &cgiContent[0]); |
| 556 | 568 | } |
| 557 | 569 | |
| 558 | - if( is_gzippable() && iReplyStatus!=206 ){ | |
| 570 | + if( is_gzippable() && iReplyStatus!=206 && iReplyStatus!=416 ){ | |
| 559 | 571 | int i; |
| 560 | 572 | gzip_begin(0); |
| 561 | 573 | for( i=0; i<2; i++ ){ |
| 562 | 574 | int size = blob_size(&cgiContent[i]); |
| 563 | 575 | if( size>0 ) gzip_step(blob_buffer(&cgiContent[i]), size); |
| @@ -570,10 +582,12 @@ | ||
| 570 | 582 | total_size = blob_size(&cgiContent[0]) + blob_size(&cgiContent[1]); |
| 571 | 583 | if( iReplyStatus==206 ){ |
| 572 | 584 | blob_appendf(&hdr, "Content-Range: bytes %d-%d/%d\r\n", |
| 573 | 585 | rangeStart, rangeEnd-1, total_size); |
| 574 | 586 | total_size = rangeEnd - rangeStart; |
| 587 | + }else if( iReplyStatus==416 ){ | |
| 588 | + blob_appendf(&hdr, "Content-Range: bytes */%d\r\n", rangeEnd); | |
| 575 | 589 | } |
| 576 | 590 | blob_appendf(&hdr, "Content-Length: %d\r\n", total_size); |
| 577 | 591 | }else{ |
| 578 | 592 | total_size = 0; |
| 579 | 593 | } |
| @@ -2267,16 +2281,21 @@ | ||
| 2267 | 2281 | if( zIpAddr!=0 ){ |
| 2268 | 2282 | g.zIpAddr = fossil_strdup(zIpAddr); |
| 2269 | 2283 | cgi_replace_parameter("REMOTE_ADDR", g.zIpAddr); |
| 2270 | 2284 | } |
| 2271 | 2285 | }else if( fossil_strcmp(zFieldName,"range:")==0 ){ |
| 2272 | - int x1 = 0; | |
| 2273 | - int x2 = 0; | |
| 2274 | - if( sscanf(zVal,"bytes=%d-%d",&x1,&x2)==2 && x1>=0 && x1<=x2 | |
| 2275 | - && x2<0x7fffffff ){ | |
| 2276 | - rangeStart = x1; | |
| 2277 | - rangeEnd = x2+1; | |
| 2286 | + sqlite3_int64 x1, x2; | |
| 2287 | + char *zEnd; | |
| 2288 | + if( strncmp(zVal,"bytes=",6)==0 && fossil_isdigit(zVal[6]) ){ | |
| 2289 | + x1 = strtoll(&zVal[6], &zEnd, 10); | |
| 2290 | + if( zEnd[0]=='-' && fossil_isdigit(zEnd[1]) ){ | |
| 2291 | + x2 = strtoll(&zEnd[1], 0, 10); | |
| 2292 | + if( x1<=x2 && x2<0x7fffffff ){ | |
| 2293 | + rangeStart = (int)x1; | |
| 2294 | + rangeEnd = (int)x2 + 1; | |
| 2295 | + } | |
| 2296 | + } | |
| 2278 | 2297 | } |
| 2279 | 2298 | } |
| 2280 | 2299 | } |
| 2281 | 2300 | cgi_setenv("REQUEST_SCHEME",zScheme); |
| 2282 | 2301 | cgi_init(); |
| 2283 | 2302 |
| --- src/cgi.c | |
| +++ src/cgi.c | |
| @@ -490,14 +490,26 @@ | |
| 490 | |
| 491 | if( g.fullHttpReply ){ |
| 492 | if( rangeEnd>0 |
| 493 | && iReplyStatus==200 |
| 494 | && fossil_strcmp(P("REQUEST_METHOD"),"GET")==0 |
| 495 | ){ |
| 496 | iReplyStatus = 206; |
| 497 | zReplyStatus = "Partial Content"; |
| 498 | } |
| 499 | blob_appendf(&hdr, "HTTP/1.0 %d %s\r\n", iReplyStatus, zReplyStatus); |
| 500 | blob_appendf(&hdr, "Date: %s\r\n", cgi_rfc822_datestamp(time(0))); |
| 501 | blob_appendf(&hdr, "Connection: close\r\n"); |
| 502 | blob_appendf(&hdr, "X-UA-Compatible: IE=edge\r\n"); |
| 503 | }else{ |
| @@ -553,11 +565,11 @@ | |
| 553 | if( fossil_strcmp(zReplyMimeType,"application/x-fossil")==0 ){ |
| 554 | cgi_combine_header_and_body(); |
| 555 | blob_compress(&cgiContent[0], &cgiContent[0]); |
| 556 | } |
| 557 | |
| 558 | if( is_gzippable() && iReplyStatus!=206 ){ |
| 559 | int i; |
| 560 | gzip_begin(0); |
| 561 | for( i=0; i<2; i++ ){ |
| 562 | int size = blob_size(&cgiContent[i]); |
| 563 | if( size>0 ) gzip_step(blob_buffer(&cgiContent[i]), size); |
| @@ -570,10 +582,12 @@ | |
| 570 | total_size = blob_size(&cgiContent[0]) + blob_size(&cgiContent[1]); |
| 571 | if( iReplyStatus==206 ){ |
| 572 | blob_appendf(&hdr, "Content-Range: bytes %d-%d/%d\r\n", |
| 573 | rangeStart, rangeEnd-1, total_size); |
| 574 | total_size = rangeEnd - rangeStart; |
| 575 | } |
| 576 | blob_appendf(&hdr, "Content-Length: %d\r\n", total_size); |
| 577 | }else{ |
| 578 | total_size = 0; |
| 579 | } |
| @@ -2267,16 +2281,21 @@ | |
| 2267 | if( zIpAddr!=0 ){ |
| 2268 | g.zIpAddr = fossil_strdup(zIpAddr); |
| 2269 | cgi_replace_parameter("REMOTE_ADDR", g.zIpAddr); |
| 2270 | } |
| 2271 | }else if( fossil_strcmp(zFieldName,"range:")==0 ){ |
| 2272 | int x1 = 0; |
| 2273 | int x2 = 0; |
| 2274 | if( sscanf(zVal,"bytes=%d-%d",&x1,&x2)==2 && x1>=0 && x1<=x2 |
| 2275 | && x2<0x7fffffff ){ |
| 2276 | rangeStart = x1; |
| 2277 | rangeEnd = x2+1; |
| 2278 | } |
| 2279 | } |
| 2280 | } |
| 2281 | cgi_setenv("REQUEST_SCHEME",zScheme); |
| 2282 | cgi_init(); |
| 2283 |
| --- src/cgi.c | |
| +++ src/cgi.c | |
| @@ -490,14 +490,26 @@ | |
| 490 | |
| 491 | if( g.fullHttpReply ){ |
| 492 | if( rangeEnd>0 |
| 493 | && iReplyStatus==200 |
| 494 | && fossil_strcmp(P("REQUEST_METHOD"),"GET")==0 |
| 495 | && fossil_strcmp(zReplyMimeType,"application/x-fossil")!=0 |
| 496 | ){ |
| 497 | int nContent = blob_size(&cgiContent[0]) + blob_size(&cgiContent[1]); |
| 498 | if( rangeStart>=nContent ){ |
| 499 | iReplyStatus = 416; |
| 500 | zReplyStatus = "Range Not Satisfiable"; |
| 501 | rangeEnd = nContent; |
| 502 | blob_reset(&cgiContent[0]); |
| 503 | blob_reset(&cgiContent[1]); |
| 504 | }else{ |
| 505 | iReplyStatus = 206; |
| 506 | zReplyStatus = "Partial Content"; |
| 507 | if( rangeEnd>nContent ) rangeEnd = nContent; |
| 508 | } |
| 509 | } |
| 510 | if( iReplyStatus!=206 ) rangeStart = 0; |
| 511 | blob_appendf(&hdr, "HTTP/1.0 %d %s\r\n", iReplyStatus, zReplyStatus); |
| 512 | blob_appendf(&hdr, "Date: %s\r\n", cgi_rfc822_datestamp(time(0))); |
| 513 | blob_appendf(&hdr, "Connection: close\r\n"); |
| 514 | blob_appendf(&hdr, "X-UA-Compatible: IE=edge\r\n"); |
| 515 | }else{ |
| @@ -553,11 +565,11 @@ | |
| 565 | if( fossil_strcmp(zReplyMimeType,"application/x-fossil")==0 ){ |
| 566 | cgi_combine_header_and_body(); |
| 567 | blob_compress(&cgiContent[0], &cgiContent[0]); |
| 568 | } |
| 569 | |
| 570 | if( is_gzippable() && iReplyStatus!=206 && iReplyStatus!=416 ){ |
| 571 | int i; |
| 572 | gzip_begin(0); |
| 573 | for( i=0; i<2; i++ ){ |
| 574 | int size = blob_size(&cgiContent[i]); |
| 575 | if( size>0 ) gzip_step(blob_buffer(&cgiContent[i]), size); |
| @@ -570,10 +582,12 @@ | |
| 582 | total_size = blob_size(&cgiContent[0]) + blob_size(&cgiContent[1]); |
| 583 | if( iReplyStatus==206 ){ |
| 584 | blob_appendf(&hdr, "Content-Range: bytes %d-%d/%d\r\n", |
| 585 | rangeStart, rangeEnd-1, total_size); |
| 586 | total_size = rangeEnd - rangeStart; |
| 587 | }else if( iReplyStatus==416 ){ |
| 588 | blob_appendf(&hdr, "Content-Range: bytes */%d\r\n", rangeEnd); |
| 589 | } |
| 590 | blob_appendf(&hdr, "Content-Length: %d\r\n", total_size); |
| 591 | }else{ |
| 592 | total_size = 0; |
| 593 | } |
| @@ -2267,16 +2281,21 @@ | |
| 2281 | if( zIpAddr!=0 ){ |
| 2282 | g.zIpAddr = fossil_strdup(zIpAddr); |
| 2283 | cgi_replace_parameter("REMOTE_ADDR", g.zIpAddr); |
| 2284 | } |
| 2285 | }else if( fossil_strcmp(zFieldName,"range:")==0 ){ |
| 2286 | sqlite3_int64 x1, x2; |
| 2287 | char *zEnd; |
| 2288 | if( strncmp(zVal,"bytes=",6)==0 && fossil_isdigit(zVal[6]) ){ |
| 2289 | x1 = strtoll(&zVal[6], &zEnd, 10); |
| 2290 | if( zEnd[0]=='-' && fossil_isdigit(zEnd[1]) ){ |
| 2291 | x2 = strtoll(&zEnd[1], 0, 10); |
| 2292 | if( x1<=x2 && x2<0x7fffffff ){ |
| 2293 | rangeStart = (int)x1; |
| 2294 | rangeEnd = (int)x2 + 1; |
| 2295 | } |
| 2296 | } |
| 2297 | } |
| 2298 | } |
| 2299 | } |
| 2300 | cgi_setenv("REQUEST_SCHEME",zScheme); |
| 2301 | cgi_init(); |
| 2302 |