Include ipv6 wrapping brackets [] in the result of mg_url_host()

This commit is contained in:
Sergey Lyubka 2021-09-21 08:10:27 +01:00
parent 8652006aea
commit 90a131bd29
5 changed files with 10 additions and 15 deletions

View File

@ -2499,7 +2499,7 @@ bool mg_aton(struct mg_str str, struct mg_addr *addr);
Parse IP address held by `str` and store it in `addr`.
Parameters:
- `str` - string to parse
- `str` - string to parse, for example `1.2.3.4`, `[::1]`, `01:02::03`
- `addr` - pointer to `mg_addr` string to receive parsed value
Return value: `true` on success, `false` otherwise.
@ -3285,14 +3285,15 @@ struct mg_str mg_url_host(const char *url);
Extract host name from given URL.
Parameters:
- `url` - URL to extract host
- `url` - a URL string
Return value: host name
Usage example:
```c
struct mg_str host = mg_url_host("https://my.example.org:1234"); // host is now "my.example.org"
struct mg_str a = mg_url_host("https://my.example.org:1234"); // a == "my.example.org"
struct mg_str b = mg_url_host("tcp://[::1]"); // b == "[::1]"
```
### mg\_url\_user()

View File

@ -2439,6 +2439,7 @@ static bool mg_v4mapped(struct mg_str str, struct mg_addr *addr) {
static bool mg_aton6(struct mg_str str, struct mg_addr *addr) {
size_t i, j = 0, n = 0, dc = 42;
if (str.len > 2 && str.ptr[0] == '[') str.ptr++, str.len -= 2;
if (mg_v4mapped(str, addr)) return true;
for (i = 0; i < str.len; i++) {
if ((str.ptr[i] >= '0' && str.ptr[i] <= '9') ||
@ -4048,8 +4049,8 @@ long mg_tls_send(struct mg_connection *c, const void *buf, size_t len) {
#ifdef MG_ENABLE_LINES
#line 1 "src/url.c"
#endif
#include <stdlib.h>
#include <stdlib.h>
struct url {
size_t key, user, pass, host, port, uri, end;
@ -4095,10 +4096,6 @@ struct mg_str mg_url_host(const char *url) {
: u.uri ? u.uri - u.host
: u.end - u.host;
struct mg_str s = mg_str_n(url + u.host, n);
if (s.len > 2 && s.ptr[0] == '[' && s.ptr[s.len - 1] == ']') {
s.len -= 2;
s.ptr++;
}
return s;
}

View File

@ -87,6 +87,7 @@ static bool mg_v4mapped(struct mg_str str, struct mg_addr *addr) {
static bool mg_aton6(struct mg_str str, struct mg_addr *addr) {
size_t i, j = 0, n = 0, dc = 42;
if (str.len > 2 && str.ptr[0] == '[') str.ptr++, str.len -= 2;
if (mg_v4mapped(str, addr)) return true;
for (i = 0; i < str.len; i++) {
if ((str.ptr[i] >= '0' && str.ptr[i] <= '9') ||

View File

@ -1,5 +1,5 @@
#include <stdlib.h>
#include "url.h"
#include <stdlib.h>
struct url {
size_t key, user, pass, host, port, uri, end;
@ -45,10 +45,6 @@ struct mg_str mg_url_host(const char *url) {
: u.uri ? u.uri - u.host
: u.end - u.host;
struct mg_str s = mg_str_n(url + u.host, n);
if (s.len > 2 && s.ptr[0] == '[' && s.ptr[s.len - 1] == ']') {
s.len -= 2;
s.ptr++;
}
return s;
}

View File

@ -135,8 +135,8 @@ static void test_url(void) {
ASSERT(vcmp(mg_url_host("p://bar:1234/a"), "bar"));
ASSERT(vcmp(mg_url_host("p://u@bar:1234/a"), "bar"));
ASSERT(vcmp(mg_url_host("p://u:p@bar:1234/a"), "bar"));
ASSERT(vcmp(mg_url_host("p://u:p@[::1]:1234/a"), "::1"));
ASSERT(vcmp(mg_url_host("p://u:p@[1:2::3]:1234/a"), "1:2::3"));
ASSERT(vcmp(mg_url_host("p://u:p@[::1]:1234/a"), "[::1]"));
ASSERT(vcmp(mg_url_host("p://u:p@[1:2::3]:1234/a"), "[1:2::3]"));
ASSERT(vcmp(mg_url_host("p://foo/x:y/z"), "foo"));
// Port