summaryrefslogtreecommitdiff
path: root/Source/WebCore/Modules/fetch/FetchBody.cpp
blob: c42c7768e81c71b56d2260390f6c1c6d4ab48370 (plain)
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
/*
 * Copyright (C) 2016 Canon 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 Canon 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 CANON 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 CANON 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 "FetchBody.h"

#if ENABLE(FETCH_API)

#include "Dictionary.h"
#include "ExceptionCode.h"
#include "JSBlob.h"
#include "JSDOMFormData.h"
#include <runtime/JSONObject.h>

namespace WebCore {

FetchBody::FetchBody(Ref<Blob>&& blob)
    : m_type(Type::Blob)
    , m_mimeType(blob->type())
    , m_blob(WTFMove(blob))
{
}

FetchBody::FetchBody(Ref<DOMFormData>&& formData)
    : m_type(Type::FormData)
    , m_mimeType(ASCIILiteral("multipart/form-data"))
    , m_formData(WTFMove(formData))
{
    // FIXME: Handle the boundary parameter of multipart/form-data MIME type.
}

FetchBody::FetchBody(String&& text)
    : m_type(Type::Text)
    , m_mimeType(ASCIILiteral("text/plain;charset=UTF-8"))
    , m_text(WTFMove(text))
{
}

FetchBody FetchBody::fromJSValue(JSC::ExecState& state, JSC::JSValue value)
{
    if (value.inherits(JSBlob::info()))
        return FetchBody(*JSBlob::toWrapped(value));
    if (value.inherits(JSDOMFormData::info()))
        return FetchBody(*JSDOMFormData::toWrapped(value));
    if (value.isString())
        return FetchBody(value.toWTFString(&state));
    return FetchBody();
}

// FIXME: Once FetchResponse is added, check whether using a move constructor instead.
// Ensure whether resetting m_mimeType to the empty string be not observable.
FetchBody FetchBody::fromRequestBody(FetchBody* body)
{
    if (!body)
        return FetchBody();

    FetchBody result;
    result.m_type = body->m_type;
    result.m_mimeType = body->m_mimeType;

    result.m_blob = WTFMove(body->m_blob);
    result.m_formData = WTFMove(body->m_formData);
    result.m_text = WTFMove(body->m_text);

    body->m_isDisturbed = true;

    return result;
}

template<typename T> inline bool FetchBody::processIfEmptyOrDisturbed(DOMPromise<T, ExceptionCode>& promise)
{
    if (m_type == Type::None) {
        promise.resolve(T());
        return true;
    }

    if (m_isDisturbed) {
        promise.reject(TypeError);
        return true;
    }
    m_isDisturbed = true;
    return false;
}

void FetchBody::arrayBuffer(ArrayBufferPromise&& promise)
{
    if (processIfEmptyOrDisturbed(promise))
        return;

    if (m_type == Type::Text) {
        // FIXME: promise expects a Vector<unsigned char> that will be converted to an ArrayBuffer.
        // We should try to create a Vector<unsigned char> or an ArrayBuffer directly from m_text.
        CString data = m_text.utf8();
        Vector<unsigned char> value(data.length());
        memcpy(value.data(), data.data(), data.length());
        promise.resolve(WTFMove(value));
        return;
    }
    // FIXME: Support other types.
    promise.reject(0);
}

void FetchBody::formData(FormDataPromise&& promise)
{
    if (m_type == Type::None || m_isDisturbed) {
        promise.reject(TypeError);
        return;
    }
    m_isDisturbed = true;

    // FIXME: Support other types.
    promise.reject(0);
}

void FetchBody::blob(BlobPromise&& promise)
{
    if (processIfEmptyOrDisturbed(promise))
        return;

    // FIXME: Support other types.
    promise.reject(0);
}

void FetchBody::text(TextPromise&& promise)
{
    if (processIfEmptyOrDisturbed(promise))
        return;

    if (m_type == Type::Text) {
        promise.resolve(m_text);
        return;
    }
    // FIXME: Support other types.
    promise.reject(0);
}

void FetchBody::json(JSC::ExecState& state, JSONPromise&& promise)
{
    if (processIfEmptyOrDisturbed(promise))
        return;

    if (m_type == Type::Text) {
        JSC::JSValue value = JSC::JSONParse(&state, m_text);
        if (!value)
            promise.reject(SYNTAX_ERR);
        else
            promise.resolve(value);
        return;
    }
    // FIXME: Support other types.
    promise.reject(0);
}

}

#endif // ENABLE(FETCH_API)