src/server/util/util.c

changeset 385
a1f4cb076d2f
parent 382
9e2289c77b04
child 415
d938228c382e
equal deleted inserted replaced
210:21274e5950af 385:a1f4cb076d2f
53 53
54 #include "../daemon/netsite.h" 54 #include "../daemon/netsite.h"
55 #include "../public/nsapi.h" 55 #include "../public/nsapi.h"
56 #include <ucx/string.h> 56 #include <ucx/string.h>
57 #include <ucx/mempool.h> 57 #include <ucx/mempool.h>
58 #include <ucx/utils.h>
58 59
59 #include "pblock.h" 60 #include "pblock.h"
60 #include "util.h" 61 #include "util.h"
61 62
62 #include <openssl/bio.h> 63 #include <openssl/bio.h>
286 va_list args; 287 va_list args;
287 va_start(args, fmt); 288 va_start(args, fmt);
288 return vsnprintf(s, n, fmt, args); 289 return vsnprintf(s, n, fmt, args);
289 } 290 }
290 291
292 NSAPI_PUBLIC int util_vasprintf(pool_handle_t *pool, char **s, const char *fmt,
293 va_list args)
294 {
295 UcxAllocator a = util_pool_allocator(pool);
296 va_list ap;
297 va_copy(ap, args);
298 sstr_t str = ucx_vasprintf(&a, fmt, ap);
299 *s = str.ptr;
300 return str.length;
301 }
302
303 NSAPI_PUBLIC int util_asprintf(pool_handle_t *pool, char **s, const char *fmt, ...)
304 {
305 va_list args;
306 va_start(args, fmt);
307 return util_vasprintf(pool, s, fmt, args);
308 }
309
291 NSAPI_PUBLIC int util_vsprintf(char *s, register const char *fmt, va_list args) 310 NSAPI_PUBLIC int util_vsprintf(char *s, register const char *fmt, va_list args)
292 { 311 {
293 return vsprintf(s, fmt, args); 312 return vsprintf(s, fmt, args);
294 } 313 }
295 314
392 return 0; 411 return 0;
393 } 412 }
394 return def; 413 return def;
395 } 414 }
396 415
397 int util_getboolean_s(sstr_t s, int def) { 416 int util_getboolean_s(scstr_t s, int def) {
398 if(s.length == 0) { 417 if(s.length == 0) {
399 return def; 418 return def;
400 } 419 }
401 if(s.ptr[0] == 'T' || s.ptr[0] == 't') { 420 if(s.ptr[0] == 'T' || s.ptr[0] == 't') {
402 return 1; 421 return 1;
405 return 0; 424 return 0;
406 } 425 }
407 return def; 426 return def;
408 } 427 }
409 428
410 NSAPI_PUBLIC int util_strtoint(char *str, int64_t *value) { 429 NSAPI_PUBLIC int util_strtoint(const char *str, int64_t *value) {
411 char *end; 430 char *end;
412 errno = 0; 431 errno = 0;
413 int64_t val = strtoll(str, &end, 0); 432 int64_t val = strtoll(str, &end, 0);
414 if(errno == 0) { 433 if(errno == 0) {
415 *value = val; 434 *value = val;
417 } else { 436 } else {
418 return 0; 437 return 0;
419 } 438 }
420 } 439 }
421 440
441 NSAPI_PUBLIC const char* util_resource_name(const char *url) {
442 scstr_t urlstr = scstr(url);
443 if(urlstr.ptr[urlstr.length-1] == '/') {
444 urlstr.length--;
445 }
446 scstr_t resname = scstrrchr(urlstr, '/');
447 if(resname.length > 1) {
448 return resname.ptr+1;
449 } else {
450 return url;
451 }
452 }
453
454 NSAPI_PUBLIC char* util_parent_path(const char *path) {
455 char *name = (char*)util_resource_name((char*)path);
456 size_t namelen = strlen(name);
457 size_t pathlen = strlen(path);
458 size_t parentlen = pathlen - namelen;
459 char *parent = MALLOC(parentlen + 1);
460 memcpy(parent, path, parentlen);
461 parent[parentlen] = '\0';
462 return parent;
463 }
464
465 NSAPI_PUBLIC char* util_parent_path_pool(pool_handle_t *pool, const char *path) {
466 // maybe we can unify this function with util_parent_path
467 char *name = (char*)util_resource_name((char*)path);
468 size_t namelen = strlen(name);
469 size_t pathlen = strlen(path);
470 size_t parentlen = pathlen - namelen;
471 char *parent = pool_malloc(pool, parentlen + 1);
472 memcpy(parent, path, parentlen);
473 parent[parentlen] = '\0';
474 return parent;
475 }
422 476
423 /* ------------------------------ util_itoa ------------------------------- */ 477 /* ------------------------------ util_itoa ------------------------------- */
424 /* 478 /*
425 NSAPI_PUBLIC int util_itoa(int i, char *a) 479 NSAPI_PUBLIC int util_itoa(int i, char *a)
426 { 480 {
1031 r[4], 1085 r[4],
1032 r[5]); 1086 r[5]);
1033 1087
1034 return pos; 1088 return pos;
1035 } 1089 }
1090
1091
1092 /* ------------------------- util_html_escape -------------------------- */
1093
1094 NSAPI_PUBLIC char *util_html_escape(const char *s)
1095 {
1096 const char *in;
1097
1098 int len = 0;
1099 for (in = s; *in; in++) {
1100 switch (*in) {
1101 case '<':
1102 len += 4; // &lt;
1103 break;
1104 case '>':
1105 len += 4; // &gt;
1106 break;
1107 case '&':
1108 len += 5; // &amp;
1109 break;
1110 case '"':
1111 len += 6; // &quot;
1112 break;
1113 case '\'':
1114 len += 6; // &apos;
1115 break;
1116 case '+':
1117 len += 5; // &#43;
1118 break;
1119 default:
1120 len++;
1121 break;
1122 }
1123 }
1124
1125 char *ns = (char *) MALLOC(len + 1);
1126 if (!ns)
1127 return ns;
1128
1129 char *out = ns;
1130 for (in = s; *in; in++) {
1131 switch (*in) {
1132 case '<':
1133 *out++ = '&';
1134 *out++ = 'l';
1135 *out++ = 't';
1136 *out++ = ';';
1137 break;
1138 case '>':
1139 *out++ = '&';
1140 *out++ = 'g';
1141 *out++ = 't';
1142 *out++ = ';';
1143 break;
1144 case '&':
1145 *out++ = '&';
1146 *out++ = 'a';
1147 *out++ = 'm';
1148 *out++ = 'p';
1149 *out++ = ';';
1150 break;
1151 case '"':
1152 *out++ = '&';
1153 *out++ = 'q';
1154 *out++ = 'u';
1155 *out++ = 'o';
1156 *out++ = 't';
1157 *out++ = ';';
1158 break;
1159 case '\'':
1160 *out++ = '&';
1161 *out++ = 'a';
1162 *out++ = 'p';
1163 *out++ = 'o';
1164 *out++ = 's';
1165 *out++ = ';';
1166 break;
1167 case '+':
1168 *out++ = '&';
1169 *out++ = '#';
1170 *out++ = '4';
1171 *out++ = '3';
1172 *out++ = ';';
1173 break;
1174 default:
1175 *out++ = *in;
1176 break;
1177 }
1178 }
1179 *out = '\0';
1180
1181 return ns;
1182 }
1183

mercurial