summaryrefslogtreecommitdiff
path: root/rds-csv-nginx-module-0.09/src/ngx_http_rds_csv_util.c
blob: 8ca04141af3f818ed359bb4a50ad808d570d2576 (plain)
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
/*
 * Copyright (C) Yichun Zhang (agentzh)
 */


#ifndef DDEBUG
#define DDEBUG 0
#endif
#include "ddebug.h"


#include "resty_dbd_stream.h"
#include "ngx_http_rds_csv_util.h"


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)
{
    ngx_uint_t                   n;

    if (dst == NULL) {
        *need_quotes = 0;

        /* find the number of characters to be escaped */

        n = 0;

        while (size) {
            switch (*src) {
                case '"':
                    n++;
                    /* fallthrough */

                case '\r':
                case '\n':
                    *need_quotes = 1;
                    break;

                default:
                    if (*src == field_sep) {
                        *need_quotes = 1;
                    }
                    break;
            }

            src++;
            size--;
        }

        return (uintptr_t) n;
    }

    while (size) {
        if (*src == '"') {
            *dst++ = '"';
            *dst++ = '"';
            src++;

        } else {
            *dst++ = *src++;
        }

        size--;
    }

    return (uintptr_t) dst;
}


ngx_int_t
ngx_http_rds_csv_test_content_type(ngx_http_request_t *r)
{
    ngx_str_t           *type;

    type = &r->headers_out.content_type;
    if (type->len != rds_content_type_len
        || ngx_strncmp(type->data, rds_content_type, rds_content_type_len)
           != 0)
    {
        return NGX_DECLINED;
    }

    return NGX_OK;
}


void
ngx_http_rds_csv_discard_bufs(ngx_pool_t *pool, ngx_chain_t *in)
{
    ngx_chain_t         *cl;

    for (cl = in; cl; cl = cl->next) {
#if 0
        if (cl->buf->temporary
            && ngx_buf_size(cl->buf) > 0)
        {
            ngx_pfree(pool, cl->buf->start);
        }
#endif

        cl->buf->pos = cl->buf->last;
    }
}