nginx / unit

NGINX Unit - universal web app server - a lightweight and versatile open source server that simplifies the application stack by natively executing application code across eight different programming language runtimes.
https://unit.nginx.org
Apache License 2.0
5.27k stars 322 forks source link

unit-http req.httpVersion is incorrect #1085

Closed dbit-xia closed 5 months ago

dbit-xia commented 5 months ago

The req.httpVersion property value in the unit-http module is incorrect. It should only have values of 1.0 or 1.1. However, Unit is returning both HTTP/1.0 and HTTP/1.1. Please refer to the official documentation at for more information.

https://nodejs.org/dist/latest-v8.x/docs/api/http.html#http_message_httpversion

image

Probably either '1.1' or '1.0'.

andrey-zelenkov commented 5 months ago

Hi @dbit-xia,

could you please try following patch if it works for you:

diff --git a/src/nodejs/unit-http/unit.cpp b/src/nodejs/unit-http/unit.cpp
--- a/src/nodejs/unit-http/unit.cpp
+++ b/src/nodejs/unit-http/unit.cpp
@@ -582,6 +582,7 @@ void
 Unit::create_headers(nxt_unit_request_info_t *req, napi_value request)
 {
     uint32_t            i;
+    const char          *p;
     napi_value          headers, raw_headers;
     napi_status         status;
     nxt_unit_request_t  *r;
@@ -602,7 +603,12 @@ Unit::create_headers(nxt_unit_request_in

     set_named_property(request, "headers", headers);
     set_named_property(request, "rawHeaders", raw_headers);
-    set_named_property(request, "httpVersion", r->version, r->version_length);
+
+    // need to strip "HTTP/" from version
+    p = (const char *) nxt_unit_sptr_get(&r->version);
+    p += 5;
+
+    set_named_property(request, "httpVersion", create_string_latin1(p, r->version_length - 5));
     set_named_property(request, "method", r->method, r->method_length);
     set_named_property(request, "url", r->target, r->target_length);

diff --git a/test/test_node_application.py b/test/test_node_application.py
--- a/test/test_node_application.py
+++ b/test/test_node_application.py
@@ -80,7 +80,7 @@ def test_node_application_variables(date
         'Request-Method': 'POST',
         'Request-Uri': '/',
         'Http-Host': 'localhost',
-        'Server-Protocol': 'HTTP/1.1',
+        'Server-Protocol': '1.1',
         'Custom-Header': 'blah',
     }, 'headers'
     assert resp['body'] == body, 'body'
dbit-xia commented 5 months ago

@andrey-zelenkov Thank you for providing the fix patch so quickly. I have verified that the value of httpVersion is now correct.