src/server/pblock.cpp

changeset 14
b8bf95b39952
parent 13
1fdbf4170ef4
child 15
cff9c4101dd7
equal deleted inserted replaced
13:1fdbf4170ef4 14:b8bf95b39952
1 /*
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3 *
4 * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
5 *
6 * THE BSD LICENSE
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions are met:
10 *
11 * Redistributions of source code must retain the above copyright notice, this
12 * list of conditions and the following disclaimer.
13 * Redistributions in binary form must reproduce the above copyright notice,
14 * this list of conditions and the following disclaimer in the documentation
15 * and/or other materials provided with the distribution.
16 *
17 * Neither the name of the nor the names of its contributors may be
18 * used to endorse or promote products derived from this software without
19 * specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
25 * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
26 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
27 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
28 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
29 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
30 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
31 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 */
33
34 /*
35 * pblock.c: Handles Parameter Blocks
36 *
37 * See pblock.h for public documentation.
38 *
39 * Rob McCool
40 *
41 * This code uses property lists to implement pblocks.
42 */
43
44
45 #include <limits.h>
46 #include "pblock.h"
47 #include "plist_pvt.h"
48 #include "plist.h"
49 #include "LinkedList.hh"
50 #include "util.h" /* util_itoa */
51 #include "pool.h"
52 #include "systhr.h"
53
54 #define MALLOC_POOL_HANDLE (thread_malloc_key != -1 ? (pool_handle_t *)systhread_getdata(thread_malloc_key) : getThreadMallocPool())
55
56 static int thread_malloc_key = -1;
57 static int _pblock_str2pblock(const char* str, pblock* pb, PRBool lowerCase);
58
59 static pool_handle_t *getThreadMallocPool()
60 {
61 pool_handle_t *thread_malloc_pool = 0;
62
63 thread_malloc_key = getThreadMallocKey();
64 if (thread_malloc_key != -1) {
65 thread_malloc_pool = (pool_handle_t *)systhread_getdata(thread_malloc_key);
66 }
67
68 return thread_malloc_pool;
69 }
70
71 /* ------------------------------- HashList ------------------------------- */
72
73 template <class Name, class Value>
74 class HashList {
75 public:
76 HashList(int mask)
77 : _mask(mask),
78 _list(new CList<Value>[mask + 1])
79 { }
80
81 void Insert(Name name, Value *value)
82 {
83 _list[name & _mask].Append(value);
84 }
85
86 const CList<Value>& Find(Name name)
87 {
88 return _list[name & _mask];
89 }
90
91 private:
92 CList<Value> *_list;
93 unsigned int _mask;
94 };
95
96 /* ---------------------- pb_key static initializers ---------------------- */
97
98 /*
99 * pb_key
100 *
101 * Contains a precomputed hash value for a specific pblock variable name.
102 */
103 typedef struct pb_key pb_key;
104 struct pb_key {
105 const char *name;
106 int namelen;
107 unsigned int hashval;
108 int sizendx;
109 int hashndx;
110 };
111
112 static HashList<unsigned int, pb_key> _hashKeys(0x7f);
113 CList<pb_key> _listKeys;
114
115 static const pb_key *const _create_key(const char *name, int sizendx = 0)
116 {
117 /* Create a the new pb_key */
118 pb_key *key = (pb_key*)malloc(sizeof(pb_key));
119 key->name = STRDUP(name);
120 key->namelen = strlen(name);
121 key->hashval = PListHash(name);
122 key->sizendx = sizendx;
123 key->hashndx = key->hashval % PLSIZENDX(sizendx);
124
125 /* Group pb_keys by hashval for later retrieval */
126 _hashKeys.Insert(key->hashval, key);
127
128 /* Keep a list of all the registered keys */
129 _listKeys.Append(key);
130
131 return key;
132 }
133
134 const pb_key *const pb_key_accept = _create_key("accept");
135 const pb_key *const pb_key_accept_charset = _create_key("accept-charset");
136 const pb_key *const pb_key_accept_encoding = _create_key("accept-encoding");
137 const pb_key *const pb_key_accept_language = _create_key("accept-language");
138 const pb_key *const pb_key_accept_ranges = _create_key("accept-ranges");
139 const pb_key *const pb_key_actual_route = _create_key("actual-route");
140 const pb_key *const pb_key_age = _create_key("age");
141 const pb_key *const pb_key_always_allow_chunked = _create_key("always-allow-chunked");
142 const pb_key *const pb_key_always_use_keep_alive = _create_key("always-use-keep-alive");
143 const pb_key *const pb_key_auth_cert = _create_key("auth-cert");
144 const pb_key *const pb_key_auth_expiring = _create_key("auth-expiring");
145 const pb_key *const pb_key_auth_group = _create_key("auth-group");
146 const pb_key *const pb_key_auth_type = _create_key("auth-type");
147 const pb_key *const pb_key_auth_user = _create_key("auth-user");
148 const pb_key *const pb_key_authorization = _create_key("authorization");
149 const pb_key *const pb_key_browser = _create_key("browser");
150 const pb_key *const pb_key_c2p_cl = _create_key("c2p-cl");
151 const pb_key *const pb_key_c2p_hl = _create_key("c2p-hl");
152 const pb_key *const pb_key_cache_info = _create_key("cache-info");
153 const pb_key *const pb_key_charset = _create_key("charset");
154 const pb_key *const pb_key_check_http_server = _create_key("check-http-server");
155 const pb_key *const pb_key_ChunkedRequestBufferSize = _create_key("ChunkedRequestBufferSize");
156 const pb_key *const pb_key_ChunkedRequestTimeout = _create_key("ChunkedRequestTimeout");
157 const pb_key *const pb_key_cipher = _create_key("cipher");
158 const pb_key *const pb_key_clf_request = _create_key("clf-request");
159 const pb_key *const pb_key_cli_status = _create_key("cli-status");
160 const pb_key *const pb_key_client_cert_nickname = _create_key("client-cert-nickname");
161 const pb_key *const pb_key_client_ip = _create_key("client-ip");
162 const pb_key *const pb_key_close = _create_key("close");
163 const pb_key *const pb_key_connect_timeout = _create_key("connect-timeout");
164 const pb_key *const pb_key_connection = _create_key("connection");
165 const pb_key *const pb_key_cont = _create_key("cont");
166 const pb_key *const pb_key_content_encoding = _create_key("content-encoding");
167 const pb_key *const pb_key_content_language = _create_key("content-language");
168 const pb_key *const pb_key_content_length = _create_key("content-length");
169 const pb_key *const pb_key_content_location = _create_key("content-location");
170 const pb_key *const pb_key_content_md5 = _create_key("content-md5");
171 const pb_key *const pb_key_content_range = _create_key("content-range");
172 const pb_key *const pb_key_content_type = _create_key("content-type");
173 const pb_key *const pb_key_cookie = _create_key("cookie");
174 const pb_key *const pb_key_date = _create_key("date");
175 const pb_key *const pb_key_DATE_GMT = _create_key("DATE_GMT");
176 const pb_key *const pb_key_DATE_LOCAL = _create_key("DATE_LOCAL");
177 const pb_key *const pb_key_dir = _create_key("dir");
178 const pb_key *const pb_key_Directive = _create_key("Directive");
179 const pb_key *const pb_key_dns = _create_key("dns");
180 const pb_key *const pb_key_DOCUMENT_NAME = _create_key("DOCUMENT_NAME");
181 const pb_key *const pb_key_DOCUMENT_URI = _create_key("DOCUMENT_URI");
182 const pb_key *const pb_key_domain = _create_key("domain");
183 const pb_key *const pb_key_enc = _create_key("enc");
184 const pb_key *const pb_key_engine = _create_key("engine");
185 const pb_key *const pb_key_error_action = _create_key("error-action");
186 const pb_key *const pb_key_error_desc = _create_key("error-desc");
187 const pb_key *const pb_key_error_fn = _create_key("error-fn");
188 const pb_key *const pb_key_escape = _create_key("escape");
189 const pb_key *const pb_key_escaped = _create_key("escaped");
190 const pb_key *const pb_key_etag = _create_key("etag");
191 const pb_key *const pb_key_expect = _create_key("expect");
192 const pb_key *const pb_key_expires = _create_key("expires");
193 const pb_key *const pb_key_expr = _create_key("expr");
194 const pb_key *const pb_key_filter = _create_key("filter");
195 const pb_key *const pb_key_find_pathinfo_forward = _create_key("find-pathinfo-forward");
196 const pb_key *const pb_key_flushTimer = _create_key("flushTimer");
197 const pb_key *const pb_key_fn = _create_key("fn");
198 const pb_key *const pb_key_from = _create_key("from");
199 const pb_key *const pb_key_full_headers = _create_key("full-headers");
200 const pb_key *const pb_key_hdr = _create_key("hdr");
201 const pb_key *const pb_key_host = _create_key("host");
202 const pb_key *const pb_key_hostname = _create_key("hostname");
203 const pb_key *const pb_key_if_match = _create_key("if-match");
204 const pb_key *const pb_key_if_modified_since = _create_key("if-modified-since");
205 const pb_key *const pb_key_if_none_match = _create_key("if-none-match");
206 const pb_key *const pb_key_if_range = _create_key("if-range");
207 const pb_key *const pb_key_if_unmodified_since = _create_key("if-unmodified-since");
208 const pb_key *const pb_key_ip = _create_key("ip");
209 const pb_key *const pb_key_iponly = _create_key("iponly");
210 const pb_key *const pb_key_issuer_dn = _create_key("issuer_dn");
211 const pb_key *const pb_key_jroute = _create_key("jroute");
212 const pb_key *const pb_key_keep_alive = _create_key("keep-alive");
213 const pb_key *const pb_key_keep_alive_timeout = _create_key("keep-alive-timeout");
214 const pb_key *const pb_key_keysize = _create_key("keysize");
215 const pb_key *const pb_key_lang = _create_key("lang");
216 const pb_key *const pb_key_LAST_MODIFIED = _create_key("LAST_MODIFIED");
217 const pb_key *const pb_key_last_modified = _create_key("last-modified");
218 const pb_key *const pb_key_level = _create_key("level");
219 const pb_key *const pb_key_location = _create_key("location");
220 const pb_key *const pb_key_lock_owner = _create_key("lock-owner");
221 const pb_key *const pb_key_magnus_charset = _create_key("magnus-charset");
222 const pb_key *const pb_key_magnus_internal = _create_key("magnus-internal");
223 const pb_key *const pb_key_magnus_internal_dav_src = _create_key("magnus-internal/dav-src");
224 const pb_key *const pb_key_magnus_internal_default_acls_only = _create_key("magnus-internal/default-acls-only");
225 const pb_key *const pb_key_magnus_internal_error_j2ee = _create_key("magnus-internal/error-j2ee");
226 const pb_key *const pb_key_magnus_internal_j2ee_nsapi = _create_key("magnus-internal/j2ee-nsapi");
227 const pb_key *const pb_key_magnus_internal_preserve_srvhdrs = _create_key("magnus-internal/preserve-srvhdrs-after-req-restart");
228 const pb_key *const pb_key_magnus_internal_set_request_status = _create_key("magnus-internal/set-request-status");
229 const pb_key *const pb_key_magnus_internal_set_response_status = _create_key("magnus-internal/set-response-status");
230 const pb_key *const pb_key_magnus_internal_webapp_errordesc = _create_key("magnus-internal/webapp-errordesc");
231 const pb_key *const pb_key_matched_browser = _create_key("matched-browser");
232 const pb_key *const pb_key_max_age = _create_key("max-age");
233 const pb_key *const pb_key_max_forwards = _create_key("max-forwards");
234 const pb_key *const pb_key_message = _create_key("message");
235 const pb_key *const pb_key_method = _create_key("method");
236 const pb_key *const pb_key_name = _create_key("name");
237 const pb_key *const pb_key_nocache = _create_key("nocache");
238 const pb_key *const pb_key_nostat = _create_key("nostat");
239 const pb_key *const pb_key_ntrans_base = _create_key("ntrans-base");
240 const pb_key *const pb_key_offline_origin_addr = _create_key("offline-origin-addr");
241 const pb_key *const pb_key_offline_proxy_addr = _create_key("offline-proxy-addr");
242 const pb_key *const pb_key_origin_addr = _create_key("origin-addr");
243 const pb_key *const pb_key_p2c_cl = _create_key("p2c-cl");
244 const pb_key *const pb_key_p2c_hl = _create_key("p2c-hl");
245 const pb_key *const pb_key_p2r_cl = _create_key("p2r-cl");
246 const pb_key *const pb_key_p2r_hl = _create_key("p2r-hl");
247 const pb_key *const pb_key_parse_timeout = _create_key("parse-timeout");
248 const pb_key *const pb_key_password = _create_key("password");
249 const pb_key *const pb_key_path = _create_key("path");
250 const pb_key *const pb_key_PATH_INFO = _create_key("PATH_INFO");
251 const pb_key *const pb_key_path_info = _create_key("path-info");
252 const pb_key *const pb_key_pblock = _create_key("pblock");
253 const pb_key *const pb_key_poll_interval = _create_key("poll-interval");
254 const pb_key *const pb_key_port = _create_key("port");
255 const pb_key *const pb_key_ppath = _create_key("ppath");
256 const pb_key *const pb_key_pragma = _create_key("pragma");
257 const pb_key *const pb_key_process_request_body = _create_key("process-request-body");
258 const pb_key *const pb_key_process_response_body = _create_key("process-response-body");
259 const pb_key *const pb_key_protocol = _create_key("protocol");
260 const pb_key *const pb_key_proxy_addr = _create_key("proxy-addr");
261 const pb_key *const pb_key_proxy_agent = _create_key("proxy-agent");
262 const pb_key *const pb_key_proxy_auth_cert = _create_key("proxy-auth-cert");
263 const pb_key *const pb_key_proxy_authorization = _create_key("proxy-authorization");
264 const pb_key *const pb_key_proxy_cipher = _create_key("proxy-cipher");
265 const pb_key *const pb_key_proxy_issuer_dn = _create_key("proxy-issuer-dn");
266 const pb_key *const pb_key_proxy_jroute = _create_key("proxy-jroute");
267 const pb_key *const pb_key_proxy_keysize = _create_key("proxy-keysize");
268 const pb_key *const pb_key_proxy_ping = _create_key("proxy-ping");
269 const pb_key *const pb_key_proxy_request = _create_key("proxy-request");
270 const pb_key *const pb_key_proxy_secret_keysize = _create_key("proxy-secret-keysize");
271 const pb_key *const pb_key_proxy_ssl_id = _create_key("proxy-ssl-id");
272 const pb_key *const pb_key_proxy_user_dn = _create_key("proxy-user-dn");
273 const pb_key *const pb_key_query = _create_key("query");
274 const pb_key *const pb_key_QUERY_STRING = _create_key("QUERY_STRING");
275 const pb_key *const pb_key_QUERY_STRING_UNESCAPED = _create_key("QUERY_STRING_UNESCAPED");
276 const pb_key *const pb_key_r2p_cl = _create_key("r2p-cl");
277 const pb_key *const pb_key_r2p_hl = _create_key("r2p-hl");
278 const pb_key *const pb_key_range = _create_key("range");
279 const pb_key *const pb_key_referer = _create_key("referer");
280 const pb_key *const pb_key_reformat_request_headers = _create_key("reformat-request-headers");
281 const pb_key *const pb_key_remote_status = _create_key("remote-status");
282 const pb_key *const pb_key_request_jroute = _create_key("request-jroute");
283 const pb_key *const pb_key_required_rights = _create_key("required-rights");
284 const pb_key *const pb_key_retries = _create_key("retries");
285 const pb_key *const pb_key_rewrite_content_location = _create_key("rewrite-content-location");
286 const pb_key *const pb_key_rewrite_host = _create_key("rewrite-host");
287 const pb_key *const pb_key_rewrite_location = _create_key("rewrite-location");
288 const pb_key *const pb_key_rewrite_set_cookie = _create_key("rewrite-set-cookie");
289 const pb_key *const pb_key_root = _create_key("root");
290 const pb_key *const pb_key_route = _create_key("route");
291 const pb_key *const pb_key_route_cookie = _create_key("route-cookie");
292 const pb_key *const pb_key_route_hdr = _create_key("route-hdr");
293 const pb_key *const pb_key_route_offline = _create_key("route-offline");
294 const pb_key *const pb_key_script_name = _create_key("script-name");
295 const pb_key *const pb_key_secret_keysize = _create_key("secret-keysize");
296 const pb_key *const pb_key_secure = _create_key("secure");
297 const pb_key *const pb_key_server = _create_key("server");
298 const pb_key *const pb_key_set_cookie = _create_key("set-cookie");
299 const pb_key *const pb_key_socks_addr = _create_key("socks_addr");
300 const pb_key *const pb_key_ssl_id = _create_key("ssl-id");
301 const pb_key *const pb_key_ssl_unclean_shutdown = _create_key("ssl-unclean-shutdown");
302 const pb_key *const pb_key_status = _create_key("status");
303 const pb_key *const pb_key_sticky_cookie = _create_key("sticky-cookie");
304 const pb_key *const pb_key_sticky_param = _create_key("sticky-param");
305 const pb_key *const pb_key_suppress_request_headers = _create_key("suppress-request-headers");
306 const pb_key *const pb_key_svr_status = _create_key("svr-status");
307 const pb_key *const pb_key_timeout = _create_key("timeout");
308 const pb_key *const pb_key_to = _create_key("to");
309 const pb_key *const pb_key_transfer_encoding = _create_key("transfer-encoding");
310 const pb_key *const pb_key_transmit_timeout = _create_key("transmit-timeout");
311 const pb_key *const pb_key_tunnel_non_http_response = _create_key("tunnel-non-http-response");
312 const pb_key *const pb_key_type = _create_key("type");
313 const pb_key *const pb_key_upstream_jroute = _create_key("upstream-jroute");
314 const pb_key *const pb_key_uri = _create_key("uri");
315 const pb_key *const pb_key_url = _create_key("url");
316 const pb_key *const pb_key_url_prefix = _create_key("url-prefix");
317 const pb_key *const pb_key_UseOutputStreamSize = _create_key("UseOutputStreamSize");
318 const pb_key *const pb_key_user = _create_key("user");
319 const pb_key *const pb_key_user_agent = _create_key("user-agent");
320 const pb_key *const pb_key_user_dn = _create_key("user_dn");
321 const pb_key *const pb_key_validate_server_cert = _create_key("validate-server-cert");
322 const pb_key *const pb_key_value = _create_key("value");
323 const pb_key *const pb_key_vary = _create_key("vary");
324 const pb_key *const pb_key_via = _create_key("via");
325 const pb_key *const pb_key_warning = _create_key("warning");
326
327
328 /* ------------------------------ _find_key ------------------------------- */
329
330 static inline const pb_key *_find_key(const char *name, unsigned int hashval)
331 {
332 /* Check to see if name corresponds to a pb_key */
333 CListConstIterator<pb_key> iter(&_hashKeys.Find(hashval));
334 const pb_key *key;
335 while (key = ++iter) {
336 if (key->hashval == hashval && !strcmp(key->name, name))
337 return key;
338 }
339 return NULL;
340 }
341
342
343 /* --------------------------- _get_hash_index ---------------------------- */
344
345 static inline int _get_hash_index(const PListStruct_t *pl, const pb_key *key)
346 {
347 /* Get the hash index from the key. Requires a symbol table. */
348 int i;
349 if (key->sizendx == pl->pl_symtab->pt_sizendx)
350 i = key->hashndx;
351 else
352 i = key->hashval % PLSIZENDX(pl->pl_symtab->pt_sizendx);
353 return i;
354 }
355
356
357 /* ---------------------------- _param_create ----------------------------- */
358
359 static inline pb_param *_param_create(pool_handle_t *pool_handle, const char *name, int namelen, const char *value, int valuelen)
360 {
361 PLValueStruct_t *ret;
362
363 ret = (PLValueStruct_t *)pool_malloc(pool_handle, sizeof(PLValueStruct_t));
364
365 ret->pv_pbentry.param = &ret->pv_pbparam;
366 ret->pv_pbentry.next = 0;
367 ret->pv_next = 0;
368 ret->pv_type = 0;
369 ret->pv_mempool = pool_handle;
370
371 if (name || namelen) {
372 ret->pv_name = (char*)pool_malloc(pool_handle, namelen + 1);
373 if (name) {
374 memcpy(ret->pv_name, name, namelen);
375 ret->pv_name[namelen] = '\0';
376 } else {
377 ret->pv_name[0] = '\0';
378 }
379 } else {
380 ret->pv_name = 0;
381 }
382
383 if (value || valuelen) {
384 ret->pv_value = (char*)pool_malloc(pool_handle, valuelen + 1);
385 if (value) {
386 memcpy(ret->pv_value, value, valuelen);
387 ret->pv_value[valuelen] = '\0';
388 } else {
389 ret->pv_value[0] = '\0';
390 }
391 } else {
392 ret->pv_value = 0;
393 }
394
395 return &ret->pv_pbparam;
396 }
397
398
399 /* ----------------------- pblock_key_param_create ----------------------- */
400
401 NSAPI_PUBLIC pb_param *pblock_key_param_create(pblock *pb, const pb_key *key, const char *value, int valuelen)
402 {
403 /*
404 * Allocate a PLValueStruct_t from the property list's memory pool.
405 */
406 PListStruct_t *pl = PBTOPL(pb);
407 return _param_create(pl->pl_mempool, key->name, key->namelen, value, valuelen);
408 }
409
410
411 /* ------------------------- pblock_param_create -------------------------- */
412
413 NSAPI_PUBLIC pb_param *pblock_param_create(pblock *pb, const char *name, const char *value)
414 {
415 /*
416 * Allocate a PLValueStruct_t from the property list's memory pool.
417 */
418 PListStruct_t *pl = PBTOPL(pb);
419 return _param_create(pl->pl_mempool, name, name ? strlen(name) : 0, value, value ? strlen(value) : 0);
420 }
421
422
423 /* ----------------------------- param_create ----------------------------- */
424
425 NSAPI_PUBLIC pb_param *param_create(const char *name, const char *value)
426 {
427 /*
428 * Allocate a PLValueStruct_t containing the pb_param that will
429 * be returned. Normally PLValueStruct_ts are allocated from the
430 * memory pool associated with a property list, but we don't have
431 * that here, so we just use the thread's pool and indicate we were
432 * allocated from a specific pool.
433 */
434 return _param_create(system_pool(), name, name ? strlen(name) : 0, value, value ? strlen(value) : 0);
435 }
436
437
438 /* ------------------------------ param_free ------------------------------ */
439
440 NSAPI_PUBLIC int param_free(pb_param *pp)
441 {
442 if (pp) {
443 PLValueStruct_t *pv = PATOPV(pp);
444
445 /* Don't bother if the pblock was allocated from a pool */
446 if (!pv->pv_mempool) {
447 pool_free(pv->pv_mempool, pv->pv_name);
448 pool_free(pv->pv_mempool, pv->pv_value);
449 pool_free(pv->pv_mempool, pv);
450 }
451
452 return 1;
453 }
454
455 return 0;
456 }
457
458
459 /* -------------------------- pblock_create_pool -------------------------- */
460
461 NSAPI_PUBLIC pblock *pblock_create_pool(pool_handle_t *pool_handle, int n)
462 {
463 /* Create a property list with n property indices */
464 PListStruct_t *plist = (PListStruct_t *)PListCreate(pool_handle, n, 0, 0);
465 if (!plist)
466 return NULL;
467
468 plist->pl_resvpi = 0;
469
470 return &plist->pl_pb;
471 }
472
473
474 /* ----------------------------- pblock_pool ------------------------------ */
475
476 NSAPI_PUBLIC pool_handle_t *pblock_pool(pblock *pb)
477 {
478 PListStruct_t *pl = PBTOPL(pb);
479 return pl->pl_mempool;
480 }
481
482
483 /* ---------------------------- pblock_create ----------------------------- */
484
485 NSAPI_PUBLIC pblock *pblock_create(int n)
486 {
487 return pblock_create_pool(MALLOC_POOL_HANDLE, n);
488 }
489
490
491 /* ----------------------------- pblock_free ------------------------------ */
492
493 NSAPI_PUBLIC void pblock_free(pblock *pb)
494 {
495 PListStruct_t *pl = PBTOPL(pb);
496 PLValueStruct_t **ppval;
497 PLValueStruct_t *pv;
498 int i;
499
500 if (!pb) {
501 return;
502 }
503
504 /* If the pools are enabled, this routine has no effect anyway, so
505 * just return.
506 */
507 if (pl->pl_mempool || pool_enabled()) {
508 return;
509 }
510
511 /* Free the property name symbol table if any */
512 if (pl->pl_symtab) {
513 pool_free(pl->pl_mempool, (void *)(pl->pl_symtab));
514 }
515
516 ppval = (PLValueStruct_t **)(pl->pl_ppval);
517
518 /* Loop over the initialized property indices */
519 for (i = 0; i < pl->pl_initpi; ++i) {
520
521 /* Got a property here? */
522 pv = ppval[i];
523 if (pv) {
524
525 param_free(&pv->pv_pbparam);
526 }
527 }
528
529 /* Free the array of pointers to property values */
530 pool_free(pl->pl_mempool, (void *)ppval);
531
532 /* Free the property list head */
533 pool_free(pl->pl_mempool, (void *)pl);
534 }
535
536
537 /* ------------------------------ pblock_key ------------------------------ */
538
539 NSAPI_PUBLIC const pb_key *pblock_key(const char *name)
540 {
541 if (!name)
542 return NULL;
543
544 return _find_key(name, PListHash(name));
545 }
546
547
548 /* --------------------------- pblock_kpinsert ---------------------------- */
549
550 NSAPI_PUBLIC void pblock_kpinsert(const pb_key *key, pb_param *pp, pblock *pb)
551 {
552 PListStruct_t *pl = PBTOPL(pb);
553 PLValueStruct_t *pv = PATOPV(pp);
554
555 PR_ASSERT(pv->pv_mempool == pl->pl_mempool);
556
557 /* Check to see if the name corresponds to a pb_key */
558 unsigned int hashval;
559 if (!key) {
560 hashval = PListHash(pv->pv_name);
561 key = _find_key(pv->pv_name, hashval);
562 }
563
564 /* Find property index */
565 int pindex = PListGetFreeIndex(pl);
566 if (pindex < 1) {
567 /* Error - invalid property index */
568 printf("Error - invalid property index\n");
569 return;
570 }
571
572 /* Allocate/grow the symbol table as needed */
573 PLSymbolTable_t *pt = PListSymbolTable(pl);
574 if (!pt) {
575 printf("!pt\n");
576 return;
577 }
578
579 /* Add PLValueStruct_t to the property list */
580 PLValueStruct_t **ppval = (PLValueStruct_t **)(pl->pl_ppval);
581 pv->pv_pbkey = key;
582 pv->pv_pi = pindex;
583 ppval[pv->pv_pi - 1] = pv;
584
585 /* Add name to symbol table */
586 int i = key ? _get_hash_index(pl, key) : (hashval % PLSIZENDX(pt->pt_sizendx));
587 pv->pv_next = pt->pt_hash[i];
588 pt->pt_hash[i] = pv;
589 pt->pt_nsyms++;
590
591 PR_ASSERT(param_key(pp) == key);
592 }
593
594
595 /* ---------------------------- pblock_pinsert ---------------------------- */
596
597 NSAPI_PUBLIC void pblock_pinsert(pb_param *pp, pblock *pb)
598 {
599 pblock_kpinsert(NULL, pp, pb);
600 }
601
602
603 /* --------------------------- pblock_nvinsert ---------------------------- */
604
605 NSAPI_PUBLIC pb_param *pblock_nvinsert(const char *name, const char *value, pblock *pb)
606 {
607 pb_param *pp = pblock_param_create(pb, name, value);
608 if (pp)
609 pblock_kpinsert(NULL, pp, pb);
610 return pp;
611 }
612
613
614 /* --------------------------- pblock_kvinsert ---------------------------- */
615
616 NSAPI_PUBLIC pb_param *pblock_kvinsert(const pb_key *key, const char *value, int valuelen, pblock *pb)
617 {
618 pb_param *pp = pblock_key_param_create(pb, key, value, valuelen);
619 if (pp)
620 pblock_kpinsert(key, pp, pb);
621 return pp;
622 }
623
624
625 /* --------------------------- pblock_nninsert ---------------------------- */
626
627 NSAPI_PUBLIC pb_param *pblock_nninsert(const char *name, int value, pblock *pb)
628 {
629 char num[UTIL_ITOA_SIZE];
630
631 util_itoa(value, num);
632 return pblock_nvinsert(name, num, pb);
633 }
634
635
636 /* --------------------------- pblock_kninsert ---------------------------- */
637
638 NSAPI_PUBLIC pb_param *pblock_kninsert(const pb_key *key, int value, pblock *pb)
639 {
640 pb_param *pp = pblock_key_param_create(pb, key, NULL, UTIL_ITOA_SIZE);
641 if (pp) {
642 util_itoa(value, pp->value);
643 pblock_kpinsert(key, pp, pb);
644 }
645 return pp;
646 }
647
648
649 /* --------------------------- pblock_kllinsert --------------------------- */
650
651 NSAPI_PUBLIC pb_param *pblock_kllinsert(const pb_key *key, PRInt64 value, pblock *pb)
652 {
653 pb_param *pp = pblock_key_param_create(pb, key, NULL, UTIL_I64TOA_SIZE);
654 if (pp) {
655 util_i64toa(value, pp->value);
656 pblock_kpinsert(key, pp, pb);
657 }
658 return pp;
659 }
660
661
662 /* ---------------------------pblock_nvlinsert ---------------------------- */
663
664 NSAPI_PUBLIC pb_param *pblock_nvlinsert(const char *name, int namelen, const char *value, int valuelen, pblock *pb)
665 {
666 PListStruct_t *pl = PBTOPL(pb);
667
668 pb_param *pp = _param_create(pl->pl_mempool, name, namelen, value, valuelen);
669
670 if(pp) {
671 pblock_kpinsert(NULL, pp, pb);
672 }
673
674 return pp;
675 }
676
677
678 /* ---------------------------- pblock_findkey ---------------------------- */
679
680 NSAPI_PUBLIC pb_param *pblock_findkey(const pb_key *key, const pblock *pb)
681 {
682 PListStruct_t *pl = PBTOPL(pb);
683
684 /* Lookup key by examining symbol table */
685 if (pl->pl_symtab) {
686 int i = _get_hash_index(pl, key);
687 PLValueStruct_t *pv;
688
689 /* Search hash collision list for matching name */
690 for (pv = pl->pl_symtab->pt_hash[i]; pv; pv = pv->pv_next) {
691 if (pv->pv_pbkey == key)
692 return &pv->pv_pbparam;
693 }
694 }
695
696 return NULL;
697 }
698
699
700 /* -------------------------- pblock_findkeyval --------------------------- */
701
702 NSAPI_PUBLIC char *pblock_findkeyval(const pb_key *key, const pblock *pb)
703 {
704 pb_param *pp = pblock_findkey(key, pb);
705 return pp ? pp->value : NULL;
706 }
707
708
709 /* ---------------------------- pblock_findval ---------------------------- */
710
711 NSAPI_PUBLIC char *pblock_findval(const char *name, const pblock *pb)
712 {
713 void *pvalue = 0;
714
715 (void)PListFindValue((PList_t)(PBTOPL(pb)), name, &pvalue, 0);
716
717 return (char *)pvalue;
718 }
719
720
721 /* ------------------------------ pblock_fr ------------------------------ */
722
723 NSAPI_PUBLIC pb_param *pblock_fr(const char *name, pblock *pb, int remove)
724 {
725 PListStruct_t *pl = PBTOPL(pb);
726 PLValueStruct_t **ppval;
727 PLValueStruct_t **pvp;
728 PLValueStruct_t *pv = NULL;
729 int pindex;
730 int i;
731
732 if (pl->pl_symtab) {
733
734 /* Compute hash index of specified property name */
735 i = PListHashName(pl->pl_symtab, name);
736
737 /* Search hash collision list for matching name */
738 for (pvp = &pl->pl_symtab->pt_hash[i];
739 (pv = *pvp); pvp = &(*pvp)->pv_next) {
740
741 if (!strcmp(name, pv->pv_name)) {
742
743 if (remove) {
744 /* Remove PLValueStruct_t from symbol table */
745 *pvp = pv->pv_next;
746 pl->pl_symtab->pt_nsyms--;
747
748 /* Remove it from pl_ppval too */
749 ppval = (PLValueStruct_t **)(pl->pl_ppval);
750 pindex = pv->pv_pi;
751 ppval[pindex - 1] = 0;
752 }
753 break;
754 }
755 }
756 }
757
758 return (pv) ? &pv->pv_pbparam : NULL;
759 }
760
761
762 /* --------------------------- pblock_removekey --------------------------- */
763
764 NSAPI_PUBLIC pb_param *pblock_removekey(const pb_key *key, pblock *pb)
765 {
766 PListStruct_t *pl = PBTOPL(pb);
767 PLValueStruct_t **ppval;
768 PLValueStruct_t **pvp;
769 PLValueStruct_t *pv = NULL;
770 int pindex;
771 int i;
772
773 if (pl->pl_symtab) {
774 /* Lookup hash index for specified property key */
775 i = _get_hash_index(pl, key);
776
777 /* Search hash collision list for matching key */
778 for (pvp = &pl->pl_symtab->pt_hash[i]; (pv = *pvp); pvp = &pv->pv_next) {
779 /* If this value has the requested key... */
780 if (pv->pv_pbkey == key) {
781 /* Remove PLValueStruct_t from symbol table */
782 *pvp = pv->pv_next;
783 pl->pl_symtab->pt_nsyms--;
784
785 /* Remove it from pl_ppval too */
786 ppval = (PLValueStruct_t **)(pl->pl_ppval);
787 pindex = pv->pv_pi;
788 ppval[pindex - 1] = 0;
789
790 break;
791 }
792 }
793 }
794
795 return (pv) ? &pv->pv_pbparam : NULL;
796 }
797
798
799 /* -------------------------- pblock_removeone --------------------------- */
800
801 NSAPI_PUBLIC pb_param *pblock_removeone(pblock *pb)
802 {
803 PListStruct_t *pl = PBTOPL(pb);
804
805 if (pl && pl->pl_symtab) {
806 /* Search hash buckets */
807 for (int i = 0; i < PLSIZENDX(pl->pl_symtab->pt_sizendx); i++) {
808 /* Search hash collision list */
809 PLValueStruct_t *pv = pl->pl_symtab->pt_hash[i];
810 if (pv) {
811 /* Remove PLValueStruct_t from symbol table */
812 pl->pl_symtab->pt_hash[i] = pv->pv_next;
813 pl->pl_symtab->pt_nsyms--;
814
815 /* Remove it from pl_ppval too */
816 PLValueStruct_t **ppval = (PLValueStruct_t**)pl->pl_ppval;
817 ppval[pv->pv_pi - 1] = 0;
818
819 return &pv->pv_pbparam;
820 }
821 }
822 }
823
824 return NULL;
825 }
826
827
828 /* -------------------------- pblock_str2pblock --------------------------- */
829
830
831 int _verify_pbstr(const char *str)
832 {
833 const char *cp;
834 const char *scp;
835 int np;
836 int state;
837 int quote;
838
839 for(cp = str, np = 0, state = 0; *cp; ) {
840 switch (state) {
841 case 0: /* skipping leading spaces */
842
843 while (*cp && isspace(*cp)) ++cp;
844 if (*cp == '=') {
845 return -1;
846 }
847 if (*cp) state = 1;
848 break;
849
850 case 1: /* scanning parameter name */
851
852 scp = cp;
853 while (*cp && (*cp != '=') && !isspace(*cp)) ++cp;
854 if (*cp == '=') ++cp;
855 else cp = scp;
856 state = 2;
857 break;
858
859 case 2: /* scanning parameter value */
860 quote = 0;
861 if (*cp == '\"') {
862 quote = 1;
863 ++cp;
864 }
865 for (;;) {
866 if (*cp == '\\') {
867 ++cp;
868 if (*cp == 0) {
869 return -1;
870 }
871 }
872 else if (*cp == '\"') {
873 if (!quote) {
874 return -1;
875 }
876 ++np;
877 ++cp;
878 quote = 0;
879 state = 0;
880 break;
881 }
882 else if (!quote && (!*cp || isspace(*cp))) {
883 ++np;
884 if (*cp) ++cp;
885 state = 0;
886 break;
887 }
888 else if (*cp == 0) {
889 return -1;
890 }
891 ++cp;
892 }
893 if (quote) {
894 return -1;
895 }
896 break;
897 }
898 }
899
900 return (state == 0) ? np : -1;
901 }
902
903 NSAPI_PUBLIC int
904 INTpblock_str2pblock_lowercasename(const char *str, pblock *pb)
905 {
906 return _pblock_str2pblock(str, pb, PR_TRUE);
907 }
908
909 NSAPI_PUBLIC int pblock_str2pblock(const char *str, pblock *pb)
910 {
911 return _pblock_str2pblock(str, pb, PR_FALSE);
912 }
913
914 int
915 _pblock_str2pblock(const char* str, pblock* pb, PRBool lowerCase)
916 {
917 char *cpy;
918 char *cp;
919 char *dp;
920 char *pname;
921 char *pvalue;
922 int np;
923 int quote;
924 int state;
925 char numbuf[UTIL_ITOA_SIZE];
926
927 if((np = _verify_pbstr(str)) < 1)
928 return -1;
929
930 while (*str && isspace(*str)) ++str;
931
932 cpy = STRDUP(str);
933
934 for (np = 0, cp = cpy, state = 0; *cp; ) {
935 switch (state) {
936
937 case 0: /* skipping leading spaces */
938
939 while (*cp && isspace(*cp)) ++cp;
940 if (*cp) state = 1;
941 break;
942
943 case 1: /* scanning parameter name */
944
945 pname = cp;
946 while (*cp && (*cp != '=') && !isspace(*cp)) ++cp;
947 if (*cp == '=') {
948 *cp++ = 0;
949 }
950 else {
951 cp = pname;
952 pname = numbuf;
953 util_itoa(np+1, numbuf);
954 }
955 state = 2;
956 break;
957
958 case 2: /* scanning parameter value */
959 quote = 0;
960 if (*cp == '\"') {
961 quote = 1;
962 ++cp;
963 }
964 for (pvalue = cp, dp = cp; ; ++cp, ++dp) {
965 if (*cp == '\\') {
966 ++cp;
967 }
968 else if (*cp == '\"') {
969 ++np;
970 ++cp;
971 *dp = 0;
972 quote = 0;
973 state = 0;
974 break;
975 }
976 else if (!quote && ((*cp == 0) || isspace(*cp))) {
977 ++np;
978 if (*cp != 0) {
979 ++cp;
980 }
981 *dp = 0;
982 state = 0;
983 break;
984 }
985 if (cp != dp) *dp = *cp;
986 }
987 if (lowerCase == PR_TRUE) {
988 for (char* p = pname; *p; p++) {
989 *p = tolower(*p);
990 }
991 }
992 pblock_nvinsert(pname, pvalue, pb);
993 break;
994 }
995 }
996
997 FREE(cpy);
998
999 return np;
1000 }
1001
1002
1003 /* -------------------------- pblock_pblock2str --------------------------- */
1004
1005
1006 NSAPI_PUBLIC char *pblock_pblock2str(const pblock *pb, char *str)
1007 {
1008 register char *s = str, *t, *u;
1009 PListStruct_t *pl = PBTOPL(pb);
1010 PLValueStruct_t **ppval;
1011 PLValueStruct_t *pv;
1012 int i;
1013 int sl;
1014 int xlen;
1015
1016 ppval = (PLValueStruct_t **)(pl->pl_ppval);
1017
1018 /* Loop over the initialized property indices */
1019 for (i = 0, xlen = 0; i < pl->pl_initpi; ++i) {
1020
1021 /* Got a property here? */
1022 pv = ppval[i];
1023 if (pv && pv->pv_name) {
1024
1025 int ln = strlen(pv->pv_name);
1026 int lv = strlen((char *)(pv->pv_value));
1027
1028 /* Check for " or \ because we'll have to escape them */
1029 for (t = (char *)(pv->pv_value); *t; ++t) {
1030 if ((*t == '\"') || (*t == '\\')) ++lv;
1031 }
1032
1033 /* 4: two quotes, =, and a null */
1034 xlen += (ln + lv + 4);
1035 }
1036 }
1037
1038 /* Allocate string to hold parameter settings, or increase size */
1039 if (!s) {
1040 s = (char *)MALLOC(xlen);
1041 s[0] = '\0';
1042 t = &s[0];
1043 sl = xlen;
1044 }
1045 else {
1046 sl = strlen(s);
1047 t = &s[sl];
1048 sl += xlen;
1049 s = (char *)REALLOC(s, sl);
1050 }
1051
1052 /* Loop over the initialized property indices */
1053 for (i = 0; i < pl->pl_initpi; ++i) {
1054
1055 /* Got a property here? */
1056 pv = ppval[i];
1057 if (pv && pv->pv_name) {
1058
1059 if (t != s) *t++ = ' ';
1060
1061 for (u = pv->pv_name; *u; ) *t++ = *u++;
1062
1063 *t++ = '=';
1064 *t++ = '\"';
1065
1066 for (u = (char *)(pv->pv_value); *u; ) {
1067 if ((*u == '\\') || (*u == '\"')) *t++ = '\\';
1068 *t++ = *u++;
1069 }
1070
1071 *t++ = '\"';
1072 *t = '\0';
1073 }
1074 }
1075
1076 return s;
1077 }
1078
1079
1080 /* ----------------------------- pblock_copy ------------------------------ */
1081
1082
1083 NSAPI_PUBLIC int pblock_copy(const pblock *src, pblock *dst)
1084 {
1085 PListStruct_t *pl = PBTOPL(src);
1086 PLValueStruct_t **ppval;
1087 PLValueStruct_t *pv;
1088 int rv = 0;
1089 int i;
1090
1091 ppval = (PLValueStruct_t **)(pl->pl_ppval);
1092
1093 for (i = 0; i < pl->pl_initpi; ++i) {
1094 pv = ppval[i];
1095 if (pv) {
1096 if (pv->pv_pbkey) {
1097 if (pv->pv_pbkey != pb_key_magnus_internal) {
1098 if (!pblock_kvinsert(pv->pv_pbkey, (char *)(pv->pv_value), strlen(pv->pv_value), dst))
1099 rv = -1;
1100 }
1101 } else {
1102 if (!pblock_nvinsert(pv->pv_name, (char *)(pv->pv_value), dst))
1103 rv = -1;
1104 }
1105 }
1106 }
1107
1108 return rv;
1109 }
1110
1111 /* ---------------------------- pblock_dup -------------------------------- */
1112
1113 NSAPI_PUBLIC pblock *pblock_dup(const pblock *src)
1114 {
1115 pblock *dst;
1116
1117 if (!src)
1118 return NULL;
1119
1120 if ( (dst = pblock_create(src->hsize)) )
1121 pblock_copy(src, dst);
1122
1123 return dst;
1124 }
1125
1126
1127 /* ---------------------------- pblock_pb2env ----------------------------- */
1128
1129 /*
1130 NSAPI_PUBLIC char **pblock_pb2env(const pblock *pb, char **env)
1131 {
1132 PListStruct_t *pl = PBTOPL(pb);
1133 PLValueStruct_t **ppval;
1134 PLValueStruct_t *pv;
1135 int i;
1136 int nval;
1137 int pos;
1138
1139 /* Find out how many there are. */
1140 /*
1141 ppval = (PLValueStruct_t **)(pl->pl_ppval);
1142
1143 for (i = 0, nval = 0; i < pl->pl_initpi; ++i) {
1144 if (ppval[i]) ++nval;
1145 }
1146
1147 env = util_env_create(env, nval, &pos);
1148
1149 for (i = 0; i < pl->pl_initpi; ++i) {
1150 pv = ppval[i];
1151 if (pv) {
1152 env[pos++] = util_env_str(pv->pv_name, (char *)(pv->pv_value));
1153 }
1154 }
1155 env[pos] = NULL;
1156
1157 return env;
1158 }
1159 */
1160
1161 /* ---------------------------- pblock_replace ---------------------------- */
1162
1163 NSAPI_PUBLIC char * pblock_replace(const char *name,
1164 char * new_value, pblock *pb)
1165 {
1166 PListStruct_t *pl = PBTOPL(pb);
1167
1168 /* Replace an existing value */
1169 pb_param *pp = pblock_find(name, pb);
1170 if (!pp)
1171 return NULL;
1172 pool_free(pl->pl_mempool, pp->value);
1173 pp->value = new_value;
1174
1175 return new_value;
1176 }
1177
1178
1179 /* --------------------------- pblock_nvreplace --------------------------- */
1180
1181 NSAPI_PUBLIC void pblock_nvreplace (const char *name, const char *value, pblock *pb)
1182 {
1183 PListStruct_t *pl = PBTOPL(pb);
1184
1185 /* Replace an existing value or insert a new value */
1186 pb_param *pp = pblock_find(name, pb);
1187 if (pp) {
1188 pool_free(pl->pl_mempool, pp->value);
1189 pp->value = pool_strdup(pl->pl_mempool, value);
1190 } else {
1191 pblock_nvinsert(name, value, pb);
1192 }
1193 }
1194
1195
1196 /* --------------------------- pblock_kvreplace --------------------------- */
1197
1198 NSAPI_PUBLIC void pblock_kvreplace(const pb_key *key, const char *value, int valuelen, pblock *pb)
1199 {
1200 PListStruct_t *pl = PBTOPL(pb);
1201
1202 /* Replace an existing value or insert a new value */
1203 pb_param *pp = pblock_findkey(key, pb);
1204 if (pp) {
1205 pool_free(pl->pl_mempool, pp->value);
1206 pp->value = (char*)pool_malloc(pl->pl_mempool, valuelen + 1);
1207 memcpy(pp->value, value, valuelen + 1);
1208 } else {
1209 pblock_kvinsert(key, value, valuelen, pb);
1210 }
1211 }

mercurial