Skip to content

intern URL scheme #1936

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

Merged
merged 2 commits into from
Nov 18, 2024
Merged
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
22 changes: 11 additions & 11 deletions docs/ref/api/url.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,16 @@ that are not part of the IETF standards.

```c
typedef struct nng_url {
char *u_rawurl;
char *u_scheme;
char *u_userinfo;
char *u_host;
char *u_hostname;
char *u_port;
char *u_path;
char *u_query;
char *u_fragment;
char *u_requri;
char *u_rawurl;
const char *u_scheme;
char *u_userinfo;
char *u_host;
char *u_hostname;
uint16_t u_port;
char *u_path;
char *u_query;
char *u_fragment;
char *u_requri;
} nng_url;
```

Expand All @@ -37,7 +37,7 @@ The fields of an `nng_url` object are as follows:
- `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.)
- `u_hostname`: The name of the host, and may be the empty string in some cases.
- `u_port`: The port. May be empty if irrelevant or not specified.
- `u_port`: The port. May be zero if irrelevant or not specified.
- `u_path`: The path, typically used with HTTP or WebSockets. Will be empty string if not specified.
- `u_query`: The query info (typically following `?` in the URL.) Will be `NULL` if not present.
- `u_fragment`: This is used for specifying an anchor, the part after `#` in a URL. Will be `NULL` if not present.
Expand Down
7 changes: 7 additions & 0 deletions docs/ref/migrate/nng1.md
Original file line number Diff line number Diff line change
Expand Up @@ -160,4 +160,11 @@ A number of the [statistics][statistic] functions take, or return, `const nng_st
of plain `nng_stat *`. The ABI has not changed, but it may be necessary to declare
certain methods variables `const` to avoid warnings about misuse of `const`.

## Url Structure Members

The details of [`nng_url`] have changed as follows:

- `u_port` is no longer a string, but a `uint16_t`
- `u_scheme` is a const char \*

{{#include ../xref.md}}
20 changes: 10 additions & 10 deletions include/nng/nng.h
Original file line number Diff line number Diff line change
Expand Up @@ -1083,16 +1083,16 @@ enum nng_errno_enum {
// give us a convenient way of doing so.

typedef struct nng_url {
char *u_rawurl; // never NULL
char *u_scheme; // never NULL
char *u_userinfo; // will be NULL if not specified
char *u_host; // including colon and port
char *u_hostname; // name only, will be "" if not specified
char *u_port; // port, will be "" if not specified
char *u_path; // path, will be "" if not specified
char *u_query; // without '?', will be NULL if not specified
char *u_fragment; // without '#', will be NULL if not specified
char *u_requri; // includes query and fragment, "" if not specified
char *u_rawurl; // never NULL
const char *u_scheme; // never NULL
char *u_userinfo; // will be NULL if not specified
char *u_host; // including colon and port
char *u_hostname; // name only, will be "" if not specified
uint16_t u_port; // port, may be zero for schemes that do not use
char *u_path; // path, will be "" if not specified
char *u_query; // without '?', will be NULL if not specified
char *u_fragment; // without '#', will be NULL if not specified
char *u_requri; // includes query and fragment, "" if not specified
} nng_url;

// nng_url_parse parses a URL string into a structured form.
Expand Down
6 changes: 5 additions & 1 deletion src/core/platform.h
Original file line number Diff line number Diff line change
Expand Up @@ -343,14 +343,18 @@ extern int nni_tcp_listener_get(
// Symbolic service names will be looked up assuming SOCK_STREAM, so
// they may not work with UDP.
extern void nni_resolv_ip(
const char *, const char *, int, bool, nng_sockaddr *sa, nni_aio *);
const char *, uint16_t, int, bool, nng_sockaddr *sa, nni_aio *);

// nni_parse_ip parses an IP address, without a port.
extern int nni_parse_ip(const char *, nng_sockaddr *);

// nni_parse_ip_port parses an IP address with an optional port appended.
extern int nni_parse_ip_port(const char *, nng_sockaddr *);

// nni_get_port_by_name resolves a name (which may be an ASCII representation
// of a number) to a port number (the value returned is in native byte order.)
extern int nni_get_port_by_name(const char *, uint16_t *);

//
// IPC (UNIX Domain Sockets & Named Pipes) Support.
//
Expand Down
24 changes: 8 additions & 16 deletions src/core/tcp.c
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]>
// Copyright 2019 Devolutions <[email protected]>
//
Expand All @@ -10,6 +10,7 @@
//

#include <stdint.h>
#include <stdio.h>
#include <string.h>

#include <nng/nng.h>
Expand All @@ -20,8 +21,8 @@

typedef struct {
nng_stream_dialer ops;
char *host;
char *port;
char host[256];
uint16_t port;
int af; // address family
bool closed;
nng_sockaddr sa;
Expand Down Expand Up @@ -155,8 +156,6 @@ tcp_dialer_free(void *arg)
nni_tcp_dialer_fini(d->d);
}
nni_mtx_fini(&d->mtx);
nni_strfree(d->host);
nni_strfree(d->port);
NNI_FREE_STRUCT(d);
}

Expand Down Expand Up @@ -236,17 +235,13 @@ nni_tcp_dialer_alloc(nng_stream_dialer **dp, const nng_url *url)
{
tcp_dialer *d;
int rv;
const char *p;

if ((rv = tcp_dialer_alloc(&d)) != 0) {
return (rv);
}

if (((p = url->u_port) == NULL) || (strlen(p) == 0)) {
p = nni_url_default_port(url->u_scheme);
}

if ((strlen(p) == 0) || (strlen(url->u_hostname) == 0)) {
if ((url->u_port == 0) || strlen(url->u_hostname) == 0 ||
strlen(url->u_hostname) >= sizeof(d->host)) {
// Dialer needs both a destination hostname and port.
tcp_dialer_free(d);
return (NNG_EADDRINVAL);
Expand All @@ -260,11 +255,8 @@ nni_tcp_dialer_alloc(nng_stream_dialer **dp, const nng_url *url)
d->af = NNG_AF_UNSPEC;
}

if (((d->host = nng_strdup(url->u_hostname)) == NULL) ||
((d->port = nng_strdup(p)) == NULL)) {
tcp_dialer_free(d);
return (NNG_ENOMEM);
}
snprintf(d->host, sizeof(d->host), "%s", url->u_hostname);
d->port = url->u_port;

*dp = (void *) d;
return (0);
Expand Down
Loading
Loading