2019-01-14 12:17:08 +00:00
|
|
|
#include "App.h"
|
2019-01-14 10:45:25 +00:00
|
|
|
#include "Utilities.h"
|
2019-12-05 03:07:18 +00:00
|
|
|
|
|
|
|
#include <v8.h>
|
2019-01-14 10:45:25 +00:00
|
|
|
using namespace v8;
|
|
|
|
|
2019-01-14 12:17:08 +00:00
|
|
|
struct HttpResponseWrapper {
|
|
|
|
|
|
|
|
template <bool SSL>
|
|
|
|
static inline uWS::HttpResponse<SSL> *getHttpResponse(const FunctionCallbackInfo<Value> &args) {
|
2019-12-05 03:07:18 +00:00
|
|
|
Isolate *isolate = args.GetIsolate();
|
2019-01-18 15:33:56 +00:00
|
|
|
auto *res = (uWS::HttpResponse<SSL> *) args.Holder()->GetAlignedPointerFromInternalField(0);
|
|
|
|
if (!res) {
|
2019-11-08 12:49:40 +00:00
|
|
|
args.GetReturnValue().Set(isolate->ThrowException(String::NewFromUtf8(isolate, "Invalid access of discarded (invalid, deleted) uWS.HttpResponse/SSLHttpResponse.", NewStringType::kNormal).ToLocalChecked()));
|
2019-01-18 15:33:56 +00:00
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Marks this JS object invalid */
|
|
|
|
static inline void invalidateResObject(const FunctionCallbackInfo<Value> &args) {
|
|
|
|
args.Holder()->SetAlignedPointerInInternalField(0, nullptr);
|
2019-01-14 12:17:08 +00:00
|
|
|
}
|
|
|
|
|
2019-01-17 07:22:43 +00:00
|
|
|
/* Takes nothing, kills the connection */
|
|
|
|
template <bool SSL>
|
|
|
|
static void res_close(const FunctionCallbackInfo<Value> &args) {
|
2019-01-18 15:33:56 +00:00
|
|
|
auto *res = getHttpResponse<SSL>(args);
|
|
|
|
if (res) {
|
|
|
|
invalidateResObject(args);
|
|
|
|
res->close();
|
|
|
|
args.GetReturnValue().Set(args.Holder());
|
|
|
|
}
|
2019-01-17 07:22:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Takes function of data and isLast. Expects nothing from callback, returns this */
|
|
|
|
template <bool SSL>
|
|
|
|
static void res_onData(const FunctionCallbackInfo<Value> &args) {
|
2019-12-05 03:07:18 +00:00
|
|
|
Isolate *isolate = args.GetIsolate();
|
2019-01-18 15:33:56 +00:00
|
|
|
auto *res = getHttpResponse<SSL>(args);
|
|
|
|
if (res) {
|
|
|
|
/* This thing perfectly fits in with unique_function, and will Reset on destructor */
|
|
|
|
UniquePersistent<Function> p(isolate, Local<Function>::Cast(args[0]));
|
2019-01-17 07:22:43 +00:00
|
|
|
|
2019-12-05 03:07:18 +00:00
|
|
|
res->onData([p = std::move(p), isolate](std::string_view data, bool last) {
|
2019-01-18 15:33:56 +00:00
|
|
|
HandleScope hs(isolate);
|
2019-01-17 07:22:43 +00:00
|
|
|
|
2019-01-18 15:33:56 +00:00
|
|
|
Local<ArrayBuffer> dataArrayBuffer = ArrayBuffer::New(isolate, (void *) data.data(), data.length());
|
2019-01-17 07:22:43 +00:00
|
|
|
|
2019-01-18 15:33:56 +00:00
|
|
|
Local<Value> argv[] = {dataArrayBuffer, Boolean::New(isolate, last)};
|
2020-01-15 17:19:47 +00:00
|
|
|
CallJS(isolate, Local<Function>::New(isolate, p), 2, argv);
|
2019-01-17 07:22:43 +00:00
|
|
|
|
2020-04-23 19:57:38 +00:00
|
|
|
NeuterArrayBuffer(dataArrayBuffer);
|
2019-01-18 15:33:56 +00:00
|
|
|
});
|
2019-01-17 07:22:43 +00:00
|
|
|
|
2019-01-18 15:33:56 +00:00
|
|
|
args.GetReturnValue().Set(args.Holder());
|
|
|
|
}
|
2019-01-17 07:22:43 +00:00
|
|
|
}
|
2019-01-15 07:10:57 +00:00
|
|
|
|
2019-01-17 05:42:45 +00:00
|
|
|
/* Takes nothing, returns nothing. Cb wants nothing returned. */
|
2019-01-17 05:31:55 +00:00
|
|
|
template <bool SSL>
|
|
|
|
static void res_onAborted(const FunctionCallbackInfo<Value> &args) {
|
2019-12-05 03:07:18 +00:00
|
|
|
Isolate *isolate = args.GetIsolate();
|
2019-01-18 15:33:56 +00:00
|
|
|
auto *res = getHttpResponse<SSL>(args);
|
|
|
|
if (res) {
|
|
|
|
/* This thing perfectly fits in with unique_function, and will Reset on destructor */
|
|
|
|
UniquePersistent<Function> p(isolate, Local<Function>::Cast(args[0]));
|
2019-01-17 05:31:55 +00:00
|
|
|
|
2019-01-18 15:33:56 +00:00
|
|
|
/* This is how we capture res (C++ this in invocation of this function) */
|
|
|
|
UniquePersistent<Object> resObject(isolate, args.Holder());
|
2019-01-17 05:31:55 +00:00
|
|
|
|
2019-12-05 03:07:18 +00:00
|
|
|
res->onAborted([p = std::move(p), resObject = std::move(resObject), isolate]() {
|
2019-01-18 15:33:56 +00:00
|
|
|
HandleScope hs(isolate);
|
2019-01-17 05:31:55 +00:00
|
|
|
|
2019-01-18 15:33:56 +00:00
|
|
|
/* Mark this resObject invalid */
|
|
|
|
Local<Object>::New(isolate, resObject)->SetAlignedPointerInInternalField(0, nullptr);
|
|
|
|
|
2020-01-15 17:19:47 +00:00
|
|
|
CallJS(isolate, Local<Function>::New(isolate, p), 0, nullptr);
|
2019-01-18 15:33:56 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
args.GetReturnValue().Set(args.Holder());
|
|
|
|
}
|
2019-01-17 05:31:55 +00:00
|
|
|
}
|
|
|
|
|
2019-03-03 06:42:10 +00:00
|
|
|
/* Takes nothing, returns arraybuffer */
|
|
|
|
template <bool SSL>
|
|
|
|
static void res_getRemoteAddress(const FunctionCallbackInfo<Value> &args) {
|
2019-12-05 03:07:18 +00:00
|
|
|
Isolate *isolate = args.GetIsolate();
|
2019-03-03 06:42:10 +00:00
|
|
|
auto *res = getHttpResponse<SSL>(args);
|
|
|
|
if (res) {
|
|
|
|
std::string_view ip = res->getRemoteAddress();
|
|
|
|
|
|
|
|
/* Todo: we need to pass a copy here */
|
|
|
|
args.GetReturnValue().Set(ArrayBuffer::New(isolate, (void *) ip.data(), ip.length()/*, ArrayBufferCreationMode::kInternalized*/));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-15 10:52:55 +00:00
|
|
|
/* Returns the current write offset */
|
|
|
|
template <bool SSL>
|
|
|
|
static void res_getWriteOffset(const FunctionCallbackInfo<Value> &args) {
|
2019-12-05 03:07:18 +00:00
|
|
|
Isolate *isolate = args.GetIsolate();
|
2019-01-18 15:33:56 +00:00
|
|
|
auto *res = getHttpResponse<SSL>(args);
|
|
|
|
if (res) {
|
|
|
|
args.GetReturnValue().Set(Integer::New(isolate, getHttpResponse<SSL>(args)->getWriteOffset()));
|
|
|
|
}
|
2019-01-15 10:52:55 +00:00
|
|
|
}
|
|
|
|
|
2019-01-15 07:10:57 +00:00
|
|
|
/* Takes function of bool(int), returns this */
|
|
|
|
template <bool SSL>
|
|
|
|
static void res_onWritable(const FunctionCallbackInfo<Value> &args) {
|
2019-12-05 03:07:18 +00:00
|
|
|
Isolate *isolate = args.GetIsolate();
|
2019-01-18 15:33:56 +00:00
|
|
|
auto *res = getHttpResponse<SSL>(args);
|
|
|
|
if (res) {
|
|
|
|
/* This thing perfectly fits in with unique_function, and will Reset on destructor */
|
|
|
|
UniquePersistent<Function> p(isolate, Local<Function>::Cast(args[0]));
|
2019-01-15 07:10:57 +00:00
|
|
|
|
2019-12-05 03:07:18 +00:00
|
|
|
res->onWritable([p = std::move(p), isolate](int offset) -> bool {
|
2019-01-18 15:33:56 +00:00
|
|
|
HandleScope hs(isolate);
|
2019-01-15 07:10:57 +00:00
|
|
|
|
2019-01-18 15:33:56 +00:00
|
|
|
Local<Value> argv[] = {Integer::NewFromUnsigned(isolate, offset)};
|
2019-08-18 19:49:06 +00:00
|
|
|
|
|
|
|
/* We should check if this is really here! */
|
2020-01-15 17:19:47 +00:00
|
|
|
MaybeLocal<Value> maybeBoolean = CallJS(isolate, Local<Function>::New(isolate, p), 1, argv);
|
2019-08-18 19:49:06 +00:00
|
|
|
if (maybeBoolean.IsEmpty()) {
|
|
|
|
std::cerr << "ERROR! onWritable must return a boolean value according to documentation!" << std::endl;
|
|
|
|
exit(-1);
|
|
|
|
}
|
|
|
|
|
2019-11-08 12:49:40 +00:00
|
|
|
return BooleanValue(isolate, maybeBoolean.ToLocalChecked());
|
2019-01-18 15:33:56 +00:00
|
|
|
/* How important is this return? */
|
|
|
|
});
|
2019-01-15 07:10:57 +00:00
|
|
|
|
2019-01-18 15:33:56 +00:00
|
|
|
args.GetReturnValue().Set(args.Holder());
|
|
|
|
}
|
2019-01-15 07:10:57 +00:00
|
|
|
}
|
2019-01-14 12:39:43 +00:00
|
|
|
|
|
|
|
/* Takes string or arraybuffer, returns this */
|
|
|
|
template <bool SSL>
|
|
|
|
static void res_writeStatus(const FunctionCallbackInfo<Value> &args) {
|
2019-01-18 15:33:56 +00:00
|
|
|
auto *res = getHttpResponse<SSL>(args);
|
|
|
|
if (res) {
|
|
|
|
NativeString data(args.GetIsolate(), args[0]);
|
|
|
|
if (data.isInvalid(args)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
res->writeStatus(data.getString());
|
|
|
|
|
|
|
|
args.GetReturnValue().Set(args.Holder());
|
|
|
|
}
|
2019-01-14 12:39:43 +00:00
|
|
|
}
|
2019-01-14 12:17:08 +00:00
|
|
|
|
|
|
|
/* Takes string or arraybuffer, returns this */
|
|
|
|
template <bool SSL>
|
|
|
|
static void res_end(const FunctionCallbackInfo<Value> &args) {
|
2019-01-18 15:33:56 +00:00
|
|
|
auto *res = getHttpResponse<SSL>(args);
|
|
|
|
if (res) {
|
|
|
|
NativeString data(args.GetIsolate(), args[0]);
|
|
|
|
if (data.isInvalid(args)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
invalidateResObject(args);
|
|
|
|
res->end(data.getString());
|
|
|
|
|
|
|
|
args.GetReturnValue().Set(args.Holder());
|
|
|
|
}
|
2019-01-14 12:17:08 +00:00
|
|
|
}
|
|
|
|
|
2019-01-14 12:39:43 +00:00
|
|
|
/* Takes data and optionally totalLength, returns true for success, false for backpressure */
|
|
|
|
template <bool SSL>
|
|
|
|
static void res_tryEnd(const FunctionCallbackInfo<Value> &args) {
|
2019-12-05 03:07:18 +00:00
|
|
|
Isolate *isolate = args.GetIsolate();
|
2019-01-18 15:33:56 +00:00
|
|
|
auto *res = getHttpResponse<SSL>(args);
|
|
|
|
if (res) {
|
|
|
|
NativeString data(args.GetIsolate(), args[0]);
|
|
|
|
if (data.isInvalid(args)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
int totalSize = 0;
|
|
|
|
if (args.Length() > 1) {
|
2019-04-24 01:01:54 +00:00
|
|
|
totalSize = args[1]->Uint32Value(isolate->GetCurrentContext()).ToChecked();
|
2019-01-18 15:33:56 +00:00
|
|
|
}
|
|
|
|
|
2019-02-09 22:32:07 +00:00
|
|
|
auto [ok, hasResponded] = res->tryEnd(data.getString(), totalSize);
|
2019-01-18 15:33:56 +00:00
|
|
|
|
|
|
|
/* Invalidate this object if we responded completely */
|
2019-02-09 22:32:07 +00:00
|
|
|
if (hasResponded) {
|
2019-01-18 15:33:56 +00:00
|
|
|
invalidateResObject(args);
|
|
|
|
}
|
|
|
|
|
2019-01-31 19:36:31 +00:00
|
|
|
/* This is a quick fix, it will need updating in µWS later on */
|
|
|
|
Local<Array> array = Array::New(isolate, 2);
|
2019-11-08 12:49:40 +00:00
|
|
|
array->Set(isolate->GetCurrentContext(), 0, Boolean::New(isolate, ok)).ToChecked();
|
|
|
|
array->Set(isolate->GetCurrentContext(), 1, Boolean::New(isolate, hasResponded)).ToChecked();
|
2019-01-31 19:36:31 +00:00
|
|
|
|
|
|
|
args.GetReturnValue().Set(array);
|
2019-01-14 12:39:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Takes data, returns true for success, false for backpressure */
|
|
|
|
template <bool SSL>
|
|
|
|
static void res_write(const FunctionCallbackInfo<Value> &args) {
|
2019-12-05 03:07:18 +00:00
|
|
|
Isolate *isolate = args.GetIsolate();
|
2019-01-18 15:33:56 +00:00
|
|
|
auto *res = getHttpResponse<SSL>(args);
|
|
|
|
if (res) {
|
|
|
|
NativeString data(args.GetIsolate(), args[0]);
|
|
|
|
if (data.isInvalid(args)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
bool ok = res->write(data.getString());
|
|
|
|
|
|
|
|
args.GetReturnValue().Set(Boolean::New(isolate, ok));
|
|
|
|
}
|
2019-01-14 12:39:43 +00:00
|
|
|
}
|
|
|
|
|
2019-01-14 12:17:08 +00:00
|
|
|
/* Takes key, value. Returns this */
|
|
|
|
template <bool SSL>
|
|
|
|
static void res_writeHeader(const FunctionCallbackInfo<Value> &args) {
|
2019-12-05 03:07:18 +00:00
|
|
|
Isolate *isolate = args.GetIsolate();
|
2019-01-18 15:33:56 +00:00
|
|
|
auto *res = getHttpResponse<SSL>(args);
|
|
|
|
if (res) {
|
|
|
|
NativeString header(args.GetIsolate(), args[0]);
|
|
|
|
if (header.isInvalid(args)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
NativeString value(args.GetIsolate(), args[1]);
|
|
|
|
if (value.isInvalid(args)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
res->writeHeader(header.getString(),value.getString());
|
|
|
|
|
|
|
|
args.GetReturnValue().Set(args.Holder());
|
|
|
|
}
|
2019-01-14 12:17:08 +00:00
|
|
|
}
|
|
|
|
|
2020-01-10 18:02:51 +00:00
|
|
|
/* Takes function, returns this */
|
2019-04-16 15:00:27 +00:00
|
|
|
template <bool SSL>
|
|
|
|
static void res_cork(const FunctionCallbackInfo<Value> &args) {
|
2019-12-05 03:07:18 +00:00
|
|
|
Isolate *isolate = args.GetIsolate();
|
2019-04-16 15:00:27 +00:00
|
|
|
auto *res = getHttpResponse<SSL>(args);
|
|
|
|
if (res) {
|
|
|
|
|
2019-12-05 03:07:18 +00:00
|
|
|
res->cork([cb = Local<Function>::Cast(args[0]), isolate]() {
|
2020-01-15 17:19:47 +00:00
|
|
|
/* This one is called from JS so we don't need CallJS */
|
2019-08-18 19:49:06 +00:00
|
|
|
cb->Call(isolate->GetCurrentContext(), isolate->GetCurrentContext()->Global(), 0, nullptr).IsEmpty();
|
2019-04-16 15:00:27 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
args.GetReturnValue().Set(args.Holder());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-14 12:17:08 +00:00
|
|
|
template <bool SSL>
|
2019-12-05 03:07:18 +00:00
|
|
|
static Local<Object> init(Isolate *isolate) {
|
2019-01-14 12:17:08 +00:00
|
|
|
Local<FunctionTemplate> resTemplateLocal = FunctionTemplate::New(isolate);
|
|
|
|
if (SSL) {
|
2019-11-08 12:49:40 +00:00
|
|
|
resTemplateLocal->SetClassName(String::NewFromUtf8(isolate, "uWS.SSLHttpResponse", NewStringType::kNormal).ToLocalChecked());
|
2019-01-14 12:17:08 +00:00
|
|
|
} else {
|
2019-11-08 12:49:40 +00:00
|
|
|
resTemplateLocal->SetClassName(String::NewFromUtf8(isolate, "uWS.HttpResponse", NewStringType::kNormal).ToLocalChecked());
|
2019-01-14 12:17:08 +00:00
|
|
|
}
|
|
|
|
resTemplateLocal->InstanceTemplate()->SetInternalFieldCount(1);
|
|
|
|
|
|
|
|
/* Register our functions */
|
2019-11-08 12:49:40 +00:00
|
|
|
resTemplateLocal->PrototypeTemplate()->Set(String::NewFromUtf8(isolate, "writeStatus", NewStringType::kNormal).ToLocalChecked(), FunctionTemplate::New(isolate, res_writeStatus<SSL>));
|
|
|
|
resTemplateLocal->PrototypeTemplate()->Set(String::NewFromUtf8(isolate, "end", NewStringType::kNormal).ToLocalChecked(), FunctionTemplate::New(isolate, res_end<SSL>));
|
|
|
|
resTemplateLocal->PrototypeTemplate()->Set(String::NewFromUtf8(isolate, "tryEnd", NewStringType::kNormal).ToLocalChecked(), FunctionTemplate::New(isolate, res_tryEnd<SSL>));
|
|
|
|
resTemplateLocal->PrototypeTemplate()->Set(String::NewFromUtf8(isolate, "write", NewStringType::kNormal).ToLocalChecked(), FunctionTemplate::New(isolate, res_write<SSL>));
|
|
|
|
resTemplateLocal->PrototypeTemplate()->Set(String::NewFromUtf8(isolate, "writeHeader", NewStringType::kNormal).ToLocalChecked(), FunctionTemplate::New(isolate, res_writeHeader<SSL>));
|
|
|
|
resTemplateLocal->PrototypeTemplate()->Set(String::NewFromUtf8(isolate, "close", NewStringType::kNormal).ToLocalChecked(), FunctionTemplate::New(isolate, res_close<SSL>));
|
|
|
|
resTemplateLocal->PrototypeTemplate()->Set(String::NewFromUtf8(isolate, "getWriteOffset", NewStringType::kNormal).ToLocalChecked(), FunctionTemplate::New(isolate, res_getWriteOffset<SSL>));
|
|
|
|
resTemplateLocal->PrototypeTemplate()->Set(String::NewFromUtf8(isolate, "onWritable", NewStringType::kNormal).ToLocalChecked(), FunctionTemplate::New(isolate, res_onWritable<SSL>));
|
|
|
|
resTemplateLocal->PrototypeTemplate()->Set(String::NewFromUtf8(isolate, "onAborted", NewStringType::kNormal).ToLocalChecked(), FunctionTemplate::New(isolate, res_onAborted<SSL>));
|
|
|
|
resTemplateLocal->PrototypeTemplate()->Set(String::NewFromUtf8(isolate, "onData", NewStringType::kNormal).ToLocalChecked(), FunctionTemplate::New(isolate, res_onData<SSL>));
|
|
|
|
resTemplateLocal->PrototypeTemplate()->Set(String::NewFromUtf8(isolate, "getRemoteAddress", NewStringType::kNormal).ToLocalChecked(), FunctionTemplate::New(isolate, res_getRemoteAddress<SSL>));
|
2019-12-17 20:08:06 +00:00
|
|
|
resTemplateLocal->PrototypeTemplate()->Set(String::NewFromUtf8(isolate, "cork", NewStringType::kNormal).ToLocalChecked(), FunctionTemplate::New(isolate, res_cork<SSL>));
|
2019-01-15 07:10:57 +00:00
|
|
|
|
2019-01-14 12:17:08 +00:00
|
|
|
/* Create our template */
|
2019-04-24 01:01:54 +00:00
|
|
|
Local<Object> resObjectLocal = resTemplateLocal->GetFunction(isolate->GetCurrentContext()).ToLocalChecked()->NewInstance(isolate->GetCurrentContext()).ToLocalChecked();
|
2019-12-05 03:07:18 +00:00
|
|
|
|
|
|
|
return resObjectLocal;
|
2019-01-14 12:17:08 +00:00
|
|
|
}
|
|
|
|
};
|