-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathngx_http_rds_csv_output.c
783 lines (584 loc) · 19.5 KB
/
ngx_http_rds_csv_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
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
/*
* Copyright (C) Yichun Zhang (agentzh)
*/
#ifndef DDEBUG
#define DDEBUG 0
#endif
#include "ddebug.h"
#include "ngx_http_rds_csv_filter_module.h"
#include "ngx_http_rds_csv_output.h"
#include "ngx_http_rds_csv_util.h"
#include "resty_dbd_stream.h"
static u_char * ngx_http_rds_csv_request_mem(ngx_http_request_t *r,
ngx_http_rds_csv_ctx_t *ctx, size_t len);
static ngx_int_t ngx_http_rds_csv_get_buf(ngx_http_request_t *r,
ngx_http_rds_csv_ctx_t *ctx);
static u_char * ngx_http_rds_csv_get_postponed(ngx_http_request_t *r,
ngx_http_rds_csv_ctx_t *ctx, size_t len);
static ngx_int_t ngx_http_rds_csv_submit_mem(ngx_http_request_t *r,
ngx_http_rds_csv_ctx_t *ctx, size_t len, unsigned last_buf);
static size_t ngx_get_num_size(uint64_t i);
ngx_int_t
ngx_http_rds_csv_output_literal(ngx_http_request_t *r,
ngx_http_rds_csv_ctx_t *ctx, u_char *data, size_t len,
int last_buf)
{
u_char *pos;
pos = ngx_http_rds_csv_request_mem(r, ctx, len);
if (pos == NULL) {
return NGX_ERROR;
}
ngx_memcpy(pos, data, len);
dd("before output chain");
if (last_buf) {
ctx->seen_stream_end = 1;
if (r != r->main) {
last_buf = 0;
}
}
return ngx_http_rds_csv_submit_mem(r, ctx, len, (unsigned) last_buf);
}
ngx_int_t
ngx_http_rds_csv_output_bufs(ngx_http_request_t *r,
ngx_http_rds_csv_ctx_t *ctx)
{
ngx_int_t rc;
ngx_chain_t *cl;
dd("entered output chain");
if (ctx->seen_stream_end) {
ctx->seen_stream_end = 0;
if (ctx->avail_out) {
cl = ngx_alloc_chain_link(r->pool);
if (cl == NULL) {
return NGX_ERROR;
}
cl->buf = ctx->out_buf;
cl->next = NULL;
*ctx->last_out = cl;
ctx->last_out = &cl->next;
ctx->avail_out = 0;
}
}
dd_dump_chain_size();
for ( ;; ) {
if (ctx->out == NULL) {
/* fprintf(stderr, "\n"); */
return NGX_OK;
}
/* fprintf(stderr, "XXX Relooping..."); */
rc = ngx_http_rds_csv_next_body_filter(r, ctx->out);
if (rc == NGX_ERROR || rc >= NGX_HTTP_SPECIAL_RESPONSE) {
return rc;
}
#if defined(nginx_version) && nginx_version >= 1001004
ngx_chain_update_chains(r->pool, &ctx->free_bufs, &ctx->busy_bufs,
&ctx->out, ctx->tag);
#else
ngx_chain_update_chains(&ctx->free_bufs, &ctx->busy_bufs, &ctx->out,
ctx->tag);
#endif
ctx->last_out = &ctx->out;
}
/* impossible to reach here */
return NGX_ERROR;
}
ngx_int_t
ngx_http_rds_csv_output_header(ngx_http_request_t *r,
ngx_http_rds_csv_ctx_t *ctx, ngx_http_rds_header_t *header)
{
u_char *pos, *last;
size_t size;
uintptr_t escape;
unsigned last_buf = 0;
unsigned need_quotes;
u_char sep;
ngx_http_rds_csv_loc_conf_t *conf;
/* calculate the buffer size */
conf = ngx_http_get_module_loc_conf(r, ngx_http_rds_csv_filter_module);
if (conf->field_name_header) {
size = sizeof("errcode,errstr,insert_id,affected_rows") - 1
+ conf->row_term.len;
} else {
size = 0;
}
sep = (u_char) conf->field_sep;
size += 3 /* field seperators */ + conf->row_term.len;
size += ngx_get_num_size(header->std_errcode);
escape = ngx_http_rds_csv_escape_csv_str(sep, NULL, header->errstr.data,
header->errstr.len,
&need_quotes);
if (need_quotes) {
size += sizeof("\"\"") - 1;
}
size += header->errstr.len + escape
+ ngx_get_num_size(header->insert_id)
+ ngx_get_num_size(header->affected_rows);
/* create the buffer */
pos = ngx_http_rds_csv_request_mem(r, ctx, size);
if (pos == NULL) {
return NGX_ERROR;
}
last = pos;
/* fill up the buffer */
last = ngx_sprintf(last, "errcode%cerrstr%cinsert_id%caffected_rows%V"
"%uD%c", sep, sep, sep, &conf->row_term,
(uint32_t) header->std_errcode, sep);
if (need_quotes) {
*last++ = '"';
}
if (escape == 0) {
last = ngx_copy(last, header->errstr.data, header->errstr.len);
} else {
last = (u_char *)
ngx_http_rds_csv_escape_csv_str(sep, last,
header->errstr.data,
header->errstr.len, NULL);
}
if (need_quotes) {
*last++ = '"';
}
last = ngx_sprintf(last, "%c%uL%c%uL%V", sep, header->insert_id, sep,
header->affected_rows, &conf->row_term);
if ((size_t) (last - pos) != size) {
ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
"rds_csv: output header buffer error: %uz != %uz",
(size_t) (last - pos), size);
return NGX_ERROR;
}
if (r == r->main) {
last_buf = 1;
}
ctx->seen_stream_end = 1;
return ngx_http_rds_csv_submit_mem(r, ctx, size, last_buf);
}
ngx_int_t
ngx_http_rds_csv_output_field_names(ngx_http_request_t *r,
ngx_http_rds_csv_ctx_t *ctx)
{
ngx_uint_t i;
ngx_http_rds_column_t *col;
size_t size;
u_char *pos, *last;
uintptr_t escape = 0;
unsigned need_quotes;
u_char sep;
ngx_http_rds_csv_loc_conf_t *conf;
conf = ngx_http_get_module_loc_conf(r, ngx_http_rds_csv_filter_module);
sep = (u_char) conf->field_sep;
size = ctx->col_count - 1 /* field sep count */
+ conf->row_term.len;
for (i = 0; i < ctx->col_count; i++) {
col = &ctx->cols[i];
escape = ngx_http_rds_csv_escape_csv_str(sep, NULL, col->name.data,
col->name.len, &need_quotes);
dd("field escape: %d", (int) escape);
if (need_quotes) {
size += sizeof("\"\"") - 1;
}
size += col->name.len + escape;
}
ctx->generated_col_names = 1;
pos = ngx_http_rds_csv_request_mem(r, ctx, size);
if (pos == NULL) {
return NGX_ERROR;
}
last = pos;
for (i = 0; i < ctx->col_count; i++) {
col = &ctx->cols[i];
escape = ngx_http_rds_csv_escape_csv_str(sep, NULL, col->name.data,
col->name.len, &need_quotes);
if (need_quotes) {
*last++ = '"';
}
if (escape == 0) {
last = ngx_copy(last, col->name.data, col->name.len);
} else {
last = (u_char *)
ngx_http_rds_csv_escape_csv_str(sep, last,
col->name.data,
col->name.len, NULL);
}
if (need_quotes) {
*last++ = '"';
}
if (i != ctx->col_count - 1) {
*last++ = sep;
}
}
last = ngx_copy(last, conf->row_term.data, conf->row_term.len);
if ((size_t) (last - pos) != size) {
ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
"rds_csv: output field names buffer error: %uz != %uz",
(size_t) (last - pos), size);
return NGX_ERROR;
}
return ngx_http_rds_csv_submit_mem(r, ctx, size, 0);
}
ngx_int_t
ngx_http_rds_csv_output_field(ngx_http_request_t *r,
ngx_http_rds_csv_ctx_t *ctx, u_char *data, size_t len, int is_null)
{
u_char *pos, *last;
ngx_http_rds_column_t *col;
size_t size;
uintptr_t val_escape = 0;
unsigned need_quotes;
u_char sep;
ngx_http_rds_csv_loc_conf_t *conf;
#if DDEBUG
u_char *p;
#endif
conf = ngx_http_get_module_loc_conf(r, ngx_http_rds_csv_filter_module);
sep = (u_char) conf->field_sep;
dd("reading row %llu, col %d, len %d",
(unsigned long long) ctx->row,
(int) ctx->cur_col, (int) len);
/* calculate the buffer size */
if (ctx->cur_col == 0) {
size = 0;
} else {
size = 1 /* field sep */;
}
col = &ctx->cols[ctx->cur_col];
if (len == 0 && ctx->field_data_rest > 0) {
ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
"rds_csv: at least one octet should go with the field "
"size in one buf");
return NGX_ERROR;
}
if (is_null) {
/* SQL NULL is just empty in the CSV field */
} else if (len == 0) {
/* empty string is also empty */
} else {
switch (col->std_type & 0xc000) {
case rds_rough_col_type_float:
case rds_rough_col_type_int:
case rds_rough_col_type_bool:
size += len;
break;
default:
dd("string field found");
if (!conf->quote_string) {
dd("no need to quote string.");
size += len;
break;
}
val_escape = ngx_http_rds_csv_escape_csv_str(sep, NULL, data, len,
&need_quotes);
if (ctx->field_data_rest > 0 && !need_quotes) {
need_quotes = 1;
}
if (need_quotes) {
if (ctx->field_data_rest == 0) {
size += sizeof("\"\"") - 1;
} else {
size += sizeof("\"") - 1;
}
}
size += len + val_escape;
break;
}
}
if (ctx->field_data_rest == 0 && ctx->cur_col == ctx->col_count - 1) {
/* last column in the row */
size += conf->row_term.len;
}
/* allocate the buffer */
pos = ngx_http_rds_csv_request_mem(r, ctx, size);
if (pos == NULL) {
return NGX_ERROR;
}
last = pos;
/* fill up the buffer */
if (ctx->cur_col != 0) {
*last++ = sep;
}
if (is_null || len == 0) {
/* do nothing */
} else {
switch (col->std_type & 0xc000) {
case rds_rough_col_type_int:
case rds_rough_col_type_float:
case rds_rough_col_type_bool:
last = ngx_copy(last, data, len);
break;
default:
/* string */
if (!conf->quote_string) {
dd("no need to quote string, just copy string buffer.");
last = ngx_copy(last, data, len);
break;
}
if (need_quotes) {
*last++ = '"';
}
if (val_escape == 0) {
last = ngx_copy(last, data, len);
} else {
dd("field: string value escape non-zero: %d",
(int) val_escape);
#if DDEBUG
p = last;
#endif
last = (u_char *)
ngx_http_rds_csv_escape_csv_str(sep, last, data, len,
NULL);
dd("escaped value \"%.*s\" (len %d, escape %d, escape2 %d)",
(int) (len + val_escape),
p, (int) (len + val_escape),
(int) val_escape,
(int) ((last - p) - len));
}
if (need_quotes && ctx->field_data_rest == 0) {
*last++ = '"';
}
break;
}
}
if (ctx->field_data_rest == 0 && ctx->cur_col == ctx->col_count - 1) {
last = ngx_copy(last, conf->row_term.data, conf->row_term.len);
}
if ((size_t) (last - pos) != size) {
ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
"rds_csv: output field: buffer error (%d left)",
(int) size - (last - pos));
return NGX_ERROR;
}
return ngx_http_rds_csv_submit_mem(r, ctx, size, 0);
}
ngx_int_t
ngx_http_rds_csv_output_more_field_data(ngx_http_request_t *r,
ngx_http_rds_csv_ctx_t *ctx, u_char *data, size_t len)
{
u_char *pos, *last;
size_t size = 0;
ngx_http_rds_column_t *col;
uintptr_t escape = 0;
#if DDEBUG
u_char *p;
#endif
unsigned need_quotes;
u_char sep;
ngx_http_rds_csv_loc_conf_t *conf;
conf = ngx_http_get_module_loc_conf(r, ngx_http_rds_csv_filter_module);
sep = (u_char) conf->field_sep;
/* calculate the buffer size */
col = &ctx->cols[ctx->cur_col];
switch (col->std_type & 0xc000) {
case rds_rough_col_type_int:
case rds_rough_col_type_float:
case rds_rough_col_type_bool:
size += len;
break;
default:
/* string */
if (!conf->quote_string) {
dd("no need to quote string.");
size += len;
break;
}
escape = ngx_http_rds_csv_escape_csv_str(sep, NULL, data, len,
&need_quotes);
size = len + escape;
if (ctx->field_data_rest == 0) {
size += sizeof("\"") - 1;
}
break;
}
if (ctx->field_data_rest == 0 && ctx->cur_col == ctx->col_count - 1) {
/* last column in the row */
size += conf->row_term.len;
}
/* allocate the buffer */
pos = ngx_http_rds_csv_request_mem(r, ctx, size);
if (pos == NULL) {
return NGX_ERROR;
}
last = pos;
/* fill up the buffer */
switch (col->std_type & 0xc000) {
case rds_rough_col_type_int:
case rds_rough_col_type_float:
case rds_rough_col_type_bool:
last = ngx_copy(last, data, len);
break;
default:
/* string */
if (!conf->quote_string) {
dd("no need to quote string, just copy string buffer.");
last = ngx_copy(last, data, len);
break;
}
if (escape == 0) {
last = ngx_copy(last, data, len);
} else {
dd("more field data: string value escape non-zero: %d",
(int) escape);
#if DDEBUG
p = last;
#endif
last = (u_char *) ngx_http_rds_csv_escape_csv_str(sep, last, data,
len, NULL);
dd("escaped value \"%.*s\" (len %d, escape %d, escape2 %d)",
(int) (len + escape),
p, (int) (len + escape),
(int) escape,
(int) ((last - p) - len));
}
if (ctx->field_data_rest == 0) {
*last++ = '"';
}
break;
} /* switch */
if (ctx->field_data_rest == 0 && ctx->cur_col == ctx->col_count - 1) {
/* last column in the row */
last = ngx_copy(last, conf->row_term.data, conf->row_term.len);
}
if ((size_t) (last - pos) != size) {
ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
"rds_csv: output more field data: buffer error "
"(%d left)", (int) (size - (last - pos)));
return NGX_ERROR;
}
return ngx_http_rds_csv_submit_mem(r, ctx, size, 0);
}
static u_char *
ngx_http_rds_csv_request_mem(ngx_http_request_t *r,
ngx_http_rds_csv_ctx_t *ctx, size_t len)
{
ngx_int_t rc;
u_char *p;
rc = ngx_http_rds_csv_get_buf(r, ctx);
if (rc != NGX_OK) {
return NULL;
}
if (ctx->avail_out < len) {
p = ngx_http_rds_csv_get_postponed(r, ctx, len);
if (p == NULL) {
return NULL;
}
ctx->postponed.pos = p;
ctx->postponed.last = p + len;
return p;
}
return ctx->out_buf->last;
}
static ngx_int_t
ngx_http_rds_csv_get_buf(ngx_http_request_t *r, ngx_http_rds_csv_ctx_t *ctx)
{
ngx_http_rds_csv_loc_conf_t *conf;
dd("MEM enter");
if (ctx->avail_out) {
return NGX_OK;
}
conf = ngx_http_get_module_loc_conf(r, ngx_http_rds_csv_filter_module);
if (ctx->free_bufs) {
dd("MEM reusing temp buf from free_bufs");
ctx->out_buf = ctx->free_bufs->buf;
ctx->free_bufs = ctx->free_bufs->next;
} else {
dd("MEM creating temp buf with size: %d", (int) conf->buf_size);
ctx->out_buf = ngx_create_temp_buf(r->pool, conf->buf_size);
if (ctx->out_buf == NULL) {
return NGX_ERROR;
}
ctx->out_buf->tag = (ngx_buf_tag_t) &ngx_http_rds_csv_filter_module;
ctx->out_buf->recycled = 1;
}
ctx->avail_out = conf->buf_size;
return NGX_OK;
}
static u_char *
ngx_http_rds_csv_get_postponed(ngx_http_request_t *r,
ngx_http_rds_csv_ctx_t *ctx, size_t len)
{
u_char *p;
dd("MEM enter");
if (ctx->cached.start == NULL) {
goto alloc;
}
if ((size_t) (ctx->cached.end - ctx->cached.start) < len) {
ngx_pfree(r->pool, ctx->cached.start);
goto alloc;
}
return ctx->cached.start;
alloc:
p = ngx_palloc(r->pool, len);
if (p == NULL) {
return NULL;
}
ctx->cached.start = p;
ctx->cached.end = p + len;
return p;
}
static ngx_int_t
ngx_http_rds_csv_submit_mem(ngx_http_request_t *r,
ngx_http_rds_csv_ctx_t *ctx, size_t len, unsigned last_buf)
{
ngx_chain_t *cl;
ngx_int_t rc;
if (ctx->postponed.pos != NULL) {
dd("MEM copy postponed data over to ctx->out for len %d", (int) len);
for ( ;; ) {
len = ctx->postponed.last - ctx->postponed.pos;
if (len > ctx->avail_out) {
len = ctx->avail_out;
}
ctx->out_buf->last = ngx_copy(ctx->out_buf->last,
ctx->postponed.pos, len);
ctx->avail_out -= len;
ctx->postponed.pos += len;
if (ctx->postponed.pos == ctx->postponed.last) {
ctx->postponed.pos = NULL;
}
if (ctx->avail_out > 0) {
break;
}
dd("MEM save ctx->out_buf");
cl = ngx_alloc_chain_link(r->pool);
if (cl == NULL) {
return NGX_ERROR;
}
cl->buf = ctx->out_buf;
cl->next = NULL;
*ctx->last_out = cl;
ctx->last_out = &cl->next;
if (ctx->postponed.pos == NULL) {
ctx->out_buf->last_buf = last_buf;
break;
}
rc = ngx_http_rds_csv_get_buf(r, ctx);
if (rc != NGX_OK) {
return NGX_ERROR;
}
}
return NGX_OK;
}
dd("MEM consuming out_buf for %d", (int) len);
ctx->out_buf->last += len;
ctx->avail_out -= len;
ctx->out_buf->last_buf = last_buf;
if (ctx->avail_out == 0) {
dd("MEM save ctx->out_buf");
cl = ngx_alloc_chain_link(r->pool);
if (cl == NULL) {
return NGX_ERROR;
}
cl->buf = ctx->out_buf;
cl->next = NULL;
*ctx->last_out = cl;
ctx->last_out = &cl->next;
}
return NGX_OK;
}
static size_t
ngx_get_num_size(uint64_t i)
{
size_t n = 0;
do {
i = i / 10;
n++;
} while (i > 0);
return n;
}