Skip to content

Commit 5e030b0

Browse files
committed
refactor: output hander context start
1 parent ddb615b commit 5e030b0

File tree

1 file changed

+21
-10
lines changed

1 file changed

+21
-10
lines changed

brotli.c

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -356,10 +356,23 @@ static int php_brotli_output_encoding(void)
356356
return BROTLI_G(compression_coding);
357357
}
358358

359+
static int php_brotli_output_handler_context_start(php_brotli_context *ctx)
360+
{
361+
long level = BROTLI_G(output_compression_level);
362+
363+
int result = php_brotli_context_create_encoder_ex(ctx,
364+
level,
365+
BROTLI_DEFAULT_WINDOW,
366+
BROTLI_MODE_GENERIC,
367+
dict,
368+
0);
369+
370+
return result;
371+
}
372+
359373
static int php_brotli_output_handler(void **handler_context,
360374
php_output_context *output_context)
361375
{
362-
long level = BROTLI_DEFAULT_QUALITY;
363376
php_brotli_context *ctx = *(php_brotli_context **)handler_context;
364377

365378
if (!php_brotli_output_encoding()) {
@@ -377,13 +390,8 @@ static int php_brotli_output_handler(void **handler_context,
377390
return FAILURE;
378391
}
379392

380-
level = BROTLI_G(output_compression_level);
381-
if (level < BROTLI_MIN_QUALITY || level > BROTLI_MAX_QUALITY) {
382-
level = BROTLI_DEFAULT_QUALITY;
383-
}
384-
385393
if (output_context->op & PHP_OUTPUT_HANDLER_START) {
386-
if (php_brotli_context_create_encoder(ctx, level, 0, 0) != SUCCESS) {
394+
if (php_brotli_output_handler_context_start(ctx) != SUCCESS) {
387395
return FAILURE;
388396
}
389397
}
@@ -455,8 +463,7 @@ static int php_brotli_output_handler(void **handler_context,
455463
return SUCCESS;
456464
} else {
457465
// restart
458-
if (php_brotli_context_create_encoder(ctx,
459-
level, 0, 0) != SUCCESS) {
466+
if (php_brotli_output_handler_context_start(ctx) != SUCCESS) {
460467
return FAILURE;
461468
}
462469
}
@@ -614,12 +621,16 @@ static int php_brotli_output_conflict(const char *handler_name, size_t handler_n
614621
return SUCCESS;
615622
}
616623

624+
#define STRINGIFY(n) #n
625+
#define TOSTRING(n) STRINGIFY(n)
626+
617627
PHP_INI_BEGIN()
618628
STD_PHP_INI_BOOLEAN("brotli.output_compression", "0",
619629
PHP_INI_ALL, OnUpdate_brotli_output_compression,
620630
output_compression_default,
621631
zend_brotli_globals, brotli_globals)
622-
STD_PHP_INI_ENTRY("brotli.output_compression_level", "-1",
632+
STD_PHP_INI_ENTRY("brotli.output_compression_level",
633+
TOSTRING(BROTLI_DEFAULT_QUALITY),
623634
PHP_INI_ALL, OnUpdateLong, output_compression_level,
624635
zend_brotli_globals, brotli_globals)
625636
PHP_INI_END()

0 commit comments

Comments
 (0)