FskErr KprURLMerge(char* base, char* reference, char** result) { FskErr err = kFskErrNone; KprURLPartsRecord parts; KprURLPartsRecord baseParts; char* path = NULL; UInt32 length; KprURLSplit(reference, &parts); if (!parts.scheme) { KprURLSplit(base, &baseParts); parts.scheme = baseParts.scheme; parts.schemeLength = baseParts.schemeLength; if (!parts.authority) { parts.authority = baseParts.authority; parts.authorityLength = baseParts.authorityLength; if (!parts.pathLength) { if (baseParts.authority && !baseParts.pathLength) { bailIfError(FskMemPtrNew(1, &path)); FskMemMove(path, "/", 1); parts.path = path; parts.pathLength = 1; } else { parts.path = baseParts.path; parts.pathLength = baseParts.pathLength; } if (!parts.query) { parts.query = baseParts.query; parts.queryLength = baseParts.queryLength; } } else if (*parts.path != '/') { if (baseParts.authority && !baseParts.pathLength) { bailIfError(FskMemPtrNew(1 + parts.pathLength, &path)); FskMemMove(path, "/", 1); FskMemMove(path + 1, parts.path, parts.pathLength); parts.path = path; parts.pathLength++; } else if (baseParts.name) { length = baseParts.name - baseParts.path; bailIfError(FskMemPtrNew(length + parts.pathLength, &path)); FskMemMove(path, baseParts.path, length); FskMemMove(path + length, parts.path, parts.pathLength); parts.path = path; parts.pathLength += length; } } } } bailIfError(KprURLJoin(&parts, result)); bail: FskMemPtrDispose(path); return err; }
FskErr KprMessageURL(KprContext context, char* url, char** result) { FskErr err = kFskErrNone; KprURLPartsRecord parts; KprURLSplit(url, &parts); if (!parts.scheme) { parts.scheme = "xkpr"; parts.schemeLength = 4; if (!parts.authority) { parts.authority = context->id; parts.authorityLength = FskStrLen(parts.authority); } bailIfError(KprURLJoin(&parts, result)); } else { *result = FskStrDoCopy(url); bailIfNULL(*result); } bail: return err; }
void KPR_serializeURI(xsMachine *the) { KprURLPartsRecord parts; xsStringValue target; FskMemSet(&parts, 0, sizeof(KprURLPartsRecord)); xsEnterSandbox(); if (xsFindString(xsArg(0), xsID_scheme, &parts.scheme)) parts.schemeLength = FskStrLen(parts.scheme); if (xsFindString(xsArg(0), xsID_authority, &parts.authority)) parts.authorityLength = FskStrLen(parts.authority); if (xsFindString(xsArg(0), xsID_path, &parts.path)) parts.pathLength = FskStrLen(parts.path); if (xsFindString(xsArg(0), xsID_query, &parts.query)) parts.queryLength = FskStrLen(parts.query); if (xsFindString(xsArg(0), xsID_fragment, &parts.fragment)) parts.fragmentLength = FskStrLen(parts.fragment); xsLeaveSandbox(); xsThrowIfFskErr(KprURLJoin(&parts, &target)); xsResult = xsString(target); FskMemPtrDispose(target); }