| 519 #endif |
519 #endif |
| 520 |
520 |
| 521 char* util_path_normalize(const char *path) { |
521 char* util_path_normalize(const char *path) { |
| 522 size_t len = strlen(path); |
522 size_t len = strlen(path); |
| 523 CxBuffer buf; |
523 CxBuffer buf; |
| 524 cxBufferInit(&buf, NULL, len+1, cxDefaultAllocator, CX_BUFFER_FREE_CONTENTS|CX_BUFFER_AUTO_EXTEND); |
524 cxBufferInit(&buf, cxDefaultAllocator, NULL, len+1, CX_BUFFER_FREE_CONTENTS|CX_BUFFER_AUTO_EXTEND); |
| 525 |
525 |
| 526 if(path[0] == '/') { |
526 if(path[0] == '/') { |
| 527 cxBufferPut(&buf, '/'); |
527 cxBufferPut(&buf, '/'); |
| 528 } |
528 } |
| 529 |
529 |
| 539 seg_len--; |
539 seg_len--; |
| 540 } |
540 } |
| 541 |
541 |
| 542 if(seg_len > 0) { |
542 if(seg_len > 0) { |
| 543 cxstring seg = cx_strn(seg_ptr, seg_len); |
543 cxstring seg = cx_strn(seg_ptr, seg_len); |
| 544 if(!cx_strcmp(seg, CX_STR(".."))) { |
544 if(!cx_strcmp(seg, cx_str(".."))) { |
| 545 for(int j=buf.pos;j>=0;j--) { |
545 for(int j=buf.pos;j>=0;j--) { |
| 546 char t = j < buf.pos ? buf.space[j] : 0; |
546 char t = j < buf.pos ? buf.space[j] : 0; |
| 547 if(IS_PATH_SEPARATOR(t) || j == 0) { |
547 if(IS_PATH_SEPARATOR(t) || j == 0) { |
| 548 buf.pos = j; |
548 buf.pos = j; |
| 549 buf.size = j; |
549 buf.size = j; |
| 550 buf.space[j] = 0; |
550 buf.space[j] = 0; |
| 551 add_separator = IS_PATH_SEPARATOR(t) ? 1 : 0; |
551 add_separator = IS_PATH_SEPARATOR(t) ? 1 : 0; |
| 552 break; |
552 break; |
| 553 } |
553 } |
| 554 } |
554 } |
| 555 } else if(!cx_strcmp(seg, CX_STR("."))) { |
555 } else if(!cx_strcmp(seg, cx_str("."))) { |
| 556 // ignore |
556 // ignore |
| 557 } else { |
557 } else { |
| 558 if(add_separator) { |
558 if(add_separator) { |
| 559 cxBufferPut(&buf, PATH_SEPARATOR); |
559 cxBufferPut(&buf, PATH_SEPARATOR); |
| 560 } |
560 } |
| 613 if(IS_PATH_SEPARATOR(base[i])) { |
613 if(IS_PATH_SEPARATOR(base[i])) { |
| 614 dircount++; |
614 dircount++; |
| 615 } |
615 } |
| 616 } |
616 } |
| 617 |
617 |
| 618 cxBufferInit(&out, NULL, dircount*3+path_len-last_dir, cxDefaultAllocator, CX_BUFFER_FREE_CONTENTS|CX_BUFFER_AUTO_EXTEND); |
618 cxBufferInit(&out, cxDefaultAllocator, NULL, dircount*3+path_len-last_dir, CX_BUFFER_FREE_CONTENTS|CX_BUFFER_AUTO_EXTEND); |
| 619 |
619 |
| 620 for(size_t i=0;i<dircount;i++) { |
620 for(size_t i=0;i<dircount;i++) { |
| 621 cxBufferPutString(&out, "../"); |
621 cxBufferPutString(&out, "../"); |
| 622 } |
622 } |
| 623 } else { |
623 } else { |
| 624 cxBufferInit(&out, NULL, path_len - last_dir, cxDefaultAllocator, CX_BUFFER_FREE_CONTENTS|CX_BUFFER_AUTO_EXTEND); |
624 cxBufferInit(&out, cxDefaultAllocator, NULL, path_len - last_dir, CX_BUFFER_FREE_CONTENTS|CX_BUFFER_AUTO_EXTEND); |
| 625 } |
625 } |
| 626 |
626 |
| 627 cxBufferPutString(&out, abspath + last_dir + 1); |
627 cxBufferPutString(&out, abspath + last_dir + 1); |
| 628 cxBufferPut(&out, 0); |
628 cxBufferPut(&out, 0); |
| 629 |
629 |
| 717 cxstring base = cx_str(url_base); |
717 cxstring base = cx_str(url_base); |
| 718 cxstring path; |
718 cxstring path; |
| 719 if(p) { |
719 if(p) { |
| 720 path = cx_str((char*)p); |
720 path = cx_str((char*)p); |
| 721 } else { |
721 } else { |
| 722 path = CX_STR(""); |
722 path = cx_str(""); |
| 723 } |
723 } |
| 724 |
724 |
| 725 return util_concat_path_s(base, path).ptr; |
725 return util_concat_path_s(base, path).ptr; |
| 726 } |
726 } |
| 727 |
727 |
| 728 cxmutstr util_concat_path_s(cxstring base, cxstring path) { |
728 cxmutstr util_concat_path_s(cxstring base, cxstring path) { |
| 729 if(!path.ptr) { |
729 if(!path.ptr) { |
| 730 path = CX_STR(""); |
730 path = cx_str(""); |
| 731 } |
731 } |
| 732 |
732 |
| 733 int add_separator = 0; |
733 int add_separator = 0; |
| 734 if(base.length != 0 && base.ptr[base.length-1] == '/') { |
734 if(base.length != 0 && base.ptr[base.length-1] == '/') { |
| 735 if(path.ptr[0] == '/') { |
735 if(path.ptr[0] == '/') { |
| 741 } |
741 } |
| 742 } |
742 } |
| 743 |
743 |
| 744 cxmutstr url; |
744 cxmutstr url; |
| 745 if(add_separator) { |
745 if(add_separator) { |
| 746 url = cx_strcat(3, base, CX_STR("/"), path); |
746 url = cx_strcat(3, base, cx_str("/"), path); |
| 747 } else { |
747 } else { |
| 748 url = cx_strcat(2, base, path); |
748 url = cx_strcat(2, base, path); |
| 749 } |
749 } |
| 750 |
750 |
| 751 return url; |
751 return url; |
| 752 } |
752 } |
| 753 |
753 |
| 754 cxmutstr util_concat_path_ext(cxstring base, cxstring path, char separator) { |
754 cxmutstr util_concat_path_ext(cxstring base, cxstring path, char separator) { |
| 755 if(!path.ptr) { |
755 if(!path.ptr) { |
| 756 path = CX_STR(""); |
756 path = cx_str(""); |
| 757 } |
757 } |
| 758 |
758 |
| 759 int add_separator = 0; |
759 int add_separator = 0; |
| 760 if(base.length != 0 && base.ptr[base.length-1] == separator) { |
760 if(base.length != 0 && base.ptr[base.length-1] == separator) { |
| 761 if(path.ptr[0] == separator) { |
761 if(path.ptr[0] == separator) { |
| 807 if(pathlen == 0) { |
807 if(pathlen == 0) { |
| 808 return strdup(sn->base_url); |
808 return strdup(sn->base_url); |
| 809 } |
809 } |
| 810 |
810 |
| 811 CxBuffer url; |
811 CxBuffer url; |
| 812 cxBufferInit(&url, NULL, 256, cxDefaultAllocator, CX_BUFFER_FREE_CONTENTS|CX_BUFFER_AUTO_EXTEND); |
812 cxBufferInit(&url, cxDefaultAllocator, NULL, 256, CX_BUFFER_FREE_CONTENTS|CX_BUFFER_AUTO_EXTEND); |
| 813 |
813 |
| 814 // add base url |
814 // add base url |
| 815 cxBufferWrite(sn->base_url, 1, strlen(sn->base_url), &url); |
815 cxBufferWrite(sn->base_url, 1, strlen(sn->base_url), &url); |
| 816 // remove trailing slash |
816 // remove trailing slash |
| 817 cxBufferSeek(&url, -1, SEEK_CUR); |
817 cxBufferSeek(&url, -1, SEEK_CUR); |
| 818 |
818 |
| 819 cxstring p = cx_strn(path, pathlen); |
819 cxstring p = cx_strn(path, pathlen); |
| 820 |
820 |
| 821 CxStrtokCtx tkctx = cx_strtok(p, CX_STR("/"), INT_MAX); |
821 CxStrtokCtx tkctx = cx_strtok(p, cx_str("/"), INT_MAX); |
| 822 cxstring node; |
822 cxstring node; |
| 823 while(cx_strtok_next(&tkctx, &node)) { |
823 while(cx_strtok_next(&tkctx, &node)) { |
| 824 if(node.length > 0) { |
824 if(node.length > 0) { |
| 825 char *esc = curl_easy_escape(sn->handle, node.ptr, node.length); |
825 char *esc = curl_easy_escape(sn->handle, node.ptr, node.length); |
| 826 cxBufferPut(&url, '/'); |
826 cxBufferPut(&url, '/'); |
| 1153 |
1153 |
| 1154 char* util_random_str() { |
1154 char* util_random_str() { |
| 1155 unsigned char *str = malloc(25); |
1155 unsigned char *str = malloc(25); |
| 1156 str[24] = '\0'; |
1156 str[24] = '\0'; |
| 1157 |
1157 |
| 1158 cxstring t = CX_STR( |
1158 cxstring t = cx_str( |
| 1159 "01234567890" |
1159 "01234567890" |
| 1160 "abcdefghijklmnopqrstuvwxyz" |
1160 "abcdefghijklmnopqrstuvwxyz" |
| 1161 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"); |
1161 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"); |
| 1162 const unsigned char *table = (const unsigned char*)t.ptr; |
1162 const unsigned char *table = (const unsigned char*)t.ptr; |
| 1163 |
1163 |
| 1258 |
1258 |
| 1259 #endif |
1259 #endif |
| 1260 |
1260 |
| 1261 // read password input |
1261 // read password input |
| 1262 CxBuffer buf; |
1262 CxBuffer buf; |
| 1263 cxBufferInit(&buf, NULL, 128, cxDefaultAllocator, CX_BUFFER_FREE_CONTENTS|CX_BUFFER_AUTO_EXTEND); |
1263 cxBufferInit(&buf, cxDefaultAllocator, NULL, 128, CX_BUFFER_FREE_CONTENTS|CX_BUFFER_AUTO_EXTEND); |
| 1264 int c = 0; |
1264 int c = 0; |
| 1265 while((c = getpasswordchar()) != EOF) { |
1265 while((c = getpasswordchar()) != EOF) { |
| 1266 if(c == '\n' || c == '\r') { |
1266 if(c == '\n' || c == '\r') { |
| 1267 break; |
1267 break; |
| 1268 } |
1268 } |
| 1333 } |
1333 } |
| 1334 |
1334 |
| 1335 char* util_hexstr(const unsigned char *data, size_t len) { |
1335 char* util_hexstr(const unsigned char *data, size_t len) { |
| 1336 size_t buflen = 2*len + 4; |
1336 size_t buflen = 2*len + 4; |
| 1337 CxBuffer buf; |
1337 CxBuffer buf; |
| 1338 cxBufferInit(&buf, NULL, buflen + 1, cxDefaultAllocator, CX_BUFFER_FREE_CONTENTS|CX_BUFFER_AUTO_EXTEND); |
1338 cxBufferInit(&buf, cxDefaultAllocator, NULL, buflen + 1, CX_BUFFER_FREE_CONTENTS|CX_BUFFER_AUTO_EXTEND); |
| 1339 for(int i=0;i<len;i++) { |
1339 for(int i=0;i<len;i++) { |
| 1340 cx_bprintf(&buf, "%02x", data[i]); |
1340 cx_bprintf(&buf, "%02x", data[i]); |
| 1341 } |
1341 } |
| 1342 cxBufferPut(&buf, 0); |
1342 cxBufferPut(&buf, 0); |
| 1343 return buf.space; |
1343 return buf.space; |