summaryrefslogtreecommitdiffhomepage
path: root/src/nxt_js.c
blob: 4327e848762f2cde3d5881aa3c7e5866a13a8814 (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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316

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

#include <nxt_main.h>


struct nxt_js_s {
    uint32_t            index;
    njs_vm_t            *vm;
};


struct nxt_js_conf_s {
    nxt_mp_t            *pool;
    njs_vm_t            *vm;
    njs_uint_t          protos;
    njs_external_t      *proto;
    nxt_array_t         *funcs;
};


njs_int_t  nxt_js_proto_id;


nxt_js_conf_t *
nxt_js_conf_new(nxt_mp_t *mp)
{
    njs_vm_opt_t   opts;
    nxt_js_conf_t  *jcf;

    jcf = nxt_mp_zget(mp, sizeof(nxt_js_conf_t));
    if (nxt_slow_path(jcf == NULL)) {
        return NULL;
    }

    jcf->pool = mp;

    njs_vm_opt_init(&opts);

    jcf->vm = njs_vm_create(&opts);
    if (nxt_slow_path(jcf->vm == NULL)) {
        return NULL;
    }

    jcf->funcs = nxt_array_create(mp, 4, sizeof(nxt_str_t));
    if (nxt_slow_path(jcf->funcs == NULL)) {
        njs_vm_destroy(jcf->vm);
        return NULL;
    }

    return jcf;
}


void
nxt_js_conf_release(nxt_js_conf_t *jcf)
{
    njs_vm_destroy(jcf->vm);
}


void
nxt_js_set_proto(nxt_js_conf_t *jcf, njs_external_t *proto, njs_uint_t n)
{
    jcf->protos = n;
    jcf->proto = proto;
}


nxt_js_t *
nxt_js_add_tpl(nxt_js_conf_t *jcf, nxt_str_t *str, nxt_bool_t strz)
{
    size_t     size;
    u_char     *p, *start;
    nxt_js_t   *js;
    nxt_str_t  *func;

    static nxt_str_t  func_str = nxt_string("function(uri, host, remoteAddr, "
                                            "args, headers, cookies) {"
                                            "    return ");

    /*
     * Appending a terminating null character if strz is true.
     */
    static nxt_str_t  strz_str = nxt_string(" + '\\x00'");

    size = func_str.length + str->length + 1;

    if (strz) {
        size += strz_str.length;
    }

    start = nxt_mp_nget(jcf->pool, size);
    if (nxt_slow_path(start == NULL)) {
        return NULL;
    }

    p = start;

    p = nxt_cpymem(p, func_str.start, func_str.length);
    p = nxt_cpymem(p, str->start, str->length);

    if (strz) {
        p = nxt_cpymem(p, strz_str.start, strz_str.length);
    }

    *p++ = '}';

    js = nxt_mp_get(jcf->pool, sizeof(nxt_js_t));
    if (nxt_slow_path(js == NULL)) {
        return NULL;
    }

    js->vm = jcf->vm;

    func = nxt_array_add(jcf->funcs);
    if (nxt_slow_path(func == NULL)) {
        return NULL;
    }

    func->start = start;
    func->length = p - start;

    js->index = jcf->funcs->nelts - 1;

    return js;
}


nxt_int_t
nxt_js_compile(nxt_js_conf_t *jcf)
{
    size_t      size;
    u_char      *p, *start;
    njs_int_t   ret;
    nxt_str_t   *func;
    nxt_uint_t  i;

    size = 2;
    func = jcf->funcs->elts;

    for (i = 0; i < jcf->funcs->nelts; i++) {
        size += func[i].length + 1;
    }

    start = nxt_mp_nget(jcf->pool, size);
    if (nxt_slow_path(start == NULL)) {
        return NXT_ERROR;
    }

    p = start;
    *p++ = '[';

    func = jcf->funcs->elts;

    for (i = 0; i < jcf->funcs->nelts; i++) {
        p = nxt_cpymem(p, func[i].start, func[i].length);
        *p++ = ',';
    }

    *p++ = ']';

    nxt_js_proto_id = njs_vm_external_prototype(jcf->vm, jcf->proto,
                                                jcf->protos);
    if (nxt_slow_path(nxt_js_proto_id < 0)) {
        return NXT_ERROR;
    }

    ret = njs_vm_compile(jcf->vm, &start, p);

    return (ret == NJS_OK) ? NXT_OK : NXT_ERROR;
}


nxt_int_t
nxt_js_test(nxt_js_conf_t *jcf, nxt_str_t *str, u_char *error)
{
    u_char     *start;
    nxt_str_t  err;
    njs_int_t  ret;
    njs_str_t  res;

    start = nxt_mp_nget(jcf->pool, str->length);
    if (nxt_slow_path(start == NULL)) {
        return NXT_ERROR;
    }

    nxt_memcpy(start, str->start, str->length);

    ret = njs_vm_compile(jcf->vm, &start, start + str->length);

    if (nxt_slow_path(ret != NJS_OK)) {
        (void) njs_vm_retval_string(jcf->vm, &res);

        err.start = res.start;
        err.length = res.length;

        nxt_sprintf(error, error + NXT_MAX_ERROR_STR, "\"%V\"%Z", &err);

        return NXT_ERROR;
    }

    return NXT_OK;
}


nxt_int_t
nxt_js_call(nxt_task_t *task, nxt_js_cache_t *cache, nxt_js_t *js,
    nxt_str_t *str, void *ctx)
{
    njs_vm_t            *vm;
    njs_int_t           rc, ret;
    njs_str_t           res;
    njs_value_t         *array, *value;
    njs_function_t      *func;
    njs_opaque_value_t  opaque_value, arguments[6];

    static const njs_str_t  uri_str = njs_str("uri");
    static const njs_str_t  host_str = njs_str("host");
    static const njs_str_t  remote_addr_str = njs_str("remoteAddr");
    static const njs_str_t  args_str = njs_str("args");
    static const njs_str_t  headers_str = njs_str("headers");
    static const njs_str_t  cookies_str = njs_str("cookies");

    vm = cache->vm;

    if (vm == NULL) {
        vm = njs_vm_clone(js->vm, ctx);
        if (nxt_slow_path(vm == NULL)) {
            return NXT_ERROR;
        }

        ret = njs_vm_start(vm);
        if (ret != NJS_OK) {
            return NXT_ERROR;
        }

        array = njs_vm_retval(vm);

        cache->vm = vm;
        cache->array = *array;
    }

    value = njs_vm_array_prop(vm, &cache->array, js->index, &opaque_value);
    func = njs_value_function(value);

    ret = njs_vm_external_create(vm, njs_value_arg(&opaque_value),
                                 nxt_js_proto_id, ctx, 0);
    if (nxt_slow_path(ret != NJS_OK)) {
        return NXT_ERROR;
    }

    value = njs_vm_object_prop(vm, njs_value_arg(&opaque_value), &uri_str,
                               &arguments[0]);
    if (nxt_slow_path(value == NULL)) {
        return NXT_ERROR;
    }

    value = njs_vm_object_prop(vm, njs_value_arg(&opaque_value), &host_str,
                               &arguments[1]);
    if (nxt_slow_path(value == NULL)) {
        return NXT_ERROR;
    }

    value = njs_vm_object_prop(vm, njs_value_arg(&opaque_value),
                               &remote_addr_str, &arguments[2]);
    if (nxt_slow_path(value == NULL)) {
        return NXT_ERROR;
    }

    value = njs_vm_object_prop(vm, njs_value_arg(&opaque_value), &args_str,
                               &arguments[3]);
    if (nxt_slow_path(value == NULL)) {
        return NXT_ERROR;
    }

    value = njs_vm_object_prop(vm, njs_value_arg(&opaque_value), &headers_str,
                               &arguments[4]);
    if (nxt_slow_path(value == NULL)) {
        return NXT_ERROR;
    }

    value = njs_vm_object_prop(vm, njs_value_arg(&opaque_value), &cookies_str,
                               &arguments[5]);
    if (nxt_slow_path(value == NULL)) {
        return NXT_ERROR;
    }

    ret = njs_vm_call(vm, func, njs_value_arg(&arguments), 6);

    rc = njs_vm_retval_string(vm, &res);
    if (rc != NJS_OK) {
        return NXT_ERROR;
    }

    if (ret != NJS_OK) {
        nxt_alert(task, "js exception: %V", &res);
        return NXT_ERROR;
    }

    str->length = res.length;
    str->start = res.start;

    return NXT_OK;
}


void
nxt_js_release(nxt_js_cache_t *cache)
{
    if (cache->vm != NULL) {
        njs_vm_destroy(cache->vm);
    }
}