src/server/pblock.cpp

changeset 1
3c066d52342d
child 3
137197831306
equal deleted inserted replaced
0:4c89c7683fb6 1:3c066d52342d
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 return;
569 }
570
571 /* Allocate/grow the symbol table as needed */
572 PLSymbolTable_t *pt = PListSymbolTable(pl);
573 if (!pt) {
574 return;
575 }
576
577 /* Add PLValueStruct_t to the property list */
578 PLValueStruct_t **ppval = (PLValueStruct_t **)(pl->pl_ppval);
579 pv->pv_pbkey = key;
580 pv->pv_pi = pindex;
581 ppval[pv->pv_pi - 1] = pv;
582
583 /* Add name to symbol table */
584 int i = key ? _get_hash_index(pl, key) : (hashval % PLSIZENDX(pt->pt_sizendx));
585 pv->pv_next = pt->pt_hash[i];
586 pt->pt_hash[i] = pv;
587 pt->pt_nsyms++;
588
589 PR_ASSERT(param_key(pp) == key);
590 }
591
592
593 /* ---------------------------- pblock_pinsert ---------------------------- */
594
595 NSAPI_PUBLIC void pblock_pinsert(pb_param *pp, pblock *pb)
596 {
597 pblock_kpinsert(NULL, pp, pb);
598 }
599
600
601 /* --------------------------- pblock_nvinsert ---------------------------- */
602
603 NSAPI_PUBLIC pb_param *pblock_nvinsert(const char *name, const char *value, pblock *pb)
604 {
605 pb_param *pp = pblock_param_create(pb, name, value);
606 if (pp)
607 pblock_kpinsert(NULL, pp, pb);
608 return pp;
609 }
610
611
612 /* --------------------------- pblock_kvinsert ---------------------------- */
613
614 NSAPI_PUBLIC pb_param *pblock_kvinsert(const pb_key *key, const char *value, int valuelen, pblock *pb)
615 {
616 pb_param *pp = pblock_key_param_create(pb, key, value, valuelen);
617 if (pp)
618 pblock_kpinsert(key, pp, pb);
619 return pp;
620 }
621
622
623 /* --------------------------- pblock_nninsert ---------------------------- */
624
625 NSAPI_PUBLIC pb_param *pblock_nninsert(const char *name, int value, pblock *pb)
626 {
627 char num[UTIL_ITOA_SIZE];
628
629 util_itoa(value, num);
630 return pblock_nvinsert(name, num, pb);
631 }
632
633
634 /* --------------------------- pblock_kninsert ---------------------------- */
635
636 NSAPI_PUBLIC pb_param *pblock_kninsert(const pb_key *key, int value, pblock *pb)
637 {
638 pb_param *pp = pblock_key_param_create(pb, key, NULL, UTIL_ITOA_SIZE);
639 if (pp) {
640 util_itoa(value, pp->value);
641 pblock_kpinsert(key, pp, pb);
642 }
643 return pp;
644 }
645
646
647 /* --------------------------- pblock_kllinsert --------------------------- */
648
649 NSAPI_PUBLIC pb_param *pblock_kllinsert(const pb_key *key, PRInt64 value, pblock *pb)
650 {
651 pb_param *pp = pblock_key_param_create(pb, key, NULL, UTIL_I64TOA_SIZE);
652 if (pp) {
653 util_i64toa(value, pp->value);
654 pblock_kpinsert(key, pp, pb);
655 }
656 return pp;
657 }
658
659
660 /* ---------------------------- pblock_findkey ---------------------------- */
661
662 NSAPI_PUBLIC pb_param *pblock_findkey(const pb_key *key, const pblock *pb)
663 {
664 PListStruct_t *pl = PBTOPL(pb);
665
666 /* Lookup key by examining symbol table */
667 if (pl->pl_symtab) {
668 int i = _get_hash_index(pl, key);
669 PLValueStruct_t *pv;
670
671 /* Search hash collision list for matching name */
672 for (pv = pl->pl_symtab->pt_hash[i]; pv; pv = pv->pv_next) {
673 if (pv->pv_pbkey == key)
674 return &pv->pv_pbparam;
675 }
676 }
677
678 return NULL;
679 }
680
681
682 /* -------------------------- pblock_findkeyval --------------------------- */
683
684 NSAPI_PUBLIC char *pblock_findkeyval(const pb_key *key, const pblock *pb)
685 {
686 pb_param *pp = pblock_findkey(key, pb);
687 return pp ? pp->value : NULL;
688 }
689
690
691 /* ---------------------------- pblock_findval ---------------------------- */
692
693 NSAPI_PUBLIC char *pblock_findval(const char *name, const pblock *pb)
694 {
695 void *pvalue = 0;
696
697 (void)PListFindValue((PList_t)(PBTOPL(pb)), name, &pvalue, 0);
698
699 return (char *)pvalue;
700 }
701
702
703 /* ------------------------------ pblock_fr ------------------------------ */
704
705 NSAPI_PUBLIC pb_param *pblock_fr(const char *name, pblock *pb, int remove)
706 {
707 PListStruct_t *pl = PBTOPL(pb);
708 PLValueStruct_t **ppval;
709 PLValueStruct_t **pvp;
710 PLValueStruct_t *pv = NULL;
711 int pindex;
712 int i;
713
714 if (pl->pl_symtab) {
715
716 /* Compute hash index of specified property name */
717 i = PListHashName(pl->pl_symtab, name);
718
719 /* Search hash collision list for matching name */
720 for (pvp = &pl->pl_symtab->pt_hash[i];
721 (pv = *pvp); pvp = &(*pvp)->pv_next) {
722
723 if (!strcmp(name, pv->pv_name)) {
724
725 if (remove) {
726 /* Remove PLValueStruct_t from symbol table */
727 *pvp = pv->pv_next;
728 pl->pl_symtab->pt_nsyms--;
729
730 /* Remove it from pl_ppval too */
731 ppval = (PLValueStruct_t **)(pl->pl_ppval);
732 pindex = pv->pv_pi;
733 ppval[pindex - 1] = 0;
734 }
735 break;
736 }
737 }
738 }
739
740 return (pv) ? &pv->pv_pbparam : NULL;
741 }
742
743
744 /* --------------------------- pblock_removekey --------------------------- */
745
746 NSAPI_PUBLIC pb_param *pblock_removekey(const pb_key *key, pblock *pb)
747 {
748 PListStruct_t *pl = PBTOPL(pb);
749 PLValueStruct_t **ppval;
750 PLValueStruct_t **pvp;
751 PLValueStruct_t *pv = NULL;
752 int pindex;
753 int i;
754
755 if (pl->pl_symtab) {
756 /* Lookup hash index for specified property key */
757 i = _get_hash_index(pl, key);
758
759 /* Search hash collision list for matching key */
760 for (pvp = &pl->pl_symtab->pt_hash[i]; (pv = *pvp); pvp = &pv->pv_next) {
761 /* If this value has the requested key... */
762 if (pv->pv_pbkey == key) {
763 /* Remove PLValueStruct_t from symbol table */
764 *pvp = pv->pv_next;
765 pl->pl_symtab->pt_nsyms--;
766
767 /* Remove it from pl_ppval too */
768 ppval = (PLValueStruct_t **)(pl->pl_ppval);
769 pindex = pv->pv_pi;
770 ppval[pindex - 1] = 0;
771
772 break;
773 }
774 }
775 }
776
777 return (pv) ? &pv->pv_pbparam : NULL;
778 }
779
780
781 /* -------------------------- pblock_removeone --------------------------- */
782
783 NSAPI_PUBLIC pb_param *pblock_removeone(pblock *pb)
784 {
785 PListStruct_t *pl = PBTOPL(pb);
786
787 if (pl && pl->pl_symtab) {
788 /* Search hash buckets */
789 for (int i = 0; i < PLSIZENDX(pl->pl_symtab->pt_sizendx); i++) {
790 /* Search hash collision list */
791 PLValueStruct_t *pv = pl->pl_symtab->pt_hash[i];
792 if (pv) {
793 /* Remove PLValueStruct_t from symbol table */
794 pl->pl_symtab->pt_hash[i] = pv->pv_next;
795 pl->pl_symtab->pt_nsyms--;
796
797 /* Remove it from pl_ppval too */
798 PLValueStruct_t **ppval = (PLValueStruct_t**)pl->pl_ppval;
799 ppval[pv->pv_pi - 1] = 0;
800
801 return &pv->pv_pbparam;
802 }
803 }
804 }
805
806 return NULL;
807 }
808
809
810 /* -------------------------- pblock_str2pblock --------------------------- */
811
812
813 int _verify_pbstr(const char *str)
814 {
815 const char *cp;
816 const char *scp;
817 int np;
818 int state;
819 int quote;
820
821 for(cp = str, np = 0, state = 0; *cp; ) {
822 switch (state) {
823 case 0: /* skipping leading spaces */
824
825 while (*cp && isspace(*cp)) ++cp;
826 if (*cp == '=') {
827 return -1;
828 }
829 if (*cp) state = 1;
830 break;
831
832 case 1: /* scanning parameter name */
833
834 scp = cp;
835 while (*cp && (*cp != '=') && !isspace(*cp)) ++cp;
836 if (*cp == '=') ++cp;
837 else cp = scp;
838 state = 2;
839 break;
840
841 case 2: /* scanning parameter value */
842 quote = 0;
843 if (*cp == '\"') {
844 quote = 1;
845 ++cp;
846 }
847 for (;;) {
848 if (*cp == '\\') {
849 ++cp;
850 if (*cp == 0) {
851 return -1;
852 }
853 }
854 else if (*cp == '\"') {
855 if (!quote) {
856 return -1;
857 }
858 ++np;
859 ++cp;
860 quote = 0;
861 state = 0;
862 break;
863 }
864 else if (!quote && (!*cp || isspace(*cp))) {
865 ++np;
866 if (*cp) ++cp;
867 state = 0;
868 break;
869 }
870 else if (*cp == 0) {
871 return -1;
872 }
873 ++cp;
874 }
875 if (quote) {
876 return -1;
877 }
878 break;
879 }
880 }
881
882 return (state == 0) ? np : -1;
883 }
884
885 NSAPI_PUBLIC int
886 INTpblock_str2pblock_lowercasename(const char *str, pblock *pb)
887 {
888 return _pblock_str2pblock(str, pb, PR_TRUE);
889 }
890
891 NSAPI_PUBLIC int pblock_str2pblock(const char *str, pblock *pb)
892 {
893 return _pblock_str2pblock(str, pb, PR_FALSE);
894 }
895
896 int
897 _pblock_str2pblock(const char* str, pblock* pb, PRBool lowerCase)
898 {
899 char *cpy;
900 char *cp;
901 char *dp;
902 char *pname;
903 char *pvalue;
904 int np;
905 int quote;
906 int state;
907 char numbuf[UTIL_ITOA_SIZE];
908
909 if((np = _verify_pbstr(str)) < 1)
910 return -1;
911
912 while (*str && isspace(*str)) ++str;
913
914 cpy = STRDUP(str);
915
916 for (np = 0, cp = cpy, state = 0; *cp; ) {
917 switch (state) {
918
919 case 0: /* skipping leading spaces */
920
921 while (*cp && isspace(*cp)) ++cp;
922 if (*cp) state = 1;
923 break;
924
925 case 1: /* scanning parameter name */
926
927 pname = cp;
928 while (*cp && (*cp != '=') && !isspace(*cp)) ++cp;
929 if (*cp == '=') {
930 *cp++ = 0;
931 }
932 else {
933 cp = pname;
934 pname = numbuf;
935 util_itoa(np+1, numbuf);
936 }
937 state = 2;
938 break;
939
940 case 2: /* scanning parameter value */
941 quote = 0;
942 if (*cp == '\"') {
943 quote = 1;
944 ++cp;
945 }
946 for (pvalue = cp, dp = cp; ; ++cp, ++dp) {
947 if (*cp == '\\') {
948 ++cp;
949 }
950 else if (*cp == '\"') {
951 ++np;
952 ++cp;
953 *dp = 0;
954 quote = 0;
955 state = 0;
956 break;
957 }
958 else if (!quote && ((*cp == 0) || isspace(*cp))) {
959 ++np;
960 if (*cp != 0) {
961 ++cp;
962 }
963 *dp = 0;
964 state = 0;
965 break;
966 }
967 if (cp != dp) *dp = *cp;
968 }
969 if (lowerCase == PR_TRUE) {
970 for (char* p = pname; *p; p++) {
971 *p = tolower(*p);
972 }
973 }
974 pblock_nvinsert(pname, pvalue, pb);
975 break;
976 }
977 }
978
979 FREE(cpy);
980
981 return np;
982 }
983
984
985 /* -------------------------- pblock_pblock2str --------------------------- */
986
987
988 NSAPI_PUBLIC char *pblock_pblock2str(const pblock *pb, char *str)
989 {
990 register char *s = str, *t, *u;
991 PListStruct_t *pl = PBTOPL(pb);
992 PLValueStruct_t **ppval;
993 PLValueStruct_t *pv;
994 int i;
995 int sl;
996 int xlen;
997
998 ppval = (PLValueStruct_t **)(pl->pl_ppval);
999
1000 /* Loop over the initialized property indices */
1001 for (i = 0, xlen = 0; i < pl->pl_initpi; ++i) {
1002
1003 /* Got a property here? */
1004 pv = ppval[i];
1005 if (pv && pv->pv_name) {
1006
1007 int ln = strlen(pv->pv_name);
1008 int lv = strlen((char *)(pv->pv_value));
1009
1010 /* Check for " or \ because we'll have to escape them */
1011 for (t = (char *)(pv->pv_value); *t; ++t) {
1012 if ((*t == '\"') || (*t == '\\')) ++lv;
1013 }
1014
1015 /* 4: two quotes, =, and a null */
1016 xlen += (ln + lv + 4);
1017 }
1018 }
1019
1020 /* Allocate string to hold parameter settings, or increase size */
1021 if (!s) {
1022 s = (char *)MALLOC(xlen);
1023 s[0] = '\0';
1024 t = &s[0];
1025 sl = xlen;
1026 }
1027 else {
1028 sl = strlen(s);
1029 t = &s[sl];
1030 sl += xlen;
1031 s = (char *)REALLOC(s, sl);
1032 }
1033
1034 /* Loop over the initialized property indices */
1035 for (i = 0; i < pl->pl_initpi; ++i) {
1036
1037 /* Got a property here? */
1038 pv = ppval[i];
1039 if (pv && pv->pv_name) {
1040
1041 if (t != s) *t++ = ' ';
1042
1043 for (u = pv->pv_name; *u; ) *t++ = *u++;
1044
1045 *t++ = '=';
1046 *t++ = '\"';
1047
1048 for (u = (char *)(pv->pv_value); *u; ) {
1049 if ((*u == '\\') || (*u == '\"')) *t++ = '\\';
1050 *t++ = *u++;
1051 }
1052
1053 *t++ = '\"';
1054 *t = '\0';
1055 }
1056 }
1057
1058 return s;
1059 }
1060
1061
1062 /* ----------------------------- pblock_copy ------------------------------ */
1063
1064
1065 NSAPI_PUBLIC int pblock_copy(const pblock *src, pblock *dst)
1066 {
1067 PListStruct_t *pl = PBTOPL(src);
1068 PLValueStruct_t **ppval;
1069 PLValueStruct_t *pv;
1070 int rv = 0;
1071 int i;
1072
1073 ppval = (PLValueStruct_t **)(pl->pl_ppval);
1074
1075 for (i = 0; i < pl->pl_initpi; ++i) {
1076 pv = ppval[i];
1077 if (pv) {
1078 if (pv->pv_pbkey) {
1079 if (pv->pv_pbkey != pb_key_magnus_internal) {
1080 if (!pblock_kvinsert(pv->pv_pbkey, (char *)(pv->pv_value), strlen(pv->pv_value), dst))
1081 rv = -1;
1082 }
1083 } else {
1084 if (!pblock_nvinsert(pv->pv_name, (char *)(pv->pv_value), dst))
1085 rv = -1;
1086 }
1087 }
1088 }
1089
1090 return rv;
1091 }
1092
1093 /* ---------------------------- pblock_dup -------------------------------- */
1094
1095 NSAPI_PUBLIC pblock *pblock_dup(const pblock *src)
1096 {
1097 pblock *dst;
1098
1099 if (!src)
1100 return NULL;
1101
1102 if ( (dst = pblock_create(src->hsize)) )
1103 pblock_copy(src, dst);
1104
1105 return dst;
1106 }
1107
1108
1109 /* ---------------------------- pblock_pb2env ----------------------------- */
1110
1111 /*
1112 NSAPI_PUBLIC char **pblock_pb2env(const pblock *pb, char **env)
1113 {
1114 PListStruct_t *pl = PBTOPL(pb);
1115 PLValueStruct_t **ppval;
1116 PLValueStruct_t *pv;
1117 int i;
1118 int nval;
1119 int pos;
1120
1121 /* Find out how many there are. */
1122 /*
1123 ppval = (PLValueStruct_t **)(pl->pl_ppval);
1124
1125 for (i = 0, nval = 0; i < pl->pl_initpi; ++i) {
1126 if (ppval[i]) ++nval;
1127 }
1128
1129 env = util_env_create(env, nval, &pos);
1130
1131 for (i = 0; i < pl->pl_initpi; ++i) {
1132 pv = ppval[i];
1133 if (pv) {
1134 env[pos++] = util_env_str(pv->pv_name, (char *)(pv->pv_value));
1135 }
1136 }
1137 env[pos] = NULL;
1138
1139 return env;
1140 }
1141 */
1142
1143 /* ---------------------------- pblock_replace ---------------------------- */
1144
1145 NSAPI_PUBLIC char * pblock_replace(const char *name,
1146 char * new_value, pblock *pb)
1147 {
1148 PListStruct_t *pl = PBTOPL(pb);
1149
1150 /* Replace an existing value */
1151 pb_param *pp = pblock_find(name, pb);
1152 if (!pp)
1153 return NULL;
1154 pool_free(pl->pl_mempool, pp->value);
1155 pp->value = new_value;
1156
1157 return new_value;
1158 }
1159
1160
1161 /* --------------------------- pblock_nvreplace --------------------------- */
1162
1163 NSAPI_PUBLIC void pblock_nvreplace (const char *name, const char *value, pblock *pb)
1164 {
1165 PListStruct_t *pl = PBTOPL(pb);
1166
1167 /* Replace an existing value or insert a new value */
1168 pb_param *pp = pblock_find(name, pb);
1169 if (pp) {
1170 pool_free(pl->pl_mempool, pp->value);
1171 pp->value = pool_strdup(pl->pl_mempool, value);
1172 } else {
1173 pblock_nvinsert(name, value, pb);
1174 }
1175 }
1176
1177
1178 /* --------------------------- pblock_kvreplace --------------------------- */
1179
1180 NSAPI_PUBLIC void pblock_kvreplace(const pb_key *key, const char *value, int valuelen, pblock *pb)
1181 {
1182 PListStruct_t *pl = PBTOPL(pb);
1183
1184 /* Replace an existing value or insert a new value */
1185 pb_param *pp = pblock_findkey(key, pb);
1186 if (pp) {
1187 pool_free(pl->pl_mempool, pp->value);
1188 pp->value = (char*)pool_malloc(pl->pl_mempool, valuelen + 1);
1189 memcpy(pp->value, value, valuelen + 1);
1190 } else {
1191 pblock_kvinsert(key, value, valuelen, pb);
1192 }
1193 }

mercurial