forked from FRiCKLE/ngx_postgres
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ngx_postgres_output.c
648 lines (526 loc) · 17.7 KB
/
ngx_postgres_output.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
/*
* Copyright (c) 2010, FRiCKLE Piotr Sikora <[email protected]>
* Copyright (c) 2009-2010, Xiaozhe Wang <[email protected]>
* Copyright (c) 2009-2010, Yichun Zhang <[email protected]>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#ifndef DDEBUG
#define DDEBUG 0
#endif
#include "ngx_postgres_ddebug.h"
#include "ngx_postgres_module.h"
#include "ngx_postgres_output.h"
ngx_int_t
ngx_postgres_output_value(ngx_http_request_t *r, PGresult *res)
{
ngx_postgres_ctx_t *pgctx;
ngx_http_core_loc_conf_t *clcf;
ngx_chain_t *cl;
ngx_buf_t *b;
size_t size;
dd("entering");
pgctx = ngx_http_get_module_ctx(r, ngx_postgres_module);
if ((pgctx->var_rows != 1) || (pgctx->var_cols != 1)) {
clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module);
ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
"postgres: \"postgres_output value\" received %d value(s)"
" instead of expected single value in location \"%V\"",
pgctx->var_rows * pgctx->var_cols, &clcf->name);
dd("returning NGX_DONE, status NGX_HTTP_INTERNAL_SERVER_ERROR");
pgctx->status = NGX_HTTP_INTERNAL_SERVER_ERROR;
return NGX_DONE;
}
if (PQgetisnull(res, 0, 0)) {
clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module);
ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
"postgres: \"postgres_output value\" received NULL value"
" in location \"%V\"", &clcf->name);
dd("returning NGX_DONE, status NGX_HTTP_INTERNAL_SERVER_ERROR");
pgctx->status = NGX_HTTP_INTERNAL_SERVER_ERROR;
return NGX_DONE;
}
size = PQgetlength(res, 0, 0);
if (size == 0) {
clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module);
ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
"postgres: \"postgres_output value\" received empty value"
" in location \"%V\"", &clcf->name);
dd("returning NGX_DONE, status NGX_HTTP_INTERNAL_SERVER_ERROR");
pgctx->status = NGX_HTTP_INTERNAL_SERVER_ERROR;
return NGX_DONE;
}
b = ngx_create_temp_buf(r->pool, size);
if (b == NULL) {
dd("returning NGX_ERROR");
return NGX_ERROR;
}
cl = ngx_alloc_chain_link(r->pool);
if (cl == NULL) {
dd("returning NGX_ERROR");
return NGX_ERROR;
}
cl->buf = b;
b->memory = 1;
b->tag = r->upstream->output.tag;
b->last = ngx_copy(b->last, PQgetvalue(res, 0, 0), size);
if (b->last != b->end) {
dd("returning NGX_ERROR");
return NGX_ERROR;
}
cl->next = NULL;
/* set output response */
pgctx->response = cl;
dd("returning NGX_DONE");
return NGX_DONE;
}
ngx_int_t
ngx_postgres_output_text(ngx_http_request_t *r, PGresult *res)
{
ngx_postgres_ctx_t *pgctx;
ngx_chain_t *cl;
ngx_buf_t *b;
size_t size;
ngx_int_t col_count, row_count, col, row;
dd("entering");
pgctx = ngx_http_get_module_ctx(r, ngx_postgres_module);
col_count = pgctx->var_cols;
row_count = pgctx->var_rows;
/* pre-calculate total length up-front for single buffer allocation */
size = 0;
for (row = 0; row < row_count; row++) {
for (col = 0; col < col_count; col++) {
if (PQgetisnull(res, row, col)) {
size += sizeof("(null)") - 1;
} else {
size += PQgetlength(res, row, col); /* field string data */
}
}
}
size += row_count * col_count - 1; /* delimiters */
if ((row_count == 0) || (size == 0)) {
dd("returning NGX_DONE (empty result)");
return NGX_DONE;
}
b = ngx_create_temp_buf(r->pool, size);
if (b == NULL) {
dd("returning NGX_ERROR");
return NGX_ERROR;
}
cl = ngx_alloc_chain_link(r->pool);
if (cl == NULL) {
dd("returning NGX_ERROR");
return NGX_ERROR;
}
cl->buf = b;
b->memory = 1;
b->tag = r->upstream->output.tag;
/* fill data */
for (row = 0; row < row_count; row++) {
for (col = 0; col < col_count; col++) {
if (PQgetisnull(res, row, col)) {
b->last = ngx_copy(b->last, "(null)", sizeof("(null)") - 1);
} else {
size = PQgetlength(res, row, col);
if (size) {
b->last = ngx_copy(b->last, PQgetvalue(res, row, col),
size);
}
}
if ((row != row_count - 1) || (col != col_count - 1)) {
b->last = ngx_copy(b->last, "\n", 1);
}
}
}
if (b->last != b->end) {
dd("returning NGX_ERROR");
return NGX_ERROR;
}
cl->next = NULL;
/* set output response */
pgctx->response = cl;
dd("returning NGX_DONE");
return NGX_DONE;
}
ngx_int_t
ngx_postgres_output_rds(ngx_http_request_t *r, PGresult *res)
{
ngx_postgres_ctx_t *pgctx;
ngx_chain_t *first, *last;
ngx_int_t col_count, row_count, aff_count, row;
dd("entering");
pgctx = ngx_http_get_module_ctx(r, ngx_postgres_module);
col_count = pgctx->var_cols;
row_count = pgctx->var_rows;
aff_count = (pgctx->var_affected == NGX_ERROR) ? 0 : pgctx->var_affected;
/* render header */
first = last = ngx_postgres_render_rds_header(r, r->pool, res, col_count,
aff_count);
if (last == NULL) {
dd("returning NGX_ERROR");
return NGX_ERROR;
}
if (PQresultStatus(res) != PGRES_TUPLES_OK) {
goto done;
}
/* render columns */
last->next = ngx_postgres_render_rds_columns(r, r->pool, res, col_count);
if (last->next == NULL) {
dd("returning NGX_ERROR");
return NGX_ERROR;
}
last = last->next;
/* render rows */
for (row = 0; row < row_count; row++) {
last->next = ngx_postgres_render_rds_row(r, r->pool, res, col_count,
row, (row == row_count - 1));
if (last->next == NULL) {
dd("returning NGX_ERROR");
return NGX_ERROR;
}
last = last->next;
}
/* render row terminator (for empty result-set only) */
if (row == 0) {
last->next = ngx_postgres_render_rds_row_terminator(r, r->pool);
if (last->next == NULL) {
dd("returning NGX_ERROR");
return NGX_ERROR;
}
last = last->next;
}
done:
last->next = NULL;
/* set output response */
pgctx->response = first;
dd("returning NGX_DONE");
return NGX_DONE;
}
ngx_chain_t *
ngx_postgres_render_rds_header(ngx_http_request_t *r, ngx_pool_t *pool,
PGresult *res, ngx_int_t col_count, ngx_int_t aff_count)
{
ngx_chain_t *cl;
ngx_buf_t *b;
size_t size;
char *errstr;
size_t errstr_len;
dd("entering");
errstr = PQresultErrorMessage(res);
errstr_len = ngx_strlen(errstr);
size = sizeof(uint8_t) /* endian type */
+ sizeof(uint32_t) /* format version */
+ sizeof(uint8_t) /* result type */
+ sizeof(uint16_t) /* standard error code */
+ sizeof(uint16_t) /* driver-specific error code */
+ sizeof(uint16_t) /* driver-specific error string length */
+ (uint16_t) errstr_len /* driver-specific error string data */
+ sizeof(uint64_t) /* rows affected */
+ sizeof(uint64_t) /* insert id */
+ sizeof(uint16_t) /* column count */
;
b = ngx_create_temp_buf(pool, size);
if (b == NULL) {
dd("returning NULL");
return NULL;
}
cl = ngx_alloc_chain_link(pool);
if (cl == NULL) {
dd("returning NULL");
return NULL;
}
cl->buf = b;
b->memory = 1;
b->tag = r->upstream->output.tag;
/* fill data */
#if NGX_HAVE_LITTLE_ENDIAN
*b->last++ = 0;
#else
*b->last++ = 1;
#endif
*(uint32_t *) b->last = (uint32_t) resty_dbd_stream_version;
b->last += sizeof(uint32_t);
*b->last++ = 0;
*(uint16_t *) b->last = (uint16_t) 0;
b->last += sizeof(uint16_t);
*(uint16_t *) b->last = (uint16_t) PQresultStatus(res);
b->last += sizeof(uint16_t);
*(uint16_t *) b->last = (uint16_t) errstr_len;
b->last += sizeof(uint16_t);
if (errstr_len) {
b->last = ngx_copy(b->last, (u_char *) errstr, errstr_len);
}
*(uint64_t *) b->last = (uint64_t) aff_count;
b->last += sizeof(uint64_t);
*(uint64_t *) b->last = (uint64_t) PQoidValue(res);
b->last += sizeof(uint64_t);
*(uint16_t *) b->last = (uint16_t) col_count;
b->last += sizeof(uint16_t);
if (b->last != b->end) {
dd("returning NULL");
return NULL;
}
dd("returning");
return cl;
}
ngx_chain_t *
ngx_postgres_render_rds_columns(ngx_http_request_t *r, ngx_pool_t *pool,
PGresult *res, ngx_int_t col_count)
{
ngx_chain_t *cl;
ngx_buf_t *b;
size_t size;
ngx_int_t col;
Oid col_type;
char *col_name;
size_t col_name_len;
dd("entering");
/* pre-calculate total length up-front for single buffer allocation */
size = col_count
* (sizeof(uint16_t) /* standard column type */
+ sizeof(uint16_t) /* driver-specific column type */
+ sizeof(uint16_t) /* column name string length */
)
;
for (col = 0; col < col_count; col++) {
size += ngx_strlen(PQfname(res, col)); /* column name string data */
}
b = ngx_create_temp_buf(pool, size);
if (b == NULL) {
dd("returning NULL");
return NULL;
}
cl = ngx_alloc_chain_link(pool);
if (cl == NULL) {
dd("returning NULL");
return NULL;
}
cl->buf = b;
b->memory = 1;
b->tag = r->upstream->output.tag;
/* fill data */
for (col = 0; col < col_count; col++) {
col_type = PQftype(res, col);
col_name = PQfname(res, col);
col_name_len = (uint16_t) ngx_strlen(col_name);
*(uint16_t *) b->last = (uint16_t) ngx_postgres_rds_col_type(col_type);
b->last += sizeof(uint16_t);
*(uint16_t *) b->last = col_type;
b->last += sizeof(uint16_t);
*(uint16_t *) b->last = col_name_len;
b->last += sizeof(uint16_t);
b->last = ngx_copy(b->last, col_name, col_name_len);
}
if (b->last != b->end) {
dd("returning NULL");
return NULL;
}
dd("returning");
return cl;
}
ngx_chain_t *
ngx_postgres_render_rds_row(ngx_http_request_t *r, ngx_pool_t *pool,
PGresult *res, ngx_int_t col_count, ngx_int_t row, ngx_int_t last_row)
{
ngx_chain_t *cl;
ngx_buf_t *b;
size_t size;
ngx_int_t col;
dd("entering, row:%d", (int) row);
/* pre-calculate total length up-front for single buffer allocation */
size = sizeof(uint8_t) /* row number */
+ (col_count * sizeof(uint32_t)) /* field string length */
;
if (last_row) {
size += sizeof(uint8_t);
}
for (col = 0; col < col_count; col++) {
size += PQgetlength(res, row, col); /* field string data */
}
b = ngx_create_temp_buf(pool, size);
if (b == NULL) {
dd("returning NULL");
return NULL;
}
cl = ngx_alloc_chain_link(pool);
if (cl == NULL) {
dd("returning NULL");
return NULL;
}
cl->buf = b;
b->memory = 1;
b->tag = r->upstream->output.tag;
/* fill data */
*b->last++ = (uint8_t) 1; /* valid row */
for (col = 0; col < col_count; col++) {
if (PQgetisnull(res, row, col)) {
*(uint32_t *) b->last = (uint32_t) -1;
b->last += sizeof(uint32_t);
} else {
size = PQgetlength(res, row, col);
*(uint32_t *) b->last = (uint32_t) size;
b->last += sizeof(uint32_t);
if (size) {
b->last = ngx_copy(b->last, PQgetvalue(res, row, col), size);
}
}
}
if (last_row) {
*b->last++ = (uint8_t) 0; /* row terminator */
}
if (b->last != b->end) {
dd("returning NULL");
return NULL;
}
dd("returning");
return cl;
}
ngx_chain_t *
ngx_postgres_render_rds_row_terminator(ngx_http_request_t *r, ngx_pool_t *pool)
{
ngx_chain_t *cl;
ngx_buf_t *b;
dd("entering");
b = ngx_create_temp_buf(pool, sizeof(uint8_t));
if (b == NULL) {
dd("returning NULL");
return NULL;
}
cl = ngx_alloc_chain_link(pool);
if (cl == NULL) {
dd("returning NULL");
return NULL;
}
cl->buf = b;
b->memory = 1;
b->tag = r->upstream->output.tag;
/* fill data */
*b->last++ = (uint8_t) 0; /* row terminator */
if (b->last != b->end) {
dd("returning NULL");
return NULL;
}
dd("returning");
return cl;
}
ngx_int_t
ngx_postgres_output_chain(ngx_http_request_t *r, ngx_chain_t *cl)
{
ngx_http_upstream_t *u = r->upstream;
ngx_http_core_loc_conf_t *clcf;
ngx_postgres_loc_conf_t *pglcf;
ngx_postgres_ctx_t *pgctx;
ngx_int_t rc;
dd("entering");
if (!r->header_sent) {
ngx_http_clear_content_length(r);
pglcf = ngx_http_get_module_loc_conf(r, ngx_postgres_module);
pgctx = ngx_http_get_module_ctx(r, ngx_postgres_module);
r->headers_out.status = pgctx->status ? ngx_abs(pgctx->status)
: NGX_HTTP_OK;
if (pglcf->output_handler == &ngx_postgres_output_rds) {
/* RDS for output rds */
r->headers_out.content_type.data = (u_char *) rds_content_type;
r->headers_out.content_type.len = rds_content_type_len;
r->headers_out.content_type_len = rds_content_type_len;
} else if (pglcf->output_handler != NULL) {
/* default type for output value|row */
clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module);
r->headers_out.content_type = clcf->default_type;
r->headers_out.content_type_len = clcf->default_type.len;
}
r->headers_out.content_type_lowcase = NULL;
rc = ngx_http_send_header(r);
if (rc == NGX_ERROR || rc > NGX_OK || r->header_only) {
dd("returning rc:%d", (int) rc);
return rc;
}
}
if (cl == NULL) {
dd("returning NGX_DONE");
return NGX_DONE;
}
rc = ngx_http_output_filter(r, cl);
if (rc == NGX_ERROR || rc > NGX_OK) {
dd("returning rc:%d", (int) rc);
return rc;
}
#if defined(nginx_version) && (nginx_version >= 1001004)
ngx_chain_update_chains(r->pool, &u->free_bufs, &u->busy_bufs, &cl,
u->output.tag);
#else
ngx_chain_update_chains(&u->free_bufs, &u->busy_bufs, &cl, u->output.tag);
#endif
dd("returning rc:%d", (int) rc);
return rc;
}
rds_col_type_t
ngx_postgres_rds_col_type(Oid col_type)
{
switch (col_type) {
case 20: /* int8 */
return rds_col_type_bigint;
case 1560: /* bit */
return rds_col_type_bit;
case 1562: /* varbit */
return rds_col_type_bit_varying;
case 16: /* bool */
return rds_col_type_bool;
case 18: /* char */
return rds_col_type_char;
case 19: /* name */
/* FALLTROUGH */
case 25: /* text */
/* FALLTROUGH */
case 1043: /* varchar */
return rds_col_type_varchar;
case 1082: /* date */
return rds_col_type_date;
case 701: /* float8 */
return rds_col_type_double;
case 23: /* int4 */
return rds_col_type_integer;
case 1186: /* interval */
return rds_col_type_interval;
case 1700: /* numeric */
return rds_col_type_decimal;
case 700: /* float4 */
return rds_col_type_real;
case 21: /* int2 */
return rds_col_type_smallint;
case 1266: /* timetz */
return rds_col_type_time_with_time_zone;
case 1083: /* time */
return rds_col_type_time;
case 1184: /* timestamptz */
return rds_col_type_timestamp_with_time_zone;
case 1114: /* timestamp */
return rds_col_type_timestamp;
case 142: /* xml */
return rds_col_type_xml;
case 17: /* bytea */
return rds_col_type_blob;
default:
return rds_col_type_unknown;
}
}