Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
casimiro committed Aug 7, 2023
1 parent 7311cae commit 64b00da
Show file tree
Hide file tree
Showing 5 changed files with 135 additions and 107 deletions.
38 changes: 35 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,38 @@ 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;

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

#if (NGX_HAVE_UNIX_DOMAIN)
sock->url.port = 0;
#else
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
56 changes: 1 addition & 55 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 All @@ -150,9 +148,6 @@ ngx_http_proxy_wasm_dispatch(ngx_proxy_wasm_exec_t *pwexec,
ngx_http_wasm_req_ctx_t *rctxp = NULL;
ngx_http_proxy_wasm_dispatch_t *call = NULL;
unsigned enable_ssl = 0;
#if (NGX_HAVE_UNIX_DOMAIN)
static ngx_str_t uds_prefix = ngx_string("unix:");
#endif

/* rctx or fake request */

Expand Down Expand Up @@ -230,37 +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;
}
}

#if (NGX_HAVE_UNIX_DOMAIN)
if (host->len < uds_prefix.len ||
!ngx_str_eq(p, uds_prefix.len, uds_prefix.data, uds_prefix.len))
{
p = ngx_strlchr(p, last, ':');

if (p) {
port = ngx_atoi(p + 1, last - p);
}
}
#else
p = ngx_strlchr(p, last, ':');

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

/* headers/trailers */

if (ngx_proxy_wasm_pairs_unmarshal(pwexec, &call->headers, headers)
Expand Down Expand Up @@ -369,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 Expand Up @@ -621,24 +584,7 @@ ngx_http_proxy_wasm_dispatch_request(ngx_http_proxy_wasm_dispatch_t *call)

b->last = ngx_cpymem(b->last, ngx_http_proxy_wasm_host,
sizeof(ngx_http_proxy_wasm_host) - 1);

#if (NGX_HAVE_UNIX_DOMAIN)
static ngx_str_t uds = ngx_string("unix:");
static ngx_str_t localhost = ngx_string("localhost");

if (call->authority.len > uds.len
&& ngx_str_eq(call->authority.data, uds.len, uds.data, uds.len))
{
b->last = ngx_cpymem(b->last, localhost.data, localhost.len);

} else {
b->last = ngx_cpymem(b->last, call->authority.data,
call->authority.len);
}
#else
b->last = ngx_cpymem(b->last, call->authority.data, call->authority.len);
#endif

*b->last++ = CR;
*b->last++ = LF;

Expand Down
Loading

0 comments on commit 64b00da

Please sign in to comment.