internal/httpd/pages.go
110 lines · 3581 bytes
1package httpd
2
3import (
4 "mime"
5 "net"
6 "net/http"
7 "path"
8 "strings"
9
10 "gitbay.org/gitbay/internal/control"
11 "gitbay.org/gitbay/internal/gitutil"
12 "gitbay.org/gitbay/internal/store"
13)
14
15// PagesBranch is the branch a repo publishes as its static site.
16const PagesBranch = "refs/heads/pages"
17
18// pagesRouter sends <owner>.<domain> requests to the pages server and
19// everything else to the forge. Pages responses deliberately bypass the
20// forge's security headers: sites need their own scripts, and they run on
21// a separate origin where the forge has no cookies to protect.
22func (s *Server) pagesRouter(forge http.Handler) http.Handler {
23 domain := s.cfg.Pages.Domain
24 return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
25 host := hostOnly(r.Host)
26 if host == domain || strings.HasSuffix(host, "."+domain) {
27 s.servePage(w, r, host)
28 return
29 }
30 forge.ServeHTTP(w, r)
31 })
32}
33
34func hostOnly(hostport string) string {
35 if h, _, err := net.SplitHostPort(hostport); err == nil {
36 return h
37 }
38 return hostport
39}
40
41// servePage maps <owner>.<domain>/<repo>/<path> to the repo's pages
42// branch, and <owner>.<domain>/<path> to the owner's repo named "pages".
43// Private repos and missing branches are plain 404s.
44func (s *Server) servePage(w http.ResponseWriter, r *http.Request, host string) {
45 if r.Method != http.MethodGet && r.Method != http.MethodHead {
46 http.Error(w, "method not allowed", http.StatusMethodNotAllowed)
47 return
48 }
49 owner, ok := strings.CutSuffix(host, "."+s.cfg.Pages.Domain)
50 if !ok || owner == "" || strings.Contains(owner, ".") {
51 http.NotFound(w, r)
52 return
53 }
54 reqPath := strings.TrimPrefix(path.Clean("/"+r.URL.Path), "/")
55
56 // A first segment naming a public repo with a pages branch wins;
57 // everything else falls through to the owner's "pages" repo.
58 if seg, rest, _ := strings.Cut(reqPath, "/"); seg != "" && seg != "pages" {
59 if repo, err := s.st.RepoByPath(owner + "/" + seg); err == nil && repo.Visibility == "public" {
60 dir := control.RepoDir(s.cfg.Server.Root, repo.OwnerName, repo.Name)
61 if _, err := gitutil.ResolveRef(dir, PagesBranch); err == nil {
62 if rest == "" && !strings.HasSuffix(r.URL.Path, "/") {
63 http.Redirect(w, r, r.URL.Path+"/", http.StatusMovedPermanently)
64 return
65 }
66 s.servePageFile(w, r, repo, rest)
67 return
68 }
69 }
70 }
71 repo, err := s.st.RepoByPath(owner + "/pages")
72 if err != nil || repo.Visibility != "public" {
73 http.NotFound(w, r)
74 return
75 }
76 s.servePageFile(w, r, repo, reqPath)
77}
78
79func (s *Server) servePageFile(w http.ResponseWriter, r *http.Request, repo store.Repo, filePath string) {
80 dir := control.RepoDir(s.cfg.Server.Root, repo.OwnerName, repo.Name)
81 if filePath == "" {
82 filePath = "index.html"
83 }
84 data, err := gitutil.ReadBlob(dir, PagesBranch, filePath, s.cfg.Limits.MaxBlobBytes)
85 if err != nil {
86 // A directory path serves its index.html; /guide -> /guide/ keeps
87 // relative links working.
88 if idx, ierr := gitutil.ReadBlob(dir, PagesBranch, filePath+"/index.html", s.cfg.Limits.MaxBlobBytes); ierr == nil {
89 if !strings.HasSuffix(r.URL.Path, "/") {
90 http.Redirect(w, r, r.URL.Path+"/", http.StatusMovedPermanently)
91 return
92 }
93 data, filePath = idx, filePath+"/index.html"
94 } else {
95 http.NotFound(w, r)
96 return
97 }
98 }
99 ct := mime.TypeByExtension(path.Ext(filePath))
100 if ct == "" {
101 ct = http.DetectContentType(data)
102 }
103 w.Header().Set("Content-Type", ct)
104 w.Header().Set("X-Content-Type-Options", "nosniff")
105 w.Header().Set("Cache-Control", "public, max-age=60")
106 if r.Method == http.MethodHead {
107 return
108 }
109 w.Write(data)
110}