Skip to content

Commit

Permalink
More url work. Undocument rawurl, and stop using it stats and logs.
Browse files Browse the repository at this point in the history
Also expose nng_url_sprintf() for users who need it.

This avoids some need to do dynamic memory on some things.  Soon
the entirety of nng_url will be allocation free in the usual case.
  • Loading branch information
gdamore committed Nov 18, 2024
1 parent 6333c9c commit e54e2b1
Show file tree
Hide file tree
Showing 8 changed files with 39 additions and 32 deletions.
21 changes: 19 additions & 2 deletions docs/ref/api/url.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ that are not part of the IETF standards.

```c
typedef struct nng_url {
char *u_rawurl;
const char *u_scheme;
char *u_userinfo;
char *u_host;
Expand All @@ -32,7 +31,6 @@ alter them, as the underlying memory is managed by the library.

The fields of an `nng_url` object are as follows:

- `u_rawurl`: The unparsed URL string. This will never be `NULL`.
- `u_scheme`: The URL scheme, such as "http" or "inproc". Always lower case. This will never be `NULL`.
- `u_userinfo`: This username and password if supplied in the URL string. Will be `NULL` when not present.
- `u_host`: The full host part of the URL, including the port if present (separated by a colon.)
Expand All @@ -46,6 +44,25 @@ The fields of an `nng_url` object are as follows:
> [!NOTE]
> Other fields may also be present, but only those documented here are safe for application use.
## Format a URL

```c
int nng_url_sprintf(char *buf, size_t bufsz, const nng_url *url);
```
The {{i:`nng_url_sprintf`}} function formats the _url_ to the _buf_,
which must have `bufsz` bytes of free space associated with it.
This function returns the number of bytes formatted to _buf_, excludng
the terminating zero byte, or if _bufsz_ is too small, then it returns
the number of bytes that would have been formatted if there was sufficient
space. The semantics are similar to the `snprintf` function from C99.
> [!TIP]
> If _bufsz_ is 0, then _buf_ can be `NULL`, and the return value
> can be used to determine the amount of space to allocate for a dynamically
> sized buffer.
## Parse a URL
```c
Expand Down
1 change: 1 addition & 0 deletions docs/ref/xref.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
[`nng_url`]: /api/url.md#url-structure
[`nng_url_parse`]: /api/url.md#parse-a-url
[`nng_url_free`]: /api/url.md#destroy-a-url
[`nng_url_sprintf`]: /api/url.md#format-a-url
[`nng_socket_pair`]: /api/misc.md#create-socket-pair
[`nng_random`]: /api/misc.md#get-random-number
[`nng_version`]: /api/misc.md#report-library-version
Expand Down
4 changes: 4 additions & 0 deletions include/nng/nng.h
Original file line number Diff line number Diff line change
Expand Up @@ -1108,6 +1108,10 @@ NNG_DECL void nng_url_free(nng_url *);
// nng_url_clone clones a URL structure.
NNG_DECL int nng_url_clone(nng_url **, const nng_url *);

// nng_url_sprintf prints a URL to a string using semantics similar to
// snprintf.
NNG_DECL int nng_url_sprintf(char *, size_t, const nng_url *);

// nng_version returns the library version as a human readable string.
NNG_DECL const char *nng_version(void);

Expand Down
15 changes: 3 additions & 12 deletions src/core/dialer.c
Original file line number Diff line number Diff line change
Expand Up @@ -72,12 +72,6 @@ dialer_stats_init(nni_dialer *d)
.si_desc = "socket for dialer",
.si_type = NNG_STAT_ID,
};
static const nni_stat_info url_info = {
.si_name = "url",
.si_desc = "dialer url",
.si_type = NNG_STAT_STRING,
.si_alloc = true,
};
static const nni_stat_info pipes_info = {
.si_name = "pipes",
.si_desc = "open pipes",
Expand Down Expand Up @@ -149,7 +143,6 @@ dialer_stats_init(nni_dialer *d)

dialer_stat_init(d, &d->st_id, &id_info);
dialer_stat_init(d, &d->st_sock, &socket_info);
dialer_stat_init(d, &d->st_url, &url_info);
dialer_stat_init(d, &d->st_pipes, &pipes_info);
dialer_stat_init(d, &d->st_connect, &connect_info);
dialer_stat_init(d, &d->st_refused, &refused_info);
Expand All @@ -165,7 +158,6 @@ dialer_stats_init(nni_dialer *d)
nni_stat_set_id(&d->st_root, (int) d->d_id);
nni_stat_set_id(&d->st_id, (int) d->d_id);
nni_stat_set_id(&d->st_sock, (int) nni_sock_id(d->d_sock));
nni_stat_set_string(&d->st_url, d->d_url->u_rawurl);
nni_stat_register(&d->st_root);
}
#endif // NNG_ENABLE_STATS
Expand Down Expand Up @@ -384,8 +376,7 @@ dialer_connect_cb(void *arg)
case NNG_ETIMEDOUT:
default:
nng_log_warn("NNG-CONN-FAIL",
"Failed connecting socket<%u> to %s: %s",
nni_sock_id(d->d_sock), d->d_url->u_rawurl,
"Failed connecting socket<%u>: %s", nni_sock_id(d->d_sock),
nng_strerror(rv));

nni_dialer_bump_error(d, rv);
Expand Down Expand Up @@ -438,8 +429,8 @@ nni_dialer_start(nni_dialer *d, unsigned flags)
nni_aio_free(aio);
}

