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 "nsEscape.h" michael@0: #include "nsString.h" michael@0: #include "nsIURI.h" michael@0: #include "nsNetUtil.h" michael@0: #include "nsUrlClassifierUtils.h" michael@0: #include "nsTArray.h" michael@0: #include "nsReadableUtils.h" michael@0: #include "plbase64.h" michael@0: #include "prprf.h" michael@0: michael@0: static char int_to_hex_digit(int32_t i) michael@0: { michael@0: NS_ASSERTION((i >= 0) && (i <= 15), "int too big in int_to_hex_digit"); michael@0: return static_cast(((i < 10) ? (i + '0') : ((i - 10) + 'A'))); michael@0: } michael@0: michael@0: static bool michael@0: IsDecimal(const nsACString & num) michael@0: { michael@0: for (uint32_t i = 0; i < num.Length(); i++) { michael@0: if (!isdigit(num[i])) { michael@0: return false; michael@0: } michael@0: } michael@0: michael@0: return true; michael@0: } michael@0: michael@0: static bool michael@0: IsHex(const nsACString & num) michael@0: { michael@0: if (num.Length() < 3) { michael@0: return false; michael@0: } michael@0: michael@0: if (num[0] != '0' || !(num[1] == 'x' || num[1] == 'X')) { michael@0: return false; michael@0: } michael@0: michael@0: for (uint32_t i = 2; i < num.Length(); i++) { michael@0: if (!isxdigit(num[i])) { michael@0: return false; michael@0: } michael@0: } michael@0: michael@0: return true; michael@0: } michael@0: michael@0: static bool michael@0: IsOctal(const nsACString & num) michael@0: { michael@0: if (num.Length() < 2) { michael@0: return false; michael@0: } michael@0: michael@0: if (num[0] != '0') { michael@0: return false; michael@0: } michael@0: michael@0: for (uint32_t i = 1; i < num.Length(); i++) { michael@0: if (!isdigit(num[i]) || num[i] == '8' || num[i] == '9') { michael@0: return false; michael@0: } michael@0: } michael@0: michael@0: return true; michael@0: } michael@0: michael@0: nsUrlClassifierUtils::nsUrlClassifierUtils() : mEscapeCharmap(nullptr) michael@0: { michael@0: } michael@0: michael@0: nsresult michael@0: nsUrlClassifierUtils::Init() michael@0: { michael@0: // Everything but alpha numerics, - and . michael@0: mEscapeCharmap = new Charmap(0xffffffff, 0xfc009fff, 0xf8000001, 0xf8000001, michael@0: 0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff); michael@0: if (!mEscapeCharmap) michael@0: return NS_ERROR_OUT_OF_MEMORY; michael@0: return NS_OK; michael@0: } michael@0: michael@0: NS_IMPL_ISUPPORTS(nsUrlClassifierUtils, nsIUrlClassifierUtils) michael@0: michael@0: ///////////////////////////////////////////////////////////////////////////// michael@0: // nsIUrlClassifierUtils michael@0: michael@0: NS_IMETHODIMP michael@0: nsUrlClassifierUtils::GetKeyForURI(nsIURI * uri, nsACString & _retval) michael@0: { michael@0: nsCOMPtr innerURI = NS_GetInnermostURI(uri); michael@0: if (!innerURI) michael@0: innerURI = uri; michael@0: michael@0: nsAutoCString host; michael@0: innerURI->GetAsciiHost(host); michael@0: michael@0: if (host.IsEmpty()) { michael@0: return NS_ERROR_MALFORMED_URI; michael@0: } michael@0: michael@0: nsresult rv = CanonicalizeHostname(host, _retval); michael@0: NS_ENSURE_SUCCESS(rv, rv); michael@0: michael@0: nsAutoCString path; michael@0: rv = innerURI->GetPath(path); michael@0: NS_ENSURE_SUCCESS(rv, rv); michael@0: michael@0: // strip out anchors michael@0: int32_t ref = path.FindChar('#'); michael@0: if (ref != kNotFound) michael@0: path.SetLength(ref); michael@0: michael@0: nsAutoCString temp; michael@0: rv = CanonicalizePath(path, temp); michael@0: NS_ENSURE_SUCCESS(rv, rv); michael@0: michael@0: _retval.Append(temp); michael@0: michael@0: return NS_OK; michael@0: } michael@0: michael@0: ///////////////////////////////////////////////////////////////////////////// michael@0: // non-interface methods michael@0: michael@0: nsresult michael@0: nsUrlClassifierUtils::CanonicalizeHostname(const nsACString & hostname, michael@0: nsACString & _retval) michael@0: { michael@0: nsAutoCString unescaped; michael@0: if (!NS_UnescapeURL(PromiseFlatCString(hostname).get(), michael@0: PromiseFlatCString(hostname).Length(), michael@0: 0, unescaped)) { michael@0: unescaped.Assign(hostname); michael@0: } michael@0: michael@0: nsAutoCString cleaned; michael@0: CleanupHostname(unescaped, cleaned); michael@0: michael@0: nsAutoCString temp; michael@0: ParseIPAddress(cleaned, temp); michael@0: if (!temp.IsEmpty()) { michael@0: cleaned.Assign(temp); michael@0: } michael@0: michael@0: ToLowerCase(cleaned); michael@0: SpecialEncode(cleaned, false, _retval); michael@0: michael@0: return NS_OK; michael@0: } michael@0: michael@0: michael@0: nsresult michael@0: nsUrlClassifierUtils::CanonicalizePath(const nsACString & path, michael@0: nsACString & _retval) michael@0: { michael@0: _retval.Truncate(); michael@0: michael@0: nsAutoCString decodedPath(path); michael@0: nsAutoCString temp; michael@0: while (NS_UnescapeURL(decodedPath.get(), decodedPath.Length(), 0, temp)) { michael@0: decodedPath.Assign(temp); michael@0: temp.Truncate(); michael@0: } michael@0: michael@0: SpecialEncode(decodedPath, true, _retval); michael@0: // XXX: lowercase the path? michael@0: michael@0: return NS_OK; michael@0: } michael@0: michael@0: void michael@0: nsUrlClassifierUtils::CleanupHostname(const nsACString & hostname, michael@0: nsACString & _retval) michael@0: { michael@0: _retval.Truncate(); michael@0: michael@0: const char* curChar = hostname.BeginReading(); michael@0: const char* end = hostname.EndReading(); michael@0: char lastChar = '\0'; michael@0: while (curChar != end) { michael@0: unsigned char c = static_cast(*curChar); michael@0: if (c == '.' && (lastChar == '\0' || lastChar == '.')) { michael@0: // skip michael@0: } else { michael@0: _retval.Append(*curChar); michael@0: } michael@0: lastChar = c; michael@0: ++curChar; michael@0: } michael@0: michael@0: // cut off trailing dots michael@0: while (_retval.Length() > 0 && _retval[_retval.Length() - 1] == '.') { michael@0: _retval.SetLength(_retval.Length() - 1); michael@0: } michael@0: } michael@0: michael@0: void michael@0: nsUrlClassifierUtils::ParseIPAddress(const nsACString & host, michael@0: nsACString & _retval) michael@0: { michael@0: _retval.Truncate(); michael@0: nsACString::const_iterator iter, end; michael@0: host.BeginReading(iter); michael@0: host.EndReading(end); michael@0: michael@0: if (host.Length() <= 15) { michael@0: // The Windows resolver allows a 4-part dotted decimal IP address to michael@0: // have a space followed by any old rubbish, so long as the total length michael@0: // of the string doesn't get above 15 characters. So, "10.192.95.89 xy" michael@0: // is resolved to 10.192.95.89. michael@0: // If the string length is greater than 15 characters, e.g. michael@0: // "10.192.95.89 xy.wildcard.example.com", it will be resolved through michael@0: // DNS. michael@0: michael@0: if (FindCharInReadable(' ', iter, end)) { michael@0: end = iter; michael@0: } michael@0: } michael@0: michael@0: for (host.BeginReading(iter); iter != end; iter++) { michael@0: if (!(isxdigit(*iter) || *iter == 'x' || *iter == 'X' || *iter == '.')) { michael@0: // not an IP michael@0: return; michael@0: } michael@0: } michael@0: michael@0: host.BeginReading(iter); michael@0: nsTArray parts; michael@0: ParseString(PromiseFlatCString(Substring(iter, end)), '.', parts); michael@0: if (parts.Length() > 4) { michael@0: return; michael@0: } michael@0: michael@0: // If any potentially-octal numbers (start with 0 but not hex) have michael@0: // non-octal digits, no part of the ip can be in octal michael@0: // XXX: this came from the old javascript implementation, is it really michael@0: // supposed to be like this? michael@0: bool allowOctal = true; michael@0: uint32_t i; michael@0: michael@0: for (i = 0; i < parts.Length(); i++) { michael@0: const nsCString& part = parts[i]; michael@0: if (part[0] == '0') { michael@0: for (uint32_t j = 1; j < part.Length(); j++) { michael@0: if (part[j] == 'x') { michael@0: break; michael@0: } michael@0: if (part[j] == '8' || part[j] == '9') { michael@0: allowOctal = false; michael@0: break; michael@0: } michael@0: } michael@0: } michael@0: } michael@0: michael@0: for (i = 0; i < parts.Length(); i++) { michael@0: nsAutoCString canonical; michael@0: michael@0: if (i == parts.Length() - 1) { michael@0: CanonicalNum(parts[i], 5 - parts.Length(), allowOctal, canonical); michael@0: } else { michael@0: CanonicalNum(parts[i], 1, allowOctal, canonical); michael@0: } michael@0: michael@0: if (canonical.IsEmpty()) { michael@0: _retval.Truncate(); michael@0: return; michael@0: } michael@0: michael@0: if (_retval.IsEmpty()) { michael@0: _retval.Assign(canonical); michael@0: } else { michael@0: _retval.Append('.'); michael@0: _retval.Append(canonical); michael@0: } michael@0: } michael@0: return; michael@0: } michael@0: michael@0: void michael@0: nsUrlClassifierUtils::CanonicalNum(const nsACString& num, michael@0: uint32_t bytes, michael@0: bool allowOctal, michael@0: nsACString& _retval) michael@0: { michael@0: _retval.Truncate(); michael@0: michael@0: if (num.Length() < 1) { michael@0: return; michael@0: } michael@0: michael@0: uint32_t val; michael@0: if (allowOctal && IsOctal(num)) { michael@0: if (PR_sscanf(PromiseFlatCString(num).get(), "%o", &val) != 1) { michael@0: return; michael@0: } michael@0: } else if (IsDecimal(num)) { michael@0: if (PR_sscanf(PromiseFlatCString(num).get(), "%u", &val) != 1) { michael@0: return; michael@0: } michael@0: } else if (IsHex(num)) { michael@0: if (PR_sscanf(PromiseFlatCString(num).get(), num[1] == 'X' ? "0X%x" : "0x%x", michael@0: &val) != 1) { michael@0: return; michael@0: } michael@0: } else { michael@0: return; michael@0: } michael@0: michael@0: while (bytes--) { michael@0: char buf[20]; michael@0: PR_snprintf(buf, sizeof(buf), "%u", val & 0xff); michael@0: if (_retval.IsEmpty()) { michael@0: _retval.Assign(buf); michael@0: } else { michael@0: _retval = nsDependentCString(buf) + NS_LITERAL_CSTRING(".") + _retval; michael@0: } michael@0: val >>= 8; michael@0: } michael@0: } michael@0: michael@0: // This function will encode all "special" characters in typical url michael@0: // encoding, that is %hh where h is a valid hex digit. It will also fold michael@0: // any duplicated slashes. michael@0: bool michael@0: nsUrlClassifierUtils::SpecialEncode(const nsACString & url, michael@0: bool foldSlashes, michael@0: nsACString & _retval) michael@0: { michael@0: bool changed = false; michael@0: const char* curChar = url.BeginReading(); michael@0: const char* end = url.EndReading(); michael@0: michael@0: unsigned char lastChar = '\0'; michael@0: while (curChar != end) { michael@0: unsigned char c = static_cast(*curChar); michael@0: if (ShouldURLEscape(c)) { michael@0: _retval.Append('%'); michael@0: _retval.Append(int_to_hex_digit(c / 16)); michael@0: _retval.Append(int_to_hex_digit(c % 16)); michael@0: michael@0: changed = true; michael@0: } else if (foldSlashes && (c == '/' && lastChar == '/')) { michael@0: // skip michael@0: } else { michael@0: _retval.Append(*curChar); michael@0: } michael@0: lastChar = c; michael@0: curChar++; michael@0: } michael@0: return changed; michael@0: } michael@0: michael@0: bool michael@0: nsUrlClassifierUtils::ShouldURLEscape(const unsigned char c) const michael@0: { michael@0: return c <= 32 || c == '%' || c >=127; michael@0: }