Skip to content

Commit f8bd6fb

Browse files
committed
Fetch: refactored request building logic into separate function.
1 parent 71b7764 commit f8bd6fb

File tree

4 files changed

+153
-250
lines changed

4 files changed

+153
-250
lines changed

nginx/ngx_js_fetch.c

Lines changed: 1 addition & 125 deletions
Original file line numberDiff line numberDiff line change
@@ -508,15 +508,10 @@ ngx_js_ext_fetch(njs_vm_t *vm, njs_value_t *args, njs_uint_t nargs,
508508
{
509509
njs_int_t ret;
510510
ngx_url_t u;
511-
ngx_uint_t i;
512-
njs_bool_t has_host;
513-
ngx_str_t method;
514511
ngx_pool_t *pool;
515512
njs_value_t *init, *value;
516513
ngx_js_http_t *http;
517514
ngx_js_fetch_t *fetch;
518-
ngx_list_part_t *part;
519-
ngx_js_tb_elt_t *h;
520515
ngx_js_request_t request;
521516
ngx_connection_t *c;
522517
ngx_resolver_ctx_t *ctx;
@@ -600,126 +595,7 @@ ngx_js_ext_fetch(njs_vm_t *vm, njs_value_t *args, njs_uint_t nargs,
600595
NJS_CHB_MP_INIT(&http->chain, njs_vm_memory_pool(vm));
601596
NJS_CHB_MP_INIT(&http->response.chain, njs_vm_memory_pool(vm));
602597

603-
njs_chb_append(&http->chain, request.method.data, request.method.len);
604-
njs_chb_append_literal(&http->chain, " ");
605-
606-
if (u.uri.len == 0 || u.uri.data[0] != '/') {
607-
njs_chb_append_literal(&http->chain, "/");
608-
}
609-
610-
njs_chb_append(&http->chain, u.uri.data, u.uri.len);
611-
njs_chb_append_literal(&http->chain, " HTTP/1.1" CRLF);
612-
613-
has_host = 0;
614-
part = &request.headers.header_list.part;
615-
h = part->elts;
616-
617-
for (i = 0; /* void */; i++) {
618-
619-
if (i >= part->nelts) {
620-
if (part->next == NULL) {
621-
break;
622-
}
623-
624-
part = part->next;
625-
h = part->elts;
626-
i = 0;
627-
}
628-
629-
if (h[i].hash == 0) {
630-
continue;
631-
}
632-
633-
if (h[i].key.len == 4
634-
&& ngx_strncasecmp(h[i].key.data, (u_char *) "Host", 4) == 0)
635-
{
636-
has_host = 1;
637-
njs_chb_append_literal(&http->chain, "Host: ");
638-
njs_chb_append(&http->chain, h[i].value.data, h[i].value.len);
639-
njs_chb_append_literal(&http->chain, CRLF);
640-
break;
641-
}
642-
}
643-
644-
if (!has_host) {
645-
njs_chb_append_literal(&http->chain, "Host: ");
646-
njs_chb_append(&http->chain, u.host.data, u.host.len);
647-
648-
if (!u.no_port) {
649-
njs_chb_sprintf(&http->chain, 32, ":%d", u.port);
650-
}
651-
652-
njs_chb_append_literal(&http->chain, CRLF);
653-
}
654-
655-
part = &request.headers.header_list.part;
656-
h = part->elts;
657-
658-
for (i = 0; /* void */; i++) {
659-
660-
if (i >= part->nelts) {
661-
if (part->next == NULL) {
662-
break;
663-
}
664-
665-
part = part->next;
666-
h = part->elts;
667-
i = 0;
668-
}
669-
670-
if (h[i].hash == 0) {
671-
continue;
672-
}
673-
674-
if (h[i].key.len == 4
675-
&& ngx_strncasecmp(h[i].key.data, (u_char *) "Host", 4) == 0)
676-
{
677-
continue;
678-
}
679-
680-
if (h[i].key.len == 14
681-
&& ngx_strncasecmp(h[i].key.data, (u_char *) "Content-Length", 14)
682-
== 0)
683-
{
684-
continue;
685-
}
686-
687-
if (h[i].key.len == 10
688-
&& ngx_strncasecmp(h[i].key.data, (u_char *) "Connection", 10)
689-
== 0)
690-
{
691-
continue;
692-
}
693-
694-
njs_chb_append(&http->chain, h[i].key.data, h[i].key.len);
695-
njs_chb_append_literal(&http->chain, ": ");
696-
njs_chb_append(&http->chain, h[i].value.data, h[i].value.len);
697-
njs_chb_append_literal(&http->chain, CRLF);
698-
}
699-
700-
if (!http->keepalive) {
701-
njs_chb_append_literal(&http->chain, "Connection: close" CRLF);
702-
}
703-
704-
if (request.body.len != 0) {
705-
njs_chb_sprintf(&http->chain, 32, "Content-Length: %uz" CRLF CRLF,
706-
request.body.len);
707-
njs_chb_append(&http->chain, request.body.data, request.body.len);
708-
709-
} else {
710-
method = request.method;
711-
712-
if ((method.len == 4
713-
&& (ngx_strncasecmp(method.data, (u_char *) "POST", 4) == 0))
714-
|| (method.len == 3
715-
&& ngx_strncasecmp(method.data, (u_char *) "PUT", 3) == 0))
716-
{
717-
njs_chb_append_literal(&http->chain, "Content-Length: 0" CRLF CRLF);
718-
719-
} else {
720-
njs_chb_append_literal(&http->chain, CRLF);
721-
}
722-
}
598+
ngx_js_fetch_build_request(http, &request, &u.uri, &u);
723599

724600
if (u.addrs == NULL) {
725601
ctx = ngx_js_http_resolve(http, ngx_external_resolver(vm, external),

nginx/ngx_js_http.c

Lines changed: 148 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,9 @@ static ngx_int_t ngx_js_http_parse_header_line(ngx_js_http_parse_t *hp,
4747
static ngx_int_t ngx_js_http_parse_chunked(ngx_js_http_chunk_parse_t *hcp,
4848
ngx_buf_t *b, njs_chb_t *chain);
4949

50+
static void ngx_js_fetch_append_request_headers(njs_chb_t *chain,
51+
ngx_js_request_t *request);
52+
5053
#if (NGX_SSL)
5154
static void ngx_js_http_ssl_init_connection(ngx_js_http_t *http);
5255
static void ngx_js_http_ssl_handshake_handler(ngx_connection_t *c);
@@ -1856,3 +1859,148 @@ ngx_js_http_keepalive_close_handler(ngx_event_t *ev)
18561859
ngx_queue_remove(&cache->queue);
18571860
ngx_queue_insert_head(&conf->fetch_keepalive_free, &cache->queue);
18581861
}
1862+
1863+
1864+
static void
1865+
ngx_js_fetch_append_request_headers(njs_chb_t *chain,
1866+
ngx_js_request_t *request)
1867+
{
1868+
ngx_uint_t i;
1869+
ngx_list_part_t *part;
1870+
ngx_js_tb_elt_t *h;
1871+
1872+
part = &request->headers.header_list.part;
1873+
h = part->elts;
1874+
1875+
for (i = 0; /* void */; i++) {
1876+
1877+
if (i >= part->nelts) {
1878+
if (part->next == NULL) {
1879+
break;
1880+
}
1881+
1882+
part = part->next;
1883+
h = part->elts;
1884+
i = 0;
1885+
}
1886+
1887+
if (h[i].hash == 0) {
1888+
continue;
1889+
}
1890+
1891+
if (h[i].key.len == 4
1892+
&& ngx_strncasecmp(h[i].key.data, (u_char *) "Host", 4) == 0)
1893+
{
1894+
continue;
1895+
}
1896+
1897+
if (h[i].key.len == 14
1898+
&& ngx_strncasecmp(h[i].key.data, (u_char *) "Content-Length",
1899+
14) == 0)
1900+
{
1901+
continue;
1902+
}
1903+
1904+
if (h[i].key.len == 10
1905+
&& ngx_strncasecmp(h[i].key.data, (u_char *) "Connection", 10)
1906+
== 0)
1907+
{
1908+
continue;
1909+
}
1910+
1911+
njs_chb_append(chain, h[i].key.data, h[i].key.len);
1912+
njs_chb_append_literal(chain, ": ");
1913+
njs_chb_append(chain, h[i].value.data, h[i].value.len);
1914+
njs_chb_append_literal(chain, CRLF);
1915+
}
1916+
}
1917+
1918+
1919+
void
1920+
ngx_js_fetch_build_request(ngx_js_http_t *http, ngx_js_request_t *request,
1921+
ngx_str_t *path, ngx_url_t *u)
1922+
{
1923+
ngx_str_t method;
1924+
ngx_uint_t i;
1925+
njs_bool_t has_host;
1926+
ngx_list_part_t *part;
1927+
ngx_js_tb_elt_t *h;
1928+
1929+
njs_chb_append(&http->chain, request->method.data, request->method.len);
1930+
njs_chb_append_literal(&http->chain, " ");
1931+
1932+
if (path->len == 0 || path->data[0] != '/') {
1933+
njs_chb_append_literal(&http->chain, "/");
1934+
}
1935+
1936+
njs_chb_append(&http->chain, path->data, path->len);
1937+
njs_chb_append_literal(&http->chain, " HTTP/1.1" CRLF);
1938+
1939+
has_host = 0;
1940+
part = &request->headers.header_list.part;
1941+
h = part->elts;
1942+
1943+
for (i = 0; /* void */; i++) {
1944+
1945+
if (i >= part->nelts) {
1946+
if (part->next == NULL) {
1947+
break;
1948+
}
1949+
1950+
part = part->next;
1951+
h = part->elts;
1952+
i = 0;
1953+
}
1954+
1955+
if (h[i].hash == 0) {
1956+
continue;
1957+
}
1958+
1959+
if (h[i].key.len == 4
1960+
&& ngx_strncasecmp(h[i].key.data, (u_char *) "Host", 4) == 0)
1961+
{
1962+
has_host = 1;
1963+
njs_chb_append_literal(&http->chain, "Host: ");
1964+
njs_chb_append(&http->chain, h[i].value.data, h[i].value.len);
1965+
njs_chb_append_literal(&http->chain, CRLF);
1966+
break;
1967+
}
1968+
}
1969+
1970+
if (!has_host) {
1971+
njs_chb_append_literal(&http->chain, "Host: ");
1972+
njs_chb_append(&http->chain, u->host.data, u->host.len);
1973+
1974+
if (!u->no_port) {
1975+
njs_chb_sprintf(&http->chain, 32, ":%d", u->port);
1976+
}
1977+
1978+
njs_chb_append_literal(&http->chain, CRLF);
1979+
}
1980+
1981+
ngx_js_fetch_append_request_headers(&http->chain, request);
1982+
1983+
if (!http->keepalive) {
1984+
njs_chb_append_literal(&http->chain, "Connection: close" CRLF);
1985+
}
1986+
1987+
if (request->body.len != 0) {
1988+
njs_chb_sprintf(&http->chain, 32, "Content-Length: %uz" CRLF CRLF,
1989+
request->body.len);
1990+
njs_chb_append(&http->chain, request->body.data, request->body.len);
1991+
1992+
} else {
1993+
method = request->method;
1994+
1995+
if ((method.len == 4
1996+
&& (ngx_strncasecmp(method.data, (u_char *) "POST", 4) == 0))
1997+
|| (method.len == 3
1998+
&& ngx_strncasecmp(method.data, (u_char *) "PUT", 3) == 0))
1999+
{
2000+
njs_chb_append_literal(&http->chain, "Content-Length: 0" CRLF CRLF);
2001+
2002+
} else {
2003+
njs_chb_append_literal(&http->chain, CRLF);
2004+
}
2005+
}
2006+
}

nginx/ngx_js_http.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,5 +167,8 @@ void ngx_js_http_trim(u_char **value, size_t *len,
167167
int trim_c0_control_or_space);
168168
ngx_int_t ngx_js_check_header_name(u_char *name, size_t len);
169169

170+
void ngx_js_fetch_build_request(ngx_js_http_t *http, ngx_js_request_t *request,
171+
ngx_str_t *path, ngx_url_t *u);
172+
170173

171174
#endif /* _NGX_JS_HTTP_H_INCLUDED_ */

0 commit comments

Comments
 (0)