Skip to content

Commit 58b60fc

Browse files
committed
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 parent fa7aba7 commit 58b60fc

File tree

2 files changed

+14
-3
lines changed

2 files changed

+14
-3
lines changed

src/netops.c

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,15 @@ int gitno__match_host(const char *pattern, const char *host)
119119
return -1;
120120
}
121121

122+
static const char *default_port_http = "80";
123+
static const char *default_port_https = "443";
124+
125+
const char *gitno__default_port(
126+
gitno_connection_data *data)
127+
{
128+
return data->use_ssl ? default_port_https : default_port_http;
129+
}
130+
122131
static const char *prefix_http = "http://";
123132
static const char *prefix_https = "https://";
124133

@@ -141,18 +150,18 @@ int gitno_connection_data_from_url(
141150

142151
if (!git__prefixcmp(url, prefix_http)) {
143152
path_search_start = url + strlen(prefix_http);
144-
default_port = "80";
153+
default_port = default_port_http;
145154

146155
if (data->use_ssl) {
147156
giterr_set(GITERR_NET, "redirect from HTTPS to HTTP is not allowed");
148157
goto cleanup;
149158
}
150159
} else if (!git__prefixcmp(url, prefix_https)) {
151160
path_search_start = url + strlen(prefix_https);
152-
default_port = "443";
161+
default_port = default_port_https;
153162
data->use_ssl = true;
154163
} else if (url[0] == '/')
155-
default_port = data->use_ssl ? "443" : "80";
164+
default_port = gitno__default_port(data);
156165

157166
if (!default_port) {
158167
giterr_set(GITERR_NET, "unrecognized URL prefix");

src/netops.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,4 +96,6 @@ int gitno_extract_url_parts(
9696
const char *url,
9797
const char *default_port);
9898

99+
const char *gitno__default_port(gitno_connection_data *data);
100+
99101
#endif

0 commit comments

Comments
 (0)