mirror of
https://github.com/oven-sh/bun
synced 2026-02-13 12:29:07 +00:00
Make request.method getter not allocate memory (#18961)
This commit is contained in:
@@ -561,103 +561,22 @@ static EncodedJSValue assignHeadersFromFetchHeaders(FetchHeaders& impl, JSObject
|
||||
return JSValue::encode(tuple);
|
||||
}
|
||||
|
||||
static void assignHeadersFromUWebSocketsForCall(uWS::HttpRequest* request, MarkedArgumentBuffer& args, JSC::JSGlobalObject* globalObject, JSC::VM& vm)
|
||||
static void assignHeadersFromUWebSocketsForCall(uWS::HttpRequest* request, JSValue methodString, MarkedArgumentBuffer& args, JSC::JSGlobalObject* globalObject, JSC::VM& vm)
|
||||
{
|
||||
auto scope = DECLARE_THROW_SCOPE(vm);
|
||||
std::string_view fullURLStdStr = request->getFullUrl();
|
||||
String fullURL = String::fromUTF8ReplacingInvalidSequences({ reinterpret_cast<const LChar*>(fullURLStdStr.data()), fullURLStdStr.length() });
|
||||
|
||||
// Get the URL.
|
||||
{
|
||||
args.append(jsString(vm, fullURL));
|
||||
std::string_view fullURLStdStr = request->getFullUrl();
|
||||
String fullURL = String::fromUTF8ReplacingInvalidSequences({ reinterpret_cast<const LChar*>(fullURLStdStr.data()), fullURLStdStr.length() });
|
||||
args.append(jsString(vm, WTFMove(fullURL)));
|
||||
}
|
||||
|
||||
// Get the method.
|
||||
{
|
||||
if (UNLIKELY(methodString.isUndefinedOrNull())) {
|
||||
std::string_view methodView = request->getMethod();
|
||||
WTF::String methodString;
|
||||
switch (methodView.length()) {
|
||||
case 3: {
|
||||
if (methodView == std::string_view("get", 3)) {
|
||||
methodString = "GET"_s;
|
||||
break;
|
||||
}
|
||||
if (methodView == std::string_view("put", 3)) {
|
||||
methodString = "PUT"_s;
|
||||
break;
|
||||
}
|
||||
|
||||
break;
|
||||
}
|
||||
case 4: {
|
||||
if (methodView == std::string_view("post", 4)) {
|
||||
methodString = "POST"_s;
|
||||
break;
|
||||
}
|
||||
if (methodView == std::string_view("head", 4)) {
|
||||
methodString = "HEAD"_s;
|
||||
break;
|
||||
}
|
||||
|
||||
if (methodView == std::string_view("copy", 4)) {
|
||||
methodString = "COPY"_s;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
case 5: {
|
||||
if (methodView == std::string_view("patch", 5)) {
|
||||
methodString = "PATCH"_s;
|
||||
break;
|
||||
}
|
||||
if (methodView == std::string_view("merge", 5)) {
|
||||
methodString = "MERGE"_s;
|
||||
break;
|
||||
}
|
||||
if (methodView == std::string_view("trace", 5)) {
|
||||
methodString = "TRACE"_s;
|
||||
break;
|
||||
}
|
||||
if (methodView == std::string_view("fetch", 5)) {
|
||||
methodString = "FETCH"_s;
|
||||
break;
|
||||
}
|
||||
if (methodView == std::string_view("purge", 5)) {
|
||||
methodString = "PURGE"_s;
|
||||
break;
|
||||
}
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
case 6: {
|
||||
if (methodView == std::string_view("delete", 6)) {
|
||||
methodString = "DELETE"_s;
|
||||
break;
|
||||
}
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
case 7: {
|
||||
if (methodView == std::string_view("connect", 7)) {
|
||||
methodString = "CONNECT"_s;
|
||||
break;
|
||||
}
|
||||
if (methodView == std::string_view("options", 7)) {
|
||||
methodString = "OPTIONS"_s;
|
||||
break;
|
||||
}
|
||||
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (methodString.isNull()) {
|
||||
methodString = String::fromUTF8ReplacingInvalidSequences({ reinterpret_cast<const LChar*>(methodView.data()), methodView.length() });
|
||||
}
|
||||
|
||||
args.append(jsString(vm, methodString));
|
||||
WTF::String methodString = String::fromUTF8ReplacingInvalidSequences({ reinterpret_cast<const LChar*>(methodView.data()), methodView.length() });
|
||||
args.append(jsString(vm, WTFMove(methodString)));
|
||||
} else {
|
||||
args.append(methodString);
|
||||
}
|
||||
|
||||
size_t size = 0;
|
||||
@@ -740,12 +659,12 @@ static EncodedJSValue assignHeadersFromUWebSockets(uWS::HttpRequest* request, JS
|
||||
{
|
||||
auto scope = DECLARE_THROW_SCOPE(vm);
|
||||
auto& builtinNames = WebCore::builtinNames(vm);
|
||||
std::string_view fullURLStdStr = request->getFullUrl();
|
||||
String fullURL = String::fromUTF8ReplacingInvalidSequences({ reinterpret_cast<const LChar*>(fullURLStdStr.data()), fullURLStdStr.length() });
|
||||
|
||||
{
|
||||
std::string_view fullURLStdStr = request->getFullUrl();
|
||||
String fullURL = String::fromUTF8ReplacingInvalidSequences({ reinterpret_cast<const LChar*>(fullURLStdStr.data()), fullURLStdStr.length() });
|
||||
PutPropertySlot slot(objectValue, false);
|
||||
objectValue->put(objectValue, globalObject, builtinNames.urlPublicName(), jsString(vm, fullURL), slot);
|
||||
objectValue->put(objectValue, globalObject, builtinNames.urlPublicName(), jsString(vm, WTFMove(fullURL)), slot);
|
||||
RETURN_IF_EXCEPTION(scope, {});
|
||||
}
|
||||
|
||||
@@ -938,6 +857,7 @@ static EncodedJSValue NodeHTTPServer__onRequest(
|
||||
Zig::GlobalObject* globalObject,
|
||||
JSValue thisValue,
|
||||
JSValue callback,
|
||||
JSValue methodString,
|
||||
uWS::HttpRequest* request,
|
||||
uWS::HttpResponse<isSSL>* response,
|
||||
void* upgrade_ctx,
|
||||
@@ -950,7 +870,7 @@ static EncodedJSValue NodeHTTPServer__onRequest(
|
||||
MarkedArgumentBuffer args;
|
||||
args.append(thisValue);
|
||||
|
||||
assignHeadersFromUWebSocketsForCall(request, args, globalObject, vm);
|
||||
assignHeadersFromUWebSocketsForCall(request, methodString, args, globalObject, vm);
|
||||
if (scope.exception()) {
|
||||
auto* exception = scope.exception();
|
||||
response->endWithoutBody();
|
||||
@@ -1185,12 +1105,22 @@ extern "C" EncodedJSValue NodeHTTPServer__onRequest_http(
|
||||
Zig::GlobalObject* globalObject,
|
||||
EncodedJSValue thisValue,
|
||||
EncodedJSValue callback,
|
||||
EncodedJSValue methodString,
|
||||
uWS::HttpRequest* request,
|
||||
uWS::HttpResponse<false>* response,
|
||||
void* upgrade_ctx,
|
||||
void** nodeHttpResponsePtr)
|
||||
{
|
||||
return NodeHTTPServer__onRequest<false>(any_server, globalObject, JSValue::decode(thisValue), JSValue::decode(callback), request, response, upgrade_ctx, nodeHttpResponsePtr);
|
||||
return NodeHTTPServer__onRequest<false>(
|
||||
any_server,
|
||||
globalObject,
|
||||
JSValue::decode(thisValue),
|
||||
JSValue::decode(callback),
|
||||
JSValue::decode(methodString),
|
||||
request,
|
||||
response,
|
||||
upgrade_ctx,
|
||||
nodeHttpResponsePtr);
|
||||
}
|
||||
|
||||
extern "C" EncodedJSValue NodeHTTPServer__onRequest_https(
|
||||
@@ -1198,12 +1128,22 @@ extern "C" EncodedJSValue NodeHTTPServer__onRequest_https(
|
||||
Zig::GlobalObject* globalObject,
|
||||
EncodedJSValue thisValue,
|
||||
EncodedJSValue callback,
|
||||
EncodedJSValue methodString,
|
||||
uWS::HttpRequest* request,
|
||||
uWS::HttpResponse<true>* response,
|
||||
void* upgrade_ctx,
|
||||
void** nodeHttpResponsePtr)
|
||||
{
|
||||
return NodeHTTPServer__onRequest<true>(any_server, globalObject, JSValue::decode(thisValue), JSValue::decode(callback), request, response, upgrade_ctx, nodeHttpResponsePtr);
|
||||
return NodeHTTPServer__onRequest<true>(
|
||||
any_server,
|
||||
globalObject,
|
||||
JSValue::decode(thisValue),
|
||||
JSValue::decode(callback),
|
||||
JSValue::decode(methodString),
|
||||
request,
|
||||
response,
|
||||
upgrade_ctx,
|
||||
nodeHttpResponsePtr);
|
||||
}
|
||||
|
||||
JSC_DEFINE_HOST_FUNCTION(jsHTTPAssignHeaders, (JSGlobalObject * globalObject, CallFrame* callFrame))
|
||||
|
||||
Reference in New Issue
Block a user