-
Notifications
You must be signed in to change notification settings - Fork 6
/
ngx_http_redis_module.c
991 lines (778 loc) · 29.1 KB
/
ngx_http_redis_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
/*
* Copyright (C) Igor Sysoev
* Copyright (C) Sergey A. Osokin
*/
#define NGX_ESCAPE_REDIS 4
#define REDIS_AUTH_CMD "*2\r\n$4\r\nauth\r\n"
#define REDIS_GET_CMD "*2\r\n$3\r\nget\r\n"
#define REDIS_SELECT_CMD "*2\r\n$6\r\nselect\r\n"
#define REDIS_PLUSOKCRLF "+OK\r\n"
#define REDIS_ERR "$-1"
#include <ngx_config.h>
#include <ngx_core.h>
#include <ngx_http.h>
#include <nginx.h>
typedef struct {
ngx_http_upstream_conf_t upstream;
ngx_int_t index;
ngx_int_t db;
ngx_int_t auth;
ngx_uint_t gzip_flag;
} ngx_http_redis_loc_conf_t;
typedef struct {
size_t rest;
ngx_http_request_t *request;
ngx_str_t key;
} ngx_http_redis_ctx_t;
static ngx_int_t ngx_http_redis_create_request(ngx_http_request_t *r);
static ngx_int_t ngx_http_redis_reinit_request(ngx_http_request_t *r);
static ngx_int_t ngx_http_redis_process_header(ngx_http_request_t *r);
static ngx_int_t ngx_http_redis_filter_init(void *data);
static ngx_int_t ngx_http_redis_filter(void *data, ssize_t bytes);
static void ngx_http_redis_abort_request(ngx_http_request_t *r);
static void ngx_http_redis_finalize_request(ngx_http_request_t *r,
ngx_int_t rc);
static ngx_int_t ngx_http_redis_add_variables(ngx_conf_t *cf);
static void *ngx_http_redis_create_loc_conf(ngx_conf_t *cf);
static char *ngx_http_redis_merge_loc_conf(ngx_conf_t *cf,
void *parent, void *child);
static char *ngx_http_redis_pass(ngx_conf_t *cf, ngx_command_t *cmd,
void *conf);
static ngx_conf_bitmask_t ngx_http_redis_next_upstream_masks[] = {
{ ngx_string("error"), NGX_HTTP_UPSTREAM_FT_ERROR },
{ ngx_string("timeout"), NGX_HTTP_UPSTREAM_FT_TIMEOUT },
{ ngx_string("invalid_response"), NGX_HTTP_UPSTREAM_FT_INVALID_HEADER },
{ ngx_string("not_found"), NGX_HTTP_UPSTREAM_FT_HTTP_404 },
{ ngx_string("off"), NGX_HTTP_UPSTREAM_FT_OFF },
{ ngx_null_string, 0 }
};
static ngx_command_t ngx_http_redis_commands[] = {
{ ngx_string("redis_pass"),
NGX_HTTP_LOC_CONF|NGX_HTTP_LIF_CONF|NGX_CONF_TAKE1,
ngx_http_redis_pass,
NGX_HTTP_LOC_CONF_OFFSET,
0,
NULL },
#if defined nginx_version && nginx_version >= 8022
{ ngx_string("redis_bind"),
NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
ngx_http_upstream_bind_set_slot,
NGX_HTTP_LOC_CONF_OFFSET,
offsetof(ngx_http_redis_loc_conf_t, upstream.local),
NULL },
#endif
{ ngx_string("redis_connect_timeout"),
NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
ngx_conf_set_msec_slot,
NGX_HTTP_LOC_CONF_OFFSET,
offsetof(ngx_http_redis_loc_conf_t, upstream.connect_timeout),
NULL },
{ ngx_string("redis_send_timeout"),
NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
ngx_conf_set_msec_slot,
NGX_HTTP_LOC_CONF_OFFSET,
offsetof(ngx_http_redis_loc_conf_t, upstream.send_timeout),
NULL },
{ ngx_string("redis_buffer_size"),
NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
ngx_conf_set_size_slot,
NGX_HTTP_LOC_CONF_OFFSET,
offsetof(ngx_http_redis_loc_conf_t, upstream.buffer_size),
NULL },
{ ngx_string("redis_read_timeout"),
NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
ngx_conf_set_msec_slot,
NGX_HTTP_LOC_CONF_OFFSET,
offsetof(ngx_http_redis_loc_conf_t, upstream.read_timeout),
NULL },
{ ngx_string("redis_next_upstream"),
NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_1MORE,
ngx_conf_set_bitmask_slot,
NGX_HTTP_LOC_CONF_OFFSET,
offsetof(ngx_http_redis_loc_conf_t, upstream.next_upstream),
&ngx_http_redis_next_upstream_masks },
{ ngx_string("redis_gzip_flag"),
NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
ngx_conf_set_num_slot,
NGX_HTTP_LOC_CONF_OFFSET,
offsetof(ngx_http_redis_loc_conf_t, gzip_flag),
NULL },
ngx_null_command
};
static ngx_http_module_t ngx_http_redis_module_ctx = {
ngx_http_redis_add_variables, /* preconfiguration */
NULL, /* postconfiguration */
NULL, /* create main configuration */
NULL, /* init main configuration */
NULL, /* create server configuration */
NULL, /* merge server configuration */
ngx_http_redis_create_loc_conf, /* create location configration */
ngx_http_redis_merge_loc_conf /* merge location configration */
};
ngx_module_t ngx_http_redis_module = {
NGX_MODULE_V1,
&ngx_http_redis_module_ctx, /* module context */
ngx_http_redis_commands, /* module directives */
NGX_HTTP_MODULE, /* module type */
NULL, /* init master */
NULL, /* init module */
NULL, /* init process */
NULL, /* init thread */
NULL, /* exit thread */
NULL, /* exit process */
NULL, /* exit master */
NGX_MODULE_V1_PADDING
};
/* Initialize additional var for hide "Content-Enconding: gzip" header */
static ngx_str_t ngx_http_redis_hide_headers[] = {
ngx_null_string
};
static ngx_str_t ngx_http_redis_key = ngx_string("redis_key");
static ngx_str_t ngx_http_redis_db = ngx_string("redis_db");
static ngx_str_t ngx_http_redis_auth = ngx_string("redis_auth");
static ngx_uint_t ngx_http_redis_db_index;
static ngx_uint_t ngx_http_redis_auth_index;
#define NGX_HTTP_REDIS_END (sizeof(ngx_http_redis_end) - 1)
static u_char ngx_http_redis_end[] = CRLF;
static ngx_int_t
ngx_http_redis_handler(ngx_http_request_t *r)
{
ngx_int_t rc;
ngx_http_upstream_t *u;
ngx_http_redis_ctx_t *ctx;
ngx_http_redis_loc_conf_t *rlcf;
if (!(r->method & (NGX_HTTP_GET|NGX_HTTP_HEAD))) {
return NGX_HTTP_NOT_ALLOWED;
}
rc = ngx_http_discard_request_body(r);
if (rc != NGX_OK) {
return rc;
}
if (ngx_http_set_content_type(r) != NGX_OK) {
return NGX_HTTP_INTERNAL_SERVER_ERROR;
}
#if defined nginx_version && nginx_version >= 8011
if (ngx_http_upstream_create(r) != NGX_OK) {
#else
rlcf = ngx_http_get_module_loc_conf(r, ngx_http_redis_module);
u = ngx_pcalloc(r->pool, sizeof(ngx_http_upstream_t));
if (u == NULL) {
#endif
return NGX_HTTP_INTERNAL_SERVER_ERROR;
}
#if defined nginx_version && nginx_version >= 8011
u = r->upstream;
#endif
#if defined nginx_version && nginx_version >= 8037
ngx_str_set(&u->schema, "redis://");
#else
u->schema.len = sizeof("redis://") - 1;
u->schema.data = (u_char *) "redis://";
#endif
#if defined nginx_version && nginx_version >= 8011
u->output.tag = (ngx_buf_tag_t) &ngx_http_redis_module;
#else
u->peer.log = r->connection->log;
u->peer.log_error = NGX_ERROR_ERR;
#endif
#if defined nginx_version && nginx_version >= 8011
rlcf = ngx_http_get_module_loc_conf(r, ngx_http_redis_module);
#else
u->output.tag = (ngx_buf_tag_t) &ngx_http_redis_module;
#endif
u->conf = &rlcf->upstream;
u->create_request = ngx_http_redis_create_request;
u->reinit_request = ngx_http_redis_reinit_request;
u->process_header = ngx_http_redis_process_header;
u->abort_request = ngx_http_redis_abort_request;
u->finalize_request = ngx_http_redis_finalize_request;
#if defined nginx_version && nginx_version < 8011
r->upstream = u;
#endif
ctx = ngx_palloc(r->pool, sizeof(ngx_http_redis_ctx_t));
if (ctx == NULL) {
return NGX_HTTP_INTERNAL_SERVER_ERROR;
}
ctx->rest = NGX_HTTP_REDIS_END;
ctx->request = r;
ngx_http_set_ctx(r, ctx, ngx_http_redis_module);
u->input_filter_init = ngx_http_redis_filter_init;
u->input_filter = ngx_http_redis_filter;
u->input_filter_ctx = ctx;
#if defined nginx_version && nginx_version >= 8011
r->main->count++;
#endif
ngx_http_upstream_init(r);
return NGX_DONE;
}
static ngx_int_t
ngx_http_redis_create_request(ngx_http_request_t *r)
{
size_t len = 0;
uintptr_t escape;
ngx_buf_t *b;
ngx_chain_t *cl;
ngx_http_redis_ctx_t *ctx;
ngx_http_variable_value_t *vv[3];
ngx_http_redis_loc_conf_t *rlcf;
u_char lenbuf[NGX_INT_T_LEN];
rlcf = ngx_http_get_module_loc_conf(r, ngx_http_redis_module);
vv[0] = ngx_http_get_indexed_variable(r, ngx_http_redis_auth_index);
if (vv[0] == NULL || vv[0]->not_found || vv[0]->len == 0) {
ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
"no auth command provided" );
} else {
len += sizeof(REDIS_AUTH_CMD) + sizeof("$") - 1;
len += ngx_sprintf(lenbuf, "%d", vv[0]->len) - lenbuf;
len += sizeof(CRLF) - 1 + vv[0]->len;
ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
"auth info: %s", vv[0]->data);
}
len += sizeof(CRLF) - 1;
vv[1] = ngx_http_get_indexed_variable(r, ngx_http_redis_db_index);
/*
* If user do not select redis database in nginx.conf by redis_db
* variable, just add size of "select 0" to request. This is add
* some overhead in talk with redis, but this way simplify parsing
* the redis answer in ngx_http_redis_process_header().
*/
if (vv[1] == NULL || vv[1]->not_found || vv[1]->len == 0) {
ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
"select 0 redis database" );
len += sizeof(REDIS_SELECT_CMD) + sizeof("$1") + sizeof(CRLF) + sizeof("0") - 1;
} else {
len += sizeof(REDIS_SELECT_CMD) + sizeof("$") - 1;
len += ngx_sprintf(lenbuf, "%d", vv[1]->len) - lenbuf;
len += sizeof(CRLF) - 1 + vv[1]->len;
ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
"select %s redis database", vv[1]->data);
}
len += sizeof(CRLF) - 1;
vv[2] = ngx_http_get_indexed_variable(r, rlcf->index);
/* If nginx.conf have no redis_key return error. */
if (vv[2] == NULL || vv[2]->not_found || vv[2]->len == 0) {
ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
"the \"$redis_key\" variable is not set");
return NGX_ERROR;
}
/* Count have space required escape symbols. */
escape = 2 * ngx_escape_uri(NULL, vv[2]->data, vv[2]->len, NGX_ESCAPE_REDIS);
len += sizeof(REDIS_GET_CMD) + sizeof("$") - 1;
len += ngx_sprintf(lenbuf, "%d", vv[2]->len) - lenbuf;
len += sizeof(CRLF) - 1 + vv[2]->len + escape + sizeof(CRLF) - 1;
/* Create temporary buffer for request with size len. */
b = ngx_create_temp_buf(r->pool, len);
if (b == NULL) {
return NGX_ERROR;
}
cl = ngx_alloc_chain_link(r->pool);
if (cl == NULL) {
return NGX_ERROR;
}
cl->buf = b;
cl->next = NULL;
r->upstream->request_bufs = cl;
/* add "auth " for request */
if (vv[0] != NULL && !(vv[0]->not_found) && vv[0]->len != 0) {
/* Add "auth " for request. */
b->last = ngx_sprintf(b->last, "%s$%d%s", REDIS_AUTH_CMD, vv[0]->len, CRLF);
b->last = ngx_copy(b->last, vv[0]->data, vv[0]->len);
*b->last++ = CR; *b->last++ = LF;
}
/* Get context redis_db from configuration file. */
ctx = ngx_http_get_module_ctx(r, ngx_http_redis_module);
ctx->key.data = b->last;
/*
* Add "0" as redis number db to request if redis_db undefined,
* othervise add real number from context.
*/
if (vv[1] == NULL || vv[1]->not_found || vv[1]->len == 0) {
b->last = ngx_sprintf(b->last, "%s$1%s", REDIS_SELECT_CMD, CRLF);
ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
"select 0 redis database" );
*b->last++ = '0';
} else {
b->last = ngx_sprintf(b->last, "%s$%d%s", REDIS_SELECT_CMD, vv[1]->len, CRLF);
b->last = ngx_copy(b->last, vv[1]->data, vv[1]->len);
ctx->key.len = b->last - ctx->key.data;
ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
"select %V redis database", &ctx->key);
}
/* Add "\r\n". */
*b->last++ = CR; *b->last++ = LF;
b->last = ngx_sprintf(b->last, "%s$%d%s", REDIS_GET_CMD, vv[2]->len, CRLF);
/* Get context redis_key from nginx.conf. */
ctx = ngx_http_get_module_ctx(r, ngx_http_redis_module);
ctx->key.data = b->last;
/*
* If no escape symbols then copy data as is, othervise use
* escape-copy function.
*/
if (escape == 0) {
b->last = ngx_copy(b->last, vv[2]->data, vv[2]->len);
} else {
b->last = (u_char *) ngx_escape_uri(b->last, vv[2]->data, vv[2]->len,
NGX_ESCAPE_REDIS);
}
ctx->key.len = b->last - ctx->key.data;
ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
"http redis request: \"%V\"", &ctx->key);
/* Add one more "\r\n". */
*b->last++ = CR; *b->last++ = LF;
/*
* Summary, the request looks like this:
* "auth $redis_auth\r\nselect $redis_db\r\nget $redis_key\r\n", where
* $redis_auth, $redis_db and $redis_key are variable's values.
*/
return NGX_OK;
}
static ngx_int_t
ngx_http_redis_reinit_request(ngx_http_request_t *r)
{
return NGX_OK;
}
static ngx_int_t
ngx_http_redis_process_header(ngx_http_request_t *r)
{
u_char *p, *len;
u_int c, try;
ngx_str_t line;
ngx_table_elt_t *h;
ngx_http_upstream_t *u;
ngx_http_redis_ctx_t *ctx;
ngx_http_redis_loc_conf_t *rlcf;
ngx_http_variable_value_t *vv;
ngx_int_t no_auth_cmd;
vv = ngx_http_get_indexed_variable(r, ngx_http_redis_auth_index);
no_auth_cmd = (vv == NULL || vv->not_found || vv->len == 0);
c = try = 0;
u = r->upstream;
p = u->buffer.pos;
/*
* Good answer from redis should looks like this:
* "+OK\r\n+OK\r\n$8\r\n12345678\r\n"
*
* Here is:
* "+OK\r\n+OK\r\n" is answer for first two commands
* "auth password" and "select 0"
* Next two strings are answer for command "get $redis_key", where
*
* "$8" is length of following next string and
* "12345678" is value of $redis_key, the string.
*
* So, if the first symbol is:
* "+" (good answer) - try to find 2 or 3 strings;
* "-" (bad answer) - try to find 1 string;
* othervise answer is invalid.
*/
if (*p == '+') {
if (no_auth_cmd) {
try = 2;
} else {
try = 3;
}
} else if (*p == '-') {
try = 1;
} else {
goto no_valid;
}
for (p = u->buffer.pos; p < u->buffer.last; p++) {
if (*p == LF) {
c++;
if (c == try) {
goto found;
}
}
}
return NGX_AGAIN;
found:
*p = '\0';
line.len = p - u->buffer.pos - 1;
line.data = u->buffer.pos;
ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
"redis: \"%V\"", &line);
p = u->buffer.pos;
/* Get context of redis_key for future error messages, i.e. ctx->key */
ctx = ngx_http_get_module_ctx(r, ngx_http_redis_module);
rlcf = ngx_http_get_module_loc_conf(r, ngx_http_redis_module);
/* Compare pointer and error message, if yes set 502 and return */
if (ngx_strncmp(p, "-ERR", sizeof("-ERR") - 1) == 0) {
ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
"redis sent error in response \"%V\" "
"for key \"%V\"",
&line, &ctx->key);
u->headers_in.status_n = 502;
u->state->status = 502;
return NGX_OK;
}
/* Compare pointer and good message, if yes move on the pointer */
vv = ngx_http_get_indexed_variable(r, ngx_http_redis_auth_index);
if (no_auth_cmd) {
if (ngx_strncmp(p, REDIS_PLUSOKCRLF, sizeof(REDIS_PLUSOKCRLF) - 1) == 0) {
p += sizeof(REDIS_PLUSOKCRLF) - 1;
} else {
ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
"+OK\\r\\n was expected here");
}
} else { /* check double of the "+OK\r\n" */
if ((ngx_strncmp(p, REDIS_PLUSOKCRLF, sizeof(REDIS_PLUSOKCRLF) - 1) == 0) &&
(ngx_strncmp(p + sizeof(REDIS_PLUSOKCRLF) - 1,
REDIS_PLUSOKCRLF, sizeof(REDIS_PLUSOKCRLF) - 1) == 0)) {
p += 2 * (sizeof(REDIS_PLUSOKCRLF) - 1);
} else {
ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
"+OK\\r\\n+OK\\r\\n was expected here");
}
}
/*
* Compare pointer and "get" answer. As said before, "$" means, that
* next symbols are length for upcoming key, "-1" means no key.
* Set 404 and return.
*/
if (ngx_strncmp(p, REDIS_ERR, sizeof(REDIS_ERR) - 1) == 0) {
ngx_log_error(NGX_LOG_INFO, r->connection->log, 0,
"key: \"%V\" was not found by redis", &ctx->key);
u->headers_in.status_n = 404;
u->state->status = 404;
#if defined nginx_version && nginx_version >= 1001004
u->keepalive = 1;
#endif
return NGX_OK;
}
/* Compare pointer and "get" answer, if "$"... */
if (ngx_strncmp(p, "$", sizeof("$") - 1) == 0) {
/* move on pointer */
p += sizeof("$") - 1;
/* set len to pointer */
len = p;
/* if defined gzip_flag... */
if (rlcf->gzip_flag) {
/* hash init */
h = ngx_list_push(&r->upstream->headers_in.headers);
if (h == NULL) {
return NGX_ERROR;
}
/*
* add Content-Encoding header for future gunzipping
* with ngx_http_gunzip_filter module
*/
h->hash = ngx_hash(ngx_hash(ngx_hash(ngx_hash(
ngx_hash(ngx_hash(ngx_hash(
ngx_hash(ngx_hash(ngx_hash(
ngx_hash(ngx_hash(ngx_hash(
ngx_hash(ngx_hash('c', 'o'), 'n'), 't'), 'e'),
'n'), 't'), '-'), 'e'), 'n'), 'c'), 'o'),
'd'), 'i'), 'n'), 'g');
ngx_str_set(&h->key, "Content-Encoding");
ngx_str_set(&h->value, "gzip");
h->lowcase_key = (u_char*) "content-encoding";
#if (NGX_HTTP_GZIP)
u->headers_in.content_encoding = h;
#endif
}
/* try to find end of string */
while (*p && *p++ != CR) { /* void */ }
/*
* get the length of upcoming redis_key value, convert from ascii
* if the length is empty, return
*/
#if defined nginx_version && nginx_version < 1001004
r->headers_out.content_length_n = ngx_atoof(len, p - len - 1);
if (r->headers_out.content_length_n == -1) {
#else
u->headers_in.content_length_n = ngx_atoof(len, p - len - 1);
if (u->headers_in.content_length_n == -1) {
#endif
ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
"redis sent invalid length in response \"%V\" "
"for key \"%V\"",
&line, &ctx->key);
return NGX_HTTP_UPSTREAM_INVALID_HEADER;
}
/* The length of answer is not empty, set 200 */
u->headers_in.status_n = 200;
u->state->status = 200;
/* Set position to the first symbol of data and return */
u->buffer.pos = p + 1;
return NGX_OK;
}
no_valid:
ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
"redis sent invalid response: \"%V\"", &line);
return NGX_HTTP_UPSTREAM_INVALID_HEADER;
}
static ngx_int_t
ngx_http_redis_filter_init(void *data)
{
ngx_http_redis_ctx_t *ctx = data;
ngx_http_upstream_t *u;
u = ctx->request->upstream;
#if defined nginx_version && nginx_version < 1005003
u->length += NGX_HTTP_REDIS_END;
#else
if (u->headers_in.status_n != 404) {
u->length = u->headers_in.content_length_n + NGX_HTTP_REDIS_END;
ctx->rest = NGX_HTTP_REDIS_END;
} else {
u->length = 0;
}
#endif
return NGX_OK;
}
static ngx_int_t
ngx_http_redis_filter(void *data, ssize_t bytes)
{
ngx_http_redis_ctx_t *ctx = data;
u_char *last;
ngx_buf_t *b;
ngx_chain_t *cl, **ll;
ngx_http_upstream_t *u;
u = ctx->request->upstream;
b = &u->buffer;
#if defined nginx_version && nginx_version < 1001004
if (u->length == ctx->rest) {
#else
if (u->length == (ssize_t) ctx->rest) {
#endif
if (ngx_strncmp(b->last,
ngx_http_redis_end + NGX_HTTP_REDIS_END - ctx->rest,
bytes)
!= 0)
{
ngx_log_error(NGX_LOG_ERR, ctx->request->connection->log, 0,
"redis sent invalid trailer");
u->length = 0;
ctx->rest = 0;
return NGX_OK;
}
u->length -= bytes;
ctx->rest -= bytes;
#if defined nginx_version && nginx_version >= 1001004
if (u->length == 0) {
u->keepalive = 1;
}
#endif
return NGX_OK;
}
for (cl = u->out_bufs, ll = &u->out_bufs; cl; cl = cl->next) {
ll = &cl->next;
}
cl = ngx_chain_get_free_buf(ctx->request->pool, &u->free_bufs);
if (cl == NULL) {
return NGX_ERROR;
}
cl->buf->flush = 1;
cl->buf->memory = 1;
*ll = cl;
last = b->last;
cl->buf->pos = last;
b->last += bytes;
cl->buf->last = b->last;
cl->buf->tag = u->output.tag;
ngx_log_debug4(NGX_LOG_DEBUG_HTTP, ctx->request->connection->log, 0,
"redis filter bytes:%z size:%z length:%z rest:%z",
bytes, b->last - b->pos, u->length, ctx->rest);
if (bytes <= (ssize_t) (u->length - NGX_HTTP_REDIS_END)) {
u->length -= bytes;
return NGX_OK;
}
last += (size_t) (u->length - NGX_HTTP_REDIS_END);
if (ngx_strncmp(last, ngx_http_redis_end, b->last - last) != 0) {
ngx_log_error(NGX_LOG_ERR, ctx->request->connection->log, 0,
"redis sent invalid trailer");
#if defined nginx_version && nginx_version >= 1001004
b->last = last;
cl->buf->last = last;
u->length = 0;
ctx->rest = 0;
return NGX_OK;
#endif
}
ctx->rest -= b->last - last;
b->last = last;
cl->buf->last = last;
u->length = ctx->rest;
#if defined nginx_version && nginx_version >= 1001004
if (u->length == 0) {
u->keepalive = 1;
}
#endif
return NGX_OK;
}
static void
ngx_http_redis_abort_request(ngx_http_request_t *r)
{
ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
"abort http redis request");
return;
}
static void
ngx_http_redis_finalize_request(ngx_http_request_t *r, ngx_int_t rc)
{
ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
"finalize http redis request");
return;
}
static void *
ngx_http_redis_create_loc_conf(ngx_conf_t *cf)
{
ngx_http_redis_loc_conf_t *conf;
conf = ngx_pcalloc(cf->pool, sizeof(ngx_http_redis_loc_conf_t));
if (conf == NULL) {
#if defined nginx_version && nginx_version >= 8011
return NULL;
#else
return NGX_CONF_ERROR;
#endif
}
/*
* set by ngx_pcalloc():
*
* conf->upstream.bufs.num = 0;
* conf->upstream.next_upstream = 0;
* conf->upstream.temp_path = NULL;
* conf->upstream.uri = { 0, NULL };
* conf->upstream.location = NULL;
*/
conf->upstream.connect_timeout = NGX_CONF_UNSET_MSEC;
conf->upstream.send_timeout = NGX_CONF_UNSET_MSEC;
conf->upstream.read_timeout = NGX_CONF_UNSET_MSEC;
conf->upstream.buffer_size = NGX_CONF_UNSET_SIZE;
/* the hardcoded values */
conf->upstream.cyclic_temp_file = 0;
conf->upstream.buffering = 0;
conf->upstream.ignore_client_abort = 0;
conf->upstream.send_lowat = 0;
conf->upstream.bufs.num = 0;
conf->upstream.busy_buffers_size = 0;
conf->upstream.max_temp_file_size = 0;
conf->upstream.temp_file_write_size = 0;
conf->upstream.intercept_errors = 1;
conf->upstream.intercept_404 = 1;
conf->upstream.pass_request_headers = 0;
conf->upstream.pass_request_body = 0;
/*
* initialize additional parameters for hide
* "Content-Encoding: gzip" header
*/
conf->upstream.hide_headers = NGX_CONF_UNSET_PTR;
conf->upstream.pass_headers = NGX_CONF_UNSET_PTR;
conf->index = NGX_CONF_UNSET;
conf->db = NGX_CONF_UNSET;
conf->gzip_flag = NGX_CONF_UNSET_UINT;
return conf;
}
static char *
ngx_http_redis_merge_loc_conf(ngx_conf_t *cf, void *parent, void *child)
{
ngx_hash_init_t hash;
ngx_http_redis_loc_conf_t *prev = parent;
ngx_http_redis_loc_conf_t *conf = child;
ngx_conf_merge_msec_value(conf->upstream.connect_timeout,
prev->upstream.connect_timeout, 60000);
ngx_conf_merge_msec_value(conf->upstream.send_timeout,
prev->upstream.send_timeout, 60000);
ngx_conf_merge_msec_value(conf->upstream.read_timeout,
prev->upstream.read_timeout, 60000);
ngx_conf_merge_size_value(conf->upstream.buffer_size,
prev->upstream.buffer_size,
(size_t) ngx_pagesize);
ngx_conf_merge_bitmask_value(conf->upstream.next_upstream,
prev->upstream.next_upstream,
(NGX_CONF_BITMASK_SET
|NGX_HTTP_UPSTREAM_FT_ERROR
|NGX_HTTP_UPSTREAM_FT_TIMEOUT));
if (conf->upstream.next_upstream & NGX_HTTP_UPSTREAM_FT_OFF) {
conf->upstream.next_upstream = NGX_CONF_BITMASK_SET
|NGX_HTTP_UPSTREAM_FT_OFF;
}
/* Initialize hash for hide "Content-Encoding" header */
hash.max_size = 512;
hash.bucket_size = ngx_align(64, ngx_cacheline_size);
hash.name = "redis_hide_headers_hash";
if (ngx_http_upstream_hide_headers_hash(cf, &conf->upstream,
&prev->upstream, ngx_http_redis_hide_headers, &hash)
!= NGX_OK)
{
return NGX_CONF_ERROR;
}
if (conf->upstream.upstream == NULL) {
conf->upstream.upstream = prev->upstream.upstream;
}
if (conf->index == NGX_CONF_UNSET) {
conf->index = prev->index;
}
if (conf->db == NGX_CONF_UNSET) {
conf->db = prev->db;
}
ngx_conf_merge_uint_value(conf->gzip_flag, prev->gzip_flag, 0);
return NGX_CONF_OK;
}
static char *
ngx_http_redis_pass(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
{
ngx_http_redis_loc_conf_t *rlcf = conf;
ngx_str_t *value;
ngx_url_t u;
ngx_http_core_loc_conf_t *clcf;
if (rlcf->upstream.upstream) {
return "is duplicate";
}
value = cf->args->elts;
ngx_memzero(&u, sizeof(ngx_url_t));
u.url = value[1];
u.no_resolve = 1;
rlcf->upstream.upstream = ngx_http_upstream_add(cf, &u, 0);
if (rlcf->upstream.upstream == NULL) {
return NGX_CONF_ERROR;
}
clcf = ngx_http_conf_get_module_loc_conf(cf, ngx_http_core_module);
clcf->handler = ngx_http_redis_handler;
if (clcf->name.data[clcf->name.len - 1] == '/') {
clcf->auto_redirect = 1;
}
rlcf->index = ngx_http_get_variable_index(cf, &ngx_http_redis_key);
if (rlcf->index == NGX_ERROR) {
return NGX_CONF_ERROR;
}
rlcf->db = ngx_http_get_variable_index(cf, &ngx_http_redis_db);
rlcf->auth = ngx_http_get_variable_index(cf, &ngx_http_redis_auth);
return NGX_CONF_OK;
}
static ngx_int_t
ngx_http_redis_reset_variable(ngx_http_request_t *r,
ngx_http_variable_value_t *v, uintptr_t data)
{
*v = ngx_http_variable_null_value;
return NGX_OK;
}
static ngx_int_t
ngx_http_redis_add_variables(ngx_conf_t *cf)
{
ngx_int_t n;
ngx_http_variable_t *var;
ngx_http_variable_t *authvar;
var = ngx_http_add_variable(cf, &ngx_http_redis_db,
NGX_HTTP_VAR_CHANGEABLE);
if (var == NULL) {
return NGX_ERROR;
}
var->get_handler = ngx_http_redis_reset_variable;
n = ngx_http_get_variable_index(cf, &ngx_http_redis_db);
if (n == NGX_ERROR) {
return NGX_ERROR;
}
ngx_http_redis_db_index = n;
authvar = ngx_http_add_variable(cf, &ngx_http_redis_auth,
NGX_HTTP_VAR_CHANGEABLE);
if (authvar == NULL) {
return NGX_ERROR;
}
authvar->get_handler = ngx_http_redis_reset_variable;
n = ngx_http_get_variable_index(cf, &ngx_http_redis_auth);
if (n == NGX_ERROR) {
return NGX_ERROR;
}
ngx_http_redis_auth_index = n;
return NGX_OK;
}