Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add "if" option to the "match" object. #1204

Merged
merged 10 commits into from
Aug 20, 2024
4 changes: 4 additions & 0 deletions src/nxt_conf_validation.c
Original file line number Diff line number Diff line change
Expand Up @@ -696,6 +696,10 @@ static nxt_conf_vldt_object_t nxt_conf_vldt_match_members[] = {
.type = NXT_CONF_VLDT_OBJECT | NXT_CONF_VLDT_ARRAY,
.validator = nxt_conf_vldt_match_patterns_sets,
.u.string = "cookies"
}, {
.name = nxt_string("if"),
.type = NXT_CONF_VLDT_STRING,
.validator = nxt_conf_vldt_if,
},

NXT_CONF_VLDT_END
Expand Down
3 changes: 3 additions & 0 deletions src/nxt_http.h
Original file line number Diff line number Diff line change
Expand Up @@ -441,6 +441,9 @@ void nxt_h1p_complete_buffers(nxt_task_t *task, nxt_h1proto_t *h1p,
nxt_bool_t all);
nxt_msec_t nxt_h1p_conn_request_timer_value(nxt_conn_t *c, uintptr_t data);

int nxt_http_cond_value(nxt_task_t *task, nxt_http_request_t *r,
nxt_tstr_cond_t *cond);

extern const nxt_conn_state_t nxt_h1p_idle_close_state;

