Skip to content

Commit

Permalink
feat(*) wasm_socket unix domain socket support
Browse files Browse the repository at this point in the history
  • Loading branch information
casimiro committed Aug 8, 2023
1 parent d2da5e4 commit a098efb
Show file tree
Hide file tree
Showing 5 changed files with 164 additions and 66 deletions.
37 changes: 34 additions & 3 deletions src/common/ngx_wasm_socket_tcp.c
Original file line number Diff line number Diff line change
Expand Up @@ -119,9 +119,10 @@ ngx_wasm_socket_tcp_resume(ngx_wasm_socket_tcp_t *sock)

ngx_int_t
ngx_wasm_socket_tcp_init(ngx_wasm_socket_tcp_t *sock,
ngx_str_t *host, in_port_t port, unsigned tls,
ngx_wasm_subsys_env_t *env)
ngx_str_t *host, unsigned tls, ngx_wasm_subsys_env_t *env)
{
u_char *p, *last;

ngx_memzero(sock, sizeof(ngx_wasm_socket_tcp_t));

ngx_memcpy(&sock->env, env, sizeof(ngx_wasm_subsys_env_t));
Expand Down Expand Up @@ -165,7 +166,37 @@ ngx_wasm_socket_tcp_init(ngx_wasm_socket_tcp_t *sock,
sock->url.default_port = 80;
#endif
sock->url.url = sock->host;
sock->url.port = port;
sock->url.port = 0;

if (ngx_str_eq(host->data, host->len, "unix:", -1)) {

#if (!NGX_HAVE_UNIX_DOMAIN)
ngx_wasm_log_error(NGX_LOG_ERR, sock->log, 0,
"no unix domain socket support");
return NGX_ERROR;
#endif

} else {
/* extract port */

p = host->data;
last = p + host->len;

if (*p == '[') {
/* IPv6 */
p = ngx_strlchr(p, last, ']');
if (p == NULL) {
p = host->data;
}
}

p = ngx_strlchr(p, last, ':');

if (p) {
sock->url.port = ngx_atoi(p + 1, last - p);
}
}

sock->url.no_resolve = 1;

if (ngx_parse_url(sock->pool, &sock->url) != NGX_OK) {
Expand Down
3 changes: 1 addition & 2 deletions src/common/ngx_wasm_socket_tcp.h
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,7 @@ struct ngx_wasm_socket_tcp_s {


ngx_int_t ngx_wasm_socket_tcp_init(ngx_wasm_socket_tcp_t *sock,
ngx_str_t *host, in_port_t port, unsigned tls,
ngx_wasm_subsys_env_t *env);
ngx_str_t *host, unsigned tls, ngx_wasm_subsys_env_t *env);
ngx_int_t ngx_wasm_socket_tcp_connect(ngx_wasm_socket_tcp_t *sock);
ngx_int_t ngx_wasm_socket_tcp_send(ngx_wasm_socket_tcp_t *sock,
ngx_chain_t *cl);
Expand Down
24 changes: 1 addition & 23 deletions src/http/proxy_wasm/ngx_http_proxy_wasm_dispatch.c
Original file line number Diff line number Diff line change
Expand Up @@ -136,8 +136,6 @@ ngx_http_proxy_wasm_dispatch(ngx_proxy_wasm_exec_t *pwexec,
{
static uint32_t callout_ids = 0;
size_t i;
in_port_t port = 0;
u_char *p, *last;
ngx_buf_t *buf;
ngx_event_t *ev;
ngx_table_elt_t *elts, *elt;
Expand Down Expand Up @@ -227,25 +225,6 @@ ngx_http_proxy_wasm_dispatch(ngx_proxy_wasm_exec_t *pwexec,
ngx_memcpy(call->host.data, host->data, host->len);
call->host.data[call->host.len] = '\0';

/* port */

p = host->data;
last = p + host->len;

if (*p == '[') {
/* IPv6 */
p = ngx_strlchr(p, last, ']');
if (p == NULL) {
p = host->data;
}
}

p = ngx_strlchr(p, last, ':');

if (p) {
port = ngx_atoi(p + 1, last - p);
}

/* headers/trailers */

if (ngx_proxy_wasm_pairs_unmarshal(pwexec, &call->headers, headers)
Expand Down Expand Up @@ -354,8 +333,7 @@ ngx_http_proxy_wasm_dispatch(ngx_proxy_wasm_exec_t *pwexec,

ngx_wasm_assert(rctx);

if (ngx_wasm_socket_tcp_init(sock, &call->host, port, enable_ssl,
&rctx->env)
if (ngx_wasm_socket_tcp_init(sock, &call->host, enable_ssl, &rctx->env)
!= NGX_OK)
{
dd("tcp init error");
Expand Down
Loading

0 comments on commit a098efb

Please sign in to comment.