internal/httpd/routes.go

c327fbdf04d6735772a5d661b271d68fe9e39404
gitbay/internal/httpd/routes.go history · blame · raw

132 lines · 6055 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: "/static/style.css", Handler: s.stylesheet},
 37		Route{Method: "GET", Pattern: "/favicon.svg", Handler: s.favicon},
 38		Route{Method: "GET", Pattern: "/{owner}", Handler: s.ownerPage},
 39		Route{Method: "GET", Pattern: "/{owner}/{repo}", Handler: s.repoHome},
 40		Route{Method: "GET", Pattern: "/{owner}/{repo}/tree/{ref}/{path...}", Handler: s.tree},
 41		Route{Method: "GET", Pattern: "/{owner}/{repo}/blob/{ref}/{path...}", Handler: s.blob},
 42		Route{Method: "GET", Pattern: "/{owner}/{repo}/blame/{ref}/{path...}", Handler: s.blame},
 43		Route{Method: "GET", Pattern: "/{owner}/{repo}/search", Handler: s.search},
 44		Route{Method: "GET", Pattern: "/{owner}/{repo}/milestones", Handler: s.milestones},
 45		Route{Method: "GET", Pattern: "/{owner}/{repo}/releases", Handler: s.releases},
 46		Route{Method: "GET", Pattern: "/{owner}/{repo}/releases/download/{tag}/{name}", Handler: s.releaseAsset},
 47		Route{Method: "GET", Pattern: "/{owner}/{repo}/raw/{ref}/{path...}", Handler: s.raw},
 48		Route{Method: "GET", Pattern: "/{owner}/{repo}/log", Handler: s.log},
 49		Route{Method: "GET", Pattern: "/{owner}/{repo}/log/{ref}", Handler: s.log},
 50		Route{Method: "GET", Pattern: "/{owner}/{repo}/commit/{sha}", Handler: s.commit},
 51		Route{Method: "GET", Pattern: "/{owner}/{repo}/refs", Handler: s.refs},
 52		Route{Method: "GET", Pattern: "/{owner}/{repo}/archive/{file}", Handler: s.archive},
 53		Route{Method: "GET", Pattern: "/{owner}/{repo}/issues", Handler: s.issues},
 54		Route{Method: "GET", Pattern: "/{owner}/{repo}/issues/{n}", Handler: s.issue},
 55		Route{Method: "GET", Pattern: "/{owner}/{repo}/mrs", Handler: s.mrs},
 56		Route{Method: "GET", Pattern: "/{owner}/{repo}/mrs/{n}", Handler: s.mr},
 57	)
 58
 59	// The JSON API is its own opt-in surface, independent of web.mode.
 60	if s.cfg.API.Enabled {
 61		routes = append(routes,
 62			Route{Method: "POST", Pattern: "/api/v1/cmd", Mutating: true, Handler: s.apiCmd},
 63		)
 64	}
 65
 66	// Account-mode routes exist only when web.mode = "accounts". In
 67	// view_only they are never registered — the structural guarantee.
 68	if s.cfg.Web.Mode == "accounts" {
 69		routes = append(routes,
 70			Route{Method: "GET", Pattern: "/login", Handler: s.login, Mutating: true}, // consumes a one-time token
 71			Route{Method: "POST", Pattern: "/logout", Mutating: true,
 72				Handler: s.checkOrigin(s.logout)},
 73			Route{Method: "GET", Pattern: "/new", Handler: s.requireUser(s.newRepoForm)},
 74		)
 75		// Web signup fronts the same registration path as SSH register,
 76		// so it exists only when registration is open or invite.
 77		if s.cfg.Registration.Mode != "closed" {
 78			routes = append(routes,
 79				Route{Method: "GET", Pattern: "/register", Handler: s.signupForm},
 80				Route{Method: "POST", Pattern: "/register", Mutating: true,
 81					Handler: s.checkOrigin(s.signupSubmit)},
 82			)
 83		}
 84		routes = append(routes,
 85			Route{Method: "POST", Pattern: "/new", Mutating: true,
 86				Handler: s.checkOrigin(s.requireUser(s.newRepoSubmit))},
 87			Route{Method: "GET", Pattern: "/{owner}/{repo}/issues/new",
 88				Handler: s.requireUser(s.issueCreateForm)},
 89			Route{Method: "POST", Pattern: "/{owner}/{repo}/issues/new", Mutating: true,
 90				Handler: s.checkOrigin(s.requireUser(s.issueCreateSubmit))},
 91			Route{Method: "POST", Pattern: "/{owner}/{repo}/issues/{n}/comment", Mutating: true,
 92				Handler: s.checkOrigin(s.requireUser(s.issueCommentSubmit))},
 93			Route{Method: "POST", Pattern: "/{owner}/{repo}/mrs/{n}/comment", Mutating: true,
 94				Handler: s.checkOrigin(s.requireUser(s.mrCommentSubmit))},
 95			Route{Method: "GET", Pattern: "/{owner}/{repo}/edit/{ref}/{path...}",
 96				Handler: s.requireUser(s.editForm)},
 97			Route{Method: "POST", Pattern: "/{owner}/{repo}/edit/{ref}/{path...}", Mutating: true,
 98				Handler: s.checkOrigin(s.requireUser(s.editSubmit))},
 99		)
100	}
101	return routes
102}
103
104func (s *Server) Handler() http.Handler {
105	mux := http.NewServeMux()
106	for _, r := range s.Routes() {
107		mux.HandleFunc(r.Method+" "+r.Pattern, r.Handler)
108	}
109	if len(s.cfg.GoImport) == 0 {
110		return mux
111	}
112	// Vanity Go modules: ?go-get=1 requests under a configured module
113	// path answer with the go-import meta tag before normal routing.
114	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
115		if r.URL.Query().Get("go-get") == "1" {
116			host := r.Host
117			if h, _, err := net.SplitHostPort(host); err == nil {
118				host = h
119			}
120			requested := host + strings.TrimSuffix(r.URL.Path, "/")
121			for module, repo := range s.cfg.GoImport {
122				if requested == module || strings.HasPrefix(requested, module+"/") {
123					w.Header().Set("Content-Type", "text/html; charset=utf-8")
124					fmt.Fprintf(w, `<!DOCTYPE html><html><head><meta name="go-import" content="%s git %s/%s.git"></head><body>%s</body></html>`,
125						module, s.cfg.Server.SiteURL, repo, module)
126					return
127				}
128			}
129		}
130		mux.ServeHTTP(w, r)
131	})
132}