nng_log_info("NNG-DIAL", "Starting dialer for socket<%u> on %s",
nni_sock_id(d->d_sock), d->d_url->u_rawurl);
nng_log_info("NNG-DIAL", "Starting dialer for socket<%u>",
nni_sock_id(d->d_sock));

return (rv);
}
Expand Down
19 changes: 4 additions & 15 deletions src/core/listener.c
Original file line number Diff line number Diff line change
Expand Up @@ -74,12 +74,6 @@ listener_stats_init(nni_listener *l)
.si_desc = "socket id",
.si_type = NNG_STAT_ID,
};
static const nni_stat_info url_info = {
.si_name = "url",
.si_desc = "listener url",
.si_type = NNG_STAT_STRING,
.si_alloc = true,
};
static const nni_stat_info pipes_info = {
.si_name = "pipes",
.si_desc = "open pipes",
Expand Down Expand Up @@ -145,7 +139,6 @@ listener_stats_init(nni_listener *l)

listener_stat_init(l, &l->st_id, &id_info);
listener_stat_init(l, &l->st_sock, &sock_info);
listener_stat_init(l, &l->st_url, &url_info);
listener_stat_init(l, &l->st_pipes, &pipes_info);
listener_stat_init(l, &l->st_accept, &accept_info);
listener_stat_init(l, &l->st_disconnect, &disconnect_info);
Expand All @@ -160,7 +153,6 @@ listener_stats_init(nni_listener *l)
nni_stat_set_id(&l->st_root, (int) l->l_id);
nni_stat_set_id(&l->st_id, (int) l->l_id);
nni_stat_set_id(&l->st_sock, (int) nni_sock_id(l->l_sock));
nni_stat_set_string(&l->st_url, l->l_url->u_rawurl);
nni_stat_register(&l->st_root);
}
#endif // NNG_ENABLE_STATS
Expand Down Expand Up @@ -362,9 +354,8 @@ listener_accept_cb(void *arg)
case NNG_ETIMEDOUT: // No need to sleep, we timed out already.
case NNG_EPEERAUTH: // peer validation failure
nng_log_warn("NNG-ACCEPT-FAIL",
"Failed accepting for socket<%u> on %s: %s",
nni_sock_id(l->l_sock), l->l_url->u_rawurl,
nng_strerror(rv));
"Failed accepting for socket<%u>: %s",
nni_sock_id(l->l_sock), nng_strerror(rv));
nni_listener_bump_error(l, rv);
listener_accept_start(l);
break;
Expand Down Expand Up @@ -404,10 +395,8 @@ nni_listener_start(nni_listener *l, int flags)
}

if ((rv = l->l_ops.l_bind(l->l_data)) != 0) {
nng_log_warn("NNG-BIND-FAIL",
"Failed binding socket<%u> to %s: %s",
nni_sock_id(l->l_sock), l->l_url->u_rawurl,
nng_strerror(rv));
nng_log_warn("NNG-BIND-FAIL", "Failed binding socket<%u>: %s",
nni_sock_id(l->l_sock), nng_strerror(rv));
nni_listener_bump_error(l, rv);
nni_atomic_flag_reset(&l->l_started);
return (rv);
Expand Down
2 changes: 0 additions & 2 deletions src/core/sockimpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ struct nni_dialer {
nni_stat_item st_root;
nni_stat_item st_id;
nni_stat_item st_sock;
nni_stat_item st_url;
nni_stat_item st_pipes;
nni_stat_item st_connect;
nni_stat_item st_refused;
Expand Down Expand Up @@ -78,7 +77,6 @@ struct nni_listener {
nni_stat_item st_root;
nni_stat_item st_id;
nni_stat_item st_sock;
nni_stat_item st_url;
nni_stat_item st_pipes;
nni_stat_item st_accept;
nni_stat_item st_disconnect; // aborted remotely
Expand Down
3 changes: 2 additions & 1 deletion src/core/url.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//
// Copyright 2020 Staysail Systems, Inc. <[email protected]>
// Copyright 2024 Staysail Systems, Inc. <[email protected]>
// Copyright 2018 Capitar IT Group BV <[email protected]>
//
// This software is supplied under the terms of the MIT License, a
Expand All @@ -17,6 +17,7 @@ extern int nni_url_parse(nni_url **, const char *path);
extern void nni_url_free(nni_url *);
extern int nni_url_clone(nni_url **, const nni_url *);
extern uint16_t nni_url_default_port(const char *);
extern int nni_url_sprintf(char *, size_t, const nni_url *);
extern int nni_url_asprintf(char **, const nni_url *);
extern int nni_url_asprintf_port(char **, const nni_url *, int);
extern size_t nni_url_decode(uint8_t *, const char *, size_t);
Expand Down
6 changes: 6 additions & 0 deletions src/nng.c
Original file line number Diff line number Diff line change
Expand Up @@ -2038,6 +2038,12 @@ nng_url_clone(nng_url **dstp, const nng_url *src)
return (nni_url_clone(dstp, src));
}

int
nng_url_sprintf(char *buf, size_t bufsz, const nng_url *src)
{
return (nni_url_sprintf(buf, bufsz, src));
}

#define xstr(a) str(a)
#define str(a) #a

Expand Down

0 comments on commit e54e2b1

Please sign in to comment.