Skip to content

Commit 3407cc2

Browse files
authored
Support query string encoding for control API #279 (#287)
1 parent 8c24c00 commit 3407cc2

File tree

4 files changed

+227
-4
lines changed

4 files changed

+227
-4
lines changed

src/ngx_http_vhost_traffic_status_display.c

Lines changed: 49 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ static ngx_int_t
6767
ngx_http_vhost_traffic_status_display_handler_control(ngx_http_request_t *r)
6868
{
6969
ngx_int_t size, rc;
70-
ngx_str_t type, alpha, arg_cmd, arg_group, arg_zone;
70+
ngx_str_t type, alpha, encoded_ch, arg_cmd, arg_group, arg_zone;
7171
ngx_buf_t *b;
7272
ngx_chain_t out;
7373
ngx_slab_pool_t *shpool;
@@ -125,7 +125,8 @@ ngx_http_vhost_traffic_status_display_handler_control(ngx_http_request_t *r)
125125

126126
if (ngx_http_arg(r, (u_char *) "group", 5, &arg_group) == NGX_OK) {
127127

128-
if (arg_group.len == 1 && ngx_strncmp(arg_group.data, "*", 1) == 0)
128+
if ((arg_group.len == 1 && ngx_strncmp(arg_group.data, "*", 1) == 0)
129+
|| (arg_group.len == 3 && ngx_strncasecmp(arg_group.data, (u_char *) "%2A", 3) == 0))
129130
{
130131
control->group = -1;
131132
}
@@ -134,13 +135,17 @@ ngx_http_vhost_traffic_status_display_handler_control(ngx_http_request_t *r)
134135
{
135136
control->group = NGX_HTTP_VHOST_TRAFFIC_STATUS_UPSTREAM_NO;
136137
}
137-
else if (arg_group.len == 14
138+
else if ((arg_group.len == 14
138139
&& ngx_strncasecmp(arg_group.data, (u_char *) "upstream@alone", 14) == 0)
140+
|| (arg_group.len == 16
141+
&& ngx_strncasecmp(arg_group.data, (u_char *) "upstream%40alone", 16) == 0))
139142
{
140143
control->group = NGX_HTTP_VHOST_TRAFFIC_STATUS_UPSTREAM_UA;
141144
}
142-
else if (arg_group.len == 14
145+
else if ((arg_group.len == 14
143146
&& ngx_strncasecmp(arg_group.data, (u_char *) "upstream@group", 14) == 0)
147+
|| (arg_group.len == 16
148+
&& ngx_strncasecmp(arg_group.data, (u_char *) "upstream%40group", 16) == 0))
144149
{
145150
control->group = NGX_HTTP_VHOST_TRAFFIC_STATUS_UPSTREAM_UG;
146151
}
@@ -171,6 +176,46 @@ ngx_http_vhost_traffic_status_display_handler_control(ngx_http_request_t *r)
171176
"display_handler_control::copy_str() failed");
172177
}
173178

179+
ngx_str_set(&encoded_ch, "%2A");
180+
181+
rc = ngx_http_vhost_traffic_status_replace_strc(control->zone, &encoded_ch, '*');
182+
if (rc != NGX_OK) {
183+
ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
184+
"display_handler_control::replace_strc() failed");
185+
}
186+
187+
ngx_str_set(&encoded_ch, "%2a");
188+
189+
rc = ngx_http_vhost_traffic_status_replace_strc(control->zone, &encoded_ch, '*');
190+
if (rc != NGX_OK) {
191+
ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
192+
"display_handler_control::replace_strc() failed");
193+
}
194+
195+
ngx_str_set(&encoded_ch, "%3A");
196+
197+
rc = ngx_http_vhost_traffic_status_replace_strc(control->zone, &encoded_ch, ':');
198+
if (rc != NGX_OK) {
199+
ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
200+
"display_handler_control::replace_strc() failed");
201+
}
202+
203+
ngx_str_set(&encoded_ch, "%3a");
204+
205+
rc = ngx_http_vhost_traffic_status_replace_strc(control->zone, &encoded_ch, ':');
206+
if (rc != NGX_OK) {
207+
ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
208+
"display_handler_control::replace_strc() failed");
209+
}
210+
211+
ngx_str_set(&encoded_ch, "%40");
212+
213+
rc = ngx_http_vhost_traffic_status_replace_strc(control->zone, &encoded_ch, '@');
214+
if (rc != NGX_OK) {
215+
ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
216+
"display_handler_control::replace_strc() failed");
217+
}
218+
174219
(void) ngx_http_vhost_traffic_status_replace_chrc(control->zone, '@',
175220
NGX_HTTP_VHOST_TRAFFIC_STATUS_KEY_SEPARATOR);
176221

src/ngx_http_vhost_traffic_status_string.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,7 @@ ngx_http_vhost_traffic_status_replace_strc(ngx_str_t *buf,
161161
if (n > 0) {
162162
buf->len = buf->len - (n * dst->len) + n;
163163
}
164+
*(buf->data + buf->len) = '\0';
164165

165166
return NGX_OK;
166167
}

