From d89afcecc175be1d00d97f55309513e6f25f1b28 Mon Sep 17 00:00:00 2001 From: MentalOfCrow <145018989+MentalOfCrow@users.noreply.github.com> Date: Fri, 4 Dec 2026 13:56:05 +0100 Subject: [PATCH] fix: recognize Mercurial repositories over HTTP --- README.md | 2 +- vcsurl.go | 28 +++++++++++++++++++++++++++- vcsurl_test.go | 47 +++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 75 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 3b176aa..c91591c 100644 --- a/README.md +++ b/README.md @@ -19,7 +19,7 @@ This parser performs syntactic and semantic validation according to the [errorformat](https://vim-jp.org/vimdoc-en/quickfix.html#error-file-format) friendly way - Verifies the existence of URLs by checking the response for URL fields - (can be disabled) + (can be disabled), including Mercurial repositories served over HTTP(S) ## As a library diff --git a/vcsurl.go b/vcsurl.go index 6408866..ccc1c97 100644 --- a/vcsurl.go +++ b/vcsurl.go @@ -4,6 +4,7 @@ import ( "context" "fmt" "io" + "mime" "net/http" "net/url" "strings" @@ -73,8 +74,33 @@ func (p *Parser) isRepo(ctx context.Context, u *url.URL) bool { var ok bool err := p.callVCS(ctx, func() { ok = vcsurl.IsRepo(u) }) + if err != nil { + return false + } + + return ok || p.isMercurialRepo(ctx, *u) +} + +// Mercurial servers advertise the wire protocol on the same URL as the repository. +func (p *Parser) isMercurialRepo(ctx context.Context, u url.URL) bool { + query := u.Query() + query.Set("cmd", "capabilities") + u.RawQuery = query.Encode() + + req, err := http.NewRequestWithContext(ctx, http.MethodGet, u.String(), nil) + if err != nil { + return false + } + + resp, err := p.client.Do(req) + if err != nil { + return false + } + defer func() { _ = resp.Body.Close() }() + + mediaType, _, err := mime.ParseMediaType(resp.Header.Get("Content-Type")) - return err == nil && ok + return err == nil && resp.StatusCode == http.StatusOK && mediaType == "application/mercurial-0.1" } // repositoryURL drops a trailing slash and a ".git" suffix from a repository diff --git a/vcsurl_test.go b/vcsurl_test.go index e2debaf..15d3042 100644 --- a/vcsurl_test.go +++ b/vcsurl_test.go @@ -10,6 +10,53 @@ import ( "testing" ) +func TestIsRepoRecognizesMercurialOverHTTP(t *testing.T) { + tests := []struct { + name string + contentType string + status int + want bool + }{ + {"Mercurial", "application/mercurial-0.1", http.StatusOK, true}, + {"Mercurial with a media type parameter", "application/mercurial-0.1; charset=utf-8", http.StatusOK, true}, + {"ordinary web page", "text/html", http.StatusOK, false}, + {"failed Mercurial request", "application/mercurial-0.1", http.StatusNotFound, false}, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + if r.URL.Path != "/hg/project/" || r.URL.Query().Get("cmd") != "capabilities" { + http.NotFound(w, r) + + return + } + if r.Method != http.MethodGet || r.URL.Query().Get("tenant") != "example" { + t.Errorf("unexpected capabilities request: %s %s", r.Method, r.URL) + } + w.Header().Set("Content-Type", tt.contentType) + w.WriteHeader(tt.status) + })) + defer server.Close() + + p, err := NewParser(ParserConfig{AllowNetworkToPrivateHosts: true}) + if err != nil { + t.Fatal(err) + } + u, err := url.Parse(server.URL + "/hg/project/?tenant=example") + if err != nil { + t.Fatal(err) + } + if got := p.isRepo(t.Context(), u); got != tt.want { + t.Errorf("isRepo = %t, want %t", got, tt.want) + } + if got := u.RawQuery; got != "tenant=example" { + t.Errorf("isRepo changed the repository URL query to %q", got) + } + }) + } +} + func TestParserNetworkPolicyIsolation(t *testing.T) { var requests atomic.Int64 srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {