Skip to content

Commit

Permalink
refactor(*) generalize bytes readers for all ngx_wa
Browse files Browse the repository at this point in the history
These will be used in IPC code.
  • Loading branch information
thibaultcha committed Oct 3, 2024
1 parent 80da044 commit a365850
Show file tree
Hide file tree
Showing 6 changed files with 130 additions and 112 deletions.
2 changes: 2 additions & 0 deletions config
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@ NGX_WASMX_INCS="\

NGX_WASMX_DEPS="\
$ngx_addon_dir/src/ngx_wasmx.h \
$ngx_addon_dir/src/common/ngx_wa_readers.h \
$ngx_addon_dir/src/common/ngx_wasm_subsystem.h \
$ngx_addon_dir/src/common/ngx_wasm_socket_tcp.h \
$ngx_addon_dir/src/common/ngx_wasm_socket_tcp_readers.h \
Expand All @@ -147,6 +148,7 @@ NGX_WASMX_DEPS="\

NGX_WASMX_SRCS="\
$ngx_addon_dir/src/ngx_wasmx.c \
$ngx_addon_dir/src/common/ngx_wa_readers.c \
$ngx_addon_dir/src/common/ngx_wasm_subsystem.c \
$ngx_addon_dir/src/common/ngx_wasm_socket_tcp.c \
$ngx_addon_dir/src/common/ngx_wasm_socket_tcp_readers.c \
Expand Down
5 changes: 5 additions & 0 deletions src/common/debug/ngx_wasm_debug_module.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include "ddebug.h"

#include <ngx_wasm.h>
#include <ngx_wa_readers.h>
#include <ngx_wa_metrics.h>
#include <ngx_wasm_subsystem.h>
#ifdef NGX_HTTP_WASM
Expand Down Expand Up @@ -39,6 +40,10 @@ ngx_wasm_debug_init(ngx_cycle_t *cycle)

/* coverage */

ngx_wa_assert(
ngx_wa_read_bytes(NULL, NULL, 0, NULL) == NGX_ERROR
);

/**
* ngx_wasm_phase_lookup NULL return branch is unreacheable in
* normal ngx_wasmx_module usage.
Expand Down
104 changes: 104 additions & 0 deletions src/common/ngx_wa_readers.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
#ifndef DDEBUG
#define DDEBUG 0
#endif
#include "ddebug.h"

#include <ngx_wa_readers.h>


#if 0
static void
ngx_wa_read_log(ngx_buf_t *src, ngx_chain_t *buf_in, char *fmt)
{
size_t chunk_len;
ngx_buf_t *buf;

if (src) {
dd("src %s: %p: %.*s (pos: %p, last: %p)",
fmt, src, (int) ngx_buf_size(src), src->pos,
src->pos, src->last);
}

for (/* void */; buf_in; buf_in = buf_in->next) {
buf = buf_in->buf;
chunk_len = buf->last - buf->pos;

dd("buf_in %s: %p: %.*s", fmt, buf,
(int) chunk_len, buf->pos);
}
}


ngx_int_t
ngx_wa_read_all(ngx_buf_t *src, ngx_chain_t *buf_in, ssize_t bytes)
{
if (bytes == 0) {
return NGX_OK;
}

buf_in->buf->last += bytes;
src->pos += bytes;

return NGX_AGAIN;
}


ngx_int_t
ngx_wa_read_line(ngx_buf_t *src, ngx_chain_t *buf_in, ssize_t bytes)
{
u_char *dst;
u_char c;

if (bytes == 0) {
return NGX_ERROR;
}

dst = buf_in->buf->last;

while (bytes--) {
c = *src->pos++;

switch (c) {
case LF:
buf_in->buf->last = dst;

return NGX_OK;

case CR:
break;

default:
*dst++ = c;
break;
}
}

buf_in->buf->last = dst;

return NGX_AGAIN;
}
#endif


ngx_int_t
ngx_wa_read_bytes(ngx_buf_t *src, ngx_chain_t *buf_in, ssize_t bytes,
size_t *rest)
{
if (bytes == 0) {
return NGX_ERROR;
}

if ((size_t) bytes >= *rest) {
src->pos += *rest;
buf_in->buf->last = src->pos;
*rest = 0;

return NGX_OK;
}

buf_in->buf->last += bytes;
src->pos += bytes;
*rest -= bytes;

return NGX_AGAIN;
}
16 changes: 16 additions & 0 deletions src/common/ngx_wa_readers.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#ifndef _NGX_WA_READERS_H_INCLUDED_
#define _NGX_WA_READERS_H_INCLUDED_


#include <ngx_core.h>


