-
Notifications
You must be signed in to change notification settings - Fork 0
/
ngx_http_mg_module.c
477 lines (394 loc) · 15.6 KB
/
ngx_http_mg_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
#include <ngx_config.h>
#include <ngx_core.h>
#include <ngx_http.h>
#include "mongoc.h"
typedef struct {
ngx_str_t mongo_server_addr;
ngx_str_t mongo_port;
ngx_str_t mongo_database;
ngx_str_t mongo_collection;
ngx_str_t mongo_user;
ngx_str_t mongo_passw;
bson_t unreadable_fields;
} ngx_http_mg_conf_t;
typedef struct {
ngx_str_t error_msg;
ngx_int_t error_code;
} error_s;
static ngx_int_t ngx_http_mg_handler(ngx_http_request_t *);
static char* ngx_http_mg(ngx_conf_t *, ngx_command_t *, void *);
static void* ngx_http_mg_create_loc_conf(ngx_conf_t *);
static char* ngx_http_mg_merge_loc_conf(ngx_conf_t *, void *, void *);
static ngx_int_t ngx_http_mg_handle_get_request(ngx_http_request_t*, ngx_http_mg_conf_t*, ngx_http_complex_value_t*, error_s*);
void ngx_http_mg_handle_post_request(ngx_http_request_t*);
static mongoc_uri_t* ngx_http_create_mongo_conn_uri(char*, char*, char*, char*);
static char* ngx_http_mg_unreadable_fields(ngx_conf_t*, ngx_command_t*, void*);
static ngx_str_t application_type = ngx_string("application/json");
static ngx_command_t ngx_http_mg_commands[] = {
{
ngx_string("mongo"),
NGX_HTTP_LOC_CONF|NGX_CONF_NOARGS|NGX_CONF_TAKE1,
ngx_http_mg,
NGX_HTTP_LOC_CONF_OFFSET,
offsetof(ngx_http_mg_conf_t, mongo_server_addr),
NULL
},
{
ngx_string("mongo_port"),
NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
ngx_conf_set_str_slot,
NGX_HTTP_LOC_CONF_OFFSET,
offsetof(ngx_http_mg_conf_t, mongo_port),
NULL
},
{
ngx_string("mongo_database"),
NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
ngx_conf_set_str_slot,
NGX_HTTP_LOC_CONF_OFFSET,
offsetof(ngx_http_mg_conf_t, mongo_database),
NULL
},
{
ngx_string("mongo_collection"),
NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
ngx_conf_set_str_slot,
NGX_HTTP_LOC_CONF_OFFSET,
offsetof(ngx_http_mg_conf_t, mongo_collection),
NULL
},
{ ngx_string("mongo_user"),
NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
ngx_conf_set_str_slot,
NGX_HTTP_LOC_CONF_OFFSET,
offsetof(ngx_http_mg_conf_t, mongo_user),
NULL
},
{ ngx_string("mongo_passw"),
NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
ngx_conf_set_str_slot,
NGX_HTTP_LOC_CONF_OFFSET,
offsetof(ngx_http_mg_conf_t, mongo_passw),
NULL
},
{ ngx_string("mongo_unreadable_field"),
NGX_HTTP_LOC_CONF|NGX_CONF_TAKE12, //Up to 12 unreadable fields
ngx_http_mg_unreadable_fields,
NGX_HTTP_LOC_CONF_OFFSET,
0,
NULL
},
ngx_null_command
};
static ngx_http_module_t ngx_http_mg_module_ctx = {
NULL,
NULL, //postconfiguration
NULL,
NULL,
NULL,
NULL,
ngx_http_mg_create_loc_conf,
ngx_http_mg_merge_loc_conf
};
ngx_module_t ngx_http_mg_module = {
NGX_MODULE_V1,
&ngx_http_mg_module_ctx,
ngx_http_mg_commands,
NGX_HTTP_MODULE,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
NGX_MODULE_V1_PADDING
};
static ngx_int_t
ngx_http_mg_handler(ngx_http_request_t *r)
{
ngx_int_t rc;
ngx_http_mg_conf_t *mgcf;
ngx_http_complex_value_t cv;
error_s* error = ngx_palloc(r->pool, sizeof(error_s));
//Let's assume we can handle the following.
//GET will get
//POST will create/update
//Todo: What about PUT?
if (!(r->method & (NGX_HTTP_GET|NGX_HTTP_POST))) {
return NGX_HTTP_NOT_ALLOWED;
}
if (r->headers_in.if_modified_since) {
return NGX_HTTP_NOT_MODIFIED;
}
mgcf = ngx_http_get_module_loc_conf(r, ngx_http_mg_module);
//If we've got a GET request, process it
if (r->method == NGX_HTTP_GET) {
if(ngx_http_mg_handle_get_request(r, mgcf, &cv, error) == NGX_ERROR) {
ngx_memzero(&cv, sizeof(ngx_http_complex_value_t));
cv.value.len = error->error_msg.len;
cv.value.data = error->error_msg.data;
return ngx_http_send_response(r, error->error_code, &application_type, &cv);
}
}
if (r->method == NGX_HTTP_POST) {
//Figure out if we want to handle this post request..
if(strcmp(r->headers_in.content_type->value.data, application_type.data) != 0) {
cv.value.data = "{ \"ok\" : false, \"reason\" : \"Incorrect Media Type.\" }";
cv.value.len = strlen(cv.value.data);
return ngx_http_send_response(r, NGX_HTTP_UNSUPPORTED_MEDIA_TYPE, &application_type, &cv);
}
//This will process as many times as it needs to.
//If it's not done, rc will return NGX_AGAIN. In that case,
//let's come back to this at a later date.
//Else, our post handler will execute
rc = ngx_http_read_client_request_body(r, ngx_http_mg_handle_post_request);
if (rc == NGX_AGAIN) {
return NGX_AGAIN;
}
}
return ngx_http_send_response(r, NGX_HTTP_OK, &application_type, &cv);
}
//Build out the complex value from our query and modify our
//request appropriately (handle the application type, etc).
static ngx_int_t
ngx_http_mg_handle_get_request(ngx_http_request_t *r, ngx_http_mg_conf_t* mgcf, ngx_http_complex_value_t* cv, error_s* ngx_mg_req_error) {
//Mongoc related vars
mongoc_client_t *client;
mongoc_collection_t *collection;
mongoc_cursor_t *cursor;
//Dealing with bson here.
bson_error_t error;
const bson_t *doc;
bson_t doc_response = BSON_INITIALIZER;
bson_t query;
bson_t find;
mongoc_uri_t* uri;
int count;
ngx_str_t q;
char* response;
//Query vars.
char* field;
char* value;
ngx_str_t offset = ngx_string("0");
int offsetI;
char* offsetA;
ngx_str_t limit = ngx_string("0");
int limitI;
char* limitA;
uri = ngx_http_create_mongo_conn_uri(mgcf->mongo_server_addr.data, mgcf->mongo_port.data, mgcf->mongo_user.data, mgcf->mongo_passw.data);
//Init our client and connect
mongoc_init();
client = mongoc_client_new_from_uri(uri); //Connect to db. Let's assume for the moment all actions require connection.
bson_init( &query ); //init bson
//https://github.com/mongodb/mongo-c-driver/blob/master/doc/mongoc_collection_find.txt
collection = mongoc_client_get_collection(client, mgcf->mongo_database.data, mgcf->mongo_collection.data); //Get our requested collection
//Also a 500
if(!collection) {
ngx_mg_req_error->error_msg = (ngx_str_t)ngx_string("{ \"ok\" : false, \"reason\" : \"Unable to get collection.\" }");
ngx_mg_req_error->error_code = NGX_HTTP_INTERNAL_SERVER_ERROR;
return NGX_ERROR;
}
//Before we find, let's see if we're querying for anything specific.
//The query will come in the form of ?q=field:value
if(ngx_http_arg(r, (u_char *) "q", 1, &q) == NGX_OK) {
u_char* ptr = strchr(q.data, ':' ); //Find our dividing character.
int index = ptr - q.data; //The index of :
field = ngx_palloc(r->pool, index);
value = ngx_palloc(r->pool, q.len - index - 1);
strncpy(field, q.data, index);
strncpy(value, q.data + index + 1, q.len - index - 1);
bson_append_regex(&query, field, index, value, "i");
}
//Do we need an offset?
ngx_http_arg(r, (u_char *) "offset", 6, &offset);
//What about a limit?
ngx_http_arg(r, (u_char *) "limit", 5, &limit);
offsetI = ngx_atoi(offset.data, offset.len); //Convert offset to int
limitI = ngx_atoi(limit.data, limit.len); //Convert limit to int
//Todo: This needs a check to determine if there's memory left?
limitA = ngx_palloc(r->pool, limit.len);
offsetA = ngx_palloc(r->pool, offset.len);
//Format our offset and limit.
snprintf(offsetA, offset.len + 1, "%s", offset.data); //Offset as string + null char
snprintf(limitA, limit.len + 1, "%s", limit.data); //Offset as string + null char
//Before we query, append our document of unreadable fields (if there are any)
//If we don't have any unreadable fields, make this null.
if(mgcf->unreadable_fields.len > 0) {
cursor = mongoc_collection_find(collection, MONGOC_QUERY_NONE, offsetI, limitI, 0, &query, &mgcf->unreadable_fields, NULL);
} else {
cursor = mongoc_collection_find(collection, MONGOC_QUERY_NONE, offsetI, limitI, 0, &query, NULL, NULL);
}
if(mongoc_cursor_error(cursor, &error)) {
ngx_mg_req_error->error_msg = (ngx_str_t)ngx_string("{ \"ok\" : false, \"reason\" : \"There was a problem connecting to the mongo cluster.\" }");
ngx_mg_req_error->error_code = NGX_HTTP_INTERNAL_SERVER_ERROR;
return NGX_ERROR;
}
while(!mongoc_cursor_error(cursor, &error) && mongoc_cursor_more(cursor)) {
if(mongoc_cursor_next(cursor, &doc)) {
bson_concat(&doc_response, doc); //Concatenate the bson documents into one large document.
}
}
if(mongoc_cursor_error(cursor, &error)) {
ngx_mg_req_error->error_msg = (ngx_str_t)ngx_string("{ \"ok\" : false, \"reason\" : \"There was a problem with the Mongo Cursor.\" }");
ngx_mg_req_error->error_code = NGX_HTTP_INTERNAL_SERVER_ERROR;
return NGX_ERROR;
}
char* str = bson_as_json(&doc_response, NULL);
count = mongoc_collection_count(collection, MONGOC_QUERY_NONE, &query, offsetI, limitI, NULL, &error);
//Ok, need a large enough string to contain our docs + extra info.
response = (char*)ngx_palloc(r->pool, strlen(str) + 49 + limit.len + offset.len + sizeof(count));
//If we couldn't allocate, fail.
if( !response ) {
ngx_mg_req_error->error_msg = (ngx_str_t)ngx_string("{ \"ok\" : false, \"reason\" : \"Unable to allocate enough memory for a response.\" }");
ngx_mg_req_error->error_code = NGX_HTTP_INTERNAL_SERVER_ERROR;
return NGX_ERROR;
}
//Get rid of strncpy, we're well aware of the size being allocated.
sprintf(response, "{\"q_results\" : [ %s ], count: %d, limit: %s, offset: %s }", str, count, limitA, offsetA);
bson_free(str); //Free up str.
bson_destroy(&query);
mongoc_cursor_destroy(cursor);
mongoc_collection_destroy(collection);
ngx_memzero(cv, sizeof(ngx_http_complex_value_t));
cv->value.len = strlen(response);
cv->value.data = response;
return NGX_OK;
}
//Handle posting to Mongo.
void
ngx_http_mg_handle_post_request(ngx_http_request_t* r) {
//Since this will just be called as handler, get our conf information.
ngx_http_mg_conf_t* mgcf = ngx_http_get_module_loc_conf(r, ngx_http_mg_module);
ngx_http_complex_value_t cv;
//Mongoc related vars
mongoc_client_t *client;
mongoc_collection_t *collection;
ngx_int_t rc;
bool inserted;
//Dealing with bson here.
char* response;
mongoc_uri_t* uri;
bson_error_t error;
bson_t* post_data;
//Init our client and connect
mongoc_init();
uri = ngx_http_create_mongo_conn_uri(mgcf->mongo_server_addr.data, mgcf->mongo_port.data, mgcf->mongo_user.data, mgcf->mongo_passw.data);
client = mongoc_client_new_from_uri(uri); //Connect to db. Let's assume for the moment all actions require connection.
if(!client) {
ngx_memzero(&cv, sizeof(ngx_http_complex_value_t));
cv.value.data = "{ \"ok\" : false, \"reason\" : \"Could not authenticate.\" }";
cv.value.len = strlen(cv.value.data);
ngx_http_send_response(r, NGX_HTTP_UNAUTHORIZED, &application_type, &cv);
}
collection = mongoc_client_get_collection(client, mgcf->mongo_database.data, mgcf->mongo_collection.data); //Get our requested collection
if(!collection) {
ngx_memzero(&cv, sizeof(ngx_http_complex_value_t));
cv.value.data = "{ \"ok\" : false, \"reason\" : \"Unable to get collection.\" }";
cv.value.len = strlen(cv.value.data);
ngx_http_send_response(r, NGX_HTTP_INTERNAL_SERVER_ERROR, &application_type, &cv);
}
//Fetch posted data and digest it, then attempt to upsert the data.
post_data = bson_new_from_json(r->request_body->bufs->buf->pos, strlen(r->request_body->bufs->buf->pos), &error);
//If not able to create bson from our json
if(!post_data) {
ngx_memzero(&cv, sizeof(ngx_http_complex_value_t));
cv.value.data = "{ \"ok\" : false, \"reason\" : \"Parsing error. Unable to create bson from json.\" }";
cv.value.len = strlen(cv.value.data);
ngx_http_send_response(r, NGX_HTTP_INTERNAL_SERVER_ERROR, &application_type, &cv);
}
inserted = mongoc_collection_update(collection, MONGOC_UPDATE_UPSERT, post_data, post_data, NULL, &error);
//Unable to do upsert.
if(!inserted) {
ngx_memzero(&cv, sizeof(ngx_http_complex_value_t));
cv.value.data = "{ \"ok\" : false, \"reason\" : \"Unable to upsert document.\" }";
cv.value.len = strlen(cv.value.data);
ngx_http_send_response(r, NGX_HTTP_INTERNAL_SERVER_ERROR, &application_type, &cv);
}
//Sweet. Updated correctly.
ngx_memzero(&cv, sizeof(ngx_http_complex_value_t));
cv.value.data = "{ \"ok\" true, \"reason\" : \"Insert/Update successful.\" }";
cv.value.len = strlen(cv.value.data);
ngx_http_send_response(r, NGX_HTTP_OK, &application_type, &cv);
//Is this necessary?
ngx_http_finalize_request(r, NGX_OK);
}
static char*
ngx_http_mg_unreadable_fields(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
{
ngx_http_mg_conf_t *mgcf = conf;
ngx_str_t *value;
value = cf->args->elts;
//Start our bson doc, this will be used for our unreadable fields.
bson_init(&mgcf->unreadable_fields);
//Per the docs, explicitly exclude certain fields by zeroin'g their value out.
int i = 1;
for(i; i < cf->args->nelts; i++) {
if(!bson_append_int32(&mgcf->unreadable_fields, value[i].data, value[i].len, 0)){
return NGX_CONF_ERROR;
}
}
return NGX_CONF_OK;
}
static mongoc_uri_t*
ngx_http_create_mongo_conn_uri(char* addr, char* port, char* username, char* pass)
{
char* uri_string;
if(strlen(username) > 0 && strlen(pass) > 0) {
uri_string = bson_strdup_printf("mongodb://%s:%s@%s:%s",
username,
pass,
addr,
port);
} else if(strlen(username) > 0) {
uri_string = bson_strdup_printf("mongodb://%s@%s:%s",
username,
addr,
port);
} else {
uri_string = bson_strdup_printf("mongodb://%s:%s",
addr,
port);
}
return mongoc_uri_new(uri_string);
}
static char *
ngx_http_mg(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
{
ngx_http_core_loc_conf_t *clcf;
clcf = ngx_http_conf_get_module_loc_conf(cf, ngx_http_core_module);
clcf->handler = ngx_http_mg_handler;
return NGX_CONF_OK;
}
static void *
ngx_http_mg_create_loc_conf(ngx_conf_t *cf)
{
ngx_http_mg_conf_t *conf;
conf = (ngx_http_mg_conf_t *)ngx_pcalloc(cf->pool, sizeof(ngx_http_mg_conf_t));
if (conf == NULL) {
return NULL;
}
/*
* set by ngx_pcalloc():
* conf->mongo_server_addr = { 0, NULL }
* conf->mongo_database = { 0, NULL }
* conf->mongo_collection = { 0, NULL }
* conf->mongo_user = { 0, NULL }
* conf->mongo_passw = { 0, NULL }
*/
return conf;
}
static char *
ngx_http_mg_merge_loc_conf(ngx_conf_t *cf, void * parent, void * child)
{
ngx_http_mg_conf_t *prev = (ngx_http_mg_conf_t *) parent;
ngx_http_mg_conf_t *conf = (ngx_http_mg_conf_t *) child;
//Merge up all our defaults.
ngx_conf_merge_str_value(conf->mongo_server_addr, prev->mongo_server_addr, "127.0.0.1");
ngx_conf_merge_str_value(conf->mongo_port, prev->mongo_port, "27017");
ngx_conf_merge_str_value(conf->mongo_database, prev->mongo_database, "test");
ngx_conf_merge_str_value(conf->mongo_collection, prev->mongo_collection, "test");
ngx_conf_merge_str_value(conf->mongo_user, prev->mongo_user, "");
ngx_conf_merge_str_value(conf->mongo_passw, prev->mongo_passw, "");
return NGX_CONF_OK;
}