summaryrefslogtreecommitdiffhomepage
path: root/src/nxt_http_set_headers.c
blob: 25dd7478f5d3979491256f1318da302a4aa343b5 (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

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

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


typedef struct {
    nxt_str_t               name;
    nxt_tstr_t              *value;
} nxt_http_header_val_t;


nxt_int_t
nxt_http_set_headers_init(nxt_router_conf_t *rtcf, nxt_http_action_t *action,
     nxt_http_action_conf_t *acf)
 {
    uint32_t               next;
    nxt_str_t              str, name;
    nxt_array_t            *headers;
    nxt_conf_value_t       *value;
    nxt_http_header_val_t  *hv;

    headers = nxt_array_create(rtcf->mem_pool, 4,
                               sizeof(nxt_http_header_val_t));
    if (nxt_slow_path(headers == NULL)) {
        return NXT_ERROR;
    }

    action->set_headers = headers;

    next = 0;

    for ( ;; ) {
        value = nxt_conf_next_object_member(acf->set_headers, &name, &next);
        if (value == NULL) {
            break;
        }

        hv = nxt_array_zero_add(headers);
        if (nxt_slow_path(hv == NULL)) {
            return NXT_ERROR;
        }

        hv->name.length = name.length;

        hv->name.start = nxt_mp_nget(rtcf->mem_pool, name.length);
        if (nxt_slow_path(hv->name.start == NULL)) {
            return NXT_ERROR;
        }

        nxt_memcpy(hv->name.start, name.start, name.length);

        if (nxt_conf_type(value) == NXT_CONF_STRING) {
            nxt_conf_get_string(value, &str);

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

    return NXT_OK;
}


static nxt_http_field_t *
nxt_http_resp_header_find(nxt_http_request_t *r, u_char *name, size_t length)
{
    nxt_http_field_t  *f;

    nxt_list_each(f, r->resp.fields) {

        if (f->skip) {
            continue;
        }

        if (length == f->name_length
            && nxt_memcasecmp(name, f->name, f->name_length) == 0)
        {
            return f;
        }

    } nxt_list_loop;

    return NULL;
}


nxt_int_t
nxt_http_set_headers(nxt_http_request_t *r)
{
    nxt_int_t              ret;
    nxt_uint_t             i, n;
    nxt_str_t              *value;
    nxt_http_field_t       *f;
    nxt_router_conf_t      *rtcf;
    nxt_http_action_t      *action;
    nxt_http_header_val_t  *hv, *header;

    action = r->action;

    if (action == NULL || action->set_headers == NULL) {
        return NXT_OK;
    }

    if ((r->status < NXT_HTTP_OK || r->status >= NXT_HTTP_BAD_REQUEST)) {
        return NXT_OK;
    }

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

    header = action->set_headers->elts;
    n = action->set_headers->nelts;

    value = nxt_mp_zalloc(r->mem_pool, sizeof(nxt_str_t) * n);
    if (nxt_slow_path(value == NULL)) {
        return NXT_ERROR;
    }

    for (i = 0; i < n; i++) {
        hv = &header[i];

        if (hv->value == NULL) {
            continue;
        }

        if (nxt_tstr_is_const(hv->value)) {
            nxt_tstr_str(hv->value, &value[i]);

        } 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_ERROR;
            }

            nxt_tstr_query(&r->task, r->tstr_query, hv->value, &value[i]);

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

    for (i = 0; i < n; i++) {
        hv = &header[i];

        f = nxt_http_resp_header_find(r, hv->name.start, hv->name.length);

        if (value[i].start != NULL) {

            if (f == NULL) {
                f = nxt_list_zero_add(r->resp.fields);
                if (nxt_slow_path(f == NULL)) {
                    return NXT_ERROR;
                }

                f->name = hv->name.start;
                f->name_length = hv->name.length;
            }

            f->value = value[i].start;
            f->value_length = value[i].length;

        } else if (f != NULL) {
            f->skip = 1;
        }
    }

    return NXT_OK;
}