Skip to content

fix: use net.SplitHostPort in stripHostPort to correctly handle IPv6 hosts#2208

Closed
Copilot wants to merge 3 commits intodev-vhostfrom
copilot/sub-pr-2202-one-more-time
Closed

fix: use net.SplitHostPort in stripHostPort to correctly handle IPv6 hosts#2208
Copilot wants to merge 3 commits intodev-vhostfrom
copilot/sub-pr-2202-one-more-time

Conversation

Copy link
Contributor

Copilot AI commented Mar 9, 2026

stripHostPort used a manual heuristic (checking for [) to detect IPv6 addresses, which caused it to leave the port in place for bracketed IPv6 hosts with ports (e.g. [::1]:5244), breaking virtual host matching.

Changes

  • server/static/static.go: Replace manual string parsing in stripHostPort with net.SplitHostPort, which correctly handles all host header formats. Falls back to returning the host as-is when no port is present.
// Before — [::1]:5244 returned unchanged (port not stripped)
func stripHostPort(host string) string {
    if idx := strings.LastIndex(host, ":"); idx != -1 {
        if !strings.Contains(host, "[") {
            return host[:idx]
        }
    }
    return host
}

// After — correctly handles IPv4, IPv6 bracketed, and bare hosts
func stripHostPort(host string) string {
    h, _, err := net.SplitHostPort(host)
    if err != nil {
        return host
    }
    return h
}
Input Before After
[::1]:5244 [::1]:5244 ::1
example.com:8080 example.com example.com
192.168.1.1:8080 192.168.1.1 192.168.1.1
example.com example.com example.com

💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.

Copilot AI and others added 2 commits March 9, 2026 07:31
…ectly

Co-authored-by: PIKACHUIM <40362270+PIKACHUIM@users.noreply.github.com>
…hosts

Co-authored-by: PIKACHUIM <40362270+PIKACHUIM@users.noreply.github.com>
Copilot AI changed the title [WIP] Work in progress on virtual host support implementation fix: use net.SplitHostPort in stripHostPort to correctly handle IPv6 hosts Mar 9, 2026
@jyxjjj jyxjjj closed this Mar 9, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants