summaryrefslogblamecommitdiffhomepage
path: root/src/java/nxt_jni_Request.c
blob: 6fb9cb443f0e80bb2119d14815f40df5914eb709 (plain) (tree)
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
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658

















































































































































































































































































































































































































































































































































































































































































                                                                                                 

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

#include <nxt_auto_config.h>

#include <nxt_unit.h>
#include <nxt_unit_request.h>
#include <jni.h>
#include <stdio.h>
#include <stdlib.h>

#include "nxt_jni.h"
#include "nxt_jni_Request.h"
#include "nxt_jni_URLClassLoader.h"
#include "nxt_jni_HeadersEnumeration.h"
#include "nxt_jni_HeaderNamesEnumeration.h"


static jstring JNICALL nxt_java_Request_getHeader(JNIEnv *env, jclass cls,
    jlong req_ptr, jstring name, jint name_len);
static jobject JNICALL nxt_java_Request_getHeaderNames(JNIEnv *env, jclass cls,
    jlong req_ptr);
static jobject JNICALL nxt_java_Request_getHeaders(JNIEnv *env, jclass cls,
    jlong req_ptr, jstring name, jint name_len);
static jint JNICALL nxt_java_Request_getIntHeader(JNIEnv *env, jclass cls,
    jlong req_ptr, jstring name, jint name_len);
static jstring JNICALL nxt_java_Request_getMethod(JNIEnv *env, jclass cls,
    jlong req_ptr);
static jstring JNICALL nxt_java_Request_getQueryString(JNIEnv *env, jclass cls,
    jlong req_ptr);
static jstring JNICALL nxt_java_Request_getRequestURI(JNIEnv *env, jclass cls,
    jlong req_ptr);
static jlong JNICALL nxt_java_Request_getContentLength(JNIEnv *env, jclass cls,
    jlong req_ptr);
static jstring JNICALL nxt_java_Request_getContentType(JNIEnv *env, jclass cls,
    jlong req_ptr);
static jstring JNICALL nxt_java_Request_getLocalAddr(JNIEnv *env, jclass cls,
    jlong req_ptr);
static jstring JNICALL nxt_java_Request_getLocalName(JNIEnv *env, jclass cls,
    jlong req_ptr);
static jint JNICALL nxt_java_Request_getLocalPort(JNIEnv *env, jclass cls,
    jlong req_ptr);
static jstring JNICALL nxt_java_Request_getProtocol(JNIEnv *env, jclass cls,
    jlong req_ptr);
static jstring JNICALL nxt_java_Request_getRemoteAddr(JNIEnv *env, jclass cls,
    jlong req_ptr);
static jstring JNICALL nxt_java_Request_getRemoteHost(JNIEnv *env, jclass cls,
    jlong req_ptr);
static jint JNICALL nxt_java_Request_getRemotePort(JNIEnv *env, jclass cls,
    jlong req_ptr);
static jstring JNICALL nxt_java_Request_getScheme(JNIEnv *env, jclass cls,
    jlong req_ptr);
static jstring JNICALL nxt_java_Request_getServerName(JNIEnv *env, jclass cls,
    jlong req_ptr);
static jint JNICALL nxt_java_Request_getServerPort(JNIEnv *env, jclass cls,
    jlong req_ptr);
static void JNICALL nxt_java_Request_log(JNIEnv *env, jclass cls,
    jlong req_info_ptr, jstring msg, jint msg_len);
static void JNICALL nxt_java_Request_trace(JNIEnv *env, jclass cls,
    jlong req_info_ptr, jstring msg, jint msg_len);
static jobject JNICALL nxt_java_Request_getResponse(JNIEnv *env, jclass cls,
    jlong req_info_ptr);


static jclass     nxt_java_Request_class;
static jmethodID  nxt_java_Request_ctor;


