internal/httpd/routes.go

35cb93f6f17f984193c93df7ff9278f5bb662ffa
gitbay/internal/httpd/routes.go history · blame · raw

133 lines · 6120 bytes

  1package httpd
  2
  3import (
  4	"fmt"
  5	"net"
  6	"net/http"
  7	"strings"
  8)
  9
 10// Route is one entry in the explicit route table. The view-only guarantee is
 11// structural: Handler() consults web.mode when building the table, and the
 12// route test asserts no mutating route exists in view_only mode.
 13type Route struct {
 14	Method  string
 15	Pattern string // without method prefix
 16	// Mutating marks routes that can change server state. The git transport
 17	// POSTs are not mutating: upload-pack is a pure read, and the
 18	// receive-pack endpoint is a static refusal that writes nothing.
 19	Mutating bool
 20	Handler  http.HandlerFunc
 21}
 22
 23// Routes returns the route table for the configured web.mode.
 24func (s *Server) Routes() []Route {
 25	// Git smart transport (anonymous, public repos only).
 26	routes := []Route{
 27		{Method: "GET", Pattern: "/{owner}/{repo}/info/refs", Handler: s.infoRefs},
 28		{Method: "POST", Pattern: "/{owner}/{repo}/git-upload-pack", Handler: s.uploadPack},
 29		{Method: "POST", Pattern: "/{owner}/{repo}/git-receive-pack", Handler: s.receivePackRefusal},
 30	}
 31
 32	// Web UI, read-only. These exist in every mode.
 33	routes = append(routes,
 34		Route{Method: "GET", Pattern: "/{$}", Handler: s.index},
 35		Route{Method: "GET", Pattern: "/explore", Handler: s.explore},
 36		Route{Method: "GET", Pattern: "/privacy", Handler: s.privacy},
 37		Route{Method: "GET", Pattern: "/static/style.css", Handler: s.stylesheet},
 38		Route{Method: "GET", Pattern: "/favicon.svg", Handler: s.favicon},
 39		Route{Method: "GET", Pattern: "/{owner}", Handler: s.ownerPage},
 40		Route{Method: "GET", Pattern: "/{owner}/{repo}", Handler: s.repoHome},
 41		Route{Method: "GET", Pattern: "/{owner}/{repo}/tree/{ref}/{path...}", Handler: s.tree},
 42		Route{Method: "GET", Pattern: "/{owner}/{repo}/blob/{ref}/{path...}", Handler: s.blob},
 43		Route{Method: "GET", Pattern: "/{owner}/{repo}/blame/{ref}/{path...}", Handler: s.blame},
 44		Route{Method: "GET", Pattern: "/{owner}/{repo}/search", Handler: s.search},
 45		Route{Method: "GET", Pattern: "/{owner}/{repo}/milestones", Handler: s.milestones},
 46		Route{Method: "GET", Pattern: "/{owner}/{repo}/releases", Handler: s.releases},
 47		Route{Method: "GET", Pattern: "/{owner}/{repo}/releases/download/{tag}/{name}", Handler: s.releaseAsset},
 48		Route{Method: "GET", Pattern: "/{owner}/{repo}/raw/{ref}/{path...}", Handler: s.raw},
 49		Route{Method: "GET", Pattern: "/{owner}/{repo}/log", Handler: s.log},
 50		Route{Method: "GET", Pattern: "/{owner}/{repo}/log/{ref}", Handler: s.log},
 51		Route{Method: "GET", Pattern: "/{owner}/{repo}/commit/{sha}", Handler: s.commit},
 52		Route{Method: "GET", Pattern: "/{owner}/{repo}/refs", Handler: s.refs},
 53		Route{Method: "GET", Pattern: "/{owner}/{repo}/archive/{file}", Handler: s.archive},
 54		Route{Method: "GET", Pattern: "/{owner}/{repo}/issues", Handler: s.issues},
 55		Route{Method: "GET", Pattern: "/{owner}/{repo}/issues/{n}", Handler: s.issue},
 56		Route{Method: "GET", Pattern: "/{owner}/{repo}/mrs", Handler: s.mrs},
 57		Route{Method: "GET", Pattern: "/{owner}/{repo}/mrs/{n}", Handler: s.mr},
 58	)
 59
 60	// The JSON API is its own opt-in surface, independent of web.mode.
 61	if s.cfg.API.Enabled {
 62		routes = append(routes,
 63			Route{Method: "POST", Pattern: "/api/v1/cmd", Mutating: true, Handler: s.apiCmd},
 64		)
 65	}
 66
 67	// Account-mode routes exist only when web.mode = "accounts". In
 68	// view_only they are never registered — the structural guarantee.
 69	if s.cfg.Web.Mode == "accounts" {
 70		routes = append(routes,
 71			Route{Method: "GET", Pattern: "/login", Handler: s.login, Mutating: true}, // consumes a one-time token
 72			Route{Method: "POST", Pattern: "/logout", Mutating: true,
 73				Handler: s.checkOrigin(s.logout)},
 74			Route{Method: "GET", Pattern: "/new", Handler: s.requireUser(s.newRepoForm)},
 75		)
 76		// Web signup fronts the same registration path as SSH register,
 77		// so it exists only when registration is open or invite.
 78		if s.cfg.Registration.Mode != "closed" {
 79			routes = append(routes,
 80				Route{Method: "GET", Pattern: "/register", Handler: s.signupForm},
 81				Route{Method: "POST", Pattern: "/register", Mutating: true,
 82					Handler: s.checkOrigin(s.signupSubmit)},
 83			)
 84		}
 85		routes = append(routes,
 86			Route{Method: "POST", Pattern: "/new", Mutating: true,
 87				Handler: s.checkOrigin(s.requireUser(s.newRepoSubmit))},
 88			Route{Method: "GET", Pattern: "/{owner}/{repo}/issues/new",
 89				Handler: s.requireUser(s.issueCreateForm)},
 90			Route{Method: "POST", Pattern: "/{owner}/{repo}/issues/new", Mutating: true,
 91				Handler: s.checkOrigin(s.requireUser(s.issueCreateSubmit))},
 92			Route{Method: "POST", Pattern: "/{owner}/{repo}/issues/{n}/comment", Mutating: true,
 93				Handler: s.checkOrigin(s.requireUser(s.issueCommentSubmit))},
 94			Route{Method: "POST", Pattern: "/{owner}/{repo}/mrs/{n}/comment", Mutating: true,
 95				Handler: s.checkOrigin(s.requireUser(s.mrCommentSubmit))},
 96			Route{Method: "GET", Pattern: "/{owner}/{repo}/edit/{ref}/{path...}",
 97				Handler: s.requireUser(s.editForm)},
 98			Route{Method: "POST", Pattern: "/{owner}/{repo}/edit/{ref}/{path...}", Mutating: true,
 99				Handler: s.checkOrigin(s.requireUser(s.editSubmit))},
100		)
101	}
102	return routes
103}
104
105func (s *Server) Handler() http.Handler {
106	mux := http.NewServeMux()
107	for _, r := range s.Routes() {
108		mux.HandleFunc(r.Method+" "+r.Pattern, r.Handler)
109	}
110	if len(s.cfg.GoImport) == 0 {
111		return mux
112	}
113	// Vanity Go modules: ?go-get=1 requests under a configured module
114	// path answer with the go-import meta tag before normal routing.
115	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
116		if r.URL.Query().Get("go-get") == "1" {
117			host := r.Host
118			if h, _, err := net.SplitHostPort(host); err == nil {
119				host = h
120			}
121			requested := host + strings.TrimSuffix(r.URL.Path, "/")
122			for module, repo := range s.cfg.GoImport {
123				if requested == module || strings.HasPrefix(requested, module+"/") {
124					w.Header().Set("Content-Type", "text/html; charset=utf-8")
125					fmt.Fprintf(w, `<!DOCTYPE html><html><head><meta name="go-import" content="%s git %s/%s.git"></head><body>%s</body></html>`,
126						module, s.cfg.Server.SiteURL, repo, module)
127					return
128				}
129			}
130		}
131		mux.ServeHTTP(w, r)
132	})
133}