internal/httpd

e5dee0c2a4a8b36de5250096240d4355060384ba
gitbay/internal/httpd/
namelast commitupdated
account.go web: resolve a snippet before checking its confirmation 2 months ago
accounts.go web: the editor answers 404 for a missing branch and names a new file 2 months ago
activity.go httpd, web: month labels on the contribution grid 2 months ago
activity_test.go httpd, web: month labels on the contribution grid 2 months ago
admin.go control: the payloads other surfaces decode are named types 2 months ago
anchors_test.go web: markdown headings carry ids, and the stylesheet revalidates 2 months ago
api.go http.trusted_proxies, and a cap on verification mail per account 2 months ago
apilimit.go http.trusted_proxies, and a cap on verification mail per account 2 months ago
apilimit_test.go httpd: rate limit the JSON API 3 months ago
apiread.go http.trusted_proxies, and a cap on verification mail per account 2 months ago
atom.go web, store, policy, wiki: Atom feeds for releases, commits, and an owner's activity 2 months ago
atom_test.go web, store, policy, wiki: Atom feeds for releases, commits, and an owner's activity 2 months ago
badge.go build cancel: withdraw a queued build 2 months ago
badgepng.go Serve build badges as PNG as well as SVG 2 months ago
badgepng_test.go Serve build badges as PNG as well as SVG 2 months ago
buildpages_test.go httpd, web: builds page filters and groups by commit 2 months ago
builds.go httpd: an unknown build status is never reported as success 2 months ago
builds_test.go httpd: an unknown build status is never reported as success 2 months ago
checkorigin_test.go httpd: open registration in the checkOrigin route walk so POST /register is reachable 2 months ago
chip_test.go web: a visible edge on dark-mode buttons, and label colours held legible 2 months ago
clientip_test.go http.trusted_proxies, and a cap on verification mail per account 2 months ago
compare.go web: one timestamp format, with the zone named 2 months ago
confirm.go web: resolve a snippet before checking its confirmation 2 months ago
control.go control: the payloads other surfaces decode are named types 2 months ago
cookieclear_test.go web: a suspended account cannot mail itself a session 2 months ago
crumbs_test.go web: directory crumbs on blob and blame pages link to the tree 2 months ago
depspage_test.go web: one timestamp format, with the zone named 2 months ago
diff.go web: keep an added line's leading dash in the diff 2 months ago
diff_test.go web: keep an added line's leading dash in the diff 2 months ago
facts.go store, control: build list gets --ref, --status and --job filters 2 months ago
feed.go dashboard: activity feed shows relative times with the exact time in a title 2 months ago
feed_test.go dashboard: activity feed shows relative times with the exact time in a title 2 months ago
filters.go issue list and mr list: filter by label, assignee, author, milestone 2 months ago
flash.go web: login returns to the page that needed it 2 months ago
fonts_test.go web: landing picture, light and dark 2 months ago
headings_test.go web: a rendered document's headings sit under the page's own 2 months ago
healthz.go http: /healthz 2 months ago
inputlabels_test.go web: the topics-remove field gets a label, and a test that would have caught it 2 months ago
issueactions.go web: a failed form action's reason rides a one-shot cookie, not the URL 2 months ago
labels.go web: the labels page hides an empty colour column 2 months ago
langs.go web: repository facts on the code page 3 months ago
lfs.go lfs: count it in stats, and gc --lfs for orphaned objects 2 months ago
logincookie_test.go web: a suspended account cannot mail itself a session 2 months ago
logindisabled_test.go httpd: pin the login() disabled guard and the checkOrigin invariant 2 months ago
mractions.go web: labels on the merge request page and list 2 months ago
mrpage_test.go control: admin mr prune drops MR head refs and prunes the repository 2 months ago
mrsrow_test.go web: the merge request list shows check state and comment count 2 months ago
notifyweb.go control: repo unwatch clears the row, repo mute silences 2 months ago
orglabels.go web: org label and milestone pages under /{org}/-/, org mark on repository pages 2 months ago
orgrender_test.go httpd: org autolinks stop before trailing punctuation 2 months ago
orgweb.go web: type the name to confirm a destructive control 2 months ago
page.go web: the rail is an icon column 2 months ago
pages.go pages: a directory redirect cannot leave the site 2 months ago
pagesredirect_test.go pages: a directory redirect cannot leave the site 2 months ago
palette_test.go web: rewrite the stylesheet on measured tokens 2 months ago
readme.go web: the owner page dispatches profile show 2 months ago
releaseactions.go web: type the name to confirm a destructive control 2 months ago
routes.go web: labels on the merge request page and list 2 months ago
routes_test.go httpd: open registration in the checkOrigin route walk so POST /register is reachable 2 months ago
searchweb.go web: the rail is an icon column 2 months ago
settings.go httpd: runner attach reports failure without dropping the key 2 months ago
smart.go exec: the two smart-HTTP spawns the sweep missed 2 months ago
snippets.go web: resolve a snippet before checking its confirmation 2 months ago
web.go web: labels on the merge request page and list 2 months ago
wiki.go wiki: fix raw-route climb-out, filter HasWiki by extension, drop stale claims 2 months 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())
}