summaryrefslogtreecommitdiffhomepage
path: root/src/nxt_http_return.c
blob: b50e4ad0e9536d840ebedc9997f37600f22cc624 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234

/*
 * Copyright (C) NGINX, Inc.
 */

#include <nxt_router.h>
#include <nxt_http.h>


typedef struct {
    nxt_http_status_t  status;
    nxt_tstr_t         *location;
    nxt_str_t          encoded;
} nxt_http_return_conf_t;


typedef struct {
    nxt_str_t          location;
    nxt_str_t          encoded;
} nxt_http_return_ctx_t;


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 const nxt_http_request_state_t  nxt_http_return_send_state;


nxt_int_t
nxt_http_return_init(nxt_router_conf_t *rtcf, nxt_http_action_t *action,
    nxt_http_action_conf_t *acf)
{
    nxt_mp_t                *mp;
    nxt_str_t               str;
    nxt_http_return_conf_t  *conf;

    mp = rtcf->mem_pool;

    conf = nxt_mp_zget(mp, sizeof(nxt_http_return_conf_t));
    if (nxt_slow_path(conf == NULL)) {
        return NXT_ERROR;
    }

    action->handler = nxt_http_return;
    action->u.conf = conf;

    conf->status = nxt_conf_get_number(acf->ret);

    if (acf->location == NULL) {
        return NXT_OK;
    }

    nxt_conf_get_string(acf->location, &str);

    conf->location = nxt_tstr_compile(rtcf->tstr_state, &str, 0);
    if (nxt_slow_path(conf->location == NULL)) {
        return NXT_ERROR;
    }

    if (nxt_tstr_is_const(conf->location)) {
        nxt_tstr_str(conf->location, &str);
        return nxt_http_return_encode(mp, &conf->encoded, &str);
    }

    return NXT_OK;
}


nxt_http_action_t *
nxt_http_return(nxt_task_t *task, nxt_http_request_t *r,
    nxt_http_action_t *action)
{
    nxt_int_t               ret;
    nxt_router_conf_t       *rtcf;
    nxt_http_return_ctx_t   *ctx;
    nxt_http_return_conf_t  *conf;

    conf = action->u.conf;

#if (NXT_DEBUG)
    nxt_str_t  loc;

    if (conf->location == NULL) {
        nxt_str_set(&loc, "");

    } else {
        nxt_tstr_str(conf->location, &loc);
    }

    nxt_debug(task, "http return: %d (loc: \"%V\")", conf->status, &loc);
#endif

    if (conf->status >= NXT_HTTP_BAD_REQUEST
        && conf->status <= NXT_HTTP_SERVER_ERROR_MAX)
    {
        nxt_http_request_error(task, r, conf->status);
        return NULL;
    }

    if (conf->location == NULL) {
        ctx = NULL;

    } else {
        ctx = nxt_mp_zget(r->mem_pool, sizeof(nxt_http_return_ctx_t));
        if (nxt_slow_path(ctx == NULL)) {
            goto fail;
        }
    }

    r->status = conf->status;
    r->resp.content_length_n = 0;

    if (ctx == NULL || nxt_tstr_is_const(conf->location)) {
        if (ctx != NULL) {
            ctx->encoded = conf->encoded;
        }

        nxt_http_return_send_ready(task, r, ctx);

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

        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)) {
            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);
    }

    return NULL;

fail:

    nxt_http_request_error(task, r, NXT_HTTP_INTERNAL_SERVER_ERROR);
    return NULL;
}


static nxt_int_t
nxt_http_return_encode(nxt_mp_t *mp, nxt_str_t *encoded,
    const nxt_str_t *location)
{
    nxt_uint_t  encode;

    if (nxt_is_complex_uri_encoded(location->start, location->length)) {
        *encoded = *location;

        return NXT_OK;
    }

    encode = nxt_encode_complex_uri(NULL, location->start, location->length);
    encoded->length = location->length + encode * 2;

    encoded->start = nxt_mp_nget(mp, encoded->length);
    if (nxt_slow_path(encoded->start == NULL)) {
        return NXT_ERROR;
    }

    nxt_encode_complex_uri(encoded->start, location->start, location->length);

    return NXT_OK;
}


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

    r = obj;
    ctx = data;

    if (ctx != NULL) {
        if (ctx->location.length > 0) {
            ret = nxt_http_return_encode(r->mem_pool, &ctx->encoded,
                                         &ctx->location);
            if (nxt_slow_path(ret == NXT_ERROR)) {
                goto fail;
            }
        }

        field = nxt_list_zero_add(r->resp.fields);
        if (nxt_slow_path(field == NULL)) {
            goto fail;
        }

        nxt_http_field_name_set(field, "Location");

        field->value = ctx->encoded.start;
        field->value_length = ctx->encoded.length;
    }

    r->state = &nxt_http_return_send_state;

    nxt_http_request_header_send(task, r, NULL, NULL);

    return;

fail:

    nxt_http_request_error(task, r, NXT_HTTP_INTERNAL_SERVER_ERROR);
}


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) =
{
    .error_handler = nxt_http_request_error_handler,
};