Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for listening on unix sockets #43

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 21 additions & 5 deletions src/h2get.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
#include <sys/socket.h>
#include <sys/types.h>
#include <unistd.h>
#include <sys/un.h>


#include <openssl/ssl.h>

Expand Down Expand Up @@ -304,8 +306,6 @@ int h2get_connect(struct h2get_ctx *ctx, struct h2get_conn *conn, struct h2get_b
xprt = H2GET_TRANSPORT_PLAIN;
} else if (!h2get_buf_cmp(&url.raw.scheme, &H2GET_BUFSTR("https"))) {
xprt = H2GET_TRANSPORT_SSL;
} else if (!h2get_buf_cmp(&url.raw.scheme, &H2GET_BUFSTR("unix"))) {
xprt = H2GET_TRANSPORT_UNIX;
} else {
*err = "Unknown URL scheme";
return -1;
Expand Down Expand Up @@ -387,6 +387,8 @@ int h2get_connect(struct h2get_ctx *ctx, struct h2get_conn *conn, struct h2get_b

static int parse_url(struct h2get_buf url_buf, struct h2get_url *url, enum h2get_transport *xprt, struct h2get_conn *conn, const char **err)
{
bool uds = false;

*url = h2get_buf_parse_url(url_buf);
if (url->parsed.parse_err) {
*err = url->parsed.parse_err;
Expand All @@ -400,8 +402,12 @@ static int parse_url(struct h2get_buf url_buf, struct h2get_url *url, enum h2get
*xprt = H2GET_TRANSPORT_PLAIN;
} else if (!h2get_buf_cmp(&url->raw.scheme, &H2GET_BUFSTR("https"))) {
*xprt = H2GET_TRANSPORT_SSL;
} else if (!h2get_buf_cmp(&url->raw.scheme, &H2GET_BUFSTR("unix"))) {
*xprt = H2GET_TRANSPORT_UNIX;
} else if (!h2get_buf_cmp(&url->raw.scheme, &H2GET_BUFSTR("http+unix"))) {
*xprt = H2GET_TRANSPORT_PLAIN;
uds = true;
} else if (!h2get_buf_cmp(&url->raw.scheme, &H2GET_BUFSTR("https+unix"))) {
*xprt = H2GET_TRANSPORT_SSL;
uds = true;
} else {
*err = "Unknown URL scheme";
return -1;
Expand All @@ -415,6 +421,16 @@ static int parse_url(struct h2get_buf url_buf, struct h2get_url *url, enum h2get
default_port = "80";
}
}
if (uds) {
conn->protocol = SOCK_STREAM;
conn->socktype = AF_UNIX;
conn->sa.sa = (void *)&conn->sa.sa_storage;
struct sockaddr_un *sun = (void *)conn->sa.sa;
sprintf(sun->sun_path, "%.*s", (int)url->raw.host.len, url->raw.host.buf);
sun->sun_family = AF_UNIX;
conn->sa.len = sizeof(*sun);
return 0;
}

if (*xprt == H2GET_TRANSPORT_PLAIN || *xprt == H2GET_TRANSPORT_SSL) {
struct addrinfo hints;
Expand Down Expand Up @@ -445,7 +461,7 @@ static int parse_url(struct h2get_buf url_buf, struct h2get_url *url, enum h2get
break;
}
if (!rp) {
*err = "Connection failed";
*err = "Socket creation failed";
return -1;
}
conn->protocol = rp->ai_protocol;
Expand Down
1 change: 0 additions & 1 deletion src/h2get.h
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,6 @@ struct h2get_conn {
};

enum h2get_transport {
H2GET_TRANSPORT_UNIX,
H2GET_TRANSPORT_PLAIN,
H2GET_TRANSPORT_SSL,
};
Expand Down
2 changes: 1 addition & 1 deletion src/h2get_read.c
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ static void h2get_frame_render_settings(struct h2get_conn *conn, struct h2get_bu
default:
/* An endpoint that receives a SETTINGS frame with any unknown or
* unsupported identifier MUST ignore that setting. */
h2get_buf_printf(out, "\n\tunknown frame type: %u", ntohs(settings[i].id));
h2get_buf_printf(out, "\n\tunknown settings type: %u=%u", htons(settings[i].id), v);
break;
}
}
Expand Down