1 
2 /*
3  * Copyright (C) Yichun Zhang (agentzh)
4  */
5 
6 
7 #ifndef DDEBUG
8 #define DDEBUG 0
9 #endif
10 #include "ddebug.h"
11 
12 
13 #include "resty_dbd_stream.h"
14 #include "ngx_http_rds_csv_util.h"
15 
16 
17 uintptr_t
ngx_http_rds_csv_escape_csv_str(u_char field_sep,u_char * dst,u_char * src,size_t size,unsigned * need_quotes)18 ngx_http_rds_csv_escape_csv_str(u_char field_sep, u_char *dst, u_char *src,
19     size_t size, unsigned *need_quotes)
20 {
21     ngx_uint_t                   n;
22 
23     if (dst == NULL) {
24         *need_quotes = 0;
25 
26         /* find the number of characters to be escaped */
27 
28         n = 0;
29 
30         while (size) {
31             switch (*src) {
32                 case '"':
33                     n++;
34                     /* fallthrough */
35 
36                 case '\r':
37                 case '\n':
38                     *need_quotes = 1;
39                     break;
40 
41                 default:
42                     if (*src == field_sep) {
43                         *need_quotes = 1;
44                     }
45                     break;
46             }
47 
48             src++;
49             size--;
50         }
51 
52         return (uintptr_t) n;
53     }
54 
55     while (size) {
56         if (*src == '"') {
57             *dst++ = '"';
58             *dst++ = '"';
59             src++;
60 
61         } else {
62             *dst++ = *src++;
63         }
64 
65         size--;
66     }
67 
68     return (uintptr_t) dst;
69 }
70 
71 
72 ngx_int_t
ngx_http_rds_csv_test_content_type(ngx_http_request_t * r)73 ngx_http_rds_csv_test_content_type(ngx_http_request_t *r)
74 {
75     ngx_str_t           *type;
76 
77     type = &r->headers_out.content_type;
78     if (type->len != rds_content_type_len
79         || ngx_strncmp(type->data, rds_content_type, rds_content_type_len)
80            != 0)
81     {
82         return NGX_DECLINED;
83     }
84 
85     return NGX_OK;
86 }
87 
88 
89 void
ngx_http_rds_csv_discard_bufs(ngx_pool_t * pool,ngx_chain_t * in)90 ngx_http_rds_csv_discard_bufs(ngx_pool_t *pool, ngx_chain_t *in)
91 {
92     ngx_chain_t         *cl;
93 
94     for (cl = in; cl; cl = cl->next) {
95 #if 0
96         if (cl->buf->temporary
97             && ngx_buf_size(cl->buf) > 0)
98         {
99             ngx_pfree(pool, cl->buf->start);
100         }
101 #endif
102 
103         cl->buf->pos = cl->buf->last;
104     }
105 }
106