mirror of
https://github.com/Ylianst/MeshAgent
synced 2025-12-10 13:23:41 +00:00
removed files that aren't used
This commit is contained in:
4
makefile
4
makefile
@@ -61,8 +61,8 @@ SOURCES += microstack/ILibRemoteLogging.c microstack/ILibWebClient.c microstack/
|
|||||||
SOURCES += microstack/ILibWrapperWebRTC.c microstack/ILibSimpleDataStore.c microstack/ILibProcessPipe.c microstack/ILibIPAddressMonitor.c
|
SOURCES += microstack/ILibWrapperWebRTC.c microstack/ILibSimpleDataStore.c microstack/ILibProcessPipe.c microstack/ILibIPAddressMonitor.c
|
||||||
SOURCES += microscript/duktape.c microscript/duk_module_duktape.c microscript/ILibDuktape_DuplexStream.c microscript/ILibDuktape_Helpers.c
|
SOURCES += microscript/duktape.c microscript/duk_module_duktape.c microscript/ILibDuktape_DuplexStream.c microscript/ILibDuktape_Helpers.c
|
||||||
SOURCES += microscript/ILibDuktape_http.c microscript/ILibDuktape_net.c microscript/ILibDuktape_ReadableStream.c microscript/ILibDuktape_WritableStream.c
|
SOURCES += microscript/ILibDuktape_http.c microscript/ILibDuktape_net.c microscript/ILibDuktape_ReadableStream.c microscript/ILibDuktape_WritableStream.c
|
||||||
SOURCES += microscript/ILibDuktapeModSearch.c microscript/ILibParsers_Duktape.c microscript/ILibDuktape_WebRTC.c
|
SOURCES += microscript/ILibDuktapeModSearch.c microscript/ILibDuktape_WebRTC.c
|
||||||
SOURCES += microscript/ILibWebServer_Duktape.c microscript/ILibDuktape_SimpleDataStore.c microscript/ILibDuktape_GenericMarshal.c
|
SOURCES += microscript/ILibDuktape_SimpleDataStore.c microscript/ILibDuktape_GenericMarshal.c
|
||||||
SOURCES += microscript/ILibDuktape_fs.c microscript/ILibDuktape_SHA256.c microscript/ILibduktape_EventEmitter.c
|
SOURCES += microscript/ILibDuktape_fs.c microscript/ILibDuktape_SHA256.c microscript/ILibduktape_EventEmitter.c
|
||||||
SOURCES += microscript/ILibDuktape_EncryptionStream.c microscript/ILibDuktape_Polyfills.c microscript/ILibDuktape_Dgram.c
|
SOURCES += microscript/ILibDuktape_EncryptionStream.c microscript/ILibDuktape_Polyfills.c microscript/ILibDuktape_Dgram.c
|
||||||
SOURCES += microscript/ILibDuktape_ScriptContainer.c microscript/ILibDuktape_MemoryStream.c microscript/ILibDuktape_NetworkMonitor.c
|
SOURCES += microscript/ILibDuktape_ScriptContainer.c microscript/ILibDuktape_MemoryStream.c microscript/ILibDuktape_NetworkMonitor.c
|
||||||
|
|||||||
@@ -1,265 +0,0 @@
|
|||||||
/*
|
|
||||||
Copyright 2006 - 2017 Intel Corporation
|
|
||||||
|
|
||||||
Licensed under the Apache License, Version 2.0 (the "License");
|
|
||||||
you may not use this file except in compliance with the License.
|
|
||||||
You may obtain a copy of the License at
|
|
||||||
|
|
||||||
http://www.apache.org/licenses/LICENSE-2.0
|
|
||||||
|
|
||||||
Unless required by applicable law or agreed to in writing, software
|
|
||||||
distributed under the License is distributed on an "AS IS" BASIS,
|
|
||||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
||||||
See the License for the specific language governing permissions and
|
|
||||||
limitations under the License.
|
|
||||||
*/
|
|
||||||
|
|
||||||
#ifdef WIN32
|
|
||||||
#include <WinSock2.h>
|
|
||||||
#include <WS2tcpip.h>
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#include "microstack/ILibParsers.h"
|
|
||||||
#include "microstack/ILibAsyncServerSocket.h"
|
|
||||||
#include "ILibAsyncSocket_Duktape.h"
|
|
||||||
#include "ILibDuktape_Helpers.h"
|
|
||||||
|
|
||||||
extern char *Duktape_GetStashKey(void* value);
|
|
||||||
extern char* Duktape_GetBuffer(duk_context *ctx, duk_idx_t i, duk_size_t *bufLen);
|
|
||||||
duk_idx_t ILibAsyncSocket_Duktape_PUSH_AsyncSocketModule(duk_context *ctx, ILibAsyncSocket_SocketModule module);
|
|
||||||
|
|
||||||
typedef struct ILibAsyncSocket_Duktape_Bindings
|
|
||||||
{
|
|
||||||
duk_context *context;
|
|
||||||
void *OnData;
|
|
||||||
void *OnConnect;
|
|
||||||
void *OnDisconnect;
|
|
||||||
void *OnSendOK;
|
|
||||||
}ILibAsyncSocket_Duktape_Bindings;
|
|
||||||
|
|
||||||
void ILibAsyncSocket_Duktape_OnData(ILibAsyncSocket_SocketModule socketModule, char* buffer, int *p_beginPointer, int endPointer, ILibAsyncSocket_OnInterrupt* OnInterrupt, void **user, int *PAUSE)
|
|
||||||
{
|
|
||||||
ILibAsyncSocket_Duktape_Bindings *bindings = (ILibAsyncSocket_Duktape_Bindings*)((ILibChain_Link*)socketModule)->ExtraMemoryPtr;
|
|
||||||
if (bindings->OnData == NULL) { *p_beginPointer = endPointer; return; }
|
|
||||||
|
|
||||||
duk_push_heapptr(bindings->context, bindings->OnData); // [ptr]
|
|
||||||
ILibAsyncSocket_Duktape_PUSH_AsyncSocketModule(bindings->context, socketModule); // [ptr][obj]
|
|
||||||
if (duk_has_prop_string(bindings->context, -1, "BufferPtr"))
|
|
||||||
{
|
|
||||||
duk_get_prop_string(bindings->context, -1, "BufferPtr"); // [ptr][obj][buffer]
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
duk_push_external_buffer(bindings->context); // [ptr][obj][buffer]
|
|
||||||
duk_dup(bindings->context, -1); // [ptr][obj][buffer][buffer]
|
|
||||||
duk_put_prop_string(bindings->context, -3, "BufferPtr"); // [ptr][obj][buffer]
|
|
||||||
}
|
|
||||||
duk_config_buffer(bindings->context, -1, buffer + *p_beginPointer, endPointer);
|
|
||||||
duk_push_int(bindings->context, endPointer); // [ptr][obj][buffer][endPointer]
|
|
||||||
if (duk_pcall(bindings->context, 3) == 0) // [retVal]
|
|
||||||
{
|
|
||||||
if (duk_is_undefined(bindings->context, -1)) { *p_beginPointer = endPointer; duk_pop(bindings->context); }
|
|
||||||
else if (duk_is_number(bindings->context, -1))
|
|
||||||
{
|
|
||||||
int val = duk_to_int(bindings->context, -1);
|
|
||||||
if (val < 0) { *PAUSE = 1; }
|
|
||||||
else { *p_beginPointer = val; }
|
|
||||||
duk_pop(bindings->context); // ...
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
ILibDuktape_Process_UncaughtException(bindings->context);
|
|
||||||
duk_pop(bindings->context);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
void ILibAsyncSocket_Duktape_OnConnect(ILibAsyncSocket_SocketModule socketModule, int Connected, void *user)
|
|
||||||
{
|
|
||||||
ILibAsyncSocket_Duktape_Bindings *bindings = (ILibAsyncSocket_Duktape_Bindings*)((ILibChain_Link*)socketModule)->ExtraMemoryPtr;
|
|
||||||
if (bindings->OnConnect != NULL)
|
|
||||||
{
|
|
||||||
duk_push_heapptr(bindings->context, bindings->OnConnect); // [ptr]
|
|
||||||
ILibAsyncSocket_Duktape_PUSH_AsyncSocketModule(bindings->context, socketModule); // [ptr][obj]
|
|
||||||
duk_push_int(bindings->context, Connected); // [ptr][obj][Connected]
|
|
||||||
if (duk_pcall(bindings->context, 2) != 0) // [retVal]
|
|
||||||
{
|
|
||||||
ILibDuktape_Process_UncaughtException(bindings->context);
|
|
||||||
}
|
|
||||||
duk_pop(bindings->context); // ...
|
|
||||||
}
|
|
||||||
}
|
|
||||||
void ILibAsyncSocket_Duktape_OnDisconnect(ILibAsyncSocket_SocketModule socketModule, void *user)
|
|
||||||
{
|
|
||||||
ILibAsyncSocket_Duktape_Bindings *bindings = (ILibAsyncSocket_Duktape_Bindings*)((ILibChain_Link*)socketModule)->ExtraMemoryPtr;
|
|
||||||
if (bindings->OnDisconnect != NULL)
|
|
||||||
{
|
|
||||||
duk_push_heapptr(bindings->context, bindings->OnDisconnect); // [ptr]
|
|
||||||
ILibAsyncSocket_Duktape_PUSH_AsyncSocketModule(bindings->context, socketModule); // [ptr][obj]
|
|
||||||
if (duk_pcall(bindings->context, 1) != 0) // [...]
|
|
||||||
{
|
|
||||||
ILibDuktape_Process_UncaughtException(bindings->context);
|
|
||||||
}
|
|
||||||
duk_pop(bindings->context); //
|
|
||||||
}
|
|
||||||
}
|
|
||||||
void ILibAsyncSocket_Duktape_OnSendOK(ILibAsyncSocket_SocketModule socketModule, void *user)
|
|
||||||
{
|
|
||||||
ILibAsyncSocket_Duktape_Bindings *bindings = (ILibAsyncSocket_Duktape_Bindings*)((ILibChain_Link*)socketModule)->ExtraMemoryPtr;
|
|
||||||
if (bindings->OnSendOK != NULL)
|
|
||||||
{
|
|
||||||
duk_push_heapptr(bindings->context, bindings->OnSendOK); // [ptr]
|
|
||||||
ILibAsyncSocket_Duktape_PUSH_AsyncSocketModule(bindings->context, socketModule); // [ptr][obj]
|
|
||||||
if (duk_pcall(bindings->context, 1) != 0) // [...]
|
|
||||||
{
|
|
||||||
ILibDuktape_Process_UncaughtException(bindings->context);
|
|
||||||
}
|
|
||||||
duk_pop(bindings->context); //
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
duk_ret_t ILibAsyncSocket_Duktape_Create(duk_context *ctx)
|
|
||||||
{
|
|
||||||
// ILibCreateAsyncSocketModuleWithMemory(void *Chain, int initialBufferSize, ILibAsyncSocket_OnData OnData, ILibAsyncSocket_OnConnect OnConnect, ILibAsyncSocket_OnDisconnect OnDisconnect, ILibAsyncSocket_OnSendOK OnSendOK, int UserMappedMemorySize)
|
|
||||||
|
|
||||||
void *chain;
|
|
||||||
int initialBufferSize = duk_require_int(ctx, 0);
|
|
||||||
void *OnData = duk_is_null(ctx, 1) ? NULL : duk_require_heapptr(ctx, 1);
|
|
||||||
void *OnConnect = duk_is_null(ctx, 2) ? NULL : duk_require_heapptr(ctx, 2);
|
|
||||||
void *OnDisconnect = duk_is_null(ctx, 3) ? NULL : duk_require_heapptr(ctx, 3);
|
|
||||||
void *OnSendOK = duk_is_null(ctx, 4) ? NULL : duk_require_heapptr(ctx, 4);
|
|
||||||
ILibAsyncSocket_SocketModule module;
|
|
||||||
|
|
||||||
duk_push_current_function(ctx); // [func]
|
|
||||||
duk_get_prop_string(ctx, -1, "chain"); // [func][chain]
|
|
||||||
chain = duk_to_pointer(ctx, -1);
|
|
||||||
|
|
||||||
module = ILibCreateAsyncSocketModuleWithMemory(chain, initialBufferSize, ILibAsyncSocket_Duktape_OnData, ILibAsyncSocket_Duktape_OnConnect, ILibAsyncSocket_Duktape_OnDisconnect, ILibAsyncSocket_Duktape_OnSendOK, sizeof(ILibAsyncSocket_Duktape_Bindings));
|
|
||||||
((ILibAsyncSocket_Duktape_Bindings*)((ILibChain_Link*)module)->ExtraMemoryPtr)->context = ctx;
|
|
||||||
((ILibAsyncSocket_Duktape_Bindings*)((ILibChain_Link*)module)->ExtraMemoryPtr)->OnConnect = OnConnect;
|
|
||||||
((ILibAsyncSocket_Duktape_Bindings*)((ILibChain_Link*)module)->ExtraMemoryPtr)->OnData = OnData;
|
|
||||||
((ILibAsyncSocket_Duktape_Bindings*)((ILibChain_Link*)module)->ExtraMemoryPtr)->OnDisconnect = OnDisconnect;
|
|
||||||
((ILibAsyncSocket_Duktape_Bindings*)((ILibChain_Link*)module)->ExtraMemoryPtr)->OnSendOK = OnSendOK;
|
|
||||||
|
|
||||||
ILibAsyncSocket_Duktape_PUSH_AsyncSocketModule(ctx, module); // [obj]
|
|
||||||
duk_dup(ctx, 1); // [obj][OnData]
|
|
||||||
duk_put_prop_string(ctx, -2, "OnDataPtr"); // [obj]
|
|
||||||
duk_dup(ctx, 2); // [obj][OnConnect]
|
|
||||||
duk_put_prop_string(ctx, -2, "OnConnectPtr"); // [obj]
|
|
||||||
duk_dup(ctx, 3); // [obj][OnDisconnect]
|
|
||||||
duk_put_prop_string(ctx, -2, "OnDisconnectPtr"); // [obj]
|
|
||||||
duk_dup(ctx, 4); // [obj][OnSendOK]
|
|
||||||
duk_put_prop_string(ctx, -2, "OnSendOKPtr"); // [obj]
|
|
||||||
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
duk_ret_t ILibAsyncSocket_Duktape_Send(duk_context *ctx)
|
|
||||||
{
|
|
||||||
ILibAsyncSocket_SocketModule module;
|
|
||||||
char *buffer = Duktape_GetBuffer(ctx, 0, NULL);
|
|
||||||
int bufferLen = duk_require_int(ctx, 1);
|
|
||||||
ILibAsyncSocket_SendStatus retVal;
|
|
||||||
|
|
||||||
duk_push_this(ctx); // [obj]
|
|
||||||
duk_get_prop_string(ctx, -1, "ModulePtr"); // [obj][ptr]
|
|
||||||
module = duk_to_pointer(ctx, -1);
|
|
||||||
|
|
||||||
retVal = ILibAsyncSocket_Send(module, buffer, bufferLen, ILibAsyncSocket_MemoryOwnership_USER);
|
|
||||||
duk_push_int(ctx, retVal);
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
duk_ret_t ILibAsyncSocket_Duktape_Disconnect(duk_context *ctx)
|
|
||||||
{
|
|
||||||
ILibAsyncSocket_SocketModule module;
|
|
||||||
|
|
||||||
duk_push_this(ctx);
|
|
||||||
duk_get_prop_string(ctx, -1, "ModulePtr");
|
|
||||||
module = duk_to_pointer(ctx, -1);
|
|
||||||
ILibAsyncSocket_Disconnect(module);
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
duk_ret_t ILibAsyncSocket_Duktape_ConnectTo(duk_context *ctx)
|
|
||||||
{
|
|
||||||
char *localAddress;
|
|
||||||
int localPort;
|
|
||||||
char *remoteAddress;
|
|
||||||
int remotePort;
|
|
||||||
struct sockaddr_in6 localAddr;
|
|
||||||
struct sockaddr_in6 remoteAddr;
|
|
||||||
ILibAsyncSocket_SocketModule module;
|
|
||||||
|
|
||||||
if (!duk_has_prop_string(ctx, 0, "IPAddress")) { duk_push_string(ctx, "Missing [Local] IPAddress Property"); duk_throw(ctx); return DUK_RET_ERROR; }
|
|
||||||
if (!duk_has_prop_string(ctx, 0, "Port")) { duk_push_string(ctx, "Missing [Local] Port Property"); duk_throw(ctx); return DUK_RET_ERROR; }
|
|
||||||
if (!duk_has_prop_string(ctx, 1, "IPAddress")) { duk_push_string(ctx, "Missing [Remote] IPAddress Property"); duk_throw(ctx); return DUK_RET_ERROR; }
|
|
||||||
if (!duk_has_prop_string(ctx, 1, "Port")) { duk_push_string(ctx, "Missing [Remote] Port Property"); duk_throw(ctx); return DUK_RET_ERROR; }
|
|
||||||
|
|
||||||
duk_get_prop_string(ctx, 0, "IPAddress");
|
|
||||||
localAddress = (char*)duk_to_string(ctx, -1);
|
|
||||||
duk_get_prop_string(ctx, 0, "Port");
|
|
||||||
localPort = duk_to_int(ctx, -1);
|
|
||||||
duk_get_prop_string(ctx, 1, "IPAddress");
|
|
||||||
remoteAddress = (char*)duk_to_string(ctx, -1);
|
|
||||||
duk_get_prop_string(ctx, 1, "Port");
|
|
||||||
remotePort = duk_to_int(ctx, -1);
|
|
||||||
|
|
||||||
memset(&localAddr, 0, sizeof(struct sockaddr_in6));
|
|
||||||
memset(&remoteAddr, 0, sizeof(struct sockaddr_in6));
|
|
||||||
|
|
||||||
((struct sockaddr_in*)&localAddr)->sin_family = AF_INET;
|
|
||||||
((struct sockaddr_in*)&localAddr)->sin_port = htons(localPort);
|
|
||||||
ILibInet_pton(AF_INET, localAddress, &(((struct sockaddr_in*)&localAddr)->sin_addr));
|
|
||||||
|
|
||||||
((struct sockaddr_in*)&remoteAddr)->sin_family = AF_INET;
|
|
||||||
((struct sockaddr_in*)&remoteAddr)->sin_port = htons(remotePort);
|
|
||||||
ILibInet_pton(AF_INET, remoteAddress, &(((struct sockaddr_in*)&remoteAddr)->sin_addr));
|
|
||||||
|
|
||||||
duk_push_this(ctx);
|
|
||||||
duk_get_prop_string(ctx, -1, "ModulePtr");
|
|
||||||
module = duk_to_pointer(ctx, -1);
|
|
||||||
|
|
||||||
ILibAsyncSocket_ConnectTo(module, (struct sockaddr*)&localAddr, (struct sockaddr*)&remoteAddr, NULL, NULL);
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
duk_idx_t ILibAsyncSocket_Duktape_PUSH_AsyncSocketModule(duk_context *ctx, ILibAsyncSocket_SocketModule module)
|
|
||||||
{
|
|
||||||
char* key = Duktape_GetStashKey(module);
|
|
||||||
|
|
||||||
duk_push_heap_stash(ctx); // [stash]
|
|
||||||
if (duk_has_prop_string(ctx, -1, key))
|
|
||||||
{
|
|
||||||
duk_get_prop_string(ctx, -1, key); // [stash][obj]
|
|
||||||
duk_swap_top(ctx, -2); // [obj][stash]
|
|
||||||
duk_pop(ctx); // [obj]
|
|
||||||
return duk_get_top_index(ctx);
|
|
||||||
}
|
|
||||||
|
|
||||||
duk_push_object(ctx); // [stash][obj]
|
|
||||||
duk_push_pointer(ctx, module); // [stash][obj][pointer]
|
|
||||||
duk_put_prop_string(ctx, -2, "ModulePtr"); // [stash][obj]
|
|
||||||
|
|
||||||
|
|
||||||
duk_dup(ctx, -1); // [stash][obj][obj]
|
|
||||||
duk_put_prop_string(ctx, -3, key); // [stash][obj]
|
|
||||||
duk_swap_top(ctx, -2); // [obj][stash]
|
|
||||||
duk_pop(ctx); // [obj]
|
|
||||||
|
|
||||||
duk_push_c_function(ctx, ILibAsyncSocket_Duktape_Send, 2); // [obj][func]
|
|
||||||
duk_put_prop_string(ctx, -2, "Send"); // [obj]
|
|
||||||
|
|
||||||
duk_push_c_function(ctx, ILibAsyncSocket_Duktape_Disconnect, 0); // [obj][func]
|
|
||||||
duk_put_prop_string(ctx, -2, "Disconnect"); // [obj]
|
|
||||||
|
|
||||||
duk_push_c_function(ctx, ILibAsyncSocket_Duktape_ConnectTo, 2);
|
|
||||||
duk_put_prop_string(ctx, -2, "ConnectTo");
|
|
||||||
|
|
||||||
return duk_get_top_index(ctx);
|
|
||||||
}
|
|
||||||
|
|
||||||
void ILibAsyncSocket_DukTape_Init(duk_context * ctx, void * chain)
|
|
||||||
{
|
|
||||||
duk_push_global_object(ctx); // [Global]
|
|
||||||
duk_push_c_function(ctx, ILibAsyncSocket_Duktape_Create, 5); // [Global][func]
|
|
||||||
duk_push_pointer(ctx, chain); // [Global][func][chain]
|
|
||||||
duk_put_prop_string(ctx, -2, "chain"); // [Global][func]
|
|
||||||
duk_put_prop_string(ctx, -2, "ILibAsyncSocket_Create"); // [Global]
|
|
||||||
duk_pop(ctx); //
|
|
||||||
}
|
|
||||||
@@ -1,25 +0,0 @@
|
|||||||
/*
|
|
||||||
Copyright 2006 - 2017 Intel Corporation
|
|
||||||
|
|
||||||
Licensed under the Apache License, Version 2.0 (the "License");
|
|
||||||
you may not use this file except in compliance with the License.
|
|
||||||
You may obtain a copy of the License at
|
|
||||||
|
|
||||||
http://www.apache.org/licenses/LICENSE-2.0
|
|
||||||
|
|
||||||
Unless required by applicable law or agreed to in writing, software
|
|
||||||
distributed under the License is distributed on an "AS IS" BASIS,
|
|
||||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
||||||
See the License for the specific language governing permissions and
|
|
||||||
limitations under the License.
|
|
||||||
*/
|
|
||||||
|
|
||||||
#ifndef ___ILIBASYNCSOCKETDUKTAPE___
|
|
||||||
#define ___ILIBASYNCSOCKETDUKTAPE___
|
|
||||||
|
|
||||||
#include "duktape.h"
|
|
||||||
|
|
||||||
void ILibAsyncSocket_DukTape_Init(duk_context *ctx, void * chain);
|
|
||||||
|
|
||||||
|
|
||||||
#endif
|
|
||||||
@@ -1,124 +0,0 @@
|
|||||||
/*
|
|
||||||
Copyright 2006 - 2017 Intel Corporation
|
|
||||||
|
|
||||||
Licensed under the Apache License, Version 2.0 (the "License");
|
|
||||||
you may not use this file except in compliance with the License.
|
|
||||||
You may obtain a copy of the License at
|
|
||||||
|
|
||||||
http://www.apache.org/licenses/LICENSE-2.0
|
|
||||||
|
|
||||||
Unless required by applicable law or agreed to in writing, software
|
|
||||||
distributed under the License is distributed on an "AS IS" BASIS,
|
|
||||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
||||||
See the License for the specific language governing permissions and
|
|
||||||
limitations under the License.
|
|
||||||
*/
|
|
||||||
|
|
||||||
#ifdef WIN32
|
|
||||||
#include <WinSock2.h>
|
|
||||||
#include <WS2tcpip.h>
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#include "ILibWebServer_Duktape.h"
|
|
||||||
#include "microstack/ILibParsers.h"
|
|
||||||
#include "ILibParsers_Duktape.h"
|
|
||||||
#include "ILibDuktape_Helpers.h"
|
|
||||||
#include "microstack/ILibRemoteLogging.h"
|
|
||||||
#include "microstack/ILibCrypto.h"
|
|
||||||
|
|
||||||
#if defined(WIN32) && !defined(_WIN32_WCE) && !defined(_MINCORE)
|
|
||||||
#define _CRTDBG_MAP_ALLOC
|
|
||||||
#include <crtdbg.h>
|
|
||||||
#endif
|
|
||||||
|
|
||||||
|
|
||||||
void ILibParsers_Duktape_OnStart(void *chain, void *user)
|
|
||||||
{
|
|
||||||
duk_context *ctx = (duk_context*)((void**)user)[0];
|
|
||||||
void *OnStart = ((void**)user)[1];
|
|
||||||
|
|
||||||
duk_push_heapptr(ctx, OnStart);
|
|
||||||
if (duk_pcall(ctx, 0) != 0)
|
|
||||||
{
|
|
||||||
ILibDuktape_Process_UncaughtException(ctx);
|
|
||||||
}
|
|
||||||
duk_pop(ctx);
|
|
||||||
|
|
||||||
free(user);
|
|
||||||
}
|
|
||||||
duk_ret_t ILibParsers_Duktape_ChainOnStart(duk_context *ctx)
|
|
||||||
{
|
|
||||||
void *chain;
|
|
||||||
void *OnStart = (duk_is_undefined(ctx, 0) || duk_is_null(ctx, 0)) ? NULL : duk_require_heapptr(ctx, 0);
|
|
||||||
void **state;
|
|
||||||
|
|
||||||
duk_push_current_function(ctx);
|
|
||||||
duk_get_prop_string(ctx, -1, "chain");
|
|
||||||
chain = duk_to_pointer(ctx, -1);
|
|
||||||
|
|
||||||
if (OnStart != NULL)
|
|
||||||
{
|
|
||||||
state = (void**)ILibMemory_Allocate(2 * sizeof(void*), 0, NULL, NULL);
|
|
||||||
duk_push_heap_stash(ctx);
|
|
||||||
duk_dup(ctx, 0);
|
|
||||||
duk_put_prop_string(ctx, -2, Duktape_GetStashKey(OnStart));
|
|
||||||
|
|
||||||
state[0] = ctx;
|
|
||||||
state[1] = OnStart;
|
|
||||||
|
|
||||||
ILibChain_OnStartEvent_AddHandler(chain, ILibParsers_Duktape_OnStart, state);
|
|
||||||
ILibStartChain(chain);
|
|
||||||
}
|
|
||||||
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
#ifdef _REMOTELOGGING
|
|
||||||
duk_ret_t ILibParsers_Duktape_StartLogger(duk_context *ctx)
|
|
||||||
{
|
|
||||||
int nargs = duk_get_top(ctx);
|
|
||||||
void *chain;
|
|
||||||
int port = duk_require_int(ctx, 0);
|
|
||||||
int actualPort;
|
|
||||||
char *path = nargs > 1 ? (char*)duk_require_string(ctx, 1) : NULL;
|
|
||||||
|
|
||||||
duk_push_current_function(ctx);
|
|
||||||
duk_get_prop_string(ctx, -1, "chain");
|
|
||||||
chain = duk_to_pointer(ctx, -1);
|
|
||||||
|
|
||||||
actualPort = ILibStartDefaultLoggerEx(chain, port, path);
|
|
||||||
|
|
||||||
duk_push_int(ctx, actualPort);
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
|
|
||||||
void ILibParsers_DukTape_Init(duk_context * ctx, void * chain)
|
|
||||||
{
|
|
||||||
duk_push_heap_stash(ctx); // [stash]
|
|
||||||
duk_push_pointer(ctx, chain); // [stash][chainPtr]
|
|
||||||
duk_put_prop_string(ctx, -2, "gChainPtr"); // [stash]
|
|
||||||
duk_pop(ctx); // ...
|
|
||||||
|
|
||||||
|
|
||||||
duk_push_global_object(ctx); // [g]
|
|
||||||
|
|
||||||
duk_push_c_function(ctx, ILibParsers_Duktape_ChainOnStart, 1); // [g][func]
|
|
||||||
duk_push_pointer(ctx, chain); // [g][func][chain]
|
|
||||||
duk_put_prop_string(ctx, -2, "chain"); // [g][func]
|
|
||||||
duk_put_prop_string(ctx, -2, "ILibParsers_Start"); // [g]
|
|
||||||
|
|
||||||
|
|
||||||
#ifdef _REMOTELOGGING
|
|
||||||
duk_push_c_function(ctx, ILibParsers_Duktape_StartLogger, DUK_VARARGS); // [g][func]
|
|
||||||
duk_push_pointer(ctx, chain); // [g][func][chain]
|
|
||||||
duk_put_prop_string(ctx, -2, "chain"); // [g][func]
|
|
||||||
duk_put_prop_string(ctx, -2, "ILibParsers_StartDefaultLogger"); // [g]
|
|
||||||
#endif
|
|
||||||
|
|
||||||
Duktape_CreateEnum(ctx, "RemoteLogging_Modules", (char*[]) { "UNKNOWN", "WEBRTC_ICE", "WEBRTC_DTLS", "WEBRTC_SCTP", "MESHAGENT_GUARDPOST", "MESHAGENT_P2P", "MESHAGENT_KVM", "MICROSTACK_ASYNCSOCKET", "MICROSTACK_WEB", "MICROSTACK_PIPE", "MICROSTACK_GENERIC" }, (int[]) { 0x00, 0x02, 0x04, 0x08, 0x10, 0x20, 0x200, 0x40, 0x80, 0x400, 0x100 }, 11);
|
|
||||||
Duktape_CreateEnum(ctx, "RemoteLogging_Flags", (char*[]) { "NONE", "DISABLE", "VERBOSITY_1", "VERBOSITY_2", "VERBOSITY_3", "VERBOSITY_4", "VERBOSITY_5" }, (int[]) { 0x00, 0x01, 0x02, 0x04, 0x08, 0x10, 0x20 }, 7);
|
|
||||||
|
|
||||||
|
|
||||||
duk_pop(ctx); // Pop Global Object
|
|
||||||
}
|
|
||||||
@@ -1,24 +0,0 @@
|
|||||||
/*
|
|
||||||
Copyright 2006 - 2017 Intel Corporation
|
|
||||||
|
|
||||||
Licensed under the Apache License, Version 2.0 (the "License");
|
|
||||||
you may not use this file except in compliance with the License.
|
|
||||||
You may obtain a copy of the License at
|
|
||||||
|
|
||||||
http://www.apache.org/licenses/LICENSE-2.0
|
|
||||||
|
|
||||||
Unless required by applicable law or agreed to in writing, software
|
|
||||||
distributed under the License is distributed on an "AS IS" BASIS,
|
|
||||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
||||||
See the License for the specific language governing permissions and
|
|
||||||
limitations under the License.
|
|
||||||
*/
|
|
||||||
|
|
||||||
#ifndef ___ILIBPARSERSDUKTAPE___
|
|
||||||
#define ___ILIBPARSERSDUKTAPE___
|
|
||||||
|
|
||||||
#include "duktape.h"
|
|
||||||
|
|
||||||
void ILibParsers_DukTape_Init(duk_context *ctx, void * chain);
|
|
||||||
|
|
||||||
#endif
|
|
||||||
@@ -1,576 +0,0 @@
|
|||||||
/*
|
|
||||||
Copyright 2006 - 2017 Intel Corporation
|
|
||||||
|
|
||||||
Licensed under the Apache License, Version 2.0 (the "License");
|
|
||||||
you may not use this file except in compliance with the License.
|
|
||||||
You may obtain a copy of the License at
|
|
||||||
|
|
||||||
http://www.apache.org/licenses/LICENSE-2.0
|
|
||||||
|
|
||||||
Unless required by applicable law or agreed to in writing, software
|
|
||||||
distributed under the License is distributed on an "AS IS" BASIS,
|
|
||||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
||||||
See the License for the specific language governing permissions and
|
|
||||||
limitations under the License.
|
|
||||||
*/
|
|
||||||
|
|
||||||
#include "ILibWebClient_Duktape.h"
|
|
||||||
#include "microstack/ILibParsers.h"
|
|
||||||
#include "microstack/ILibWebClient.h"
|
|
||||||
#include "ILibDuktape_Helpers.h"
|
|
||||||
|
|
||||||
#if defined(WIN32) && !defined(_WIN32_WCE) && !defined(_MINCORE)
|
|
||||||
#define _CRTDBG_MAP_ALLOC
|
|
||||||
#include <crtdbg.h>
|
|
||||||
#endif
|
|
||||||
|
|
||||||
extern duk_idx_t ILibWebServer_DukTape_Push_PacketHeader(duk_context *ctx, ILibHTTPPacket *packet);
|
|
||||||
|
|
||||||
typedef struct ILibWebClient_DukTape_WebSocketCallbacks
|
|
||||||
{
|
|
||||||
void *OnMessage;
|
|
||||||
void *OnClose;
|
|
||||||
void *OnSendOK;
|
|
||||||
}ILibWebClient_DukTape_WebSocketCallbacks;
|
|
||||||
|
|
||||||
duk_idx_t ILibWebClient_DukTape_Push_WebRequestManager(duk_context *ctx, void* wcm);
|
|
||||||
duk_ret_t ILibWebClient_DukTape_RequestToken_Cancel(duk_context *ctx)
|
|
||||||
{
|
|
||||||
void *token;
|
|
||||||
duk_push_this(ctx);
|
|
||||||
duk_get_prop_string(ctx, -1, "TokenPtr");
|
|
||||||
token = duk_to_pointer(ctx, -1);
|
|
||||||
ILibWebClient_CancelRequest(token); //ToDo: Check to see if we should delete from stash here, or from somewhere else
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
duk_idx_t ILibWebClient_DukTape_Push_RequestToken(duk_context *ctx, ILibWebClient_RequestToken token)
|
|
||||||
{
|
|
||||||
char *key = Duktape_GetStashKey(token);
|
|
||||||
duk_push_heap_stash(ctx);
|
|
||||||
if (duk_has_prop_string(ctx, -1, key) != 0)
|
|
||||||
{
|
|
||||||
duk_get_prop_string(ctx, -1, key); // [stash][obj]
|
|
||||||
duk_swap_top(ctx, -2); // [obj][stash]
|
|
||||||
duk_pop(ctx); // [obj]
|
|
||||||
return(duk_get_top_index(ctx));
|
|
||||||
}
|
|
||||||
duk_push_object(ctx); // [stash][obj]
|
|
||||||
duk_push_pointer(ctx, token); // [stash][obj][ptr]
|
|
||||||
duk_put_prop_string(ctx, -2, "TokenPtr"); // [stash][obj]
|
|
||||||
duk_push_c_function(ctx, ILibWebClient_DukTape_RequestToken_Cancel, 0); // [stash][obj][func]
|
|
||||||
duk_put_prop_string(ctx, -2, "Cancel"); // [stash][obj]
|
|
||||||
|
|
||||||
duk_dup(ctx, -1); // [stash][obj1][obj2]
|
|
||||||
duk_put_prop_string(ctx, -3, key); // [stash][obj2]
|
|
||||||
duk_swap_top(ctx, -2); // [obj2][stash]
|
|
||||||
duk_pop(ctx); // [obj2]
|
|
||||||
return(duk_get_top_index(ctx));
|
|
||||||
}
|
|
||||||
|
|
||||||
duk_ret_t ILibWebClient_DukTape_StateObject_Resume(duk_context *ctx)
|
|
||||||
{
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
duk_ret_t ILibWebClient_DukTape_StateObject_Digest_NeedAuthenticate(duk_context *ctx)
|
|
||||||
{
|
|
||||||
ILibWebClient_StateObject wcdo;
|
|
||||||
duk_push_this(ctx);
|
|
||||||
duk_get_prop_string(ctx, -1, "StatePtr");
|
|
||||||
wcdo = (ILibWebClient_StateObject)duk_to_pointer(ctx, -1);
|
|
||||||
duk_push_int(ctx, ILibWebClient_Digest_NeedAuthenticate(wcdo));
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
duk_ret_t ILibWebClient_DukTape_StateObject_Digest_GetRealm(duk_context *ctx)
|
|
||||||
{
|
|
||||||
char *realm;
|
|
||||||
ILibWebClient_StateObject wcdo;
|
|
||||||
duk_push_this(ctx);
|
|
||||||
duk_get_prop_string(ctx, -1, "StatePtr");
|
|
||||||
wcdo = (ILibWebClient_StateObject)duk_to_pointer(ctx, -1);
|
|
||||||
realm = ILibWebClient_Digest_GetRealm(wcdo);
|
|
||||||
duk_push_string(ctx, realm != NULL ? realm : "");
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
duk_ret_t ILibWebClient_DukTape_StateObject_Digest_AddAuthenticationHeader(duk_context *ctx)
|
|
||||||
{
|
|
||||||
ILibWebClient_StateObject wcdo;
|
|
||||||
char *username;
|
|
||||||
char *password;
|
|
||||||
ILibHTTPPacket *packet;
|
|
||||||
|
|
||||||
duk_push_this(ctx);
|
|
||||||
duk_get_prop_string(ctx, -1, "StatePtr");
|
|
||||||
wcdo = (ILibWebClient_StateObject)duk_to_pointer(ctx, -1);
|
|
||||||
|
|
||||||
duk_get_prop_string(ctx, 0, "PacketPtr");
|
|
||||||
packet = (ILibHTTPPacket*)duk_to_pointer(ctx, -1);
|
|
||||||
username = (char*)duk_require_string(ctx, 1);
|
|
||||||
password = (char*)duk_require_string(ctx, 2);
|
|
||||||
|
|
||||||
ILibWebClient_GenerateAuthenticationHeader(wcdo, packet, username, password);
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
duk_idx_t ILibWebClient_DukTape_WebClient_WebSocket_Send(duk_context *ctx)
|
|
||||||
{
|
|
||||||
int bufferType = duk_require_int(ctx, 0);
|
|
||||||
char *buffer = Duktape_GetBuffer(ctx, 1, NULL);
|
|
||||||
int bufferLen = duk_require_int(ctx, 2);
|
|
||||||
int fragmentFlags = duk_require_int(ctx, 3);
|
|
||||||
ILibWebClient_StateObject wcdo;
|
|
||||||
ILibAsyncSocket_SendStatus retVal;
|
|
||||||
|
|
||||||
duk_push_this(ctx);
|
|
||||||
duk_get_prop_string(ctx, -1, "StatePtr");
|
|
||||||
wcdo = (ILibWebClient_StateObject)duk_to_pointer(ctx, -1);
|
|
||||||
|
|
||||||
retVal = ILibWebClient_WebSocket_Send(wcdo, bufferType, buffer, bufferLen, ILibAsyncSocket_MemoryOwnership_USER, fragmentFlags);
|
|
||||||
duk_push_int(ctx, retVal);
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
duk_idx_t ILibWebClient_DukTape_Push_StateObject(duk_context *ctx, ILibWebClient_StateObject WebStateObject)
|
|
||||||
{
|
|
||||||
char* key = Duktape_GetStashKey(WebStateObject);
|
|
||||||
|
|
||||||
duk_push_heap_stash(ctx); // [stash]
|
|
||||||
if (duk_has_prop_string(ctx, -1, key) != 0)
|
|
||||||
{
|
|
||||||
duk_get_prop_string(ctx, -1, key); // [stash][obj]
|
|
||||||
duk_swap_top(ctx, -2); // [obj][stash]
|
|
||||||
duk_pop(ctx); // [obj]
|
|
||||||
return(duk_get_top_index(ctx));
|
|
||||||
}
|
|
||||||
|
|
||||||
duk_push_object(ctx); // [stash][obj]
|
|
||||||
duk_push_pointer(ctx, WebStateObject); // [stash][obj][ptr]
|
|
||||||
duk_put_prop_string(ctx, -2, "StatePtr"); // [stash][obj]
|
|
||||||
duk_push_external_buffer(ctx); // [stash][obj][buffer]
|
|
||||||
duk_put_prop_string(ctx, -2, "BufferPtr"); // [stash][obj]
|
|
||||||
duk_push_c_function(ctx, ILibWebClient_DukTape_StateObject_Resume, 0); // [stash][obj][func]
|
|
||||||
duk_put_prop_string(ctx, -2, "Resume"); // [stash][obj]
|
|
||||||
duk_push_c_function(ctx, ILibWebClient_DukTape_StateObject_Digest_NeedAuthenticate, 0); // [stash][obj][func]
|
|
||||||
duk_put_prop_string(ctx, -2, "Digest_NeedAuthenticate"); // [stash][obj]
|
|
||||||
duk_push_c_function(ctx, ILibWebClient_DukTape_StateObject_Digest_GetRealm, 0); // [stash][obj][func]
|
|
||||||
duk_put_prop_string(ctx, -2, "Digest_GetRealm"); // [stash][obj]
|
|
||||||
duk_push_c_function(ctx, ILibWebClient_DukTape_StateObject_Digest_AddAuthenticationHeader, 3); // [stash][obj][func]
|
|
||||||
duk_put_prop_string(ctx, -2, "Digest_AddAuthenticationHeader"); // [stash][obj]
|
|
||||||
duk_push_c_function(ctx, ILibWebClient_DukTape_WebClient_WebSocket_Send, 4); // [stash][obj][func]
|
|
||||||
duk_put_prop_string(ctx, -2, "WebSocket_Send"); // [stash][obj]
|
|
||||||
|
|
||||||
|
|
||||||
duk_dup(ctx, -1); // [stash][obj][obj]
|
|
||||||
duk_put_prop_string(ctx, -3, key); // [stash][obj]
|
|
||||||
duk_swap_top(ctx, -2); // [obj][stash]
|
|
||||||
duk_pop(ctx); // [obj]
|
|
||||||
return(duk_get_top_index(ctx));
|
|
||||||
}
|
|
||||||
|
|
||||||
void ILibWebClient_DukTape_OnResponse(ILibWebClient_StateObject WebStateObject, int InterruptFlag, struct packetheader *header, char *bodyBuffer, int *beginPointer, int endPointer, ILibWebClient_ReceiveStatus recvStatus, void *user1, void *user2, int *PAUSE)
|
|
||||||
{
|
|
||||||
duk_context *ctx = (duk_context*)user1;
|
|
||||||
void *OnResp = user2;
|
|
||||||
int retVal;
|
|
||||||
|
|
||||||
duk_push_heapptr(ctx, OnResp); // [Func]
|
|
||||||
ILibWebClient_DukTape_Push_StateObject(ctx, WebStateObject); // [Func][state]
|
|
||||||
duk_push_int(ctx, InterruptFlag); // [Func][state][Interrupt]
|
|
||||||
ILibWebServer_DukTape_Push_PacketHeader(ctx, header); // [Func][state][Interrupt][header]
|
|
||||||
duk_get_prop_string(ctx, -3, "BufferPtr"); // [Func][state][Interrupt][header][buffer]
|
|
||||||
duk_config_buffer(ctx, -1, bodyBuffer + *beginPointer, endPointer - *beginPointer);
|
|
||||||
duk_push_int(ctx, endPointer - *beginPointer); // [Func][state][Interrupt][header][buffer][len]
|
|
||||||
duk_push_int(ctx, recvStatus); // [Func][state][Interrupt][header][buffer][len][status]
|
|
||||||
if (duk_pcall(ctx, 6) == 0)
|
|
||||||
{
|
|
||||||
if (duk_get_type(ctx, -1) == DUK_TYPE_UNDEFINED)
|
|
||||||
{
|
|
||||||
retVal = duk_to_int(ctx, -1);
|
|
||||||
if (retVal < 0)
|
|
||||||
{
|
|
||||||
*PAUSE = 1;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
*beginPointer = retVal;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
*beginPointer = endPointer;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
ILibDuktape_Process_UncaughtException(ctx);
|
|
||||||
}
|
|
||||||
duk_pop(ctx);
|
|
||||||
|
|
||||||
|
|
||||||
if (recvStatus == ILibWebClient_ReceiveStatus_Complete)
|
|
||||||
{
|
|
||||||
// Done, so we can clear our reference in the heap stash
|
|
||||||
ILibWebClient_RequestToken token = ILibWebClient_GetRequestToken_FromStateObject(WebStateObject);
|
|
||||||
|
|
||||||
duk_push_heap_stash(ctx);
|
|
||||||
duk_del_prop_string(ctx, -1, Duktape_GetStashKey(WebStateObject));
|
|
||||||
if (token != NULL)
|
|
||||||
{
|
|
||||||
duk_del_prop_string(ctx, -1, Duktape_GetStashKey(token));
|
|
||||||
}
|
|
||||||
duk_pop(ctx);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
duk_idx_t ILibWebClient_DukTape_PipelineRequest(duk_context *ctx)
|
|
||||||
{
|
|
||||||
int args = duk_get_top(ctx);
|
|
||||||
ILibHTTPPacket *packet;
|
|
||||||
char *addr;
|
|
||||||
int port;
|
|
||||||
struct sockaddr_in6* dest;
|
|
||||||
void *wcm;
|
|
||||||
ILibWebClient_OnResponse OnResp = NULL;
|
|
||||||
ILibWebClient_RequestToken token;
|
|
||||||
|
|
||||||
if (args < 3) { duk_push_string(ctx, "Too few arguments"); duk_throw(ctx); return(DUK_RET_ERROR); }
|
|
||||||
if (duk_get_prop_string(ctx, 1, "PacketPtr") == 0) { duk_push_string(ctx, "Invalid Argument[packet]"); duk_throw(ctx); return(DUK_RET_ERROR); }
|
|
||||||
packet = (ILibHTTPPacket*)duk_to_pointer(ctx, -1);
|
|
||||||
|
|
||||||
if (duk_get_prop_string(ctx, 0, "IPAddress") == 0) { duk_push_string(ctx, "Invalid Argument[RemoteEndpoint]"); duk_throw(ctx); return(DUK_RET_ERROR); }
|
|
||||||
addr = (char*)duk_to_string(ctx, -1);
|
|
||||||
if (duk_get_prop_string(ctx, 0, "Port") == 0) { duk_push_string(ctx, "Invalid Argument[RemoteEndpoint]"); duk_throw(ctx); return(DUK_RET_ERROR); }
|
|
||||||
port = duk_to_int(ctx, -1);
|
|
||||||
|
|
||||||
dest = Duktape_IPAddress4_FromString(addr, (unsigned short)port);
|
|
||||||
OnResp = duk_require_heapptr(ctx, 2);
|
|
||||||
|
|
||||||
duk_push_this(ctx);
|
|
||||||
duk_get_prop_string(ctx, -1, "ManagerPtr");
|
|
||||||
wcm = duk_to_pointer(ctx, -1);
|
|
||||||
duk_pop(ctx);
|
|
||||||
duk_dup(ctx, 2);
|
|
||||||
duk_put_prop_string(ctx, -2, "OnResponsePtr");
|
|
||||||
|
|
||||||
token = ILibWebClient_PipelineRequest(wcm, (struct sockaddr*)dest, packet, ILibWebClient_DukTape_OnResponse, ctx, OnResp);
|
|
||||||
ILibWebClient_DukTape_Push_RequestToken(ctx, token);
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
duk_idx_t ILibWebClient_DukTape_Push_WebRequestManager(duk_context *ctx, void* wcm)
|
|
||||||
{
|
|
||||||
char* key = Duktape_GetStashKey(wcm);
|
|
||||||
|
|
||||||
duk_push_heap_stash(ctx);
|
|
||||||
if (duk_has_prop_string(ctx, -1, key))
|
|
||||||
{
|
|
||||||
duk_get_prop_string(ctx, -1, key);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
duk_push_object(ctx);
|
|
||||||
duk_push_pointer(ctx, wcm);
|
|
||||||
duk_put_prop_string(ctx, -2, "ManagerPtr");
|
|
||||||
|
|
||||||
duk_push_c_function(ctx, ILibWebClient_DukTape_PipelineRequest, DUK_VARARGS);
|
|
||||||
duk_push_pointer(ctx, wcm);
|
|
||||||
duk_put_prop_string(ctx, -2, "ManagerPtr");
|
|
||||||
duk_put_prop_string(ctx, -2, "PipelineRequest");
|
|
||||||
}
|
|
||||||
|
|
||||||
duk_swap_top(ctx, -2); // Swap Stash and Object, so stash is on top
|
|
||||||
duk_pop(ctx); // Pop Stash off stack, leaving the object at top
|
|
||||||
return(duk_get_top_index(ctx));
|
|
||||||
}
|
|
||||||
|
|
||||||
duk_ret_t ILibWebClient_DukTape_Create(duk_context *ctx)
|
|
||||||
{
|
|
||||||
int poolSize = duk_require_int(ctx, 0);
|
|
||||||
void *chain;
|
|
||||||
ILibWebClient_RequestManager wcm;
|
|
||||||
|
|
||||||
duk_push_current_function(ctx);
|
|
||||||
duk_get_prop_string(ctx, -1, "chain");
|
|
||||||
chain = duk_to_pointer(ctx, -1);
|
|
||||||
duk_pop_2(ctx); // Pop Chain & Function
|
|
||||||
|
|
||||||
wcm = ILibCreateWebClient(poolSize, chain);
|
|
||||||
ILibWebClient_DukTape_Push_WebRequestManager(ctx, wcm);
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
duk_ret_t ILibWebClient_DukTape_WebSocket_Finalizer(duk_context *ctx)
|
|
||||||
{
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
duk_ret_t ILibWebClient_DukTape_WebSocket_Ptr_Setter(duk_context *ctx)
|
|
||||||
{
|
|
||||||
if (!duk_is_null(ctx, 0) && !duk_is_undefined(ctx, 0))
|
|
||||||
{
|
|
||||||
duk_push_this(ctx); // [wsock]
|
|
||||||
duk_push_current_function(ctx); // [wsock][func]
|
|
||||||
duk_get_prop_string(ctx, -1, "SetterKey"); // [wsock][func][key]
|
|
||||||
duk_swap_top(ctx, -2); // [wsock][key][func]
|
|
||||||
duk_pop(ctx); // [wsock][key]
|
|
||||||
duk_dup(ctx, 0); // [wsock][key][Ptr]
|
|
||||||
duk_put_prop(ctx, -3); // [wsock]
|
|
||||||
}
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
void ILibWebClient_DukTape_WebSocket_OnResponse(ILibWebClient_StateObject WebStateObject, int InterruptFlag, struct packetheader *header, char *bodyBuffer, int *beginPointer, int endPointer, ILibWebClient_ReceiveStatus recvStatus, void *user1, void *user2, int *PAUSE)
|
|
||||||
{
|
|
||||||
duk_context *ctx = (duk_context*)user1;
|
|
||||||
|
|
||||||
if (header == NULL || (header->StatusCode != 101 && recvStatus == ILibWebClient_ReceiveStatus_Complete))
|
|
||||||
{
|
|
||||||
duk_push_heapptr(ctx, user2); // [wsock]
|
|
||||||
if (duk_has_prop_string(ctx, -1, "OnErrorPtr"))
|
|
||||||
{
|
|
||||||
duk_get_prop_string(ctx, -1, "OnErrorPtr"); // [wsock][OnError]
|
|
||||||
duk_swap_top(ctx, -2); // [OnError][this]
|
|
||||||
|
|
||||||
if (duk_pcall_method(ctx, 0) != 0) // [retVal]
|
|
||||||
{
|
|
||||||
ILibDuktape_Process_UncaughtException(ctx);
|
|
||||||
}
|
|
||||||
duk_pop(ctx);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
duk_pop(ctx); // ...
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
else if (header->StatusCode == 101)
|
|
||||||
{
|
|
||||||
ILibWebClient_DukTape_WebSocketCallbacks *callbacks = (ILibWebClient_DukTape_WebSocketCallbacks*)ILibMemory_GetExtraMemory(ILibWebClient_GetRequestToken_FromStateObject(WebStateObject), ILibMemory_WebClient_RequestToken_CONTAINERSIZE);
|
|
||||||
|
|
||||||
if (recvStatus == ILibWebClient_ReceiveStatus_Connection_Established)
|
|
||||||
{
|
|
||||||
memset(callbacks, 0, sizeof(ILibWebClient_DukTape_WebSocketCallbacks));
|
|
||||||
duk_push_heapptr(ctx, user2); // [wsock]
|
|
||||||
if (duk_has_prop_string(ctx, -1, "OnOpenPtr"))
|
|
||||||
{
|
|
||||||
duk_get_prop_string(ctx, -1, "OnMessagePtr"); // [wsock][OnMessage]
|
|
||||||
callbacks->OnMessage = duk_to_pointer(ctx, -1);
|
|
||||||
duk_pop(ctx); // [wsock]
|
|
||||||
|
|
||||||
duk_get_prop_string(ctx, -1, "OnSendOKPtr"); // [wsock][OnSendOK]
|
|
||||||
callbacks->OnSendOK = duk_to_pointer(ctx, -1);
|
|
||||||
duk_pop(ctx); // [wsock]
|
|
||||||
|
|
||||||
duk_push_pointer(ctx, WebStateObject); // [wsock][wcdo]
|
|
||||||
duk_put_prop_string(ctx, -2, "wcdo"); // [wsock]
|
|
||||||
|
|
||||||
duk_get_prop_string(ctx, -1, "OnOpenPtr"); // [wsock][OnOpen]
|
|
||||||
duk_swap_top(ctx, -2); // [OnOpen][this]
|
|
||||||
if (duk_pcall_method(ctx, 0) != 0) // [retVal]
|
|
||||||
{
|
|
||||||
ILibDuktape_Process_UncaughtException(ctx);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
duk_pop(ctx); // ...
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
if (callbacks->OnMessage != NULL)
|
|
||||||
{
|
|
||||||
duk_push_heapptr(ctx, callbacks->OnMessage); // [func]
|
|
||||||
duk_push_heapptr(ctx, user2); // [func][this]
|
|
||||||
duk_get_prop_string(ctx, -1, "buffer"); // [func][this][buffer]
|
|
||||||
duk_config_buffer(ctx, -1, bodyBuffer + *beginPointer, endPointer - *beginPointer);
|
|
||||||
duk_push_int(ctx, recvStatus); // [func][this][buffer][fragmentFlag]
|
|
||||||
if (duk_pcall_method(ctx, 2) == 0) // [retVal]
|
|
||||||
{
|
|
||||||
if (duk_is_number(ctx, -1))
|
|
||||||
{
|
|
||||||
*beginPointer = duk_to_int(ctx, -1);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
*beginPointer = endPointer;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
ILibDuktape_Process_UncaughtException(ctx);
|
|
||||||
}
|
|
||||||
duk_pop(ctx);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void ILibWebClient_DukTape_WebSocket_OnSendOK(ILibWebClient_StateObject wcdo, void* user1, void* user2)
|
|
||||||
{
|
|
||||||
duk_context *ctx = (duk_context*)user1;
|
|
||||||
ILibWebClient_DukTape_WebSocketCallbacks *callbacks = (ILibWebClient_DukTape_WebSocketCallbacks*)ILibMemory_GetExtraMemory(ILibWebClient_GetRequestToken_FromStateObject(wcdo), ILibMemory_WebClient_RequestToken_CONTAINERSIZE);
|
|
||||||
if (callbacks->OnSendOK != NULL)
|
|
||||||
{
|
|
||||||
duk_push_heapptr(ctx, callbacks->OnSendOK); // [func]
|
|
||||||
duk_push_heapptr(ctx, user2); // [func][this]
|
|
||||||
if (duk_pcall_method(ctx, 0) != 0) // [retVal]
|
|
||||||
{
|
|
||||||
ILibDuktape_Process_UncaughtException(ctx);
|
|
||||||
}
|
|
||||||
duk_pop(ctx); // ...
|
|
||||||
}
|
|
||||||
}
|
|
||||||
duk_ret_t ILibWebClient_DukTape_W3CWebSocket_Send(duk_context *ctx)
|
|
||||||
{
|
|
||||||
char *buffer;
|
|
||||||
duk_size_t bufferLen;
|
|
||||||
ILibWebClient_WebSocket_FragmentFlags fragmentFlag = ILibWebClient_WebSocket_FragmentFlag_Complete;
|
|
||||||
ILibWebClient_WebSocket_DataTypes bufferType = duk_is_string(ctx, 0) ? ILibWebClient_WebSocket_DataType_TEXT : ILibWebClient_WebSocket_DataType_BINARY;
|
|
||||||
ILibAsyncSocket_SendStatus status;
|
|
||||||
|
|
||||||
int nargs = duk_get_top(ctx);
|
|
||||||
if (nargs < 1) { duk_push_string(ctx, "Too Few Arguments"); duk_throw(ctx); return(DUK_RET_ERROR); }
|
|
||||||
if (nargs > 1) { fragmentFlag = (ILibWebClient_WebSocket_FragmentFlags)duk_require_int(ctx, 1); }
|
|
||||||
|
|
||||||
if (duk_is_string(ctx, 0))
|
|
||||||
{
|
|
||||||
buffer = (char*)duk_get_lstring(ctx, 0, &bufferLen);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
buffer = Duktape_GetBuffer(ctx, 0, &bufferLen);
|
|
||||||
}
|
|
||||||
|
|
||||||
duk_push_this(ctx); // [wsock]
|
|
||||||
duk_get_prop_string(ctx, -1, "wcdo"); // [wsock][wcdo]
|
|
||||||
|
|
||||||
status = ILibWebClient_WebSocket_Send(duk_to_pointer(ctx, -1), bufferType, buffer, (int)bufferLen, ILibAsyncSocket_MemoryOwnership_USER, fragmentFlag);
|
|
||||||
duk_push_int(ctx, status);
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
duk_ret_t ILibWebClient_DukTape_WebSocketContructor(duk_context *ctx)
|
|
||||||
{
|
|
||||||
if (!duk_is_string(ctx, 0)) { return(ILibDuktape_Error(ctx, "WebSocketConstructor(): Invalid Uri")); }
|
|
||||||
duk_size_t uriLen;
|
|
||||||
char *uri = (char*)duk_get_lstring(ctx, 0, &uriLen);
|
|
||||||
char *host;
|
|
||||||
char *path;
|
|
||||||
unsigned short port;
|
|
||||||
ILibHTTPPacket *packet;
|
|
||||||
int len;
|
|
||||||
int reassemblySize = 4096;
|
|
||||||
int poolSize = 5;
|
|
||||||
void *chain;
|
|
||||||
ILibWebClient_RequestManager *wcm;
|
|
||||||
ILibWebClient_RequestToken token;
|
|
||||||
struct sockaddr_in6* dest;
|
|
||||||
|
|
||||||
if (!duk_is_constructor_call(ctx))
|
|
||||||
{
|
|
||||||
return DUK_RET_TYPE_ERROR;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (duk_get_top(ctx) > 1)
|
|
||||||
{
|
|
||||||
if (duk_has_prop_string(ctx, 1, "MaxBufferSize"))
|
|
||||||
{
|
|
||||||
duk_get_prop_string(ctx, 1, "MaxBufferSize");
|
|
||||||
reassemblySize = duk_to_int(ctx, -1);
|
|
||||||
duk_pop(ctx);
|
|
||||||
}
|
|
||||||
if (duk_has_prop_string(ctx, 1, "PoolSize"))
|
|
||||||
{
|
|
||||||
duk_get_prop_string(ctx, 1, "PoolSize");
|
|
||||||
poolSize = duk_to_int(ctx, -1);
|
|
||||||
duk_pop(ctx);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
duk_push_current_function(ctx); // [func]
|
|
||||||
duk_get_prop_string(ctx, -1, "chain"); // [func][chain]
|
|
||||||
chain = duk_to_pointer(ctx, -1);
|
|
||||||
duk_push_this(ctx); // [func][chain][wsock]
|
|
||||||
duk_swap_top(ctx, -2); // [func][wsock][chain]
|
|
||||||
duk_put_prop_string(ctx, -2, "chain"); // [func][wsock]
|
|
||||||
|
|
||||||
ILibParseUri(uri, &host, &port, &path, NULL);
|
|
||||||
packet = ILibCreateEmptyPacket();
|
|
||||||
ILibSetVersion(packet, "1.1", 3);
|
|
||||||
ILibSetDirective(packet, "GET", 3, path, (int)strnlen_s(path, (int)uriLen));
|
|
||||||
len = sprintf_s(ILibScratchPad, sizeof(ILibScratchPad), "%s:%u", host, port);
|
|
||||||
ILibAddHeaderLine(packet, "Host", 4, ILibScratchPad, len);
|
|
||||||
ILibWebClient_AddWebSocketRequestHeaders(packet, reassemblySize, ILibWebClient_DukTape_WebSocket_OnSendOK);
|
|
||||||
|
|
||||||
duk_push_heap_stash(ctx); // [heapstash]
|
|
||||||
if (duk_has_prop_string(ctx, -1, "WSockClient"))
|
|
||||||
{
|
|
||||||
duk_get_prop_string(ctx, -1, "WSockClient"); // [heapstash][wcm]
|
|
||||||
wcm = (ILibWebClient_RequestManager)duk_to_pointer(ctx, -1);
|
|
||||||
duk_pop_2(ctx); // ...
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
wcm = ILibCreateWebClient(poolSize, chain);
|
|
||||||
duk_push_pointer(ctx, wcm); // [heapstash][wcm]
|
|
||||||
duk_put_prop_string(ctx, -2, "WSockClient"); // [heapstash]
|
|
||||||
}
|
|
||||||
duk_pop(ctx); // [func][wsock]
|
|
||||||
duk_push_c_function(ctx, ILibWebClient_DukTape_WebSocket_Finalizer, 1); // [func][wsock][fin]
|
|
||||||
duk_set_finalizer(ctx, -2); // [func][wsock]
|
|
||||||
|
|
||||||
duk_push_external_buffer(ctx); // [func][wsock][buffer]
|
|
||||||
duk_put_prop_string(ctx, -2, "buffer"); // [func][wsock]
|
|
||||||
|
|
||||||
duk_push_string(ctx, "onopen"); // [func][wsock][key]
|
|
||||||
duk_push_c_function(ctx, ILibWebClient_DukTape_WebSocket_Ptr_Setter, 1); // [func][wsock][key][func]
|
|
||||||
duk_push_string(ctx, "OnOpenPtr"); // [func][wsock][key][func][str]
|
|
||||||
duk_put_prop_string(ctx, -2, "SetterKey"); // [func][wsock][key][func]
|
|
||||||
duk_def_prop(ctx, -3, DUK_DEFPROP_HAVE_SETTER); // [func][wsock]
|
|
||||||
|
|
||||||
duk_push_string(ctx, "onmessage"); // [func][wsock][key]
|
|
||||||
duk_push_c_function(ctx, ILibWebClient_DukTape_WebSocket_Ptr_Setter, 1); // [func][wsock][key][func]
|
|
||||||
duk_push_string(ctx, "OnMessagePtr"); // [func][wsock][key][func][str]
|
|
||||||
duk_put_prop_string(ctx, -2, "SetterKey"); // [func][wsock][key][func]
|
|
||||||
duk_def_prop(ctx, -3, DUK_DEFPROP_HAVE_SETTER); // [func][wsock]
|
|
||||||
|
|
||||||
duk_push_string(ctx, "onerror"); // [func][wsock][key]
|
|
||||||
duk_push_c_function(ctx, ILibWebClient_DukTape_WebSocket_Ptr_Setter, 1); // [func][wsock][key][func]
|
|
||||||
duk_push_string(ctx, "OnErrorPtr"); // [func][wsock][key][func][str]
|
|
||||||
duk_put_prop_string(ctx, -2, "SetterKey"); // [func][wsock][key][func]
|
|
||||||
duk_def_prop(ctx, -3, DUK_DEFPROP_HAVE_SETTER); // [func][wsock]
|
|
||||||
|
|
||||||
duk_push_string(ctx, "onclose"); // [func][wsock][key]
|
|
||||||
duk_push_c_function(ctx, ILibWebClient_DukTape_WebSocket_Ptr_Setter, 1); // [func][wsock][key][func]
|
|
||||||
duk_push_string(ctx, "OnClosePtr"); // [func][wsock][key][func][str]
|
|
||||||
duk_put_prop_string(ctx, -2, "SetterKey"); // [func][wsock][key][func]
|
|
||||||
duk_def_prop(ctx, -3, DUK_DEFPROP_HAVE_SETTER); // [func][wsock]
|
|
||||||
|
|
||||||
duk_push_string(ctx, "onsendok"); // [func][wsock][key]
|
|
||||||
duk_push_c_function(ctx, ILibWebClient_DukTape_WebSocket_Ptr_Setter, 1); // [func][wsock][key][func]
|
|
||||||
duk_push_string(ctx, "OnSendOKPtr"); // [func][wsock][key][func][str]
|
|
||||||
duk_put_prop_string(ctx, -2, "SetterKey"); // [func][wsock][key][func]
|
|
||||||
duk_def_prop(ctx, -3, DUK_DEFPROP_HAVE_SETTER); // [func][wsock]
|
|
||||||
|
|
||||||
duk_push_c_function(ctx, ILibWebClient_DukTape_W3CWebSocket_Send, DUK_VARARGS); // [func][wsock][func]
|
|
||||||
duk_put_prop_string(ctx, -2, "Send"); // [func][wsock]
|
|
||||||
|
|
||||||
dest = Duktape_IPAddress4_FromString(host, port);
|
|
||||||
|
|
||||||
token = ILibWebClient_PipelineRequest(wcm, (struct sockaddr*)dest, packet, ILibWebClient_DukTape_WebSocket_OnResponse, ctx, duk_get_heapptr(ctx, -1));
|
|
||||||
duk_push_pointer(ctx, token); // [func][wsock][token]
|
|
||||||
duk_put_prop_string(ctx, -2, "RequestTokenPtr");// [func][wsock]
|
|
||||||
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
void ILibWebClient_DukTape_Init(duk_context * ctx, void * chain)
|
|
||||||
{
|
|
||||||
duk_push_global_object(ctx);
|
|
||||||
|
|
||||||
duk_push_c_function(ctx, ILibWebClient_DukTape_Create, 1); // [global][func]
|
|
||||||
duk_push_pointer(ctx, chain); // [global][func][chain]
|
|
||||||
duk_put_prop_string(ctx, -2, "chain"); // [global][func]
|
|
||||||
duk_put_prop_string(ctx, -2, "ILibWebClient_Create"); // [global]
|
|
||||||
|
|
||||||
duk_push_c_function(ctx, ILibWebClient_DukTape_WebSocketContructor, DUK_VARARGS); // [global][func]
|
|
||||||
duk_push_pointer(ctx, chain); // [global][func][chain]
|
|
||||||
duk_put_prop_string(ctx, -2, "chain"); // [global][func]
|
|
||||||
duk_put_prop_string(ctx, -2, "WebSocket"); // [global]
|
|
||||||
|
|
||||||
Duktape_CreateEnum(ctx, "WebSocket_Status", (char*[]) { "COMPLETE_FRAGMENT", "END", "PARTIAL_FRAGMENT", "LAST_PARTIAL_FRAGMENT" }, (int[]) { 0, 1, 10, 11 }, 4);
|
|
||||||
|
|
||||||
duk_pop(ctx);
|
|
||||||
}
|
|
||||||
@@ -1,25 +0,0 @@
|
|||||||
/*
|
|
||||||
Copyright 2006 - 2017 Intel Corporation
|
|
||||||
|
|
||||||
Licensed under the Apache License, Version 2.0 (the "License");
|
|
||||||
you may not use this file except in compliance with the License.
|
|
||||||
You may obtain a copy of the License at
|
|
||||||
|
|
||||||
http://www.apache.org/licenses/LICENSE-2.0
|
|
||||||
|
|
||||||
Unless required by applicable law or agreed to in writing, software
|
|
||||||
distributed under the License is distributed on an "AS IS" BASIS,
|
|
||||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
||||||
See the License for the specific language governing permissions and
|
|
||||||
limitations under the License.
|
|
||||||
*/
|
|
||||||
|
|
||||||
#ifndef ___ILIBWEBCLIENTDUKTAPE___
|
|
||||||
#define ___ILIBWEBCLIENTDUKTAPE___
|
|
||||||
|
|
||||||
#include "duktape.h"
|
|
||||||
|
|
||||||
void ILibWebClient_DukTape_Init(duk_context *ctx, void * chain);
|
|
||||||
|
|
||||||
|
|
||||||
#endif
|
|
||||||
@@ -1,717 +0,0 @@
|
|||||||
/*
|
|
||||||
Copyright 2006 - 2017 Intel Corporation
|
|
||||||
|
|
||||||
Licensed under the Apache License, Version 2.0 (the "License");
|
|
||||||
you may not use this file except in compliance with the License.
|
|
||||||
You may obtain a copy of the License at
|
|
||||||
|
|
||||||
http://www.apache.org/licenses/LICENSE-2.0
|
|
||||||
|
|
||||||
Unless required by applicable law or agreed to in writing, software
|
|
||||||
distributed under the License is distributed on an "AS IS" BASIS,
|
|
||||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
||||||
See the License for the specific language governing permissions and
|
|
||||||
limitations under the License.
|
|
||||||
*/
|
|
||||||
|
|
||||||
#include "ILibWebServer_Duktape.h"
|
|
||||||
#include "ILibDuktapeModSearch.h"
|
|
||||||
#include "microstack/ILibParsers.h"
|
|
||||||
#include "microstack/ILibWebServer.h"
|
|
||||||
#include "microstack/ILibWebClient.h"
|
|
||||||
#include "ILibDuktape_Helpers.h"
|
|
||||||
|
|
||||||
#if defined(WIN32) && !defined(_WIN32_WCE) && !defined(_MINCORE)
|
|
||||||
#define _CRTDBG_MAP_ALLOC
|
|
||||||
#include <crtdbg.h>
|
|
||||||
#endif
|
|
||||||
|
|
||||||
|
|
||||||
duk_idx_t ILibWebServer_DukTape_Push_ILibWebServerSession(duk_context *ctx, ILibWebServer_Session *session);
|
|
||||||
duk_idx_t ILibWebServer_DukTape_Push_PacketHeader(duk_context *ctx, ILibHTTPPacket *packet);
|
|
||||||
void ILibWebServer_DukTape_PUSH_IncomingMessage(duk_context *ctx, ILibHTTPPacket *packet, ILibWebServer_Session *session);
|
|
||||||
void ILibWebServer_DukTape_PUSH_ServerResponse(duk_context *ctx, ILibWebServer_Session *session);
|
|
||||||
|
|
||||||
|
|
||||||
void* Duktape_GetSessionPtr(duk_context *ctx)
|
|
||||||
{
|
|
||||||
void *retVal;
|
|
||||||
duk_push_this(ctx); // [session]
|
|
||||||
duk_get_prop_string(ctx, -1, "SessionPtr"); // [session][ptr]
|
|
||||||
retVal = duk_to_pointer(ctx, -1);
|
|
||||||
duk_pop_2(ctx); // ...
|
|
||||||
return retVal;
|
|
||||||
}
|
|
||||||
|
|
||||||
duk_ret_t ILibWebServer_DukTape_SendResponse(duk_context *ctx)
|
|
||||||
{
|
|
||||||
ILibWebServer_Session *session;
|
|
||||||
ILibHTTPPacket *packet;
|
|
||||||
int retVal;
|
|
||||||
|
|
||||||
duk_get_prop_string(ctx, 0, "PacketPtr");
|
|
||||||
packet = (ILibHTTPPacket*)duk_to_pointer(ctx, -1);
|
|
||||||
|
|
||||||
duk_push_this(ctx);
|
|
||||||
duk_get_prop_string(ctx, -1, "SessionPtr");
|
|
||||||
session = (ILibWebServer_Session*)duk_to_pointer(ctx, -1);
|
|
||||||
|
|
||||||
retVal = (int)ILibWebServer_Send(session, packet);
|
|
||||||
duk_push_int(ctx, retVal);
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
duk_ret_t ILibWebServer_DukTape_SendResponseRaw(duk_context *ctx)
|
|
||||||
{
|
|
||||||
ILibWebServer_Session *session;
|
|
||||||
char* buffer;
|
|
||||||
int bufferLen;
|
|
||||||
int retVal;
|
|
||||||
int doneFlag;
|
|
||||||
|
|
||||||
bufferLen = duk_require_int(ctx, 1);
|
|
||||||
buffer = Duktape_GetBuffer(ctx, 0, NULL);
|
|
||||||
doneFlag = duk_require_int(ctx, 2);
|
|
||||||
|
|
||||||
duk_push_this(ctx);
|
|
||||||
duk_get_prop_string(ctx, -1, "SessionPtr");
|
|
||||||
session = (ILibWebServer_Session*)duk_to_pointer(ctx, -1);
|
|
||||||
|
|
||||||
retVal = (int)ILibWebServer_Send_Raw(session, buffer, bufferLen, ILibAsyncSocket_MemoryOwnership_USER, (ILibWebServer_DoneFlag)doneFlag);
|
|
||||||
duk_push_int(ctx, retVal);
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
duk_ret_t ILibWebServer_DukTape_Session_StreamHeader(duk_context *ctx)
|
|
||||||
{
|
|
||||||
// enum ILibWebServer_Status ILibWebServer_StreamHeader(struct ILibWebServer_Session *session, struct packetheader *header);
|
|
||||||
|
|
||||||
ILibWebServer_Session *session;
|
|
||||||
ILibHTTPPacket *packet;
|
|
||||||
int retVal;
|
|
||||||
|
|
||||||
duk_get_prop_string(ctx, 0, "PacketPtr");
|
|
||||||
packet = (ILibHTTPPacket*)duk_to_pointer(ctx, -1);
|
|
||||||
|
|
||||||
duk_push_this(ctx);
|
|
||||||
duk_get_prop_string(ctx, -1, "SessionPtr");
|
|
||||||
session = (ILibWebServer_Session*)duk_to_pointer(ctx, -1);
|
|
||||||
|
|
||||||
retVal = (int)ILibWebServer_StreamHeader(session, packet);
|
|
||||||
duk_push_int(ctx, retVal);
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
duk_ret_t ILibWebServer_DukTape_Session_StreamHeaderRaw(duk_context *ctx)
|
|
||||||
{
|
|
||||||
// enum ILibWebServer_Status ILibWebServer_StreamHeader_Raw(struct ILibWebServer_Session *session, int StatusCode, char *StatusData, char *ResponseHeaders, enum ILibAsyncSocket_MemoryOwnership ResponseHeaders_FREE)
|
|
||||||
|
|
||||||
ILibWebServer_Session *session;
|
|
||||||
int responseCode;
|
|
||||||
char *statusData;
|
|
||||||
char *responseHeaders;
|
|
||||||
int retVal;
|
|
||||||
|
|
||||||
responseCode = duk_require_int(ctx, 0);
|
|
||||||
statusData = (char*)duk_require_string(ctx, 1);
|
|
||||||
responseHeaders = (char*)duk_require_string(ctx, 2);
|
|
||||||
|
|
||||||
duk_push_this(ctx);
|
|
||||||
duk_get_prop_string(ctx, -1, "SessionPtr");
|
|
||||||
session = (ILibWebServer_Session*)duk_to_pointer(ctx, -1);
|
|
||||||
|
|
||||||
retVal = ILibWebServer_StreamHeader_Raw(session, responseCode, statusData, responseHeaders, ILibAsyncSocket_MemoryOwnership_USER);
|
|
||||||
duk_push_int(ctx, retVal);
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
duk_ret_t ILibWebServer_DukTape_Session_StreamBody(duk_context *ctx)
|
|
||||||
{
|
|
||||||
// enum ILibWebServer_Status ILibWebServer_StreamBody(struct ILibWebServer_Session *session, char *buffer, int bufferSize, enum ILibAsyncSocket_MemoryOwnership userFree, ILibWebServer_DoneFlag done);
|
|
||||||
|
|
||||||
ILibWebServer_Session *session;
|
|
||||||
char *body;
|
|
||||||
int bodyLen;
|
|
||||||
int doneFlag;
|
|
||||||
int retVal;
|
|
||||||
|
|
||||||
bodyLen = duk_require_int(ctx, 1);
|
|
||||||
doneFlag = duk_require_int(ctx, 2);
|
|
||||||
body = Duktape_GetBuffer(ctx, 0, NULL);
|
|
||||||
|
|
||||||
duk_push_this(ctx);
|
|
||||||
duk_get_prop_string(ctx, -1, "SessionPtr");
|
|
||||||
session = (ILibWebServer_Session*)duk_to_pointer(ctx, -1);
|
|
||||||
|
|
||||||
retVal = (int)ILibWebServer_StreamBody(session, body, bodyLen, ILibAsyncSocket_MemoryOwnership_USER, (ILibWebServer_DoneFlag)doneFlag);
|
|
||||||
duk_push_int(ctx, retVal);
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
duk_ret_t ILibWebServer_DukTape_Session_Digest_IsAuthenticated(duk_context *ctx)
|
|
||||||
{
|
|
||||||
// ILibWebServer_Digest_IsAuthenticated
|
|
||||||
if (!duk_is_string(ctx, 0)) { return(ILibDuktape_Error(ctx, "IsAuthenticated(): Invalid Parameter/Type")); }
|
|
||||||
ILibWebServer_Session *session;
|
|
||||||
duk_size_t realmLen;
|
|
||||||
char* realm = (char*)duk_get_lstring(ctx, 0, &realmLen);
|
|
||||||
int retVal;
|
|
||||||
|
|
||||||
duk_push_this(ctx);
|
|
||||||
duk_get_prop_string(ctx, -1, "SessionPtr");
|
|
||||||
session = (ILibWebServer_Session*)duk_to_pointer(ctx, -1);
|
|
||||||
|
|
||||||
retVal = (int)ILibWebServer_Digest_IsAuthenticated(session, realm, (int)realmLen);
|
|
||||||
duk_push_int(ctx, retVal);
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
duk_ret_t ILibWebServer_DukTape_Session_Digest_SendUnauthorized(duk_context *ctx)
|
|
||||||
{
|
|
||||||
// void ILibWebServer_Digest_SendUnauthorized(struct ILibWebServer_Session *session, char* realm, int realmLen, char* html, int htmllen);
|
|
||||||
|
|
||||||
ILibWebServer_Session *session;
|
|
||||||
|
|
||||||
if (!duk_is_string(ctx, 0)) { return(ILibDuktape_Error(ctx, "SendUnAuthorized(): Invalid Parameter/Type (realm)")); }
|
|
||||||
duk_size_t realmLen;
|
|
||||||
char *realm = (char*)duk_get_lstring(ctx, 0, &realmLen);
|
|
||||||
int htmlLen = duk_require_int(ctx, 2);
|
|
||||||
char *html = Duktape_GetBuffer(ctx, 1, NULL);
|
|
||||||
|
|
||||||
duk_push_this(ctx);
|
|
||||||
duk_get_prop_string(ctx, -1, "SessionPtr");
|
|
||||||
session = (ILibWebServer_Session*)duk_to_pointer(ctx, -1);
|
|
||||||
|
|
||||||
ILibWebServer_Digest_SendUnauthorized(session, realm, (int)realmLen, html, htmlLen);
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
duk_ret_t ILibWebServer_DukTape_Session_Digest_GetUsername(duk_context *ctx)
|
|
||||||
{
|
|
||||||
ILibWebServer_Session *session;
|
|
||||||
char *username;
|
|
||||||
|
|
||||||
duk_push_this(ctx);
|
|
||||||
duk_get_prop_string(ctx, -1, "SessionPtr");
|
|
||||||
session = (ILibWebServer_Session*)duk_to_pointer(ctx, -1);
|
|
||||||
|
|
||||||
username = ILibWebServer_Digest_GetUsername(session);
|
|
||||||
duk_push_string(ctx, username != NULL ? username : "");
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
duk_ret_t ILibWebServer_DukTape_Session_Digest_ValidatePassword(duk_context *ctx)
|
|
||||||
{
|
|
||||||
// int ILibWebServer_Digest_ValidatePassword(struct ILibWebServer_Session *session, char* password, int passwordLen);
|
|
||||||
ILibWebServer_Session *session;
|
|
||||||
int passwordLen = duk_require_int(ctx, 1);
|
|
||||||
char *password = Duktape_GetBuffer(ctx, 0, NULL);
|
|
||||||
int retVal;
|
|
||||||
|
|
||||||
duk_push_this(ctx);
|
|
||||||
duk_get_prop_string(ctx, -1, "SessionPtr");
|
|
||||||
session = (ILibWebServer_Session*)duk_to_pointer(ctx, -1);
|
|
||||||
|
|
||||||
retVal = ILibWebServer_Digest_ValidatePassword(session, password, passwordLen);
|
|
||||||
duk_push_int(ctx, retVal);
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
void ILibWebServer_DukTape_Session_OnReceive(struct ILibWebServer_Session *sender, int InterruptFlag, struct packetheader *header, char *bodyBuffer, int *beginPointer, int endPointer, ILibWebServer_DoneFlag done)
|
|
||||||
{
|
|
||||||
duk_context *ctx = (duk_context*)((void**)sender->ParentExtraMemory)[0];
|
|
||||||
void* OnReceive = ((void**)sender->Reserved_Transport.ChainLink.ExtraMemoryPtr)[0];
|
|
||||||
|
|
||||||
duk_push_heapptr(ctx, OnReceive);
|
|
||||||
|
|
||||||
ILibWebServer_DukTape_Push_ILibWebServerSession(ctx, sender);
|
|
||||||
ILibWebServer_DukTape_Push_PacketHeader(ctx, header); // [func][this][header]
|
|
||||||
duk_get_prop_string(ctx, -2, "buffer"); // [func][this][header][buffer]
|
|
||||||
duk_config_buffer(ctx, -1, bodyBuffer + *beginPointer, endPointer - *beginPointer);
|
|
||||||
duk_push_int(ctx, (int)done); // [func][this][header][buffer][done]
|
|
||||||
|
|
||||||
if (duk_pcall_method(ctx, 3) == 0) // [retVal]
|
|
||||||
{
|
|
||||||
if (duk_get_type(ctx, -1) == DUK_TYPE_NUMBER)
|
|
||||||
{
|
|
||||||
*beginPointer = duk_get_int(ctx, -1);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
*beginPointer = endPointer;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
ILibDuktape_Process_UncaughtException(ctx);
|
|
||||||
}
|
|
||||||
duk_pop(ctx);
|
|
||||||
}
|
|
||||||
void ILibWebServer_DukTape_Session_OnDisconnect(struct ILibWebServer_Session *sender)
|
|
||||||
{
|
|
||||||
duk_context *ctx = (duk_context*)((void**)sender->ParentExtraMemory)[0];
|
|
||||||
void* OnDisconnect = ((void**)sender->Reserved_Transport.ChainLink.ExtraMemoryPtr)[1];
|
|
||||||
|
|
||||||
duk_push_heapptr(ctx, OnDisconnect);
|
|
||||||
ILibWebServer_DukTape_Push_ILibWebServerSession(ctx, sender);
|
|
||||||
if (duk_pcall_method(ctx, 0) != 0)
|
|
||||||
{
|
|
||||||
ILibDuktape_Process_UncaughtException(ctx);
|
|
||||||
}
|
|
||||||
duk_pop(ctx);
|
|
||||||
}
|
|
||||||
void ILibWebServer_DukTape_Session_OnSendOk(struct ILibWebServer_Session *sender)
|
|
||||||
{
|
|
||||||
duk_context *ctx = (duk_context*)((void**)sender->ParentExtraMemory)[0];
|
|
||||||
void* OnSendOK = ((void**)sender->Reserved_Transport.ChainLink.ExtraMemoryPtr)[2];
|
|
||||||
|
|
||||||
duk_push_heapptr(ctx, OnSendOK); // [func]
|
|
||||||
ILibWebServer_DukTape_Push_ILibWebServerSession(ctx, sender); // [func][this]
|
|
||||||
if (duk_pcall_method(ctx, 0) != 0)
|
|
||||||
{
|
|
||||||
ILibDuktape_Process_UncaughtException(ctx);
|
|
||||||
}
|
|
||||||
duk_pop(ctx);
|
|
||||||
}
|
|
||||||
|
|
||||||
duk_ret_t ILibWebServer_DukTape_OnReceive_Setter(duk_context *ctx)
|
|
||||||
{
|
|
||||||
void *OnReceive = duk_require_heapptr(ctx, 0);
|
|
||||||
ILibWebServer_Session *session;
|
|
||||||
|
|
||||||
duk_push_this(ctx);
|
|
||||||
duk_get_prop_string(ctx, -1, "SessionPtr");
|
|
||||||
session = (ILibWebServer_Session*)duk_to_pointer(ctx, -1);
|
|
||||||
((void**)session->Reserved_Transport.ChainLink.ExtraMemoryPtr)[0] = OnReceive;
|
|
||||||
session->OnReceive = ILibWebServer_DukTape_Session_OnReceive;
|
|
||||||
|
|
||||||
duk_push_heapptr(ctx, session->User); // [session]
|
|
||||||
duk_dup(ctx, 0); // [session][ptr]
|
|
||||||
duk_put_prop_string(ctx, -2, "OnReceivePtr"); // [session]
|
|
||||||
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
duk_ret_t ILibWebServer_DukTape_OnDisconnect_Setter(duk_context *ctx)
|
|
||||||
{
|
|
||||||
void *OnDisconnect = duk_require_heapptr(ctx, 0);
|
|
||||||
ILibWebServer_Session *session;
|
|
||||||
|
|
||||||
duk_push_this(ctx);
|
|
||||||
duk_get_prop_string(ctx, -1, "SessionPtr");
|
|
||||||
session = (ILibWebServer_Session*)duk_to_pointer(ctx, -1);
|
|
||||||
((void**)session->Reserved_Transport.ChainLink.ExtraMemoryPtr)[1] = OnDisconnect;
|
|
||||||
session->OnDisconnect = ILibWebServer_DukTape_Session_OnDisconnect;
|
|
||||||
|
|
||||||
duk_push_heapptr(ctx, session->User); // [session]
|
|
||||||
duk_dup(ctx, 0); // [session][ptr]
|
|
||||||
duk_put_prop_string(ctx, -2, "OnDisconnectPtr"); // [session]
|
|
||||||
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
duk_ret_t ILibWebServer_DukTape_OnSendOk_Setter(duk_context *ctx)
|
|
||||||
{
|
|
||||||
void *OnSendOk = duk_require_heapptr(ctx, 0);
|
|
||||||
ILibWebServer_Session *session;
|
|
||||||
|
|
||||||
duk_push_this(ctx);
|
|
||||||
duk_get_prop_string(ctx, -1, "SessionPtr");
|
|
||||||
session = (ILibWebServer_Session*)duk_to_pointer(ctx, -1);
|
|
||||||
((void**)session->Reserved_Transport.ChainLink.ExtraMemoryPtr)[2] = OnSendOk;
|
|
||||||
session->OnSendOK = ILibWebServer_DukTape_Session_OnSendOk;
|
|
||||||
|
|
||||||
duk_push_heapptr(ctx, session->User); // [session]
|
|
||||||
duk_dup(ctx, 0); // [session][ptr]
|
|
||||||
duk_put_prop_string(ctx, -2, "OnSendOKPtr"); // [session]
|
|
||||||
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
duk_idx_t ILibWebServer_DukTape_Session_IsCrossSiteRequest(duk_context *ctx)
|
|
||||||
{
|
|
||||||
ILibWebServer_Session *session;
|
|
||||||
char *retVal;
|
|
||||||
|
|
||||||
duk_push_this(ctx);
|
|
||||||
duk_get_prop_string(ctx, -1, "SessionPtr");
|
|
||||||
session = (ILibWebServer_Session*)duk_to_pointer(ctx, -1);
|
|
||||||
|
|
||||||
retVal = ILibWebServer_IsCrossSiteRequest(session);
|
|
||||||
duk_push_string(ctx, retVal == NULL ? "" : retVal);
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
duk_idx_t ILibWebServer_DukTape_Session_GetWebSocketDataType(duk_context *ctx)
|
|
||||||
{
|
|
||||||
ILibWebServer_Session *session;
|
|
||||||
ILibWebServer_WebSocket_DataTypes retVal;
|
|
||||||
|
|
||||||
duk_push_this(ctx);
|
|
||||||
duk_get_prop_string(ctx, -1, "SessionPtr");
|
|
||||||
session = (ILibWebServer_Session*)duk_to_pointer(ctx, -1);
|
|
||||||
|
|
||||||
retVal = ILibWebServer_WebSocket_GetDataType(session);
|
|
||||||
duk_push_int(ctx, (int)retVal);
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
duk_idx_t ILibWebServer_DukTape_Session_UpgradeWebSocket(duk_context *ctx)
|
|
||||||
{
|
|
||||||
ILibWebServer_Session *session = Duktape_GetSessionPtr(ctx);
|
|
||||||
int fragmentReassemblySize = duk_require_int(ctx, 0);
|
|
||||||
int retVal;
|
|
||||||
|
|
||||||
retVal = ILibWebServer_UpgradeWebSocket(session, fragmentReassemblySize);
|
|
||||||
duk_push_int(ctx, retVal);
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
duk_idx_t ILibWebServer_DukTape_Session_WebSocket_Send(duk_context *ctx)
|
|
||||||
{
|
|
||||||
// enum ILibWebServer_Status ILibWebServer_WebSocket_Send(struct ILibWebServer_Session *session, char* buffer, int bufferLen, ILibWebServer_WebSocket_DataTypes bufferType, enum ILibAsyncSocket_MemoryOwnership userFree, ILibWebServer_WebSocket_FragmentFlags fragmentStatus);
|
|
||||||
ILibWebServer_Session *session = Duktape_GetSessionPtr(ctx);
|
|
||||||
char *buffer = Duktape_GetBuffer(ctx, 0, NULL);
|
|
||||||
int bufferLen = duk_require_int(ctx, 1);
|
|
||||||
int dataType = duk_require_int(ctx, 2);
|
|
||||||
int fragmentFlags = duk_require_int(ctx, 3);
|
|
||||||
|
|
||||||
ILibWebServer_Status retVal = ILibWebServer_WebSocket_Send(session, buffer, bufferLen, dataType, ILibAsyncSocket_MemoryOwnership_USER, fragmentFlags);
|
|
||||||
duk_push_int(ctx, (int)retVal);
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
duk_idx_t ILibWebServer_DukTape_Session_WebSocket_Close(duk_context *ctx)
|
|
||||||
{
|
|
||||||
ILibWebServer_Session *session = Duktape_GetSessionPtr(ctx);
|
|
||||||
|
|
||||||
ILibWebServer_WebSocket_Close(session);
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
duk_ret_t ILibWebServer_DukTape_Session_Pause(duk_context *ctx)
|
|
||||||
{
|
|
||||||
duk_push_this(ctx);
|
|
||||||
duk_get_prop_string(ctx, -1, "SessionPtr");
|
|
||||||
ILibWebServer_Pause((ILibWebServer_Session*)duk_to_pointer(ctx, -1));
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
duk_ret_t ILibWebServer_DukTape_Session_Resume(duk_context *ctx)
|
|
||||||
{
|
|
||||||
duk_push_this(ctx);
|
|
||||||
duk_get_prop_string(ctx, -1, "SessionPtr");
|
|
||||||
ILibWebServer_Resume((ILibWebServer_Session*)duk_to_pointer(ctx, -1));
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
duk_idx_t ILibWebServer_DukTape_Push_ILibWebServerSession(duk_context *ctx, ILibWebServer_Session *session)
|
|
||||||
{
|
|
||||||
if (session->User != NULL)
|
|
||||||
{
|
|
||||||
duk_push_heapptr(ctx, session->User); // [session]
|
|
||||||
return(duk_get_top_index(ctx));
|
|
||||||
}
|
|
||||||
duk_push_heap_stash(ctx); // [stash]
|
|
||||||
duk_push_object(ctx); // [stash][obj]
|
|
||||||
session->User = duk_get_heapptr(ctx, -1);
|
|
||||||
duk_dup(ctx, -1); // [stash][obj][obj]
|
|
||||||
duk_put_prop_string(ctx, -3, Duktape_GetStashKey(session->User)); // [stash][obj]
|
|
||||||
|
|
||||||
duk_swap_top(ctx, -2); // [obj][stash]
|
|
||||||
duk_pop(ctx); // [obj]
|
|
||||||
|
|
||||||
duk_push_external_buffer(ctx); // [obj][buffer]
|
|
||||||
duk_put_prop_string(ctx, -2, "buffer"); // [obj]
|
|
||||||
|
|
||||||
duk_push_pointer(ctx, session); // [obj][pointer]
|
|
||||||
duk_put_prop_string(ctx, -2, "SessionPtr"); // [obj]
|
|
||||||
|
|
||||||
duk_push_c_function(ctx, ILibWebServer_DukTape_SendResponse, 1); // [obj][func]
|
|
||||||
duk_put_prop_string(ctx, -2, "SendResponse"); // [obj]
|
|
||||||
duk_push_c_function(ctx, ILibWebServer_DukTape_SendResponseRaw, 3);
|
|
||||||
duk_put_prop_string(ctx, -2, "SendResponseRaw");
|
|
||||||
duk_push_c_function(ctx, ILibWebServer_DukTape_Session_StreamHeader, 1);
|
|
||||||
duk_put_prop_string(ctx, -2, "StreamHeader");
|
|
||||||
duk_push_c_function(ctx, ILibWebServer_DukTape_Session_StreamHeaderRaw, 3);
|
|
||||||
duk_put_prop_string(ctx, -2, "StreamHeaderRaw");
|
|
||||||
duk_push_c_function(ctx, ILibWebServer_DukTape_Session_StreamBody, 3);
|
|
||||||
duk_put_prop_string(ctx, -2, "StreamBody");
|
|
||||||
duk_push_c_function(ctx, ILibWebServer_DukTape_Session_Pause, 0);
|
|
||||||
duk_put_prop_string(ctx, -2, "Pause");
|
|
||||||
duk_push_c_function(ctx, ILibWebServer_DukTape_Session_Resume, 0);
|
|
||||||
duk_put_prop_string(ctx, -2, "Resume");
|
|
||||||
|
|
||||||
duk_push_c_function(ctx, ILibWebServer_DukTape_Session_Digest_IsAuthenticated, 1);
|
|
||||||
duk_put_prop_string(ctx, -2, "Digest_IsAuthenticated");
|
|
||||||
duk_push_c_function(ctx, ILibWebServer_DukTape_Session_Digest_SendUnauthorized, 3);
|
|
||||||
duk_put_prop_string(ctx, -2, "Digest_SendUnauthorized");
|
|
||||||
duk_push_c_function(ctx, ILibWebServer_DukTape_Session_Digest_GetUsername, 0);
|
|
||||||
duk_put_prop_string(ctx, -2, "Digest_GetUsername");
|
|
||||||
duk_push_c_function(ctx, ILibWebServer_DukTape_Session_Digest_ValidatePassword, 2);
|
|
||||||
duk_put_prop_string(ctx, -2, "Digest_ValidatePassword");
|
|
||||||
|
|
||||||
duk_push_c_function(ctx, ILibWebServer_DukTape_Session_IsCrossSiteRequest, 0);
|
|
||||||
duk_put_prop_string(ctx, -2, "IsCrossSiteRequest");
|
|
||||||
|
|
||||||
duk_push_c_function(ctx, ILibWebServer_DukTape_Session_GetWebSocketDataType, 0);
|
|
||||||
duk_put_prop_string(ctx, -2, "WebSocket_GetDataType");
|
|
||||||
duk_push_c_function(ctx, ILibWebServer_DukTape_Session_UpgradeWebSocket, 1);
|
|
||||||
duk_put_prop_string(ctx, -2, "WebSocket_UpgradeToWebSocket");
|
|
||||||
duk_push_c_function(ctx, ILibWebServer_DukTape_Session_WebSocket_Send, 4);
|
|
||||||
duk_put_prop_string(ctx, -2, "WebSocket_Send");
|
|
||||||
duk_push_c_function(ctx, ILibWebServer_DukTape_Session_WebSocket_Close, 0);
|
|
||||||
duk_put_prop_string(ctx, -2, "WebSocket_Close");
|
|
||||||
|
|
||||||
duk_push_string(ctx, "OnReceive"); //[obj][key]
|
|
||||||
duk_push_c_function(ctx, ILibWebServer_DukTape_OnReceive_Setter, 1); //[obj][key][Func]
|
|
||||||
duk_def_prop(ctx, -3, DUK_DEFPROP_HAVE_SETTER); //[obj]
|
|
||||||
|
|
||||||
duk_push_string(ctx, "OnDisconnect"); //[obj][key]
|
|
||||||
duk_push_c_function(ctx, ILibWebServer_DukTape_OnDisconnect_Setter, 1); //[obj][key][Func]
|
|
||||||
duk_def_prop(ctx, -3, DUK_DEFPROP_HAVE_SETTER); //[obj]
|
|
||||||
|
|
||||||
duk_push_string(ctx, "OnSendOk"); //[obj][key]
|
|
||||||
duk_push_c_function(ctx, ILibWebServer_DukTape_OnSendOk_Setter, 1); //[obj][key][Func]
|
|
||||||
duk_def_prop(ctx, -3, DUK_DEFPROP_HAVE_SETTER); //[obj]
|
|
||||||
|
|
||||||
return duk_get_top_index(ctx);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
duk_ret_t ILibWebServer_DukTape_GetHeaderline(duk_context *ctx)
|
|
||||||
{
|
|
||||||
if (!duk_is_string(ctx, 0)) { return(ILibDuktape_Error(ctx, "GetHeaderLine(): Invalid Parameter/Type")); }
|
|
||||||
duk_size_t headerLen;
|
|
||||||
char* header = (char*)duk_get_lstring(ctx, 0, &headerLen);
|
|
||||||
struct packetheader *packet;
|
|
||||||
char* val;
|
|
||||||
|
|
||||||
duk_push_this(ctx);
|
|
||||||
duk_get_prop_string(ctx, -1, "PacketPtr");
|
|
||||||
packet = (struct packetheader*)duk_to_pointer(ctx, -1);
|
|
||||||
|
|
||||||
val = ILibGetHeaderLine(packet, header, (int)headerLen);
|
|
||||||
if (val == NULL)
|
|
||||||
{
|
|
||||||
duk_push_string(ctx, "");
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
duk_push_string(ctx, val);
|
|
||||||
}
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
duk_ret_t ILibWebServer_DukTape_Packet_SetDirective(duk_context *ctx)
|
|
||||||
{
|
|
||||||
if (!duk_is_string(ctx, 0) || !duk_is_string(ctx, 1)) { return(ILibDuktape_Error(ctx, "SetDirective(): Invalid Parameter/Type(s)")); }
|
|
||||||
duk_size_t directiveLen, pathLen;
|
|
||||||
char *directive = (char*)duk_get_lstring(ctx, 0, &directiveLen);
|
|
||||||
char *path = (char*)duk_get_lstring(ctx, 1, &pathLen);
|
|
||||||
struct packetheader *packet;
|
|
||||||
|
|
||||||
duk_push_this(ctx);
|
|
||||||
duk_get_prop_string(ctx, -1, "PacketPtr");
|
|
||||||
packet = (struct packetheader*)duk_to_pointer(ctx, -1);
|
|
||||||
|
|
||||||
ILibSetDirective(packet, directive, (int)directiveLen, path, (int)pathLen);
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
duk_ret_t ILibWebServer_DukTape_Packet_AddHeader(duk_context *ctx)
|
|
||||||
{
|
|
||||||
if (!duk_is_string(ctx, 0) || !duk_is_string(ctx, 1)) { return(ILibDuktape_Error(ctx, "AddHeader(): Invalid Parameter/Type(s)")); }
|
|
||||||
duk_size_t fieldNameLen, fieldNameValueLen;
|
|
||||||
char* fieldName = (char*)duk_get_lstring(ctx, 0, &fieldNameLen);
|
|
||||||
char* fieldValue = (char*)duk_get_lstring(ctx, 1, &fieldNameValueLen);
|
|
||||||
struct packetheader *packet;
|
|
||||||
|
|
||||||
duk_push_this(ctx);
|
|
||||||
duk_get_prop_string(ctx, -1, "PacketPtr");
|
|
||||||
packet = (struct packetheader*)duk_to_pointer(ctx, -1);
|
|
||||||
|
|
||||||
ILibAddHeaderLine(packet, fieldName, (int)fieldNameLen, fieldValue, (int)fieldNameValueLen);
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
duk_ret_t ILibWebServer_DukTape_Packet_SetResponse(duk_context *ctx)
|
|
||||||
{
|
|
||||||
int statusCode = duk_require_int(ctx, 0);
|
|
||||||
duk_size_t responseLen;
|
|
||||||
if (!duk_is_string(ctx, 1)) { return(ILibDuktape_Error(ctx, "SetResponse(): Response was invalid ParameterType")); }
|
|
||||||
char *response = (char*)duk_get_lstring(ctx, 1, &responseLen);
|
|
||||||
struct packetheader *packet;
|
|
||||||
|
|
||||||
duk_push_this(ctx);
|
|
||||||
duk_get_prop_string(ctx, -1, "PacketPtr");
|
|
||||||
packet = (struct packetheader*)duk_to_pointer(ctx, -1);
|
|
||||||
|
|
||||||
ILibSetStatusCode(packet, statusCode, response, (int)responseLen);
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
duk_ret_t ILibWebServer_DukTape_Packet_SetStringBody(duk_context *ctx)
|
|
||||||
{
|
|
||||||
duk_size_t bodyLen;
|
|
||||||
if (!duk_is_string(ctx, 0)) { return(ILibDuktape_Error(ctx, "SetStringBody(): Invalid Parameter/Type")); }
|
|
||||||
char *body = (char*)duk_get_lstring(ctx, 0, &bodyLen);
|
|
||||||
struct packetheader *packet;
|
|
||||||
char *tmp;
|
|
||||||
char len[65];
|
|
||||||
|
|
||||||
duk_push_this(ctx);
|
|
||||||
duk_get_prop_string(ctx, -1, "PacketPtr");
|
|
||||||
packet = (struct packetheader*)duk_to_pointer(ctx, -1);
|
|
||||||
|
|
||||||
tmp = ILibString_Copy(body, (int)bodyLen);
|
|
||||||
packet->Body = tmp;
|
|
||||||
packet->BodyLength = (int)bodyLen;
|
|
||||||
|
|
||||||
#ifdef WIN32
|
|
||||||
_itoa_s(packet->BodyLength, len, 65, 10);
|
|
||||||
#else
|
|
||||||
sprintf_s(len, 65, "%d", packet->BodyLength);
|
|
||||||
#endif
|
|
||||||
ILibAddHeaderLine(packet, "Content-Length", 14, len, (int)strnlen_s(len, sizeof(len)));
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
duk_ret_t ILibWebServer_DukTape_Packet_SetBody(duk_context *ctx)
|
|
||||||
{
|
|
||||||
int bodyLen = duk_require_int(ctx, 1);
|
|
||||||
char len[65];
|
|
||||||
struct packetheader *packet;
|
|
||||||
char* body;
|
|
||||||
|
|
||||||
body = Duktape_GetBuffer(ctx, 0, NULL);
|
|
||||||
|
|
||||||
duk_push_this(ctx);
|
|
||||||
duk_get_prop_string(ctx, -1, "PacketPtr");
|
|
||||||
packet = (struct packetheader*)duk_to_pointer(ctx, -1);
|
|
||||||
|
|
||||||
packet->Body = (char*)malloc(bodyLen);
|
|
||||||
memcpy_s(packet->Body, bodyLen, body, bodyLen);
|
|
||||||
packet->BodyLength = bodyLen;
|
|
||||||
|
|
||||||
#ifdef WIN32
|
|
||||||
_itoa_s(packet->BodyLength, len, 65, 10);
|
|
||||||
#else
|
|
||||||
sprintf_s(len, 65, "%d", packet->BodyLength);
|
|
||||||
#endif
|
|
||||||
ILibAddHeaderLine(packet, "Content-Length", 14, len, (int)strnlen_s(len, sizeof(len)));
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
duk_idx_t ILibWebServer_DukTape_PacketHeader_AddWebSocketRequestHeaders(duk_context *ctx)
|
|
||||||
{
|
|
||||||
ILibHTTPPacket *packet;
|
|
||||||
int maxReassemblySize = duk_require_int(ctx, 0);
|
|
||||||
|
|
||||||
duk_push_this(ctx);
|
|
||||||
duk_get_prop_string(ctx, -1, "PacketPtr");
|
|
||||||
packet = (ILibHTTPPacket*)duk_to_pointer(ctx, -1);
|
|
||||||
|
|
||||||
ILibWebClient_AddWebSocketRequestHeaders(packet, maxReassemblySize, NULL);
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
duk_idx_t ILibWebServer_DukTape_Push_PacketHeader(duk_context *ctx, ILibHTTPPacket *packet)
|
|
||||||
{
|
|
||||||
duk_idx_t j;
|
|
||||||
|
|
||||||
j = duk_push_object(ctx);
|
|
||||||
duk_push_pointer(ctx, packet);
|
|
||||||
duk_put_prop_string(ctx, j, "PacketPtr");
|
|
||||||
|
|
||||||
if (packet->Directive != NULL)
|
|
||||||
{
|
|
||||||
packet->Directive[packet->DirectiveLength] = 0;
|
|
||||||
duk_push_string(ctx, packet->Directive);
|
|
||||||
duk_put_prop_string(ctx, j, "Directive");
|
|
||||||
packet->DirectiveObj[packet->DirectiveObjLength] = 0;
|
|
||||||
duk_push_string(ctx, packet->DirectiveObj);
|
|
||||||
duk_put_prop_string(ctx, j, "Path");
|
|
||||||
}
|
|
||||||
|
|
||||||
if(packet->StatusData != NULL)
|
|
||||||
{
|
|
||||||
duk_push_int(ctx, packet->StatusCode);
|
|
||||||
duk_put_prop_string(ctx, j, "StatusCode");
|
|
||||||
packet->StatusData[packet->StatusDataLength] = 0;
|
|
||||||
duk_push_string(ctx, packet->StatusData);
|
|
||||||
duk_put_prop_string(ctx, j, "StatusData");
|
|
||||||
}
|
|
||||||
|
|
||||||
duk_push_c_function(ctx, ILibWebServer_DukTape_PacketHeader_AddWebSocketRequestHeaders, 1);
|
|
||||||
duk_put_prop_string(ctx, j, "WebSocket_AddRequestHeaders");
|
|
||||||
|
|
||||||
duk_push_c_function(ctx, ILibWebServer_DukTape_GetHeaderline, 1);
|
|
||||||
duk_put_prop_string(ctx, j, "GetHeader");
|
|
||||||
|
|
||||||
duk_push_c_function(ctx, ILibWebServer_DukTape_Packet_AddHeader, 2);
|
|
||||||
duk_put_prop_string(ctx, j, "AddHeader");
|
|
||||||
|
|
||||||
duk_push_c_function(ctx, ILibWebServer_DukTape_Packet_SetDirective, 2);
|
|
||||||
duk_put_prop_string(ctx, j, "SetDirective");
|
|
||||||
|
|
||||||
duk_push_c_function(ctx, ILibWebServer_DukTape_Packet_SetResponse, 2);
|
|
||||||
duk_put_prop_string(ctx, j, "SetResponse");
|
|
||||||
|
|
||||||
duk_push_c_function(ctx, ILibWebServer_DukTape_Packet_SetStringBody, 1);
|
|
||||||
duk_put_prop_string(ctx, j, "SetStringBody");
|
|
||||||
|
|
||||||
duk_push_c_function(ctx, ILibWebServer_DukTape_Packet_SetBody, 2);
|
|
||||||
duk_put_prop_string(ctx, j, "SetBody");
|
|
||||||
return(j);
|
|
||||||
}
|
|
||||||
|
|
||||||
void ILibWebServer_DukTape_OnSession(struct ILibWebServer_Session *SessionToken, void *User)
|
|
||||||
{
|
|
||||||
duk_context* ctx = (duk_context*)((void**)SessionToken->ParentExtraMemory)[0];
|
|
||||||
void* DukOnSession = ((void**)SessionToken->ParentExtraMemory)[1];
|
|
||||||
|
|
||||||
duk_push_heapptr(ctx, DukOnSession);
|
|
||||||
ILibWebServer_DukTape_Push_ILibWebServerSession(ctx, SessionToken);
|
|
||||||
|
|
||||||
if (duk_pcall_method(ctx, 0) != 0)
|
|
||||||
{
|
|
||||||
ILibDuktape_Process_UncaughtException(ctx);
|
|
||||||
}
|
|
||||||
duk_pop(ctx);
|
|
||||||
}
|
|
||||||
|
|
||||||
duk_ret_t ILibWebServer_DukTape_Create(duk_context *ctx)
|
|
||||||
{
|
|
||||||
//MaxConnections, PortNumber, OnSession
|
|
||||||
int MaxConnection = duk_require_int(ctx, 0);
|
|
||||||
int PortNumber = duk_require_int(ctx, 1);
|
|
||||||
void* OnSession = duk_require_heapptr(ctx, 2);
|
|
||||||
void* server;
|
|
||||||
void* chain;
|
|
||||||
|
|
||||||
duk_push_current_function(ctx);
|
|
||||||
duk_get_prop_string(ctx, -1, "chain");
|
|
||||||
chain = duk_to_pointer(ctx, -1);
|
|
||||||
|
|
||||||
server = ILibWebServer_Create2(chain, MaxConnection, PortNumber, ILibWebServer_DukTape_OnSession, 3 * sizeof(void*), NULL);
|
|
||||||
((void**)((ILibChain_Link*)server)->ExtraMemoryPtr)[0] = ctx;
|
|
||||||
((void**)((ILibChain_Link*)server)->ExtraMemoryPtr)[1] = OnSession;
|
|
||||||
|
|
||||||
duk_push_heap_stash(ctx);
|
|
||||||
duk_dup(ctx, 2);
|
|
||||||
duk_put_prop_string(ctx, -2, Duktape_GetStashKey(server));
|
|
||||||
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
duk_ret_t ILibWebServer_DukTape_CreatePacket(duk_context *ctx)
|
|
||||||
{
|
|
||||||
duk_size_t versionLen;
|
|
||||||
if (!duk_is_string(ctx, 0)) { return(ILibDuktape_Error(ctx, "CreatePacket(): Invalid Parameter/Type")); }
|
|
||||||
char* version = (char*)duk_get_lstring(ctx, 0, &versionLen);
|
|
||||||
struct packetheader *header = ILibCreateEmptyPacket();
|
|
||||||
ILibSetVersion(header, version, (int)versionLen);
|
|
||||||
|
|
||||||
ILibWebServer_DukTape_Push_PacketHeader(ctx, header);
|
|
||||||
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
void ILibWebServer_DukTape_Init(duk_context* ctx, void *chain)
|
|
||||||
{
|
|
||||||
duk_idx_t i;
|
|
||||||
|
|
||||||
duk_push_global_object(ctx); // [global]
|
|
||||||
i = duk_push_c_function(ctx, ILibWebServer_DukTape_Create, 3); // [global][func]
|
|
||||||
duk_push_pointer(ctx, chain); // [global][func][ptr]
|
|
||||||
duk_put_prop_string(ctx, i, "chain"); // [global][func]
|
|
||||||
duk_put_prop_string(ctx, -2, "ILibWebServer_Create"); // [global]
|
|
||||||
|
|
||||||
duk_push_c_function(ctx, ILibWebServer_DukTape_CreatePacket, 1); // [global][func]
|
|
||||||
duk_put_prop_string(ctx, -2, "ILibWebServer_CreatePacket"); // [global]
|
|
||||||
Duktape_CreateEnum(ctx, "WebSocket_DataTypes", (char* []){ "UNKNOWN", "REQUEST", "BINARY", "TEXT" }, (int []){ 0x00, 0xFF, 0x2, 0x1 }, 4);
|
|
||||||
Duktape_CreateEnum(ctx, "WebSocket_FragmentFlags", (char* []) { "INCOMPLETE", "COMPLETE" }, (int[]) { 0, 1 }, 2);
|
|
||||||
Duktape_CreateEnum(ctx, "WebServer_DoneFlags", (char* []) { "NOTDONE", "DONE", "PARTIAL", "LASTPARTIAL" }, (int[]) { 0, 1, 10, 11 }, 4);
|
|
||||||
|
|
||||||
duk_pop(ctx); // ...
|
|
||||||
}
|
|
||||||
@@ -1,24 +0,0 @@
|
|||||||
/*
|
|
||||||
Copyright 2006 - 2017 Intel Corporation
|
|
||||||
|
|
||||||
Licensed under the Apache License, Version 2.0 (the "License");
|
|
||||||
you may not use this file except in compliance with the License.
|
|
||||||
You may obtain a copy of the License at
|
|
||||||
|
|
||||||
http://www.apache.org/licenses/LICENSE-2.0
|
|
||||||
|
|
||||||
Unless required by applicable law or agreed to in writing, software
|
|
||||||
distributed under the License is distributed on an "AS IS" BASIS,
|
|
||||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
||||||
See the License for the specific language governing permissions and
|
|
||||||
limitations under the License.
|
|
||||||
*/
|
|
||||||
|
|
||||||
#ifndef ___ILIBWEBSERVERDUKTAPE___
|
|
||||||
#define ___ILIBWEBSERVERDUKTAPE___
|
|
||||||
|
|
||||||
#include "duktape.h"
|
|
||||||
|
|
||||||
void ILibWebServer_DukTape_Init(duk_context *ctx, void * chain);
|
|
||||||
|
|
||||||
#endif
|
|
||||||
Reference in New Issue
Block a user