int
nxt_java_initRequest(JNIEnv *env, jobject cl)
{
    int     res;
    jclass  cls;

    cls = nxt_java_loadClass(env, cl, "nginx.unit.Request");
    if (cls == NULL) {
        return NXT_UNIT_ERROR;
    }

    nxt_java_Request_class = (*env)->NewGlobalRef(env, cls);
    (*env)->DeleteLocalRef(env, cls);
    cls = nxt_java_Request_class;

    nxt_java_Request_ctor = (*env)->GetMethodID(env, cls, "<init>", "(Lnginx/unit/Context;JJ)V");
    if (nxt_java_Request_ctor == NULL) {
        (*env)->DeleteGlobalRef(env, cls);
        return NXT_UNIT_ERROR;
    }

    JNINativeMethod request_methods[] = {
        { (char *) "getHeader",
          (char *) "(JLjava/lang/String;I)Ljava/lang/String;",
          nxt_java_Request_getHeader },

        { (char *) "getHeaderNames",
          (char *) "(J)Ljava/util/Enumeration;",
          nxt_java_Request_getHeaderNames },

        { (char *) "getHeaders",
          (char *) "(JLjava/lang/String;I)Ljava/util/Enumeration;",
          nxt_java_Request_getHeaders },

        { (char *) "getIntHeader",
          (char *) "(JLjava/lang/String;I)I",
          nxt_java_Request_getIntHeader },

        { (char *) "getMethod",
          (char *) "(J)Ljava/lang/String;",
          nxt_java_Request_getMethod },

        { (char *) "getQueryString",
          (char *) "(J)Ljava/lang/String;",
          nxt_java_Request_getQueryString },

        { (char *) "getRequestURI",
          (char *) "(J)Ljava/lang/String;",
          nxt_java_Request_getRequestURI },

        { (char *) "getContentLength",
          (char *) "(J)J",
          nxt_java_Request_getContentLength },

        { (char *) "getContentType",
          (char *) "(J)Ljava/lang/String;",
          nxt_java_Request_getContentType },

        { (char *) "getLocalAddr",
          (char *) "(J)Ljava/lang/String;",
          nxt_java_Request_getLocalAddr },

        { (char *) "getLocalName",
          (char *) "(J)Ljava/lang/String;",
          nxt_java_Request_getLocalName },

        { (char *) "getLocalPort",
          (char *) "(J)I",
          nxt_java_Request_getLocalPort },

        { (char *) "getProtocol",
          (char *) "(J)Ljava/lang/String;",
          nxt_java_Request_getProtocol },

        { (char *) "getRemoteAddr",
          (char *) "(J)Ljava/lang/String;",
          nxt_java_Request_getRemoteAddr },

        { (char *) "getRemoteHost",
          (char *) "(J)Ljava/lang/String;",
          nxt_java_Request_getRemoteHost },

        { (char *) "getRemotePort",
          (char *) "(J)I",
          nxt_java_Request_getRemotePort },

        { (char *) "getScheme",
          (char *) "(J)Ljava/lang/String;",
          nxt_java_Request_getScheme },

        { (char *) "getServerName",
          (char *) "(J)Ljava/lang/String;",
          nxt_java_Request_getServerName },

        { (char *) "getServerPort",
          (char *) "(J)I",
          nxt_java_Request_getServerPort },

        { (char *) "log",
          (char *) "(JLjava/lang/String;I)V",
          nxt_java_Request_log },

        { (char *) "trace",
          (char *) "(JLjava/lang/String;I)V",
          nxt_java_Request_trace },

        { (char *) "getResponse",
          (char *) "(J)Lnginx/unit/Response;",
          nxt_java_Request_getResponse },

    };

    res = (*env)->RegisterNatives(env, nxt_java_Request_class,
                                  request_methods,
                                  sizeof(request_methods) / sizeof(request_methods[0]));

    nxt_unit_debug(NULL, "registered Request methods: %d", res);

    if (res != 0) {
        nxt_unit_warn(NULL, "registering natives for Request failed");
        goto failed;
    }

    res = nxt_java_initHeadersEnumeration(env, cl);
    if (res != NXT_UNIT_OK) {
        goto failed;
    }

    res = nxt_java_initHeaderNamesEnumeration(env, cl);
    if (res != NXT_UNIT_OK) {
        goto failed;
    }

    return NXT_UNIT_OK;

failed:

    (*env)->DeleteGlobalRef(env, cls);
    return NXT_UNIT_ERROR;
}