#if 0
ngx_int_t ngx_wa_read_all(ngx_buf_t *src, ngx_chain_t *buf_in, ssize_t bytes);
ngx_int_t ngx_wa_read_line(ngx_buf_t *src, ngx_chain_t *buf_in, ssize_t bytes);
#endif
ngx_int_t ngx_wa_read_bytes(ngx_buf_t *src, ngx_chain_t *buf_in,
ssize_t bytes, size_t *rest);


#endif /* _NGX_WA_READERS_H_INCLUDED_ */
105 changes: 3 additions & 102 deletions src/common/ngx_wasm_socket_tcp_readers.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,110 +3,11 @@
#endif
#include "ddebug.h"

#include <ngx_wa_readers.h>
#include <ngx_wasm_socket_tcp.h>
#include <ngx_wasm_socket_tcp_readers.h>


#if 0
static void
ngx_wasm_read_log(ngx_buf_t *src, ngx_chain_t *buf_in, char *fmt)
{
size_t chunk_len;
ngx_buf_t *buf;

if (src) {
dd("src %s: %p: %.*s (pos: %p, last: %p)",
fmt, src, (int) ngx_buf_size(src), src->pos,
src->pos, src->last);
}

for (/* void */; buf_in; buf_in = buf_in->next) {
buf = buf_in->buf;
chunk_len = buf->last - buf->pos;

dd("buf_in %s: %p: %.*s", fmt, buf,
(int) chunk_len, buf->pos);
}
}
#endif


#if 0
ngx_int_t
ngx_wasm_read_all(ngx_buf_t *src, ngx_chain_t *buf_in, ssize_t bytes)
{
if (bytes == 0) {
return NGX_OK;
}

buf_in->buf->last += bytes;
src->pos += bytes;

return NGX_AGAIN;
}


ngx_int_t
ngx_wasm_read_line(ngx_buf_t *src, ngx_chain_t *buf_in, ssize_t bytes)
{
u_char *dst;
u_char c;

if (bytes == 0) {
return NGX_ERROR;
}

dst = buf_in->buf->last;

while (bytes--) {
c = *src->pos++;

switch (c) {
case LF:
buf_in->buf->last = dst;

return NGX_OK;

case CR:
break;

default:
*dst++ = c;
break;
}
}

buf_in->buf->last = dst;

return NGX_AGAIN;
}
#endif


ngx_int_t
ngx_wasm_read_bytes(ngx_buf_t *src, ngx_chain_t *buf_in, ssize_t bytes,
size_t *rest)
{
if (bytes == 0) {
return NGX_ERROR;
}

if ((size_t) bytes >= *rest) {
src->pos += *rest;
buf_in->buf->last = src->pos;
*rest = 0;

return NGX_OK;
}

buf_in->buf->last += bytes;
src->pos += bytes;
*rest -= bytes;

return NGX_AGAIN;
}


#ifdef NGX_WASM_HTTP
static ngx_int_t
ngx_wasm_http_alloc_large_buffer(ngx_wasm_http_reader_ctx_t *in_ctx)
Expand Down Expand Up @@ -572,8 +473,8 @@ ngx_wasm_read_http_response(ngx_buf_t *src, ngx_chain_t *buf_in, ssize_t bytes,

bytes = ngx_buf_size(src);

rc = ngx_wasm_read_bytes(src, buf_in, bytes,
(size_t *) &in_ctx->rest);
rc = ngx_wa_read_bytes(src, buf_in, bytes,
(size_t *) &in_ctx->rest);

b->last = buf_in->buf->last;

Expand Down
10 changes: 0 additions & 10 deletions src/common/ngx_wasm_socket_tcp_readers.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,18 +26,8 @@ typedef struct {
size_t rest;
unsigned header_done:1;
} ngx_wasm_http_reader_ctx_t;
#endif


#if 0
ngx_int_t ngx_wasm_read_all(ngx_buf_t *src, ngx_chain_t *buf_in,
ssize_t bytes);
ngx_int_t ngx_wasm_read_line(ngx_buf_t *src, ngx_chain_t *buf_in,
ssize_t bytes);
#endif
ngx_int_t ngx_wasm_read_bytes(ngx_buf_t *src, ngx_chain_t *buf_in,
ssize_t bytes, size_t *rest);
#ifdef NGX_WASM_HTTP
ngx_int_t ngx_wasm_read_http_response(ngx_buf_t *src, ngx_chain_t *buf_in,
ssize_t bytes, ngx_wasm_http_reader_ctx_t *in_ctx);
#endif
Expand Down

0 comments on commit a365850

Please sign in to comment.