Python: Added "prefix" to configuration.

This patch gives users the option to set a `"prefix"` attribute
for Python applications, either at the top level or for specific
`"target"`s. If the attribute is present, the value of `"prefix"`
must be a string beginning with `"/"`. If the value of the `"prefix"`
attribute is longer than 1 character and ends in `"/"`, the
trailing `"/"` is stripped.

The purpose of the `"prefix"` attribute is to set the `SCRIPT_NAME`
context value for WSGI applications and the `root_path` context
value for ASGI applications, allowing applications to properly route
requests regardless of the path that the server uses to expose the
application.

The context value is only set if the request's URL path begins with
the value of the `"prefix"` attribute. In all other cases, the
`SCRIPT_NAME` or `root_path` values are not set. In addition, for
WSGI applications, the value of `"prefix"` will be stripped from
the beginning of the request's URL path before it is sent to the
application.

Reviewed-by: Andrei Zeliankou <zelenkov@nginx.com>
Reviewed-by: Artem Konev <artem.konev@nginx.com>
Signed-off-by: Alejandro Colomar <alx@nginx.com>
This commit is contained in:
OutOfFocus4
2021-11-14 10:47:07 -05:00
committed by Alejandro Colomar
parent 7a81d9d61d
commit 6dae517ebd
17 changed files with 472 additions and 23 deletions

View File

@@ -0,0 +1,15 @@
async def application(scope, receive, send):
assert scope['type'] == 'http'
await send(
{
'type': 'http.response.start',
'status': 200,
'headers': [
(b'content-length', b'0'),
(b'prefix', scope.get('root_path', 'NULL').encode()),
],
}
)
await send({'type': 'http.response.body', 'body': b''})

View File

@@ -0,0 +1,10 @@
def application(environ, start_response):
start_response(
'200',
[
('Content-Length', '0'),
('Script-Name', environ.get('SCRIPT_NAME', 'NULL')),
('Path-Info', environ['PATH_INFO']),
],
)
return []

View File

@@ -22,6 +22,23 @@ async def application_200(scope, receive, send):
)
async def application_prefix(scope, receive, send):
assert scope['type'] == 'http'
await send(
{
'type': 'http.response.start',
'status': 200,
'headers': [
(b'content-length', b'0'),
(b'prefix', scope.get('root_path', 'NULL').encode()),
],
}
)
await send({'type': 'http.response.body', 'body': b''})
def legacy_application_200(scope):
assert scope['type'] == 'http'

View File

@@ -6,3 +6,12 @@ def wsgi_target_a(env, start_response):
def wsgi_target_b(env, start_response):
start_response('200', [('Content-Length', '1')])
return [b'2']
def wsgi_target_prefix(env, start_response):
data = u'%s %s' % (
env.get('SCRIPT_NAME', 'No Script Name'),
env['PATH_INFO'],
)
start_response('200', [('Content-Length', '%d' % len(data))])
return [data.encode('utf-8')]

View File

@@ -79,6 +79,43 @@ custom-header: BLAH
resp['headers']['query-string'] == 'var1=val1&var2=val2'
), 'query-string header'
def test_asgi_application_prefix(self):
self.load('prefix', prefix='/api/rest')
def set_prefix(prefix):
self.conf('"' + prefix + '"', 'applications/prefix/prefix')
def check_prefix(url, prefix):
resp = self.get(url=url)
assert resp['status'] == 200
assert resp['headers']['prefix'] == prefix
check_prefix('/ap', 'NULL')
check_prefix('/api', 'NULL')
check_prefix('/api/', 'NULL')
check_prefix('/api/res', 'NULL')
check_prefix('/api/restful', 'NULL')
check_prefix('/api/rest', '/api/rest')
check_prefix('/api/rest/', '/api/rest')
check_prefix('/api/rest/get', '/api/rest')
check_prefix('/api/rest/get/blah', '/api/rest')
set_prefix('/api/rest/')
check_prefix('/api/rest', '/api/rest')
check_prefix('/api/restful', 'NULL')
check_prefix('/api/rest/', '/api/rest')
check_prefix('/api/rest/blah', '/api/rest')
set_prefix('/app')
check_prefix('/ap', 'NULL')
check_prefix('/app', '/app')
check_prefix('/app/', '/app')
check_prefix('/application/', 'NULL')
set_prefix('/')
check_prefix('/', 'NULL')
check_prefix('/app', 'NULL')
def test_asgi_application_query_string_space(self):
self.load('query_string')

View File

@@ -90,3 +90,48 @@ class TestASGITargets(TestApplicationPython):
)
assert self.get(url='/1')['status'] != 200
def test_asgi_targets_prefix(self):
self.conf_targets(
{
"1": {
"module": "asgi",
"callable": "application_prefix",
"prefix": "/1/",
},
"2": {
"module": "asgi",
"callable": "application_prefix",
"prefix": "/api",
},
}
)
self.conf(
[
{
"match": {"uri": "/1*"},
"action": {"pass": "applications/targets/1"},
},
{
"match": {"uri": "*"},
"action": {"pass": "applications/targets/2"},
},
],
"routes",
)
def check_prefix(url, prefix):
resp = self.get(url=url)
assert resp['status'] == 200
assert resp['headers']['prefix'] == prefix
check_prefix('/1', '/1')
check_prefix('/11', 'NULL')
check_prefix('/1/', '/1')
check_prefix('/', 'NULL')
check_prefix('/ap', 'NULL')
check_prefix('/api', '/api')
check_prefix('/api/', '/api')
check_prefix('/api/test/', '/api')
check_prefix('/apis', 'NULL')
check_prefix('/apis/', 'NULL')

