-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig_store.c
298 lines (253 loc) · 8.8 KB
/
config_store.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
/* Copyright 2013 Justin Erenkrantz and Greg Stein
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <apr_strings.h>
#include "serf.h"
#include "serf_bucket_util.h"
#include "serf_private.h"
/* Use a linked list to store the config values, as we'll only store a couple
of values per context. */
struct serf__config_hdr_t {
apr_pool_t *pool;
struct config_entry_t *first;
};
typedef struct config_entry_t {
apr_uint32_t key;
void *value;
struct config_entry_t *next;
} config_entry_t;
static serf__config_hdr_t *create_config_hdr(apr_pool_t *pool)
{
serf__config_hdr_t *hdr = apr_pcalloc(pool, sizeof(serf__config_hdr_t));
hdr->pool = pool;
return hdr;
}
static apr_status_t
add_or_replace_entry(serf__config_hdr_t *hdr, serf_config_key_t key, void *value)
{
config_entry_t *iter = hdr->first;
config_entry_t *last = iter;
int found = FALSE;
/* Find the entry with the matching key. If it exists, replace its value. */
while (iter != NULL) {
if (iter->key == key) {
found = TRUE;
break;
}
last = iter;
iter = iter->next;
}
if (found) {
iter->key = key;
iter->value = value;
} else {
/* Not found, create a new entry and append it to the list. */
config_entry_t *entry = apr_palloc(hdr->pool, sizeof(config_entry_t));
entry->key = key;
entry->value = value;
entry->next = NULL;
if (last)
last->next = entry;
else
hdr->first = entry;
}
return APR_SUCCESS;
}
/*** Config Store ***/
apr_status_t serf__config_store_init(serf_context_t *ctx)
{
apr_pool_t *pool = ctx->pool;
ctx->config_store.pool = pool;
ctx->config_store.global_per_context = create_config_hdr(pool);
ctx->config_store.global_per_host = apr_hash_make(pool);
ctx->config_store.global_per_conn = apr_hash_make(pool);
return APR_ENOTIMPL;
}
/* Defines the key to use for per host settings */
static const char * host_key_for_conn(serf_connection_t *conn,
apr_pool_t *pool)
{
/* SCHEME://HOSTNAME:PORT, e.g. http://localhost:12345 */
return conn->host_url;
}
/* Defines the key to use for per connection settings */
static const char * conn_key_for_conn(serf_connection_t *conn,
apr_pool_t *pool)
{
/* Key needs to be unique per connection, so stringify its pointer value */
return apr_psprintf(pool, "%x", (unsigned int)conn);
}
/* TODO: when will this be released? Related config to a specific lifecyle:
connection or context */
apr_status_t serf__config_store_get_config(serf_context_t *ctx,
serf_connection_t *conn,
serf_config_t **config,
apr_pool_t *out_pool)
{
serf__config_store_t *config_store = &ctx->config_store;
serf_config_t *cfg = apr_pcalloc(out_pool, sizeof(serf_config_t));
cfg->ctx_pool = ctx->pool;
cfg->per_context = config_store->global_per_context;
if (conn) {
const char *host_key, *conn_key;
serf__config_hdr_t *per_conn, *per_host;
apr_pool_t *tmp_pool;
apr_status_t status;
cfg->conn_pool = conn->pool;
if ((status = apr_pool_create(&tmp_pool, out_pool)) != APR_SUCCESS)
return status;
/* Find the config values for this connection, create empty structure
if needed */
conn_key = conn_key_for_conn(conn, tmp_pool);
per_conn = apr_hash_get(config_store->global_per_conn, conn_key,
APR_HASH_KEY_STRING);
if (!per_conn) {
per_conn = create_config_hdr(conn->pool);
apr_hash_set(config_store->global_per_conn,
apr_pstrdup(conn->pool, conn_key),
APR_HASH_KEY_STRING, per_conn);
}
cfg->per_conn = per_conn;
/* Find the config values for this host, create empty structure
if needed */
host_key = host_key_for_conn(conn, tmp_pool);
per_host = apr_hash_get(config_store->global_per_host,
host_key,
APR_HASH_KEY_STRING);
if (!per_host) {
per_host = create_config_hdr(config_store->pool);
apr_hash_set(config_store->global_per_host,
apr_pstrdup(config_store->pool, host_key),
APR_HASH_KEY_STRING, per_host);
}
cfg->per_host = per_host;
apr_pool_destroy(tmp_pool);
}
*config = cfg;
return APR_SUCCESS;
}
apr_status_t
serf__config_store_remove_connection(serf__config_store_t config_store,
serf_connection_t *conn)
{
return APR_ENOTIMPL;
}
apr_status_t
serf__config_store_remove_host(serf__config_store_t config_store,
const char *hostname_port)
{
return APR_ENOTIMPL;
}
/*** Config ***/
apr_status_t serf_config_set_string(serf_config_t *config,
serf_config_key_t key,
const char *value)
{
/* Cast away const is ok here, the callers should always use
serf_config_get_string for this key. */
return serf_config_set_object(config, key, (void *)value);
}
apr_status_t serf_config_set_stringc(serf_config_t *config,
serf_config_key_t key,
const char *value)
{
const char *cvalue;
apr_pool_t *pool;
if (key & SERF_CONFIG_PER_CONTEXT ||
key & SERF_CONFIG_PER_HOST) {
pool = config->ctx_pool;
} else {
pool = config->conn_pool;
}
cvalue = apr_pstrdup(pool, value);
return serf_config_set_string(config, key, cvalue);
}
apr_status_t serf_config_set_stringf(serf_config_t *config,
serf_config_key_t key,
const char *fmt, ...)
{
apr_pool_t *pool;
va_list argp;
char *cvalue;
if (key & SERF_CONFIG_PER_CONTEXT)
pool = config->ctx_pool;
else if (key & SERF_CONFIG_PER_HOST)
pool = config->ctx_pool;
else
pool = config->conn_pool;
va_start(argp, fmt);
cvalue = apr_pvsprintf(pool, fmt, argp);
va_end(argp);
return serf_config_set_string(config, key, cvalue);
}
apr_status_t serf_config_set_object(serf_config_t *config,
serf_config_key_t key,
void *value)
{
serf__config_hdr_t *target;
/* Set the value in the hash table of the selected category */
if (key & SERF_CONFIG_PER_CONTEXT) {
target = config->per_context;
}
else if (key & SERF_CONFIG_PER_HOST) {
target = config->per_host;
}
else {
target = config->per_conn;
}
if (!target) {
/* Config object doesn't manage keys in this category */
return APR_EINVAL;
}
return add_or_replace_entry(target, key, value);
}
apr_status_t serf_config_get_string(serf_config_t *config,
serf_config_key_t key,
const char **value)
{
return serf_config_get_object(config, key, (void**)value);
}
apr_status_t serf_config_get_object(serf_config_t *config,
serf_config_key_t key,
void **value)
{
serf__config_hdr_t *target;
if (key & SERF_CONFIG_PER_CONTEXT)
target = config->per_context;
else if (key & SERF_CONFIG_PER_HOST)
target = config->per_host;
else
target = config->per_conn;
*value = NULL;
if (target) {
config_entry_t *iter = target->first;
/* Find the matching key and return its value */
while (iter != NULL) {
if (iter->key == key) {
*value = iter->value;
return APR_SUCCESS;
}
iter = iter->next;
}
return APR_SUCCESS;
} else {
/* Config object doesn't manage keys in this category */
return APR_EINVAL;
}
}
apr_status_t serf_config_remove_value(serf_config_t *config,
serf_config_key_t key)
{
return serf_config_set_object(config, key, NULL);
}