jobject
nxt_java_newRequest(JNIEnv *env, jobject ctx, nxt_unit_request_info_t *req)
{
    return (*env)->NewObject(env, nxt_java_Request_class,
        nxt_java_Request_ctor, ctx, nxt_ptr2jlong(req),
        nxt_ptr2jlong(req->request));
}


static jstring JNICALL
nxt_java_Request_getHeader(JNIEnv *env, jclass cls, jlong req_ptr,
    jstring name, jint name_len)
{
    const char          *name_str;
    nxt_unit_field_t    *f;
    nxt_unit_request_t  *r;

    name_str = (*env)->GetStringUTFChars(env, name, NULL);
    if (name_str == NULL) {
        return NULL;
    }

    r = nxt_jlong2ptr(req_ptr);

    f = nxt_java_findHeader(r->fields, r->fields + r->fields_count,
                            name_str, name_len);

    (*env)->ReleaseStringUTFChars(env, name, name_str);

    if (f == NULL) {
        return NULL;
    }

    return (*env)->NewStringUTF(env, nxt_unit_sptr_get(&f->value));
}


static jobject JNICALL
nxt_java_Request_getHeaderNames(JNIEnv *env, jclass cls, jlong req_ptr)
{
    nxt_unit_request_t  *r;

    r = nxt_jlong2ptr(req_ptr);

    return nxt_java_newHeaderNamesEnumeration(env, r->fields, r->fields_count);
}


static jobject JNICALL
nxt_java_Request_getHeaders(JNIEnv *env, jclass cls, jlong req_ptr,
    jstring name, jint name_len)
{
    const char          *name_str;
    nxt_unit_field_t    *f;
    nxt_unit_request_t  *r;

    name_str = (*env)->GetStringUTFChars(env, name, NULL);
    if (name_str == NULL) {
        return NULL;
    }

    r = nxt_jlong2ptr(req_ptr);

    f = nxt_java_findHeader(r->fields, r->fields + r->fields_count,
                            name_str, name_len);

    (*env)->ReleaseStringUTFChars(env, name, name_str);

    if (f == NULL) {
        f = r->fields + r->fields_count;
    }

    return nxt_java_newHeadersEnumeration(env, r->fields, r->fields_count,
                                          f - r->fields);
}


static jint JNICALL
nxt_java_Request_getIntHeader(JNIEnv *env, jclass cls, jlong req_ptr,
    jstring name, jint name_len)
{
    jint                res;
    char                *value, *end;
    const char          *name_str;
    nxt_unit_field_t    *f;
    nxt_unit_request_t  *r;

    res = -1;

    name_str = (*env)->GetStringUTFChars(env, name, NULL);
    if (name_str == NULL) {
        return res;
    }

    r = nxt_jlong2ptr(req_ptr);

    f = nxt_java_findHeader(r->fields, r->fields + r->fields_count,
                            name_str, name_len);

    (*env)->ReleaseStringUTFChars(env, name, name_str);

    if (f == NULL) {
        return res;
    }

    value = nxt_unit_sptr_get(&f->value);
    end = value + f->value_length;

    res = strtol(value, &end, 10);

    if (end < value + f->value_length) {
        // TODO throw NumberFormatException.forInputString(value)
    }

    return res;
}


static jstring JNICALL
nxt_java_Request_getMethod(JNIEnv *env, jclass cls, jlong req_ptr)
{
    nxt_unit_request_t  *r;

    r = nxt_jlong2ptr(req_ptr);

    return (*env)->NewStringUTF(env, nxt_unit_sptr_get(&r->method));
}


static jstring JNICALL
nxt_java_Request_getQueryString(JNIEnv *env, jclass cls, jlong req_ptr)
{
    char                *query;
    nxt_unit_request_t  *r;

    r = nxt_jlong2ptr(req_ptr);

    if (r->query.offset != 0) {
        query = nxt_unit_sptr_get(&r->query);
        return (*env)->NewStringUTF(env, query);
    }

    return NULL;
}


