Skip to content

Commit 2d70c4e

Browse files
committed
fix(headers): avoid double content type, server header
1 parent 644564c commit 2d70c4e

File tree

1 file changed

+22
-10
lines changed

1 file changed

+22
-10
lines changed

Diff for: src/ngx_http_redirectionio_module_filter.c

+22-10
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ static ngx_int_t ngx_http_redirectionio_header_read(ngx_http_request_t *r, ngx_t
88

99
static ngx_int_t ngx_http_redirectionio_header_content_type_read(ngx_http_request_t *r, struct REDIRECTIONIO_HeaderMap **first);
1010

11-
static ngx_int_t ngx_http_redirectionio_header_content_type_write(ngx_http_request_t *r, ngx_table_elt_t *h);
11+
static ngx_int_t ngx_http_redirectionio_header_content_type_write(ngx_http_request_t *r, u_char *value);
1212

1313
static ngx_int_t ngx_http_redirectionio_buffer_read(ngx_buf_t *buffer, struct REDIRECTIONIO_Buffer *output);
1414

@@ -147,6 +147,14 @@ ngx_int_t ngx_http_redirectionio_headers_filter(ngx_http_request_t *r) {
147147
continue;
148148
}
149149

150+
// Some header should not be written into the headers list of the response, and in some special fields instead
151+
if (ngx_strcasecmp((u_char *)header_map->name, (u_char *)"Content-Type") == 0) {
152+
ngx_http_redirectionio_header_content_type_write(r, (u_char *)header_map->value);
153+
header_map = header_map->next;
154+
155+
continue;
156+
}
157+
150158
h = ngx_list_push(&r->headers_out.headers);
151159

152160
if (h == NULL) {
@@ -167,12 +175,13 @@ ngx_int_t ngx_http_redirectionio_headers_filter(ngx_http_request_t *r) {
167175

168176
ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, "http redirectionio add header to response \"%s: %s\"", header_map->name, header_map->value);
169177

178+
// Some headers need to be set in some special fields
170179
if (ngx_strcasecmp((u_char *)header_map->name, (u_char *)"Content-Encoding") == 0) {
171180
r->headers_out.content_encoding = h;
172181
}
173182

174-
if (ngx_strcasecmp((u_char *)header_map->name, (u_char *)"Content-Type") == 0) {
175-
ngx_http_redirectionio_header_content_type_write(r, h);
183+
if (ngx_strcasecmp((u_char *)header_map->name, (u_char *)"Server") == 0) {
184+
r->headers_out.server = h;
176185
}
177186

178187
header_map = header_map->next;
@@ -456,15 +465,18 @@ static ngx_int_t ngx_http_redirectionio_header_content_type_read(ngx_http_reques
456465
return NGX_OK;
457466
}
458467

459-
static ngx_int_t ngx_http_redirectionio_header_content_type_write(ngx_http_request_t *r, ngx_table_elt_t *h) {
468+
static ngx_int_t ngx_http_redirectionio_header_content_type_write(ngx_http_request_t *r, u_char *value) {
460469
u_char *p, *last;
470+
size_t value_len = strlen((const char *)value);
461471

462-
r->headers_out.content_type_len = h->value.len;
463-
r->headers_out.content_type = h->value;
464-
r->headers_out.content_type_lowcase = NULL;
472+
r->headers_out.content_type_len = value_len;
473+
r->headers_out.content_type.len = value_len;
474+
r->headers_out.content_type.data = ngx_pcalloc(r->pool, value_len);
475+
ngx_memcpy(r->headers_out.content_type.data, value, value_len);
465476

466-
for (p = h->value.data; *p; p++) {
477+
r->headers_out.content_type_lowcase = NULL;
467478

479+
for (p = value; *p != '\0'; p++) {
468480
if (*p != ';') {
469481
continue;
470482
}
@@ -483,13 +495,13 @@ static ngx_int_t ngx_http_redirectionio_header_content_type_write(ngx_http_reque
483495

484496
p += 8;
485497

486-
r->headers_out.content_type_len = last - h->value.data;
498+
r->headers_out.content_type_len = last - value;
487499

488500
if (*p == '"') {
489501
p++;
490502
}
491503

492-
last = h->value.data + h->value.len;
504+
last = value + value_len;
493505

494506
if (*(last - 1) == '"') {
495507
last--;

0 commit comments

Comments
 (0)