Properly support SSL and non-SSL

This commit is contained in:
Alex Hultman 2019-01-08 22:04:56 +01:00
parent ba90a20e0f
commit 31278c13f3
3 changed files with 104 additions and 50 deletions

View File

@ -5,18 +5,19 @@ const port = 9001;
// nice milestone to pass autobahn on both ssl and non-ssl with an without compression // nice milestone to pass autobahn on both ssl and non-ssl with an without compression
const app = uWS./*SSL*/App({ const app = uWS.SSLApp({
key_file_name: '/home/alexhultman/key.pem', key_file_name: '/home/alexhultman/key.pem',
cert_file_name: '/home/alexhultman/cert.pem', cert_file_name: '/home/alexhultman/cert.pem',
passphrase: '1234' passphrase: '1234'
}).get('/hello', (res, req) => { }).get('/hello', (res, req) => {
res.end('Hejdå!'); res.end('Hejdå!');
}).ws('/*', { }).ws('/*', {
maxPayloadLength: 16 * 1024 * 1024,
open: (ws, req) => { open: (ws, req) => {
console.log(ws); console.log(ws);
}, },
message: (ws, message, opCode) => { message: (ws, message, isBinary) => {
ws.send(message, opCode); // need op to pass autobahn (guess binary on off is enough?) ws.send(message, isBinary);
} }
}).listen(port, (token) => { }).listen(port, (token) => {
if (token) { if (token) {

View File

@ -19,9 +19,6 @@
#include <v8.h> #include <v8.h>
using namespace v8; using namespace v8;
Isolate *isolate; Isolate *isolate;
Persistent<Object> resTemplate;
Persistent<Object> reqTemplate;
Persistent<Object> wsTemplate;
#include <iostream> #include <iostream>
#include <vector> #include <vector>
@ -68,29 +65,100 @@ public:
} }
}; };
// also wrap all of this in some common struct
Persistent<Object> wsTemplate[2];
/* WebSocket send */
template <bool SSL>
void uWS_WebSocket_send(const FunctionCallbackInfo<Value> &args) {
NativeString nativeString(args.GetIsolate(), args[0]);
bool isBinary = args[1]->Int32Value();
bool ok = ((uWS::WebSocket<SSL, true> *) args.Holder()->GetAlignedPointerFromInternalField(0))->send(
std::string_view(nativeString.getData(), nativeString.getLength()), isBinary ? uWS::OpCode::BINARY : uWS::OpCode::TEXT
);
args.GetReturnValue().Set(Boolean::New(isolate, ok));
}
template <bool SSL>
void initWsTemplate() {
Local<FunctionTemplate> wsTemplateLocal = FunctionTemplate::New(isolate);
if (SSL) {
wsTemplateLocal->SetClassName(String::NewFromUtf8(isolate, "uWS.SSLWebSocket"));
} else {
wsTemplateLocal->SetClassName(String::NewFromUtf8(isolate, "uWS.WebSocket"));
}
wsTemplateLocal->InstanceTemplate()->SetInternalFieldCount(1);
wsTemplateLocal->PrototypeTemplate()->Set(String::NewFromUtf8(isolate, "send"), FunctionTemplate::New(isolate, uWS_WebSocket_send<SSL>));
Local<Object> wsObjectLocal = wsTemplateLocal->GetFunction()->NewInstance(isolate->GetCurrentContext()).ToLocalChecked();
wsTemplate[SSL].Reset(isolate, wsObjectLocal);
}
template <class APP>
Local<Object> getWsInstance() {
return Local<Object>::New(isolate, wsTemplate[std::is_same<APP, uWS::SSLApp>::value])->Clone();
}
/*
template <bool SSL>
struct HttpResponseWrapper {
HttpResponseWrapper() {
}
};*/
// this whole template thing could be one struct with members to order tihngs better
Persistent<Object> resTemplate[2];
template <bool SSL>
void res_end(const FunctionCallbackInfo<Value> &args) { void res_end(const FunctionCallbackInfo<Value> &args) {
// you might want to do extra work here to swap to tryEnd if passed a Buffer?
// or always use tryEnd and simply grab the object as persistent?
NativeString data(args.GetIsolate(), args[0]); NativeString data(args.GetIsolate(), args[0]);
((uWS::HttpResponse<false> *) args.Holder()->GetAlignedPointerFromInternalField(0))->end(std::string_view(data.getData(), data.getLength())); ((uWS::HttpResponse<SSL> *) args.Holder()->GetAlignedPointerFromInternalField(0))->end(std::string_view(data.getData(), data.getLength()));
// Return this
args.GetReturnValue().Set(args.Holder()); args.GetReturnValue().Set(args.Holder());
} }
template <bool SSL>
void res_writeHeader(const FunctionCallbackInfo<Value> &args) { void res_writeHeader(const FunctionCallbackInfo<Value> &args) {
// get string
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()),
((uWS::HttpResponse<false> *) args.Holder()->GetAlignedPointerFromInternalField(0))->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>
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);
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);
}
template <class APP>
Local<Object> getResInstance() {
return Local<Object>::New(isolate, resTemplate[std::is_same<APP, uWS::SSLApp>::value])->Clone();
}
Persistent<Object> reqTemplate;
void req_getHeader(const FunctionCallbackInfo<Value> &args) { void req_getHeader(const FunctionCallbackInfo<Value> &args) {
// get string // get string
NativeString data(args.GetIsolate(), args[0]); NativeString data(args.GetIsolate(), args[0]);
@ -101,18 +169,6 @@ void req_getHeader(const FunctionCallbackInfo<Value> &args) {
args.GetReturnValue().Set(String::NewFromUtf8(isolate, header.data(), v8::String::kNormalString, header.length())); args.GetReturnValue().Set(String::NewFromUtf8(isolate, header.data(), v8::String::kNormalString, header.length()));
} }
/* WebSocket send */
// not properly templated, just like the httpsockets are not!
void uWS_WebSocket_send(const FunctionCallbackInfo<Value> &args) {
uWS::WebSocket<false, true> *ws = (uWS::WebSocket<false, true> *) args.Holder()->GetAlignedPointerFromInternalField(0);
NativeString nativeString(args.GetIsolate(), args[0]);
//std::cout << std::string_view(nativeString.getData(), nativeString.getLength()) << std::endl;
ws->send(std::string_view(nativeString.getData(), nativeString.getLength()), uWS::OpCode::TEXT);
}
/* uWS.App.ws('/pattern', options) */ /* uWS.App.ws('/pattern', options) */
template <typename APP> template <typename APP>
void uWS_App_ws(const FunctionCallbackInfo<Value> &args) { void uWS_App_ws(const FunctionCallbackInfo<Value> &args) {
@ -123,6 +179,8 @@ void uWS_App_ws(const FunctionCallbackInfo<Value> &args) {
Persistent<Function> *openPf = new Persistent<Function>(); Persistent<Function> *openPf = new Persistent<Function>();
Persistent<Function> *messagePf = new Persistent<Function>(); Persistent<Function> *messagePf = new Persistent<Function>();
int maxPayloadLength = 0;
struct PerSocketData { struct PerSocketData {
Persistent<Object> *socketPf; Persistent<Object> *socketPf;
}; };
@ -131,6 +189,9 @@ void uWS_App_ws(const FunctionCallbackInfo<Value> &args) {
if (args.Length() == 2) { if (args.Length() == 2) {
Local<Object> behaviorObject = Local<Object>::Cast(args[1]); Local<Object> behaviorObject = Local<Object>::Cast(args[1]);
/* maxPayloadLength */
maxPayloadLength = behaviorObject->Get(String::NewFromUtf8(isolate, "maxPayloadLength"))->Int32Value();
/* Open */ /* Open */
openPf->Reset(args.GetIsolate(), Local<Function>::Cast(behaviorObject->Get(String::NewFromUtf8(isolate, "open")))); openPf->Reset(args.GetIsolate(), Local<Function>::Cast(behaviorObject->Get(String::NewFromUtf8(isolate, "open"))));
/* Message */ /* Message */
@ -139,13 +200,13 @@ void uWS_App_ws(const FunctionCallbackInfo<Value> &args) {
app->template ws<PerSocketData>(std::string(nativeString.getData(), nativeString.getLength()), { app->template ws<PerSocketData>(std::string(nativeString.getData(), nativeString.getLength()), {
/*.compression = uWS::SHARED_COMPRESSOR, /*.compression = uWS::SHARED_COMPRESSOR,*/
.maxPayloadLength = 16 * 1024,*/ .maxPayloadLength = maxPayloadLength,
.open = [openPf](auto *ws, auto *req) { .open = [openPf](auto *ws, auto *req) {
HandleScope hs(isolate); HandleScope hs(isolate);
/* Create a new websocket object */ /* Create a new websocket object */
Local<Object> wsObject = Local<Object>::New(isolate, wsTemplate)->Clone(); Local<Object> wsObject = getWsInstance<APP>();
wsObject->SetAlignedPointerInInternalField(0, ws); wsObject->SetAlignedPointerInInternalField(0, ws);
/* Attach a new V8 object with pointer to us, to us */ /* Attach a new V8 object with pointer to us, to us */
@ -160,8 +221,11 @@ void uWS_App_ws(const FunctionCallbackInfo<Value> &args) {
HandleScope hs(isolate); HandleScope hs(isolate);
PerSocketData *perSocketData = (PerSocketData *) ws->getUserData(); PerSocketData *perSocketData = (PerSocketData *) ws->getUserData();
Local<Value> argv[2] = {Local<Object>::New(isolate, *(perSocketData->socketPf)), ArrayBuffer::New(isolate, (void *) message.data(), message.length())}; // ws, message, opCode Local<Value> argv[3] = {Local<Object>::New(isolate, *(perSocketData->socketPf)),
Local<Function>::New(isolate, *messagePf)->Call(isolate->GetCurrentContext()->Global(), 2, argv); ArrayBuffer::New(isolate, (void *) message.data(), message.length()),
Boolean::New(isolate, opCode == uWS::OpCode::BINARY)
};
Local<Function>::New(isolate, *messagePf)->Call(isolate->GetCurrentContext()->Global(), 3, argv);
}/* }/*
.drain = []() {}, .drain = []() {},
.ping = []() {}, .ping = []() {},
@ -188,7 +252,7 @@ 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 = Local<Object>::New(isolate, resTemplate)->Clone(); Local<Object> resObject = getResInstance<APP>();
resObject->SetAlignedPointerInInternalField(0, res); resObject->SetAlignedPointerInInternalField(0, res);
Local<Object> reqObject = Local<Object>::New(isolate, reqTemplate)->Clone(); Local<Object> reqObject = Local<Object>::New(isolate, reqTemplate)->Clone();
@ -324,23 +388,12 @@ void Main(Local<Object> exports) {
exports->Set(String::NewFromUtf8(isolate, "nextTick"), FunctionTemplate::New(isolate, nextTick)->GetFunction()); exports->Set(String::NewFromUtf8(isolate, "nextTick"), FunctionTemplate::New(isolate, nextTick)->GetFunction());
/* The template for websockets */ /* The template for websockets */
Local<FunctionTemplate> wsTemplateLocal = FunctionTemplate::New(isolate); initWsTemplate<0>();
wsTemplateLocal->SetClassName(String::NewFromUtf8(isolate, "uWS.WebSocket")); initWsTemplate<1>();
wsTemplateLocal->InstanceTemplate()->SetInternalFieldCount(1);
wsTemplateLocal->PrototypeTemplate()->Set(String::NewFromUtf8(isolate, "send"), FunctionTemplate::New(isolate, uWS_WebSocket_send));
Local<Object> wsObjectLocal = wsTemplateLocal->GetFunction()->NewInstance(isolate->GetCurrentContext()).ToLocalChecked(); /* Initialize SSL and non-SSL templates */
wsTemplate.Reset(isolate, wsObjectLocal); initResTemplate<0>();
initResTemplate<1>();
// HttpResponse template (not templated)
Local<FunctionTemplate> resTemplateLocal = FunctionTemplate::New(isolate);
resTemplateLocal->SetClassName(String::NewFromUtf8(isolate, "uWS.HttpResponse"));
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<Object> resObjectLocal = resTemplateLocal->GetFunction()->NewInstance(isolate->GetCurrentContext()).ToLocalChecked();
resTemplate.Reset(isolate, resObjectLocal);
// Request template (do we need to clone this?) // Request template (do we need to clone this?)
Local<FunctionTemplate> reqTemplateLocal = FunctionTemplate::New(isolate); Local<FunctionTemplate> reqTemplateLocal = FunctionTemplate::New(isolate);

@ -1 +1 @@
Subproject commit 46537f6f2d500bc611e5dc6bb78a7a5da3baea01 Subproject commit 8efea340e66bc91efbe31b618a446f821c214bbf