static jstring JNICALL
nxt_java_Request_getRequestURI(JNIEnv *env, jclass cls, jlong req_ptr)
{
    char                *target, *query;
    nxt_unit_request_t  *r;

    r = nxt_jlong2ptr(req_ptr);

    target = nxt_unit_sptr_get(&r->target);

    if (r->query.offset != 0) {
        query = nxt_unit_sptr_get(&r->query);
        return nxt_java_newString(env, target, query - target - 1);
    }

    return (*env)->NewStringUTF(env, target);
}


static jlong JNICALL
nxt_java_Request_getContentLength(JNIEnv *env, jclass cls, jlong req_ptr)
{
    nxt_unit_request_t  *r;

    r = nxt_jlong2ptr(req_ptr);

    return r->content_length;
}


static jstring JNICALL
nxt_java_Request_getContentType(JNIEnv *env, jclass cls, jlong req_ptr)
{
    nxt_unit_field_t    *f;
    nxt_unit_request_t  *r;

    r = nxt_jlong2ptr(req_ptr);

    if (r->content_type_field != NXT_UNIT_NONE_FIELD) {
        f = r->fields + r->content_type_field;

        return (*env)->NewStringUTF(env, nxt_unit_sptr_get(&f->value));
    }

    return NULL;
}


static jstring JNICALL
nxt_java_Request_getLocalAddr(JNIEnv *env, jclass cls, jlong req_ptr)
{
    nxt_unit_request_t  *r;

    r = nxt_jlong2ptr(req_ptr);

    return nxt_java_newString(env, nxt_unit_sptr_get(&r->local),
                              r->local_length);
}


static jstring JNICALL
nxt_java_Request_getLocalName(JNIEnv *env, jclass cls, jlong req_ptr)
{
    char                *local, *colon;
    nxt_unit_request_t  *r;

    r = nxt_jlong2ptr(req_ptr);

    local = nxt_unit_sptr_get(&r->local);
    colon = memchr(local, ':', r->local_length);

    if (colon == NULL) {
        colon = local + r->local_length;
    }

    return nxt_java_newString(env, local, colon - local);
}


static jint JNICALL
nxt_java_Request_getLocalPort(JNIEnv *env, jclass cls, jlong req_ptr)
{
    jint                res;
    char                *local, *colon, tmp;
    nxt_unit_request_t  *r;

    r = nxt_jlong2ptr(req_ptr);

    local = nxt_unit_sptr_get(&r->local);
    colon = memchr(local, ':', r->local_length);

    if (colon == NULL) {
        return 80;
    }

    tmp = local[r->local_length];

    local[r->local_length] = '\0';

    res = strtol(colon + 1, NULL, 10);

    local[r->local_length] = tmp;

    return res;
}


static jstring JNICALL
nxt_java_Request_getProtocol(JNIEnv *env, jclass cls, jlong req_ptr)
{
    nxt_unit_request_t  *r;

    r = nxt_jlong2ptr(req_ptr);

    return (*env)->NewStringUTF(env, nxt_unit_sptr_get(&r->version));
}


static jstring JNICALL
nxt_java_Request_getRemoteAddr(JNIEnv *env, jclass cls, jlong req_ptr)
{
    nxt_unit_request_t  *r;

    r = nxt_jlong2ptr(req_ptr);

    return nxt_java_newString(env, nxt_unit_sptr_get(&r->remote),
                              r->remote_length);
}


static jstring JNICALL
nxt_java_Request_getRemoteHost(JNIEnv *env, jclass cls, jlong req_ptr)
{
    char                *remote, *colon;
    nxt_unit_request_t  *r;

    r = nxt_jlong2ptr(req_ptr);

    remote = nxt_unit_sptr_get(&r->remote);
    colon = memchr(remote, ':', r->remote_length);

    if (colon == NULL) {
        colon = remote + r->remote_length;
    }

    return nxt_java_newString(env, remote, colon - remote);
}


