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) {
|
app->get(std::string(nativeString.getData(), nativeString.getLength()), [pf](auto *res, auto *req) {
|
||||||
HandleScope hs(isolate);
|
HandleScope hs(isolate);
|
||||||
|
|
||||||
Local<Object> resObject = getResInstance<APP>();
|
Local<Object> resObject = HttpResponseWrapper::getResInstance<APP>();
|
||||||
resObject->SetAlignedPointerInInternalField(0, res);
|
resObject->SetAlignedPointerInInternalField(0, res);
|
||||||
|
|
||||||
Local<Object> reqObject = HttpRequestWrapper::getReqInstance();//Local<Object>::New(isolate, reqTemplate)->Clone();
|
Local<Object> reqObject = HttpRequestWrapper::getReqInstance();
|
||||||
reqObject->SetAlignedPointerInInternalField(0, req);
|
reqObject->SetAlignedPointerInInternalField(0, req);
|
||||||
|
|
||||||
Local<Value> argv[] = {resObject, reqObject};
|
Local<Value> argv[] = {resObject, reqObject};
|
||||||
|
@ -3,6 +3,12 @@
|
|||||||
#include "Utilities.h"
|
#include "Utilities.h"
|
||||||
using namespace v8;
|
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 */
|
/* This one is the same for SSL and non-SSL */
|
||||||
struct HttpRequestWrapper {
|
struct HttpRequestWrapper {
|
||||||
static Persistent<Object> reqTemplate;
|
static Persistent<Object> reqTemplate;
|
||||||
@ -13,8 +19,6 @@ struct HttpRequestWrapper {
|
|||||||
return ((uWS::HttpRequest *) args.Holder()->GetAlignedPointerFromInternalField(0));
|
return ((uWS::HttpRequest *) args.Holder()->GetAlignedPointerFromInternalField(0));
|
||||||
}
|
}
|
||||||
|
|
||||||
// req.onAbort ?
|
|
||||||
|
|
||||||
/* Takes int, returns string (must be in bounds) */
|
/* Takes int, returns string (must be in bounds) */
|
||||||
static void req_getParameter(const FunctionCallbackInfo<Value> &args) {
|
static void req_getParameter(const FunctionCallbackInfo<Value> &args) {
|
||||||
int index = args[0]->Uint32Value();
|
int index = args[0]->Uint32Value();
|
||||||
@ -41,11 +45,6 @@ struct HttpRequestWrapper {
|
|||||||
}
|
}
|
||||||
|
|
||||||
static void initReqTemplate() {
|
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 */
|
/* 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);
|
Local<FunctionTemplate> reqTemplateLocal = FunctionTemplate::New(isolate);
|
||||||
reqTemplateLocal->SetClassName(String::NewFromUtf8(isolate, "uWS.HttpRequest"));
|
reqTemplateLocal->SetClassName(String::NewFromUtf8(isolate, "uWS.HttpRequest"));
|
||||||
@ -61,8 +60,9 @@ struct HttpRequestWrapper {
|
|||||||
reqTemplate.Reset(isolate, reqObjectLocal);
|
reqTemplate.Reset(isolate, reqObjectLocal);
|
||||||
}
|
}
|
||||||
|
|
||||||
//template <class APP>
|
|
||||||
static Local<Object> getReqInstance() {
|
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();
|
return Local<Object>::New(isolate, reqTemplate)->Clone();
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
@ -1,35 +1,42 @@
|
|||||||
// res.onData(JS function)
|
#include "App.h"
|
||||||
// res.write
|
|
||||||
// res.tryEnd
|
|
||||||
|
|
||||||
/* Helping QtCreator */
|
|
||||||
#include <v8.h>
|
#include <v8.h>
|
||||||
#include "Utilities.h"
|
#include "Utilities.h"
|
||||||
using namespace v8;
|
using namespace v8;
|
||||||
|
|
||||||
// this whole template thing could be one struct with members to order tihngs better
|
struct HttpResponseWrapper {
|
||||||
Persistent<Object> resTemplate[2];
|
static Persistent<Object> resTemplate[2];
|
||||||
|
|
||||||
template <bool SSL>
|
template <bool SSL>
|
||||||
void res_end(const FunctionCallbackInfo<Value> &args) {
|
static inline uWS::HttpResponse<SSL> *getHttpResponse(const FunctionCallbackInfo<Value> &args) {
|
||||||
|
return (uWS::HttpResponse<SSL> *) args.Holder()->GetAlignedPointerFromInternalField(0);
|
||||||
|
}
|
||||||
|
|
||||||
|
// res.onData(JS function)
|
||||||
|
// res.write
|
||||||
|
// res.tryEnd
|
||||||
|
|
||||||
|
/* Takes string or arraybuffer, returns this */
|
||||||
|
template <bool SSL>
|
||||||
|
static void res_end(const FunctionCallbackInfo<Value> &args) {
|
||||||
NativeString data(args.GetIsolate(), args[0]);
|
NativeString data(args.GetIsolate(), args[0]);
|
||||||
((uWS::HttpResponse<SSL> *) args.Holder()->GetAlignedPointerFromInternalField(0))->end(std::string_view(data.getData(), data.getLength()));
|
getHttpResponse<SSL>(args)->end(std::string_view(data.getData(), data.getLength()));
|
||||||
|
|
||||||
args.GetReturnValue().Set(args.Holder());
|
args.GetReturnValue().Set(args.Holder());
|
||||||
}
|
}
|
||||||
|
|
||||||
template <bool SSL>
|
/* Takes key, value. Returns this */
|
||||||
void res_writeHeader(const FunctionCallbackInfo<Value> &args) {
|
template <bool SSL>
|
||||||
|
static void res_writeHeader(const FunctionCallbackInfo<Value> &args) {
|
||||||
NativeString header(args.GetIsolate(), args[0]);
|
NativeString header(args.GetIsolate(), args[0]);
|
||||||
NativeString value(args.GetIsolate(), args[1]);
|
NativeString value(args.GetIsolate(), args[1]);
|
||||||
((uWS::HttpResponse<SSL> *) args.Holder()->GetAlignedPointerFromInternalField(0))->writeHeader(std::string_view(header.getData(), header.getLength()),
|
getHttpResponse<SSL>(args)->writeHeader(std::string_view(header.getData(), header.getLength()),
|
||||||
std::string_view(value.getData(), value.getLength()));
|
std::string_view(value.getData(), value.getLength()));
|
||||||
|
|
||||||
args.GetReturnValue().Set(args.Holder());
|
args.GetReturnValue().Set(args.Holder());
|
||||||
}
|
}
|
||||||
|
|
||||||
template <bool SSL>
|
template <bool SSL>
|
||||||
void initResTemplate() {
|
static void initResTemplate() {
|
||||||
Local<FunctionTemplate> resTemplateLocal = FunctionTemplate::New(isolate);
|
Local<FunctionTemplate> resTemplateLocal = FunctionTemplate::New(isolate);
|
||||||
if (SSL) {
|
if (SSL) {
|
||||||
resTemplateLocal->SetClassName(String::NewFromUtf8(isolate, "uWS.SSLHttpResponse"));
|
resTemplateLocal->SetClassName(String::NewFromUtf8(isolate, "uWS.SSLHttpResponse"));
|
||||||
@ -37,14 +44,20 @@ void initResTemplate() {
|
|||||||
resTemplateLocal->SetClassName(String::NewFromUtf8(isolate, "uWS.HttpResponse"));
|
resTemplateLocal->SetClassName(String::NewFromUtf8(isolate, "uWS.HttpResponse"));
|
||||||
}
|
}
|
||||||
resTemplateLocal->InstanceTemplate()->SetInternalFieldCount(1);
|
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, "end"), FunctionTemplate::New(isolate, res_end<SSL>));
|
||||||
resTemplateLocal->PrototypeTemplate()->Set(String::NewFromUtf8(isolate, "writeHeader"), FunctionTemplate::New(isolate, res_writeHeader<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();
|
Local<Object> resObjectLocal = resTemplateLocal->GetFunction()->NewInstance(isolate->GetCurrentContext()).ToLocalChecked();
|
||||||
resTemplate[SSL].Reset(isolate, resObjectLocal);
|
resTemplate[SSL].Reset(isolate, resObjectLocal);
|
||||||
}
|
}
|
||||||
|
|
||||||
template <class APP>
|
template <class APP>
|
||||||
Local<Object> getResInstance() {
|
static Local<Object> getResInstance() {
|
||||||
return Local<Object>::New(isolate, resTemplate[std::is_same<APP, uWS::SSLApp>::value])->Clone();
|
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>();
|
WebSocketWrapper::initWsTemplate<1>();
|
||||||
|
|
||||||
/* Initialize SSL and non-SSL templates */
|
/* Initialize SSL and non-SSL templates */
|
||||||
initResTemplate<0>();
|
HttpResponseWrapper::initResTemplate<0>();
|
||||||
initResTemplate<1>();
|
HttpResponseWrapper::initResTemplate<1>();
|
||||||
|
|
||||||
/* Init a shared request object */
|
/* Init a shared request object */
|
||||||
HttpRequestWrapper::initReqTemplate();
|
HttpRequestWrapper::initReqTemplate();
|
||||||
|
Loading…
Reference in New Issue
Block a user