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
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
|
/*
* Copyright (C) NGINX, Inc.
*/
#include <nxt_auto_config.h>
#include <nxt_unit.h>
#include <nxt_unit_response.h>
#include <jni.h>
#include <stdio.h>
#include "nxt_jni.h"
#include "nxt_jni_Response.h"
#include "nxt_jni_HeadersEnumeration.h"
#include "nxt_jni_HeaderNamesEnumeration.h"
#include "nxt_jni_OutputStream.h"
#include "nxt_jni_URLClassLoader.h"
static jclass nxt_java_Response_class;
static jmethodID nxt_java_Response_ctor;
static void JNICALL nxt_java_Response_addHeader(JNIEnv *env, jclass cls,
jlong req_info_ptr, jarray name, jarray value);
static nxt_unit_request_info_t *nxt_java_get_response_info(
jlong req_info_ptr, uint32_t extra_fields, uint32_t extra_data);
static void JNICALL nxt_java_Response_addIntHeader(JNIEnv *env, jclass cls,
jlong req_info_ptr, jarray name, jint value);
static void nxt_java_add_int_header(nxt_unit_request_info_t *req,
const char *name, uint8_t name_len, int value);
static jboolean JNICALL nxt_java_Response_containsHeader(JNIEnv *env,
jclass cls, jlong req_info_ptr, jarray name);
static jstring JNICALL nxt_java_Response_getHeader(JNIEnv *env, jclass cls,
jlong req_info_ptr, jarray name);
static jobject JNICALL nxt_java_Response_getHeaderNames(JNIEnv *env,
jclass cls, jlong req_info_ptr);
static jobject JNICALL nxt_java_Response_getHeaders(JNIEnv *env, jclass cls,
jlong req_info_ptr, jarray name);
static jint JNICALL nxt_java_Response_getStatus(JNIEnv *env, jclass cls,
jlong req_info_ptr);
static jobject JNICALL nxt_java_Response_getRequest(JNIEnv *env, jclass cls,
jlong req_info_ptr);
static void JNICALL nxt_java_Response_commit(JNIEnv *env, jclass cls,
jlong req_info_ptr);
static void JNICALL nxt_java_Response_sendRedirect(JNIEnv *env, jclass cls,
jlong req_info_ptr, jarray loc);
static int nxt_java_response_set_header(jlong req_info_ptr,
const char *name, jint name_len, const char *value, jint value_len);
static void JNICALL nxt_java_Response_setHeader(JNIEnv *env, jclass cls,
jlong req_info_ptr, jarray name, jarray value);
static void JNICALL nxt_java_Response_removeHeader(JNIEnv *env, jclass cls,
jlong req_info_ptr, jarray name);
static int nxt_java_response_remove_header(jlong req_info_ptr,
const char *name, jint name_len);
static void JNICALL nxt_java_Response_setIntHeader(JNIEnv *env, jclass cls,
jlong req_info_ptr, jarray name, jint value);
static void JNICALL nxt_java_Response_setStatus(JNIEnv *env, jclass cls,
jlong req_info_ptr, jint sc);
static jstring JNICALL nxt_java_Response_getContentType(JNIEnv *env,
jclass cls, jlong req_info_ptr);
static jboolean JNICALL nxt_java_Response_isCommitted(JNIEnv *env, jclass cls,
jlong req_info_ptr);
static void JNICALL nxt_java_Response_reset(JNIEnv *env, jclass cls,
jlong req_info_ptr);
static void JNICALL nxt_java_Response_resetBuffer(JNIEnv *env, jclass cls,
jlong req_info_ptr);
static void JNICALL nxt_java_Response_setBufferSize(JNIEnv *env, jclass cls,
jlong req_info_ptr, jint size);
static jint JNICALL nxt_java_Response_getBufferSize(JNIEnv *env, jclass cls,
jlong req_info_ptr);
static void JNICALL nxt_java_Response_setContentLength(JNIEnv *env, jclass cls,
jlong req_info_ptr, jlong len);
static void JNICALL nxt_java_Response_setContentType(JNIEnv *env, jclass cls,
jlong req_info_ptr, jarray type);
static void JNICALL nxt_java_Response_removeContentType(JNIEnv *env, jclass cls,
jlong req_info_ptr);
static void JNICALL nxt_java_Response_log(JNIEnv *env, jclass cls,
jlong req_info_ptr, jarray msg);
static void JNICALL nxt_java_Response_trace(JNIEnv *env, jclass cls,
jlong req_info_ptr, jarray msg);
int
nxt_java_initResponse(JNIEnv *env, jobject cl)
{
int res;
jclass cls;
cls = nxt_java_loadClass(env, cl, "nginx.unit.Response");
if (cls == NULL) {
return NXT_UNIT_ERROR;
}
nxt_java_Response_class = (*env)->NewGlobalRef(env, cls);
(*env)->DeleteLocalRef(env, cls);
cls = nxt_java_Response_class;
nxt_java_Response_ctor = (*env)->GetMethodID(env, cls, "<init>", "(J)V");
if (nxt_java_Response_ctor == NULL) {
(*env)->DeleteGlobalRef(env, cls);
return NXT_UNIT_ERROR;
}
JNINativeMethod resp_methods[] = {
{ (char *) "addHeader",
(char *) "(J[B[B)V",
nxt_java_Response_addHeader },
{ (char *) "addIntHeader",
(char *) "(J[BI)V",
nxt_java_Response_addIntHeader },
{ (char *) "containsHeader",
(char *) "(J[B)Z",
nxt_java_Response_containsHeader },
{ (char *) "getHeader",
(char *) "(J[B)Ljava/lang/String;",
nxt_java_Response_getHeader },
{ (char *) "getHeaderNames",
(char *) "(J)Ljava/util/Enumeration;",
nxt_java_Response_getHeaderNames },
{ (char *) "getHeaders",
(char *) "(J[B)Ljava/util/Enumeration;",
nxt_java_Response_getHeaders },
{ (char *) "getStatus",
(char *) "(J)I",
nxt_java_Response_getStatus },
{ (char *) "getRequest",
(char *) "(J)Lnginx/unit/Request;",
nxt_java_Response_getRequest },
{ (char *) "commit",
(char *) "(J)V",
nxt_java_Response_commit },
{ (char *) "sendRedirect",
(char *) "(J[B)V",
nxt_java_Response_sendRedirect },
{ (char *) "setHeader",
(char *) "(J[B[B)V",
nxt_java_Response_setHeader },
{ (char *) "removeHeader",
(char *) "(J[B)V",
nxt_java_Response_removeHeader },
{ (char *) "setIntHeader",
(char *) "(J[BI)V",
nxt_java_Response_setIntHeader },
{ (char *) "setStatus",
(char *) "(JI)V",
nxt_java_Response_setStatus },
{ (char *) "getContentType",
(char *) "(J)Ljava/lang/String;",
nxt_java_Response_getContentType },
{ (char *) "isCommitted",
(char *) "(J)Z",
nxt_java_Response_isCommitted },
{ (char *) "reset",
(char *) "(J)V",
nxt_java_Response_reset },
{ (char *) "resetBuffer",
(char *) "(J)V",
nxt_java_Response_resetBuffer },
{ (char *) "setBufferSize",
(char *) "(JI)V",
nxt_java_Response_setBufferSize },
{ (char *) "getBufferSize",
(char *) "(J)I",
nxt_java_Response_getBufferSize },
{ (char *) "setContentLength",
(char *) "(JJ)V",
nxt_java_Response_setContentLength },
{ (char *) "setContentType",
(char *) "(J[B)V",
nxt_java_Response_setContentType },
{ (char *) "removeContentType",
(char *) "(J)V",
nxt_java_Response_removeContentType },
{ (char *) "log",
(char *) "(J[B)V",
nxt_java_Response_log },
{ (char *) "trace",
(char *) "(J[B)V",
nxt_java_Response_trace },
};
res = (*env)->RegisterNatives(env, nxt_java_Response_class,
resp_methods,
sizeof(resp_methods)
/ sizeof(resp_methods[0]));
nxt_unit_debug(NULL, "registered Response methods: %d", res);
if (res != 0) {
(*env)->DeleteGlobalRef(env, cls);
return NXT_UNIT_ERROR;
}
return NXT_UNIT_OK;
}
jobject
nxt_java_newResponse(JNIEnv *env, nxt_unit_request_info_t *req)
{
return (*env)->NewObject(env, nxt_java_Response_class,
nxt_java_Response_ctor, nxt_ptr2jlong(req));
}
static void JNICALL
nxt_java_Response_addHeader(JNIEnv *env, jclass cls, jlong req_info_ptr,
jarray name, jarray value)
{
int rc;
char *name_str, *value_str;
jsize name_len, value_len;
nxt_unit_request_info_t *req;
name_len = (*env)->GetArrayLength(env, name);
value_len = (*env)->GetArrayLength(env, value);
req = nxt_java_get_response_info(req_info_ptr, 1, name_len + value_len + 2);
if (req == NULL) {
return;
}
name_str = (*env)->GetPrimitiveArrayCritical(env, name, NULL);
if (name_str == NULL) {
nxt_unit_req_warn(req, "addHeader: failed to get name content");
return;
}
value_str = (*env)->GetPrimitiveArrayCritical(env, value, NULL);
if (value_str == NULL) {
(*env)->ReleasePrimitiveArrayCritical(env, name, name_str, 0);
nxt_unit_req_warn(req, "addHeader: failed to get value content");
return;
}
rc = nxt_unit_response_add_field(req, name_str, name_len,
value_str, value_len);
if (rc != NXT_UNIT_OK) {
// throw
}
(*env)->ReleasePrimitiveArrayCritical(env, value, value_str, 0);
(*env)->ReleasePrimitiveArrayCritical(env, name, name_str, 0);
}
static nxt_unit_request_info_t *
nxt_java_get_response_info(jlong req_info_ptr, uint32_t extra_fields,
uint32_t extra_data)
{
int rc;
char *p;
uint32_t max_size;
nxt_unit_buf_t *buf;
nxt_unit_request_info_t *req;
nxt_java_request_data_t *data;
req = nxt_jlong2ptr(req_info_ptr);
if (nxt_unit_response_is_sent(req)) {
return NULL;
}
data = req->data;
if (!nxt_unit_response_is_init(req)) {
max_size = nxt_unit_buf_max();
max_size = max_size < data->header_size ? max_size : data->header_size;
rc = nxt_unit_response_init(req, 200, 16, max_size);
if (rc != NXT_UNIT_OK) {
return NULL;
}
}
buf = req->response_buf;
if (extra_fields > req->response_max_fields
- req->response->fields_count
|| extra_data > (uint32_t) (buf->end - buf->free))
{
p = buf->start + req->response_max_fields * sizeof(nxt_unit_field_t);
max_size = 2 * (buf->end - p);
if (max_size > nxt_unit_buf_max()) {
nxt_unit_req_warn(req, "required max_size is too big: %"PRIu32,
max_size);
return NULL;
}
rc = nxt_unit_response_realloc(req, 2 * req->response_max_fields,
max_size);
if (rc != NXT_UNIT_OK) {
nxt_unit_req_warn(req, "reallocation failed: %"PRIu32", %"PRIu32,
2 * req->response_max_fields, max_size);
return NULL;
}
}
return req;
}
static void JNICALL
nxt_java_Response_addIntHeader(JNIEnv *env, jclass cls, jlong req_info_ptr,
jarray name, jint value)
{
char *name_str;
jsize name_len;
nxt_unit_request_info_t *req;
name_len = (*env)->GetArrayLength(env, name);
req = nxt_java_get_response_info(req_info_ptr, 1, name_len + 40);
if (req == NULL) {
return;
}
name_str = (*env)->GetPrimitiveArrayCritical(env, name, NULL);
if (name_str == NULL) {
nxt_unit_req_warn(req, "addIntHeader: failed to get name content");
return;
}
nxt_java_add_int_header(req, name_str, name_len, value);
(*env)->ReleasePrimitiveArrayCritical(env, name, name_str, 0);
}
static void
nxt_java_add_int_header(nxt_unit_request_info_t *req, const char *name,
uint8_t name_len, int value)
{
char *p;
nxt_unit_field_t *f;
nxt_unit_response_t *resp;
resp = req->response;
f = resp->fields + resp->fields_count;
p = req->response_buf->free;
f->hash = nxt_unit_field_hash(name, name_len);
f->skip = 0;
f->name_length = name_len;
nxt_unit_sptr_set(&f->name, p);
memcpy(p, name, name_len);
p += name_len;
nxt_unit_sptr_set(&f->value, p);
f->value_length = snprintf(p, 40, "%d", (int) value);
p += f->value_length + 1;
resp->fields_count++;
req->response_buf->free = p;
}
static jboolean JNICALL
nxt_java_Response_containsHeader(JNIEnv *env,
jclass cls, jlong req_info_ptr, jarray name)
{
jboolean res;
char *name_str;
jsize name_len;
nxt_unit_response_t *resp;
nxt_unit_request_info_t *req;
req = nxt_jlong2ptr(req_info_ptr);
if (!nxt_unit_response_is_init(req)) {
nxt_unit_req_debug(req, "containsHeader: response is not initialized");
return 0;
}
if (nxt_unit_response_is_sent(req)) {
nxt_unit_req_debug(req, "containsHeader: response already sent");
return 0;
}
name_len = (*env)->GetArrayLength(env, name);
name_str = (*env)->GetPrimitiveArrayCritical(env, name, NULL);
if (name_str == NULL) {
nxt_unit_req_warn(req, "containsHeader: failed to get name content");
return 0;
}
resp = req->response;
res = nxt_java_findHeader(resp->fields,
resp->fields + resp->fields_count,
name_str, name_len) != NULL;
(*env)->ReleasePrimitiveArrayCritical(env, name, name_str, 0);
return res;
}
static jstring JNICALL
nxt_java_Response_getHeader(JNIEnv *env, jclass cls, jlong req_info_ptr,
jarray name)
{
char *name_str;
jsize name_len;
nxt_unit_field_t *f;
nxt_unit_request_info_t *req;
req = nxt_jlong2ptr(req_info_ptr);
if (!nxt_unit_response_is_init(req)) {
nxt_unit_req_debug(req, "getHeader: response is not initialized");
return NULL;
}
if (nxt_unit_response_is_sent(req)) {
nxt_unit_req_debug(req, "getHeader: response already sent");
return NULL;
}
name_len = (*env)->GetArrayLength(env, name);
name_str = (*env)->GetPrimitiveArrayCritical(env, name, NULL);
if (name_str == NULL) {
nxt_unit_req_warn(req, "getHeader: failed to get name content");
return NULL;
}
f = nxt_java_findHeader(req->response->fields,
req->response->fields + req->response->fields_count,
name_str, name_len);
(*env)->ReleasePrimitiveArrayCritical(env, name, name_str, 0);
if (f == NULL) {
return NULL;
}
return nxt_java_newString(env, nxt_unit_sptr_get(&f->value),
f->value_length);
}
static jobject JNICALL
nxt_java_Response_getHeaderNames(JNIEnv *env, jclass cls, jlong req_info_ptr)
{
nxt_unit_request_info_t *req;
req = nxt_jlong2ptr(req_info_ptr);
if (!nxt_unit_response_is_init(req)) {
nxt_unit_req_debug(req, "getHeaderNames: response is not initialized");
return NULL;
}
if (nxt_unit_response_is_sent(req)) {
nxt_unit_req_debug(req, "getHeaderNames: response already sent");
return NULL;
}
return nxt_java_newHeaderNamesEnumeration(env, req->response->fields,
req->response->fields_count);
}
static jobject JNICALL
nxt_java_Response_getHeaders(JNIEnv *env, jclass cls,
jlong req_info_ptr, jarray name)
{
char *name_str;
jsize name_len;
nxt_unit_field_t *f;
nxt_unit_response_t *resp;
nxt_unit_request_info_t *req;
req = nxt_jlong2ptr(req_info_ptr);
if (!nxt_unit_response_is_init(req)) {
nxt_unit_req_debug(req, "getHeaders: response is not initialized");
return NULL;
}
if (nxt_unit_response_is_sent(req)) {
nxt_unit_req_debug(req, "getHeaders: response already sent");
return NULL;
}
resp = req->response;
name_len = (*env)->GetArrayLength(env, name);
name_str = (*env)->GetPrimitiveArrayCritical(env, name, NULL);
if (name_str == NULL) {
nxt_unit_req_warn(req, "getHeaders: failed to get name content");
return NULL;
}
f = nxt_java_findHeader(resp->fields, resp->fields + resp->fields_count,
name_str, name_len);
(*env)->ReleasePrimitiveArrayCritical(env, name, name_str, 0);
if (f == NULL) {
f = resp->fields + resp->fields_count;
}
return nxt_java_newHeadersEnumeration(env, resp->fields, resp->fields_count,
f - resp->fields);
}
static jint JNICALL
nxt_java_Response_getStatus(JNIEnv *env, jclass cls, jlong req_info_ptr)
{
nxt_unit_request_info_t *req;
req = nxt_jlong2ptr(req_info_ptr);
if (!nxt_unit_response_is_init(req)) {
nxt_unit_req_debug(req, "getStatus: response is not initialized");
return 200;
}
if (nxt_unit_response_is_sent(req)) {
nxt_unit_req_debug(req, "getStatus: response already sent");
return 200;
}
return req->response->status;
}
static jobject JNICALL
nxt_java_Response_getRequest(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->jreq;
}
static void JNICALL
nxt_java_Response_commit(JNIEnv *env, jclass cls, jlong req_info_ptr)
{
nxt_unit_request_info_t *req;
req = nxt_jlong2ptr(req_info_ptr);
nxt_java_OutputStream_flush_buf(env, req);
}
static void JNICALL
nxt_java_Response_sendRedirect(JNIEnv *env, jclass cls,
jlong req_info_ptr, jarray loc)
{
int rc;
char *loc_str;
jsize loc_len;
nxt_unit_request_info_t *req;
static const char location[] = "Location";
static const uint32_t location_len = sizeof(location) - 1;
req = nxt_jlong2ptr(req_info_ptr);
if (nxt_unit_response_is_sent(req)) {
nxt_java_throw_IllegalStateException(env, "Response already sent");
return;
}
loc_len = (*env)->GetArrayLength(env, loc);
req = nxt_java_get_response_info(req_info_ptr, 1,
location_len + loc_len + 2);
if (req == NULL) {
return;
}
loc_str = (*env)->GetPrimitiveArrayCritical(env, loc, NULL);
if (loc_str == NULL) {
nxt_unit_req_warn(req, "sendRedirect: failed to get loc content");
return;
}
req->response->status = 302;
rc = nxt_java_response_set_header(req_info_ptr, location, location_len,
loc_str, loc_len);
if (rc != NXT_UNIT_OK) {
// throw
}
(*env)->ReleasePrimitiveArrayCritical(env, loc, loc_str, 0);
nxt_unit_response_send(req);
}
static int
nxt_java_response_set_header(jlong req_info_ptr,
const char *name, jint name_len, const char *value, jint value_len)
{
int add_field;
char *dst;
nxt_unit_field_t *f, *e;
nxt_unit_response_t *resp;
nxt_unit_request_info_t *req;
req = nxt_java_get_response_info(req_info_ptr, 0, 0);
if (req == NULL) {
return NXT_UNIT_ERROR;
}
resp = req->response;
f = resp->fields;
e = f + resp->fields_count;
add_field = 1;
for ( ;; ) {
f = nxt_java_findHeader(f, e, name, name_len);
if (f == NULL) {
break;
}
if (add_field && f->value_length >= (uint32_t) value_len) {
dst = nxt_unit_sptr_get(&f->value);
memcpy(dst, value, value_len);
dst[value_len] = '\0';
f->value_length = value_len;
add_field = 0;
f->skip = 0;
} else {
f->skip = 1;
}
++f;
}
if (!add_field) {
return NXT_UNIT_OK;
}
req = nxt_java_get_response_info(req_info_ptr, 1, name_len + value_len + 2);
if (req == NULL) {
return NXT_UNIT_ERROR;
}
return nxt_unit_response_add_field(req, name, name_len, value, value_len);
}
static void JNICALL
nxt_java_Response_setHeader(JNIEnv *env, jclass cls,
jlong req_info_ptr, jarray name, jarray value)
{
int rc;
char *name_str, *value_str;
jsize name_len, value_len;
nxt_unit_request_info_t *req;
name_str = (*env)->GetPrimitiveArrayCritical(env, name, NULL);
if (name_str == NULL) {
req = nxt_jlong2ptr(req_info_ptr);
nxt_unit_req_warn(req, "setHeader: failed to get name content");
return;
}
value_str = (*env)->GetPrimitiveArrayCritical(env, value, NULL);
if (value_str == NULL) {
(*env)->ReleasePrimitiveArrayCritical(env, name, name_str, 0);
req = nxt_jlong2ptr(req_info_ptr);
nxt_unit_req_warn(req, "setHeader: failed to get value content");
return;
}
name_len = (*env)->GetArrayLength(env, name);
value_len = (*env)->GetArrayLength(env, value);
rc = nxt_java_response_set_header(req_info_ptr, name_str, name_len,
value_str, value_len);
if (rc != NXT_UNIT_OK) {
// throw
}
(*env)->ReleasePrimitiveArrayCritical(env, value, value_str, 0);
(*env)->ReleasePrimitiveArrayCritical(env, name, name_str, 0);
}
static void JNICALL
nxt_java_Response_removeHeader(JNIEnv *env, jclass cls,
jlong req_info_ptr, jarray name)
{
int rc;
char *name_str;
jsize name_len;
nxt_unit_request_info_t *req;
name_len = (*env)->GetArrayLength(env, name);
name_str = (*env)->GetPrimitiveArrayCritical(env, name, NULL);
if (name_str == NULL) {
req = nxt_jlong2ptr(req_info_ptr);
nxt_unit_req_warn(req, "setHeader: failed to get name content");
return;
}
rc = nxt_java_response_remove_header(req_info_ptr, name_str, name_len);
if (rc != NXT_UNIT_OK) {
// throw
}
(*env)->ReleasePrimitiveArrayCritical(env, name, name_str, 0);
}
static int
nxt_java_response_remove_header(jlong req_info_ptr,
const char *name, jint name_len)
{
nxt_unit_field_t *f, *e;
nxt_unit_response_t *resp;
nxt_unit_request_info_t *req;
req = nxt_java_get_response_info(req_info_ptr, 0, 0);
if (req == NULL) {
return NXT_UNIT_ERROR;
}
resp = req->response;
f = resp->fields;
e = f + resp->fields_count;
for ( ;; ) {
f = nxt_java_findHeader(f, e, name, name_len);
if (f == NULL) {
break;
}
f->skip = 1;
++f;
}
return NXT_UNIT_OK;
}
static void JNICALL
nxt_java_Response_setIntHeader(JNIEnv *env, jclass cls,
jlong req_info_ptr, jarray name, jint value)
{
int value_len, rc;
char value_str[40];
char *name_str;
jsize name_len;
value_len = snprintf(value_str, sizeof(value_str), "%d", (int) value);
name_len = (*env)->GetArrayLength(env, name);
name_str = (*env)->GetPrimitiveArrayCritical(env, name, NULL);
if (name_str == NULL) {
nxt_unit_req_warn(nxt_jlong2ptr(req_info_ptr),
"setIntHeader: failed to get name content");
return;
}
rc = nxt_java_response_set_header(req_info_ptr, name_str, name_len,
value_str, value_len);
if (rc != NXT_UNIT_OK) {
// throw
}
(*env)->ReleasePrimitiveArrayCritical(env, name, name_str, 0);
}
static void JNICALL
nxt_java_Response_setStatus(JNIEnv *env, jclass cls, jlong req_info_ptr,
jint sc)
{
nxt_unit_request_info_t *req;
req = nxt_java_get_response_info(req_info_ptr, 0, 0);
if (req == NULL) {
return;
}
req->response->status = sc;
}
static jstring JNICALL
nxt_java_Response_getContentType(JNIEnv *env, jclass cls, jlong req_info_ptr)
{
nxt_unit_field_t *f;
nxt_unit_request_info_t *req;
req = nxt_jlong2ptr(req_info_ptr);
if (!nxt_unit_response_is_init(req)) {
nxt_unit_req_debug(req, "getContentType: response is not initialized");
return NULL;
}
if (nxt_unit_response_is_sent(req)) {
nxt_unit_req_debug(req, "getContentType: response already sent");
return NULL;
}
f = nxt_java_findHeader(req->response->fields,
req->response->fields + req->response->fields_count,
"Content-Type", sizeof("Content-Type") - 1);
if (f == NULL) {
return NULL;
}
return nxt_java_newString(env, nxt_unit_sptr_get(&f->value),
f->value_length);
}
static jboolean JNICALL
nxt_java_Response_isCommitted(JNIEnv *env, jclass cls, jlong req_info_ptr)
{
nxt_unit_request_info_t *req;
req = nxt_jlong2ptr(req_info_ptr);
if (nxt_unit_response_is_sent(req)) {
return 1;
}
return 0;
}
static void JNICALL
nxt_java_Response_reset(JNIEnv *env, jclass cls, jlong req_info_ptr)
{
nxt_unit_buf_t *buf;
nxt_unit_request_info_t *req;
nxt_java_request_data_t *data;
req = nxt_jlong2ptr(req_info_ptr);
if (nxt_unit_response_is_sent(req)) {
nxt_java_throw_IllegalStateException(env, "Response already sent");
return;
}
data = req->data;
if (data->buf != NULL && data->buf->free > data->buf->start) {
data->buf->free = data->buf->start;
}
if (nxt_unit_response_is_init(req)) {
req->response->status = 200;
req->response->fields_count = 0;
buf = req->response_buf;
buf->free = buf->start + req->response_max_fields
* sizeof(nxt_unit_field_t);
}
}
static void JNICALL
nxt_java_Response_resetBuffer(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;
if (data->buf != NULL && data->buf->free > data->buf->start) {
data->buf->free = data->buf->start;
}
}
static void JNICALL
nxt_java_Response_setBufferSize(JNIEnv *env, jclass cls, jlong req_info_ptr,
jint size)
{
nxt_unit_request_info_t *req;
nxt_java_request_data_t *data;
req = nxt_jlong2ptr(req_info_ptr);
data = req->data;
if (data->buf_size == (uint32_t) size) {
return;
}
if (data->buf != NULL && data->buf->free > data->buf->start) {
nxt_java_throw_IllegalStateException(env, "Buffer is not empty");
return;
}
data->buf_size = size;
if (data->buf_size > nxt_unit_buf_max()) {
data->buf_size = nxt_unit_buf_max();
}
if (data->buf != NULL
&& (uint32_t) (data->buf->end - data->buf->start) < data->buf_size)
{
nxt_unit_buf_free(data->buf);
data->buf = NULL;
}
}
static jint JNICALL
nxt_java_Response_getBufferSize(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->buf_size;
}
static void JNICALL
nxt_java_Response_setContentLength(JNIEnv *env, jclass cls, jlong req_info_ptr,
jlong len)
{
nxt_unit_request_info_t *req;
req = nxt_java_get_response_info(req_info_ptr, 0, 0);
if (req == NULL) {
return;
}
req->response->content_length = len;
}
static void JNICALL
nxt_java_Response_setContentType(JNIEnv *env, jclass cls, jlong req_info_ptr,
jarray type)
{
int rc;
char *type_str;
jsize type_len;
static const char content_type[] = "Content-Type";
static const uint32_t content_type_len = sizeof(content_type) - 1;
type_len = (*env)->GetArrayLength(env, type);
type_str = (*env)->GetPrimitiveArrayCritical(env, type, NULL);
if (type_str == NULL) {
return;
}
rc = nxt_java_response_set_header(req_info_ptr,
content_type, content_type_len,
type_str, type_len);
if (rc != NXT_UNIT_OK) {
// throw
}
(*env)->ReleasePrimitiveArrayCritical(env, type, type_str, 0);
}
static void JNICALL
nxt_java_Response_removeContentType(JNIEnv *env, jclass cls, jlong req_info_ptr)
{
nxt_java_response_remove_header(req_info_ptr, "Content-Type",
sizeof("Content-Type") - 1);
}
static void JNICALL
nxt_java_Response_log(JNIEnv *env, jclass cls, jlong req_info_ptr, jarray msg)
{
char *msg_str;
jsize msg_len;
nxt_unit_request_info_t *req;
req = nxt_jlong2ptr(req_info_ptr);
msg_len = (*env)->GetArrayLength(env, msg);
msg_str = (*env)->GetPrimitiveArrayCritical(env, msg, NULL);
if (msg_str == NULL) {
nxt_unit_req_warn(req, "log: failed to get msg content");
return;
}
nxt_unit_req_log(req, NXT_UNIT_LOG_INFO, "%.*s", msg_len, msg_str);
(*env)->ReleasePrimitiveArrayCritical(env, msg, msg_str, 0);
}
static void JNICALL
nxt_java_Response_trace(JNIEnv *env, jclass cls, jlong req_info_ptr, jarray msg)
{
#if (NXT_DEBUG)
char *msg_str;
jsize msg_len;
nxt_unit_request_info_t *req;
req = nxt_jlong2ptr(req_info_ptr);
msg_len = (*env)->GetArrayLength(env, msg);
msg_str = (*env)->GetPrimitiveArrayCritical(env, msg, NULL);
if (msg_str == NULL) {
nxt_unit_req_warn(req, "trace: failed to get msg content");
return;
}
nxt_unit_req_debug(req, "%.*s", msg_len, msg_str);
(*env)->ReleasePrimitiveArrayCritical(env, msg, msg_str, 0);
#endif
}
|