t/007.control_status_group.t

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,3 +201,96 @@ __DATA__
201201
'OK',
202202
'{"cacheZones.*cache_(one|two)'
203203
]
204+
205+
=== TEST 7: /status/control?cmd=status&group=upstream%40group&zone=%2A
206+
--- http_config
207+
vhost_traffic_status_zone;
208+
upstream backend {
209+
server localhost;
210+
}
211+
server {
212+
server_name backend;
213+
}
214+
--- config
215+
location /status {
216+
vhost_traffic_status_display;
217+
vhost_traffic_status_display_format json;
218+
access_log off;
219+
}
220+
location /backend {
221+
proxy_set_header Host backend;
222+
proxy_pass http://backend;
223+
}
224+
--- user_files eval
225+
[
226+
['backend/file.txt' => 'upstream@group:OK']
227+
]
228+
--- request eval
229+
[
230+
'GET /backend/file.txt',
231+
'GET /status/control?cmd=status&group=upstream%40group&zone=%2A',
232+
]
233+
--- response_body_like eval
234+
[
235+
'OK',
236+
'{"upstreamZones.*backend'
237+
]
238+
239+
=== TEST 8: /status/control?cmd=status&group=upstream%40group&zone=%2a
240+
--- http_config
241+
vhost_traffic_status_zone;
242+
upstream backend {
243+
server localhost;
244+
}
245+
server {
246+
server_name backend;
247+
}
248+
--- config
249+
location /status {
250+
vhost_traffic_status_display;
251+
vhost_traffic_status_display_format json;
252+
access_log off;
253+
}
254+
location /backend {
255+
proxy_set_header Host backend;
256+
proxy_pass http://backend;
257+
}
258+
--- user_files eval
259+
[
260+
['backend/file.txt' => 'upstream@group:OK']
261+
]
262+
--- request eval
263+
[
264+
'GET /backend/file.txt',
265+
'GET /status/control?cmd=status&group=upstream%40group&zone=%2a',
266+
]
267+
--- response_body_like eval
268+
[
269+
'OK',
270+
'{"upstreamZones.*backend'
271+
]
272+
273+
=== TEST 9: /status/control?cmd=status&group=server&zone=%3A%3Amain
274+
--- http_config
275+
vhost_traffic_status_zone;
276+
--- config
277+
location /status {
278+
vhost_traffic_status_display;
279+
vhost_traffic_status_display_format json;
280+
access_log off;
281+
}
282+
--- user_files eval
283+
[
284+
['storage/control/file.txt' => 'server:OK']
285+
]
286+
--- request eval
287+
[
288+
'GET /storage/control/file.txt',
289+
'GET /status/control?cmd=status&group=server&zone=%3A%3Amain',
290+
]
291+
--- response_body_like eval
292+
[
293+
'OK',
294+
'hostName'
295+
]
296+

t/008.control_status_zone.t

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,3 +175,87 @@ __DATA__
175175
'OK',
176176
'{"cache_one"'
177177
]
178+
179+
=== TEST 6: /status/control?cmd=status&group=filter&zone=storage%3A%3Alocalhost%40vol0
180+
--- http_config
181+
vhost_traffic_status_zone;
182+
--- config
183+
location /status {
184+
vhost_traffic_status_display;
185+
vhost_traffic_status_display_format json;
186+
access_log off;
187+
}
188+
location ~ ^/storage/(.+)/.*$ {
189+
set $volume $1;
190+
vhost_traffic_status_filter_by_set_key $volume storage::$server_name;
191+
}
192+
--- user_files eval
193+
[
194+
['storage/vol0/file.txt' => 'filter:OK']
195+
]
196+
--- request eval
197+
[
198+
'GET /storage/vol0/file.txt',
199+
'GET /status/control?cmd=status&group=filter&zone=storage%3A%3Alocalhost%40vol0',
200+
]
201+
--- response_body_like eval
202+
[
203+
'OK',
204+
'{"vol0"'
205+
]
206+
207+
=== TEST 7: /status/control?cmd=status&group=upstream%40alone&zone=127.0.0.1%3A1981
208+
--- http_config
209+
vhost_traffic_status_zone;
210+
--- config
211+
location /status {
212+
vhost_traffic_status_display;
213+
vhost_traffic_status_display_format json;
214+
access_log off;
215+
}
216+
location /backend {
217+
proxy_set_header Host backend;
218+
proxy_pass http://127.0.0.1:1981;
219+
}
220+
--- tcp_listen: 1981
221+
--- tcp_reply eval
222+
"HTTP/1.1 200 OK\r\n\r\nupstream\@alone:OK"
223+
--- request eval
224+
[
225+
'GET /backend/file.txt',
226+
'GET /status/control?cmd=status&group=upstream%40alone&zone=127.0.0.1%3A1981',
227+
]
228+
--- response_body_like eval
229+
[
230+
'OK',
231+
'{"server":"127.0.0.1:1981"'
232+
]
233+
234+
=== TEST 8: /status/control?cmd=status&group=filter&zone=storage%3a%3alocalhost%40vol0
235+
--- http_config
236+
vhost_traffic_status_zone;
237+
--- config
238+
location /status {
239+
vhost_traffic_status_display;
240+
vhost_traffic_status_display_format json;
241+
access_log off;
242+
}
243+
location ~ ^/storage/(.+)/.*$ {
244+
set $volume $1;
245+
vhost_traffic_status_filter_by_set_key $volume storage::$server_name;
246+
}
247+
--- user_files eval
248+
[
249+
['storage/vol0/file.txt' => 'filter:OK']
250+
]
251+
--- request eval
252+
[
253+
'GET /storage/vol0/file.txt',
254+
'GET /status/control?cmd=status&group=filter&zone=storage%3A%3Alocalhost%40vol0',
255+
]
256+
--- response_body_like eval
257+
[
258+
'OK',
259+
'{"vol0"'
260+
]
261+

0 commit comments

Comments
 (0)