michael@0: /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #include michael@0: michael@0: #include "mozilla/RangedPtr.h" michael@0: michael@0: #include "nsURLParsers.h" michael@0: #include "nsURLHelper.h" michael@0: #include "nsString.h" michael@0: #include "nsCRT.h" michael@0: michael@0: using namespace mozilla; michael@0: michael@0: //---------------------------------------------------------------------------- michael@0: michael@0: static uint32_t michael@0: CountConsecutiveSlashes(const char *str, int32_t len) michael@0: { michael@0: RangedPtr p(str, len); michael@0: uint32_t count = 0; michael@0: while (len-- && *p++ == '/') ++count; michael@0: return count; michael@0: } michael@0: michael@0: //---------------------------------------------------------------------------- michael@0: // nsBaseURLParser implementation michael@0: //---------------------------------------------------------------------------- michael@0: michael@0: NS_IMPL_ISUPPORTS(nsAuthURLParser, nsIURLParser) michael@0: NS_IMPL_ISUPPORTS(nsNoAuthURLParser, nsIURLParser) michael@0: michael@0: #define SET_RESULT(component, pos, len) \ michael@0: PR_BEGIN_MACRO \ michael@0: if (component ## Pos) \ michael@0: *component ## Pos = uint32_t(pos); \ michael@0: if (component ## Len) \ michael@0: *component ## Len = int32_t(len); \ michael@0: PR_END_MACRO michael@0: michael@0: #define OFFSET_RESULT(component, offset) \ michael@0: PR_BEGIN_MACRO \ michael@0: if (component ## Pos) \ michael@0: *component ## Pos += offset; \ michael@0: PR_END_MACRO michael@0: michael@0: NS_IMETHODIMP michael@0: nsBaseURLParser::ParseURL(const char *spec, int32_t specLen, michael@0: uint32_t *schemePos, int32_t *schemeLen, michael@0: uint32_t *authorityPos, int32_t *authorityLen, michael@0: uint32_t *pathPos, int32_t *pathLen) michael@0: { michael@0: NS_PRECONDITION(spec, "null pointer"); michael@0: michael@0: if (specLen < 0) michael@0: specLen = strlen(spec); michael@0: michael@0: const char *stop = nullptr; michael@0: const char *colon = nullptr; michael@0: const char *slash = nullptr; michael@0: const char *p; michael@0: uint32_t offset = 0; michael@0: int32_t len = specLen; michael@0: for (p = spec; len && *p && !colon && !slash; ++p, --len) { michael@0: // skip leading whitespace michael@0: if (*p == ' ' || *p == '\n' || *p == '\r' || *p == '\t') { michael@0: spec++; michael@0: specLen--; michael@0: offset++; michael@0: continue; michael@0: } michael@0: switch (*p) { michael@0: case ':': michael@0: if (!colon) michael@0: colon = p; michael@0: break; michael@0: case '/': // start of filepath michael@0: case '?': // start of query michael@0: case '#': // start of ref michael@0: if (!slash) michael@0: slash = p; michael@0: break; michael@0: case '@': // username@hostname michael@0: case '[': // start of IPv6 address literal michael@0: if (!stop) michael@0: stop = p; michael@0: break; michael@0: } michael@0: } michael@0: // disregard the first colon if it follows an '@' or a '[' michael@0: if (colon && stop && colon > stop) michael@0: colon = nullptr; michael@0: michael@0: // if the spec only contained whitespace ... michael@0: if (specLen == 0) { michael@0: SET_RESULT(scheme, 0, -1); michael@0: SET_RESULT(authority, 0, 0); michael@0: SET_RESULT(path, 0, 0); michael@0: return NS_OK; michael@0: } michael@0: michael@0: // ignore trailing whitespace and control characters michael@0: for (p = spec + specLen - 1; ((unsigned char) *p <= ' ') && (p != spec); --p) michael@0: ; michael@0: michael@0: specLen = p - spec + 1; michael@0: michael@0: if (colon && (colon < slash || !slash)) { michael@0: // michael@0: // spec = :/ michael@0: // michael@0: // or michael@0: // michael@0: // spec = : michael@0: // spec = : michael@0: // michael@0: if (!net_IsValidScheme(spec, colon - spec) || (*(colon+1) == ':')) { michael@0: NS_WARNING("malformed uri"); michael@0: return NS_ERROR_MALFORMED_URI; michael@0: } michael@0: SET_RESULT(scheme, offset, colon - spec); michael@0: if (authorityLen || pathLen) { michael@0: uint32_t schemeLen = colon + 1 - spec; michael@0: offset += schemeLen; michael@0: ParseAfterScheme(colon + 1, specLen - schemeLen, michael@0: authorityPos, authorityLen, michael@0: pathPos, pathLen); michael@0: OFFSET_RESULT(authority, offset); michael@0: OFFSET_RESULT(path, offset); michael@0: } michael@0: } michael@0: else { michael@0: // michael@0: // spec = / michael@0: // spec = michael@0: // michael@0: // or michael@0: // michael@0: // spec = / michael@0: // spec = michael@0: // michael@0: // or michael@0: // michael@0: // spec = michael@0: // spec = michael@0: // michael@0: SET_RESULT(scheme, 0, -1); michael@0: if (authorityLen || pathLen) { michael@0: ParseAfterScheme(spec, specLen, michael@0: authorityPos, authorityLen, michael@0: pathPos, pathLen); michael@0: OFFSET_RESULT(authority, offset); michael@0: OFFSET_RESULT(path, offset); michael@0: } michael@0: } michael@0: return NS_OK; michael@0: } michael@0: michael@0: NS_IMETHODIMP michael@0: nsBaseURLParser::ParseAuthority(const char *auth, int32_t authLen, michael@0: uint32_t *usernamePos, int32_t *usernameLen, michael@0: uint32_t *passwordPos, int32_t *passwordLen, michael@0: uint32_t *hostnamePos, int32_t *hostnameLen, michael@0: int32_t *port) michael@0: { michael@0: NS_PRECONDITION(auth, "null pointer"); michael@0: michael@0: if (authLen < 0) michael@0: authLen = strlen(auth); michael@0: michael@0: SET_RESULT(username, 0, -1); michael@0: SET_RESULT(password, 0, -1); michael@0: SET_RESULT(hostname, 0, authLen); michael@0: if (port) michael@0: *port = -1; michael@0: return NS_OK; michael@0: } michael@0: michael@0: NS_IMETHODIMP michael@0: nsBaseURLParser::ParseUserInfo(const char *userinfo, int32_t userinfoLen, michael@0: uint32_t *usernamePos, int32_t *usernameLen, michael@0: uint32_t *passwordPos, int32_t *passwordLen) michael@0: { michael@0: SET_RESULT(username, 0, -1); michael@0: SET_RESULT(password, 0, -1); michael@0: return NS_OK; michael@0: } michael@0: michael@0: NS_IMETHODIMP michael@0: nsBaseURLParser::ParseServerInfo(const char *serverinfo, int32_t serverinfoLen, michael@0: uint32_t *hostnamePos, int32_t *hostnameLen, michael@0: int32_t *port) michael@0: { michael@0: SET_RESULT(hostname, 0, -1); michael@0: if (port) michael@0: *port = -1; michael@0: return NS_OK; michael@0: } michael@0: michael@0: NS_IMETHODIMP michael@0: nsBaseURLParser::ParsePath(const char *path, int32_t pathLen, michael@0: uint32_t *filepathPos, int32_t *filepathLen, michael@0: uint32_t *queryPos, int32_t *queryLen, michael@0: uint32_t *refPos, int32_t *refLen) michael@0: { michael@0: NS_PRECONDITION(path, "null pointer"); michael@0: michael@0: if (pathLen < 0) michael@0: pathLen = strlen(path); michael@0: michael@0: // path = [/]//<...>/?# michael@0: michael@0: // XXX PL_strnpbrk would be nice, but it's buggy michael@0: michael@0: // search for first occurrence of either ? or # michael@0: const char *query_beg = 0, *query_end = 0; michael@0: const char *ref_beg = 0; michael@0: const char *p = 0; michael@0: for (p = path; p < path + pathLen; ++p) { michael@0: // only match the query string if it precedes the reference fragment michael@0: if (!ref_beg && !query_beg && *p == '?') michael@0: query_beg = p + 1; michael@0: else if (*p == '#') { michael@0: ref_beg = p + 1; michael@0: if (query_beg) michael@0: query_end = p; michael@0: break; michael@0: } michael@0: } michael@0: michael@0: if (query_beg) { michael@0: if (query_end) michael@0: SET_RESULT(query, query_beg - path, query_end - query_beg); michael@0: else michael@0: SET_RESULT(query, query_beg - path, pathLen - (query_beg - path)); michael@0: } michael@0: else michael@0: SET_RESULT(query, 0, -1); michael@0: michael@0: if (ref_beg) michael@0: SET_RESULT(ref, ref_beg - path, pathLen - (ref_beg - path)); michael@0: else michael@0: SET_RESULT(ref, 0, -1); michael@0: michael@0: const char *end; michael@0: if (query_beg) michael@0: end = query_beg - 1; michael@0: else if (ref_beg) michael@0: end = ref_beg - 1; michael@0: else michael@0: end = path + pathLen; michael@0: michael@0: // an empty file path is no file path michael@0: if (end != path) michael@0: SET_RESULT(filepath, 0, end - path); michael@0: else michael@0: SET_RESULT(filepath, 0, -1); michael@0: return NS_OK; michael@0: } michael@0: michael@0: NS_IMETHODIMP michael@0: nsBaseURLParser::ParseFilePath(const char *filepath, int32_t filepathLen, michael@0: uint32_t *directoryPos, int32_t *directoryLen, michael@0: uint32_t *basenamePos, int32_t *basenameLen, michael@0: uint32_t *extensionPos, int32_t *extensionLen) michael@0: { michael@0: NS_PRECONDITION(filepath, "null pointer"); michael@0: michael@0: if (filepathLen < 0) michael@0: filepathLen = strlen(filepath); michael@0: michael@0: if (filepathLen == 0) { michael@0: SET_RESULT(directory, 0, -1); michael@0: SET_RESULT(basename, 0, 0); // assume a zero length file basename michael@0: SET_RESULT(extension, 0, -1); michael@0: return NS_OK; michael@0: } michael@0: michael@0: const char *p; michael@0: const char *end = filepath + filepathLen; michael@0: michael@0: // search backwards for filename michael@0: for (p = end - 1; *p != '/' && p > filepath; --p) michael@0: ; michael@0: if (*p == '/') { michael@0: // catch /.. and /. michael@0: if ((p+1 < end && *(p+1) == '.') && michael@0: (p+2 == end || (*(p+2) == '.' && p+3 == end))) michael@0: p = end - 1; michael@0: // filepath = . michael@0: SET_RESULT(directory, 0, p - filepath + 1); michael@0: ParseFileName(p + 1, end - (p + 1), michael@0: basenamePos, basenameLen, michael@0: extensionPos, extensionLen); michael@0: OFFSET_RESULT(basename, p + 1 - filepath); michael@0: OFFSET_RESULT(extension, p + 1 - filepath); michael@0: } michael@0: else { michael@0: // filepath = . michael@0: SET_RESULT(directory, 0, -1); michael@0: ParseFileName(filepath, filepathLen, michael@0: basenamePos, basenameLen, michael@0: extensionPos, extensionLen); michael@0: } michael@0: return NS_OK; michael@0: } michael@0: michael@0: nsresult michael@0: nsBaseURLParser::ParseFileName(const char *filename, int32_t filenameLen, michael@0: uint32_t *basenamePos, int32_t *basenameLen, michael@0: uint32_t *extensionPos, int32_t *extensionLen) michael@0: { michael@0: NS_PRECONDITION(filename, "null pointer"); michael@0: michael@0: if (filenameLen < 0) michael@0: filenameLen = strlen(filename); michael@0: michael@0: // no extension if filename ends with a '.' michael@0: if (filename[filenameLen-1] != '.') { michael@0: // ignore '.' at the beginning michael@0: for (const char *p = filename + filenameLen - 1; p > filename; --p) { michael@0: if (*p == '.') { michael@0: // filename = michael@0: SET_RESULT(basename, 0, p - filename); michael@0: SET_RESULT(extension, p + 1 - filename, filenameLen - (p - filename + 1)); michael@0: return NS_OK; michael@0: } michael@0: } michael@0: } michael@0: // filename = michael@0: SET_RESULT(basename, 0, filenameLen); michael@0: SET_RESULT(extension, 0, -1); michael@0: return NS_OK; michael@0: } michael@0: michael@0: //---------------------------------------------------------------------------- michael@0: // nsNoAuthURLParser implementation michael@0: //---------------------------------------------------------------------------- michael@0: michael@0: NS_IMETHODIMP michael@0: nsNoAuthURLParser::ParseAuthority(const char *auth, int32_t authLen, michael@0: uint32_t *usernamePos, int32_t *usernameLen, michael@0: uint32_t *passwordPos, int32_t *passwordLen, michael@0: uint32_t *hostnamePos, int32_t *hostnameLen, michael@0: int32_t *port) michael@0: { michael@0: NS_NOTREACHED("Shouldn't parse auth in a NoAuthURL!"); michael@0: return NS_ERROR_UNEXPECTED; michael@0: } michael@0: michael@0: void michael@0: nsNoAuthURLParser::ParseAfterScheme(const char *spec, int32_t specLen, michael@0: uint32_t *authPos, int32_t *authLen, michael@0: uint32_t *pathPos, int32_t *pathLen) michael@0: { michael@0: NS_PRECONDITION(specLen >= 0, "unexpected"); michael@0: michael@0: // everything is the path michael@0: uint32_t pos = 0; michael@0: switch (CountConsecutiveSlashes(spec, specLen)) { michael@0: case 0: michael@0: case 1: michael@0: break; michael@0: case 2: michael@0: { michael@0: const char *p = nullptr; michael@0: if (specLen > 2) { michael@0: // looks like there is an authority section michael@0: #if defined(XP_WIN) michael@0: // if the authority looks like a drive number then we michael@0: // really want to treat it as part of the path michael@0: // [a-zA-Z][:|]{/\} michael@0: // i.e one of: c: c:\foo c:/foo c| c|\foo c|/foo michael@0: if ((specLen > 3) && (spec[3] == ':' || spec[3] == '|') && michael@0: nsCRT::IsAsciiAlpha(spec[2]) && michael@0: ((specLen == 4) || (spec[4] == '/') || (spec[4] == '\\'))) { michael@0: pos = 1; michael@0: break; michael@0: } michael@0: #endif michael@0: // Ignore apparent authority; path is everything after it michael@0: for (p = spec + 2; p < spec + specLen; ++p) { michael@0: if (*p == '/' || *p == '?' || *p == '#') michael@0: break; michael@0: } michael@0: } michael@0: SET_RESULT(auth, 0, -1); michael@0: if (p && p != spec+specLen) michael@0: SET_RESULT(path, p - spec, specLen - (p - spec)); michael@0: else michael@0: SET_RESULT(path, 0, -1); michael@0: return; michael@0: } michael@0: default: michael@0: pos = 2; michael@0: break; michael@0: } michael@0: SET_RESULT(auth, pos, 0); michael@0: SET_RESULT(path, pos, specLen - pos); michael@0: } michael@0: michael@0: #if defined(XP_WIN) michael@0: NS_IMETHODIMP michael@0: nsNoAuthURLParser::ParseFilePath(const char *filepath, int32_t filepathLen, michael@0: uint32_t *directoryPos, int32_t *directoryLen, michael@0: uint32_t *basenamePos, int32_t *basenameLen, michael@0: uint32_t *extensionPos, int32_t *extensionLen) michael@0: { michael@0: NS_PRECONDITION(filepath, "null pointer"); michael@0: michael@0: if (filepathLen < 0) michael@0: filepathLen = strlen(filepath); michael@0: michael@0: // look for a filepath consisting of only a drive number, which may or michael@0: // may not have a leading slash. michael@0: if (filepathLen > 1 && filepathLen < 4) { michael@0: const char *end = filepath + filepathLen; michael@0: const char *p = filepath; michael@0: if (*p == '/') michael@0: p++; michael@0: if ((end-p == 2) && (p[1]==':' || p[1]=='|') && nsCRT::IsAsciiAlpha(*p)) { michael@0: // filepath = : michael@0: SET_RESULT(directory, 0, filepathLen); michael@0: SET_RESULT(basename, 0, -1); michael@0: SET_RESULT(extension, 0, -1); michael@0: return NS_OK; michael@0: } michael@0: } michael@0: michael@0: // otherwise fallback on common implementation michael@0: return nsBaseURLParser::ParseFilePath(filepath, filepathLen, michael@0: directoryPos, directoryLen, michael@0: basenamePos, basenameLen, michael@0: extensionPos, extensionLen); michael@0: } michael@0: #endif michael@0: michael@0: //---------------------------------------------------------------------------- michael@0: // nsAuthURLParser implementation michael@0: //---------------------------------------------------------------------------- michael@0: michael@0: NS_IMETHODIMP michael@0: nsAuthURLParser::ParseAuthority(const char *auth, int32_t authLen, michael@0: uint32_t *usernamePos, int32_t *usernameLen, michael@0: uint32_t *passwordPos, int32_t *passwordLen, michael@0: uint32_t *hostnamePos, int32_t *hostnameLen, michael@0: int32_t *port) michael@0: { michael@0: nsresult rv; michael@0: michael@0: NS_PRECONDITION(auth, "null pointer"); michael@0: michael@0: if (authLen < 0) michael@0: authLen = strlen(auth); michael@0: michael@0: if (authLen == 0) { michael@0: SET_RESULT(username, 0, -1); michael@0: SET_RESULT(password, 0, -1); michael@0: SET_RESULT(hostname, 0, 0); michael@0: if (port) michael@0: *port = -1; michael@0: return NS_OK; michael@0: } michael@0: michael@0: // search backwards for @ michael@0: const char *p = auth + authLen - 1; michael@0: for (; (*p != '@') && (p > auth); --p) { michael@0: continue; michael@0: } michael@0: if ( *p == '@' ) { michael@0: // auth = michael@0: rv = ParseUserInfo(auth, p - auth, michael@0: usernamePos, usernameLen, michael@0: passwordPos, passwordLen); michael@0: if (NS_FAILED(rv)) return rv; michael@0: rv = ParseServerInfo(p + 1, authLen - (p - auth + 1), michael@0: hostnamePos, hostnameLen, michael@0: port); michael@0: if (NS_FAILED(rv)) return rv; michael@0: OFFSET_RESULT(hostname, p + 1 - auth); michael@0: } michael@0: else { michael@0: // auth = michael@0: SET_RESULT(username, 0, -1); michael@0: SET_RESULT(password, 0, -1); michael@0: rv = ParseServerInfo(auth, authLen, michael@0: hostnamePos, hostnameLen, michael@0: port); michael@0: if (NS_FAILED(rv)) return rv; michael@0: } michael@0: return NS_OK; michael@0: } michael@0: michael@0: NS_IMETHODIMP michael@0: nsAuthURLParser::ParseUserInfo(const char *userinfo, int32_t userinfoLen, michael@0: uint32_t *usernamePos, int32_t *usernameLen, michael@0: uint32_t *passwordPos, int32_t *passwordLen) michael@0: { michael@0: NS_PRECONDITION(userinfo, "null pointer"); michael@0: michael@0: if (userinfoLen < 0) michael@0: userinfoLen = strlen(userinfo); michael@0: michael@0: if (userinfoLen == 0) { michael@0: SET_RESULT(username, 0, -1); michael@0: SET_RESULT(password, 0, -1); michael@0: return NS_OK; michael@0: } michael@0: michael@0: const char *p = (const char *) memchr(userinfo, ':', userinfoLen); michael@0: if (p) { michael@0: // userinfo = michael@0: if (p == userinfo) { michael@0: // must have a username! michael@0: return NS_ERROR_MALFORMED_URI; michael@0: } michael@0: SET_RESULT(username, 0, p - userinfo); michael@0: SET_RESULT(password, p - userinfo + 1, userinfoLen - (p - userinfo + 1)); michael@0: } michael@0: else { michael@0: // userinfo = michael@0: SET_RESULT(username, 0, userinfoLen); michael@0: SET_RESULT(password, 0, -1); michael@0: } michael@0: return NS_OK; michael@0: } michael@0: michael@0: NS_IMETHODIMP michael@0: nsAuthURLParser::ParseServerInfo(const char *serverinfo, int32_t serverinfoLen, michael@0: uint32_t *hostnamePos, int32_t *hostnameLen, michael@0: int32_t *port) michael@0: { michael@0: NS_PRECONDITION(serverinfo, "null pointer"); michael@0: michael@0: if (serverinfoLen < 0) michael@0: serverinfoLen = strlen(serverinfo); michael@0: michael@0: if (serverinfoLen == 0) { michael@0: SET_RESULT(hostname, 0, 0); michael@0: if (port) michael@0: *port = -1; michael@0: return NS_OK; michael@0: } michael@0: michael@0: // search backwards for a ':' but stop on ']' (IPv6 address literal michael@0: // delimiter). check for illegal characters in the hostname. michael@0: const char *p = serverinfo + serverinfoLen - 1; michael@0: const char *colon = nullptr, *bracket = nullptr; michael@0: for (; p > serverinfo; --p) { michael@0: switch (*p) { michael@0: case ']': michael@0: bracket = p; michael@0: break; michael@0: case ':': michael@0: if (bracket == nullptr) michael@0: colon = p; michael@0: break; michael@0: case ' ': michael@0: // hostname must not contain a space michael@0: NS_WARNING("malformed hostname"); michael@0: return NS_ERROR_MALFORMED_URI; michael@0: } michael@0: } michael@0: michael@0: if (colon) { michael@0: // serverinfo = michael@0: SET_RESULT(hostname, 0, colon - serverinfo); michael@0: if (port) { michael@0: // XXX unfortunately ToInteger is not defined for substrings michael@0: nsAutoCString buf(colon+1, serverinfoLen - (colon + 1 - serverinfo)); michael@0: if (buf.Length() == 0) { michael@0: *port = -1; michael@0: } michael@0: else { michael@0: const char* nondigit = NS_strspnp("0123456789", buf.get()); michael@0: if (nondigit && *nondigit) michael@0: return NS_ERROR_MALFORMED_URI; michael@0: michael@0: nsresult err; michael@0: *port = buf.ToInteger(&err); michael@0: if (NS_FAILED(err) || *port < 0) michael@0: return NS_ERROR_MALFORMED_URI; michael@0: } michael@0: } michael@0: } michael@0: else { michael@0: // serverinfo = michael@0: SET_RESULT(hostname, 0, serverinfoLen); michael@0: if (port) michael@0: *port = -1; michael@0: } michael@0: michael@0: // In case of IPv6 address check its validity michael@0: if (*hostnameLen > 1 && *(serverinfo + *hostnamePos) == '[' && michael@0: *(serverinfo + *hostnamePos + *hostnameLen - 1) == ']' && michael@0: !net_IsValidIPv6Addr(serverinfo + *hostnamePos + 1, *hostnameLen - 2)) michael@0: return NS_ERROR_MALFORMED_URI; michael@0: michael@0: return NS_OK; michael@0: } michael@0: michael@0: void michael@0: nsAuthURLParser::ParseAfterScheme(const char *spec, int32_t specLen, michael@0: uint32_t *authPos, int32_t *authLen, michael@0: uint32_t *pathPos, int32_t *pathLen) michael@0: { michael@0: NS_PRECONDITION(specLen >= 0, "unexpected"); michael@0: michael@0: uint32_t nslash = CountConsecutiveSlashes(spec, specLen); michael@0: michael@0: // search for the end of the authority section michael@0: const char *end = spec + specLen; michael@0: const char *p; michael@0: for (p = spec + nslash; p < end; ++p) { michael@0: if (*p == '/' || *p == '?' || *p == '#') michael@0: break; michael@0: } michael@0: if (p < end) { michael@0: // spec = [/] michael@0: SET_RESULT(auth, nslash, p - (spec + nslash)); michael@0: SET_RESULT(path, p - spec, specLen - (p - spec)); michael@0: } michael@0: else { michael@0: // spec = [/] michael@0: SET_RESULT(auth, nslash, specLen - nslash); michael@0: SET_RESULT(path, 0, -1); michael@0: } michael@0: } michael@0: michael@0: //---------------------------------------------------------------------------- michael@0: // nsStdURLParser implementation michael@0: //---------------------------------------------------------------------------- michael@0: michael@0: void michael@0: nsStdURLParser::ParseAfterScheme(const char *spec, int32_t specLen, michael@0: uint32_t *authPos, int32_t *authLen, michael@0: uint32_t *pathPos, int32_t *pathLen) michael@0: { michael@0: NS_PRECONDITION(specLen >= 0, "unexpected"); michael@0: michael@0: uint32_t nslash = CountConsecutiveSlashes(spec, specLen); michael@0: michael@0: // search for the end of the authority section michael@0: const char *end = spec + specLen; michael@0: const char *p; michael@0: for (p = spec + nslash; p < end; ++p) { michael@0: if (strchr("/?#;", *p)) michael@0: break; michael@0: } michael@0: switch (nslash) { michael@0: case 0: michael@0: case 2: michael@0: if (p < end) { michael@0: // spec = (//) michael@0: SET_RESULT(auth, nslash, p - (spec + nslash)); michael@0: SET_RESULT(path, p - spec, specLen - (p - spec)); michael@0: } michael@0: else { michael@0: // spec = (//) michael@0: SET_RESULT(auth, nslash, specLen - nslash); michael@0: SET_RESULT(path, 0, -1); michael@0: } michael@0: break; michael@0: case 1: michael@0: // spec = / michael@0: SET_RESULT(auth, 0, -1); michael@0: SET_RESULT(path, 0, specLen); michael@0: break; michael@0: default: michael@0: // spec = ///[/] michael@0: SET_RESULT(auth, 2, 0); michael@0: SET_RESULT(path, 2, specLen - 2); michael@0: } michael@0: }