netops: add method to return default http port for a connection Constant strings and logic for HTTP(S) default ports were starting to be spread throughout netops.c. Instead of duplicating this again to determine if a Host header should include the port, move the default port constants and logic into an internal method in netops.{c,h}.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
diff --git a/src/netops.c b/src/netops.c
index efcc6c5..272d0cc 100644
--- a/src/netops.c
+++ b/src/netops.c
@@ -119,6 +119,15 @@ int gitno__match_host(const char *pattern, const char *host)
return -1;
}
+static const char *default_port_http = "80";
+static const char *default_port_https = "443";
+
+const char *gitno__default_port(
+ gitno_connection_data *data)
+{
+ return data->use_ssl ? default_port_https : default_port_http;
+}
+
static const char *prefix_http = "http://";
static const char *prefix_https = "https://";
@@ -141,7 +150,7 @@ int gitno_connection_data_from_url(
if (!git__prefixcmp(url, prefix_http)) {
path_search_start = url + strlen(prefix_http);
- default_port = "80";
+ default_port = default_port_http;
if (data->use_ssl) {
giterr_set(GITERR_NET, "redirect from HTTPS to HTTP is not allowed");
@@ -149,10 +158,10 @@ int gitno_connection_data_from_url(
}
} else if (!git__prefixcmp(url, prefix_https)) {
path_search_start = url + strlen(prefix_https);
- default_port = "443";
+ default_port = default_port_https;
data->use_ssl = true;
} else if (url[0] == '/')
- default_port = data->use_ssl ? "443" : "80";
+ default_port = gitno__default_port(data);
if (!default_port) {
giterr_set(GITERR_NET, "unrecognized URL prefix");
diff --git a/src/netops.h b/src/netops.h
index 75fd9a5..f376bd9 100644
--- a/src/netops.h
+++ b/src/netops.h
@@ -96,4 +96,6 @@ int gitno_extract_url_parts(
const char *url,
const char *default_port);
+const char *gitno__default_port(gitno_connection_data *data);
+
#endif