Skip to content

Commit 4ef2b88

Browse files
authored
Merge pull request libgit2#4882 from kc8apf/include_port_in_host_header
transport/http: Include non-default ports in Host header
2 parents 7321cff + 83b3518 commit 4ef2b88

File tree

3 files changed

+19
-4
lines changed

3 files changed

+19
-4
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

src/transports/http.c

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,11 @@ static int gen_request(
208208
git_buf_puts(buf, "User-Agent: ");
209209
git_http__user_agent(buf);
210210
git_buf_puts(buf, "\r\n");
211-
git_buf_printf(buf, "Host: %s\r\n", t->connection_data.host);
211+
git_buf_printf(buf, "Host: %s", t->connection_data.host);
212+
if (strcmp(t->connection_data.port, gitno__default_port(&t->connection_data)) != 0) {
213+
git_buf_printf(buf, ":%s", t->connection_data.port);
214+
}
215+
git_buf_puts(buf, "\r\n");
212216

213217
if (s->chunked || content_length > 0) {
214218
git_buf_printf(buf, "Accept: application/x-git-%s-result\r\n", s->service);

0 commit comments

Comments
 (0)