From 9076bc3f75e05af80d2b812c7c1f72b69e1ce86f Mon Sep 17 00:00:00 2001 From: Gary Wang Date: Fri, 8 Jul 2022 13:39:24 +0200 Subject: [PATCH] Support access branch that contains slash character (#102) So we can access branch that contain slash like `branch/name` with `username.codeberg.page/repo/@branch~name/`. Branch name cannot contain `~` character but it can be in a HTTP URL, so replace the `~` from URL to `/` could be a valid solution to me. Resolve #101 Co-authored-by: Gary Wang Reviewed-on: https://codeberg.org/Codeberg/pages-server/pulls/102 Reviewed-by: 6543 <6543@noreply.codeberg.org> Co-authored-by: Gary Wang Co-committed-by: Gary Wang --- README.md | 3 ++- integration/get_test.go | 10 ++++++++++ server/handler.go | 4 ++++ 3 files changed, 16 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 6543c90..700f279 100644 --- a/README.md +++ b/README.md @@ -18,7 +18,8 @@ Mapping custom domains is not static anymore, but can be done with DNS: first line will be the canonical domain/URL; all other occurrences will be redirected to it. 2) add a CNAME entry to your domain, pointing to `[[{branch}.]{repo}.]{owner}.codeberg.page` (repo defaults to -"pages", "branch" defaults to the default branch if "repo" is "pages", or to "pages" if "repo" is something else): +"pages", "branch" defaults to the default branch if "repo" is "pages", or to "pages" if "repo" is something else. +If the branch name contains slash characters, you need to replace "/" in the branch name to "~"): `www.example.org. IN CNAME main.pages.example.codeberg.page.` 3) if a CNAME is set for "www.example.org", you can redirect there from the naked domain by adding an ALIAS record diff --git a/integration/get_test.go b/integration/get_test.go index 9cb66f5..1997010 100644 --- a/integration/get_test.go +++ b/integration/get_test.go @@ -49,6 +49,16 @@ func TestGetContent(t *testing.T) { assert.EqualValues(t, "text/html; charset=utf-8", resp.Header.Get("Content-Type")) assert.True(t, getSize(resp.Body) > 1000) assert.Len(t, resp.Header.Get("ETag"), 42) + + // access branch name contains '/' + resp, err = getTestHTTPSClient().Get("https://blumia.localhost.mock.directory:4430/pages-server-integration-tests/@docs~main/") + assert.NoError(t, err) + if !assert.EqualValues(t, http.StatusOK, resp.StatusCode) { + t.FailNow() + } + assert.EqualValues(t, "text/html; charset=utf-8", resp.Header.Get("Content-Type")) + assert.True(t, getSize(resp.Body) > 100) + assert.Len(t, resp.Header.Get("ETag"), 42) } func TestCustomDomain(t *testing.T) { diff --git a/server/handler.go b/server/handler.go index 3a2667d..11da0a5 100644 --- a/server/handler.go +++ b/server/handler.go @@ -89,6 +89,10 @@ func Handler(mainDomainSuffix, rawDomain []byte, return false } + // Replace "~" to "/" so we can access branch that contains slash character + // Branch name cannot contain "~" so doing this is okay + branch = strings.ReplaceAll(branch, "~", "/") + // Check if the branch exists, otherwise treat it as a file path branchTimestampResult := upstream.GetBranchTimestamp(giteaClient, targetOwner, repo, branch, branchTimestampCache) if branchTimestampResult == nil {