2019-01-14 12:17:08 +00:00
|
|
|
#include "App.h"
|
2019-01-14 10:45:25 +00:00
|
|
|
#include <v8.h>
|
|
|
|
#include "Utilities.h"
|
|
|
|
using namespace v8;
|
|
|
|
|
2019-01-14 12:17:08 +00:00
|
|
|
struct HttpResponseWrapper {
|
|
|
|
static Persistent<Object> resTemplate[2];
|
|
|
|
|
|
|
|
template <bool SSL>
|
|
|
|
static inline uWS::HttpResponse<SSL> *getHttpResponse(const FunctionCallbackInfo<Value> &args) {
|
2019-01-18 15:33:56 +00:00
|
|
|
auto *res = (uWS::HttpResponse<SSL> *) args.Holder()->GetAlignedPointerFromInternalField(0);
|
|
|
|
if (!res) {
|
|
|
|
args.GetReturnValue().Set(isolate->ThrowException(String::NewFromUtf8(isolate, "Invalid access of discarded (invalid, deleted) uWS.HttpResponse/SSLHttpResponse.")));
|
|
|
|
}
|
|
|
|
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-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-01-18 15:33:56 +00:00
|
|
|
res->onData([p = std::move(p)](std::string_view data, bool last) {
|
|
|
|
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)};
|
|
|
|
Local<Function>::New(isolate, p)->Call(isolate->GetCurrentContext()->Global(), 2, argv);
|
2019-01-17 07:22:43 +00:00
|
|
|
|
2019-01-18 15:33:56 +00:00
|
|
|
dataArrayBuffer->Neuter();
|
|
|
|
});
|
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-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-01-18 15:33:56 +00:00
|
|
|
res->onAborted([p = std::move(p), resObject = std::move(resObject)]() {
|
|
|
|
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);
|
|
|
|
|
|
|
|
Local<Function>::New(isolate, p)->Call(isolate->GetCurrentContext()->Global(), 0, nullptr);
|
|
|
|
});
|
|
|
|
|
|
|
|
args.GetReturnValue().Set(args.Holder());
|
|
|
|
}
|
2019-01-17 05:31:55 +00:00
|
|
|
}
|
|
|
|
|
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-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-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-01-18 15:33:56 +00:00
|
|
|
res->onWritable([p = std::move(p)](int offset) {
|
|
|
|
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)};
|
|
|
|
return Local<Function>::New(isolate, p)->Call(isolate->GetCurrentContext()->Global(), 1, argv)->BooleanValue();
|
|
|
|
/* 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-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) {
|
|
|
|
totalSize = args[1]->Uint32Value();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ok = res->tryEnd(data.getString(), totalSize);
|
|
|
|
|
|
|
|
/* Invalidate this object if we responded completely */
|
|
|
|
if (res->hasResponded()) {
|
|
|
|
invalidateResObject(args);
|
|
|
|
}
|
|
|
|
|
|
|
|
args.GetReturnValue().Set(Boolean::New(isolate, ok));
|
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-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-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
|
|
|
}
|
|
|
|
|
|
|
|
template <bool SSL>
|
|
|
|
static void initResTemplate() {
|
|
|
|
Local<FunctionTemplate> resTemplateLocal = FunctionTemplate::New(isolate);
|
|
|
|
if (SSL) {
|
|
|
|
resTemplateLocal->SetClassName(String::NewFromUtf8(isolate, "uWS.SSLHttpResponse"));
|
|
|
|
} else {
|
|
|
|
resTemplateLocal->SetClassName(String::NewFromUtf8(isolate, "uWS.HttpResponse"));
|
|
|
|
}
|
|
|
|
resTemplateLocal->InstanceTemplate()->SetInternalFieldCount(1);
|
|
|
|
|
|
|
|
/* Register our functions */
|
2019-01-14 12:39:43 +00:00
|
|
|
resTemplateLocal->PrototypeTemplate()->Set(String::NewFromUtf8(isolate, "writeStatus"), FunctionTemplate::New(isolate, res_writeStatus<SSL>));
|
2019-01-14 12:17:08 +00:00
|
|
|
resTemplateLocal->PrototypeTemplate()->Set(String::NewFromUtf8(isolate, "end"), FunctionTemplate::New(isolate, res_end<SSL>));
|
2019-01-14 12:39:43 +00:00
|
|
|
resTemplateLocal->PrototypeTemplate()->Set(String::NewFromUtf8(isolate, "tryEnd"), FunctionTemplate::New(isolate, res_tryEnd<SSL>));
|
|
|
|
resTemplateLocal->PrototypeTemplate()->Set(String::NewFromUtf8(isolate, "write"), FunctionTemplate::New(isolate, res_write<SSL>));
|
2019-01-14 12:17:08 +00:00
|
|
|
resTemplateLocal->PrototypeTemplate()->Set(String::NewFromUtf8(isolate, "writeHeader"), FunctionTemplate::New(isolate, res_writeHeader<SSL>));
|
2019-01-17 07:22:43 +00:00
|
|
|
resTemplateLocal->PrototypeTemplate()->Set(String::NewFromUtf8(isolate, "close"), FunctionTemplate::New(isolate, res_close<SSL>));
|
2019-01-15 10:52:55 +00:00
|
|
|
resTemplateLocal->PrototypeTemplate()->Set(String::NewFromUtf8(isolate, "getWriteOffset"), FunctionTemplate::New(isolate, res_getWriteOffset<SSL>));
|
2019-01-15 07:10:57 +00:00
|
|
|
resTemplateLocal->PrototypeTemplate()->Set(String::NewFromUtf8(isolate, "onWritable"), FunctionTemplate::New(isolate, res_onWritable<SSL>));
|
2019-01-17 05:31:55 +00:00
|
|
|
resTemplateLocal->PrototypeTemplate()->Set(String::NewFromUtf8(isolate, "onAborted"), FunctionTemplate::New(isolate, res_onAborted<SSL>));
|
2019-01-17 07:22:43 +00:00
|
|
|
resTemplateLocal->PrototypeTemplate()->Set(String::NewFromUtf8(isolate, "onData"), FunctionTemplate::New(isolate, res_onData<SSL>));
|
2019-01-15 07:10:57 +00:00
|
|
|
|
2019-01-14 12:17:08 +00:00
|
|
|
/* Create our template */
|
|
|
|
Local<Object> resObjectLocal = resTemplateLocal->GetFunction()->NewInstance(isolate->GetCurrentContext()).ToLocalChecked();
|
|
|
|
resTemplate[SSL].Reset(isolate, resObjectLocal);
|
2019-01-14 10:34:20 +00:00
|
|
|
}
|
2019-01-14 12:17:08 +00:00
|
|
|
|
|
|
|
template <class APP>
|
|
|
|
static Local<Object> getResInstance() {
|
|
|
|
return Local<Object>::New(isolate, resTemplate[std::is_same<APP, uWS::SSLApp>::value])->Clone();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
Persistent<Object> HttpResponseWrapper::resTemplate[2];
|