forked from mdirolf/nginx-gridfs
/
ngx_http_gridfs_module.c
883 lines (756 loc) · 30.5 KB
/
ngx_http_gridfs_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
/*
* Copyright 2009-2010 Michael Dirolf
*
* Dual Licensed under the Apache License, Version 2.0 and the GNU
* General Public License, version 2 or (at your option) any later
* version.
*
* -- Apache License
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* -- GNU GPL
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
/*
* TODO range support http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.35
*/
#include <ngx_config.h>
#include <ngx_core.h>
#include <ngx_http.h>
#include "mongo-c-driver/src/mongo.h"
#include "mongo-c-driver/src/gridfs.h"
static char * ngx_http_mongo(ngx_conf_t *cf, ngx_command_t *cmd, void *dummy);
static char* ngx_http_gridfs(ngx_conf_t* directive, ngx_command_t* command, void* gridfs_conf);
static void* ngx_http_gridfs_create_main_conf(ngx_conf_t* directive);
static void* ngx_http_gridfs_create_loc_conf(ngx_conf_t* directive);
static char* ngx_http_gridfs_merge_loc_conf(ngx_conf_t* directive, void* parent, void* child);
static ngx_int_t ngx_http_gridfs_init_worker(ngx_cycle_t* cycle);
static ngx_int_t ngx_http_gridfs_handler(ngx_http_request_t* request);
static void ngx_http_gridfs_cleanup(void* data);
typedef struct {
ngx_str_t db;
ngx_str_t root_collection;
ngx_str_t field;
ngx_uint_t type;
ngx_str_t user;
ngx_str_t pass;
ngx_str_t mongo;
ngx_array_t* mongods; /* ngx_http_mongod_server_t */
} ngx_http_gridfs_loc_conf_t;
typedef struct {
ngx_str_t db;
ngx_str_t user;
ngx_str_t pass;
} ngx_http_mongo_auth_t;
typedef struct {
ngx_str_t name;
mongo_connection conn;
ngx_array_t *auths; /* ngx_http_mongo_auth_t */
} ngx_http_mongo_connection_t;
typedef struct {
ngx_str_t host;
in_port_t port;
} ngx_http_mongod_server_t;
typedef struct {
ngx_array_t loc_confs; /* ngx_http_gridfs_loc_conf_t */
} ngx_http_gridfs_main_conf_t;
typedef struct {
mongo_cursor ** cursors;
ngx_uint_t numchunks;
} ngx_http_gridfs_cleanup_t;
static ngx_command_t ngx_http_gridfs_commands[] = {
{
ngx_string("mongo"),
NGX_HTTP_LOC_CONF | NGX_CONF_1MORE,
ngx_http_mongo,
NGX_HTTP_LOC_CONF_OFFSET,
0,
NULL
},
{
ngx_string("gridfs"),
NGX_HTTP_LOC_CONF | NGX_CONF_1MORE,
ngx_http_gridfs,
NGX_HTTP_LOC_CONF_OFFSET,
0,
NULL
},
ngx_null_command
};
static ngx_http_module_t ngx_http_gridfs_module_ctx = {
NULL, /* preconfiguration */
NULL, /* postconfiguration */
ngx_http_gridfs_create_main_conf,
NULL, /* init main configuration */
NULL, /* create server configuration */
NULL, /* init serever configuration */
ngx_http_gridfs_create_loc_conf,
ngx_http_gridfs_merge_loc_conf
};
ngx_module_t ngx_http_gridfs_module = {
NGX_MODULE_V1,
&ngx_http_gridfs_module_ctx,
ngx_http_gridfs_commands,
NGX_HTTP_MODULE,
NULL,
NULL,
ngx_http_gridfs_init_worker,
NULL,
NULL,
NULL,
NULL,
NGX_MODULE_V1_PADDING
};
ngx_array_t ngx_http_mongo_connections;
static char * ngx_http_mongo(ngx_conf_t *cf, ngx_command_t *cmd, void *void_conf) {
ngx_str_t *value;
ngx_url_t u;
ngx_uint_t i;
ngx_http_mongod_server_t *mongod_server;
ngx_http_gridfs_loc_conf_t *gridfs_loc_conf;
gridfs_loc_conf = void_conf;
value = cf->args->elts;
gridfs_loc_conf->mongo = value[1];
gridfs_loc_conf->mongods = ngx_array_create(cf->pool, 4,
sizeof(ngx_http_mongod_server_t));
if (gridfs_loc_conf->mongods == NULL) {
return NULL;
}
for (i = 1; i < cf->args->nelts; i++) {
ngx_memzero(&u, sizeof(ngx_url_t));
u.url = value[i];
u.default_port = 27017;
if (ngx_parse_url(cf->pool, &u) != NGX_OK) {
if (u.err) {
ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
"%s in mongo \"%V\"", u.err, &u.url);
}
return NGX_CONF_ERROR;
}
mongod_server = ngx_array_push(gridfs_loc_conf->mongods);
mongod_server->host = u.host;
mongod_server->port = u.port;
}
return NGX_CONF_OK;
}
static char* ngx_http_gridfs(ngx_conf_t* cf, ngx_command_t* command, void* void_conf) {
ngx_http_gridfs_loc_conf_t *gridfs_loc_conf = void_conf;
ngx_http_core_loc_conf_t* core_conf;
ngx_str_t *value, type;
ngx_uint_t i;
core_conf = ngx_http_conf_get_module_loc_conf(cf, ngx_http_core_module);
core_conf-> handler = ngx_http_gridfs_handler;
value = cf->args->elts;
gridfs_loc_conf->db = value[1];
/* Parse the parameters */
for (i = 2; i < cf->args->nelts; i++) {
if (ngx_strncmp(value[i].data, "root_collection=", 16) == 0) {
gridfs_loc_conf->root_collection.data = (u_char *) &value[i].data[16];
gridfs_loc_conf->root_collection.len = ngx_strlen(&value[i].data[16]);
continue;
}
if (ngx_strncmp(value[i].data, "field=", 6) == 0) {
gridfs_loc_conf->field.data = (u_char *) &value[i].data[6];
gridfs_loc_conf->field.len = ngx_strlen(&value[i].data[6]);
/* Currently only support for "_id" and "filename" */
if (gridfs_loc_conf->field.data != NULL
&& ngx_strcmp(gridfs_loc_conf->field.data, "filename") != 0
&& ngx_strcmp(gridfs_loc_conf->field.data, "_id") != 0) {
ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
"Unsupported Field: %s", gridfs_loc_conf->field.data);
return NGX_CONF_ERROR;
}
continue;
}
if (ngx_strncmp(value[i].data, "type=", 5) == 0) {
type = (ngx_str_t) ngx_string(&value[i].data[5]);
/* Currently only support for "objectid", "string", and "int" */
if (type.len == 0) {
gridfs_loc_conf->type = NGX_CONF_UNSET_UINT;
} else if (ngx_strcasecmp(type.data, (u_char *)"objectid") == 0) {
gridfs_loc_conf->type = bson_oid;
} else if (ngx_strcasecmp(type.data, (u_char *)"string") == 0) {
gridfs_loc_conf->type = bson_string;
} else if (ngx_strcasecmp(type.data, (u_char *)"int") == 0) {
gridfs_loc_conf->type = bson_int;
} else {
ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
"Unsupported Type: %s", (char *)value[i].data);
return NGX_CONF_ERROR;
}
continue;
}
if (ngx_strncmp(value[i].data, "user=", 5) == 0) {
gridfs_loc_conf->user.data = (u_char *) &value[i].data[5];
gridfs_loc_conf->user.len = ngx_strlen(&value[i].data[5]);
continue;
}
if (ngx_strncmp(value[i].data, "pass=", 5) == 0) {
gridfs_loc_conf->pass.data = (u_char *) &value[i].data[5];
gridfs_loc_conf->pass.len = ngx_strlen(&value[i].data[5]);
continue;
}
ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
"invalid parameter \"%V\"", &value[i]);
return NGX_CONF_ERROR;
}
if (gridfs_loc_conf->field.data != NULL
&& ngx_strcmp(gridfs_loc_conf->field.data, "filename") == 0
&& gridfs_loc_conf->type != bson_string) {
ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
"Field: filename, must be of Type: string");
return NGX_CONF_ERROR;
}
if ((gridfs_loc_conf->user.data == NULL || gridfs_loc_conf->user.len == 0)
&& !(gridfs_loc_conf->pass.data == NULL || gridfs_loc_conf->pass.len == 0)) {
ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
"Password without username");
return NGX_CONF_ERROR;
}
if (!(gridfs_loc_conf->user.data == NULL || gridfs_loc_conf->user.len == 0)
&& (gridfs_loc_conf->pass.data == NULL || gridfs_loc_conf->pass.len == 0)) {
ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
"Username without password");
return NGX_CONF_ERROR;
}
return NGX_CONF_OK;
}
static void *ngx_http_gridfs_create_main_conf(ngx_conf_t *cf) {
ngx_http_gridfs_main_conf_t *gridfs_main_conf;
gridfs_main_conf = ngx_pcalloc(cf->pool, sizeof(ngx_http_gridfs_main_conf_t));
if (gridfs_main_conf == NULL) {
return NULL;
}
if (ngx_array_init(&gridfs_main_conf->loc_confs, cf->pool, 4,
sizeof(ngx_http_gridfs_loc_conf_t *))
!= NGX_OK) {
return NULL;
}
return gridfs_main_conf;
}
static void* ngx_http_gridfs_create_loc_conf(ngx_conf_t* directive) {
ngx_http_gridfs_loc_conf_t* gridfs_conf;
gridfs_conf = ngx_pcalloc(directive->pool, sizeof(ngx_http_gridfs_loc_conf_t));
if (gridfs_conf == NULL) {
ngx_conf_log_error(NGX_LOG_EMERG, directive, 0,
"Failed to allocate memory for GridFS Location Config.");
return NGX_CONF_ERROR;
}
gridfs_conf->db.data = NULL;
gridfs_conf->db.len = 0;
gridfs_conf->root_collection.data = NULL;
gridfs_conf->root_collection.len = 0;
gridfs_conf->field.data = NULL;
gridfs_conf->field.len = 0;
gridfs_conf->type = NGX_CONF_UNSET_UINT;
gridfs_conf->user.data = NULL;
gridfs_conf->user.len = 0;
gridfs_conf->pass.data = NULL;
gridfs_conf->pass.len = 0;
gridfs_conf->mongo.data = NULL;
gridfs_conf->mongo.len = 0;
gridfs_conf->mongods = NGX_CONF_UNSET_PTR;
return gridfs_conf;
}
static char* ngx_http_gridfs_merge_loc_conf(ngx_conf_t* cf, void* void_parent, void* void_child) {
ngx_http_gridfs_loc_conf_t *parent = void_parent;
ngx_http_gridfs_loc_conf_t *child = void_child;
ngx_http_gridfs_main_conf_t *gridfs_main_conf = ngx_http_conf_get_module_main_conf(cf, ngx_http_gridfs_module);
ngx_http_gridfs_loc_conf_t **gridfs_loc_conf;
ngx_http_mongod_server_t *mongod_server;
ngx_conf_merge_str_value(child->db, parent->db, NULL);
ngx_conf_merge_str_value(child->root_collection, parent->root_collection, "fs");
ngx_conf_merge_str_value(child->field, parent->field, "_id");
ngx_conf_merge_uint_value(child->type, parent->type, bson_oid);
ngx_conf_merge_str_value(child->user, parent->user, NULL);
ngx_conf_merge_str_value(child->pass, parent->pass, NULL);
ngx_conf_merge_str_value(child->mongo, parent->mongo, "127.0.0.1:27017");
if (child->mongods == NGX_CONF_UNSET_PTR) {
if (parent->mongods != NGX_CONF_UNSET_PTR) {
child->mongods = parent->mongods;
} else {
child->mongods = ngx_array_create(cf->pool, 4,
sizeof(ngx_http_mongod_server_t));
mongod_server = ngx_array_push(child->mongods);
mongod_server->host.data = (u_char *)"127.0.0.1";
mongod_server->host.len = sizeof("127.0.0.1") - 1;
mongod_server->port = 27017;
}
}
// Add the local gridfs conf to a mongo connection
if (child->db.data) {
gridfs_loc_conf = ngx_array_push(&gridfs_main_conf->loc_confs);
*gridfs_loc_conf = child;
}
return NGX_CONF_OK;
}
ngx_http_mongo_connection_t* ngx_http_get_mongo_connection( ngx_str_t name ) {
ngx_http_mongo_connection_t *mongo_conns;
ngx_uint_t i;
mongo_conns = ngx_http_mongo_connections.elts;
for ( i = 0; i < ngx_http_mongo_connections.nelts; i++ ) {
if ( name.len == mongo_conns[i].name.len
&& ngx_strncmp(name.data, mongo_conns[i].name.data, name.len) == 0 ) {
return &mongo_conns[i];
}
}
return NULL;
}
static ngx_int_t ngx_http_mongo_authenticate(ngx_log_t *log, ngx_http_gridfs_loc_conf_t *gridfs_loc_conf) {
ngx_http_mongo_connection_t* mongo_conn;
ngx_http_mongo_auth_t *mongo_auth;
bson empty;
char *test;
int error;
mongo_conn = ngx_http_get_mongo_connection( gridfs_loc_conf->mongo );
if (mongo_conn == NULL) {
ngx_log_error(NGX_LOG_ERR, log, 0,
"Mongo Connection not found: \"%V\"", &gridfs_loc_conf->mongo);
}
// Authenticate
if (gridfs_loc_conf->user.data != NULL && gridfs_loc_conf->pass.data != NULL) {
if (!mongo_cmd_authenticate( &mongo_conn->conn,
(const char*)gridfs_loc_conf->db.data,
(const char*)gridfs_loc_conf->user.data,
(const char*)gridfs_loc_conf->pass.data )) {
ngx_log_error(NGX_LOG_ERR, log, 0,
"Invalid mongo user/pass: %s/%s",
gridfs_loc_conf->user.data,
gridfs_loc_conf->pass.data);
return NGX_ERROR;
}
mongo_auth = ngx_array_push(mongo_conn->auths);
mongo_auth->db = gridfs_loc_conf->db;
mongo_auth->user = gridfs_loc_conf->user;
mongo_auth->pass = gridfs_loc_conf->pass;
}
// Run a test command to test authentication.
test = (char*)malloc( gridfs_loc_conf->db.len + sizeof(".test"));
ngx_cpystrn((u_char*)test, (u_char*)gridfs_loc_conf->db.data, gridfs_loc_conf->db.len+1);
ngx_cpystrn((u_char*)(test+gridfs_loc_conf->db.len),(u_char*)".test", sizeof(".test"));
bson_empty(&empty);
mongo_find(&mongo_conn->conn, test, &empty, NULL, 0, 0, 0);
error = mongo_cmd_get_last_error(&mongo_conn->conn, (char*)gridfs_loc_conf->db.data, NULL);
free(test);
if (error) {
ngx_log_error(NGX_LOG_ERR, log, 0, "Authentication Required");
return NGX_ERROR;
}
return NGX_OK;
}
static ngx_int_t ngx_http_mongo_add_connection(ngx_cycle_t* cycle, ngx_http_gridfs_loc_conf_t* gridfs_loc_conf) {
ngx_http_mongo_connection_t* mongo_conn;
mongo_connection_options left, right;
mongo_conn_return status;
ngx_http_mongod_server_t *mongods;
mongods = gridfs_loc_conf->mongods->elts;
mongo_conn = ngx_http_get_mongo_connection( gridfs_loc_conf->mongo );
if (mongo_conn != NULL) {
return NGX_OK;
}
mongo_conn = ngx_array_push(&ngx_http_mongo_connections);
if (mongo_conn == NULL) {
return NGX_ERROR;
}
mongo_conn->name = gridfs_loc_conf->mongo;
mongo_conn->auths = ngx_array_create(cycle->pool, 4, sizeof(ngx_http_mongo_auth_t));
if ( gridfs_loc_conf->mongods->nelts == 1 ) {
ngx_cpystrn( (u_char*)left.host,
mongods[0].host.data,
mongods[0].host.len + 1 );
left.port = mongods[0].port;
status = mongo_connect(&mongo_conn->conn, &left);
} else if ( gridfs_loc_conf->mongods->nelts == 2 ) {
ngx_cpystrn( (u_char*)left.host,
mongods[0].host.data,
mongods[0].host.len + 1 );
left.port = mongods[0].port;
ngx_cpystrn( (u_char*)right.host,
mongods[1].host.data,
mongods[1].host.len + 1 );
right.port = mongods[1].port;
status = mongo_connect_pair(&mongo_conn->conn, &left, &right);
} else {
return NGX_ERROR;
}
switch (status) {
case mongo_conn_success:
break;
case mongo_conn_bad_arg:
ngx_log_error(NGX_LOG_ERR, cycle->log, 0,
"Mongo Exception: Bad Arguments");
return NGX_ERROR;
case mongo_conn_no_socket:
ngx_log_error(NGX_LOG_ERR, cycle->log, 0,
"Mongo Exception: No Socket");
return NGX_ERROR;
case mongo_conn_fail:
ngx_log_error(NGX_LOG_ERR, cycle->log, 0,
"Mongo Exception: Connection Failure %s:%i;",
left.host, left.port);
return NGX_ERROR;
case mongo_conn_not_master:
ngx_log_error(NGX_LOG_ERR, cycle->log, 0,
"Mongo Exception: Not Master");
return NGX_ERROR;
default:
ngx_log_error(NGX_LOG_ERR, cycle->log, 0,
"Mongo Exception: Unknown Error");
return NGX_ERROR;
}
return NGX_OK;
}
static ngx_int_t ngx_http_gridfs_init_worker(ngx_cycle_t* cycle) {
ngx_http_gridfs_main_conf_t* gridfs_main_conf = ngx_http_cycle_get_module_main_conf(cycle, ngx_http_gridfs_module);
ngx_http_gridfs_loc_conf_t** gridfs_loc_confs;
ngx_uint_t i;
gridfs_loc_confs = gridfs_main_conf->loc_confs.elts;
ngx_array_init(&ngx_http_mongo_connections, cycle->pool, 4, sizeof(ngx_http_mongo_connection_t));
for (i = 0; i < gridfs_main_conf->loc_confs.nelts; i++) {
if (ngx_http_mongo_add_connection(cycle, gridfs_loc_confs[i]) == NGX_ERROR) {
return NGX_ERROR;
}
if (ngx_http_mongo_authenticate(cycle->log, gridfs_loc_confs[i]) == NGX_ERROR) {
return NGX_ERROR;
}
}
return NGX_OK;
}
static ngx_int_t ngx_http_mongo_reconnect(ngx_log_t *log, ngx_http_mongo_connection_t *mongo_conn) {
mongo_conn_return status;
mongo_disconnect(&mongo_conn->conn);
status = mongo_reconnect(&mongo_conn->conn);
switch (status) {
case mongo_conn_success:
break;
case mongo_conn_bad_arg:
ngx_log_error(NGX_LOG_ERR, log, 0,
"Mongo Exception: Bad Arguments");
return NGX_ERROR;
case mongo_conn_no_socket:
ngx_log_error(NGX_LOG_ERR, log, 0,
"Mongo Exception: No Socket");
return NGX_ERROR;
case mongo_conn_fail:
ngx_log_error(NGX_LOG_ERR, log, 0,
"Mongo Exception: Connection Failure %s:%i;",
mongo_conn->conn.left_opts->host,
mongo_conn->conn.left_opts->port);
return NGX_ERROR;
case mongo_conn_not_master:
ngx_log_error(NGX_LOG_ERR, log, 0,
"Mongo Exception: Not Master");
return NGX_ERROR;
default:
ngx_log_error(NGX_LOG_ERR, log, 0,
"Mongo Exception: Unknown Error");
return NGX_ERROR;
}
return NGX_OK;
}
static ngx_int_t ngx_http_mongo_reauth(ngx_log_t *log, ngx_http_mongo_connection_t *mongo_conn) {
ngx_http_mongo_auth_t *auths;
ngx_uint_t i;
auths = mongo_conn->auths->elts;
for (i = 0; i < mongo_conn->auths->nelts; i++) {
if(!mongo_cmd_authenticate( &mongo_conn->conn,
(const char*)auths[i].db.data,
(const char*)auths[i].user.data,
(const char*)auths[i].pass.data )) {
ngx_log_error(NGX_LOG_ERR, log, 0,
"Invalid mongo user/pass: %s/%s",
auths[i].user.data,
auths[i].pass.data);
return NGX_ERROR;
}
}
return NGX_OK;
}
static char h_digit(char hex) {
return (hex >= '0' && hex <= '9') ? hex - '0': ngx_tolower(hex)-'a'+10;
}
static int htoi(char* h) {
char ok[] = "0123456789AaBbCcDdEeFf";
if (ngx_strchr(ok, h[0]) == NULL || ngx_strchr(ok,h[1]) == NULL) { return -1; }
return h_digit(h[0])*16 + h_digit(h[1]);
}
static int url_decode(char * filename) {
char * read = filename;
char * write = filename;
char hex[3];
int c;
hex[2] = '\0';
while (*read != '\0'){
if (*read == '%') {
hex[0] = *(++read);
if (hex[0] == '\0') return 0;
hex[1] = *(++read);
if (hex[1] == '\0') return 0;
c = htoi(hex);
if (c == -1) return 0;
*write = (char)c;
}
else *write = *read;
read++;
write++;
}
*write = '\0';
return 1;
}
static ngx_int_t ngx_http_gridfs_handler(ngx_http_request_t* request) {
ngx_http_gridfs_loc_conf_t* gridfs_conf;
ngx_http_core_loc_conf_t* core_conf;
ngx_buf_t* buffer;
ngx_chain_t out;
ngx_str_t location_name;
ngx_str_t full_uri;
char* value;
ngx_http_mongo_connection_t *mongo_conn;
gridfs gfs;
gridfile gfile;
gridfs_offset length;
ngx_uint_t chunksize;
ngx_uint_t numchunks;
char* contenttype;
volatile ngx_uint_t i;
volatile ngx_int_t found = 0;
ngx_int_t rc = NGX_OK;
bson query;
bson_buffer buf;
bson_oid_t oid;
mongo_cursor ** cursors;
gridfs_offset chunk_len;
const char * chunk_data;
bson_iterator it;
bson chunk;
ngx_pool_cleanup_t* gridfs_cln;
ngx_http_gridfs_cleanup_t* gridfs_clndata;
gridfs_conf = ngx_http_get_module_loc_conf(request, ngx_http_gridfs_module);
core_conf = ngx_http_get_module_loc_conf(request, ngx_http_core_module);
location_name = core_conf->name;
full_uri = request->uri;
/* defensive */
if (full_uri.len < location_name.len) {
ngx_log_error(NGX_LOG_ERR, request->connection->log, 0,
"Invalid location name or uri.");
return NGX_HTTP_INTERNAL_SERVER_ERROR;
}
/* Extract the value from the uri */
value = (char*)malloc(sizeof(char) * (full_uri.len - location_name.len + 1));
if (value == NULL) {
ngx_log_error(NGX_LOG_ERR, request->connection->log, 0,
"Failed to allocate memory for value buffer.");
return NGX_HTTP_INTERNAL_SERVER_ERROR;
}
memcpy(value, full_uri.data + location_name.len, full_uri.len - location_name.len);
value[full_uri.len - location_name.len] = '\0';
/* URL Decoding */
if (!url_decode(value)) {
ngx_log_error(NGX_LOG_ERR, request->connection->log, 0,
"Malformed request.");
free(value);
return NGX_HTTP_BAD_REQUEST;
}
/* Find the GridFile */
mongo_conn = ngx_http_get_mongo_connection( gridfs_conf->mongo );
if (mongo_conn == NULL) {
ngx_log_error(NGX_LOG_ERR, request->connection->log, 0,
"Mongo Connection not found: \"%V\"", &gridfs_conf->mongo);
return NGX_HTTP_INTERNAL_SERVER_ERROR;
}
MONGO_TRY_GENERIC(&mongo_conn->conn){
gridfs_init(&mongo_conn->conn,
(const char*)gridfs_conf->db.data,
(const char*)gridfs_conf->root_collection.data,
&gfs);
} MONGO_CATCH_GENERIC(&mongo_conn->conn) {
if (ngx_http_mongo_reconnect(request->connection->log, mongo_conn) == NGX_ERROR
|| ngx_http_mongo_reauth(request->connection->log, mongo_conn) == NGX_ERROR) {
ngx_log_error(NGX_LOG_ERR, request->connection->log, 0,
"Mongo connection dropped, could not reconnect");
return NGX_HTTP_SERVICE_UNAVAILABLE;
}
MONGO_TRY_GENERIC(&mongo_conn->conn){
gridfs_init(&mongo_conn->conn,
(const char*)gridfs_conf->db.data,
(const char*)gridfs_conf->root_collection.data,
&gfs);
} MONGO_CATCH_GENERIC(&mongo_conn->conn) {
ngx_log_error(NGX_LOG_ERR, request->connection->log, 0,
"Mongo connection dropped, could not reconnect");
return NGX_HTTP_SERVICE_UNAVAILABLE;
}
}
bson_buffer_init(&buf);
switch (gridfs_conf->type) {
case bson_oid:
bson_oid_from_string(&oid, value);
bson_append_oid(&buf, (char*)gridfs_conf->field.data, &oid);
break;
case bson_int:
bson_append_int(&buf, (char*)gridfs_conf->field.data, ngx_atoi((u_char*)value, strlen(value)));
break;
case bson_string:
bson_append_string(&buf, (char*)gridfs_conf->field.data, value);
break;
}
bson_from_buffer(&query, &buf);
MONGO_TRY_GENERIC(&mongo_conn->conn){
found = gridfs_find_query(&gfs, &query, &gfile);
} MONGO_CATCH_GENERIC(&mongo_conn->conn) {
if (ngx_http_mongo_reconnect(request->connection->log, mongo_conn) == NGX_ERROR
|| ngx_http_mongo_reauth(request->connection->log, mongo_conn) == NGX_ERROR) {
ngx_log_error(NGX_LOG_ERR, request->connection->log, 0,
"Mongo connection dropped, could not reconnect");
return NGX_HTTP_SERVICE_UNAVAILABLE;
}
MONGO_TRY_GENERIC(&mongo_conn->conn){
found = gridfs_find_query(&gfs, &query, &gfile);
} MONGO_CATCH_GENERIC(&mongo_conn->conn) {
ngx_log_error(NGX_LOG_ERR, request->connection->log, 0,
"Mongo connection dropped, could not reconnect");
bson_destroy(&query);
free(value);
return NGX_HTTP_SERVICE_UNAVAILABLE;
}
}
bson_destroy(&query);
free(value);
if(!found){
return NGX_HTTP_NOT_FOUND;
}
/* Get information about the file */
length = gridfile_get_contentlength(&gfile);
chunksize = gridfile_get_chunksize(&gfile);
numchunks = gridfile_get_numchunks(&gfile);
contenttype = (char*)gridfile_get_contenttype(&gfile);
/* Set the headers */
request->headers_out.status = NGX_HTTP_OK;
request->headers_out.content_length_n = length;
if (contenttype != NULL) {
request->headers_out.content_type.len = strlen(contenttype);
request->headers_out.content_type.data = (u_char*)contenttype;
}
else ngx_http_set_content_type(request);
/* Determine if content is gzipped, set headers accordingly */
if ( gridfile_get_boolean(&gfile,"gzipped") ) {
ngx_log_error(NGX_LOG_ERR, request->connection->log, 0, gridfile_get_field(&gfile,"gzipped") );
request->headers_out.content_encoding = ngx_list_push(&request->headers_out.headers);
if (request->headers_out.content_encoding == NULL) {
return NGX_ERROR;
}
request->headers_out.content_encoding->hash = 1;
request->headers_out.content_encoding->key.len = sizeof("Content-Encoding") - 1;
request->headers_out.content_encoding->key.data = (u_char *) "Content-Encoding";
request->headers_out.content_encoding->value.len = sizeof("gzip") - 1;
request->headers_out.content_encoding->value.data = (u_char *) "gzip";
}
ngx_http_send_header(request);
/* Empty file */
if (numchunks == 0) {
/* Allocate space for the response buffer */
buffer = ngx_pcalloc(request->pool, sizeof(ngx_buf_t));
if (buffer == NULL) {
ngx_log_error(NGX_LOG_ERR, request->connection->log, 0,
"Failed to allocate response buffer");
return NGX_HTTP_INTERNAL_SERVER_ERROR;
}
buffer->pos = NULL;
buffer->last = NULL;
buffer->memory = 1;
buffer->last_buf = 1;
out.buf = buffer;
out.next = NULL;
return ngx_http_output_filter(request, &out);
}
cursors = (mongo_cursor **)ngx_pcalloc(request->pool, sizeof(mongo_cursor *) * numchunks);
if (cursors == NULL) {
return NGX_HTTP_INTERNAL_SERVER_ERROR;
}
/* Hook in the cleanup function */
gridfs_cln = ngx_pool_cleanup_add(request->pool, sizeof(ngx_http_gridfs_cleanup_t));
if (gridfs_cln == NULL) {
return NGX_HTTP_INTERNAL_SERVER_ERROR;
}
gridfs_cln->handler = ngx_http_gridfs_cleanup;
gridfs_clndata = gridfs_cln->data;
gridfs_clndata->cursors = cursors;
gridfs_clndata->numchunks = numchunks;
/* Read and serve chunk by chunk */
for (i = 0; i < numchunks; i++) {
/* Allocate space for the response buffer */
buffer = ngx_pcalloc(request->pool, sizeof(ngx_buf_t));
if (buffer == NULL) {
ngx_log_error(NGX_LOG_ERR, request->connection->log, 0,
"Failed to allocate response buffer");
return NGX_HTTP_INTERNAL_SERVER_ERROR;
}
/* Fetch the chunk from mongo */
MONGO_TRY_GENERIC(&mongo_conn->conn) {
cursors[i] = gridfile_get_chunks(&gfile, i, 1);
} MONGO_CATCH_GENERIC(&mongo_conn->conn) {
if (ngx_http_mongo_reconnect(request->connection->log, mongo_conn) == NGX_ERROR
|| ngx_http_mongo_reauth(request->connection->log, mongo_conn) == NGX_ERROR) {
ngx_log_error(NGX_LOG_ERR, request->connection->log, 0,
"Mongo connection dropped, could not reconnect");
return NGX_HTTP_SERVICE_UNAVAILABLE;
}
MONGO_TRY_GENERIC(&mongo_conn->conn){
cursors[i] = gridfile_get_chunks(&gfile, i, 1);
} MONGO_CATCH_GENERIC(&mongo_conn->conn) {
ngx_log_error(NGX_LOG_ERR, request->connection->log, 0,
"Mongo connection dropped, could not reconnect");
return NGX_HTTP_SERVICE_UNAVAILABLE;
}
}
mongo_cursor_next(cursors[i]);
chunk = cursors[i]->current;
bson_find(&it, &chunk, "data");
chunk_len = bson_iterator_bin_len( &it );
chunk_data = bson_iterator_bin_data( &it );
/* Set up the buffer chain */
buffer->pos = (u_char*)chunk_data;
buffer->last = (u_char*)chunk_data + chunk_len;
buffer->memory = 1;
buffer->last_buf = (i == numchunks-1);
out.buf = buffer;
out.next = NULL;
/* Serve the Chunk */
rc = ngx_http_output_filter(request, &out);
/* TODO: More Codes to Catch? */
if (rc == NGX_ERROR) {
return NGX_ERROR;
}
}
return rc;
}
static void ngx_http_gridfs_cleanup(void* data) {
ngx_http_gridfs_cleanup_t* gridfs_clndata;
ngx_uint_t i;
gridfs_clndata = data;
for (i = 0; i < gridfs_clndata->numchunks; i++) {
mongo_cursor_destroy(gridfs_clndata->cursors[i]);
}
}