internal/httpd

ebf1e30fe0ae09b2d5f93705c1cbf7f513b6ed22
gitbay/internal/httpd/
namelast commitupdated
account.go store, control, web, wiki: notifications settings watch on|off 27 days ago
accounts.go store, control, web, wiki: bookmark a repository 1 month ago
activity.go web: the owner page dispatches profile show 1 month ago
admin.go control: the payloads other surfaces decode are named types 1 month ago
anchors_test.go web: markdown headings carry ids, and the stylesheet revalidates 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
atom.go web, store, policy, wiki: Atom feeds for releases, commits, and an owner's activity 1 month ago
atom_test.go web, store, policy, wiki: Atom feeds for releases, commits, and an owner's activity 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 web: cancel a queued or running build from its page 1 month ago
builds.go web: cancel a queued or running build from its page 1 month ago
checkorigin_test.go httpd: open registration in the checkOrigin route walk so POST /register is reachable 1 month ago
chip_test.go web: a visible edge on dark-mode buttons, and label colours held legible 1 month ago
clientip_test.go http.trusted_proxies, and a cap on verification mail per account 1 month ago
compare.go compare view: what one ref adds on top of another 1 month ago
control.go control: the payloads other surfaces decode are named types 1 month ago
cookieclear_test.go web: a suspended account cannot mail itself a session 1 month ago
crumbs_test.go web: directory crumbs on blob and blame pages link to the tree 1 month ago
depspage_test.go control, web, wiki: render dependency status on the settings page 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 store, control, web, wiki: bookmark a repository 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
flash.go web: clearing a cookie carries the attributes that set it 1 month ago
fonts_test.go web: serve the fonts the stylesheet asks for 1 month ago
headings_test.go web: a rendered document's headings sit under the page's own 1 month ago
healthz.go http: /healthz 1 month ago
inputlabels_test.go web: the topics-remove field gets a label, and a test that would have caught it 1 month ago
issueactions.go web: a failed form action's reason rides a one-shot cookie, not the URL 1 month ago
labels.go control, web: repository commands see org labels and milestones and refuse to change them 27 days 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
logincookie_test.go web: a suspended account cannot mail itself a session 1 month ago
logindisabled_test.go httpd: pin the login() disabled guard and the checkOrigin invariant 1 month ago
mractions.go store, web, wiki: open a merge request from a fork in the browser 1 month ago
mrpage_test.go control, web, wiki: merge gates on mr show and the page, every unmet gate at once 1 month ago
notifyweb.go control: repo unwatch clears the row, repo mute silences 1 month ago
orglabels.go web: org label and milestone pages under /{org}/-/, org mark on repository pages 27 days ago
orgrender_test.go httpd: org autolinks stop before trailing punctuation 28 days ago
orgweb.go web, wiki: create and rename an organization from the browser 1 month ago
page.go web: search across the instance, and a page for the inbox 1 month ago
pages.go pages: a directory redirect cannot leave the site 1 month ago
pagesredirect_test.go pages: a directory redirect cannot leave the site 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, wiki: delete a release from the browser 1 month ago
routes.go web: create, edit and delete snippets 27 days ago
routes_test.go httpd: open registration in the checkOrigin route walk so POST /register is reachable 1 month ago
searchweb.go web: search across the instance, and a page for the inbox 1 month ago
settings.go httpd: attach and detach runners on the settings page 1 month ago
smart.go exec: the two smart-HTTP spawns the sweep missed 1 month ago
snippets.go httpd: budget snippet rendering, fix a dead branch and a lost failure flash 27 days ago
web.go httpd: budget snippet rendering, fix a dead branch and a lost failure flash 27 days ago
wiki.go wiki: fix raw-route climb-out, filter HasWiki by extension, drop stale claims 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())
}