Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add implementation of MultipartFormData streaming encoder #200

Merged
merged 14 commits into from
Feb 14, 2025
Merged
52 changes: 42 additions & 10 deletions builtins/web/fetch/request-response.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#include "request-response.h"

#include "../blob.h"
#include "../form-data/form-data.h"
#include "../form-data/form-data-encoder.h"
#include "../streams/native-stream-source.h"
#include "../streams/transform-stream.h"
#include "../url.h"
Expand All @@ -17,6 +19,7 @@
#include "js/Conversions.h"
#include "js/JSON.h"
#include "js/Stream.h"
#include "mozilla/ResultVariant.h"
#include <algorithm>
#include <iostream>
#include <vector>
Expand All @@ -27,8 +30,6 @@
#include "js/experimental/TypedData.h"
#pragma clang diagnostic pop

using builtins::web::blob::Blob;

namespace builtins::web::streams {

bool NativeStreamSource::stream_is_body(JSContext *cx, JS::HandleObject stream) {
Expand All @@ -41,6 +42,12 @@ bool NativeStreamSource::stream_is_body(JSContext *cx, JS::HandleObject stream)

namespace builtins::web::fetch {

using blob::Blob;
using form_data::FormData;
using form_data::MultipartFormData;

using namespace std::literals;

static api::Engine *ENGINE;

bool error_stream_controller_with_pending_exception(JSContext *cx, HandleObject stream) {
Expand Down Expand Up @@ -286,19 +293,19 @@ bool RequestOrResponse::extract_body(JSContext *cx, JS::HandleObject self,
MOZ_ASSERT(!has_body(self));
MOZ_ASSERT(!body_val.isNullOrUndefined());

const char *content_type = nullptr;
string_view content_type;
mozilla::Maybe<size_t> content_length;

// We currently support five types of body inputs:
// We support all types of body inputs required by the spec:
// - byte sequence
// - buffer source
// - Blob
// - FormData
andreiltd marked this conversation as resolved.
Show resolved Hide resolved
// - USV strings
// - URLSearchParams
// - ReadableStream
// After the other other options are checked explicitly, all other inputs are
// encoded to a UTF8 string to be treated as a USV string.
// TODO: Support the other possible inputs to Body.

JS::RootedObject body_obj(cx, body_val.isObject() ? &body_val.toObject() : nullptr);
host_api::HostString host_type_str;
Expand All @@ -318,8 +325,33 @@ bool RequestOrResponse::extract_body(JSContext *cx, JS::HandleObject self,
if (JS::GetStringLength(type_str) > 0) {
host_type_str = core::encode(cx, type_str);
MOZ_ASSERT(host_type_str);
content_type = host_type_str.ptr.get();
content_type = host_type_str;
}
} else if (FormData::is_instance(body_obj)) {
RootedObject encoder(cx, MultipartFormData::create(cx, body_obj));
if (!encoder) {
return false;
}

RootedObject stream(cx, MultipartFormData::encode_stream(cx, encoder));
if (!stream) {
return false;
}

auto boundary = MultipartFormData::boundary(encoder);
auto type = "multipart/form-data; boundary=" + boundary;
host_type_str = string_view(type);

auto length = MultipartFormData::query_length(cx, encoder);
if (length.isErr()) {
return false;
}

content_length = mozilla::Some(length.unwrap());
content_type = host_type_str;

RootedValue stream_val(cx, JS::ObjectValue(*stream));
JS_SetReservedSlot(self, static_cast<uint32_t>(RequestOrResponse::Slots::BodyStream), stream_val);
} else if (body_obj && JS::IsReadableStream(body_obj)) {
if (RequestOrResponse::body_unusable(cx, body_obj)) {
return api::throw_error(cx, FetchErrors::BodyStreamUnusable);
Expand Down Expand Up @@ -364,15 +396,15 @@ bool RequestOrResponse::extract_body(JSContext *cx, JS::HandleObject self,
auto slice = url::URLSearchParams::serialize(cx, body_obj);
buf = (char *)slice.data;
length = slice.len;
content_type = "application/x-www-form-urlencoded;charset=UTF-8";
content_type = "application/x-www-form-urlencoded;charset=UTF-8"sv;
} else {
auto text = core::encode(cx, body_val);
if (!text.ptr) {
return false;
}
buf = text.ptr.release();
length = text.len;
content_type = "text/plain;charset=UTF-8";
content_type = "text/plain;charset=UTF-8"sv;
}

if (!buffer) {
Expand Down Expand Up @@ -413,7 +445,7 @@ bool RequestOrResponse::extract_body(JSContext *cx, JS::HandleObject self,
content_length.emplace(length);
}

if (content_type || content_length.isSome()) {
if (!content_type.empty() || content_length.isSome()) {
JS::RootedObject headers(cx, RequestOrResponse::headers(cx, self));
if (!headers) {
return false;
Expand All @@ -427,7 +459,7 @@ bool RequestOrResponse::extract_body(JSContext *cx, JS::HandleObject self,
}

// Step 36.3 of Request constructor / 8.4 of Response constructor.
if (content_type &&
if (!content_type.empty() &&
!Headers::set_valid_if_undefined(cx, headers, "Content-Type", content_type)) {
return false;
}
Expand Down
5 changes: 5 additions & 0 deletions builtins/web/file.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,11 @@ bool File::lastModified_get(JSContext *cx, unsigned argc, JS::Value *vp) {
return true;
}

JSString *File::name(JSObject *self) {
MOZ_ASSERT(is_instance(self));
return JS::GetReservedSlot(self, static_cast<size_t>(Slots::Name)).toString();
}

// https://w3c.github.io/FileAPI/#file-constructor
bool File::init(JSContext *cx, HandleObject self, HandleValue fileBits, HandleValue fileName,
HandleValue opts) {
Expand Down
2 changes: 2 additions & 0 deletions builtins/web/file.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ class File : public BuiltinImpl<File> {
static const JSFunctionSpec methods[];
static const JSPropertySpec properties[];

static JSString *name(JSObject *self);

static JSObject *create(JSContext *cx, HandleValue fileBits, HandleValue fileName, HandleValue opts);
static bool init(JSContext *cx, HandleObject self, HandleValue fileBits, HandleValue fileName, HandleValue opts);
static bool init_class(JSContext *cx, HandleObject global);
Expand Down
Loading