Skip to content

Commit 54ce381

Browse files
committed
SCGI: combining headers with identical names (ticket #1724).
SCGI specification explicitly forbids headers with duplicate names (section "3. Request Format"): "Duplicate names are not allowed in the headers". Further, provided headers are expected to follow CGI specification, which also requires to combine headers (RFC 3875, section "4.1.18. Protocol-Specific Meta-Variables"): "If multiple header fields with the same field-name are received then the server MUST rewrite them as a single value having the same semantics".
1 parent d8a7c65 commit 54ce381

File tree

1 file changed

+45
-5
lines changed

1 file changed

+45
-5
lines changed

src/http/modules/ngx_http_scgi_module.c

Lines changed: 45 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -633,14 +633,14 @@ static ngx_int_t
633633
ngx_http_scgi_create_request(ngx_http_request_t *r)
634634
{
635635
off_t content_length_n;
636-
u_char ch, *key, *val, *lowcase_key;
636+
u_char ch, sep, *key, *val, *lowcase_key;
637637
size_t len, key_len, val_len, allocated;
638638
ngx_buf_t *b;
639639
ngx_str_t content_length;
640640
ngx_uint_t i, n, hash, skip_empty, header_params;
641641
ngx_chain_t *cl, *body;
642642
ngx_list_part_t *part;
643-
ngx_table_elt_t *header, **ignored;
643+
ngx_table_elt_t *header, *hn, **ignored;
644644
ngx_http_scgi_params_t *params;
645645
ngx_http_script_code_pt code;
646646
ngx_http_script_engine_t e, le;
@@ -707,7 +707,11 @@ ngx_http_scgi_create_request(ngx_http_request_t *r)
707707
allocated = 0;
708708
lowcase_key = NULL;
709709

710-
if (params->number) {
710+
if (ngx_http_link_multi_headers(r) != NGX_OK) {
711+
return NGX_ERROR;
712+
}
713+
714+
if (params->number || r->headers_in.multi) {
711715
n = 0;
712716
part = &r->headers_in.headers.part;
713717

@@ -737,6 +741,12 @@ ngx_http_scgi_create_request(ngx_http_request_t *r)
737741
i = 0;
738742
}
739743

744+
for (n = 0; n < header_params; n++) {
745+
if (&header[i] == ignored[n]) {
746+
goto next_length;
747+
}
748+
}
749+
740750
if (params->number) {
741751
if (allocated < header[i].key.len) {
742752
allocated = header[i].key.len + 16;
@@ -770,6 +780,15 @@ ngx_http_scgi_create_request(ngx_http_request_t *r)
770780

771781
len += sizeof("HTTP_") - 1 + header[i].key.len + 1
772782
+ header[i].value.len + 1;
783+
784+
for (hn = header[i].next; hn; hn = hn->next) {
785+
len += hn->value.len + 2;
786+
ignored[header_params++] = hn;
787+
}
788+
789+
next_length:
790+
791+
continue;
773792
}
774793
}
775794

@@ -869,7 +888,7 @@ ngx_http_scgi_create_request(ngx_http_request_t *r)
869888

870889
for (n = 0; n < header_params; n++) {
871890
if (&header[i] == ignored[n]) {
872-
goto next;
891+
goto next_value;
873892
}
874893
}
875894

@@ -893,12 +912,33 @@ ngx_http_scgi_create_request(ngx_http_request_t *r)
893912

894913
val = b->last;
895914
b->last = ngx_copy(val, header[i].value.data, header[i].value.len);
915+
916+
if (header[i].next) {
917+
918+
if (header[i].key.len == sizeof("Cookie") - 1
919+
&& ngx_strncasecmp(header[i].key.data, (u_char *) "Cookie",
920+
sizeof("Cookie") - 1)
921+
== 0)
922+
{
923+
sep = ';';
924+
925+
} else {
926+
sep = ',';
927+
}
928+
929+
for (hn = header[i].next; hn; hn = hn->next) {
930+
*b->last++ = sep;
931+
*b->last++ = ' ';
932+
b->last = ngx_copy(b->last, hn->value.data, hn->value.len);
933+
}
934+
}
935+
896936
*b->last++ = (u_char) 0;
897937

898938
ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
899939
"scgi param: \"%s: %s\"", key, val);
900940

901-
next:
941+
next_value:
902942

903943
continue;
904944
}

0 commit comments

Comments
 (0)