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.

dmestas 2026-11-21 01:06 UTC small-fixes
Commit 123583b517ea907d45893808a28299576f8cea7b6b5e2dac576f0380fd264c1b
1 file changed +28 -9
+28 -9
--- src/cgi.c
+++ src/cgi.c
@@ -490,14 +490,26 @@
490490
491491
if( g.fullHttpReply ){
492492
if( rangeEnd>0
493493
&& iReplyStatus==200
494494
&& fossil_strcmp(P("REQUEST_METHOD"),"GET")==0
495
+ && fossil_strcmp(zReplyMimeType,"application/x-fossil")!=0
495496
){
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
+ }
498509
}
510
+ if( iReplyStatus!=206 ) rangeStart = 0;
499511
blob_appendf(&hdr, "HTTP/1.0 %d %s\r\n", iReplyStatus, zReplyStatus);
500512
blob_appendf(&hdr, "Date: %s\r\n", cgi_rfc822_datestamp(time(0)));
501513
blob_appendf(&hdr, "Connection: close\r\n");
502514
blob_appendf(&hdr, "X-UA-Compatible: IE=edge\r\n");
503515
}else{
@@ -553,11 +565,11 @@
553565
if( fossil_strcmp(zReplyMimeType,"application/x-fossil")==0 ){
554566
cgi_combine_header_and_body();
555567
blob_compress(&cgiContent[0], &cgiContent[0]);
556568
}
557569
558
- if( is_gzippable() && iReplyStatus!=206 ){
570
+ if( is_gzippable() && iReplyStatus!=206 && iReplyStatus!=416 ){
559571
int i;
560572
gzip_begin(0);
561573
for( i=0; i<2; i++ ){
562574
int size = blob_size(&cgiContent[i]);
563575
if( size>0 ) gzip_step(blob_buffer(&cgiContent[i]), size);
@@ -570,10 +582,12 @@
570582
total_size = blob_size(&cgiContent[0]) + blob_size(&cgiContent[1]);
571583
if( iReplyStatus==206 ){
572584
blob_appendf(&hdr, "Content-Range: bytes %d-%d/%d\r\n",
573585
rangeStart, rangeEnd-1, total_size);
574586
total_size = rangeEnd - rangeStart;
587
+ }else if( iReplyStatus==416 ){
588
+ blob_appendf(&hdr, "Content-Range: bytes */%d\r\n", rangeEnd);
575589
}
576590
blob_appendf(&hdr, "Content-Length: %d\r\n", total_size);
577591
}else{
578592
total_size = 0;
579593
}
@@ -2267,16 +2281,21 @@
22672281
if( zIpAddr!=0 ){
22682282
g.zIpAddr = fossil_strdup(zIpAddr);
22692283
cgi_replace_parameter("REMOTE_ADDR", g.zIpAddr);
22702284
}
22712285
}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
+ }
22782297
}
22792298
}
22802299
}
22812300
cgi_setenv("REQUEST_SCHEME",zScheme);
22822301
cgi_init();
22832302
--- 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

Keyboard Shortcuts

Open search /
Next entry (timeline) j
Previous entry (timeline) k
Open focused entry Enter
Show this help ?
Toggle theme Top nav button