static jint JNICALL
nxt_java_Request_getRemotePort(JNIEnv *env, jclass cls, jlong req_ptr)
{
    jint                res;
    char                *remote, *colon, tmp;
    nxt_unit_request_t  *r;

    r = nxt_jlong2ptr(req_ptr);

    remote = nxt_unit_sptr_get(&r->remote);
    colon = memchr(remote, ':', r->remote_length);

    if (colon == NULL) {
        return 80;
    }

    tmp = remote[r->remote_length];

    remote[r->remote_length] = '\0';

    res = strtol(colon + 1, NULL, 10);

    remote[r->remote_length] = tmp;

    return res;
}


static jstring JNICALL
nxt_java_Request_getScheme(JNIEnv *env, jclass cls, jlong req_ptr)
{
    return (*env)->NewStringUTF(env, "http");
}


static jstring JNICALL
nxt_java_Request_getServerName(JNIEnv *env, jclass cls, jlong req_ptr)
{
    char                *host, *colon;
    nxt_unit_field_t    *f;
    nxt_unit_request_t  *r;

    r = nxt_jlong2ptr(req_ptr);

    f = nxt_java_findHeader(r->fields, r->fields + r->fields_count,
                            "Host", 4);
    if (f != NULL) {
        host = nxt_unit_sptr_get(&f->value);

        colon = memchr(host, ':', f->value_length);

        if (colon == NULL) {
            colon = host + f->value_length;
        }

        return nxt_java_newString(env, host, colon - host);
    }

    return nxt_java_Request_getLocalName(env, cls, req_ptr);
}


static jint JNICALL
nxt_java_Request_getServerPort(JNIEnv *env, jclass cls, jlong req_ptr)
{
    jint                res;
    char                *host, *colon, tmp;
    nxt_unit_field_t    *f;
    nxt_unit_request_t  *r;

    r = nxt_jlong2ptr(req_ptr);

    f = nxt_java_findHeader(r->fields, r->fields + r->fields_count,
                            "Host", 4);
    if (f != NULL) {
        host = nxt_unit_sptr_get(&f->value);

        colon = memchr(host, ':', f->value_length);

        if (colon == NULL) {
            return 80;
        }

        tmp = host[f->value_length];

        host[f->value_length] = '\0';

        res = strtol(colon + 1, NULL, 10);

        host[f->value_length] = tmp;

        return res;
    }

    return nxt_java_Request_getLocalPort(env, cls, req_ptr);
}


static void JNICALL
nxt_java_Request_log(JNIEnv *env, jclass cls, jlong req_info_ptr, jstring msg,
    jint msg_len)
{
    const char               *msg_str;
    nxt_unit_request_info_t  *req;

    req = nxt_jlong2ptr(req_info_ptr);

    msg_str = (*env)->GetStringUTFChars(env, msg, NULL);
    if (msg_str == NULL) {
        return;
    }

    nxt_unit_req_log(req, NXT_UNIT_LOG_INFO, "%.*s", msg_len, msg_str);

    (*env)->ReleaseStringUTFChars(env, msg, msg_str);
}


static void JNICALL
nxt_java_Request_trace(JNIEnv *env, jclass cls, jlong req_info_ptr, jstring msg,
    jint msg_len)
{
#if (NXT_DEBUG)
    const char               *msg_str;
    nxt_unit_request_info_t  *req;

    req = nxt_jlong2ptr(req_info_ptr);

    msg_str = (*env)->GetStringUTFChars(env, msg, NULL);
    if (msg_str == NULL) {
        return;
    }

    nxt_unit_req_debug(req, "%.*s", msg_len, msg_str);

    (*env)->ReleaseStringUTFChars(env, msg, msg_str);
#endif
}


static jobject JNICALL
nxt_java_Request_getResponse(JNIEnv *env, jclass cls, jlong req_info_ptr)
{
    nxt_unit_request_info_t  *req;
    nxt_java_request_data_t  *data;

    req = nxt_jlong2ptr(req_info_ptr);
    data = req->data;

    return data->jresp;
}