Fossil SCM

Replace cgi.c:is_gzippable() with is_compressible() and add support for brotli compression to its decision-making process. There's still a lot to do before we can actually emit brotli compression.

stephan 2025-03-31 21:04 brotli-compress
Commit 22e7b78dce5123ef99e4c5e555d4ec40869c00fb4d2483f992e14b2533367b42
1 file changed +40 -15
+40 -15
--- src/cgi.c
+++ src/cgi.c
@@ -325,17 +325,35 @@
325325
"Set-Cookie: %s=%t; Path=%s; HttpOnly; %s\r\n",
326326
zName, zValue, zPath, zSecure);
327327
}
328328
}
329329
330
-
331330
/*
332
-** Return true if the response should be sent with Content-Encoding: gzip.
331
+** Values for use with is_compressible().
333332
*/
334
-static int is_gzippable(void){
333
+#define COMPRESSIBLE_ZLIB 1
334
+#define COMPRESSIBLE_BROTLI 2
335
+/*
336
+** Return true if the response should be sent with Content-Encoding: X, where
337
+** X corresponds to the type argument (one of the COMPRESSIBLE_xyz macros).
338
+*/
339
+static int is_compressible(int type){
335340
if( g.fNoHttpCompress ) return 0;
336
- if( strstr(PD("HTTP_ACCEPT_ENCODING", ""), "gzip")==0 ) return 0;
341
+ switch( type ){
342
+ case COMPRESSIBLE_ZLIB:
343
+ if( strstr(PD("HTTP_ACCEPT_ENCODING", ""), "gzip")==0 ) return 0;
344
+ break;
345
+ case COMPRESSIBLE_BROTLI:
346
+#ifdef HAVE_BROTLIENCODERCOMPRESS
347
+ if( strstr(PD("HTTP_ACCEPT_ENCODING", ""), "br")==0 ) return 0;
348
+ break;
349
+#else
350
+ return 0;
351
+#endif
352
+ default:
353
+ break;
354
+ }
337355
/* Maintenance note: this oddball structure is intended to make
338356
** adding new mimetypes to this list less of a performance hit than
339357
** doing a strcmp/glob over a growing set of compressible types. */
340358
switch(zContentType ? *zContentType : 0){
341359
case (int)'a':
@@ -548,21 +566,28 @@
548566
if( fossil_strcmp(zContentType,"application/x-fossil")==0 ){
549567
cgi_combine_header_and_body();
550568
blob_compress(&cgiContent[0], &cgiContent[0]);
551569
}
552570
553
- if( is_gzippable() && iReplyStatus!=206 ){
554
- int i;
555
- gzip_begin(0);
556
- for( i=0; i<2; i++ ){
557
- int size = blob_size(&cgiContent[i]);
558
- if( size>0 ) gzip_step(blob_buffer(&cgiContent[i]), size);
559
- blob_reset(&cgiContent[i]);
560
- }
561
- gzip_finish(&cgiContent[0]);
562
- blob_appendf(&hdr, "Content-Encoding: gzip\r\n");
563
- blob_appendf(&hdr, "Vary: Accept-Encoding\r\n");
571
+ if( iReplyStatus!=206 ){
572
+ if( is_compressible(COMPRESSIBLE_ZLIB) ){
573
+ int i;
574
+ gzip_begin(0);
575
+ for( i=0; i<2; i++ ){
576
+ int size = blob_size(&cgiContent[i]);
577
+ if( size>0 ) gzip_step(blob_buffer(&cgiContent[i]), size);
578
+ blob_reset(&cgiContent[i]);
579
+ }
580
+ gzip_finish(&cgiContent[0]);
581
+ blob_appendf(&hdr, "Content-Encoding: gzip\r\n");
582
+ blob_appendf(&hdr, "Vary: Accept-Encoding\r\n");
583
+ }
584
+#ifdef HAVE_BROTLIENCODERCOMPRESS
585
+ else if( is_compressible(COMPRESSIBLE_BROTLI) ){
586
+ /* TODO */
587
+ }
588
+#endif
564589
}
565590
total_size = blob_size(&cgiContent[0]) + blob_size(&cgiContent[1]);
566591
if( iReplyStatus==206 ){
567592
blob_appendf(&hdr, "Content-Range: bytes %d-%d/%d\r\n",
568593
rangeStart, rangeEnd-1, total_size);
569594
--- src/cgi.c
+++ src/cgi.c
@@ -325,17 +325,35 @@
325 "Set-Cookie: %s=%t; Path=%s; HttpOnly; %s\r\n",
326 zName, zValue, zPath, zSecure);
327 }
328 }
329
330
331 /*
332 ** Return true if the response should be sent with Content-Encoding: gzip.
333 */
334 static int is_gzippable(void){
 
 
 
 
 
 
335 if( g.fNoHttpCompress ) return 0;
336 if( strstr(PD("HTTP_ACCEPT_ENCODING", ""), "gzip")==0 ) return 0;
 
 
 
 
 
 
 
 
 
 
 
 
 
337 /* Maintenance note: this oddball structure is intended to make
338 ** adding new mimetypes to this list less of a performance hit than
339 ** doing a strcmp/glob over a growing set of compressible types. */
340 switch(zContentType ? *zContentType : 0){
341 case (int)'a':
@@ -548,21 +566,28 @@
548 if( fossil_strcmp(zContentType,"application/x-fossil")==0 ){
549 cgi_combine_header_and_body();
550 blob_compress(&cgiContent[0], &cgiContent[0]);
551 }
552
553 if( is_gzippable() && iReplyStatus!=206 ){
554 int i;
555 gzip_begin(0);
556 for( i=0; i<2; i++ ){
557 int size = blob_size(&cgiContent[i]);
558 if( size>0 ) gzip_step(blob_buffer(&cgiContent[i]), size);
559 blob_reset(&cgiContent[i]);
560 }
561 gzip_finish(&cgiContent[0]);
562 blob_appendf(&hdr, "Content-Encoding: gzip\r\n");
563 blob_appendf(&hdr, "Vary: Accept-Encoding\r\n");
 
 
 
 
 
 
 
564 }
565 total_size = blob_size(&cgiContent[0]) + blob_size(&cgiContent[1]);
566 if( iReplyStatus==206 ){
567 blob_appendf(&hdr, "Content-Range: bytes %d-%d/%d\r\n",
568 rangeStart, rangeEnd-1, total_size);
569
--- src/cgi.c
+++ src/cgi.c
@@ -325,17 +325,35 @@
325 "Set-Cookie: %s=%t; Path=%s; HttpOnly; %s\r\n",
326 zName, zValue, zPath, zSecure);
327 }
328 }
329
 
330 /*
331 ** Values for use with is_compressible().
332 */
333 #define COMPRESSIBLE_ZLIB 1
334 #define COMPRESSIBLE_BROTLI 2
335 /*
336 ** Return true if the response should be sent with Content-Encoding: X, where
337 ** X corresponds to the type argument (one of the COMPRESSIBLE_xyz macros).
338 */
339 static int is_compressible(int type){
340 if( g.fNoHttpCompress ) return 0;
341 switch( type ){
342 case COMPRESSIBLE_ZLIB:
343 if( strstr(PD("HTTP_ACCEPT_ENCODING", ""), "gzip")==0 ) return 0;
344 break;
345 case COMPRESSIBLE_BROTLI:
346 #ifdef HAVE_BROTLIENCODERCOMPRESS
347 if( strstr(PD("HTTP_ACCEPT_ENCODING", ""), "br")==0 ) return 0;
348 break;
349 #else
350 return 0;
351 #endif
352 default:
353 break;
354 }
355 /* Maintenance note: this oddball structure is intended to make
356 ** adding new mimetypes to this list less of a performance hit than
357 ** doing a strcmp/glob over a growing set of compressible types. */
358 switch(zContentType ? *zContentType : 0){
359 case (int)'a':
@@ -548,21 +566,28 @@
566 if( fossil_strcmp(zContentType,"application/x-fossil")==0 ){
567 cgi_combine_header_and_body();
568 blob_compress(&cgiContent[0], &cgiContent[0]);
569 }
570
571 if( iReplyStatus!=206 ){
572 if( is_compressible(COMPRESSIBLE_ZLIB) ){
573 int i;
574 gzip_begin(0);
575 for( i=0; i<2; i++ ){
576 int size = blob_size(&cgiContent[i]);
577 if( size>0 ) gzip_step(blob_buffer(&cgiContent[i]), size);
578 blob_reset(&cgiContent[i]);
579 }
580 gzip_finish(&cgiContent[0]);
581 blob_appendf(&hdr, "Content-Encoding: gzip\r\n");
582 blob_appendf(&hdr, "Vary: Accept-Encoding\r\n");
583 }
584 #ifdef HAVE_BROTLIENCODERCOMPRESS
585 else if( is_compressible(COMPRESSIBLE_BROTLI) ){
586 /* TODO */
587 }
588 #endif
589 }
590 total_size = blob_size(&cgiContent[0]) + blob_size(&cgiContent[1]);
591 if( iReplyStatus==206 ){
592 blob_appendf(&hdr, "Content-Range: bytes %d-%d/%d\r\n",
593 rangeStart, rangeEnd-1, total_size);
594

Keyboard Shortcuts

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