diff --git a/src/AppWrapper.h b/src/AppWrapper.h index de3e4094..f4595914 100644 --- a/src/AppWrapper.h +++ b/src/AppWrapper.h @@ -142,10 +142,10 @@ void uWS_App_get(const FunctionCallbackInfo &args) { app->get(std::string(nativeString.getData(), nativeString.getLength()), [pf](auto *res, auto *req) { HandleScope hs(isolate); - Local resObject = getResInstance(); + Local resObject = HttpResponseWrapper::getResInstance(); resObject->SetAlignedPointerInInternalField(0, res); - Local reqObject = HttpRequestWrapper::getReqInstance();//Local::New(isolate, reqTemplate)->Clone(); + Local reqObject = HttpRequestWrapper::getReqInstance(); reqObject->SetAlignedPointerInInternalField(0, req); Local argv[] = {resObject, reqObject}; diff --git a/src/HttpRequestWrapper.h b/src/HttpRequestWrapper.h index 8eaa8cb7..1bce519b 100644 --- a/src/HttpRequestWrapper.h +++ b/src/HttpRequestWrapper.h @@ -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 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 &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 reqTemplateLocal = FunctionTemplate::New(isolate); reqTemplateLocal->SetClassName(String::NewFromUtf8(isolate, "uWS.HttpRequest")); @@ -61,8 +60,9 @@ struct HttpRequestWrapper { reqTemplate.Reset(isolate, reqObjectLocal); } - //template static Local 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::New(isolate, reqTemplate)->Clone(); } }; diff --git a/src/HttpResponseWrapper.h b/src/HttpResponseWrapper.h index 32a7a0c7..5e0c4e9d 100644 --- a/src/HttpResponseWrapper.h +++ b/src/HttpResponseWrapper.h @@ -1,50 +1,63 @@ -// res.onData(JS function) -// res.write -// res.tryEnd - -/* Helping QtCreator */ +#include "App.h" #include #include "Utilities.h" using namespace v8; -// this whole template thing could be one struct with members to order tihngs better -Persistent resTemplate[2]; +struct HttpResponseWrapper { + static Persistent resTemplate[2]; -template -void res_end(const FunctionCallbackInfo &args) { - NativeString data(args.GetIsolate(), args[0]); - ((uWS::HttpResponse *) args.Holder()->GetAlignedPointerFromInternalField(0))->end(std::string_view(data.getData(), data.getLength())); - - args.GetReturnValue().Set(args.Holder()); -} - -template -void res_writeHeader(const FunctionCallbackInfo &args) { - NativeString header(args.GetIsolate(), args[0]); - NativeString value(args.GetIsolate(), args[1]); - ((uWS::HttpResponse *) 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 -void initResTemplate() { - Local resTemplateLocal = FunctionTemplate::New(isolate); - if (SSL) { - resTemplateLocal->SetClassName(String::NewFromUtf8(isolate, "uWS.SSLHttpResponse")); - } else { - resTemplateLocal->SetClassName(String::NewFromUtf8(isolate, "uWS.HttpResponse")); + template + static inline uWS::HttpResponse *getHttpResponse(const FunctionCallbackInfo &args) { + return (uWS::HttpResponse *) args.Holder()->GetAlignedPointerFromInternalField(0); } - resTemplateLocal->InstanceTemplate()->SetInternalFieldCount(1); - resTemplateLocal->PrototypeTemplate()->Set(String::NewFromUtf8(isolate, "end"), FunctionTemplate::New(isolate, res_end)); - resTemplateLocal->PrototypeTemplate()->Set(String::NewFromUtf8(isolate, "writeHeader"), FunctionTemplate::New(isolate, res_writeHeader)); - Local resObjectLocal = resTemplateLocal->GetFunction()->NewInstance(isolate->GetCurrentContext()).ToLocalChecked(); - resTemplate[SSL].Reset(isolate, resObjectLocal); -} + // res.onData(JS function) + // res.write + // res.tryEnd -template -Local getResInstance() { - return Local::New(isolate, resTemplate[std::is_same::value])->Clone(); -} + /* Takes string or arraybuffer, returns this */ + template + static void res_end(const FunctionCallbackInfo &args) { + NativeString data(args.GetIsolate(), args[0]); + getHttpResponse(args)->end(std::string_view(data.getData(), data.getLength())); + + args.GetReturnValue().Set(args.Holder()); + } + + /* Takes key, value. Returns this */ + template + static void res_writeHeader(const FunctionCallbackInfo &args) { + NativeString header(args.GetIsolate(), args[0]); + NativeString value(args.GetIsolate(), args[1]); + getHttpResponse(args)->writeHeader(std::string_view(header.getData(), header.getLength()), + std::string_view(value.getData(), value.getLength())); + + args.GetReturnValue().Set(args.Holder()); + } + + template + static void initResTemplate() { + Local 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)); + resTemplateLocal->PrototypeTemplate()->Set(String::NewFromUtf8(isolate, "writeHeader"), FunctionTemplate::New(isolate, res_writeHeader)); + + /* Create our template */ + Local resObjectLocal = resTemplateLocal->GetFunction()->NewInstance(isolate->GetCurrentContext()).ToLocalChecked(); + resTemplate[SSL].Reset(isolate, resObjectLocal); + } + + template + static Local getResInstance() { + return Local::New(isolate, resTemplate[std::is_same::value])->Clone(); + } +}; + +Persistent HttpResponseWrapper::resTemplate[2]; diff --git a/src/addon.cpp b/src/addon.cpp index a95511ff..cea7f351 100644 --- a/src/addon.cpp +++ b/src/addon.cpp @@ -80,8 +80,8 @@ void Main(Local 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();