HttpResponseWrapper refactor
This commit is contained in:
parent
80bc15915b
commit
810695f32c
@ -142,10 +142,10 @@ void uWS_App_get(const FunctionCallbackInfo<Value> &args) {
|
||||
app->get(std::string(nativeString.getData(), nativeString.getLength()), [pf](auto *res, auto *req) {
|
||||
HandleScope hs(isolate);
|
||||
|
||||
Local<Object> resObject = getResInstance<APP>();
|
||||
Local<Object> resObject = HttpResponseWrapper::getResInstance<APP>();
|
||||
resObject->SetAlignedPointerInInternalField(0, res);
|
||||
|
||||
Local<Object> reqObject = HttpRequestWrapper::getReqInstance();//Local<Object>::New(isolate, reqTemplate)->Clone();
|
||||
Local<Object> reqObject = HttpRequestWrapper::getReqInstance();
|
||||
reqObject->SetAlignedPointerInInternalField(0, req);
|
||||
|
||||
Local<Value> argv[] = {resObject, reqObject};
|
||||
|
@ -3,6 +3,12 @@
|
||||
#include "Utilities.h"
|
||||
using namespace v8;
|
||||
|
||||
// du behver inte klona dessa
|
||||
// det finns bara en enda giltig request vid någon tid, och det är alltid
|
||||
// inom en callback
|
||||
|
||||
// håll en färdig request och tillåt functioner endast när du är inom callbacken
|
||||
|
||||
/* This one is the same for SSL and non-SSL */
|
||||
struct HttpRequestWrapper {
|
||||
static Persistent<Object> reqTemplate;
|
||||
@ -13,8 +19,6 @@ struct HttpRequestWrapper {
|
||||
return ((uWS::HttpRequest *) args.Holder()->GetAlignedPointerFromInternalField(0));
|
||||
}
|
||||
|
||||
// req.onAbort ?
|
||||
|
||||
/* Takes int, returns string (must be in bounds) */
|
||||
static void req_getParameter(const FunctionCallbackInfo<Value> &args) {
|
||||
int index = args[0]->Uint32Value();
|
||||
@ -41,11 +45,6 @@ struct HttpRequestWrapper {
|
||||
}
|
||||
|
||||
static void initReqTemplate() {
|
||||
/* The only thing this req needs is getHeader and similar, getParameter, getUrl and so on */
|
||||
|
||||
/*reqTemplateLocal->PrototypeTemplate()->SetAccessor(String::NewFromUtf8(isolate, "url"), Request::url);
|
||||
reqTemplateLocal->PrototypeTemplate()->SetAccessor(String::NewFromUtf8(isolate, "method"), Request::method);*/
|
||||
|
||||
/* We do clone every request object, we could share them, they are illegal to use outside the function anyways */
|
||||
Local<FunctionTemplate> reqTemplateLocal = FunctionTemplate::New(isolate);
|
||||
reqTemplateLocal->SetClassName(String::NewFromUtf8(isolate, "uWS.HttpRequest"));
|
||||
@ -61,8 +60,9 @@ struct HttpRequestWrapper {
|
||||
reqTemplate.Reset(isolate, reqObjectLocal);
|
||||
}
|
||||
|
||||
//template <class APP>
|
||||
static Local<Object> getReqInstance() {
|
||||
// if we attach a number that counts up to this req we can check if the number is still valid when calling functions?
|
||||
|
||||
return Local<Object>::New(isolate, reqTemplate)->Clone();
|
||||
}
|
||||
};
|
||||
|
@ -1,50 +1,63 @@
|
||||
// res.onData(JS function)
|
||||
// res.write
|
||||
// res.tryEnd
|
||||
|
||||
/* Helping QtCreator */
|
||||
#include "App.h"
|
||||
#include <v8.h>
|
||||
#include "Utilities.h"
|
||||
using namespace v8;
|
||||
|
||||
// this whole template thing could be one struct with members to order tihngs better
|
||||
Persistent<Object> resTemplate[2];
|
||||
struct HttpResponseWrapper {
|
||||
static Persistent<Object> resTemplate[2];
|
||||
|
||||
template <bool SSL>
|
||||
void res_end(const FunctionCallbackInfo<Value> &args) {
|
||||
NativeString data(args.GetIsolate(), args[0]);
|
||||
((uWS::HttpResponse<SSL> *) args.Holder()->GetAlignedPointerFromInternalField(0))->end(std::string_view(data.getData(), data.getLength()));
|
||||
|
||||
args.GetReturnValue().Set(args.Holder());
|
||||
}
|
||||
|
||||
template <bool SSL>
|
||||
void res_writeHeader(const FunctionCallbackInfo<Value> &args) {
|
||||
NativeString header(args.GetIsolate(), args[0]);
|
||||
NativeString value(args.GetIsolate(), args[1]);
|
||||
((uWS::HttpResponse<SSL> *) args.Holder()->GetAlignedPointerFromInternalField(0))->writeHeader(std::string_view(header.getData(), header.getLength()),
|
||||
std::string_view(value.getData(), value.getLength()));
|
||||
|
||||
args.GetReturnValue().Set(args.Holder());
|
||||
}
|
||||
|
||||
template <bool SSL>
|
||||
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"));
|
||||
template <bool SSL>
|
||||
static inline uWS::HttpResponse<SSL> *getHttpResponse(const FunctionCallbackInfo<Value> &args) {
|
||||
return (uWS::HttpResponse<SSL> *) args.Holder()->GetAlignedPointerFromInternalField(0);
|
||||
}
|
||||
resTemplateLocal->InstanceTemplate()->SetInternalFieldCount(1);
|
||||
resTemplateLocal->PrototypeTemplate()->Set(String::NewFromUtf8(isolate, "end"), FunctionTemplate::New(isolate, res_end<SSL>));
|
||||
resTemplateLocal->PrototypeTemplate()->Set(String::NewFromUtf8(isolate, "writeHeader"), FunctionTemplate::New(isolate, res_writeHeader<SSL>));
|
||||
|
||||
Local<Object> resObjectLocal = resTemplateLocal->GetFunction()->NewInstance(isolate->GetCurrentContext()).ToLocalChecked();
|
||||
resTemplate[SSL].Reset(isolate, resObjectLocal);
|
||||
}
|
||||
// res.onData(JS function)
|
||||
// res.write
|
||||
// res.tryEnd
|
||||
|
||||
template <class APP>
|
||||
Local<Object> getResInstance() {
|
||||
return Local<Object>::New(isolate, resTemplate[std::is_same<APP, uWS::SSLApp>::value])->Clone();
|
||||
}
|
||||
/* Takes string or arraybuffer, returns this */
|
||||
template <bool SSL>
|
||||
static void res_end(const FunctionCallbackInfo<Value> &args) {
|
||||
NativeString data(args.GetIsolate(), args[0]);
|
||||
getHttpResponse<SSL>(args)->end(std::string_view(data.getData(), data.getLength()));
|
||||
|
||||
args.GetReturnValue().Set(args.Holder());
|
||||
}
|
||||
|
||||
/* Takes key, value. Returns this */
|
||||
template <bool SSL>
|
||||
static void res_writeHeader(const FunctionCallbackInfo<Value> &args) {
|
||||
NativeString header(args.GetIsolate(), args[0]);
|
||||
NativeString value(args.GetIsolate(), args[1]);
|
||||
getHttpResponse<SSL>(args)->writeHeader(std::string_view(header.getData(), header.getLength()),
|
||||
std::string_view(value.getData(), value.getLength()));
|
||||
|
||||
args.GetReturnValue().Set(args.Holder());
|
||||
}
|
||||
|
||||
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 */
|
||||
resTemplateLocal->PrototypeTemplate()->Set(String::NewFromUtf8(isolate, "end"), FunctionTemplate::New(isolate, res_end<SSL>));
|
||||
resTemplateLocal->PrototypeTemplate()->Set(String::NewFromUtf8(isolate, "writeHeader"), FunctionTemplate::New(isolate, res_writeHeader<SSL>));
|
||||
|
||||
/* Create our template */
|
||||
Local<Object> resObjectLocal = resTemplateLocal->GetFunction()->NewInstance(isolate->GetCurrentContext()).ToLocalChecked();
|
||||
resTemplate[SSL].Reset(isolate, resObjectLocal);
|
||||
}
|
||||
|
||||
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];
|
||||
|
@ -80,8 +80,8 @@ void Main(Local<Object> exports) {
|
||||
WebSocketWrapper::initWsTemplate<1>();
|
||||
|
||||
/* Initialize SSL and non-SSL templates */
|
||||
initResTemplate<0>();
|
||||
initResTemplate<1>();
|
||||
HttpResponseWrapper::initResTemplate<0>();
|
||||
HttpResponseWrapper::initResTemplate<1>();
|
||||
|
||||
/* Init a shared request object */
|
||||
HttpRequestWrapper::initReqTemplate();
|
||||
|
Loading…
Reference in New Issue
Block a user