internal/httpd

273e9b95f8b87ee2be442c6bff6cbffbfd09cccf
gitbay/internal/httpd/
namelast commitupdated
account.go web: a stale session's mint or grant gets a sign-in link back to the form 1 month ago
account_test.go control: web mints and grants need a sign-in from the last 15 minutes 1 month ago
accounts.go web: new-issue form keeps milestone/assignee and a refused create 1 month ago
accounts_test.go web, store: final-review wording, header and range-diff fixes 1 month 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
admin_test.go httpd, gitd: pack generation takes a slot; ls-refs does not 1 month ago
adminusers.go web: a stale session's mint or grant gets a sign-in link back to the form 1 month ago
anchors_test.go httpd, gitd: pack generation takes a slot; ls-refs does not 1 month ago
api.go control: expiring credentials cannot run credential-minting commands 1 month 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 build log --follow: the follow says a restart ended it 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 web: builds page offers an org-mode badge snippet 1 month ago
builds.go web: step fold shows the step's first line; changelog: deploy order 1 month ago
builds_test.go builds: separate runs queued at different times 2 months ago
checkorigin_test.go httpd, gitd: pack generation takes a slot; ls-refs does not 1 month ago
chip_test.go httpd, web: a chip tone per scheme, and an underline where a link shares its line 2 months ago
clientip_test.go httpd, gitd: pack generation takes a slot; ls-refs does not 1 month ago
compare.go web: fork owners can open an MR from mrs and compare pages 1 month ago
compress.go build log --follow: end a queued follow, stream only on GET 2 months ago
compress_test.go httpd: gzipWriter passes a flush through 2 months ago
confirm.go web: resolve a snippet before checking its confirmation 2 months ago
control.go web: a stale session's mint or grant gets a sign-in link back to the form 1 month 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
dashpins.go web: the dashboard is three columns 2 months ago
dashpins_test.go control: move the feed-line sentence renderer from httpd, so the CLI can share it 1 month 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
emptystates_test.go web: drop CLI commands from four empty states, per the register 1 month ago
explorepage_test.go explore: name the filter, its count and a way out 2 months ago
facets.go web: a facet with nothing under it leaves the column 2 months ago
facets_test.go web: a facet with nothing under it leaves the column 2 months ago
facts.go store, control: build list gets --ref, --status and --job filters 2 months ago
filenav.go web: a file navigator beside blob, blame and edit 2 months ago
filenav_test.go web: a file navigator beside blob, blame and edit 2 months ago
filters.go issue list and mr list: filter by label, assignee, author, milestone 2 months ago
flash.go httpd: the login return path refuses a leading /\ 1 month ago
flash_test.go httpd: the login return path refuses a leading /\ 1 month ago
fonts_test.go httpd, gitd: pack generation takes a slot; ls-refs does not 1 month 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
issuecreate_test.go tests: token and issue-form tests pass a nil pack limiter to New 1 month 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: refuse an upload token once its repository is archived 1 month ago
lfsauth_test.go lfs: refuse an upload token once its repository is archived 1 month ago
logincookie_test.go tests: newer callers pass a nil pack limiter to New 1 month ago
logindisabled_test.go httpd, gitd: pack generation takes a slot; ls-refs does not 1 month ago
mractions.go web: fork owners can open an MR from mrs and compare pages 1 month ago
mrpage_test.go web: Discussion heading before the comment thread on issue and MR pages 1 month ago
mrrangediff.go web, store: final-review wording, header and range-diff fixes 1 month ago
mrrangediff_test.go web, store: final-review wording, header and range-diff fixes 1 month ago
mrslist_test.go web: fork owners can open an MR from mrs and compare pages 1 month ago
mrsrow_test.go web: fork owners can open an MR from mrs and compare pages 1 month ago
notfound_test.go web: the ten findings from the forge comparison 2 months ago
notifyweb.go web: pin and watch toggles dispatch through repo pin/watch/mute/unwatch 1 month ago
orglabels.go web: org label and milestone pages under /{org}/-/, org mark on repository pages 2 months ago
orgrender_test.go web: the ten findings from the forge comparison 2 months ago
orgweb.go web: interface guidelines fixes 2 months ago
ownerpage_test.go control: move the feed-line sentence renderer from httpd, so the CLI can share it 1 month ago
packlimit_test.go httpd: web archives take a pack slot 1 month ago
page.go httpd: drop the rail's dead pinned-repo loop 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
preview.go web: preview markup before writing it 2 months ago
railanon_test.go web: Explore is a rail square at every width 2 months ago
readme.go web: the owner page dispatches profile show 2 months ago
reauth_test.go web: a stale session's mint or grant gets a sign-in link back to the form 1 month ago
releaseactions.go web: preview markup before writing it 2 months ago
rendered.go web: the ten findings from the forge comparison 2 months ago
rendered_test.go web: the ten findings from the forge comparison 2 months ago
repohead_test.go web: watch button names all three states, muted included 1 month ago
routes.go web: range-diff page for a merge request's revisions 1 month ago
routes_test.go httpd, gitd: pack generation takes a slot; ls-refs does not 1 month ago
searchweb.go web: the rail is an icon column 2 months ago
searchweb_test.go web: search scope caption is permanent; document the tab zero-count rule 1 month ago
settings.go web: a stale session's mint or grant gets a sign-in link back to the form 1 month ago
sidecol_test.go web: the side column is a disclosure below 62rem 2 months ago
smart.go packlimit: log refusals once a minute per transport 1 month ago
snippets.go profile: reorder the tabs, and fold bookmarks and snippets in 2 months ago
theme.go web: the ten findings from the forge comparison 2 months ago
theme_test.go web: the ten findings from the forge comparison 2 months ago
topics.go web: topic and kind columns on explore and search 2 months ago
topics_test.go web: topic and kind columns on explore and search 2 months ago
trailingslash_test.go web: a trailing-slash redirect cannot leave the site 2 months ago
web.go web: a stale session's mint or grant gets a sign-in link back to the form 1 month ago
wiki.go wiki: a link to an existing non-page file resolves to _raw, not the page route 1 month ago
wiki_test.go wiki: a link to an existing non-page file resolves to _raw, not the page route 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())
}