src/server/util/util.c

changeset 81
d25825f37967
parent 77
f1cff81e425a
child 91
fac51f87def0
equal deleted inserted replaced
80:0de4a90979e1 81:d25825f37967
253 } 253 }
254 254
255 // TODO: asprintf 255 // TODO: asprintf
256 256
257 257
258
259 /* -------------------------- util_uri_unescape --------------------------- */
260
261 NSAPI_PUBLIC void util_uri_unescape(char *s)
262 {
263 char *t, *u;
264
265 for(t = s, u = s; *t; ++t, ++u) {
266 if((*t == '%') && t[1] && t[2]) {
267 *u = ((t[1] >= 'A' ? ((t[1] & 0xdf) - 'A')+10 : (t[1] - '0'))*16) +
268 (t[2] >= 'A' ? ((t[2] & 0xdf) - 'A')+10 : (t[2] - '0'));
269 t += 2;
270 }
271 else
272 if(u != t)
273 *u = *t;
274 }
275 *u = *t;
276 }
277
278 /*
279 * Same as util_uri_unescape, but returns success/failure
280 */
281 NSAPI_PUBLIC int util_uri_unescape_strict(char *s)
282 {
283 char *t, *u, t1, t2;
284 int rv = 1;
285
286 for(t = s, u = s; *t; ++t, ++u) {
287 if (*t == '%') {
288 t1 = t[1] & 0xdf; /* [a-f] -> [A-F] */
289 if ((t1 < 'A' || t1 > 'F') && (t[1] < '0' || t[1] > '9'))
290 rv = 0;
291
292 t2 = t[2] & 0xdf; /* [a-f] -> [A-F] */
293 if ((t2 < 'A' || t2 > 'F') && (t[2] < '0' || t[2] > '9'))
294 rv = 0;
295
296 *u = ((t[1] >= 'A' ? ((t[1] & 0xdf) - 'A')+10 : (t[1] - '0'))*16) +
297 (t[2] >= 'A' ? ((t[2] & 0xdf) - 'A')+10 : (t[2] - '0'));
298 t += 2;
299 }
300 else if (u != t)
301 *u = *t;
302 }
303 *u = *t;
304
305 return rv;
306 }
307
308
309 NSAPI_PUBLIC int
310 util_uri_unescape_plus (const char *src, char *trg, int len)
311 {
312 const char *t = src;
313 char *u = trg == NULL ? (char *)src : trg;
314 int rlen = 0;
315
316 if (len == -1)
317 len = strlen (src);
318
319 for( ; len && *t; ++t, ++u, len--, rlen++)
320 {
321 if((*t == '%') && t[1] && t[2])
322 {
323 *u = ((t[1] >= 'A' ? ((t[1] & 0xdf) - 'A') + 10 : (t[1] - '0')) * 16) +
324 (t[2] >= 'A' ? ((t[2] & 0xdf) - 'A') + 10 : (t[2] - '0'));
325 t += 2;
326 len-= 2;
327 }
328 else
329 if (*t == '+')
330 *u = ' ';
331 else
332 *u = *t;
333 }
334 *u = 0;
335 return rlen;
336 }
337
338
258 NSAPI_PUBLIC int INTutil_getboolean(const char *v, int def) { 339 NSAPI_PUBLIC int INTutil_getboolean(const char *v, int def) {
259 if(v[0] == 'T' || v[0] == 't') { 340 if(v[0] == 'T' || v[0] == 't') {
260 return 1; 341 return 1;
261 } 342 }
262 if(v[0] == 'F' || v[0] == 'f') { 343 if(v[0] == 'F' || v[0] == 'f') {
377 } 458 }
378 } 459 }
379 return 500; 460 return 500;
380 } 461 }
381 462
382 NSAPI_PUBLIC int util_uri_unescape_strict(char *s) 463
383 {
384 char *t, *u, t1, t2;
385 int rv = 1;
386
387 for(t = s, u = s; *t; ++t, ++u) {
388 if (*t == '%') {
389 t1 = t[1] & 0xdf; /* [a-f] -> [A-F] */
390 if ((t1 < 'A' || t1 > 'F') && (t[1] < '0' || t[1] > '9'))
391 rv = 0;
392
393 t2 = t[2] & 0xdf; /* [a-f] -> [A-F] */
394 if ((t2 < 'A' || t2 > 'F') && (t[2] < '0' || t[2] > '9'))
395 rv = 0;
396
397 *u = ((t[1] >= 'A' ? ((t[1] & 0xdf) - 'A')+10 : (t[1] - '0'))*16) +
398 (t[2] >= 'A' ? ((t[2] & 0xdf) - 'A')+10 : (t[2] - '0'));
399 t += 2;
400 }
401 else if (u != t)
402 *u = *t;
403 }
404 *u = *t;
405
406 return rv;
407 }
408 464
409 NSAPI_PUBLIC 465 NSAPI_PUBLIC
410 sstr_t util_path_append(pool_handle_t *pool, char *path, char *ch) { 466 sstr_t util_path_append(pool_handle_t *pool, char *path, char *ch) {
411 sstr_t parent = sstr(path); 467 sstr_t parent = sstr(path);
412 sstr_t child = sstr(ch); 468 sstr_t child = sstr(ch);
451 path.ptr = NULL; 507 path.ptr = NULL;
452 path.length = 0; 508 path.length = 0;
453 } 509 }
454 return path; 510 return path;
455 } 511 }
512
513
514 // new - code in parts from params.cpp
515 NSAPI_PUBLIC pblock* util_parse_param(pool_handle_t *pool, char *query) {
516 pblock *pb = pblock_create_pool(pool, 32);
517 if(!pb) {
518 return NULL;
519 }
520 if(!query || !(*query)) {
521 return pb;
522 }
523
524 int loopFlag = 1;
525 int nl = 0; // size of the name substring
526 int vl = 0; // size of the value substring
527 int state = 0;
528 const char *np = query;
529 const char *vp = NULL;
530
531 while (loopFlag) {
532 char delim = *query++;
533 switch (delim) {
534 case '&':
535 case '\0': {
536 if(!delim) {
537 loopFlag = 0;
538 }
539
540 state = 0;
541
542 if(nl > 0) {
543 util_uri_unescape_plus(np, NULL, nl);
544 util_uri_unescape_plus(vp, NULL, vl);
545 pblock_nvlinsert(np, nl, vp, vl, pb);
546 }
547
548 nl = 0;
549 vl = 0;
550 vp = NULL;
551 np = query;
552 break;
553 }
554 case '=': {
555 state = 1;
556 vp = query;
557 break;
558 }
559 default: {
560 if(state) {
561 vl++;
562 } else {
563 nl++;
564 }
565 }
566 } /* switch */
567 } /* while */
568
569 return pb;
570 }

mercurial