-
Notifications
You must be signed in to change notification settings - Fork 2
/
ngx_http_groonga_module.c
1711 lines (1456 loc) · 49.3 KB
/
ngx_http_groonga_module.c
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
/*
Copyright(C) 2012-2017 Brazil
Copyright(C) 2018-2023 Sutou Kouhei <kou@clear-code.com>
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#ifndef WIN32
# define NGX_GRN_SUPPORT_STOP_BY_COMMAND
#endif
#include <ngx_config.h>
#include <ngx_core.h>
#include <ngx_http.h>
#ifndef ngx_lock_fd_n
/* TODO: Support Windows with LockFileEx() and UnlockFileEx(). */
# define ngx_lock_fd(fd)
# define ngx_unlock_fd(fd)
#endif
#include <groonga.h>
#include <groonga/plugin.h>
#include <sys/stat.h>
#ifdef NGX_GRN_SUPPORT_STOP_BY_COMMAND
# include <sys/types.h>
# include <unistd.h>
#endif
#ifdef WIN32
# define GRN_FMT_SIZE "Iu"
#else
# define GRN_FMT_SIZE "zu"
#endif
#define GRN_NO_FLAGS 0
typedef struct {
ngx_flag_t enabled;
ngx_str_t database_path;
char *database_path_cstr;
ngx_flag_t database_auto_create;
ngx_str_t base_path;
ngx_str_t log_path;
ngx_open_file_t *log_file;
grn_log_level log_level;
ngx_str_t query_log_path;
ngx_open_file_t *query_log_file;
size_t cache_limit;
ngx_msec_t default_request_timeout_msec;
char *config_file;
int config_line;
char *name;
grn_obj *database;
grn_cache *cache;
ngx_str_t cache_base_path;
} ngx_http_groonga_loc_conf_t;
typedef struct {
ngx_log_t *log;
ngx_pool_t *pool;
ngx_int_t rc;
} ngx_http_groonga_database_callback_data_t;
typedef struct {
bool initialized;
grn_rc rc;
struct {
bool processed;
bool header_sent;
ngx_http_request_t *r;
ngx_int_t rc;
ngx_chain_t *free_chain;
ngx_chain_t *busy_chain;
} raw;
struct {
grn_obj head;
grn_obj body;
grn_obj foot;
} typed;
} ngx_http_groonga_handler_data_t;
typedef void (*ngx_http_groonga_loc_conf_callback_pt)(ngx_http_groonga_loc_conf_t *conf, void *user_data);
ngx_module_t ngx_http_groonga_module;
static grn_ctx *context = NULL;
static ngx_http_groonga_loc_conf_t *ngx_http_groonga_current_location_conf = NULL;
static char *
ngx_str_null_terminate(ngx_pool_t *pool, const ngx_str_t *string)
{
char *null_terminated_c_string;
null_terminated_c_string = ngx_pnalloc(pool, string->len + 1);
if (!null_terminated_c_string) {
return NULL;
}
memcpy(null_terminated_c_string, string->data, string->len);
null_terminated_c_string[string->len] = '\0';
return null_terminated_c_string;
}
static bool
ngx_str_equal_c_string(ngx_str_t *string, const char *c_string)
{
if (string->len != strlen(c_string)) {
return false;
}
return memcmp(c_string, string->data, string->len) == 0;
}
static bool
ngx_str_start_with_c_string(ngx_str_t *string, const char *c_string)
{
const size_t c_string_len = strlen(c_string);
if (string->len < c_string_len) {
return false;
}
return memcmp(c_string, string->data, c_string_len) == 0;
}
static bool
ngx_str_is_custom_path(ngx_str_t *string)
{
if (string->len == 0) {
return false;
}
if (strncmp((const char *)(string->data), "off", string->len) == 0) {
return false;
}
return true;
}
static uint32_t
ngx_http_groonga_get_thread_limit(void *data)
{
return 1;
}
static ngx_int_t
ngx_http_groonga_grn_rc_to_http_status(grn_rc rc)
{
switch (rc) {
case GRN_SUCCESS :
return NGX_HTTP_OK;
case GRN_INVALID_ARGUMENT :
case GRN_FUNCTION_NOT_IMPLEMENTED :
case GRN_SYNTAX_ERROR :
return NGX_HTTP_BAD_REQUEST;
case GRN_CANCEL :
return NGX_HTTP_REQUEST_TIME_OUT;
default :
return NGX_HTTP_INTERNAL_SERVER_ERROR;
}
}
static void
ngx_http_groonga_write_fd(ngx_fd_t fd, const char *message, size_t message_size)
{
size_t rest_message_size = message_size;
const char *current_message = message;
while (rest_message_size > 0) {
ssize_t written_size;
/* TODO: Remove the no const cast when ngx_write_fd() put const to
* "void *buf". */
written_size = ngx_write_fd(fd, (void *)current_message, rest_message_size);
if (written_size == -1) {
/* TODO: Should we report this error? But where? */
break;
}
rest_message_size -= written_size;
current_message += written_size;
}
}
static void
ngx_http_groonga_logger_log(grn_ctx *ctx, grn_log_level level,
const char *timestamp, const char *title,
const char *message, const char *location,
void *user_data)
{
ngx_open_file_t *file = user_data;
char level_marks[] = " EACewnid-";
if (!file) {
return;
}
ngx_lock_fd(file->fd);
ngx_http_groonga_write_fd(file->fd, timestamp, strlen(timestamp));
ngx_http_groonga_write_fd(file->fd, "|", 1);
ngx_http_groonga_write_fd(file->fd, level_marks + level, 1);
ngx_http_groonga_write_fd(file->fd, "|", 1);
if (location && *location) {
ngx_http_groonga_write_fd(file->fd, location, strlen(location));
ngx_http_groonga_write_fd(file->fd, ": ", 2);
if (title && *title) {
ngx_http_groonga_write_fd(file->fd, title, strlen(title));
ngx_http_groonga_write_fd(file->fd, " ", 1);
}
} else {
ngx_http_groonga_write_fd(file->fd, title, strlen(title));
ngx_http_groonga_write_fd(file->fd, " ", 1);
}
ngx_http_groonga_write_fd(file->fd,
message, strlen(message));
ngx_http_groonga_write_fd(file->fd, "\n", 1);
ngx_unlock_fd(file->fd);
}
static void
ngx_http_groonga_logger_reopen(grn_ctx *ctx, void *user_data)
{
GRN_LOG(ctx, GRN_LOG_NOTICE, "log will be closed.");
ngx_reopen_files((ngx_cycle_t *)ngx_cycle, -1);
GRN_LOG(ctx, GRN_LOG_NOTICE, "log opened.");
}
static void
ngx_http_groonga_logger_fin(grn_ctx *ctx, void *user_data)
{
}
static grn_logger ngx_http_groonga_logger = {
GRN_LOG_DEFAULT_LEVEL,
GRN_LOG_TIME | GRN_LOG_MESSAGE | GRN_LOG_PID,
NULL,
ngx_http_groonga_logger_log,
ngx_http_groonga_logger_reopen,
ngx_http_groonga_logger_fin
};
static ngx_int_t
ngx_http_groonga_context_init_logger(ngx_http_groonga_loc_conf_t *location_conf,
ngx_pool_t *pool,
ngx_log_t *log)
{
if (ngx_http_groonga_current_location_conf) {
ngx_http_groonga_current_location_conf->log_level =
grn_logger_get_max_level(context);
}
ngx_http_groonga_logger.max_level = location_conf->log_level;
ngx_http_groonga_logger.user_data = location_conf->log_file;
grn_logger_set(context, &ngx_http_groonga_logger);
return NGX_OK;
}
static void
ngx_http_groonga_query_logger_log(grn_ctx *ctx, unsigned int flag,
const char *timestamp, const char *info,
const char *message, void *user_data)
{
ngx_open_file_t *file = user_data;
if (!file) {
return;
}
ngx_lock_fd(file->fd);
ngx_http_groonga_write_fd(file->fd, timestamp, strlen(timestamp));
ngx_http_groonga_write_fd(file->fd, "|", 1);
ngx_http_groonga_write_fd(file->fd, info, strlen(info));
ngx_http_groonga_write_fd(file->fd, message, strlen(message));
ngx_http_groonga_write_fd(file->fd, "\n", 1);
ngx_unlock_fd(file->fd);
}
static void
ngx_http_groonga_query_logger_reopen(grn_ctx *ctx, void *user_data)
{
ngx_reopen_files((ngx_cycle_t *)ngx_cycle, -1);
}
static void
ngx_http_groonga_query_logger_fin(grn_ctx *ctx, void *user_data)
{
}
static grn_query_logger ngx_http_groonga_query_logger = {
GRN_QUERY_LOG_DEFAULT,
NULL,
ngx_http_groonga_query_logger_log,
ngx_http_groonga_query_logger_reopen,
ngx_http_groonga_query_logger_fin
};
static ngx_int_t
ngx_http_groonga_context_init_query_logger(ngx_http_groonga_loc_conf_t *location_conf,
ngx_pool_t *pool,
ngx_log_t *log)
{
ngx_http_groonga_query_logger.user_data = location_conf->query_log_file;
grn_query_logger_set(context, &ngx_http_groonga_query_logger);
return NGX_OK;
}
static ngx_int_t
ngx_http_groonga_context_init(ngx_http_groonga_loc_conf_t *location_conf,
ngx_pool_t *pool,
ngx_log_t *log)
{
ngx_int_t status;
if (location_conf == ngx_http_groonga_current_location_conf) {
return NGX_OK;
}
status = ngx_http_groonga_context_init_logger(location_conf,
pool,
log);
if (status == NGX_ERROR) {
return status;
}
status = ngx_http_groonga_context_init_query_logger(location_conf,
pool,
log);
if (status == NGX_ERROR) {
return status;
}
grn_ctx_use(context, location_conf->database);
grn_db_set_cache(context,
location_conf->database,
location_conf->cache);
/* TODO: It doesn't work yet. We need to implement request timeout
* handler. */
if (location_conf->default_request_timeout_msec == NGX_CONF_UNSET_MSEC) {
grn_set_default_request_timeout(0.0);
} else {
double timeout;
timeout = location_conf->default_request_timeout_msec / 1000.0;
grn_set_default_request_timeout(timeout);
}
ngx_http_groonga_current_location_conf = location_conf;
return status;
}
static void
ngx_http_groonga_context_log_error(ngx_log_t *log)
{
if (context->rc == GRN_SUCCESS) {
return;
}
ngx_log_error(NGX_LOG_ERR, log, 0, "%s", context->errbuf);
}
static ngx_int_t
ngx_http_groonga_context_check_error(ngx_log_t *log)
{
if (context->rc == GRN_SUCCESS) {
return NGX_OK;
} else {
ngx_http_groonga_context_log_error(log);
return NGX_HTTP_BAD_REQUEST;
}
}
static ngx_buf_t *
ngx_http_groonga_grn_obj_to_ngx_buf(ngx_pool_t *pool, grn_obj *object)
{
ngx_buf_t *buffer;
buffer = ngx_pcalloc(pool, sizeof(ngx_buf_t));
if (buffer == NULL) {
return NULL;
}
/* adjust the pointers of the buffer */
buffer->pos = (u_char *)GRN_TEXT_VALUE(object);
buffer->last = (u_char *)GRN_TEXT_VALUE(object) + GRN_TEXT_LEN(object);
buffer->memory = 1; /* this buffer is in memory */
buffer->in_file = 0;
return buffer;
}
static void
ngx_http_groonga_handler_cleanup(void *user_data)
{
ngx_http_groonga_handler_data_t *data = user_data;
if (!data->initialized) {
return;
}
GRN_OBJ_FIN(context, &(data->typed.head));
GRN_OBJ_FIN(context, &(data->typed.body));
GRN_OBJ_FIN(context, &(data->typed.foot));
}
static void
ngx_http_groonga_handler_set_content_type(ngx_http_request_t *r,
const char *content_type)
{
r->headers_out.content_type.len = strlen(content_type);
r->headers_out.content_type.data = (u_char *)content_type;
r->headers_out.content_type_len = r->headers_out.content_type.len;
}
static void
ngx_http_groonga_context_receive_handler_raw(grn_ctx *context,
int flags,
ngx_http_groonga_handler_data_t *data)
{
char *chunk = NULL;
unsigned int chunk_size = 0;
int recv_flags;
ngx_http_request_t *r;
ngx_log_t *log;
bool is_last_chunk;
grn_ctx_recv(context, &chunk, &chunk_size, &recv_flags);
data->raw.processed = true;
if (data->raw.rc != NGX_OK) {
return;
}
r = data->raw.r;
log = r->connection->log;
is_last_chunk = (flags & GRN_CTX_TAIL);
if (!data->raw.header_sent) {
ngx_http_groonga_handler_set_content_type(r, grn_ctx_get_mime_type(context));
r->headers_out.status = NGX_HTTP_OK;
if (is_last_chunk) {
r->headers_out.content_length_n = chunk_size;
if (chunk_size == 0) {
r->header_only = 1;
}
} else {
r->headers_out.content_length_n = -1;
}
data->raw.rc = ngx_http_send_header(r);
data->raw.header_sent = true;
if (data->raw.rc != NGX_OK) {
return;
}
}
if (chunk_size > 0 || is_last_chunk) {
ngx_chain_t *chain;
chain = ngx_chain_get_free_buf(r->pool, &(data->raw.free_chain));
if (!chain) {
ngx_log_error(NGX_LOG_ERR, log, 0,
"http_groonga: failed to allocate memory for chunked body");
data->raw.rc = NGX_ERROR;
return;
}
if (chunk_size == 0) {
chain->buf->pos = NULL;
chain->buf->last = NULL;
chain->buf->memory = 0;
} else {
chain->buf->pos = (u_char *)chunk;
chain->buf->last = (u_char *)chunk + chunk_size;
chain->buf->memory = 1;
}
chain->buf->tag = (ngx_buf_tag_t)&ngx_http_groonga_module;
chain->buf->flush = 1;
chain->buf->temporary = 0;
chain->buf->in_file = 0;
if (is_last_chunk) {
chain->buf->last_buf = 1;
} else {
chain->buf->last_buf = 0;
}
chain->next = NULL;
data->raw.rc = ngx_http_output_filter(r, chain);
ngx_chain_update_chains(r->pool,
&(data->raw.free_chain),
&(data->raw.busy_chain),
&chain,
(ngx_buf_tag_t)&ngx_http_groonga_module);
GRN_QUERY_LOG(context, GRN_QUERY_LOG_SIZE,
":",
"send(%u)",
chunk_size);
}
}
static void
ngx_http_groonga_context_receive_handler_typed(grn_ctx *context,
int flags,
ngx_http_groonga_handler_data_t *data)
{
char *result = NULL;
unsigned int result_size = 0;
int recv_flags;
if (!(flags & GRN_CTX_TAIL)) {
return;
}
grn_ctx_recv(context, &result, &result_size, &recv_flags);
#ifdef NGX_GRN_SUPPORT_STOP_BY_COMMAND
if (recv_flags == GRN_CTX_QUIT) {
ngx_int_t ngx_rc;
ngx_int_t ngx_pid;
if (ngx_process == NGX_PROCESS_SINGLE) {
ngx_pid = getpid();
} else {
ngx_pid = getppid();
}
ngx_rc = ngx_os_signal_process((ngx_cycle_t *)ngx_cycle,
"quit",
ngx_pid);
if (ngx_rc == NGX_OK) {
context->stat &= ~GRN_CTX_QUIT;
grn_ctx_recv(context, &result, &result_size, &recv_flags);
context->stat |= GRN_CTX_QUIT;
} else {
context->rc = GRN_OPERATION_NOT_PERMITTED;
result = "false";
result_size = strlen(result);
context->stat &= ~GRN_CTX_QUIT;
}
}
#endif
if (result_size > 0 ||
GRN_TEXT_LEN(&(data->typed.body)) > 0 ||
context->rc != GRN_SUCCESS) {
if (result_size > 0) {
GRN_TEXT_PUT(context, &(data->typed.body), result, result_size);
}
grn_output_envelope(context,
context->rc,
&(data->typed.head),
&(data->typed.body),
&(data->typed.foot),
NULL,
0);
GRN_QUERY_LOG(context, GRN_QUERY_LOG_SIZE,
":",
"send(%" GRN_FMT_SIZE ")",
GRN_TEXT_LEN(&(data->typed.head)) +
GRN_TEXT_LEN(&(data->typed.body)) +
GRN_TEXT_LEN(&(data->typed.foot)));
}
}
static void
ngx_http_groonga_context_receive_handler(grn_ctx *context,
int flags,
void *callback_data)
{
ngx_http_groonga_handler_data_t *data = callback_data;
switch (grn_ctx_get_output_type(context)) {
case GRN_CONTENT_GROONGA_COMMAND_LIST :
case GRN_CONTENT_NONE :
ngx_http_groonga_context_receive_handler_raw(context, flags, data);
break;
default :
ngx_http_groonga_context_receive_handler_typed(context, flags, data);
break;
}
}
static ngx_int_t
ngx_http_groonga_extract_command_path(ngx_http_request_t *r,
ngx_str_t *command_path)
{
size_t base_path_length;
ngx_http_core_loc_conf_t *http_location_conf;
ngx_http_groonga_loc_conf_t *location_conf;
http_location_conf = ngx_http_get_module_loc_conf(r, ngx_http_core_module);
location_conf = ngx_http_get_module_loc_conf(r, ngx_http_groonga_module);
command_path->data = r->unparsed_uri.data;
command_path->len = r->unparsed_uri.len;
base_path_length = http_location_conf->name.len;
if (location_conf->base_path.len > 0) {
if (command_path->len < location_conf->base_path.len) {
ngx_log_error(NGX_LOG_WARN, r->connection->log, 0,
"requested URI is shorter than groonga_base_path: "
"URI: <%V>, groonga_base_path: <%V>",
&(r->unparsed_uri), &(location_conf->base_path));
} else if (strncmp((const char *)command_path->data,
(const char *)(location_conf->base_path.data),
location_conf->base_path.len) < 0) {
ngx_log_error(NGX_LOG_WARN, r->connection->log, 0,
"groonga_base_path doesn't match requested URI: "
"URI: <%V>, groonga_base_path: <%V>",
&(r->unparsed_uri), &(location_conf->base_path));
} else {
base_path_length = location_conf->base_path.len;
}
}
command_path->data += base_path_length;
command_path->len -= base_path_length;
if (command_path->len > 0 && command_path->data[0] == '/') {
command_path->data += 1;
command_path->len -= 1;
}
if (command_path->len == 0) {
return NGX_HTTP_BAD_REQUEST;
}
return NGX_OK;
}
static ngx_int_t
ngx_http_groonga_handler_create_data(ngx_http_request_t *r,
ngx_http_groonga_handler_data_t **data_return)
{
ngx_int_t rc;
ngx_http_groonga_loc_conf_t *location_conf;
ngx_http_cleanup_t *cleanup;
ngx_http_groonga_handler_data_t *data;
location_conf = ngx_http_get_module_loc_conf(r, ngx_http_groonga_module);
rc = ngx_http_groonga_context_init(location_conf, r->pool, r->connection->log);
if (rc != NGX_OK) {
return rc;
}
cleanup = ngx_http_cleanup_add(r, sizeof(ngx_http_groonga_handler_data_t));
cleanup->handler = ngx_http_groonga_handler_cleanup;
data = cleanup->data;
*data_return = data;
data->initialized = true;
data->rc = GRN_SUCCESS;
data->raw.processed = false;
data->raw.header_sent = false;
data->raw.r = r;
data->raw.rc = NGX_OK;
data->raw.free_chain = NULL;
data->raw.busy_chain = NULL;
GRN_TEXT_INIT(&(data->typed.head), GRN_NO_FLAGS);
GRN_TEXT_INIT(&(data->typed.body), GRN_NO_FLAGS);
GRN_TEXT_INIT(&(data->typed.foot), GRN_NO_FLAGS);
grn_ctx_use(context, location_conf->database);
rc = ngx_http_groonga_context_check_error(r->connection->log);
if (rc != NGX_OK) {
return rc;
}
grn_ctx_recv_handler_set(context,
ngx_http_groonga_context_receive_handler,
data);
return NGX_OK;
}
static void
ngx_http_groonga_handler_process_command_path(ngx_http_request_t *r,
ngx_str_t *command_path,
ngx_http_groonga_handler_data_t *data,
int flags)
{
grn_obj uri;
GRN_TEXT_INIT(&uri, 0);
GRN_TEXT_PUTS(context, &uri, "/d/");
GRN_TEXT_PUT(context, &uri, command_path->data, command_path->len);
grn_ctx_send(context, GRN_TEXT_VALUE(&uri), GRN_TEXT_LEN(&uri), flags);
data->rc = context->rc;
ngx_http_groonga_context_log_error(r->connection->log);
GRN_OBJ_FIN(context, &uri);
}
static bool
ngx_http_groonga_handler_validate_post_command(ngx_http_request_t *r,
ngx_str_t *command_path,
ngx_http_groonga_handler_data_t *data)
{
ngx_str_t command;
command.data = command_path->data;
if (r->args.len == 0) {
command.len = command_path->len;
} else {
command.len = command_path->len - r->args.len - strlen("?");
}
if (ngx_str_equal_c_string(&command, "load")) {
return true;
}
if (ngx_str_start_with_c_string(&command, "load.")) {
return true;
}
data->rc = GRN_INVALID_ARGUMENT;
ngx_http_groonga_handler_set_content_type(r, "text/plain");
GRN_TEXT_PUTS(context, &(data->typed.body),
"command for POST must be <load>: <");
GRN_TEXT_PUT(context, &(data->typed.body), command.data, command.len);
GRN_TEXT_PUTS(context, &(data->typed.body), ">");
return false;
}
static void
ngx_http_groonga_send_body(ngx_http_request_t *r,
ngx_http_groonga_handler_data_t *data)
{
ngx_log_t *log;
grn_obj line_buffer;
size_t line_start_offset;
size_t line_check_start_offset;
ngx_chain_t *chain;
size_t line_buffer_chunk_size = 4096;
log = r->connection->log;
GRN_TEXT_INIT(&line_buffer, 0);
line_start_offset = 0;
line_check_start_offset = 0;
for (chain = r->request_body->bufs; chain; chain = chain->next) {
ngx_buf_t *buffer;
size_t rest_buffer_size;
off_t offset;
buffer = chain->buf;
rest_buffer_size = ngx_buf_size(buffer);
offset = 0;
while (rest_buffer_size > 0) {
size_t current_buffer_size;
if (rest_buffer_size > line_buffer_chunk_size) {
current_buffer_size = line_buffer_chunk_size;
} else {
current_buffer_size = rest_buffer_size;
}
if (ngx_buf_in_memory(buffer)) {
GRN_TEXT_PUT(context,
&line_buffer,
buffer->pos + offset,
current_buffer_size);
} else {
ngx_int_t rc;
grn_bulk_reserve(context, &line_buffer, current_buffer_size);
rc = ngx_read_file(buffer->file,
(u_char *)GRN_BULK_CURR(&line_buffer),
current_buffer_size,
offset);
if (rc < 0) {
GRN_PLUGIN_ERROR(context,
GRN_INPUT_OUTPUT_ERROR,
"[nginx][post][body][read] "
"failed to read a request body from file");
goto exit;
}
GRN_BULK_INCR_LEN(&line_buffer, current_buffer_size);
}
offset += current_buffer_size;
rest_buffer_size -= current_buffer_size;
{
const char *line_start;
const char *line_current;
const char *line_end;
line_start = GRN_TEXT_VALUE(&line_buffer) + line_start_offset;
line_end = GRN_TEXT_VALUE(&line_buffer) + GRN_TEXT_LEN(&line_buffer);
for (line_current = line_start + line_check_start_offset;
line_current < line_end;
line_current++) {
size_t line_length;
int flags = GRN_NO_FLAGS;
if (*line_current != '\n') {
continue;
}
line_length = line_current - line_start + 1;
if (line_current + 1 == line_end &&
!chain->next &&
rest_buffer_size == 0) {
flags |= GRN_CTX_TAIL;
}
grn_ctx_send(context, line_start, line_length, flags);
line_start_offset += line_length;
line_start += line_length;
ngx_http_groonga_context_log_error(log);
if (context->rc != GRN_SUCCESS && data->rc == GRN_SUCCESS) {
data->rc = context->rc;
}
}
if (line_start_offset == 0) {
line_buffer_chunk_size *= 2;
line_check_start_offset = GRN_TEXT_LEN(&line_buffer);
} else if ((size_t)GRN_TEXT_LEN(&line_buffer) == line_start_offset) {
GRN_BULK_REWIND(&line_buffer);
line_start_offset = 0;
line_check_start_offset = 0;
} else {
size_t rest_line_size;
rest_line_size = GRN_TEXT_LEN(&line_buffer) - line_start_offset;
grn_memmove(GRN_TEXT_VALUE(&line_buffer),
GRN_TEXT_VALUE(&line_buffer) + line_start_offset,
rest_line_size);
grn_bulk_truncate(context, &line_buffer, rest_line_size);
line_start_offset = 0;
line_check_start_offset = GRN_TEXT_LEN(&line_buffer);
}
}
}
}
if (GRN_TEXT_LEN(&line_buffer) > 0) {
grn_ctx_send(context,
GRN_TEXT_VALUE(&line_buffer),
GRN_TEXT_LEN(&line_buffer),
GRN_CTX_TAIL);
ngx_http_groonga_context_log_error(log);
if (context->rc != GRN_SUCCESS && data->rc == GRN_SUCCESS) {
data->rc = context->rc;
}
}
exit :
GRN_OBJ_FIN(context, &line_buffer);
}
static void
ngx_http_groonga_handler_process_body(ngx_http_request_t *r,
ngx_http_groonga_handler_data_t *data)
{
ngx_buf_t *body;
body = r->request_body->bufs->buf;
if (!body) {
data->rc = GRN_INVALID_ARGUMENT;
ngx_http_groonga_handler_set_content_type(r, "text/plain");
GRN_TEXT_PUTS(context, &(data->typed.body), "must send load data as body");
return;
}
ngx_http_groonga_send_body(r, data);
}
static void
ngx_http_groonga_handler_process_load(ngx_http_request_t *r,
ngx_str_t *command_path,
ngx_http_groonga_handler_data_t *data)
{
if (!ngx_http_groonga_handler_validate_post_command(r, command_path, data)) {
return;
}
ngx_http_groonga_handler_process_command_path(r,
command_path,
data,
GRN_NO_FLAGS);
if (data->rc != GRN_SUCCESS) {
return;
}
ngx_http_groonga_handler_process_body(r, data);
}
static ngx_chain_t *
ngx_http_groonga_attach_chain(ngx_chain_t *chain, ngx_chain_t *new_chain)
{
ngx_chain_t *last_chain;
if (new_chain->buf->last == new_chain->buf->pos) {
return chain;
}
new_chain->buf->last_buf = 1;
new_chain->next = NULL;
if (!chain) {
return new_chain;
}
chain->buf->last_buf = 0;
last_chain = chain;
while (last_chain->next) {
last_chain = last_chain->next;
}
last_chain->next = new_chain;
return chain;
}
static ngx_int_t
ngx_http_groonga_handler_send_response(ngx_http_request_t *r,
ngx_http_groonga_handler_data_t *data)
{
ngx_int_t rc;
const char *content_type;
ngx_buf_t *head_buf, *body_buf, *foot_buf;
ngx_chain_t head_chain, body_chain, foot_chain;
ngx_chain_t *output_chain = NULL;
if (data->raw.processed) {
return data->raw.rc;
}
/* set the 'Content-type' header */
if (r->headers_out.content_type.len == 0) {
grn_obj *foot = &(data->typed.foot);
if (grn_ctx_get_output_type(context) == GRN_CONTENT_JSON &&
GRN_TEXT_LEN(foot) > 0 &&
GRN_TEXT_VALUE(foot)[GRN_TEXT_LEN(foot) - 1] == ';') {
content_type = "application/javascript";
} else {
content_type = grn_ctx_get_mime_type(context);
}
ngx_http_groonga_handler_set_content_type(r, content_type);
}
/* allocate buffers for a response body */
head_buf = ngx_http_groonga_grn_obj_to_ngx_buf(r->pool, &(data->typed.head));
if (!head_buf) {
return NGX_HTTP_INTERNAL_SERVER_ERROR;
}
body_buf = ngx_http_groonga_grn_obj_to_ngx_buf(r->pool, &(data->typed.body));
if (!body_buf) {
return NGX_HTTP_INTERNAL_SERVER_ERROR;
}
foot_buf = ngx_http_groonga_grn_obj_to_ngx_buf(r->pool, &(data->typed.foot));
if (!foot_buf) {
return NGX_HTTP_INTERNAL_SERVER_ERROR;
}
/* attach buffers to the buffer chain */
head_chain.buf = head_buf;
output_chain = ngx_http_groonga_attach_chain(output_chain, &head_chain);
body_chain.buf = body_buf;
output_chain = ngx_http_groonga_attach_chain(output_chain, &body_chain);
foot_chain.buf = foot_buf;
output_chain = ngx_http_groonga_attach_chain(output_chain, &foot_chain);
/* set the status line */
r->headers_out.status = ngx_http_groonga_grn_rc_to_http_status(data->rc);
r->headers_out.content_length_n = GRN_TEXT_LEN(&(data->typed.head)) +
GRN_TEXT_LEN(&(data->typed.body)) +
GRN_TEXT_LEN(&(data->typed.foot));
if (r->headers_out.content_length_n == 0) {
r->header_only = 1;
}
/* send the headers of your response */
rc = ngx_http_send_header(r);
if (rc == NGX_ERROR || rc > NGX_OK || r->header_only) {
return rc;
}
/* send the buffer chain of your response */
rc = ngx_http_output_filter(r, output_chain);
return rc;
}