Changeset 205188 in webkit


Ignore:
Timestamp:
Aug 30, 2016 11:09:07 AM (8 years ago)
Author:
commit-queue@webkit.org
Message:

[Fetch API] Body mix-in text() should decode data as UTF-8
https://bugs.webkit.org/show_bug.cgi?id=161372

Patch by Youenn Fablet <youenn@apple.com> on 2016-08-30
Reviewed by Sam Weinig.

LayoutTests/imported/w3c:

  • web-platform-tests/fetch/api/basic/text-utf8-expected.txt: Added.
  • web-platform-tests/fetch/api/basic/text-utf8.html: Added.
  • web-platform-tests/fetch/api/resources/status.py: Added.

(main):

Source/WebCore:

Test: imported/w3c/web-platform-tests/fetch/api/basic/text-utf8.html

UsingTextResourceDecoder to decode data as UTF-8.
Making sure to prepend BOM if there is none, as specified in https://encoding.spec.whatwg.org/#utf-8-decode.

  • Modules/fetch/FetchBodyConsumer.cpp:

(WebCore::shouldPrependBOM):
(WebCore::textFromUTF8):
(WebCore::FetchBodyConsumer::resolveWithData):
(WebCore::FetchBodyConsumer::takeAsText):

Location:
trunk
Files:
3 added
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/LayoutTests/imported/w3c/ChangeLog

    r205169 r205188  
     12016-08-30  Youenn Fablet  <youenn@apple.com>
     2
     3        [Fetch API] Body mix-in text() should decode data as UTF-8
     4        https://bugs.webkit.org/show_bug.cgi?id=161372
     5
     6        Reviewed by Sam Weinig.
     7
     8        * web-platform-tests/fetch/api/basic/text-utf8-expected.txt: Added.
     9        * web-platform-tests/fetch/api/basic/text-utf8.html: Added.
     10        * web-platform-tests/fetch/api/resources/status.py: Added.
     11        (main):
     12
    1132016-08-29  Chris Dumez  <cdumez@apple.com>
    214
  • trunk/Source/WebCore/ChangeLog

    r205186 r205188  
     12016-08-30  Youenn Fablet  <youenn@apple.com>
     2
     3        [Fetch API] Body mix-in text() should decode data as UTF-8
     4        https://bugs.webkit.org/show_bug.cgi?id=161372
     5
     6        Reviewed by Sam Weinig.
     7
     8        Test: imported/w3c/web-platform-tests/fetch/api/basic/text-utf8.html
     9
     10        UsingTextResourceDecoder to decode data as UTF-8.
     11        Making sure to prepend BOM if there is none, as specified in https://encoding.spec.whatwg.org/#utf-8-decode.
     12
     13        * Modules/fetch/FetchBodyConsumer.cpp:
     14        (WebCore::shouldPrependBOM):
     15        (WebCore::textFromUTF8):
     16        (WebCore::FetchBodyConsumer::resolveWithData):
     17        (WebCore::FetchBodyConsumer::takeAsText):
     18
    1192016-08-30  Zalan Bujtas  <zalan@apple.com>
    220
  • trunk/Source/WebCore/Modules/fetch/FetchBodyConsumer.cpp

    r204171 r205188  
    3434#include "JSBlob.h"
    3535#include "JSDOMPromise.h"
     36#include "TextResourceDecoder.h"
    3637
    3738namespace WebCore {
     
    4243    memcpy(value.data(), data, length);
    4344    return Blob::create(WTFMove(value), contentType);
     45}
     46
     47static inline bool shouldPrependBOM(const unsigned char* data, unsigned length)
     48{
     49    if (length < 3)
     50        return true;
     51    return data[0] != 0xef || data[1] != 0xbb || data[2] != 0xbf;
     52}
     53
     54static String textFromUTF8(const unsigned char* data, unsigned length)
     55{
     56    auto decoder = TextResourceDecoder::create("text/plain", "UTF-8");
     57    if (shouldPrependBOM(data, length))
     58        decoder->decode("\xef\xbb\xbf", 3);
     59    return decoder->decodeAndFlush(reinterpret_cast<const char*>(data), length);
    4460}
    4561
     
    5470        return;
    5571    case Type::JSON:
    56         fulfillPromiseWithJSON(promise, String(data, length));
     72        fulfillPromiseWithJSON(promise, textFromUTF8(data, length));
    5773        return;
    5874    case Type::Text:
    59         promise.resolve(String(data, length));
     75        promise.resolve(textFromUTF8(data, length));
    6076        return;
    6177    case Type::None:
     
    127143String FetchBodyConsumer::takeAsText()
    128144{
     145    // FIXME: We could probably text decode on the fly as soon as m_type is set to JSON or Text.
    129146    if (!m_buffer)
    130147        return String();
    131148
    132     auto text = String(m_buffer->data(), m_buffer->size());
     149    auto text = textFromUTF8(reinterpret_cast<const unsigned char*>(m_buffer->data()), m_buffer->size());
    133150    m_buffer = nullptr;
    134151    return text;
Note: See TracChangeset for help on using the changeset viewer.