1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151
|
/*
* Copyright (C) 2016 Apple Inc.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted, provided that the following conditions
* are required to be met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of Apple Inc. nor the names of
* its contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS "AS IS" AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL APPLE INC. AND ITS CONTRIBUTORS BE LIABLE FOR
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "config.h"
#include "FetchBodyConsumer.h"
#include "JSBlob.h"
#include "TextResourceDecoder.h"
namespace WebCore {
static inline Ref<Blob> blobFromData(const unsigned char* data, unsigned length, const String& contentType)
{
Vector<uint8_t> value(length);
memcpy(value.data(), data, length);
return Blob::create(WTFMove(value), contentType);
}
static inline bool shouldPrependBOM(const unsigned char* data, unsigned length)
{
if (length < 3)
return true;
return data[0] != 0xef || data[1] != 0xbb || data[2] != 0xbf;
}
static String textFromUTF8(const unsigned char* data, unsigned length)
{
auto decoder = TextResourceDecoder::create("text/plain", "UTF-8");
if (shouldPrependBOM(data, length))
decoder->decode("\xef\xbb\xbf", 3);
return decoder->decodeAndFlush(reinterpret_cast<const char*>(data), length);
}
void FetchBodyConsumer::resolveWithData(Ref<DeferredPromise>&& promise, const unsigned char* data, unsigned length)
{
switch (m_type) {
case Type::ArrayBuffer:
fulfillPromiseWithArrayBuffer(WTFMove(promise), data, length);
return;
case Type::Blob:
promise->resolveWithNewlyCreated<IDLInterface<Blob>>(blobFromData(data, length, m_contentType).get());
return;
case Type::JSON:
fulfillPromiseWithJSON(WTFMove(promise), textFromUTF8(data, length));
return;
case Type::Text:
promise->resolve<IDLDOMString>(textFromUTF8(data, length));
return;
case Type::None:
ASSERT_NOT_REACHED();
return;
}
}
void FetchBodyConsumer::resolve(Ref<DeferredPromise>&& promise)
{
ASSERT(m_type != Type::None);
switch (m_type) {
case Type::ArrayBuffer:
fulfillPromiseWithArrayBuffer(WTFMove(promise), takeAsArrayBuffer().get());
return;
case Type::Blob:
promise->resolveWithNewlyCreated<IDLInterface<Blob>>(takeAsBlob().get());
return;
case Type::JSON:
fulfillPromiseWithJSON(WTFMove(promise), takeAsText());
return;
case Type::Text:
promise->resolve<IDLDOMString>(takeAsText());
return;
case Type::None:
ASSERT_NOT_REACHED();
return;
}
}
void FetchBodyConsumer::append(const char* data, unsigned length)
{
if (!m_buffer) {
m_buffer = SharedBuffer::create(data, length);
return;
}
m_buffer->append(data, length);
}
void FetchBodyConsumer::append(const unsigned char* data, unsigned length)
{
append(reinterpret_cast<const char*>(data), length);
}
RefPtr<SharedBuffer> FetchBodyConsumer::takeData()
{
return WTFMove(m_buffer);
}
RefPtr<JSC::ArrayBuffer> FetchBodyConsumer::takeAsArrayBuffer()
{
if (!m_buffer)
return ArrayBuffer::tryCreate(nullptr, 0);
auto arrayBuffer = m_buffer->tryCreateArrayBuffer();
m_buffer = nullptr;
return arrayBuffer;
}
Ref<Blob> FetchBodyConsumer::takeAsBlob()
{
if (!m_buffer)
return Blob::create(Vector<uint8_t>(), m_contentType);
// FIXME: We should try to move m_buffer to Blob without doing extra copy.
return blobFromData(reinterpret_cast<const unsigned char*>(m_buffer->data()), m_buffer->size(), m_contentType);
}
String FetchBodyConsumer::takeAsText()
{
// FIXME: We could probably text decode on the fly as soon as m_type is set to JSON or Text.
if (!m_buffer)
return String();
auto text = textFromUTF8(reinterpret_cast<const unsigned char*>(m_buffer->data()), m_buffer->size());
m_buffer = nullptr;
return text;
}
} // namespace WebCore
|