From zelenkov at nginx.com Thu Jan 27 13:14:00 2022 From: zelenkov at nginx.com (Andrei Zeliankou) Date: Thu, 27 Jan 2022 13:14:00 +0000 Subject: [PATCH v4 06/12] test_static.py: formatting fix In-Reply-To: <20211223192508.12146-7-alx.manpages@gmail.com> References: <20211219013030.51372-1-alx.manpages@gmail.com> <20211223192508.12146-7-alx.manpages@gmail.com> Message-ID: Hi! > On 23 Dec 2021, at 19:25, Alejandro Colomar wrote: > > Expand indentation, to allow for the addition of an "index" > configuration. This only changes whitespace/indentation. There is no need to move changes like this into the separate patch. > > Signed-off-by: Alejandro Colomar > Cc: Nginx Unit > Cc: "Valentin V. Bartenev" > Cc: Zhidao HONG > Cc: Igor Sysoev > Cc: Oisin Canty > --- > test/test_static.py | 6 +++++- > 1 file changed, 5 insertions(+), 1 deletion(-) > > diff --git a/test/test_static.py b/test/test_static.py > index 80f4c61..76668a1 100644 > --- a/test/test_static.py > +++ b/test/test_static.py > @@ -30,7 +30,11 @@ class TestStatic(TestApplicationProto): > { > "listeners": {"*:7080": {"pass": "routes"}}, > "routes": [ > - {"action": {"share": option.temp_dir + "/assets$uri"}} > + { > + "action": { > + "share": option.temp_dir + "/assets$uri" > + } > + } > ], > "settings": { > "http": { > -- > 2.34.1 > > _______________________________________________ > unit mailing list > unit at nginx.org > https://mailman.nginx.org/mailman/listinfo/unit From alx.manpages at gmail.com Thu Jan 27 23:14:44 2022 From: alx.manpages at gmail.com (Alejandro Colomar (man-pages)) Date: Fri, 28 Jan 2022 00:14:44 +0100 Subject: [PATCH v4 06/12] test_static.py: formatting fix In-Reply-To: References: <20211219013030.51372-1-alx.manpages@gmail.com> <20211223192508.12146-7-alx.manpages@gmail.com> Message-ID: Hi Andrei, On 1/27/22 14:14, Andrei Zeliankou wrote: > Hi! > >> On 23 Dec 2021, at 19:25, Alejandro Colomar wrote: >> >> Expand indentation, to allow for the addition of an "index" >> configuration. This only changes whitespace/indentation. > > > There is no need to move changes like this into the separate patch. I know, but then patch 07 becomes easier to read[1], IMO. While writing this feature, I found some patches that contained a bit more than what they stated, probably because a separate patch for those minor changes would be too much; but then the patch became a bit less readable. If you're okay with this separation, I tend to prefer smaller patches. Thanks, Alex [1]: @@ -32,7 +32,8 @@ class TestStatic(TestApplicationProto): "routes": [ { "action": { - "share": option.temp_dir + "/assets$uri" + "share": option.temp_dir + "/assets$uri", + "index": "index_.html" } } ], > >> >> Signed-off-by: Alejandro Colomar >> Cc: Nginx Unit >> Cc: "Valentin V. Bartenev" >> Cc: Zhidao HONG >> Cc: Igor Sysoev >> Cc: Oisin Canty >> --- >> test/test_static.py | 6 +++++- >> 1 file changed, 5 insertions(+), 1 deletion(-) >> >> diff --git a/test/test_static.py b/test/test_static.py >> index 80f4c61..76668a1 100644 >> --- a/test/test_static.py >> +++ b/test/test_static.py >> @@ -30,7 +30,11 @@ class TestStatic(TestApplicationProto): >> { >> "listeners": {"*:7080": {"pass": "routes"}}, >> "routes": [ >> - {"action": {"share": option.temp_dir + "/assets$uri"}} >> + { >> + "action": { >> + "share": option.temp_dir + "/assets$uri" >> + } >> + } >> ], >> "settings": { >> "http": { >> -- >> 2.34.1 >> >> _______________________________________________ >> unit mailing list >> unit at nginx.org >> https://mailman.nginx.org/mailman/listinfo/unit > -- Alejandro Colomar Linux man-pages comaintainer; https://www.kernel.org/doc/man-pages/ http://www.alejandro-colomar.es/ From zelenkov at nginx.com Fri Jan 28 12:27:25 2022 From: zelenkov at nginx.com (Andrei Zeliankou) Date: Fri, 28 Jan 2022 12:27:25 +0000 Subject: [PATCH v4 07/12] Static: add "index" option In-Reply-To: <20211223192508.12146-8-alx.manpages@gmail.com> References: <20211219013030.51372-1-alx.manpages@gmail.com> <20211223192508.12146-8-alx.manpages@gmail.com> Message-ID: <36737EAD-4644-409F-B6E2-022E48AFDD47@nginx.com> Hi! > On 23 Dec 2021, at 19:25, Alejandro Colomar wrote: > > This supports a new option "index", that configures a custom index > file name that will be served when a directory is requested. This > initial support only allows a single fixed string. An example: > > { > "share": "/www/data/static/$uri", > "index": "lookatthis.htm" > } > > When is requested, > will be served. > > If the option is missing, default to "index.html". > > === > > nxt_conf_validator.c: > > Accept "index" as a member of "share", and make sure it is a > string. > > === > > test_static.py: > > Rename to , to be able to test this new > option, and set "index": "index_.html". > > Perhaps we should also add a test to check that if "index" is > omitted, the default "index.html" is used. > > I also tried this feature in my own computer, where I tried the > following: > > - Setting "index" to "lookatthis.htm", and check that the correct > file is being served (check both a different name and a > different extension). > - Not setting "index", and check that is being > served. > - Settind "index" to an array of strings, and check that the > configuration fails: > > { > "error": "Invalid configuration.", > "detail": "The \"index\" value must be a string, but not an array." > } > > Signed-off-by: Alejandro Colomar > Cc: Nginx Unit > Cc: "Valentin V. Bartenev" > Cc: Zhidao HONG > Cc: Igor Sysoev > Cc: Oisin Canty > --- > docs/changes.xml | 6 ++++++ > src/nxt_conf_validation.c | 3 +++ > src/nxt_http.h | 1 + > src/nxt_http_route.c | 5 +++++ > src/nxt_http_static.c | 32 +++++++++++++++++++++++--------- > test/test_static.py | 13 +++++++------ > 6 files changed, 45 insertions(+), 15 deletions(-) [..] > diff --git a/test/test_static.py b/test/test_static.py > index 76668a1..70ae676 100644 > --- a/test/test_static.py > +++ b/test/test_static.py > @@ -14,7 +14,7 @@ class TestStatic(TestApplicationProto): > > def setup_method(self): > os.makedirs(option.temp_dir + '/assets/dir') > - with open(option.temp_dir + '/assets/index.html', 'w') as index, open( > + with open(option.temp_dir + '/assets/index_.html', 'w') as index, open( > option.temp_dir + '/assets/README', 'w' > ) as readme, open( > option.temp_dir + '/assets/log.log', 'w' > @@ -32,7 +32,8 @@ class TestStatic(TestApplicationProto): > "routes": [ > { > "action": { > - "share": option.temp_dir + "/assets$uri" > + "share": option.temp_dir + "/assets$uri", > + "index": "index_.html" > } > } > ], > @@ -90,7 +91,7 @@ class TestStatic(TestApplicationProto): > assert self.get(url='/')['body'] == '0123456789', 'before 1.26.0 2' > > def test_static_index(self): > - assert self.get(url='/index.html')['body'] == '0123456789', 'index' > + assert self.get(url='/index_.html')['body'] == '0123456789', 'index' > assert self.get(url='/')['body'] == '0123456789', 'index 2' > assert self.get(url='//')['body'] == '0123456789', 'index 3' > assert self.get(url='/.')['body'] == '0123456789', 'index 4' > @@ -99,7 +100,7 @@ class TestStatic(TestApplicationProto): > assert self.get(url='/#blah')['body'] == '0123456789', 'index anchor' > assert self.get(url='/dir/')['status'] == 404, 'index not found' > > - resp = self.get(url='/index.html/') > + resp = self.get(url='/index_.html/') > assert resp['status'] == 404, 'index not found 2 status' > assert ( > resp['headers']['Content-Type'] == 'text/html' > @@ -123,7 +124,7 @@ class TestStatic(TestApplicationProto): > assert etag != etag_2, 'different ETag' > assert etag == self.get(url='/')['headers']['ETag'], 'same ETag' > > - with open(temp_dir + '/assets/index.html', 'w') as f: > + with open(temp_dir + '/assets/index_.html', 'w') as f: > f.write('blah') > > assert etag != self.get(url='/')['headers']['ETag'], 'new ETag' > @@ -262,7 +263,7 @@ class TestStatic(TestApplicationProto): > == 'text/x-code/x-blah/x-blah' > ), 'mime_types string case insensitive' > assert ( > - self.get(url='/index.html')['headers']['Content-Type'] > + self.get(url='/index_.html')['headers']['Content-Type'] > == 'text/plain' > ), 'mime_types html' > assert ( > -- For this patch and all the others in the set: as far as patches are expanding existing functionality, test patches should do the same thing (expand existing test cases, not to replace them). So, I would suggest one following patch for tests for the whole bundle: # HG changeset patch # User Andrei Zeliankou # Date 1643372490 0 # Fri Jan 28 12:21:30 2022 +0000 # Node ID afe306b9b8d4aed242456aeb0c9e5703e7da3edb # Parent f05ba33bcd79ab13a838cf80242d784d4c385bbf Tests: added tests for "index" option. diff --git a/test/test_static.py b/test/test_static.py --- a/test/test_static.py +++ b/test/test_static.py @@ -86,6 +86,53 @@ class TestStatic(TestApplicationProto): assert self.get(url='/')['body'] == '0123456789', 'before 1.26.0 2' def test_static_index(self): + def set_index(index): + assert 'success' in self.conf( + {"share": option.temp_dir + "/assets$uri", "index": index}, + 'routes/0/action', + ), 'configure index' + + set_index('README') + assert self.get()['body'] == 'readme', 'index' + + self.conf_delete('routes/0/action/index') + assert self.get()['body'] == '0123456789', 'delete index' + + set_index('$host') + assert ( + self.get(headers={"Host": "README", "Connection": "close"})['body'] + == 'readme' + ), 'index var' + self.get(headers={"Connection": "close"})[ + 'status' + ] == 404, 'index var empty' + + set_index([]) + self.get()['status'] == 404, 'index array empty' + + set_index(['blah']) + self.get()['status'] == 404, 'index array not found' + + set_index(['$host', 'blah', 'index.html']) + assert self.get()['body'] == '0123456789', 'index array' + assert ( + self.get(headers={"Host": "README", "Connection": "close"})['body'] + == 'readme' + ), 'index array 2' + + def test_static_index_invalid(self, skip_alert): + skip_alert(r'failed to apply new conf') + + def check_index(index): + assert 'error' in self.conf( + {"share": option.temp_dir + "/assets$uri", "index": index}, + 'routes/0/action', + ) + + check_index({}) + check_index(['index.html', '$blah']) + + def test_static_index_default(self): assert self.get(url='/index.html')['body'] == '0123456789', 'index' assert self.get(url='/')['body'] == '0123456789', 'index 2' assert self.get(url='//')['body'] == '0123456789', 'index 3’ Also noticed problems: 1) can't compile patch set with specified `--debug` option: src/nxt_http_static.c:264:23: error: no member named 'index' in 'nxt_http_static_conf_t' nxt_var_raw(conf->index, &idx); ~~~~ ^ 2) router crash when index is the empty array 3) router crash when variable in index value is empty Kind regards, Andrei