Wrap req.getParameter, req.getUrl
This commit is contained in:
parent
647a6ef12c
commit
80bc15915b
@ -7,8 +7,10 @@ const app = uWS./*SSL*/App({
|
|||||||
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('/static/:param', (res, req) => {
|
||||||
|
res.end('Hello, your url is ' + req.getUrl() + ". The param1 is " + req.getParameter(0));
|
||||||
}).get('/hello', (res, req) => {
|
}).get('/hello', (res, req) => {
|
||||||
res.end('Hejdå!');
|
res.end('Hej, du är på url: ' + req.getUrl());
|
||||||
}).ws('/*', {
|
}).ws('/*', {
|
||||||
compression: 0,
|
compression: 0,
|
||||||
maxPayloadLength: 16 * 1024 * 1024,
|
maxPayloadLength: 16 * 1024 * 1024,
|
||||||
|
@ -145,7 +145,7 @@ void uWS_App_get(const FunctionCallbackInfo<Value> &args) {
|
|||||||
Local<Object> resObject = getResInstance<APP>();
|
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 = HttpRequestWrapper::getReqInstance();//Local<Object>::New(isolate, reqTemplate)->Clone();
|
||||||
reqObject->SetAlignedPointerInInternalField(0, req);
|
reqObject->SetAlignedPointerInInternalField(0, req);
|
||||||
|
|
||||||
Local<Value> argv[] = {resObject, reqObject};
|
Local<Value> argv[] = {resObject, reqObject};
|
||||||
|
@ -1,34 +1,70 @@
|
|||||||
// req.getParam(int)
|
#include "App.h"
|
||||||
// req.getUrl()
|
|
||||||
// req.onAbort ?
|
|
||||||
|
|
||||||
/* Helping QtCreator */
|
|
||||||
#include <v8.h>
|
#include <v8.h>
|
||||||
#include "Utilities.h"
|
#include "Utilities.h"
|
||||||
using namespace v8;
|
using namespace v8;
|
||||||
|
|
||||||
/* The only thing this req needs is getHeader and similar, getParameter, getUrl and so on */
|
/* This one is the same for SSL and non-SSL */
|
||||||
Persistent<Object> reqTemplate;
|
struct HttpRequestWrapper {
|
||||||
|
static Persistent<Object> reqTemplate;
|
||||||
|
|
||||||
void req_getHeader(const FunctionCallbackInfo<Value> &args) {
|
// todo: refuse all function calls if we are not inside correct callback
|
||||||
|
|
||||||
|
static inline uWS::HttpRequest *getHttpRequest(const FunctionCallbackInfo<Value> &args) {
|
||||||
|
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();
|
||||||
|
std::string_view parameter = getHttpRequest(args)->getParameter(index);
|
||||||
|
|
||||||
|
args.GetReturnValue().Set(String::NewFromUtf8(isolate, parameter.data(), v8::String::kNormalString, parameter.length()));
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Takes nothing, returns string */
|
||||||
|
static void req_getUrl(const FunctionCallbackInfo<Value> &args) {
|
||||||
|
std::string_view url = getHttpRequest(args)->getUrl();
|
||||||
|
|
||||||
|
args.GetReturnValue().Set(String::NewFromUtf8(isolate, url.data(), v8::String::kNormalString, url.length()));
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Takes String, returns String */
|
||||||
|
static void req_getHeader(const FunctionCallbackInfo<Value> &args) {
|
||||||
NativeString data(args.GetIsolate(), args[0]);
|
NativeString data(args.GetIsolate(), args[0]);
|
||||||
char *buf = data.getData(); int length = data.getLength();
|
char *buf = data.getData(); int length = data.getLength();
|
||||||
|
|
||||||
std::string_view header = ((uWS::HttpRequest *) args.Holder()->GetAlignedPointerFromInternalField(0))->getHeader(std::string_view(buf, length));
|
std::string_view header = getHttpRequest(args)->getHeader(std::string_view(buf, length));
|
||||||
|
|
||||||
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()));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static void initReqTemplate() {
|
||||||
|
/* The only thing this req needs is getHeader and similar, getParameter, getUrl and so on */
|
||||||
|
|
||||||
void initReqTemplate() {
|
|
||||||
/*reqTemplateLocal->PrototypeTemplate()->SetAccessor(String::NewFromUtf8(isolate, "url"), Request::url);
|
/*reqTemplateLocal->PrototypeTemplate()->SetAccessor(String::NewFromUtf8(isolate, "url"), Request::url);
|
||||||
reqTemplateLocal->PrototypeTemplate()->SetAccessor(String::NewFromUtf8(isolate, "method"), Request::method);*/
|
reqTemplateLocal->PrototypeTemplate()->SetAccessor(String::NewFromUtf8(isolate, "method"), Request::method);*/
|
||||||
|
|
||||||
// Request template (do we need to clone this?)
|
/* 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"));
|
||||||
reqTemplateLocal->InstanceTemplate()->SetInternalFieldCount(1);
|
reqTemplateLocal->InstanceTemplate()->SetInternalFieldCount(1);
|
||||||
reqTemplateLocal->PrototypeTemplate()->Set(String::NewFromUtf8(isolate, "getHeader"), FunctionTemplate::New(isolate, req_getHeader));
|
|
||||||
|
|
||||||
|
/* Register our functions */
|
||||||
|
reqTemplateLocal->PrototypeTemplate()->Set(String::NewFromUtf8(isolate, "getHeader"), FunctionTemplate::New(isolate, req_getHeader));
|
||||||
|
reqTemplateLocal->PrototypeTemplate()->Set(String::NewFromUtf8(isolate, "getParameter"), FunctionTemplate::New(isolate, req_getParameter));
|
||||||
|
reqTemplateLocal->PrototypeTemplate()->Set(String::NewFromUtf8(isolate, "getUrl"), FunctionTemplate::New(isolate, req_getUrl));
|
||||||
|
|
||||||
|
/* Create the template */
|
||||||
Local<Object> reqObjectLocal = reqTemplateLocal->GetFunction()->NewInstance(isolate->GetCurrentContext()).ToLocalChecked();
|
Local<Object> reqObjectLocal = reqTemplateLocal->GetFunction()->NewInstance(isolate->GetCurrentContext()).ToLocalChecked();
|
||||||
reqTemplate.Reset(isolate, reqObjectLocal);
|
reqTemplate.Reset(isolate, reqObjectLocal);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//template <class APP>
|
||||||
|
static Local<Object> getReqInstance() {
|
||||||
|
return Local<Object>::New(isolate, reqTemplate)->Clone();
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
Persistent<Object> HttpRequestWrapper::reqTemplate;
|
||||||
|
@ -84,7 +84,7 @@ void Main(Local<Object> exports) {
|
|||||||
initResTemplate<1>();
|
initResTemplate<1>();
|
||||||
|
|
||||||
/* Init a shared request object */
|
/* Init a shared request object */
|
||||||
initReqTemplate();
|
HttpRequestWrapper::initReqTemplate();
|
||||||
}
|
}
|
||||||
|
|
||||||
/* This is required when building as a Node.js addon */
|
/* This is required when building as a Node.js addon */
|
||||||
|
Loading…
Reference in New Issue
Block a user