1
0
mirror of https://github.com/Ylianst/MeshAgent synced 2025-12-06 00:13:33 +00:00

Added support for gzip stream to http.request

This commit is contained in:
Bryan Roe
2020-06-20 10:40:10 -07:00
parent 6ca4557859
commit 1962100df1
7 changed files with 59 additions and 28 deletions

View File

@@ -310,9 +310,10 @@ duk_ret_t ILibDuktape_CompressedStream_decompressor(duk_context *ctx)
cs->Z.opaque = Z_NULL;
cs->Z.avail_in = 0;
cs->Z.next_in = Z_NULL;
if (duk_is_number(ctx, 0) && duk_require_int(ctx, 0)==1)
if (duk_is_object(ctx, 0))
{
if (inflateInit2(&(cs->Z), -MAX_WBITS) != Z_OK) { return(ILibDuktape_Error(ctx, "zlib error")); }
int wbits = Duktape_GetIntPropertyValue(ctx, 0, "WBITS", -15);
if (inflateInit2(&(cs->Z), wbits) != Z_OK) { return(ILibDuktape_Error(ctx, "zlib error")); }
}
else
{

View File

@@ -67,6 +67,14 @@ typedef struct ILibDuktape_ContextData
#define duk_ctx_shutting_down(ctx) ((duk_ctx_context_data(ctx)->flags & duk_destroy_heap_in_progress)==duk_destroy_heap_in_progress)
#define duk_ctx_chain(ctx) (duk_ctx_is_alive(ctx)?duk_ctx_context_data(ctx)->chain:NULL)
#define duk_stream_flags_string 0x01
#define duk_stream_flags_buffer 0x00
#define duk_stream_flags_flush 0x04
#define duk_stream_flags_isString(flags) (((flags)&duk_stream_flags_string)==duk_stream_flags_string)
#define duk_stream_flags_isBuffer(flags) (((flags)&duk_stream_flags_string)==duk_stream_flags_buffer)
#define duk_stream_flags_isFlush(flags) (((flags)&duk_stream_flags_flush)==duk_stream_flags_flush)
typedef void(*Duktape_EventLoopDispatch)(void *chain, void *user);
void Duktape_RunOnEventLoop(void *chain, uintptr_t nonce, duk_context *ctx, Duktape_EventLoopDispatch handler, Duktape_EventLoopDispatch abortHandler, void *user);
#define Duktape_RunOnEventLoopEx(chain, nonce, ctx, handler, user, freeOnShutdown) Duktape_RunOnEventLoop(chain, nonce, ctx, handler, (freeOnShutdown==0?NULL:(Duktape_EventLoopDispatch)(uintptr_t)0x01), user)

View File

@@ -2827,27 +2827,12 @@ duk_ret_t ILibDuktape_HttpStream_IncomingMessage_finalizer(duk_context *ctx)
{
return(0);
}
void ILibDuktape_HttpStream_IncomingMessage_PUSH(duk_context *ctx, ILibHTTPPacket *header, void *httpstream)
void ILibDuktape_HttpStream_AddHeaderDef(duk_context *ctx, struct packetheader *header)
{
duk_push_object(ctx); // [message]
ILibDuktape_CreateFinalizer(ctx, ILibDuktape_HttpStream_IncomingMessage_finalizer);
duk_push_heapptr(ctx, httpstream); // [message][httpStream]
duk_dup(ctx, -1); // [message][httpStream][dup]
duk_put_prop_string(ctx, -3, ILibDuktape_IMSG2HttpStream); // [message][httpStream]
duk_get_prop_string(ctx, -1, ILibDuktape_HTTPStream2HTTP); // [message][httpStream][http]
duk_remove(ctx, -2); // [message][http]
duk_get_prop_string(ctx, -1, ILibDuktape_OBJID); // [message][http][id]
duk_get_prop_string(ctx, -1, "concat"); // [message][http][id][concat]
duk_swap_top(ctx, -2); // [message][http][concat][this]
duk_push_string(ctx, ".IncomingMessage"); // [message][http][concat][this][.IncomingMessage]
if (duk_pcall_method(ctx, 1) != 0) { duk_pop(ctx); duk_push_string(ctx, "http[s].IncomingMessage"); }
duk_remove(ctx, -2); // [message][http/s.IncomingMessage]
duk_put_prop_string(ctx, -2, ILibDuktape_OBJID); // [message]
duk_push_object(ctx); // [message][headers]
packetheader_field_node *node = header->FirstField;
while (node != NULL)
{
{
duk_push_lstring(ctx, node->Field, node->FieldLength); // [message][headers][key]
duk_push_lstring(ctx, node->FieldData, node->FieldDataLength); // [message][headers][key][value]
duk_put_prop(ctx, -3); // [message][headers]
@@ -2875,6 +2860,26 @@ void ILibDuktape_HttpStream_IncomingMessage_PUSH(duk_context *ctx, ILibHTTPPacke
duk_push_lstring(ctx, header->StatusData, header->StatusDataLength); // [message][statusMessage]
duk_put_prop_string(ctx, -2, "statusMessage"); // [message]
}
}
void ILibDuktape_HttpStream_IncomingMessage_PUSH(duk_context *ctx, ILibHTTPPacket *header, void *httpstream)
{
duk_push_object(ctx); // [message]
ILibDuktape_CreateFinalizer(ctx, ILibDuktape_HttpStream_IncomingMessage_finalizer);
duk_push_heapptr(ctx, httpstream); // [message][httpStream]
duk_dup(ctx, -1); // [message][httpStream][dup]
duk_put_prop_string(ctx, -3, ILibDuktape_IMSG2HttpStream); // [message][httpStream]
duk_get_prop_string(ctx, -1, ILibDuktape_HTTPStream2HTTP); // [message][httpStream][http]
duk_remove(ctx, -2); // [message][http]
duk_get_prop_string(ctx, -1, ILibDuktape_OBJID); // [message][http][id]
duk_get_prop_string(ctx, -1, "concat"); // [message][http][id][concat]
duk_swap_top(ctx, -2); // [message][http][concat][this]
duk_push_string(ctx, ".IncomingMessage"); // [message][http][concat][this][.IncomingMessage]
if (duk_pcall_method(ctx, 1) != 0) { duk_pop(ctx); duk_push_string(ctx, "http[s].IncomingMessage"); }
duk_remove(ctx, -2); // [message][http/s.IncomingMessage]
duk_put_prop_string(ctx, -2, ILibDuktape_OBJID); // [message]
ILibDuktape_HttpStream_AddHeaderDef(ctx, header);
ILibDuktape_CreateInstanceMethod(ctx, "Digest_IsAuthenticated", ILibDuktape_HttpStream_IncomingMessage_Digest_IsAuthenticated, DUK_VARARGS);
ILibDuktape_CreateInstanceMethod(ctx, "Digest_GetUsername", ILibDuktape_HttpStream_IncomingMessage_Digest_GetUsername, 0);
ILibDuktape_CreateInstanceMethod(ctx, "Digest_ValidatePassword", ILibDuktape_HttpStream_IncomingMessage_Digest_ValidatePassword, 1);
@@ -2940,6 +2945,8 @@ duk_ret_t ILibDuktape_HttpStream_OnReceive_bodyStreamFinalized(duk_context *ctx)
}
return(0);
}
void ILibDuktape_HttpStream_OnReceive(ILibWebClient_StateObject WebStateObject, int InterruptFlag, struct packetheader *header, char *bodyBuffer, int *beginPointer, int endPointer, ILibWebClient_ReceiveStatus recvStatus, void *user1, void *user2, int *PAUSE)
{
ILibDuktape_HttpStream_Data *data = (ILibDuktape_HttpStream_Data*)user1;
@@ -3118,6 +3125,21 @@ void ILibDuktape_HttpStream_OnReceive(ILibWebClient_StateObject WebStateObject,
duk_pop(ctx); // [emit][this][response][imsg]
ILibDuktape_EventEmitter_AddOnEx(ctx, -1, "~", ILibDuktape_HttpStream_OnReceive_bodyStreamFinalized);
int htmpLen;
char *htmp = ILibGetHeaderLineEx(header, "Content-Encoding", 16, &htmpLen);
if (htmpLen == 4 && strncmp(htmp, "gzip", 4) == 0)
{
duk_eval_string(ctx, "require('compressed-stream').createDecompressor({WBITS:31});"); // [emit][this][response][imsg][dec]
duk_prepare_method_call(ctx, -2, "pipe"); // [emit][this][response][imsg][dec][pipe][this]
duk_dup(ctx, -3); // [emit][this][response][imsg][dec][pipe][this][dec]
duk_pcall_method(ctx, 1); // [emit][this][response][imsg][dec][ret]
duk_pop(ctx); // [emit][this][response][imsg][dec]
duk_remove(ctx, -2); // [emit][this][response][dec]
ILibDuktape_HttpStream_AddHeaderDef(ctx, header);
}
if (duk_pcall_method(ctx, 2) != 0) { ILibDuktape_Process_UncaughtExceptionEx(ctx, "http.httpStream.onReceive->response(): "); }
duk_pop(ctx);

File diff suppressed because one or more lines are too long

View File

@@ -155,14 +155,14 @@ void ILibDuktape_readableStream_WriteData_OnData_ChainThread(void *chain, void *
}
stream->paused = 0;
if (data->Reserved == 0)
if(duk_stream_flags_isBuffer(data->Reserved))
{
duk_push_external_buffer(stream->ctx); // [ext]
duk_config_buffer(stream->ctx, -1, data->buffer, data->bufferLen);
}
ILibDuktape_EventEmitter_SetupEmit(stream->ctx, stream->object, "data"); // [ext][emit][this][data]
if (data->Reserved == 0)
if (duk_stream_flags_isBuffer(data->Reserved))
{
duk_push_buffer_object(stream->ctx, -4, 0, data->bufferLen, DUK_BUFOBJ_NODEJS_BUFFER); // [ext][emit][this][data][buffer]
}
@@ -174,8 +174,8 @@ void ILibDuktape_readableStream_WriteData_OnData_ChainThread(void *chain, void *
{
ILibDuktape_Process_UncaughtException(stream->ctx);
}
if (data->Reserved == 0)
{
if (duk_stream_flags_isBuffer(data->Reserved))
{
duk_pop_2(stream->ctx); // ...
}
else

View File

@@ -120,7 +120,7 @@ ILibTransport_DoneState ILibDuktape_WebRTC_DataChannel_Stream_WriteSink(ILibDukt
ILibDuktape_WebRTC_DataChannel *ptrs = (ILibDuktape_WebRTC_DataChannel*)user;
if (ptrs->dataChannel != NULL)
{
if (stream->writableStream->Reserved == 0 && ptrs->maxFragmentSize > 0 && bufferLen > ptrs->maxFragmentSize)
if (duk_stream_flags_isBuffer(stream->writableStream->Reserved) && ptrs->maxFragmentSize > 0 && bufferLen > ptrs->maxFragmentSize)
{
// We need to fragment the send
ILibTransport_DoneState ret = ILibTransport_DoneState_ERROR;
@@ -135,7 +135,7 @@ ILibTransport_DoneState ILibDuktape_WebRTC_DataChannel_Stream_WriteSink(ILibDukt
else
{
// Normal Send
return(ILibWrapper_WebRTC_DataChannel_SendEx(ptrs->dataChannel, buffer, bufferLen, stream->writableStream->Reserved == 1 ? 51 : 53));
return(ILibWrapper_WebRTC_DataChannel_SendEx(ptrs->dataChannel, buffer, bufferLen, duk_stream_flags_isString(stream->writableStream->Reserved) ? 51 : 53));
}
}
else

View File

@@ -169,7 +169,7 @@ function zippedObject(table)
}
else
{
ret = require('compressed-stream').createDecompressor(1);
ret = require('compressed-stream').createDecompressor({ WBITS: -15 });
}
ret._info = info;
ret._readSink = function _readSink(err, bytesRead, buffer)