[njs] HTTP: introduced raw headers API.

Dmitry Volyntsev xeioex at nginx.com
Fri May 8 16:36:45 UTC 2020


details:   https://hg.nginx.org/njs/rev/92838d964b19
branches:  
changeset: 1388:92838d964b19
user:      Dmitry Volyntsev <xeioex at nginx.com>
date:      Fri May 08 15:52:40 2020 +0000
description:
HTTP: introduced raw headers API.

1) r.rawHeadersIn returns an array of key-value pairs exactly as they
were received from the client.

2) r.rawHeadersOut returns an array of key-value pairs of respose
headers.

Header names are not lowercased, and duplicates are not merged.

diffstat:

 nginx/ngx_http_js_module.c |  103 +++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 103 insertions(+), 0 deletions(-)

diffs (134 lines):

diff -r 1268384be910 -r 92838d964b19 nginx/ngx_http_js_module.c
--- a/nginx/ngx_http_js_module.c	Fri May 08 12:02:32 2020 +0000
+++ b/nginx/ngx_http_js_module.c	Fri May 08 15:52:40 2020 +0000
@@ -84,6 +84,9 @@ static njs_int_t ngx_http_js_ext_keys_he
     njs_value_t *keys, ngx_list_t *headers);
 static ngx_table_elt_t *ngx_http_js_get_header(ngx_list_part_t *part,
     u_char *data, size_t len);
+static njs_int_t ngx_http_js_ext_raw_header(njs_vm_t *vm,
+    njs_object_prop_t *prop, njs_value_t *value, njs_value_t *setval,
+    njs_value_t *retval);
 static njs_int_t ngx_http_js_ext_header_out(njs_vm_t *vm,
     njs_object_prop_t *prop, njs_value_t *value, njs_value_t *setval,
     njs_value_t *retval);
@@ -354,6 +357,15 @@ static njs_external_t  ngx_http_js_ext_r
     },
 
     {
+        .flags = NJS_EXTERN_PROPERTY,
+        .name.string = njs_str("rawHeadersIn"),
+        .u.property = {
+            .handler = ngx_http_js_ext_raw_header,
+            .magic32 = 0,
+        }
+    },
+
+    {
         .flags = NJS_EXTERN_OBJECT,
         .name.string = njs_str("args"),
         .enumerable = 1,
@@ -397,6 +409,15 @@ static njs_external_t  ngx_http_js_ext_r
     },
 
     {
+        .flags = NJS_EXTERN_PROPERTY,
+        .name.string = njs_str("rawHeadersOut"),
+        .u.property = {
+            .handler = ngx_http_js_ext_raw_header,
+            .magic32 = 1,
+        }
+    },
+
+    {
         .flags = NJS_EXTERN_METHOD,
         .name.string = njs_str("subrequest"),
         .writable = 1,
@@ -934,6 +955,88 @@ ngx_http_js_get_header(ngx_list_part_t *
 
 
 static njs_int_t
+ngx_http_js_ext_raw_header(njs_vm_t *vm, njs_object_prop_t *prop,
+    njs_value_t *value, njs_value_t *setval, njs_value_t *retval)
+{
+    njs_int_t            rc;
+    ngx_uint_t           i;
+    njs_value_t         *array, *elem;
+    ngx_list_part_t     *part;
+    ngx_list_t          *headers;
+    ngx_table_elt_t     *header, *h;
+    ngx_http_request_t  *r;
+
+    r = njs_vm_external(vm, value);
+    if (r == NULL) {
+        njs_value_undefined_set(retval);
+        return NJS_DECLINED;
+    }
+
+    headers = (njs_vm_prop_magic32(prop) == 1) ? &r->headers_out.headers
+                                               : &r->headers_in.headers;
+
+    rc = njs_vm_array_alloc(vm, retval, 8);
+    if (rc != NJS_OK) {
+        return NJS_ERROR;
+    }
+
+    part = &headers->part;
+    header = part->elts;
+
+    for (i = 0; /* void */ ; i++) {
+
+        if (i >= part->nelts) {
+            if (part->next == NULL) {
+                break;
+            }
+
+            part = part->next;
+            header = part->elts;
+            i = 0;
+        }
+
+        h = &header[i];
+
+        if (h->hash == 0) {
+            continue;
+        }
+
+        array = njs_vm_array_push(vm, retval);
+        if (array == NULL) {
+            return NJS_ERROR;
+        }
+
+        rc = njs_vm_array_alloc(vm, array, 2);
+        if (rc != NJS_OK) {
+            return NJS_ERROR;
+        }
+
+        elem = njs_vm_array_push(vm, array);
+        if (elem == NULL) {
+            return NJS_ERROR;
+        }
+
+        rc = njs_vm_value_string_set(vm, elem, h->key.data, h->key.len);
+        if (rc != NJS_OK) {
+            return NJS_ERROR;
+        }
+
+        elem = njs_vm_array_push(vm, array);
+        if (elem == NULL) {
+            return NJS_ERROR;
+        }
+
+        rc = njs_vm_value_string_set(vm, elem, h->value.data, h->value.len);
+        if (rc != NJS_OK) {
+            return NJS_ERROR;
+        }
+    }
+
+    return NJS_OK;
+}
+
+
+static njs_int_t
 ngx_http_js_ext_header_out(njs_vm_t *vm, njs_object_prop_t *prop,
     njs_value_t *value, njs_value_t *setval, njs_value_t *retval)
 {


More information about the nginx-devel mailing list