@@ -162,6 +162,79 @@ nxt_http_comp_bound(size_t size)
162162}
163163
164164
165+ nxt_int_t
166+ nxt_http_comp_compress_app_response (nxt_http_request_t * r , nxt_buf_t * * b )
167+ {
168+ bool last ;
169+ size_t buf_len , in_len ;
170+ ssize_t cbytes ;
171+ // uint8_t *buf;
172+ // nxt_buf_t *buf;
173+ nxt_http_comp_ctx_t * ctx = & compressor_ctx ;
174+
175+ if (ctx -> idx == NXT_HTTP_COMP_SCHEME_IDENTITY ) {
176+ return NXT_OK ;
177+ }
178+
179+ if ((* b )-> mem .pos == NULL ) {
180+ return NXT_OK ;
181+ }
182+
183+ last = !(* b )-> next || nxt_buf_is_last (* b );
184+
185+ in_len = (* b )-> mem .free - (* b )-> mem .pos ;
186+ buf_len = nxt_http_comp_bound (in_len );
187+
188+ #if 1
189+ if (buf_len > (size_t )nxt_buf_mem_size (& (* b )-> mem )) {
190+ return NXT_OK ;
191+ }
192+
193+ uint8_t * buf = nxt_malloc (buf_len );
194+
195+ cbytes = nxt_http_comp_compress (buf , buf_len , (* b )-> mem .pos , in_len , last );
196+ if (cbytes == -1 ) {
197+ nxt_free (buf );
198+ return NXT_ERROR ;
199+ }
200+
201+ (* b )-> mem .free = nxt_cpymem ((* b )-> mem .pos , buf , cbytes );
202+
203+ nxt_free (buf );
204+ #else
205+ /*
206+ * While this produces correct compressed output, the router
207+ * process then crashes doing some shutdown/cleanup work.
208+ *
209+ * My best guess as to why this happens is due to our *new* b
210+ * not being allocated from the same memory pool as the original.
211+ */
212+ nxt_buf_t * buf = nxt_buf_mem_alloc (r -> mem_pool , buf_len , 0 );
213+ if (nxt_slow_path (buf == NULL )) {
214+ return NXT_ERROR ;
215+ }
216+
217+ nxt_memcpy (buf , * b , offsetof(nxt_buf_t , mem ));
218+
219+ cbytes = nxt_http_comp_compress (buf -> mem .start , buf_len ,
220+ (* b )-> mem .pos , in_len , last );
221+ printf ("%s: cbytes = %ld\n" , __func__ , cbytes );
222+ if (cbytes == -1 ) {
223+ return NXT_ERROR ;
224+ }
225+
226+ buf -> mem .free += cbytes ;
227+ /* Seemingly *not* the memory ppol b was allocated from... */
228+ // nxt_buf_free(r->mem_pool, *b);
229+ * b = buf ;
230+
231+ printf ("%s: buf [%p] *b [%p]\n" , __func__ , buf , * b );
232+ #endif
233+
234+ return NXT_OK ;
235+ }
236+
237+
165238nxt_int_t
166239nxt_http_comp_compress_static_response (nxt_task_t * task , nxt_file_t * * f ,
167240 nxt_file_info_t * fi ,
@@ -395,6 +468,26 @@ nxt_http_comp_set_header(nxt_http_request_t *r, nxt_uint_t comp_idx)
395468 f -> value = token -> start ;
396469 f -> value_length = token -> length ;
397470
471+ r -> resp .content_length = NULL ;
472+ r -> resp .content_length_n = -1 ;
473+
474+ if (r -> resp .mime_type == NULL ) {
475+ nxt_http_field_t * f ;
476+
477+ /*
478+ * As per RFC 2616 section 4.4 item 3, you should not send
479+ * Content-Length when a Transfer-Encoding header is present.
480+ */
481+ nxt_list_each (f , r -> resp .fields ) {
482+ if (nxt_strcasecmp (f -> name ,
483+ (const u_char * )"Content-Length" ) == 0 )
484+ {
485+ f -> skip = true;
486+ break ;
487+ }
488+ } nxt_list_loop ;
489+ }
490+
398491 return NXT_OK ;
399492}
400493
0 commit comments