Changeset 205521 in webkit


Ignore:
Timestamp:
Sep 6, 2016 4:36:36 PM (8 years ago)
Author:
achristensen@apple.com
Message:

Punycode encode non-ascii hosts in URLParser
https://bugs.webkit.org/show_bug.cgi?id=161655

Reviewed by Tim Horton.

Source/WebCore:

Covered by new API tests based on the web platform tests.

  • platform/URLParser.cpp:

(WebCore::URLParser::parse):
(WebCore::containsOnlyASCII):
(WebCore::domainToASCII):

Tools:

  • TestWebKitAPI/Tests/WebCore/URLParser.cpp:

(TestWebKitAPI::wideString):
(TestWebKitAPI::TEST_F):

Location:
trunk
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • trunk/Source/WebCore/ChangeLog

    r205520 r205521  
     12016-09-06  Alex Christensen  <achristensen@webkit.org>
     2
     3        Punycode encode non-ascii hosts in URLParser
     4        https://bugs.webkit.org/show_bug.cgi?id=161655
     5
     6        Reviewed by Tim Horton.
     7
     8        Covered by new API tests based on the web platform tests.
     9
     10        * platform/URLParser.cpp:
     11        (WebCore::URLParser::parse):
     12        (WebCore::containsOnlyASCII):
     13        (WebCore::domainToASCII):
     14
    1152016-09-06  Saam Barati  <sbarati@apple.com>
    216
  • trunk/Source/WebCore/platform/URLParser.cpp

    r205493 r205521  
    2929#include "Logging.h"
    3030#include <array>
     31#include <unicode/uidna.h>
    3132#include <wtf/HashMap.h>
    3233#include <wtf/NeverDestroyed.h>
     
    451452            LOG_STATE("SchemeEndCheckForSlashes");
    452453            if (*c == '/') {
    453                 m_buffer.append('/');
     454                m_buffer.append("//");
     455                m_url.m_userStart = m_buffer.length();
    454456                state = State::PathOrAuthority;
    455457                ++c;
     
    12701272}
    12711273
     1274static bool containsOnlyASCII(const String& string)
     1275{
     1276    if (string.is8Bit())
     1277        return charactersAreAllASCII(string.characters8(), string.length());
     1278    return charactersAreAllASCII(string.characters16(), string.length());
     1279}
     1280
    12721281static Optional<String> domainToASCII(const String& domain)
    12731282{
    1274     // FIXME: Implement correctly
    1275     CString utf8 = domain.utf8();
    1276     return String(utf8.data(), utf8.length());
     1283    const unsigned hostnameBufferLength = 2048;
     1284
     1285    if (containsOnlyASCII(domain)) {
     1286        if (domain.is8Bit())
     1287            return domain;
     1288        Vector<LChar, hostnameBufferLength> buffer;
     1289        size_t length = domain.length();
     1290        buffer.reserveInitialCapacity(length);
     1291        for (size_t i = 0; i < length; ++i)
     1292            buffer.append(domain[i]);
     1293        return String(buffer.data(), length);
     1294    }
     1295   
     1296    UChar hostnameBuffer[hostnameBufferLength];
     1297    UErrorCode error = U_ZERO_ERROR;
     1298   
     1299    int32_t numCharactersConverted = uidna_IDNToASCII(StringView(domain).upconvertedCharacters(), domain.length(), hostnameBuffer, hostnameBufferLength, UIDNA_ALLOW_UNASSIGNED, nullptr, &error);
     1300
     1301    if (error == U_ZERO_ERROR) {
     1302        LChar buffer[hostnameBufferLength];
     1303        for (int32_t i = 0; i < numCharactersConverted; ++i) {
     1304            ASSERT(isASCII(hostnameBuffer[i]));
     1305            buffer[i] = hostnameBuffer[i];
     1306        }
     1307        return String(buffer, numCharactersConverted);
     1308    }
     1309
     1310    // FIXME: Check for U_BUFFER_OVERFLOW_ERROR and retry with an allocated buffer.
     1311    return Nullopt;
    12771312}
    12781313
  • trunk/Tools/ChangeLog

    r205493 r205521  
     12016-09-06  Alex Christensen  <achristensen@webkit.org>
     2
     3        Punycode encode non-ascii hosts in URLParser
     4        https://bugs.webkit.org/show_bug.cgi?id=161655
     5
     6        Reviewed by Tim Horton.
     7
     8        * TestWebKitAPI/Tests/WebCore/URLParser.cpp:
     9        (TestWebKitAPI::wideString):
     10        (TestWebKitAPI::TEST_F):
     11
    1122016-09-05  Alex Christensen  <achristensen@webkit.org>
    213
  • trunk/Tools/TestWebKitAPI/Tests/WebCore/URLParser.cpp

    r205493 r205521  
    8383   
    8484    EXPECT_TRUE(URLParser::allValuesEqual(url, oldURL));
     85}
     86
     87template<size_t length>
     88static String wideString(const wchar_t (&url)[length])
     89{
     90    StringBuilder builder;
     91    builder.reserveCapacity(length - 1);
     92    for (size_t i = 0; i < length - 1; ++i)
     93        builder.append(url[i]);
     94    return builder.toString();
    8595}
    8696
     
    210220    checkRelativeURL("http://example\t.\norg", "http://example.org/foo/bar", {"http", "", "", "example.org", 0, "/", "", "", "http://example.org/"});
    211221    checkRelativeURL("test", "file:///path1/path2", {"file", "", "", "", 0, "/path1/test", "", "", "file:///path1/test"});
     222    checkRelativeURL(wideString(L"http://www.foo。bar.com"), "http://other.com/", {"http", "", "", "www.foo.bar.com", 0, "/", "", "", "http://www.foo.bar.com/"});
     223    checkRelativeURL(wideString(L"sc://ñ.test/"), "about:blank", {"sc", "", "", "xn--ida.test", 0, "/", "", "", "sc://xn--ida.test/"});
    212224}
    213225
     
    352364   
    353365    // URLParser matches Chrome and the spec, but not URL::parse or Firefox.
     366    checkURLDifferences(wideString(L"http://0Xc0.0250.01"),
     367        {"http", "", "", "192.168.0.1", 0, "/", "", "", "http://192.168.0.1/"},
     368        {"http", "", "", "0xc0.0250.01", 0, "/", "", "", "http://0xc0.0250.01/"});
    354369    checkURLDifferences("http://host/path%2e.%2E",
    355370        {"http", "", "", "host", 0, "/path...", "", "", "http://host/path..."},
Note: See TracChangeset for help on using the changeset viewer.