From 20f87c15edd30727110af1feda4b334c13452689 Mon Sep 17 00:00:00 2001 From: Wilfried Goesgens Date: Tue, 29 Oct 2019 09:38:04 +0100 Subject: [PATCH] identify buffers and handle them appropriate (#10331) --- arangosh/Shell/V8ClientConnection.cpp | 27 +++++++++++++++++++++++++-- 1 file changed, 25 insertions(+), 2 deletions(-) diff --git a/arangosh/Shell/V8ClientConnection.cpp b/arangosh/Shell/V8ClientConnection.cpp index fc9d3c3c04..df15b3f7a9 100644 --- a/arangosh/Shell/V8ClientConnection.cpp +++ b/arangosh/Shell/V8ClientConnection.cpp @@ -43,6 +43,7 @@ #include "SimpleHttpClient/SimpleHttpResult.h" #include "Ssl/SslInterface.h" #include "V8/v8-conv.h" +#include "V8/v8-buffer.h" #include "V8/v8-utils.h" #include "V8/v8-vpack.h" @@ -1531,12 +1532,23 @@ v8::Local V8ClientConnection::requestData( } req->header.contentType(fuerte::ContentType::Custom); req->addBinary(reinterpret_cast(contents.data()), contents.length()); - } else if (body->IsString()) { // assume JSON + } else if (body->IsString() || body->IsStringObject()) { // assume JSON TRI_Utf8ValueNFC bodyString(isolate, body); req->addBinary(reinterpret_cast(*bodyString), bodyString.length()); if (req->header.contentType() == fuerte::ContentType::Unset) { req->header.contentType(fuerte::ContentType::Json); } + } else if (body->IsObject() && V8Buffer::hasInstance(isolate, body)) { + // supplied body is a Buffer object + char const* data = V8Buffer::data(isolate, body.As()); + size_t size = V8Buffer::length(isolate, body.As()); + + if (data == nullptr) { + TRI_V8_SET_EXCEPTION_MESSAGE(TRI_ERROR_BAD_PARAMETER, + "invalid buffer value"); + return v8::Undefined(isolate); + } + req->addBinary(reinterpret_cast(data), size); } else if (!body->IsNullOrUndefined()) { VPackBuffer buffer; VPackBuilder builder(buffer, &_vpackOptions); @@ -1599,12 +1611,23 @@ v8::Local V8ClientConnection::requestDataRaw( for (auto& pair : headerFields) { req->header.addMeta(std::move(pair.first), std::move(pair.second)); } - if (body->IsString()) { // assume JSON + if (body->IsString() || body->IsStringObject()) { // assume JSON TRI_Utf8ValueNFC bodyString(isolate, body); req->addBinary(reinterpret_cast(*bodyString), bodyString.length()); if (req->header.contentType() == fuerte::ContentType::Unset) { req->header.contentType(fuerte::ContentType::Json); } + } else if (body->IsObject() && V8Buffer::hasInstance(isolate, body)) { + // supplied body is a Buffer object + char const* data = V8Buffer::data(isolate, body.As()); + size_t size = V8Buffer::length(isolate, body.As()); + + if (data == nullptr) { + TRI_V8_SET_EXCEPTION_MESSAGE(TRI_ERROR_BAD_PARAMETER, + "invalid buffer value"); + return v8::Undefined(isolate); + } + req->addBinary(reinterpret_cast(data), size); } else if (!body->IsNullOrUndefined()) { VPackBuffer buffer; VPackBuilder builder(buffer);