#endif /* _NXT_HTTP_H_INCLUDED_ */
106 changes: 50 additions & 56 deletions src/nxt_http_request.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,6 @@ static void nxt_http_request_proto_info(nxt_task_t *task,
static void nxt_http_request_mem_buf_completion(nxt_task_t *task, void *obj,
void *data);
static void nxt_http_request_done(nxt_task_t *task, void *obj, void *data);
static nxt_int_t nxt_http_request_access_log(nxt_task_t *task,
nxt_http_request_t *r, nxt_router_conf_t *rtcf);

static u_char *nxt_http_date_cache_handler(u_char *buf, nxt_realtime_t *now,
struct tm *tm, size_t size, const char *format);
Expand Down Expand Up @@ -861,12 +859,12 @@ nxt_http_request_error_handler(nxt_task_t *task, void *obj, void *data)
void
nxt_http_request_close_handler(nxt_task_t *task, void *obj, void *data)
{
nxt_int_t ret;
nxt_http_proto_t proto;
nxt_router_conf_t *rtcf;
nxt_http_request_t *r;
nxt_http_protocol_t protocol;
nxt_socket_conf_joint_t *conf;
nxt_router_access_log_t *access_log;

r = obj;
proto.any = data;
Expand All @@ -878,8 +876,10 @@ nxt_http_request_close_handler(nxt_task_t *task, void *obj, void *data)
r->logged = 1;

if (rtcf->access_log != NULL) {
ret = nxt_http_request_access_log(task, r, rtcf);
if (ret == NXT_OK) {
access_log = rtcf->access_log;

if (nxt_http_cond_value(task, r, &rtcf->log_cond)) {
access_log->handler(task, r, access_log, rtcf->log_format);
return;
}
}
Expand Down Expand Up @@ -911,57 +911,6 @@ nxt_http_request_close_handler(nxt_task_t *task, void *obj, void *data)
}


static nxt_int_t
nxt_http_request_access_log(nxt_task_t *task, nxt_http_request_t *r,
nxt_router_conf_t *rtcf)
{
nxt_int_t ret;
nxt_str_t str;
nxt_bool_t expr;
nxt_router_access_log_t *access_log;

access_log = rtcf->access_log;

expr = 1;

if (rtcf->log_expr != NULL) {

if (nxt_tstr_is_const(rtcf->log_expr)) {
nxt_tstr_str(rtcf->log_expr, &str);

} else {
ret = nxt_tstr_query_init(&r->tstr_query, rtcf->tstr_state,
&r->tstr_cache, r, r->mem_pool);
if (nxt_slow_path(ret != NXT_OK)) {
return NXT_DECLINED;
}

nxt_tstr_query(task, r->tstr_query, rtcf->log_expr, &str);

if (nxt_slow_path(nxt_tstr_query_failed(r->tstr_query))) {
return NXT_DECLINED;
}
}

if (str.length == 0
|| nxt_str_eq(&str, "0", 1)
|| nxt_str_eq(&str, "false", 5)
|| nxt_str_eq(&str, "null", 4)
|| nxt_str_eq(&str, "undefined", 9))
{
expr = 0;
}
}

if (rtcf->log_negate ^ expr) {
access_log->handler(task, r, access_log, rtcf->log_format);
return NXT_OK;
}

return NXT_DECLINED;
}


static u_char *
nxt_http_date_cache_handler(u_char *buf, nxt_realtime_t *now, struct tm *tm,
size_t size, const char *format)
Expand Down Expand Up @@ -1365,3 +1314,48 @@ nxt_http_cookie_hash(nxt_mp_t *mp, nxt_str_t *name)
{
return nxt_http_field_hash(mp, name, 1, NXT_HTTP_URI_ENCODING_NONE);
}


int
nxt_http_cond_value(nxt_task_t *task, nxt_http_request_t *r,
nxt_tstr_cond_t *cond)
{
nxt_int_t ret;
nxt_str_t str;
nxt_bool_t expr;
nxt_router_conf_t *rtcf;

rtcf = r->conf->socket_conf->router_conf;

expr = 1;

if (cond->expr != NULL) {

if (nxt_tstr_is_const(cond->expr)) {
nxt_tstr_str(cond->expr, &str);

} else {
ret = nxt_tstr_query_init(&r->tstr_query, rtcf->tstr_state,
&r->tstr_cache, r, r->mem_pool);
if (nxt_slow_path(ret != NXT_OK)) {
return -1;
}

ret = nxt_tstr_query(task, r->tstr_query, cond->expr, &str);
if (nxt_slow_path(ret != NXT_OK)) {
return -1;
}
}

if (str.length == 0
|| nxt_str_eq(&str, "0", 1)
|| nxt_str_eq(&str, "false", 5)
|| nxt_str_eq(&str, "null", 4)
|| nxt_str_eq(&str, "undefined", 9))
{
expr = 0;
}
}

return cond->negate ^ expr;
}
41 changes: 13 additions & 28 deletions src/nxt_http_return.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ static nxt_http_action_t *nxt_http_return(nxt_task_t *task,
nxt_http_request_t *r, nxt_http_action_t *action);
static nxt_int_t nxt_http_return_encode(nxt_mp_t *mp, nxt_str_t *encoded,
const nxt_str_t *location);
static void nxt_http_return_send_ready(nxt_task_t *task, void *obj, void *data);
static void nxt_http_return_send_error(nxt_task_t *task, void *obj, void *data);
static void nxt_http_return_send(nxt_task_t *task, nxt_http_request_t *r,
nxt_http_return_ctx_t *ctx);


static const nxt_http_request_state_t nxt_http_return_send_state;
Expand Down Expand Up @@ -120,8 +120,6 @@ nxt_http_return(nxt_task_t *task, nxt_http_request_t *r,
ctx->encoded = conf->encoded;
}

nxt_http_return_send_ready(task, r, ctx);

} else {
rtcf = r->conf->socket_conf->router_conf;

Expand All @@ -131,13 +129,15 @@ nxt_http_return(nxt_task_t *task, nxt_http_request_t *r,
goto fail;
}

nxt_tstr_query(task, r->tstr_query, conf->location, &ctx->location);

nxt_tstr_query_resolve(task, r->tstr_query, ctx,
nxt_http_return_send_ready,
nxt_http_return_send_error);
ret = nxt_tstr_query(task, r->tstr_query, conf->location,
&ctx->location);
if (nxt_slow_path(ret != NXT_OK)) {
goto fail;
}
}

nxt_http_return_send(task, r, ctx);

return NULL;

fail:
Expand Down Expand Up @@ -174,15 +174,11 @@ nxt_http_return_encode(nxt_mp_t *mp, nxt_str_t *encoded,


static void
nxt_http_return_send_ready(nxt_task_t *task, void *obj, void *data)
nxt_http_return_send(nxt_task_t *task, nxt_http_request_t *r,
nxt_http_return_ctx_t *ctx)
{
nxt_int_t ret;
nxt_http_field_t *field;
nxt_http_request_t *r;
nxt_http_return_ctx_t *ctx;

r = obj;
ctx = data;
nxt_int_t ret;
nxt_http_field_t *field;

if (ctx != NULL) {
if (ctx->location.length > 0) {
Expand Down Expand Up @@ -216,17 +212,6 @@ nxt_http_return_send_ready(nxt_task_t *task, void *obj, void *data)
}


static void
nxt_http_return_send_error(nxt_task_t *task, void *obj, void *data)
{
nxt_http_request_t *r;

r = obj;

nxt_http_request_error(task, r, NXT_HTTP_INTERNAL_SERVER_ERROR);
}


static const nxt_http_request_state_t nxt_http_return_send_state
nxt_aligned(64) =
{
Expand Down
5 changes: 2 additions & 3 deletions src/nxt_http_rewrite.c
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,8 @@ nxt_http_rewrite(nxt_task_t *task, nxt_http_request_t *r)
return NXT_ERROR;
}

nxt_tstr_query(task, r->tstr_query, action->rewrite, &str);

if (nxt_slow_path(nxt_tstr_query_failed(r->tstr_query))) {
ret = nxt_tstr_query(task, r->tstr_query, action->rewrite, &str);
if (nxt_slow_path(ret != NXT_OK)) {
return NXT_ERROR;
}
}
Expand Down
Loading
Loading