Skip to content

Bug: netutil.BuildUrl does not support multi-level domains like xxx.xx.com.cn #314

@huangpj0210

Description

@huangpj0210

Hi, thank you for the great work on this library!

I ran into an issue when using netutil.BuildUrl. Specifically, the hostRegex used for validating the host does not accept valid multi-level domain names such as:

host := "my.api.edu.cn"

When calling:

func TestBuildUrl(t *testing.T) {
	url, err := netutil.BuildUrl(
		"https",
		"my.api.edu.cn",
		"/api",
		map[string][]string{
			"q": {"123"},
		},
	)
	if err != nil {
		t.Errorf("err %+v", err)
	}
	t.Logf("url %s:", url)
}

It returns the following error:

build url error: invalid host: 'my.api.edu.cn' is not a valid host

After investigating the code, I found that the regular expression used for validating the host:

^([a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\-]*[a-zA-Z0-9])(\.[a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\-]*[a-zA-Z0-9])*$

only matches hosts like xxx.xx.com but fails for valid public suffixes such as .com.cn, .gov.cn, etc., which are widely used in countries like China.
Suggested Fix

Consider updating the hostRegex to support multi-level domain names like:
• api.test.gov.cn
• service.example.co.uk
• my.api.edu.cn

Thanks again for your work on this project!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions