Skip to content

Commit 1a76866

Browse files
committed
feat(filter): support writing content type header to the response
1 parent 67d5b13 commit 1a76866

File tree

1 file changed

+54
-7
lines changed

1 file changed

+54
-7
lines changed

src/ngx_http_redirectionio_module_filter.c

Lines changed: 54 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ 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);
12+
1113
static ngx_int_t ngx_http_redirectionio_buffer_read(ngx_buf_t *buffer, struct REDIRECTIONIO_Buffer *output);
1214

1315
ngx_int_t ngx_http_redirectionio_match_on_response_status_header_filter(ngx_http_request_t *r) {
@@ -145,13 +147,6 @@ ngx_int_t ngx_http_redirectionio_headers_filter(ngx_http_request_t *r) {
145147
continue;
146148
}
147149

148-
// Handle specific headers
149-
if (ngx_strcasecmp((u_char *)header_map->name, (u_char *)"Content-Type") == 0) {
150-
header_map = header_map->next;
151-
152-
continue;
153-
}
154-
155150
h = ngx_list_push(&r->headers_out.headers);
156151

157152
if (h == NULL) {
@@ -176,6 +171,10 @@ ngx_int_t ngx_http_redirectionio_headers_filter(ngx_http_request_t *r) {
176171
r->headers_out.content_encoding = h;
177172
}
178173

174+
if (ngx_strcasecmp((u_char *)header_map->name, (u_char *)"Content-Type") == 0) {
175+
ngx_http_redirectionio_header_content_type_write(r, h);
176+
}
177+
179178
header_map = header_map->next;
180179
}
181180

@@ -457,6 +456,54 @@ static ngx_int_t ngx_http_redirectionio_header_content_type_read(ngx_http_reques
457456
return NGX_OK;
458457
}
459458

459+
static ngx_int_t ngx_http_redirectionio_header_content_type_write(ngx_http_request_t *r, ngx_table_elt_t *h) {
460+
u_char *p, *last;
461+
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;
465+
466+
for (p = h->value.data; *p; p++) {
467+
468+
if (*p != ';') {
469+
continue;
470+
}
471+
472+
last = p;
473+
474+
while (*++p == ' ') { /* void */ }
475+
476+
if (*p == '\0') {
477+
return NGX_OK;
478+
}
479+
480+
if (ngx_strncasecmp(p, (u_char *) "charset=", 8) != 0) {
481+
continue;
482+
}
483+
484+
p += 8;
485+
486+
r->headers_out.content_type_len = last - h->value.data;
487+
488+
if (*p == '"') {
489+
p++;
490+
}
491+
492+
last = h->value.data + h->value.len;
493+
494+
if (*(last - 1) == '"') {
495+
last--;
496+
}
497+
498+
r->headers_out.charset.len = last - p;
499+
r->headers_out.charset.data = p;
500+
501+
return NGX_OK;
502+
}
503+
504+
return NGX_OK;
505+
}
506+
460507
static ngx_int_t ngx_http_redirectionio_buffer_read(ngx_buf_t *buffer, struct REDIRECTIONIO_Buffer *output) {
461508
#if (NGX_HAVE_SENDFILE64)
462509
off_t offset;

0 commit comments

Comments
 (0)