Skip to content

Commit

Permalink
lib-http: Started using struct uri_host in struct http_url.
Browse files Browse the repository at this point in the history
  • Loading branch information
stephanbosch authored and GitLab committed May 16, 2016
1 parent 8d2d278 commit f74dbd3
Show file tree
Hide file tree
Showing 9 changed files with 153 additions and 155 deletions.
6 changes: 3 additions & 3 deletions src/lib-http/http-client-host.c
Expand Up @@ -176,7 +176,7 @@ struct http_client_host *http_client_host_get
}

} else {
const char *hostname = host_url->host_name;
const char *hostname = host_url->host.name;

host = hash_table_lookup(client->hosts, hostname);
if (host == NULL) {
Expand All @@ -185,10 +185,10 @@ struct http_client_host *http_client_host_get
hostname = host->name;
hash_table_insert(client->hosts, hostname, host);

if (host_url->host_ip.family != 0) {
if (host_url->host.ip.family != 0) {
host->ips_count = 1;
host->ips = i_new(struct ip_addr, host->ips_count);
host->ips[0] = host_url->host_ip;
host->ips[0] = host_url->host.ip;
}

http_client_host_debug(host, "Host created");
Expand Down
16 changes: 8 additions & 8 deletions src/lib-http/http-client-request.c
Expand Up @@ -91,7 +91,7 @@ http_client_request(struct http_client *client,
struct http_client_request *req;

req = http_client_request_new(client, method, callback, context);
req->origin_url.host_name = p_strdup(req->pool, host);
req->origin_url.host.name = p_strdup(req->pool, host);
req->target = (target == NULL ? "/" : p_strdup(req->pool, target));
return req;
}
Expand Down Expand Up @@ -125,10 +125,10 @@ http_client_request_connect(struct http_client *client,
struct http_client_request *req;

req = http_client_request_new(client, "CONNECT", callback, context);
req->origin_url.host_name = p_strdup(req->pool, host);
req->origin_url.host.name = p_strdup(req->pool, host);
req->origin_url.port = port;
req->connect_tunnel = TRUE;
req->target = req->origin_url.host_name;
req->target = req->origin_url.host.name;
return req;
}

Expand All @@ -147,7 +147,7 @@ http_client_request_connect_ip(struct http_client *client,

req = http_client_request_connect
(client, hostname, port, callback, context);
req->origin_url.host_ip = *ip;
req->origin_url.host.ip = *ip;
return req;
}

Expand Down Expand Up @@ -617,21 +617,21 @@ http_client_request_get_peer_addr(const struct http_client_request *req,
addr->a.un.path = host_socket;
} else if (req->connect_direct) {
addr->type = HTTP_CLIENT_PEER_ADDR_RAW;
addr->a.tcp.ip = host_url->host_ip;
addr->a.tcp.ip = host_url->host.ip;
addr->a.tcp.port =
(host_url->port != 0 ? host_url->port : HTTPS_DEFAULT_PORT);
} else if (host_url->have_ssl) {
if (req->ssl_tunnel)
addr->type = HTTP_CLIENT_PEER_ADDR_HTTPS_TUNNEL;
else
addr->type = HTTP_CLIENT_PEER_ADDR_HTTPS;
addr->a.tcp.ip = host_url->host_ip;
addr->a.tcp.https_name = host_url->host_name;
addr->a.tcp.ip = host_url->host.ip;
addr->a.tcp.https_name = host_url->host.name;
addr->a.tcp.port =
(host_url->port != 0 ? host_url->port : HTTPS_DEFAULT_PORT);
} else {
addr->type = HTTP_CLIENT_PEER_ADDR_HTTP;
addr->a.tcp.ip = host_url->host_ip;
addr->a.tcp.ip = host_url->host.ip;
addr->a.tcp.port =
(host_url->port != 0 ? host_url->port : HTTPS_DEFAULT_PORT);
}
Expand Down
27 changes: 8 additions & 19 deletions src/lib-http/http-url.c
Expand Up @@ -79,8 +79,7 @@ static bool http_url_parse_authority(struct http_url_parser *url_parser)
}
}
if (url != NULL) {
url->host_name = p_strdup(parser->pool, auth.host.name);
url->host_ip = auth.host.ip;
uri_host_copy(parser->pool, &url->host, &auth.host);
url->port = auth.port;
url->user = p_strdup(parser->pool, user);
url->password = p_strdup(parser->pool, password);
Expand Down Expand Up @@ -232,8 +231,7 @@ static bool http_url_do_parse(struct http_url_parser *url_parser)
parser->error = "Relative HTTP URL not allowed";
return FALSE;
} else if (!have_authority && url != NULL) {
url->host_name = p_strdup_empty(parser->pool, base->host_name);
url->host_ip = base->host_ip;
uri_host_copy(parser->pool, &url->host, &base->host);
url->port = base->port;
url->have_ssl = base->have_ssl;
url->user = p_strdup_empty(parser->pool, base->user);
Expand Down Expand Up @@ -381,17 +379,15 @@ int http_url_request_target_parse(const char *request_target,

if (request_target[0] == '*' && request_target[1] == '\0') {
struct http_url *url = p_new(pool, struct http_url, 1);
url->host_name = p_strdup(pool, auth.host.name);
url->host_ip = auth.host.ip;
uri_host_copy(pool, &url->host, &auth.host);
url->port = auth.port;
target->url = url;
target->format = HTTP_REQUEST_TARGET_FORMAT_ASTERISK;
return 0;
}

memset(&base, 0, sizeof(base));
base.host_name = auth.host.name;
base.host_ip = auth.host.ip;
base.host = auth.host;
base.port = auth.port;

memset(parser, '\0', sizeof(*parser));
Expand Down Expand Up @@ -421,8 +417,7 @@ void http_url_copy_authority(pool_t pool, struct http_url *dest,
const struct http_url *src)
{
memset(dest, 0, sizeof(*dest));
dest->host_name = p_strdup(pool, src->host_name);
dest->host_ip = src->host_ip;
uri_host_copy(pool, &dest->host, &src->host);
dest->port = src->port;
dest->have_ssl = src->have_ssl;
}
Expand Down Expand Up @@ -495,15 +490,9 @@ http_url_add_scheme(string_t *urlstr, const struct http_url *url)
static void
http_url_add_authority(string_t *urlstr, const struct http_url *url)
{
/* host:port */
if (url->host_name != NULL) {
/* assume IPv6 literal if starts with '['; avoid encoding */
if (*url->host_name == '[')
str_append(urlstr, url->host_name);
else
uri_append_host_name(urlstr, url->host_name);
} else
uri_append_host_ip(urlstr, &url->host_ip);
/* host */
uri_append_host(urlstr, &url->host);
/* port */
uri_append_port(urlstr, url->port);
}

Expand Down
4 changes: 2 additions & 2 deletions src/lib-http/http-url.h
Expand Up @@ -2,13 +2,13 @@
#define HTTP_URL_H

#include "net.h"
#include "uri-util.h"

struct http_request_target;

struct http_url {
/* server */
const char *host_name;
struct ip_addr host_ip;
struct uri_host host;
in_port_t port;

/* userinfo (not parsed by default) */
Expand Down
2 changes: 1 addition & 1 deletion src/lib-http/test-http-client.c
Expand Up @@ -295,7 +295,7 @@ test_http_request_init(struct http_client *http_client,
test_req = i_new(struct http_test_request, 1);
test_req->write_output = TRUE;
http_req = http_client_request(http_client,
method, url->host_name,
method, url->host.name,
t_strconcat("/", url->path, url->enc_query, NULL),
got_request_response, test_req);
if (url->port != 0)
Expand Down
41 changes: 23 additions & 18 deletions src/lib-http/test-http-request-parser.c
Expand Up @@ -42,7 +42,7 @@ valid_request_parse_tests[] = {
.target_raw = "/",
.target = {
.format = HTTP_REQUEST_TARGET_FORMAT_ORIGIN,
.url = { .host_name = "example.com" }
.url = { .host = { .name = "example.com" } }
},
.version_major = 1, .version_minor = 1,
},{ .request =
Expand All @@ -54,7 +54,7 @@ valid_request_parse_tests[] = {
.target_raw = "*",
.target = {
.format = HTTP_REQUEST_TARGET_FORMAT_ASTERISK,
.url = { .host_name = "example.com" }
.url = { .host = { .name = "example.com" } }
},
.version_major = 1, .version_minor = 0,
},{ .request =
Expand All @@ -65,7 +65,9 @@ valid_request_parse_tests[] = {
.target_raw = "example.com:443",
.target = {
.format = HTTP_REQUEST_TARGET_FORMAT_AUTHORITY,
.url = { .host_name = "example.com", .port = 443 }
.url = {
.host = { .name = "example.com" },
.port = 443 }
},
.version_major = 1, .version_minor = 2,
},{ .request =
Expand All @@ -77,7 +79,7 @@ valid_request_parse_tests[] = {
.target = {
.format = HTTP_REQUEST_TARGET_FORMAT_ABSOLUTE,
.url = {
.host_name = "www.example.com",
.host = { .name = "www.example.com" },
.port = 443,
.have_ssl = TRUE
}
Expand All @@ -93,7 +95,9 @@ valid_request_parse_tests[] = {
.target_raw = "http://api.example.com:8080/commit?user=dirk",
.target = {
.format = HTTP_REQUEST_TARGET_FORMAT_ABSOLUTE,
.url = { .host_name = "api.example.com", .port = 8080 }
.url = {
.host = { .name = "api.example.com" },
.port = 8080 }
},
.version_major = 1, .version_minor = 1,
.payload = "Content!\r\n"
Expand All @@ -106,7 +110,8 @@ valid_request_parse_tests[] = {
.target_raw = "http://www.example.com/index.php?seq=1",
.target = {
.format = HTTP_REQUEST_TARGET_FORMAT_ABSOLUTE,
.url = { .host_name = "www.example.com" }
.url = {
.host = { .name = "www.example.com" }}
},
.version_major = 1, .version_minor = 1,
.connection_close = TRUE
Expand All @@ -118,7 +123,7 @@ valid_request_parse_tests[] = {
.target_raw = "http://www.example.com/index.html",
.target = {
.format = HTTP_REQUEST_TARGET_FORMAT_ABSOLUTE,
.url = { .host_name = "www.example.com" }
.url = { .host = { .name = "www.example.com" } }
},
.version_major = 1, .version_minor = 0,
.connection_close = TRUE
Expand All @@ -131,7 +136,7 @@ valid_request_parse_tests[] = {
.target_raw = "http://www.example.com/index.html",
.target = {
.format = HTTP_REQUEST_TARGET_FORMAT_ABSOLUTE,
.url = { .host_name = "www.example.com" }
.url = { .host = { .name = "www.example.com" } }
},
.version_major = 1, .version_minor = 1,
.expect_100_continue = TRUE
Expand Down Expand Up @@ -229,18 +234,18 @@ static void test_http_request_parse_valid(void)
}
if (request.target.url == NULL) {
test_out("request->target.url = (null)",
test->target.url.host_name == NULL && test->target.url.port == 0);
test->target.url.host.name == NULL && test->target.url.port == 0);
} else {
if (request.target.url->host_name == NULL ||
test->target.url.host_name == NULL) {
test_out(t_strdup_printf("request->target.url->host_name = %s",
request.target.url->host_name),
request.target.url->host_name == test->target.url.host_name);
if (request.target.url->host.name == NULL ||
test->target.url.host.name == NULL) {
test_out(t_strdup_printf("request->target.url->host.name = %s",
request.target.url->host.name),
request.target.url->host.name == test->target.url.host.name);
} else {
test_out(t_strdup_printf("request->target.url->host_name = %s",
request.target.url->host_name),
strcmp(request.target.url->host_name,
test->target.url.host_name) == 0);
test_out(t_strdup_printf("request->target.url->host.name = %s",
request.target.url->host.name),
strcmp(request.target.url->host.name,
test->target.url.host.name) == 0);
}
if (request.target.url->port == 0) {
test_out("request->target.url->port = (unspecified)",
Expand Down

0 comments on commit f74dbd3

Please sign in to comment.