Skip to content

Commit

Permalink
improve URL handling logic in fio_srv_listen + docs
Browse files Browse the repository at this point in the history
  • Loading branch information
boazsegev committed May 17, 2024
1 parent 9af5eee commit 1ea05d6
Show file tree
Hide file tree
Showing 6 changed files with 85 additions and 63 deletions.
46 changes: 23 additions & 23 deletions examples/server.c
Original file line number Diff line number Diff line change
Expand Up @@ -200,29 +200,29 @@ int main(int argc, char const *argv[]) {
if (!fio_cli_get("-b"))
fio_cli_set(fio_cli_get("-b"), fio_cli_unnamed(0));
if (fio_cli_get("-p")) {
fio_buf_info_s tmp;
FIO_STR_INFO_TMP_VAR(url, 2048);
tmp.buf = (char *)fio_cli_get("-b");
if (!tmp.buf)
tmp.buf = (char *)"0.0.0.0";
tmp.len = strlen(tmp.buf);
FIO_ASSERT(tmp.len < 2000, "binding address / url too long.");
fio_url_s u = fio_url_parse(tmp.buf, tmp.len);
tmp.buf = (char *)fio_cli_get("-p");
tmp.len = strlen(tmp.buf);
FIO_ASSERT(tmp.len < 6, "port number too long.");
fio_string_write2(&url,
NULL,
FIO_STRING_WRITE_STR2(u.scheme.buf, u.scheme.len),
(u.scheme.len ? FIO_STRING_WRITE_STR2("://", 3)
: FIO_STRING_WRITE_STR2(NULL, 0)),
FIO_STRING_WRITE_STR2(u.host.buf, u.host.len),
FIO_STRING_WRITE_STR2(":", 1),
FIO_STRING_WRITE_STR2(tmp.buf, tmp.len),
(u.query.len ? FIO_STRING_WRITE_STR2("?", 1)
: FIO_STRING_WRITE_STR2(NULL, 0)),
FIO_STRING_WRITE_STR2(u.query.buf, u.query.len));
fio_cli_set_unnamed(0, url.buf);
fio_buf_info_s tmp = fio_cli_get_str("-b");
if (tmp.buf) {
FIO_STR_INFO_TMP_VAR(url, 2048);
FIO_ASSERT(tmp.len < 2000, "binding address / url too long.");
fio_url_s u = fio_url_parse(tmp.buf, tmp.len);
tmp.buf = (char *)fio_cli_get("-p");
tmp.len = strlen(tmp.buf);
FIO_ASSERT(tmp.len < 6, "port number too long.");
fio_string_write2(&url,
NULL,
FIO_STRING_WRITE_STR2(u.scheme.buf, u.scheme.len),
(u.scheme.len ? FIO_STRING_WRITE_STR2("://", 3)
: FIO_STRING_WRITE_STR2(NULL, 0)),
FIO_STRING_WRITE_STR2(u.host.buf, u.host.len),
FIO_STRING_WRITE_STR2(":", 1),
FIO_STRING_WRITE_STR2(tmp.buf, tmp.len),
(u.query.len ? FIO_STRING_WRITE_STR2("?", 1)
: FIO_STRING_WRITE_STR2(NULL, 0)),
FIO_STRING_WRITE_STR2(u.query.buf, u.query.len));
fio_cli_set("-b", url.buf);
} else {
setenv("PORT", fio_cli_get("-p"), 1);
}
}

/* listen to incoming HTTP connections */
Expand Down
49 changes: 30 additions & 19 deletions fio-stl.h
Original file line number Diff line number Diff line change
Expand Up @@ -28503,7 +28503,7 @@ struct fio_protocol_s {
*/
fio_io_functions_s io_functions;
/**
* The timeout value in seconds for all connections using this protocol.
* The timeout value in milliseconds for all connections using this protocol.
*
* Limited to FIO_SRV_TIMEOUT_MAX seconds. Zero (0) == FIO_SRV_TIMEOUT_MAX
*/
Expand Down Expand Up @@ -30399,7 +30399,7 @@ SFUNC void *fio_srv_listen FIO_NOOP(struct fio_srv_listen_args args) {
fio___srv_listen_s *l = NULL;
void *built_tls = NULL;
int should_free_tls = !args.tls;
FIO_STR_INFO_TMP_VAR(url_alt, 64);
FIO_STR_INFO_TMP_VAR(url_alt, 2048);
if (!args.protocol) {
FIO_LOG_ERROR("fio_srv_listen requires a protocol to be assigned.");
return l;
Expand All @@ -30408,9 +30408,24 @@ SFUNC void *fio_srv_listen FIO_NOOP(struct fio_srv_listen_args args) {
FIO_LOG_ERROR("fio_srv_listen called with `on_root` by a non-root worker.");
return l;
}

if (!args.url ||
args.url[0] == '?') { /* if no URL is given use 0.0.0.0:3000 as default */
if (!args.url) {
args.url = getenv("ADDRESS");
if (!args.url)
args.url = "0.0.0.0";
}
url_alt.len = strlen(args.url);
if (url_alt.len > 2024) {
FIO_LOG_ERROR("binding address / url too long.");
args.url = NULL;
}
fio_url_s url = fio_url_parse(args.url, url_alt.len);
if (url.scheme.buf &&
(url.scheme.len > 2 && url.scheme.len < 5 &&
(url.scheme.buf[0] | (char)0x20) == 't' &&
(url.scheme.buf[1] | (char)0x20) == 'c') &&
(url.scheme.buf[2] | (char)0x20) == 'p')
url.scheme = FIO_BUF_INFO0;
if (!url.port.buf && !url.scheme.buf) {
static size_t port_counter = 3000;
size_t port = fio_atomic_add(&port_counter, 1);
if (port == 3000 && getenv("PORT")) {
Expand All @@ -30419,23 +30434,19 @@ SFUNC void *fio_srv_listen FIO_NOOP(struct fio_srv_listen_args args) {
if (!port | (port > 65535ULL))
port = 3000;
}
fio_buf_info_s root_addr = FIO_BUF_INFO1((char *)"0.0.0.0");
if (getenv("ADDRESS")) {
fio_buf_info_s tmp = FIO_BUF_INFO1((char *)getenv("ADDRESS"));
if (tmp.len < 56)
root_addr = tmp;
}
url_alt.len = 0;
fio_string_write2(&url_alt,
NULL,
FIO_STRING_WRITE_STR2(root_addr.buf, root_addr.len),
FIO_STRING_WRITE_STR2(url.scheme.buf, url.scheme.len),
(url.scheme.len ? FIO_STRING_WRITE_STR2("://", 3)
: FIO_STRING_WRITE_STR2(NULL, 0)),
FIO_STRING_WRITE_STR2(url.host.buf, url.host.len),
FIO_STRING_WRITE_STR2(":", 1),
FIO_STRING_WRITE_NUM(port));
if (args.url)
fio_string_write(&url_alt, NULL, args.url, strlen(args.url));
args.url = url_alt.buf;
} else
url_alt.len = strlen(args.url);
fio_url_s url = fio_url_parse(args.url, url_alt.len);
url = fio_url_parse(args.url, url_alt.len);
}

args.tls = fio_tls_from_url(args.tls, url);
fio___srv_init_protocol_test(args.protocol, !!args.tls);
built_tls = args.protocol->io_functions.build_context(args.tls, 0);
Expand Down Expand Up @@ -34330,7 +34341,7 @@ SFUNC void fio_http_write(fio_http_s *, fio_http_write_args_s args);
fio_http_write(http_handle, (fio_http_write_args_s){__VA_ARGS__})
#define fio_http_finish(http_handle) fio_http_write(http_handle, .finish = 1)

/** Closes a persistent HTTP connection (i.s., if upgraded). */
/** Closes a persistent HTTP connection (i.e., if upgraded). */
SFUNC void fio_http_close(fio_http_s *h);

/* *****************************************************************************
Expand Down Expand Up @@ -35166,7 +35177,7 @@ SFUNC void fio_http_start_time_set(fio_http_s *h) {
h->received_at = fio_http_get_timestump();
}

/** Closes a persistent HTTP connection (i.s., if upgraded). */
/** Closes a persistent HTTP connection (i.e., if upgraded). */
SFUNC void fio_http_close(fio_http_s *h) { h->controller->close(h); }

/** Creates a copy of an existing handle, copying only its request data. */
Expand Down
2 changes: 1 addition & 1 deletion fio-stl.md
Original file line number Diff line number Diff line change
Expand Up @@ -8692,7 +8692,7 @@ struct fio_protocol_s {
void (*free)(void *tls);
} io_functions;
/**
* The timeout value in seconds for all connections using this protocol.
* The timeout value in milliseconds for all connections using this protocol.
*
* Limited to FIO_SRV_TIMEOUT_MAX seconds.
*
Expand Down
45 changes: 28 additions & 17 deletions fio-stl/400 server.h
Original file line number Diff line number Diff line change
Expand Up @@ -470,7 +470,7 @@ struct fio_protocol_s {
*/
fio_io_functions_s io_functions;
/**
* The timeout value in seconds for all connections using this protocol.
* The timeout value in milliseconds for all connections using this protocol.
*
* Limited to FIO_SRV_TIMEOUT_MAX seconds. Zero (0) == FIO_SRV_TIMEOUT_MAX
*/
Expand Down Expand Up @@ -2366,7 +2366,7 @@ SFUNC void *fio_srv_listen FIO_NOOP(struct fio_srv_listen_args args) {
fio___srv_listen_s *l = NULL;
void *built_tls = NULL;
int should_free_tls = !args.tls;
FIO_STR_INFO_TMP_VAR(url_alt, 64);
FIO_STR_INFO_TMP_VAR(url_alt, 2048);
if (!args.protocol) {
FIO_LOG_ERROR("fio_srv_listen requires a protocol to be assigned.");
return l;
Expand All @@ -2375,9 +2375,24 @@ SFUNC void *fio_srv_listen FIO_NOOP(struct fio_srv_listen_args args) {
FIO_LOG_ERROR("fio_srv_listen called with `on_root` by a non-root worker.");
return l;
}

if (!args.url ||
args.url[0] == '?') { /* if no URL is given use 0.0.0.0:3000 as default */
if (!args.url) {
args.url = getenv("ADDRESS");
if (!args.url)
args.url = "0.0.0.0";
}
url_alt.len = strlen(args.url);
if (url_alt.len > 2024) {
FIO_LOG_ERROR("binding address / url too long.");
args.url = NULL;
}
fio_url_s url = fio_url_parse(args.url, url_alt.len);
if (url.scheme.buf &&
(url.scheme.len > 2 && url.scheme.len < 5 &&
(url.scheme.buf[0] | (char)0x20) == 't' &&
(url.scheme.buf[1] | (char)0x20) == 'c') &&
(url.scheme.buf[2] | (char)0x20) == 'p')
url.scheme = FIO_BUF_INFO0;
if (!url.port.buf && !url.scheme.buf) {
static size_t port_counter = 3000;
size_t port = fio_atomic_add(&port_counter, 1);
if (port == 3000 && getenv("PORT")) {
Expand All @@ -2386,23 +2401,19 @@ SFUNC void *fio_srv_listen FIO_NOOP(struct fio_srv_listen_args args) {
if (!port | (port > 65535ULL))
port = 3000;
}
fio_buf_info_s root_addr = FIO_BUF_INFO1((char *)"0.0.0.0");
if (getenv("ADDRESS")) {
fio_buf_info_s tmp = FIO_BUF_INFO1((char *)getenv("ADDRESS"));
if (tmp.len < 56)
root_addr = tmp;
}
url_alt.len = 0;
fio_string_write2(&url_alt,
NULL,
FIO_STRING_WRITE_STR2(root_addr.buf, root_addr.len),
FIO_STRING_WRITE_STR2(url.scheme.buf, url.scheme.len),
(url.scheme.len ? FIO_STRING_WRITE_STR2("://", 3)
: FIO_STRING_WRITE_STR2(NULL, 0)),
FIO_STRING_WRITE_STR2(url.host.buf, url.host.len),
FIO_STRING_WRITE_STR2(":", 1),
FIO_STRING_WRITE_NUM(port));
if (args.url)
fio_string_write(&url_alt, NULL, args.url, strlen(args.url));
args.url = url_alt.buf;
} else
url_alt.len = strlen(args.url);
fio_url_s url = fio_url_parse(args.url, url_alt.len);
url = fio_url_parse(args.url, url_alt.len);
}

args.tls = fio_tls_from_url(args.tls, url);
fio___srv_init_protocol_test(args.protocol, !!args.tls);
built_tls = args.protocol->io_functions.build_context(args.tls, 0);
Expand Down
2 changes: 1 addition & 1 deletion fio-stl/400 server.md
Original file line number Diff line number Diff line change
Expand Up @@ -512,7 +512,7 @@ struct fio_protocol_s {
void (*free)(void *tls);
} io_functions;
/**
* The timeout value in seconds for all connections using this protocol.
* The timeout value in milliseconds for all connections using this protocol.
*
* Limited to FIO_SRV_TIMEOUT_MAX seconds.
*
Expand Down
4 changes: 2 additions & 2 deletions fio-stl/431 http handle.h
Original file line number Diff line number Diff line change
Expand Up @@ -473,7 +473,7 @@ SFUNC void fio_http_write(fio_http_s *, fio_http_write_args_s args);
fio_http_write(http_handle, (fio_http_write_args_s){__VA_ARGS__})
#define fio_http_finish(http_handle) fio_http_write(http_handle, .finish = 1)

/** Closes a persistent HTTP connection (i.s., if upgraded). */
/** Closes a persistent HTTP connection (i.e., if upgraded). */
SFUNC void fio_http_close(fio_http_s *h);

/* *****************************************************************************
Expand Down Expand Up @@ -1309,7 +1309,7 @@ SFUNC void fio_http_start_time_set(fio_http_s *h) {
h->received_at = fio_http_get_timestump();
}

/** Closes a persistent HTTP connection (i.s., if upgraded). */
/** Closes a persistent HTTP connection (i.e., if upgraded). */
SFUNC void fio_http_close(fio_http_s *h) { h->controller->close(h); }

/** Creates a copy of an existing handle, copying only its request data. */
Expand Down

0 comments on commit 1ea05d6

Please sign in to comment.