Skip to content

Commit

Permalink
Include ipv6 wrapping brackets [] in the result of mg_url_host()
Browse files Browse the repository at this point in the history
  • Loading branch information
cpq committed Sep 21, 2021
1 parent 8652006 commit 90a131b
Show file tree
Hide file tree
Showing 5 changed files with 10 additions and 15 deletions.
7 changes: 4 additions & 3 deletions docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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()
Expand Down
7 changes: 2 additions & 5 deletions mongoose.c
Original file line number Diff line number Diff line change
Expand Up @@ -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') ||
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}

Expand Down
1 change: 1 addition & 0 deletions src/net.c
Original file line number Diff line number Diff line change
Expand Up @@ -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') ||
Expand Down
6 changes: 1 addition & 5 deletions src/url.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#include <stdlib.h>
#include "url.h"
#include <stdlib.h>

struct url {
size_t key, user, pass, host, port, uri, end;
Expand Down Expand Up @@ -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;
}

Expand Down
4 changes: 2 additions & 2 deletions test/unit_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 90a131b

Please sign in to comment.