internal/httpd

7effe3feb4612cffb5ed38c02300299559aa8971
gitbay/internal/httpd/
namelast commitupdated
account.go web: Settings gets a labelled rail row; tidy the account cell 1 month ago
accounts.go control: named payloads for what other surfaces decode 1 month ago
activity.go web: the owner page dispatches profile show 1 month ago
admin.go dashboard: worker queues for admins, and an /admin page 1 month ago
api.go http.trusted_proxies, and a cap on verification mail per account 1 month ago
apilimit.go http.trusted_proxies, and a cap on verification mail per account 1 month ago
apilimit_test.go httpd: rate limit the JSON API 1 month ago
apiread.go http.trusted_proxies, and a cap on verification mail per account 1 month ago
badge.go build cancel: withdraw a queued build 1 month ago
badgepng.go Serve build badges as PNG as well as SVG 1 month ago
badgepng_test.go Serve build badges as PNG as well as SVG 1 month ago
buildpages_test.go build: render tests for both build pages 1 month ago
builds.go build: a jobs command, and build pages that dispatch it 1 month ago
clientip_test.go http.trusted_proxies, and a cap on verification mail per account 1 month ago
control.go control: named payloads for what other surfaces decode 1 month ago
crumbs_test.go web: directory crumbs on blob and blame pages link to the tree 1 month ago
diff.go web: keep an added line's leading dash in the diff 1 month ago
diff_test.go web: keep an added line's leading dash in the diff 1 month ago
facts.go Collapse a contributor's several addresses into one row 1 month ago
feed.go web: a dashboard that answers "what needs me", badges, and author names 1 month ago
filters.go issue list and mr list: filter by label, assignee, author, milestone 1 month ago
fonts_test.go web: serve the fonts the stylesheet asks for 1 month ago
healthz.go http: /healthz 1 month ago
issueactions.go web: form actions answer by exit code 1 month ago
langs.go web: repository facts on the code page 1 month ago
lfs.go lfs: count it in stats, and gc --lfs for orphaned objects 1 month ago
mractions.go control: named payloads for what other surfaces decode 1 month ago
mrpage_test.go Merge requests say when things happened, and who resolved them 1 month ago
orgrender_test.go bodies: choose markdown or org per body 1 month ago
orgweb.go web: run an organization from its page 1 month ago
page.go dashboard: worker queues for admins, and an /admin page 1 month ago
pages.go Custom domains for pages 1 month ago
palette_test.go web: chroma line numbers use the site gutter colour 1 month ago
readme.go web: the owner page dispatches profile show 1 month ago
releaseactions.go web: form actions answer by exit code 1 month ago
routes.go web: serve the fonts the stylesheet asks for 1 month ago
routes_test.go Git LFS: batch API, basic transfers, SSH-minted tokens 1 month ago
settings.go Report dependencies that have fallen behind upstream 1 month ago
smart.go http.trusted_proxies, and a cap on verification mail per account 1 month ago
web.go web: directory crumbs on blob and blame pages link to the tree 1 month ago
wiki.go control: wiki list and wiki show 1 month ago
package httpd

import (
	"html/template"
	"path"
	"strings"

	"golang.org/x/net/html"
	"golang.org/x/net/html/atom"

	"gitbay.org/gitbay/internal/gitutil"
)

// rewriteRelativeLinks makes relative hrefs and srcs in rendered repo
// content resolve on the forge: links go to blob pages, images to raw.
// go-org exports .org links as .html, so an .html target whose .org (or
// .md) source exists in the tree maps back to the source file.
func rewriteRelativeLinks(rendered template.HTML, p repoPage, baseDir string) template.HTML {
	ctx := &html.Node{Type: html.ElementNode, Data: "div", DataAtom: atom.Div}
	nodes, err := html.ParseFragment(strings.NewReader(string(rendered)), ctx)
	if err != nil {
		return rendered
	}
	var walk func(*html.Node)
	walk = func(n *html.Node) {
		if n.Type == html.ElementNode {
			for i, a := range n.Attr {
				isHref := a.Key == "href" && n.Data == "a"
				isSrc := a.Key == "src" && (n.Data == "img" || n.Data == "video" || n.Data == "source")
				if !isHref && !isSrc {
					continue
				}
				v := a.Val
				if v == "" || strings.Contains(v, "://") || strings.HasPrefix(v, "/") ||
					strings.HasPrefix(v, "#") || strings.HasPrefix(v, "mailto:") ||
					strings.HasPrefix(v, "data:") {
					continue
				}
				target := path.Clean(path.Join(baseDir, v))
				if strings.HasPrefix(target, "..") {
					continue
				}
				if isSrc {
					n.Attr[i].Val = "/" + p.Repo.Path() + "/raw/" + p.Ref + "/" + target
					continue
				}
				// .html from org/markdown exports maps back to the source.
				if strings.HasSuffix(target, ".html") {
					stem := strings.TrimSuffix(target, ".html")
					for _, ext := range []string{".org", ".md"} {
						if _, err := gitutil.ReadBlob(p.Dir, p.Ref, stem+ext, 1); err == nil {
							target = stem + ext
							break
						}
					}
				}
				n.Attr[i].Val = "/" + p.Repo.Path() + "/blob/" + p.Ref + "/" + target
			}
		}
		for c := n.FirstChild; c != nil; c = c.NextSibling {
			walk(c)
		}
	}
	var out strings.Builder
	for _, n := range nodes {
		walk(n)
		if err := html.Render(&out, n); err != nil {
			return rendered
		}
	}
	return template.HTML(out.String())
}