Skip to content

Commit c522094

Browse files
committed
Static: variables in the "share" option.
This commit supports variable in the "share" option, the finding path to file serve is the value from "share". An example: { "share": "/www/data/static$uri" }
1 parent 37144d6 commit c522094

9 files changed

+135
-90
lines changed

docs/changes.xml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,19 @@ NGINX Unit updated to 1.26.0.
3131
date="" time=""
3232
packager="Andrei Belov <defan@nginx.com>">
3333

34+
<change type="change">
35+
<para>
36+
the "share" option now specifies the entire path to the files it serves,
37+
rather than a document root directory to be prepended to the request URI.
38+
</para>
39+
</change>
40+
41+
<change type="feature">
42+
<para>
43+
variables support in the "share" option.
44+
</para>
45+
</change>
46+
3447
<change type="feature">
3548
<para>
3649
variables support in the "chroot" option.

src/nxt_conf_validation.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -634,6 +634,7 @@ static nxt_conf_vldt_object_t nxt_conf_vldt_share_action_members[] = {
634634
{
635635
.name = nxt_string("share"),
636636
.type = NXT_CONF_VLDT_STRING,
637+
.flags = NXT_CONF_VLDT_VAR,
637638
}, {
638639
.name = nxt_string("types"),
639640
.type = NXT_CONF_VLDT_STRING | NXT_CONF_VLDT_ARRAY,

src/nxt_http_static.c

Lines changed: 73 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,11 @@
88

99

1010
typedef struct {
11-
nxt_str_t share;
11+
nxt_var_t *share;
1212
#if (NXT_HAVE_OPENAT2)
1313
nxt_var_t *chroot;
1414
nxt_uint_t resolve;
15+
u_char *fname;
1516
#endif
1617
nxt_http_route_rule_t *types;
1718
uint8_t is_const; /* 1 bit */
@@ -20,6 +21,7 @@ typedef struct {
2021

2122
typedef struct {
2223
nxt_http_action_t *action;
24+
nxt_str_t share;
2325
#if (NXT_HAVE_OPENAT2)
2426
nxt_str_t chroot;
2527
#endif
@@ -59,7 +61,7 @@ nxt_http_static_init(nxt_task_t *task, nxt_router_temp_conf_t *tmcf,
5961
nxt_http_action_t *action, nxt_http_action_conf_t *acf)
6062
{
6163
nxt_mp_t *mp;
62-
nxt_str_t *str, value;
64+
nxt_str_t str;
6365
nxt_http_static_conf_t *conf;
6466

6567
mp = tmcf->router_conf->mem_pool;
@@ -72,17 +74,19 @@ nxt_http_static_init(nxt_task_t *task, nxt_router_temp_conf_t *tmcf,
7274
action->handler = nxt_http_static;
7375
action->u.conf = conf;
7476

75-
nxt_conf_get_string(acf->share, &value);
77+
nxt_conf_get_string(acf->share, &str);
7678

77-
str = nxt_str_dup(mp, &conf->share, &value);
78-
if (nxt_slow_path(str == NULL)) {
79+
conf->share = nxt_var_compile(&str, mp, 1);
80+
if (nxt_slow_path(conf->share == NULL)) {
7981
return NXT_ERROR;
8082
}
8183

82-
conf->is_const = 1;
84+
conf->is_const = nxt_var_is_const(conf->share);
8385

8486
#if (NXT_HAVE_OPENAT2)
8587
if (acf->chroot.length > 0) {
88+
nxt_str_t chr, shr;
89+
8690
if (nxt_is_var(&acf->chroot)) {
8791
conf->is_const = 0;
8892
}
@@ -91,6 +95,13 @@ nxt_http_static_init(nxt_task_t *task, nxt_router_temp_conf_t *tmcf,
9195
if (nxt_slow_path(conf->chroot == NULL)) {
9296
return NXT_ERROR;
9397
}
98+
99+
if (conf->is_const) {
100+
nxt_var_raw(conf->chroot, &chr);
101+
nxt_var_raw(conf->share, &shr);
102+
103+
conf->fname = nxt_http_static_chroot_match(chr.start, shr.start);
104+
}
94105
}
95106

96107
if (acf->follow_symlinks != NULL
@@ -155,7 +166,12 @@ nxt_http_static(nxt_task_t *task, nxt_http_request_t *r,
155166

156167
conf = action->u.conf;
157168

158-
#if (NXT_DEBUG && NXT_HAVE_OPENAT2)
169+
#if (NXT_DEBUG)
170+
nxt_str_t shr;
171+
172+
nxt_var_raw(conf->share, &shr);
173+
174+
#if (NXT_HAVE_OPENAT2)
159175
nxt_str_t chr;
160176

161177
if (conf->chroot != NULL) {
@@ -165,11 +181,11 @@ nxt_http_static(nxt_task_t *task, nxt_http_request_t *r,
165181
nxt_str_set(&chr, "");
166182
}
167183

168-
nxt_debug(task, "http static: \"%V\" (chroot: \"%V\")", &conf->share, &chr);
169-
184+
nxt_debug(task, "http static: \"%V\" (chroot: \"%V\")", &shr, &chr);
170185
#else
171-
nxt_debug(task, "http static: \"%V\"", &conf->share);
186+
nxt_debug(task, "http static: \"%V\"", &shr);
172187
#endif
188+
#endif /* NXT_DEBUG */
173189

174190
ctx = nxt_mp_zget(r->mem_pool, sizeof(nxt_http_static_ctx_t));
175191
if (nxt_slow_path(ctx == NULL)) {
@@ -180,6 +196,8 @@ nxt_http_static(nxt_task_t *task, nxt_http_request_t *r,
180196
ctx->need_body = need_body;
181197

182198
if (conf->is_const) {
199+
nxt_var_raw(conf->share, &ctx->share);
200+
183201
#if (NXT_HAVE_OPENAT2)
184202
if (conf->chroot != NULL) {
185203
nxt_var_raw(conf->chroot, &ctx->chroot);
@@ -194,8 +212,12 @@ nxt_http_static(nxt_task_t *task, nxt_http_request_t *r,
194212
goto fail;
195213
}
196214

215+
nxt_var_query(task, r->var_query, conf->share, &ctx->share);
216+
197217
#if (NXT_HAVE_OPENAT2)
198-
nxt_var_query(task, r->var_query, conf->chroot, &ctx->chroot);
218+
if (conf->chroot != NULL) {
219+
nxt_var_query(task, r->var_query, conf->chroot, &ctx->chroot);
220+
}
199221
#endif
200222

201223
nxt_var_query_resolve(task, r->var_query, ctx,
@@ -220,7 +242,7 @@ nxt_http_static_send_ready(nxt_task_t *task, void *obj, void *data)
220242
struct tm tm;
221243
nxt_buf_t *fb;
222244
nxt_int_t ret;
223-
nxt_str_t index, exten, *mtype;
245+
nxt_str_t *shr, exten, *mtype;
224246
nxt_uint_t level;
225247
nxt_file_t *f, file;
226248
nxt_file_info_t fi;
@@ -233,56 +255,58 @@ nxt_http_static_send_ready(nxt_task_t *task, void *obj, void *data)
233255
nxt_http_static_ctx_t *ctx;
234256
nxt_http_static_conf_t *conf;
235257

258+
static nxt_str_t index = nxt_string("index.html");
259+
236260
r = obj;
237261
ctx = data;
238262
action = ctx->action;
239263
conf = action->u.conf;
240-
241-
if (r->path->start[r->path->length - 1] == '/') {
242-
/* TODO: dynamic index setting. */
243-
nxt_str_set(&index, "index.html");
244-
nxt_str_set(&exten, ".html");
245-
246-
} else {
247-
nxt_str_set(&index, "");
248-
nxt_str_null(&exten);
249-
}
264+
rtcf = r->conf->socket_conf->router_conf;
250265

251266
f = NULL;
267+
mtype = NULL;
252268
status = NXT_HTTP_INTERNAL_SERVER_ERROR;
253269

254-
rtcf = r->conf->socket_conf->router_conf;
270+
shr = &ctx->share;
255271

256-
mtype = NULL;
272+
if (shr->start[shr->length - 1] == '/') {
273+
/* TODO: dynamic index setting. */
274+
nxt_str_set(&exten, ".html");
257275

258-
if (conf->types != NULL && exten.start == NULL) {
259-
nxt_http_static_extract_extension(r->path, &exten);
260-
mtype = nxt_http_static_mtype_get(&rtcf->mtypes_hash, &exten);
276+
length = shr->length + index.length;
261277

262-
ret = nxt_http_route_test_rule(r, conf->types, mtype->start,
263-
mtype->length);
264-
if (nxt_slow_path(ret == NXT_ERROR)) {
278+
fname = nxt_mp_nget(r->mem_pool, length + 1);
279+
if (nxt_slow_path(fname == NULL)) {
265280
goto fail;
266281
}
267282

268-
if (ret == 0) {
269-
status = NXT_HTTP_FORBIDDEN;
270-
goto fail;
271-
}
272-
}
283+
p = fname;
284+
p = nxt_cpymem(p, shr->start, shr->length);
285+
p = nxt_cpymem(p, index.start, index.length);
286+
*p = '\0';
273287

274-
length = conf->share.length + r->path->length + index.length;
288+
} else {
289+
if (conf->types == NULL) {
290+
nxt_str_null(&exten);
275291

276-
fname = nxt_mp_nget(r->mem_pool, length + 1);
277-
if (nxt_slow_path(fname == NULL)) {
278-
goto fail;
279-
}
292+
} else {
293+
nxt_http_static_extract_extension(shr, &exten);
294+
mtype = nxt_http_static_mtype_get(&rtcf->mtypes_hash, &exten);
295+
296+
ret = nxt_http_route_test_rule(r, conf->types, mtype->start,
297+
mtype->length);
298+
if (nxt_slow_path(ret == NXT_ERROR)) {
299+
goto fail;
300+
}
280301

281-
p = fname;
282-
p = nxt_cpymem(p, conf->share.start, conf->share.length);
283-
p = nxt_cpymem(p, r->path->start, r->path->length);
284-
p = nxt_cpymem(p, index.start, index.length);
285-
*p = '\0';
302+
if (ret == 0) {
303+
status = NXT_HTTP_FORBIDDEN;
304+
goto fail;
305+
}
306+
}
307+
308+
fname = ctx->share.start;
309+
}
286310

287311
nxt_memzero(&file, sizeof(nxt_file_t));
288312

@@ -299,7 +323,9 @@ nxt_http_static_send_ready(nxt_task_t *task, void *obj, void *data)
299323
if (chr->length > 0) {
300324
resolve |= RESOLVE_IN_ROOT;
301325

302-
fname = nxt_http_static_chroot_match(chr->start, file.name);
326+
fname = conf->is_const
327+
? conf->fname
328+
: nxt_http_static_chroot_match(chr->start, file.name);
303329

304330
if (fname != NULL) {
305331
file.name = chr->start;
@@ -460,7 +486,7 @@ nxt_http_static_send_ready(nxt_task_t *task, void *obj, void *data)
460486
- p;
461487

462488
if (exten.start == NULL) {
463-
nxt_http_static_extract_extension(r->path, &exten);
489+
nxt_http_static_extract_extension(shr, &exten);
464490
}
465491

466492
if (mtype == NULL) {

test/test_static.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,9 @@ def setup_method(self):
2828
self._load_conf(
2929
{
3030
"listeners": {"*:7080": {"pass": "routes"}},
31-
"routes": [{"action": {"share": option.temp_dir + "/assets"}}],
31+
"routes": [
32+
{"action": {"share": option.temp_dir + "/assets$uri"}}
33+
],
3234
"settings": {
3335
"http": {
3436
"static": {

test/test_static_chroot.py

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ def setup_method_fixture(self, temp_dir):
2121
self._load_conf(
2222
{
2323
"listeners": {"*:7080": {"pass": "routes"}},
24-
"routes": [{"action": {"share": temp_dir + "/assets"}}],
24+
"routes": [{"action": {"share": temp_dir + "/assets$uri"}}],
2525
}
2626
)
2727

@@ -31,7 +31,7 @@ def test_static_chroot(self, temp_dir):
3131

3232
assert 'success' in self.conf(
3333
{
34-
"share": temp_dir + "/assets",
34+
"share": temp_dir + "/assets$uri",
3535
"chroot": temp_dir + "/assets/dir",
3636
},
3737
'routes/0/action',
@@ -49,7 +49,7 @@ def test_static_chroot_permission(self, is_su, temp_dir):
4949

5050
assert 'success' in self.conf(
5151
{
52-
"share": temp_dir + "/assets",
52+
"share": temp_dir + "/assets$uri",
5353
"chroot": temp_dir + "/assets/dir",
5454
},
5555
'routes/0/action',
@@ -59,15 +59,16 @@ def test_static_chroot_permission(self, is_su, temp_dir):
5959

6060
def test_static_chroot_empty(self, temp_dir):
6161
assert 'success' in self.conf(
62-
{"share": temp_dir + "/assets", "chroot": ""}, 'routes/0/action',
62+
{"share": temp_dir + "/assets$uri", "chroot": ""},
63+
'routes/0/action',
6364
), 'configure chroot empty absolute'
6465

6566
assert (
6667
self.get(url='/dir/file')['status'] == 200
6768
), 'chroot empty absolute'
6869

6970
assert 'success' in self.conf(
70-
{"share": ".", "chroot": ""}, 'routes/0/action',
71+
{"share": ".$uri", "chroot": ""}, 'routes/0/action',
7172
), 'configure chroot empty relative'
7273

7374
assert (
@@ -79,19 +80,20 @@ def test_static_chroot_relative(self, is_su, temp_dir):
7980
pytest.skip('does\'t work under root')
8081

8182
assert 'success' in self.conf(
82-
{"share": temp_dir + "/assets", "chroot": "."}, 'routes/0/action',
83+
{"share": temp_dir + "/assets$uri", "chroot": "."},
84+
'routes/0/action',
8385
), 'configure relative chroot'
8486

8587
assert self.get(url='/dir/file')['status'] == 403, 'relative chroot'
8688

8789
assert 'success' in self.conf(
88-
{"share": "."}, 'routes/0/action',
90+
{"share": ".$uri"}, 'routes/0/action',
8991
), 'configure relative share'
9092

9193
assert self.get(url=self.test_path)['status'] == 200, 'relative share'
9294

9395
assert 'success' in self.conf(
94-
{"share": ".", "chroot": "."}, 'routes/0/action',
96+
{"share": ".$uri", "chroot": "."}, 'routes/0/action',
9597
), 'configure relative'
9698

9799
assert self.get(url=self.test_path)['status'] == 200, 'relative'

test/test_static_fallback.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ def setup_method_fixture(self, temp_dir):
2323
"*:7080": {"pass": "routes"},
2424
"*:7081": {"pass": "routes"},
2525
},
26-
"routes": [{"action": {"share": temp_dir + "/assets"}}],
26+
"routes": [{"action": {"share": temp_dir + "/assets$uri"}}],
2727
"applications": {},
2828
}
2929
)
@@ -50,7 +50,7 @@ def test_static_fallback(self):
5050

5151
def test_static_fallback_valid_path(self, temp_dir):
5252
self.action_update(
53-
{"share": temp_dir + "/assets", "fallback": {"return": 200}}
53+
{"share": temp_dir + "/assets$uri", "fallback": {"return": 200}}
5454
)
5555
resp = self.get()
5656
assert resp['status'] == 200, 'fallback status'
@@ -83,7 +83,7 @@ def test_static_fallback_nested(self):
8383

8484
def test_static_fallback_share(self, temp_dir):
8585
self.action_update(
86-
{"share": "/blah", "fallback": {"share": temp_dir + "/assets"},}
86+
{"share": "/blah", "fallback": {"share": temp_dir + "/assets$uri"},}
8787
)
8888

8989
resp = self.get()

0 commit comments

Comments
 (0)