Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Normalize a URI path to remove "./", "../" and redundant separators. Note: this does not make an abs path and
- does not map separators nor change case.
- */
- PUBLIC char *websNormalizeUriPath(char *pathArg)
- {
- char *dupPath, *path, *sp, *dp, *mark, **segments;
- int firstc, j, i, nseg, len;
- if (pathArg == 0 || *pathArg == '\0') {
- return sclone("");
- }
- len = (int) slen(pathArg);
- if ((dupPath = walloc(len + 2)) == 0) {
- return NULL;
- }
- strcpy(dupPath, pathArg);
- if ((segments = walloc(sizeof(char*) * (len + 1))) == 0) {
- return NULL;
- }
- nseg = len = 0;
- firstc = *dupPath;
- for (mark = sp = dupPath; *sp; sp++) {
- if (*sp == '/') {
- *sp = '\0';
- while (sp[1] == '/') {
- sp++;
- }
- segments[nseg++] = mark;
- len += (int) (sp - mark);
- mark = sp + 1;
- }
- }
- segments[nseg++] = mark;
- len += (int) (sp - mark);
- for (j = i = 0; i < nseg; i++, j++) {
- sp = segments[i];
- if (sp[0] == '.') {
- if (sp[1] == '\0') {
- if ((i+1) == nseg) {
- segments[j] = "";
- } else {
- j--;
- }
- } else if (sp[1] == '.' && sp[2] == '\0') {
- if (i == 1 && *segments[0] == '\0') {
- j = 0;
- } else if ((i+1) == nseg) {
- if (--j >= 0) {
- segments[j] = "";
- }
- } else {
- j = max(j - 2, -1);
- }
- }
- } else {
- segments[j] = segments[i];
- }
- }
- nseg = j;
- assert(nseg >= 0);
- if ((path = walloc(len + nseg + 1)) != 0) {
- for (i = 0, dp = path; i < nseg; ) {
- strcpy(dp, segments[i]);
- len = (int) slen(segments[i]);
- dp += len;
- if (++i < nseg || (nseg == 1 && *segments[0] == '\0' && firstc == '/')) {
- *dp++ = '/';
- }
- }
- *dp = '\0';
- }
- wfree(dupPath);
- wfree(segments);
- if ((path[0] != '/') || strchr(path, '\\')) {
- return 0;
- }
- return path;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement