Lines Matching refs:hctx

243 	handler_ctx * const hctx = ck_calloc(1, sizeof(*hctx));  in handler_ctx_init()  local
244 chunkqueue_init(&hctx->in_queue); in handler_ctx_init()
245 hctx->cache_fd = -1; in handler_ctx_init()
246 return hctx; in handler_ctx_init()
249 static void handler_ctx_free(handler_ctx *hctx) { in handler_ctx_free() argument
250 if (hctx->cache_fn) { in handler_ctx_free()
251 unlink(hctx->cache_fn); in handler_ctx_free()
252 free(hctx->cache_fn); in handler_ctx_free()
254 if (-1 != hctx->cache_fd) in handler_ctx_free()
255 close(hctx->cache_fd); in handler_ctx_free()
257 if (hctx->output != &p->tmp_buf) { in handler_ctx_free()
258 buffer_free(hctx->output); in handler_ctx_free()
261 chunkqueue_reset(&hctx->in_queue); in handler_ctx_free()
262 free(hctx); in handler_ctx_free()
334 static void mod_deflate_cache_file_open (handler_ctx * const hctx, const buffer * const fn) { in mod_deflate_cache_file_open() argument
339 hctx->cache_fn = ck_malloc(fnlen+1+LI_ITOSTRING_LENGTH+1); in mod_deflate_cache_file_open()
340 memcpy(hctx->cache_fn, fn->ptr, fnlen); in mod_deflate_cache_file_open()
341 hctx->cache_fn[fnlen] = '.'; in mod_deflate_cache_file_open()
343 li_itostrn(hctx->cache_fn+fnlen+1, LI_ITOSTRING_LENGTH, getpid()); in mod_deflate_cache_file_open()
344 hctx->cache_fn[fnlen+1+ilen] = '\0'; in mod_deflate_cache_file_open()
345 hctx->cache_fd = fdevent_open_cloexec(hctx->cache_fn, 1, O_RDWR|O_CREAT, 0600); in mod_deflate_cache_file_open()
346 if (-1 == hctx->cache_fd) { in mod_deflate_cache_file_open()
347 free(hctx->cache_fn); in mod_deflate_cache_file_open()
348 hctx->cache_fn = NULL; in mod_deflate_cache_file_open()
352 static int mod_deflate_cache_file_finish (request_st * const r, handler_ctx * const hctx, const buf… in mod_deflate_cache_file_finish() argument
353 if (0 != fdevent_rename(hctx->cache_fn, fn->ptr)) in mod_deflate_cache_file_finish()
355 free(hctx->cache_fn); in mod_deflate_cache_file_finish()
356 hctx->cache_fn = NULL; in mod_deflate_cache_file_finish()
358 int rc = http_chunk_append_file_fd(r, fn, hctx->cache_fd, hctx->bytes_out); in mod_deflate_cache_file_finish()
359 hctx->cache_fd = -1; in mod_deflate_cache_file_finish()
860 static int mod_deflate_cache_file_append (handler_ctx * const hctx, const char *out, size_t len) { in mod_deflate_cache_file_append() argument
863 wr = write(hctx->cache_fd, out, len); in mod_deflate_cache_file_append()
868 static int stream_http_chunk_append_mem(handler_ctx * const hctx, const char * const out, size_t le… in stream_http_chunk_append_mem() argument
870 return (-1 == hctx->cache_fd) in stream_http_chunk_append_mem()
871 ? http_chunk_append_mem(hctx->r, out, len) in stream_http_chunk_append_mem()
872 : mod_deflate_cache_file_append(hctx, out, len); in stream_http_chunk_append_mem()
879 static int stream_deflate_init(handler_ctx *hctx) { in stream_deflate_init() argument
880 z_stream * const z = &hctx->u.z; in stream_deflate_init()
886 z->next_out = (unsigned char *)hctx->output->ptr; in stream_deflate_init()
887 z->avail_out = hctx->output->size; in stream_deflate_init()
889 const plugin_data * const p = hctx->plugin_data; in stream_deflate_init()
901 (hctx->compression_type == HTTP_ACCEPT_ENCODING_GZIP) in stream_deflate_init()
912 static int stream_deflate_compress(handler_ctx * const hctx, const unsigned char * const start, off… in stream_deflate_compress() argument
913 z_stream * const z = &(hctx->u.z); in stream_deflate_compress()
919 hctx->bytes_in += st_size; in stream_deflate_compress()
926 len = hctx->output->size - z->avail_out; in stream_deflate_compress()
927 hctx->bytes_out += len; in stream_deflate_compress()
928 if (0 != stream_http_chunk_append_mem(hctx, hctx->output->ptr, len)) in stream_deflate_compress()
930 z->next_out = (unsigned char *)hctx->output->ptr; in stream_deflate_compress()
931 z->avail_out = hctx->output->size; in stream_deflate_compress()
938 static int stream_deflate_flush(handler_ctx * const hctx, int end) { in stream_deflate_flush() argument
939 z_stream * const z = &(hctx->u.z); in stream_deflate_flush()
940 const plugin_data *p = hctx->plugin_data; in stream_deflate_flush()
965 len = hctx->output->size - z->avail_out; in stream_deflate_flush()
967 hctx->bytes_out += len; in stream_deflate_flush()
968 if (0 != stream_http_chunk_append_mem(hctx, hctx->output->ptr, len)) in stream_deflate_flush()
970 z->next_out = (unsigned char *)hctx->output->ptr; in stream_deflate_flush()
971 z->avail_out = hctx->output->size; in stream_deflate_flush()
978 static int stream_deflate_end(handler_ctx *hctx) { in stream_deflate_end() argument
979 z_stream * const z = &(hctx->u.z); in stream_deflate_end()
984 log_error(hctx->r->conf.errh, __FILE__, __LINE__, in stream_deflate_end()
987 log_error(hctx->r->conf.errh, __FILE__, __LINE__, in stream_deflate_end()
998 static int stream_bzip2_init(handler_ctx *hctx) { in stream_bzip2_init() argument
999 bz_stream * const bz = &hctx->u.bz; in stream_bzip2_init()
1007 bz->next_out = hctx->output->ptr; in stream_bzip2_init()
1008 bz->avail_out = hctx->output->size; in stream_bzip2_init()
1010 const plugin_data * const p = hctx->plugin_data; in stream_bzip2_init()
1028 static int stream_bzip2_compress(handler_ctx * const hctx, const unsigned char * const start, off_t… in stream_bzip2_compress() argument
1029 bz_stream * const bz = &(hctx->u.bz); in stream_bzip2_compress()
1034 hctx->bytes_in += st_size; in stream_bzip2_compress()
1041 len = hctx->output->size - bz->avail_out; in stream_bzip2_compress()
1042 hctx->bytes_out += len; in stream_bzip2_compress()
1043 if (0 != stream_http_chunk_append_mem(hctx, hctx->output->ptr, len)) in stream_bzip2_compress()
1045 bz->next_out = hctx->output->ptr; in stream_bzip2_compress()
1046 bz->avail_out = hctx->output->size; in stream_bzip2_compress()
1053 static int stream_bzip2_flush(handler_ctx * const hctx, int end) { in stream_bzip2_flush() argument
1054 bz_stream * const bz = &(hctx->u.bz); in stream_bzip2_flush()
1055 const plugin_data *p = hctx->plugin_data; in stream_bzip2_flush()
1080 len = hctx->output->size - bz->avail_out; in stream_bzip2_flush()
1082 hctx->bytes_out += len; in stream_bzip2_flush()
1083 if (0 != stream_http_chunk_append_mem(hctx, hctx->output->ptr, len)) in stream_bzip2_flush()
1085 bz->next_out = hctx->output->ptr; in stream_bzip2_flush()
1086 bz->avail_out = hctx->output->size; in stream_bzip2_flush()
1093 static int stream_bzip2_end(handler_ctx *hctx) { in stream_bzip2_end() argument
1094 bz_stream * const bz = &(hctx->u.bz); in stream_bzip2_end()
1098 log_error(hctx->r->conf.errh, __FILE__, __LINE__, in stream_bzip2_end()
1108 static int stream_br_init(handler_ctx *hctx) { in stream_br_init() argument
1109 BrotliEncoderState * const br = hctx->u.br = in stream_br_init()
1114 const plugin_data * const p = hctx->plugin_data; in stream_br_init()
1131 else if ((vb = http_header_response_get(hctx->r, HTTP_HEADER_CONTENT_TYPE, in stream_br_init()
1151 static int stream_br_compress(handler_ctx * const hctx, const unsigned char * const start, off_t st… in stream_br_compress() argument
1153 BrotliEncoderState * const br = hctx->u.br; in stream_br_compress()
1154 hctx->bytes_in += st_size; in stream_br_compress()
1165 hctx->bytes_out += (off_t)outsz; in stream_br_compress()
1166 if (0 != stream_http_chunk_append_mem(hctx, (char *)out, outsz)) in stream_br_compress()
1173 static int stream_br_flush(handler_ctx * const hctx, int end) { in stream_br_flush() argument
1175 BrotliEncoderState * const br = hctx->u.br; in stream_br_flush()
1183 hctx->bytes_out += (off_t)outsz; in stream_br_flush()
1184 if (0 != stream_http_chunk_append_mem(hctx, (char *)out, outsz)) in stream_br_flush()
1191 static int stream_br_end(handler_ctx *hctx) { in stream_br_end() argument
1192 BrotliEncoderState * const br = hctx->u.br; in stream_br_end()
1202 static int stream_zstd_init(handler_ctx *hctx) { in stream_zstd_init() argument
1203 ZSTD_CStream * const cctx = hctx->u.cctx = ZSTD_createCStream(); in stream_zstd_init()
1205 hctx->output->used = 0; in stream_zstd_init()
1208 const plugin_data * const p = hctx->plugin_data; in stream_zstd_init()
1239 static int stream_zstd_compress(handler_ctx * const hctx, const unsigned char * const start, off_t … in stream_zstd_compress() argument
1240 ZSTD_CStream * const cctx = hctx->u.cctx; in stream_zstd_compress()
1242 ZSTD_outBuffer zob = { hctx->output->ptr, in stream_zstd_compress()
1243 hctx->output->size, in stream_zstd_compress()
1244 hctx->output->used }; in stream_zstd_compress()
1245 hctx->output->used = 0; in stream_zstd_compress()
1246 hctx->bytes_in += st_size; in stream_zstd_compress()
1255 hctx->bytes_out += (off_t)zob.pos; in stream_zstd_compress()
1256 if (0 != stream_http_chunk_append_mem(hctx, zob.dst, zob.pos)) in stream_zstd_compress()
1260 hctx->output->used = (uint32_t)zob.pos; in stream_zstd_compress()
1264 static int stream_zstd_flush(handler_ctx * const hctx, int end) { in stream_zstd_flush() argument
1265 ZSTD_CStream * const cctx = hctx->u.cctx; in stream_zstd_flush()
1270 ZSTD_outBuffer zob = { hctx->output->ptr, in stream_zstd_flush()
1271 hctx->output->size, in stream_zstd_flush()
1272 hctx->output->used }; in stream_zstd_flush()
1283 hctx->bytes_out += (off_t)zob.pos; in stream_zstd_flush()
1284 if (0 != stream_http_chunk_append_mem(hctx, zob.dst, zob.pos)) in stream_zstd_flush()
1291 static int stream_zstd_end(handler_ctx *hctx) { in stream_zstd_end() argument
1292 ZSTD_CStream * const cctx = hctx->u.cctx; in stream_zstd_end()
1300 static int mod_deflate_stream_init(handler_ctx *hctx) { in mod_deflate_stream_init() argument
1301 switch(hctx->compression_type) { in mod_deflate_stream_init()
1305 return stream_deflate_init(hctx); in mod_deflate_stream_init()
1309 return stream_bzip2_init(hctx); in mod_deflate_stream_init()
1313 return stream_br_init(hctx); in mod_deflate_stream_init()
1317 return stream_zstd_init(hctx); in mod_deflate_stream_init()
1324 static int mod_deflate_compress(handler_ctx * const hctx, const unsigned char * const start, off_t … in mod_deflate_compress() argument
1326 switch(hctx->compression_type) { in mod_deflate_compress()
1330 return stream_deflate_compress(hctx, start, st_size); in mod_deflate_compress()
1334 return stream_bzip2_compress(hctx, start, st_size); in mod_deflate_compress()
1338 return stream_br_compress(hctx, start, st_size); in mod_deflate_compress()
1342 return stream_zstd_compress(hctx, start, st_size); in mod_deflate_compress()
1350 static int mod_deflate_stream_flush(handler_ctx * const hctx, int end) { in mod_deflate_stream_flush() argument
1351 if (0 == hctx->bytes_in) return 0; in mod_deflate_stream_flush()
1352 switch(hctx->compression_type) { in mod_deflate_stream_flush()
1356 return stream_deflate_flush(hctx, end); in mod_deflate_stream_flush()
1360 return stream_bzip2_flush(hctx, end); in mod_deflate_stream_flush()
1364 return stream_br_flush(hctx, end); in mod_deflate_stream_flush()
1368 return stream_zstd_flush(hctx, end); in mod_deflate_stream_flush()
1387 static int mod_deflate_stream_end(handler_ctx *hctx) { in mod_deflate_stream_end() argument
1388 switch(hctx->compression_type) { in mod_deflate_stream_end()
1392 return stream_deflate_end(hctx); in mod_deflate_stream_end()
1396 return stream_bzip2_end(hctx); in mod_deflate_stream_end()
1400 return stream_br_end(hctx); in mod_deflate_stream_end()
1404 return stream_zstd_end(hctx); in mod_deflate_stream_end()
1411 static int deflate_compress_cleanup(request_st * const r, handler_ctx * const hctx) { in deflate_compress_cleanup() argument
1412 int rc = mod_deflate_stream_end(hctx); in deflate_compress_cleanup()
1415 if (0 == rc && hctx->bytes_in < hctx->bytes_out) in deflate_compress_cleanup()
1418 (long long)hctx->bytes_in, (long long)hctx->bytes_out); in deflate_compress_cleanup()
1421 handler_ctx_free(hctx); in deflate_compress_cleanup()
1431 static int mod_deflate_using_libdeflate_err (handler_ctx * const hctx, buffer * const fn, const int… in mod_deflate_using_libdeflate_err() argument
1435 else if (fd == hctx->cache_fd) { in mod_deflate_using_libdeflate_err()
1437 log_perror(hctx->r->conf.errh, __FILE__, __LINE__, "ftruncate"); in mod_deflate_using_libdeflate_err()
1439 log_perror(hctx->r->conf.errh, __FILE__, __LINE__, "lseek"); in mod_deflate_using_libdeflate_err()
1443 log_perror(hctx->r->conf.errh, __FILE__, __LINE__, "unlink"); in mod_deflate_using_libdeflate_err()
1451 static int mod_deflate_using_libdeflate_sm (handler_ctx * const hctx, const plugin_data * const p) in mod_deflate_using_libdeflate_sm() argument
1463 char * const in = hctx->r->write_queue.first->mem->ptr; in mod_deflate_using_libdeflate_sm()
1464 const size_t in_nbytes = (size_t)hctx->bytes_in; in mod_deflate_using_libdeflate_sm()
1465 buffer * const addrb = hctx->output; /*(&p->tmp_buf)*/ in mod_deflate_using_libdeflate_sm()
1467 sz = (hctx->compression_type == HTTP_ACCEPT_ENCODING_GZIP) in mod_deflate_using_libdeflate_sm()
1477 chunkqueue_reset(&hctx->r->write_queue); in mod_deflate_using_libdeflate_sm()
1478 hctx->bytes_out = (off_t)sz; in mod_deflate_using_libdeflate_sm()
1479 if (0 != stream_http_chunk_append_mem(hctx, addrb->ptr, sz)) in mod_deflate_using_libdeflate_sm()
1480 return mod_deflate_using_libdeflate_err(hctx, addrb, hctx->cache_fd); in mod_deflate_using_libdeflate_sm()
1483 http_header_response_set_ptr(hctx->r, HTTP_HEADER_CONTENT_LENGTH, in mod_deflate_using_libdeflate_sm()
1485 buffer_append_int(vb, hctx->bytes_out); in mod_deflate_using_libdeflate_sm()
1502 const handler_ctx * const hctx = src; in mod_deflate_using_libdeflate_setjmp_cb() local
1503 const chunk * const c = hctx->r->write_queue.first; in mod_deflate_using_libdeflate_setjmp_cb()
1505 return (off_t)((hctx->compression_type == HTTP_ACCEPT_ENCODING_GZIP) in mod_deflate_using_libdeflate_setjmp_cb()
1513 static int mod_deflate_using_libdeflate (handler_ctx * const hctx, const plugin_data * const p) in mod_deflate_using_libdeflate() argument
1515 buffer * const fn = hctx->output; /*(&p->tmp_buf)*/ in mod_deflate_using_libdeflate()
1516 int fd = hctx->cache_fd; in mod_deflate_using_libdeflate()
1518 chunkqueue * const cq = &hctx->r->write_queue; in mod_deflate_using_libdeflate()
1531 libdeflate_zlib_compress_bound(NULL, (size_t)hctx->bytes_in); in mod_deflate_using_libdeflate()
1535 log_perror(hctx->r->conf.errh, __FILE__, __LINE__, "ftruncate"); in mod_deflate_using_libdeflate()
1536 return mod_deflate_using_libdeflate_err(hctx, fn, fd); in mod_deflate_using_libdeflate()
1542 log_perror(hctx->r->conf.errh, __FILE__, __LINE__, "mmap"); in mod_deflate_using_libdeflate()
1543 return mod_deflate_using_libdeflate_err(hctx, fn, fd); in mod_deflate_using_libdeflate()
1555 hctx->bytes_out = in mod_deflate_using_libdeflate()
1557 &outparams, hctx, hctx->bytes_in); in mod_deflate_using_libdeflate()
1570 log_perror(hctx->r->conf.errh, __FILE__, __LINE__, "munmap"); in mod_deflate_using_libdeflate()
1572 if (0 == hctx->bytes_out) { in mod_deflate_using_libdeflate()
1573 const chunk * const c = hctx->r->write_queue.first; in mod_deflate_using_libdeflate()
1574 log_error(hctx->r->conf.errh, __FILE__, __LINE__, in mod_deflate_using_libdeflate()
1577 else if (0 != ftruncate(fd, hctx->bytes_out)) in mod_deflate_using_libdeflate()
1578 hctx->bytes_out = 0; in mod_deflate_using_libdeflate()
1580 if (0 == hctx->bytes_out) in mod_deflate_using_libdeflate()
1581 return mod_deflate_using_libdeflate_err(hctx, fn, fd); in mod_deflate_using_libdeflate()
1583 if (fd != hctx->cache_fd) { in mod_deflate_using_libdeflate()
1584 chunkqueue * const cq = &hctx->r->write_queue; in mod_deflate_using_libdeflate()
1586 http_chunk_append_file_fd_range(hctx->r, fn, fd, 0, hctx->bytes_out); in mod_deflate_using_libdeflate()
1591 http_header_response_set_ptr(hctx->r, HTTP_HEADER_CONTENT_LENGTH, in mod_deflate_using_libdeflate()
1593 buffer_append_int(vb, hctx->bytes_out); in mod_deflate_using_libdeflate()
1603 static off_t mod_deflate_file_chunk_no_mmap(request_st * const r, handler_ctx * const hctx, const c… in mod_deflate_file_chunk_no_mmap() argument
1617 if (0 == mod_deflate_compress(hctx, (unsigned char *)p, rd)) in mod_deflate_file_chunk_no_mmap()
1644 static off_t mod_deflate_file_chunk_mmap(request_st * const r, handler_ctx * const hctx, chunk * co… in mod_deflate_file_chunk_mmap() argument
1654 return mod_deflate_file_chunk_no_mmap(r, hctx, c, n); in mod_deflate_file_chunk_mmap()
1659 off_t rc = sys_setjmp_eval3(mod_deflate_file_chunk_setjmp_cb, hctx, p, len); in mod_deflate_file_chunk_mmap()
1674 static off_t mod_deflate_file_chunk(request_st * const r, handler_ctx * const hctx, chunk * const c… in mod_deflate_file_chunk() argument
1682 return mod_deflate_file_chunk_mmap(r, hctx, c, n); in mod_deflate_file_chunk()
1684 return mod_deflate_file_chunk_no_mmap(r, hctx, c, n); in mod_deflate_file_chunk()
1689 static handler_t deflate_compress_response(request_st * const r, handler_ctx * const hctx) { in deflate_compress_response() argument
1698 chunkqueue_append_chunkqueue(&hctx->in_queue, cq); in deflate_compress_response()
1702 max = chunkqueue_length(&hctx->in_queue); in deflate_compress_response()
1712 chunk *c = hctx->in_queue.first; in deflate_compress_response()
1718 if (mod_deflate_compress(hctx, (unsigned char *)c->mem->ptr+c->offset, len) < 0) { in deflate_compress_response()
1726 if ((len = mod_deflate_file_chunk(r, hctx, c, len)) < 0) { in deflate_compress_response()
1738 chunkqueue_mark_written(&hctx->in_queue, len); in deflate_compress_response()
1744 && chunkqueue_is_empty(&hctx->in_queue)); in deflate_compress_response()
1745 if (mod_deflate_stream_flush(hctx, close_stream) < 0) { in deflate_compress_response()
1877 handler_ctx *hctx; in REQUEST_FUNC() local
2071 hctx = handler_ctx_init(); in REQUEST_FUNC()
2072 hctx->plugin_data = p; in REQUEST_FUNC()
2073 hctx->compression_type = compression_type; in REQUEST_FUNC()
2074 hctx->r = r; in REQUEST_FUNC()
2077 hctx->output = &p->tmp_buf; in REQUEST_FUNC()
2079 if (tb) mod_deflate_cache_file_open(hctx, tb); in REQUEST_FUNC()
2089 && (hctx->compression_type == HTTP_ACCEPT_ENCODING_GZIP in REQUEST_FUNC()
2090 || hctx->compression_type == HTTP_ACCEPT_ENCODING_DEFLATE) in REQUEST_FUNC()
2096 hctx->bytes_in = len; in REQUEST_FUNC()
2097 if (mod_deflate_using_libdeflate(hctx, p)) { in REQUEST_FUNC()
2098 if (NULL == tb || 0 == mod_deflate_cache_file_finish(r, hctx, tb)) in REQUEST_FUNC()
2099 mod_deflate_note_ratio(r, hctx->bytes_out, hctx->bytes_in); in REQUEST_FUNC()
2102 handler_ctx_free(hctx); in REQUEST_FUNC()
2105 hctx->bytes_in = hctx->bytes_out = 0; in REQUEST_FUNC()
2111 && (hctx->compression_type == HTTP_ACCEPT_ENCODING_GZIP in REQUEST_FUNC()
2112 || hctx->compression_type == HTTP_ACCEPT_ENCODING_DEFLATE) in REQUEST_FUNC()
2117 hctx->bytes_in = len; in REQUEST_FUNC()
2118 if (mod_deflate_using_libdeflate_sm(hctx, p)) { in REQUEST_FUNC()
2119 if (NULL == tb || 0 == mod_deflate_cache_file_finish(r, hctx, tb)) in REQUEST_FUNC()
2120 mod_deflate_note_ratio(r, hctx->bytes_out, hctx->bytes_in); in REQUEST_FUNC()
2123 handler_ctx_free(hctx); in REQUEST_FUNC()
2126 hctx->bytes_in = hctx->bytes_out = 0; in REQUEST_FUNC()
2130 if (0 != mod_deflate_stream_init(hctx)) { in REQUEST_FUNC()
2132 handler_ctx_free(hctx); in REQUEST_FUNC()
2146 && (hctx->compression_type & HTTP_ACCEPT_ENCODING_BR) in REQUEST_FUNC()
2148 BrotliEncoderSetParameter(hctx->u.br, BROTLI_PARAM_SIZE_HINT, (uint32_t)len); in REQUEST_FUNC()
2154 r->plugin_ctx[p->id] = hctx; in REQUEST_FUNC()
2156 rc = deflate_compress_response(r, hctx); in REQUEST_FUNC()
2161 force_assert(-1 == hctx->cache_fd || NULL != tb); in REQUEST_FUNC()
2163 if (-1 == hctx->cache_fd in REQUEST_FUNC()
2164 || 0 == mod_deflate_cache_file_finish(r, hctx, tb)) { in REQUEST_FUNC()
2165 mod_deflate_note_ratio(r, hctx->bytes_out, hctx->bytes_in); in REQUEST_FUNC()
2172 if (deflate_compress_cleanup(r, hctx) < 0) return HANDLER_ERROR; in REQUEST_FUNC()
2178 handler_ctx *hctx = r->plugin_ctx[p->id]; in mod_deflate_cleanup() local
2180 if (NULL != hctx) { in mod_deflate_cleanup()
2182 deflate_compress_cleanup(r, hctx); in mod_deflate_cleanup()