Coverage Report

Created: 2023-01-10 06:17

/src/fluent-bit/src/flb_uri.c
Line
Count
Source (jump to first uncovered line)
1
/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2
3
/*  Fluent Bit
4
 *  ==========
5
 *  Copyright (C) 2015-2022 The Fluent Bit Authors
6
 *
7
 *  Licensed under the Apache License, Version 2.0 (the "License");
8
 *  you may not use this file except in compliance with the License.
9
 *  You may obtain a copy of the License at
10
 *
11
 *      http://www.apache.org/licenses/LICENSE-2.0
12
 *
13
 *  Unless required by applicable law or agreed to in writing, software
14
 *  distributed under the License is distributed on an "AS IS" BASIS,
15
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16
 *  See the License for the specific language governing permissions and
17
 *  limitations under the License.
18
 */
19
20
#include <stdlib.h>
21
#include <monkey/mk_core.h>
22
23
#include <fluent-bit/flb_info.h>
24
#include <fluent-bit/flb_mem.h>
25
#include <fluent-bit/flb_str.h>
26
#include <fluent-bit/flb_uri.h>
27
#include <fluent-bit/flb_utils.h>
28
29
/*
30
 * Perform URI encoding for the given string. Always returns a new sds buffer,
31
 * if it fails it returns NULL.
32
 */
33
flb_sds_t flb_uri_encode(const char *uri, size_t len)
34
0
{
35
0
    int i;
36
0
    flb_sds_t buf = NULL;
37
0
    flb_sds_t tmp = NULL;
38
39
0
    buf = flb_sds_create_size(len * 2);
40
0
    if (!buf) {
41
0
        flb_error("[uri] cannot allocate buffer for URI encoding");
42
0
        return NULL;
43
0
    }
44
45
0
    for (i = 0; i < len; i++) {
46
0
        if (flb_uri_to_encode(uri[i]) == FLB_TRUE) {
47
0
            tmp = flb_sds_printf(&buf, "%%%02X", (unsigned char) *(uri + i));
48
0
            if (!tmp) {
49
0
                flb_error("[uri] error formatting special character");
50
0
                flb_sds_destroy(buf);
51
0
                return NULL;
52
0
            }
53
0
            continue;
54
0
        }
55
56
        /* Direct assignment, just copy the character */
57
0
        if (buf) {
58
0
            tmp = flb_sds_cat(buf, uri + i, 1);
59
0
            if (!tmp) {
60
0
                flb_error("[uri] error composing outgoing buffer");
61
0
                flb_sds_destroy(buf);
62
0
                return NULL;
63
0
            }
64
0
            buf = tmp;
65
0
        }
66
0
    }
67
68
0
    return buf;
69
0
}
70
71
/* Retrieve a given field based on it expected position in the URI */
72
struct flb_uri_field *flb_uri_get(struct flb_uri *uri, int pos)
73
0
{
74
0
    if (pos >= FLB_URI_MAX || pos > uri->count) {
75
0
        flb_trace("[uri] requested position > FLB_URI_MAX");
76
0
        return NULL;
77
0
    }
78
79
0
    return &uri->map[pos];
80
0
}
81
82
/*
83
 * Given a 'URI' string, split the strings separated by a slash and create a
84
 * context.
85
 */
86
struct flb_uri *flb_uri_create(const char *full_uri)
87
0
{
88
0
    int end;
89
0
    unsigned int len;
90
0
    unsigned int val_len;
91
0
    unsigned int i = 0;
92
0
    char *val;
93
0
    size_t uri_size;
94
0
    void *p;
95
0
    struct flb_uri_field *field;
96
0
    struct flb_uri *uri;
97
98
    /* Set the required memory space */
99
0
    uri_size  = sizeof(struct flb_uri);
100
0
    uri_size += (sizeof(struct flb_uri_field) * FLB_URI_MAX);
101
102
0
    p  = flb_calloc(1, uri_size);
103
0
    if (!p) {
104
0
        perror("malloc");
105
0
        return NULL;
106
0
    }
107
108
    /* Link the 'map' */
109
0
    uri = p;
110
0
    p = ((char *) p) + sizeof(struct flb_uri);
111
0
    uri->map = p;
112
113
    /* Initialize fields list */
114
0
    mk_list_init(&uri->list);
115
0
    uri->count = 0;
116
117
0
    len = strlen(full_uri);
118
0
    while (i < len && uri->count < FLB_URI_MAX) {
119
0
        end = mk_string_char_search(full_uri + i, '/', len - i);
120
121
0
        if (end >= 0 && end + i < len) {
122
0
            end += i;
123
124
0
            if (i == (unsigned int) end) {
125
0
                i++;
126
0
                continue;
127
0
            }
128
129
0
            val = mk_string_copy_substr(full_uri, i, end);
130
0
            val_len = end - i;
131
0
        }
132
0
        else {
133
0
            val = mk_string_copy_substr(full_uri, i, len);
134
0
            val_len = len - i;
135
0
            end = len;
136
137
0
        }
138
139
        /* Alloc node */
140
0
        field = &uri->map[uri->count];
141
0
        field->value         = flb_strdup(val);
142
0
        field->length        = val_len;
143
0
        mk_list_add(&field->_head, &uri->list);
144
0
        i = end + 1;
145
0
        uri->count++;
146
147
0
        mk_mem_free(val);
148
0
    }
149
150
0
    uri->full = flb_strdup(full_uri);
151
0
    return uri;
152
0
}
153
154
/* Destroy an URI context and it resources associated */
155
void flb_uri_destroy(struct flb_uri *uri)
156
0
{
157
0
    struct mk_list *tmp;
158
0
    struct mk_list *head;
159
0
    struct flb_uri_field *field;
160
161
0
    mk_list_foreach_safe(head, tmp, &uri->list) {
162
0
        field = mk_list_entry(head, struct flb_uri_field, _head);
163
0
        mk_list_del(&field->_head);
164
0
        flb_free(field->value);
165
0
    }
166
167
0
    flb_free(uri->full);
168
0
    flb_free(uri);
169
0
}
170
171
void flb_uri_dump(struct flb_uri *uri)
172
0
{
173
0
    int i;
174
0
    struct flb_uri_field *f;
175
176
0
    for (i = 0; i < uri->count; i++) {
177
0
        f = &uri->map[i];
178
0
        printf("[%i] length=%lu value='%s'\n",
179
0
               i, f->length, f->value);
180
0
    }
181
0
}