View File

@@ -318,6 +318,92 @@ class TestConfiguration(TestControl):
assert 'success' in self.conf(conf)
def test_json_application_python_prefix(self):
conf = {
"applications": {
"sub-app": {
"type": "python",
"processes": {"spare": 0},
"path": "/app",
"module": "wsgi",
"prefix": "/app",
}
},
"listeners": {"*:7080": {"pass": "routes"}},
"routes": [
{
"match": {"uri": "/app/*"},
"action": {"pass": "applications/sub-app"},
}
],
}
assert 'success' in self.conf(conf)
def test_json_application_prefix_target(self):
conf = {
"applications": {
"sub-app": {
"type": "python",
"processes": {"spare": 0},
"path": "/app",
"targets": {
"foo": {"module": "foo.wsgi", "prefix": "/app"},
"bar": {
"module": "bar.wsgi",
"callable": "bar",
"prefix": "/api",
},
},
}
},
"listeners": {"*:7080": {"pass": "routes"}},
"routes": [
{
"match": {"uri": "/app/*"},
"action": {"pass": "applications/sub-app/foo"},
},
{
"match": {"uri": "/api/*"},
"action": {"pass": "applications/sub-app/bar"},
},
],
}
assert 'success' in self.conf(conf)
def test_json_application_invalid_python_prefix(self):
conf = {
"applications": {
"sub-app": {
"type": "python",
"processes": {"spare": 0},
"path": "/app",
"module": "wsgi",
"prefix": "app",
}
},
"listeners": {"*:7080": {"pass": "applications/sub-app"}},
}
assert 'error' in self.conf(conf)
def test_json_application_empty_python_prefix(self):
conf = {
"applications": {
"sub-app": {
"type": "python",
"processes": {"spare": 0},
"path": "/app",
"module": "wsgi",
"prefix": "",
}
},
"listeners": {"*:7080": {"pass": "applications/sub-app"}},
}
assert 'error' in self.conf(conf)
def test_json_application_many2(self):
conf = {
"applications": {

View File

@@ -94,6 +94,44 @@ custom-header: BLAH
resp['headers']['Query-String'] == ' var1= val1 & var2=val2'
), 'Query-String space 4'
def test_python_application_prefix(self):
self.load('prefix', prefix='/api/rest')
def set_prefix(prefix):
self.conf('"' + prefix + '"', 'applications/prefix/prefix')
def check_prefix(url, script_name, path_info):
resp = self.get(url=url)
assert resp['status'] == 200
assert resp['headers']['Script-Name'] == script_name
assert resp['headers']['Path-Info'] == path_info
check_prefix('/ap', 'NULL', '/ap')
check_prefix('/api', 'NULL', '/api')
check_prefix('/api/', 'NULL', '/api/')
check_prefix('/api/res', 'NULL', '/api/res')
check_prefix('/api/restful', 'NULL', '/api/restful')
check_prefix('/api/rest', '/api/rest', '')
check_prefix('/api/rest/', '/api/rest', '/')
check_prefix('/api/rest/get', '/api/rest', '/get')
check_prefix('/api/rest/get/blah', '/api/rest', '/get/blah')
set_prefix('/api/rest/')
check_prefix('/api/rest', '/api/rest', '')
check_prefix('/api/restful', 'NULL', '/api/restful')
check_prefix('/api/rest/', '/api/rest', '/')
check_prefix('/api/rest/blah', '/api/rest', '/blah')
set_prefix('/app')
check_prefix('/ap', 'NULL', '/ap')
check_prefix('/app', '/app', '')
check_prefix('/app/', '/app', '/')
check_prefix('/application/', 'NULL', '/application/')
set_prefix('/')
check_prefix('/', 'NULL', '/')
check_prefix('/app', 'NULL', '/app')
def test_python_application_query_string_empty(self):
self.load('query_string')

View File

@@ -47,3 +47,55 @@ class TestPythonTargets(TestApplicationPython):
resp = self.get(url='/2')
assert resp['status'] == 200
assert resp['body'] == '2'
def test_python_targets_prefix(self):
assert 'success' in self.conf(
{
"listeners": {"*:7080": {"pass": "routes"}},
"routes": [
{
"match": {"uri": ["/app*"]},
"action": {"pass": "applications/targets/app"},
},
{
"match": {"uri": "*"},
"action": {"pass": "applications/targets/catchall"},
},
],
"applications": {
"targets": {
"type": "python",
"working_directory": option.test_dir
+ "/python/targets/",
"path": option.test_dir + '/python/targets/',
"protocol": "wsgi",
"targets": {
"app": {
"module": "wsgi",
"callable": "wsgi_target_prefix",
"prefix": "/app/",
},
"catchall": {
"module": "wsgi",
"callable": "wsgi_target_prefix",
"prefix": "/api",
},
},
}
},
}
)
def check_prefix(url, body):
resp = self.get(url=url)
assert resp['status'] == 200
assert resp['body'] == body
check_prefix('/app', '/app ')
check_prefix('/app/', '/app /')
check_prefix('/app/rest/user/', '/app /rest/user/')
check_prefix('/catchall', 'No Script Name /catchall')
check_prefix('/api', '/api ')
check_prefix('/api/', '/api /')
check_prefix('/apis', 'No Script Name /apis')
check_prefix('/api/users/', '/api /users/')

View File

@@ -50,6 +50,7 @@ class TestApplicationPython(TestApplicationProto):
'protocol',
'targets',
'threads',
'prefix',
):
if attr in kwargs:
app[attr] = kwargs.pop(attr)