From b4ca0a487b71a8191ef550bbbf00f9c9d1d8f790 Mon Sep 17 00:00:00 2001 From: Alex Hultman Date: Sun, 3 Mar 2019 07:42:10 +0100 Subject: [PATCH] Add getRemoteAddress to res and ws objects --- src/HttpResponseWrapper.h | 13 +++++++++++++ src/WebSocketWrapper.h | 13 +++++++++++++ uWebSockets | 2 +- 3 files changed, 27 insertions(+), 1 deletion(-) diff --git a/src/HttpResponseWrapper.h b/src/HttpResponseWrapper.h index 3cc3b5d..640d94c 100644 --- a/src/HttpResponseWrapper.h +++ b/src/HttpResponseWrapper.h @@ -78,6 +78,18 @@ struct HttpResponseWrapper { } } + /* Takes nothing, returns arraybuffer */ + template + static void res_getRemoteAddress(const FunctionCallbackInfo &args) { + auto *res = getHttpResponse(args); + if (res) { + std::string_view ip = res->getRemoteAddress(); + + /* Todo: we need to pass a copy here */ + args.GetReturnValue().Set(ArrayBuffer::New(isolate, (void *) ip.data(), ip.length()/*, ArrayBufferCreationMode::kInternalized*/)); + } + } + /* Returns the current write offset */ template static void res_getWriteOffset(const FunctionCallbackInfo &args) { @@ -224,6 +236,7 @@ struct HttpResponseWrapper { resTemplateLocal->PrototypeTemplate()->Set(String::NewFromUtf8(isolate, "onWritable"), FunctionTemplate::New(isolate, res_onWritable)); resTemplateLocal->PrototypeTemplate()->Set(String::NewFromUtf8(isolate, "onAborted"), FunctionTemplate::New(isolate, res_onAborted)); resTemplateLocal->PrototypeTemplate()->Set(String::NewFromUtf8(isolate, "onData"), FunctionTemplate::New(isolate, res_onData)); + resTemplateLocal->PrototypeTemplate()->Set(String::NewFromUtf8(isolate, "getRemoteAddress"), FunctionTemplate::New(isolate, res_getRemoteAddress)); /* Create our template */ Local resObjectLocal = resTemplateLocal->GetFunction()->NewInstance(isolate->GetCurrentContext()).ToLocalChecked(); diff --git a/src/WebSocketWrapper.h b/src/WebSocketWrapper.h index 3586552..f38d677 100644 --- a/src/WebSocketWrapper.h +++ b/src/WebSocketWrapper.h @@ -75,6 +75,18 @@ struct WebSocketWrapper { } } + /* Takes nothing returns arraybuffer */ + template + static void uWS_WebSocket_getRemoteAddress(const FunctionCallbackInfo &args) { + auto *ws = getWebSocket(args); + if (ws) { + std::string_view ip = ws->getRemoteAddress(); + + /* Todo: we need to pass a copy here */ + args.GetReturnValue().Set(ArrayBuffer::New(isolate, (void *) ip.data(), ip.length()/*, ArrayBufferCreationMode::kInternalized*/)); + } + } + /* Takes nothing, returns integer */ template static void uWS_WebSocket_getBufferedAmount(const FunctionCallbackInfo &args) { @@ -116,6 +128,7 @@ struct WebSocketWrapper { wsTemplateLocal->PrototypeTemplate()->Set(String::NewFromUtf8(isolate, "end"), FunctionTemplate::New(isolate, uWS_WebSocket_end)); wsTemplateLocal->PrototypeTemplate()->Set(String::NewFromUtf8(isolate, "close"), FunctionTemplate::New(isolate, uWS_WebSocket_close)); wsTemplateLocal->PrototypeTemplate()->Set(String::NewFromUtf8(isolate, "getBufferedAmount"), FunctionTemplate::New(isolate, uWS_WebSocket_getBufferedAmount)); + wsTemplateLocal->PrototypeTemplate()->Set(String::NewFromUtf8(isolate, "getRemoteAddress"), FunctionTemplate::New(isolate, uWS_WebSocket_getRemoteAddress)); wsTemplateLocal->PrototypeTemplate()->Set(String::NewFromUtf8(isolate, "subscribe"), FunctionTemplate::New(isolate, uWS_WebSocket_subscribe)); wsTemplateLocal->PrototypeTemplate()->Set(String::NewFromUtf8(isolate, "publish"), FunctionTemplate::New(isolate, uWS_WebSocket_publish)); diff --git a/uWebSockets b/uWebSockets index 35e6bad..495ccfc 160000 --- a/uWebSockets +++ b/uWebSockets @@ -1 +1 @@ -Subproject commit 35e6bad33b20c3e630e53b16c16546d2572bccfa +Subproject commit 495ccfc07602385c039a9edc9978167d9fb122ae