1
0
Fork 0
arangodb/arangod/V8Server/v8-actions.cpp

1394 lines
44 KiB
C++

////////////////////////////////////////////////////////////////////////////////
/// DISCLAIMER
///
/// Copyright 2014-2016 ArangoDB GmbH, Cologne, Germany
/// Copyright 2004-2014 triAGENS GmbH, Cologne, Germany
///
/// Licensed under the Apache License, Version 2.0 (the "License");
/// you may not use this file except in compliance with the License.
/// You may obtain a copy of the License at
///
/// http://www.apache.org/licenses/LICENSE-2.0
///
/// Unless required by applicable law or agreed to in writing, software
/// distributed under the License is distributed on an "AS IS" BASIS,
/// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
/// See the License for the specific language governing permissions and
/// limitations under the License.
///
/// Copyright holder is ArangoDB GmbH, Cologne, Germany
///
/// @author Dr. Frank Celler
////////////////////////////////////////////////////////////////////////////////
#include "v8-actions.h"
#include "Actions/ActionFeature.h"
#include "Actions/actions.h"
#include "Basics/MutexLocker.h"
#include "Basics/ReadLocker.h"
#include "Basics/StringUtils.h"
#include "Basics/WriteLocker.h"
#include "Basics/conversions.h"
#include "Basics/files.h"
#include "Basics/json.h"
#include "Basics/tri-strings.h"
#include "Cluster/ClusterComm.h"
#include "Cluster/ServerState.h"
#include "HttpServer/HttpServer.h"
#include "Logger/Logger.h"
#include "Rest/GeneralRequest.h"
#include "Rest/HttpRequest.h"
#include "Rest/HttpResponse.h"
#include "RestServer/VocbaseContext.h"
#include "V8/v8-buffer.h"
#include "V8/v8-conv.h"
#include "V8/v8-utils.h"
#include "V8Server/V8Context.h"
#include "V8Server/V8DealerFeature.h"
#include "V8Server/v8-vocbase.h"
#include "VocBase/server.h"
#include "VocBase/vocbase.h"
using namespace arangodb;
using namespace arangodb::basics;
using namespace arangodb::rest;
static TRI_action_result_t ExecuteActionVocbase(
TRI_vocbase_t*, v8::Isolate*, TRI_action_t const*,
v8::Handle<v8::Function> callback, GeneralRequest*, GeneralResponse*);
////////////////////////////////////////////////////////////////////////////////
/// @brief action description for V8
////////////////////////////////////////////////////////////////////////////////
class v8_action_t : public TRI_action_t {
public:
v8_action_t() : TRI_action_t(), _callbacks(), _callbacksLock() {
_type = "JAVASCRIPT";
}
//////////////////////////////////////////////////////////////////////////////
/// @brief creates callback for a context
//////////////////////////////////////////////////////////////////////////////
void createCallback(v8::Isolate* isolate, v8::Handle<v8::Function> callback) {
WRITE_LOCKER(writeLocker, _callbacksLock);
auto it = _callbacks.find(isolate);
if (it != _callbacks.end()) {
it->second.Reset();
}
_callbacks[isolate].Reset(isolate, callback);
}
TRI_action_result_t execute(TRI_vocbase_t* vocbase, GeneralRequest* request,
GeneralResponse* response, Mutex* dataLock,
void** data) override {
TRI_action_result_t result;
// allow use datase execution in rest calls
bool allowUseDatabaseInRestActions =
ActionFeature::ACTION->allowUseDatabase();
if (_allowUseDatabase) {
allowUseDatabaseInRestActions = true;
}
// this is for TESTING / DEBUGGING only - do not document this feature
ssize_t forceContext = -1;
bool found;
std::string const& c = request->header("x-arango-v8-context", found);
if (found && !c.empty()) {
forceContext = StringUtils::int32(c);
}
// get a V8 context
V8Context* context = V8DealerFeature::DEALER->enterContext(
vocbase, allowUseDatabaseInRestActions, forceContext);
// note: the context might be nullptr in case of shut-down
if (context == nullptr) {
return result;
}
TRI_DEFER(V8DealerFeature::DEALER->exitContext(context));
// locate the callback
READ_LOCKER(readLocker, _callbacksLock);
{
auto it = _callbacks.find(context->_isolate);
if (it == _callbacks.end()) {
LOG(WARN) << "no callback function for JavaScript action '" << _url
<< "'";
result.isValid = true;
response->setResponseCode(GeneralResponse::ResponseCode::NOT_FOUND);
return result;
}
// and execute it
{
MUTEX_LOCKER(mutexLocker, *dataLock);
if (*data != nullptr) {
result.canceled = true;
return result;
}
*data = (void*)context->_isolate;
}
v8::HandleScope scope(context->_isolate);
auto localFunction =
v8::Local<v8::Function>::New(context->_isolate, it->second);
try {
result = ExecuteActionVocbase(vocbase, context->_isolate, this,
localFunction, request, response);
} catch (...) {
result.isValid = false;
}
{
MUTEX_LOCKER(mutexLocker, *dataLock);
*data = nullptr;
}
}
return result;
}
bool cancel(Mutex* dataLock, void** data) override {
{
MUTEX_LOCKER(mutexLocker, *dataLock);
// either we have not yet reached the execute above or we are already done
if (*data == nullptr) {
*data = (void*)1; // mark as canceled
}
// data is set, cancel the execution
else {
if (!v8::V8::IsExecutionTerminating((v8::Isolate*)*data)) {
v8::V8::TerminateExecution((v8::Isolate*)*data);
}
}
}
return true;
}
private:
//////////////////////////////////////////////////////////////////////////////
/// @brief callback dictionary
//////////////////////////////////////////////////////////////////////////////
std::map<v8::Isolate*, v8::Persistent<v8::Function>> _callbacks;
//////////////////////////////////////////////////////////////////////////////
/// @brief lock for the callback dictionary
//////////////////////////////////////////////////////////////////////////////
ReadWriteLock _callbacksLock;
};
////////////////////////////////////////////////////////////////////////////////
/// @brief parses the action options
////////////////////////////////////////////////////////////////////////////////
static void ParseActionOptions(v8::Isolate* isolate, TRI_v8_global_t* v8g,
TRI_action_t* action,
v8::Handle<v8::Object> options) {
TRI_GET_GLOBAL_STRING(PrefixKey);
// check the "prefix" field
if (options->Has(PrefixKey)) {
action->_isPrefix = TRI_ObjectToBoolean(options->Get(PrefixKey));
} else {
action->_isPrefix = false;
}
// check the "allowUseDatabase" field
TRI_GET_GLOBAL_STRING(AllowUseDatabaseKey);
if (options->Has(AllowUseDatabaseKey)) {
action->_allowUseDatabase =
TRI_ObjectToBoolean(options->Get(AllowUseDatabaseKey));
} else {
action->_allowUseDatabase = false;
}
}
////////////////////////////////////////////////////////////////////////////////
/// @brief add cookie
////////////////////////////////////////////////////////////////////////////////
static void AddCookie(v8::Isolate* isolate, TRI_v8_global_t const* v8g,
HttpResponse* response, v8::Handle<v8::Object> data) {
std::string name;
std::string value;
int lifeTimeSeconds = 0;
std::string path = "/";
std::string domain = "";
bool secure = false;
bool httpOnly = false;
TRI_GET_GLOBAL_STRING(NameKey);
if (data->Has(NameKey)) {
v8::Handle<v8::Value> v = data->Get(NameKey);
name = TRI_ObjectToString(v);
} else {
// something is wrong here
return;
}
TRI_GET_GLOBAL_STRING(ValueKey);
if (data->Has(ValueKey)) {
v8::Handle<v8::Value> v = data->Get(ValueKey);
value = TRI_ObjectToString(v);
} else {
// something is wrong here
return;
}
TRI_GET_GLOBAL_STRING(LifeTimeKey);
if (data->Has(LifeTimeKey)) {
v8::Handle<v8::Value> v = data->Get(LifeTimeKey);
lifeTimeSeconds = (int)TRI_ObjectToInt64(v);
}
TRI_GET_GLOBAL_STRING(PathKey);
if (data->Has(PathKey) && !data->Get(PathKey)->IsUndefined()) {
v8::Handle<v8::Value> v = data->Get(PathKey);
path = TRI_ObjectToString(v);
}
TRI_GET_GLOBAL_STRING(DomainKey);
if (data->Has(DomainKey) && !data->Get(DomainKey)->IsUndefined()) {
v8::Handle<v8::Value> v = data->Get(DomainKey);
domain = TRI_ObjectToString(v);
}
TRI_GET_GLOBAL_STRING(SecureKey);
if (data->Has(SecureKey)) {
v8::Handle<v8::Value> v = data->Get(SecureKey);
secure = TRI_ObjectToBoolean(v);
}
TRI_GET_GLOBAL_STRING(HttpOnlyKey);
if (data->Has(HttpOnlyKey)) {
v8::Handle<v8::Value> v = data->Get(HttpOnlyKey);
httpOnly = TRI_ObjectToBoolean(v);
}
response->setCookie(name, value, lifeTimeSeconds, path, domain, secure,
httpOnly);
}
////////////////////////////////////////////////////////////////////////////////
/// @brief convert a C++ HttpRequest to a V8 request object
////////////////////////////////////////////////////////////////////////////////
static v8::Handle<v8::Object> RequestCppToV8(v8::Isolate* isolate,
TRI_v8_global_t const* v8g,
GeneralRequest* request) {
// setup the request
v8::Handle<v8::Object> req = v8::Object::New(isolate);
//auto request = dynamic_cast<HttpRequest*>(generalRequest);
// TODO generalize
if (request == nullptr) {
return req;
}
// Example:
// {
// path : "/full/path/suffix1/suffix2",
//
// prefix : "/full/path",
//
// "suffix" : [
// "suffix1",
// "suffix2"
// ],
//
// "parameters" : {
// "init" : "true"
// },
//
// "headers" : {
// "accept" : "text/html",
// "accept-encoding" : "gzip, deflate",
// "accept-language" : "de-de,en-us;q=0.7,en;q=0.3",
// "user-agent" : "Mozilla/5.0"
// },
//
// "cookies" : {
// "ARANGODB_SESSION_ID" : "0cwuzusd23nw3qiwui84uwqwqw23e"
// },
//
// "requestType" : "GET",
// "requestBody" : "... only for PUT and POST ...",
// "user" : "authenticatedUser"
// }
// create user or null
std::string const& user = request->user();
TRI_GET_GLOBAL_STRING(UserKey);
if (user.empty()) {
req->ForceSet(UserKey, v8::Null(isolate));
} else {
req->ForceSet(UserKey, TRI_V8_STD_STRING(user));
}
// create database attribute
std::string const& database = request->databaseName();
TRI_ASSERT(!database.empty());
TRI_GET_GLOBAL_STRING(DatabaseKey);
req->ForceSet(DatabaseKey, TRI_V8_STD_STRING(database));
// set the full url
std::string const& fullUrl = request->fullUrl();
TRI_GET_GLOBAL_STRING(UrlKey);
req->ForceSet(UrlKey, TRI_V8_STD_STRING(fullUrl));
// set the protocol
char const* protocol = request->protocol();
TRI_GET_GLOBAL_STRING(ProtocolKey);
req->ForceSet(ProtocolKey, TRI_V8_ASCII_STRING(protocol));
// set the task id
std::string const taskId(StringUtils::itoa(request->clientTaskId()));
// set the connection info
const ConnectionInfo& info = request->connectionInfo();
v8::Handle<v8::Object> serverArray = v8::Object::New(isolate);
TRI_GET_GLOBAL_STRING(AddressKey);
serverArray->ForceSet(AddressKey, TRI_V8_STD_STRING(info.serverAddress));
TRI_GET_GLOBAL_STRING(PortKey);
serverArray->ForceSet(PortKey, v8::Number::New(isolate, info.serverPort));
TRI_GET_GLOBAL_STRING(ServerKey);
req->ForceSet(ServerKey, serverArray);
TRI_GET_GLOBAL_STRING(PortTypeKey);
req->ForceSet(
PortTypeKey, TRI_V8_STD_STRING(info.portType()),
static_cast<v8::PropertyAttribute>(v8::ReadOnly | v8::DontEnum));
v8::Handle<v8::Object> clientArray = v8::Object::New(isolate);
clientArray->ForceSet(AddressKey, TRI_V8_STD_STRING(info.clientAddress));
clientArray->ForceSet(PortKey, v8::Number::New(isolate, info.clientPort));
TRI_GET_GLOBAL_STRING(IdKey);
clientArray->ForceSet(IdKey, TRI_V8_STD_STRING(taskId));
TRI_GET_GLOBAL_STRING(ClientKey);
req->ForceSet(ClientKey, clientArray);
req->ForceSet(TRI_V8_ASCII_STRING("internals"),
v8::External::New(isolate, request));
// copy prefix
std::string path = request->prefix();
TRI_GET_GLOBAL_STRING(PrefixKey);
req->ForceSet(PrefixKey, TRI_V8_STD_STRING(path));
// copy header fields
v8::Handle<v8::Object> headerFields = v8::Object::New(isolate);
auto headers = request->headers();
headers["content-length"] = StringUtils::itoa(request->contentLength());
for (auto const& it : headers) {
headerFields->ForceSet(TRI_V8_STD_STRING(it.first),
TRI_V8_STD_STRING(it.second));
}
TRI_GET_GLOBAL_STRING(HeadersKey);
req->ForceSet(HeadersKey, headerFields);
TRI_GET_GLOBAL_STRING(RequestTypeKey);
TRI_GET_GLOBAL_STRING(RequestBodyKey);
// copy request type
switch (request->requestType()) {
case GeneralRequest::RequestType::POST: {
TRI_GET_GLOBAL_STRING(PostConstant);
req->ForceSet(RequestTypeKey, PostConstant);
req->ForceSet(RequestBodyKey, TRI_V8_STD_STRING(request->body()));
break;
}
case GeneralRequest::RequestType::PUT: {
TRI_GET_GLOBAL_STRING(PutConstant);
req->ForceSet(RequestTypeKey, PutConstant);
req->ForceSet(RequestBodyKey, TRI_V8_STD_STRING(request->body()));
break;
}
case GeneralRequest::RequestType::PATCH: {
TRI_GET_GLOBAL_STRING(PatchConstant);
req->ForceSet(RequestTypeKey, PatchConstant);
req->ForceSet(RequestBodyKey, TRI_V8_STD_STRING(request->body()));
break;
}
case GeneralRequest::RequestType::OPTIONS: {
TRI_GET_GLOBAL_STRING(OptionsConstant);
req->ForceSet(RequestTypeKey, OptionsConstant);
break;
}
case GeneralRequest::RequestType::DELETE_REQ: {
TRI_GET_GLOBAL_STRING(DeleteConstant);
req->ForceSet(RequestTypeKey, DeleteConstant);
break;
}
case GeneralRequest::RequestType::HEAD: {
TRI_GET_GLOBAL_STRING(HeadConstant);
req->ForceSet(RequestTypeKey, HeadConstant);
break;
}
case GeneralRequest::RequestType::GET: {
default:
TRI_GET_GLOBAL_STRING(GetConstant);
req->ForceSet(RequestTypeKey, GetConstant);
break;
}
}
// copy request parameter
v8::Handle<v8::Object> valuesObject = v8::Object::New(isolate);
for (auto& it : request->values()) {
valuesObject->ForceSet(TRI_V8_STD_STRING(it.first),
TRI_V8_STD_STRING(it.second));
}
// copy request array parameter (a[]=1&a[]=2&...)
for (auto& arrayValue : request->arrayValues()) {
std::string const& k = arrayValue.first;
std::vector<std::string> const& v = arrayValue.second;
v8::Handle<v8::Array> list =
v8::Array::New(isolate, static_cast<int>(v.size()));
for (size_t i = 0; i < v.size(); ++i) {
list->Set((uint32_t)i, TRI_V8_STD_STRING(v[i]));
}
valuesObject->ForceSet(TRI_V8_STD_STRING(k), list);
}
TRI_GET_GLOBAL_STRING(ParametersKey);
req->ForceSet(ParametersKey, valuesObject);
// copy cookies
v8::Handle<v8::Object> cookiesObject = v8::Object::New(isolate);
for (auto& it : request->cookieValues()) {
cookiesObject->ForceSet(TRI_V8_STD_STRING(it.first),
TRI_V8_STD_STRING(it.second));
}
TRI_GET_GLOBAL_STRING(CookiesKey);
req->ForceSet(CookiesKey, cookiesObject);
return req;
}
////////////////////////////////////////////////////////////////////////////////
/// @brief convert a C++ HttpRequest to a V8 request object
////////////////////////////////////////////////////////////////////////////////
// TODO this needs to be generalized
static void ResponseV8ToCpp(v8::Isolate* isolate, TRI_v8_global_t const* v8g,
v8::Handle<v8::Object> const res,
HttpResponse* response) {
GeneralResponse::ResponseCode code = GeneralResponse::ResponseCode::OK;
TRI_GET_GLOBAL_STRING(ResponseCodeKey);
if (res->Has(ResponseCodeKey)) {
// Windows has issues with converting from a double to an enumeration type
code = (GeneralResponse::ResponseCode)(
(int)(TRI_ObjectToDouble(res->Get(ResponseCodeKey))));
}
response->setResponseCode(code);
TRI_GET_GLOBAL_STRING(ContentTypeKey);
if (res->Has(ContentTypeKey)) {
response->setContentType(TRI_ObjectToString(res->Get(ContentTypeKey)));
}
// .........................................................................
// body
// .........................................................................
TRI_GET_GLOBAL_STRING(BodyKey);
TRI_GET_GLOBAL_STRING(BodyFromFileKey);
TRI_GET_GLOBAL_STRING(HeadersKey);
TRI_GET_GLOBAL_STRING(CookiesKey);
if (res->Has(BodyKey)) {
// check if we should apply result transformations
// transformations turn the result from one type into another
// a Javascript action can request transformations by
// putting a list of transformations into the res.transformations
// array, e.g. res.transformations = [ "base64encode" ]
TRI_GET_GLOBAL_STRING(TransformationsKey);
v8::Handle<v8::Value> val = res->Get(TransformationsKey);
if (val->IsArray()) {
TRI_GET_GLOBAL_STRING(BodyKey);
std::string out(TRI_ObjectToString(res->Get(BodyKey)));
v8::Handle<v8::Array> transformations = val.As<v8::Array>();
for (uint32_t i = 0; i < transformations->Length(); i++) {
v8::Handle<v8::Value> transformator =
transformations->Get(v8::Integer::New(isolate, i));
std::string name = TRI_ObjectToString(transformator);
// check available transformations
if (name == "base64encode") {
// base64-encode the result
out = StringUtils::encodeBase64(out);
// set the correct content-encoding header
response->setHeaderNC(StaticStrings::ContentEncoding,
StaticStrings::Base64);
} else if (name == "base64decode") {
// base64-decode the result
out = StringUtils::decodeBase64(out);
// set the correct content-encoding header
response->setHeaderNC(StaticStrings::ContentEncoding,
StaticStrings::Binary);
}
}
response->body().appendText(out);
} else {
TRI_GET_GLOBAL_STRING(BodyKey);
v8::Handle<v8::Value> b = res->Get(BodyKey);
if (V8Buffer::hasInstance(isolate, b)) {
// body is a Buffer
auto obj = b.As<v8::Object>();
response->body().appendText(V8Buffer::data(obj), V8Buffer::length(obj));
} else {
// treat body as a string
response->body().appendText(TRI_ObjectToString(res->Get(BodyKey)));
}
}
}
// .........................................................................
// body from file
// .........................................................................
else if (res->Has(BodyFromFileKey)) {
TRI_Utf8ValueNFC filename(TRI_UNKNOWN_MEM_ZONE, res->Get(BodyFromFileKey));
size_t length;
char* content = TRI_SlurpFile(TRI_UNKNOWN_MEM_ZONE, *filename, &length);
if (content != nullptr) {
response->body().appendText(content, length);
TRI_FreeString(TRI_UNKNOWN_MEM_ZONE, content);
} else {
std::string msg = std::string("cannot read file '") + *filename + "': " +
TRI_last_error();
response->body().appendText(msg.c_str(), msg.size());
response->setResponseCode(GeneralResponse::ResponseCode::SERVER_ERROR);
}
}
// .........................................................................
// headers
// .........................................................................
if (res->Has(HeadersKey)) {
v8::Handle<v8::Value> val = res->Get(HeadersKey);
v8::Handle<v8::Object> v8Headers = val.As<v8::Object>();
if (v8Headers->IsObject()) {
v8::Handle<v8::Array> props = v8Headers->GetPropertyNames();
for (uint32_t i = 0; i < props->Length(); i++) {
v8::Handle<v8::Value> key = props->Get(v8::Integer::New(isolate, i));
response->setHeader(TRI_ObjectToString(key),
TRI_ObjectToString(v8Headers->Get(key)));
}
}
}
// .........................................................................
// cookies
// .........................................................................
if (res->Has(CookiesKey)) {
v8::Handle<v8::Value> val = res->Get(CookiesKey);
v8::Handle<v8::Object> v8Cookies = val.As<v8::Object>();
if (v8Cookies->IsArray()) {
v8::Handle<v8::Array> v8Array = v8Cookies.As<v8::Array>();
for (uint32_t i = 0; i < v8Array->Length(); i++) {
v8::Handle<v8::Value> v8Cookie = v8Array->Get(i);
if (v8Cookie->IsObject()) {
AddCookie(isolate, v8g, response, v8Cookie.As<v8::Object>());
}
}
} else if (v8Cookies->IsObject()) {
// one cookie
AddCookie(isolate, v8g, response, v8Cookies);
}
}
}
////////////////////////////////////////////////////////////////////////////////
/// @brief executes an action
////////////////////////////////////////////////////////////////////////////////
static TRI_action_result_t ExecuteActionVocbase(
TRI_vocbase_t* vocbase, v8::Isolate* isolate, TRI_action_t const* action,
v8::Handle<v8::Function> callback, GeneralRequest* request,
GeneralResponse* responseGeneral) {
v8::HandleScope scope(isolate);
v8::TryCatch tryCatch;
// TODO needs to generalized
auto response = dynamic_cast<HttpResponse*>(responseGeneral);
if (response == nullptr) {
THROW_ARANGO_EXCEPTION(TRI_ERROR_INTERNAL);
}
TRI_GET_GLOBALS();
v8::Handle<v8::Object> req = RequestCppToV8(isolate, v8g, request);
// copy suffix, which comes from the action:
std::string path = request->prefix();
v8::Handle<v8::Array> suffixArray = v8::Array::New(isolate);
std::vector<std::string> const& suffix = request->suffix();
uint32_t index = 0;
char const* sep = "";
for (size_t s = action->_urlParts; s < suffix.size(); ++s) {
suffixArray->Set(index++, TRI_V8_STD_STRING(suffix[s]));
path += sep + suffix[s];
sep = "/";
}
TRI_GET_GLOBAL_STRING(SuffixKey);
req->ForceSet(SuffixKey, suffixArray);
// copy full path
TRI_GET_GLOBAL_STRING(PathKey);
req->ForceSet(PathKey, TRI_V8_STD_STRING(path));
// create the response object
v8::Handle<v8::Object> res = v8::Object::New(isolate);
// register request & response in the context
v8g->_currentRequest = req;
v8g->_currentResponse = res;
// execute the callback
v8::Handle<v8::Value> args[2] = {req, res};
// handle C++ exceptions that happen during dynamic script execution
int errorCode;
std::string errorMessage;
try {
callback->Call(callback, 2, args);
errorCode = TRI_ERROR_NO_ERROR;
} catch (arangodb::basics::Exception const& ex) {
errorCode = ex.code();
errorMessage = ex.what();
} catch (std::bad_alloc const&) {
errorCode = TRI_ERROR_OUT_OF_MEMORY;
} catch (...) {
errorCode = TRI_ERROR_INTERNAL;
}
// invalidate request / response objects
v8g->_currentRequest = v8::Undefined(isolate);
v8g->_currentResponse = v8::Undefined(isolate);
// convert the result
TRI_action_result_t result;
result.isValid = true;
if (errorCode != TRI_ERROR_NO_ERROR) {
result.isValid = false;
result.canceled = false;
// TODO how to generalize this?
response->setResponseCode(GeneralResponse::ResponseCode::SERVER_ERROR);
if (errorMessage.empty()) {
errorMessage = TRI_errno_string(errorCode);
}
response->body().appendText(errorMessage);
}
else if (v8g->_canceled) {
result.isValid = false;
result.canceled = true;
}
else if (tryCatch.HasCaught()) {
if (tryCatch.CanContinue()) {
response->setResponseCode(GeneralResponse::ResponseCode::SERVER_ERROR);
// TODO how to generalize this?
response->body().appendText(TRI_StringifyV8Exception(isolate, &tryCatch));
} else {
v8g->_canceled = true;
result.isValid = false;
result.canceled = true;
}
}
else {
ResponseV8ToCpp(isolate, v8g, res, response);
}
return result;
}
////////////////////////////////////////////////////////////////////////////////
/// @brief defines a new action
///
/// @FUN{internal.defineAction(@FA{name}, @FA{callback}, @FA{parameter})}
////////////////////////////////////////////////////////////////////////////////
static void JS_DefineAction(v8::FunctionCallbackInfo<v8::Value> const& args) {
TRI_V8_TRY_CATCH_BEGIN(isolate);
v8::HandleScope scope(isolate);
TRI_GET_GLOBALS();
if (args.Length() != 3) {
TRI_V8_THROW_EXCEPTION_USAGE(
"defineAction(<name>, <callback>, <parameter>)");
}
// extract the action name
TRI_Utf8ValueNFC utf8name(TRI_UNKNOWN_MEM_ZONE, args[0]);
if (*utf8name == 0) {
TRI_V8_THROW_TYPE_ERROR("<name> must be an UTF-8 string");
}
std::string name = *utf8name;
// extract the action callback
if (!args[1]->IsFunction()) {
TRI_V8_THROW_TYPE_ERROR("<callback> must be a function");
}
v8::Handle<v8::Function> callback = v8::Handle<v8::Function>::Cast(args[1]);
// extract the options
v8::Handle<v8::Object> options;
if (args[2]->IsObject()) {
options = args[2]->ToObject();
} else {
options = v8::Object::New(isolate);
}
// create an action with the given options
v8_action_t* action = new v8_action_t();
ParseActionOptions(isolate, v8g, action, options);
// store an action with the given name
TRI_action_t* result = TRI_DefineActionVocBase(name, action);
// and define the callback
if (result != nullptr) {
action = dynamic_cast<v8_action_t*>(result);
if (action != nullptr) {
action->createCallback(isolate, callback);
} else {
LOG(ERR) << "cannot create callback for V8 action";
}
} else {
LOG(ERR) << "cannot define V8 action";
}
TRI_V8_RETURN_UNDEFINED();
TRI_V8_TRY_CATCH_END
}
////////////////////////////////////////////////////////////////////////////////
/// @brief eventually executes a function in all contexts
///
/// @FUN{internal.executeGlobalContextFunction(@FA{function-definition})}
////////////////////////////////////////////////////////////////////////////////
static void JS_ExecuteGlobalContextFunction(
v8::FunctionCallbackInfo<v8::Value> const& args) {
TRI_V8_TRY_CATCH_BEGIN(isolate);
v8::HandleScope scope(isolate);
if (args.Length() != 1) {
TRI_V8_THROW_EXCEPTION_USAGE(
"executeGlobalContextFunction(<function-type>)");
}
// extract the action name
v8::String::Utf8Value utf8def(args[0]);
if (*utf8def == 0) {
TRI_V8_THROW_TYPE_ERROR("<definition> must be a UTF-8 function definition");
}
std::string const def = *utf8def;
// and pass it to the V8 contexts
if (!V8DealerFeature::DEALER->addGlobalContextMethod(def)) {
TRI_V8_THROW_EXCEPTION_MESSAGE(TRI_ERROR_INTERNAL,
"invalid action definition");
}
TRI_V8_RETURN_UNDEFINED();
TRI_V8_TRY_CATCH_END
}
////////////////////////////////////////////////////////////////////////////////
/// @brief get the current request
///
/// @FUN{internal.getCurrentRequest()}
////////////////////////////////////////////////////////////////////////////////
static void JS_GetCurrentRequest(
v8::FunctionCallbackInfo<v8::Value> const& args) {
TRI_V8_TRY_CATCH_BEGIN(isolate);
v8::HandleScope scope(isolate);
TRI_GET_GLOBALS();
if (args.Length() != 0) {
TRI_V8_THROW_EXCEPTION_USAGE("getCurrentRequest()");
}
TRI_V8_RETURN(v8g->_currentRequest);
TRI_V8_TRY_CATCH_END
}
////////////////////////////////////////////////////////////////////////////////
/// @brief get the raw body of the current request
///
/// @FUN{internal.rawRequestBody()}
////////////////////////////////////////////////////////////////////////////////
static void JS_RawRequestBody(v8::FunctionCallbackInfo<v8::Value> const& args) {
TRI_V8_TRY_CATCH_BEGIN(isolate);
v8::HandleScope scope(isolate);
if (args.Length() != 1) {
TRI_V8_THROW_EXCEPTION_USAGE("rawRequestBody(req)");
}
v8::Handle<v8::Value> current = args[0];
if (current->IsObject()) {
v8::Handle<v8::Object> obj = v8::Handle<v8::Object>::Cast(current);
v8::Handle<v8::Value> property = obj->Get(TRI_V8_ASCII_STRING("internals"));
if (property->IsExternal()) {
v8::Handle<v8::External> e = v8::Handle<v8::External>::Cast(property);
auto request = static_cast<arangodb::HttpRequest*>(e->Value());
if (request != nullptr) {
std::string bodyStr = request->body();
V8Buffer* buffer =
V8Buffer::New(isolate, bodyStr.c_str(), bodyStr.size());
TRI_V8_RETURN(buffer->_handle);
}
}
}
TRI_V8_RETURN_UNDEFINED();
TRI_V8_TRY_CATCH_END
}
////////////////////////////////////////////////////////////////////////////////
/// @brief get the raw body of the current request
///
/// @FUN{internal.rawRequestBody()}
////////////////////////////////////////////////////////////////////////////////
static void JS_RequestParts(v8::FunctionCallbackInfo<v8::Value> const& args) {
TRI_V8_TRY_CATCH_BEGIN(isolate);
v8::HandleScope scope(isolate);
if (args.Length() != 1) {
TRI_V8_THROW_EXCEPTION_USAGE("requestParts(req)");
}
v8::Handle<v8::Value> current = args[0];
if (current->IsObject()) {
v8::Handle<v8::Object> obj = v8::Handle<v8::Object>::Cast(current);
v8::Handle<v8::Value> property = obj->Get(TRI_V8_ASCII_STRING("internals"));
if (property->IsExternal()) {
v8::Handle<v8::External> e = v8::Handle<v8::External>::Cast(property);
auto request = static_cast<arangodb::HttpRequest*>(e->Value());
std::string const& bodyStr = request->body();
char const* beg = bodyStr.c_str();
char const* end = beg + bodyStr.size();
while (beg < end && (*beg == '\r' || *beg == '\n' || *beg == ' ')) {
++beg;
}
// find delimiter
char const* ptr = beg;
while (ptr < end && *ptr == '-') {
++ptr;
}
while (ptr < end && *ptr != '\r' && *ptr != '\n') {
++ptr;
}
if (ptr == beg) {
// oops
TRI_V8_THROW_EXCEPTION_PARAMETER("request is no multipart request");
}
std::string const delimiter(beg, ptr - beg);
if (ptr < end && *ptr == '\r') {
++ptr;
}
if (ptr < end && *ptr == '\n') {
++ptr;
}
std::vector<std::pair<char const*, size_t>> parts;
while (ptr < end) {
char const* p = TRI_IsContainedMemory(ptr, end - ptr, delimiter.c_str(),
delimiter.size());
if (p == nullptr || p + delimiter.size() + 2 >= end || p - 2 <= ptr) {
TRI_V8_THROW_EXCEPTION_PARAMETER("bad request data");
}
char const* q = p;
if (*(q - 1) == '\n') {
--q;
}
if (*(q - 1) == '\r') {
--q;
}
parts.push_back(std::make_pair(ptr, q - ptr));
ptr = p + delimiter.size();
if (*ptr == '-' && *(ptr + 1) == '-') {
// eom
break;
}
if (*ptr == '\r') {
++ptr;
}
if (ptr < end && *ptr == '\n') {
++ptr;
}
}
v8::Handle<v8::Array> result = v8::Array::New(isolate);
uint32_t j = 0;
for (auto& part : parts) {
v8::Handle<v8::Object> headersObject = v8::Object::New(isolate);
auto ptr = part.first;
auto end = part.first + part.second;
char const* data = nullptr;
while (ptr < end) {
while (ptr < end && *ptr == ' ') {
++ptr;
}
if (ptr < end && (*ptr == '\r' || *ptr == '\n')) {
// end of headers
if (*ptr == '\r') {
++ptr;
}
if (ptr < end && *ptr == '\n') {
++ptr;
}
data = ptr;
break;
}
// header line
char const* eol = TRI_IsContainedMemory(ptr, end - ptr, "\r\n", 2);
if (eol == nullptr) {
eol = TRI_IsContainedMemory(ptr, end - ptr, "\n", 1);
}
if (eol == nullptr) {
TRI_V8_THROW_EXCEPTION_PARAMETER("bad request data");
}
char const* colon = TRI_IsContainedMemory(ptr, end - ptr, ":", 1);
if (colon == nullptr) {
TRI_V8_THROW_EXCEPTION_PARAMETER("bad request data");
}
char const* p = colon;
while (p > ptr && *(p - 1) == ' ') {
--p;
}
++colon;
while (colon < eol && *colon == ' ') {
++colon;
}
char const* q = eol;
while (q > ptr && *(q - 1) == ' ') {
--q;
}
headersObject->Set(TRI_V8_PAIR_STRING(ptr, (int)(p - ptr)),
TRI_V8_PAIR_STRING(colon, (int)(eol - colon)));
ptr = eol;
if (*ptr == '\r') {
++ptr;
}
if (ptr < end && *ptr == '\n') {
++ptr;
}
}
if (data == nullptr) {
TRI_V8_THROW_EXCEPTION_PARAMETER("bad request data");
}
v8::Handle<v8::Object> partObject = v8::Object::New(isolate);
partObject->Set(TRI_V8_ASCII_STRING("headers"), headersObject);
V8Buffer* buffer = V8Buffer::New(isolate, data, end - data);
auto localHandle = v8::Local<v8::Object>::New(isolate, buffer->_handle);
partObject->Set(TRI_V8_ASCII_STRING("data"), localHandle);
result->Set(j++, partObject);
}
TRI_V8_RETURN(result);
}
}
TRI_V8_RETURN_UNDEFINED();
TRI_V8_TRY_CATCH_END
}
////////////////////////////////////////////////////////////////////////////////
/// @brief get the current response
///
/// @FUN{internal.getCurrentRequest()}
////////////////////////////////////////////////////////////////////////////////
static void JS_GetCurrentResponse(
v8::FunctionCallbackInfo<v8::Value> const& args) {
TRI_V8_TRY_CATCH_BEGIN(isolate);
v8::HandleScope scope(isolate);
TRI_GET_GLOBALS();
if (args.Length() != 0) {
TRI_V8_THROW_EXCEPTION_USAGE("getCurrentResponse()");
}
TRI_V8_RETURN(v8g->_currentResponse);
TRI_V8_TRY_CATCH_END
}
////////////////////////////////////////////////////////////////////////////////
/// @brief sendChunk
////////////////////////////////////////////////////////////////////////////////
static void JS_SendChunk(v8::FunctionCallbackInfo<v8::Value> const& args) {
TRI_V8_TRY_CATCH_BEGIN(isolate);
v8::HandleScope scope(isolate);
if (args.Length() != 2) {
TRI_V8_THROW_EXCEPTION_USAGE("sendChunk(<id>, <value>)");
}
TRI_Utf8ValueNFC idStr(TRI_UNKNOWN_MEM_ZONE, args[0]);
uint64_t id = StringUtils::uint64(*idStr);
TRI_Utf8ValueNFC data(TRI_UNKNOWN_MEM_ZONE, args[1]);
int res = HttpServer::sendChunk(id, *data);
if (res != TRI_ERROR_NO_ERROR && res != TRI_ERROR_TASK_NOT_FOUND) {
TRI_V8_THROW_EXCEPTION_MESSAGE(TRI_ERROR_INTERNAL, "cannot send chunk");
}
TRI_V8_RETURN(res == TRI_ERROR_NO_ERROR ? v8::True(isolate)
: v8::False(isolate));
TRI_V8_TRY_CATCH_END
}
////////////////////////////////////////////////////////////////////////////////
/// @brief stores the V8 actions function inside the global variable
////////////////////////////////////////////////////////////////////////////////
void TRI_InitV8Actions(v8::Isolate* isolate, v8::Handle<v8::Context> context) {
v8::HandleScope scope(isolate);
// .............................................................................
// create the global functions
// .............................................................................
TRI_AddGlobalFunctionVocbase(isolate, context,
TRI_V8_ASCII_STRING("SYS_DEFINE_ACTION"),
JS_DefineAction);
TRI_AddGlobalFunctionVocbase(
isolate, context,
TRI_V8_ASCII_STRING("SYS_EXECUTE_GLOBAL_CONTEXT_FUNCTION"),
JS_ExecuteGlobalContextFunction);
TRI_AddGlobalFunctionVocbase(isolate, context,
TRI_V8_ASCII_STRING("SYS_GET_CURRENT_REQUEST"),
JS_GetCurrentRequest);
TRI_AddGlobalFunctionVocbase(isolate, context,
TRI_V8_ASCII_STRING("SYS_GET_CURRENT_RESPONSE"),
JS_GetCurrentResponse);
TRI_AddGlobalFunctionVocbase(isolate, context,
TRI_V8_ASCII_STRING("SYS_RAW_REQUEST_BODY"),
JS_RawRequestBody, true);
TRI_AddGlobalFunctionVocbase(isolate, context,
TRI_V8_ASCII_STRING("SYS_REQUEST_PARTS"),
JS_RequestParts, true);
TRI_AddGlobalFunctionVocbase(
isolate, context, TRI_V8_ASCII_STRING("SYS_SEND_CHUNK"), JS_SendChunk);
}
////////////////////////////////////////////////////////////////////////////////
/// Below Debugging Functions. Only compiled in maintainer mode.
////////////////////////////////////////////////////////////////////////////////
#ifdef ARANGODB_ENABLE_FAILURE_TESTS
static bool clusterSendToAllServers(
std::string const& dbname,
std::string const& path, // Note: Has to be properly encoded!
arangodb::GeneralRequest::RequestType const& method,
std::string const& body) {
ClusterInfo* ci = ClusterInfo::instance();
ClusterComm* cc = ClusterComm::instance();
std::string url = "/_db/" + StringUtils::urlEncode(dbname) + "/" + path;
// Have to propagate to DB Servers
std::vector<ServerID> DBServers;
CoordTransactionID coordTransactionID = TRI_NewTickServer();
auto reqBodyString = std::make_shared<std::string>(body);
DBServers = ci->getCurrentDBServers();
for (auto const& sid : DBServers) {
auto headers =
std::make_unique<std::unordered_map<std::string, std::string>>();
cc->asyncRequest("", coordTransactionID, "server:" + sid, method, url,
reqBodyString, headers, nullptr, 3600.0);
}
// Now listen to the results:
size_t count = DBServers.size();
for (; count > 0; count--) {
auto res = cc->wait("", coordTransactionID, 0, "", 0.0);
if (res.status == CL_COMM_TIMEOUT) {
cc->drop("", coordTransactionID, 0, "");
return TRI_ERROR_CLUSTER_TIMEOUT;
}
if (res.status == CL_COMM_ERROR || res.status == CL_COMM_DROPPED ||
res.status == CL_COMM_BACKEND_UNAVAILABLE) {
cc->drop("", coordTransactionID, 0, "");
return TRI_ERROR_INTERNAL;
}
}
return TRI_ERROR_NO_ERROR;
}
#endif
////////////////////////////////////////////////////////////////////////////////
/// @brief intentionally causes a segfault
///
/// @FUN{internal.debugSegfault(@FA{message})}
///
/// intentionally cause a segmentation violation
////////////////////////////////////////////////////////////////////////////////
#ifdef ARANGODB_ENABLE_FAILURE_TESTS
static void JS_DebugSegfault(v8::FunctionCallbackInfo<v8::Value> const& args) {
TRI_V8_TRY_CATCH_BEGIN(isolate);
v8::HandleScope scope(isolate);
// extract arguments
if (args.Length() != 1) {
TRI_V8_THROW_EXCEPTION_USAGE("debugSegfault(<message>)");
}
std::string const message = TRI_ObjectToString(args[0]);
TRI_SegfaultDebugging(message.c_str());
// we may get here if we are in non-maintainer mode
TRI_V8_RETURN_UNDEFINED();
TRI_V8_TRY_CATCH_END
}
#endif
////////////////////////////////////////////////////////////////////////////////
/// @brief sets a failure point
///
/// @FUN{internal.debugSetFailAt(@FA{point})}
///
/// Set a point for an intentional system failure
////////////////////////////////////////////////////////////////////////////////
#ifdef ARANGODB_ENABLE_FAILURE_TESTS
static void JS_DebugSetFailAt(v8::FunctionCallbackInfo<v8::Value> const& args) {
TRI_V8_TRY_CATCH_BEGIN(isolate);
v8::HandleScope scope(isolate);
TRI_GET_GLOBALS();
if (v8g->_vocbase == nullptr) {
TRI_V8_THROW_EXCEPTION_MEMORY();
}
std::string dbname(v8g->_vocbase->_name);
// extract arguments
if (args.Length() != 1) {
TRI_V8_THROW_EXCEPTION_USAGE("debugSetFailAt(<point>)");
}
std::string const point = TRI_ObjectToString(args[0]);
TRI_AddFailurePointDebugging(point.c_str());
if (ServerState::instance()->isCoordinator()) {
int res = clusterSendToAllServers(
dbname, "_admin/debug/failat/" + StringUtils::urlEncode(point),
arangodb::GeneralRequest::RequestType::PUT, "");
if (res != TRI_ERROR_NO_ERROR) {
TRI_V8_THROW_EXCEPTION(res);
}
}
TRI_V8_RETURN_UNDEFINED();
TRI_V8_TRY_CATCH_END
}
#endif
////////////////////////////////////////////////////////////////////////////////
/// @brief removes a failure point
///
/// @FUN{internal.debugRemoveFailAt(@FA{point})}
///
/// Remove a point for an intentional system failure
////////////////////////////////////////////////////////////////////////////////
#ifdef ARANGODB_ENABLE_FAILURE_TESTS
static void JS_DebugRemoveFailAt(
v8::FunctionCallbackInfo<v8::Value> const& args) {
TRI_V8_TRY_CATCH_BEGIN(isolate);
v8::HandleScope scope(isolate);
TRI_GET_GLOBALS();
if (v8g->_vocbase == nullptr) {
TRI_V8_THROW_EXCEPTION_MEMORY();
}
std::string dbname(v8g->_vocbase->_name);
// extract arguments
if (args.Length() != 1) {
TRI_V8_THROW_EXCEPTION_USAGE("debugRemoveFailAt(<point>)");
}
std::string const point = TRI_ObjectToString(args[0]);
TRI_RemoveFailurePointDebugging(point.c_str());
if (ServerState::instance()->isCoordinator()) {
int res = clusterSendToAllServers(
dbname, "_admin/debug/failat/" + StringUtils::urlEncode(point),
arangodb::GeneralRequest::RequestType::DELETE_REQ, "");
if (res != TRI_ERROR_NO_ERROR) {
TRI_V8_THROW_EXCEPTION(res);
}
}
TRI_V8_RETURN_UNDEFINED();
TRI_V8_TRY_CATCH_END
}
#endif
////////////////////////////////////////////////////////////////////////////////
/// @brief clears all failure points
///
/// @FUN{internal.debugClearFailAt()}
///
/// Remove all points for intentional system failures
////////////////////////////////////////////////////////////////////////////////
static void JS_DebugClearFailAt(
v8::FunctionCallbackInfo<v8::Value> const& args) {
TRI_V8_TRY_CATCH_BEGIN(isolate);
v8::HandleScope scope(isolate);
// extract arguments
if (args.Length() != 0) {
TRI_V8_THROW_EXCEPTION_USAGE("debugClearFailAt()");
}
// if failure testing is not enabled, this is a no-op
#ifdef ARANGODB_ENABLE_FAILURE_TESTS
TRI_ClearFailurePointsDebugging();
if (ServerState::instance()->isCoordinator()) {
TRI_GET_GLOBALS();
if (v8g->_vocbase == nullptr) {
TRI_V8_THROW_EXCEPTION_MEMORY();
}
std::string dbname(v8g->_vocbase->_name);
int res = clusterSendToAllServers(
dbname, "_admin/debug/failat",
arangodb::GeneralRequest::RequestType::DELETE_REQ, "");
if (res != TRI_ERROR_NO_ERROR) {
TRI_V8_THROW_EXCEPTION(res);
}
}
#endif
TRI_V8_RETURN_UNDEFINED();
TRI_V8_TRY_CATCH_END
}
void TRI_InitV8DebugUtils(v8::Isolate* isolate, v8::Handle<v8::Context> context,
std::string const& startupPath,
std::string const& modules) {
// debugging functions
TRI_AddGlobalFunctionVocbase(isolate, context,
TRI_V8_ASCII_STRING("SYS_DEBUG_CLEAR_FAILAT"),
JS_DebugClearFailAt);
#ifdef ARANGODB_ENABLE_FAILURE_TESTS
TRI_AddGlobalFunctionVocbase(isolate, context,
TRI_V8_ASCII_STRING("SYS_DEBUG_SEGFAULT"),
JS_DebugSegfault);
TRI_AddGlobalFunctionVocbase(isolate, context,
TRI_V8_ASCII_STRING("SYS_DEBUG_SET_FAILAT"),
JS_DebugSetFailAt);
TRI_AddGlobalFunctionVocbase(isolate, context,
TRI_V8_ASCII_STRING("SYS_DEBUG_REMOVE_FAILAT"),
JS_DebugRemoveFailAt);
#endif
}