omit port when sending request if 80 or 443

This commit is contained in:
2025-06-02 09:25:38 -05:00
parent 90b5d1430f
commit e2668b330e

View File

@@ -83,14 +83,26 @@ static int parse_url(const char *url, int *use_ssl,
// Builds a GET request into buf
static int build_get_request(buffer_t *buf,
const char *host, const char *port, const char *path) {
// HTTP/1.1 requires Host header; Connection: close for simplicity
char tmp[512];
// If port is “80” for HTTP or “443” for HTTPS, omit the “:port” in Host:
char host_header[512];
if ((strcmp(port, "80") == 0) || (strcmp(port, "443") == 0)) {
// default port: just “Host: hostname”
snprintf(host_header, sizeof(host_header), "Host: %s\r\n", host);
} else {
// non-default port: “Host: hostname:port”
snprintf(host_header, sizeof(host_header), "Host: %s:%s\r\n", host, port);
}
// Now build the rest of the request, including a User-Agent:
char tmp[1024];
int n = snprintf(tmp, sizeof(tmp),
"GET %s HTTP/1.1\r\n"
"Host: %s:%s\r\n"
"%s" // the host_header line
"User-Agent: ProsperonFetch/1.0\r\n"
"Accept: */*\r\n"
"Connection: close\r\n"
"\r\n",
path, host, port);
path, host_header);
if (n < 0) return -1;
return buffer_append(buf, tmp, (size_t)n);
}