-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathhome.go
170 lines (150 loc) · 4.14 KB
/
home.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
package main
import (
"fmt"
"html"
"log/slog"
"net/http"
"os"
"strings"
"time"
)
func readInstanceNotes() string {
if config.InstanceNotesFile == "" {
return ""
}
buf, err := os.ReadFile(config.InstanceNotesFile)
if err != nil {
slog.Error("reading instance notes failed, skipping", "err", err)
}
return html.EscapeString(string(buf))
}
func serveHome(w http.ResponseWriter, r *http.Request) {
defer observePage("home", time.Now())
if r.URL.Path == "/" {
if r.Method != "GET" {
http.Error(w, "405 - Method Not Allowed", http.StatusMethodNotAllowed)
return
}
m := r.FormValue("m")
if m != "" {
http.Redirect(w, r, "/"+m, http.StatusTemporaryRedirect)
return
}
recentBuilds.Lock()
recentLinks := append([]string{}, recentBuilds.links...)
recentBuilds.Unlock()
// Reverse order for recentLinks most recent first.
n := len(recentLinks)
for i := 0; i < n/2; i++ {
j := n - 1 - i
recentLinks[i], recentLinks[j] = recentLinks[j], recentLinks[i]
}
var args = struct {
Favicon string
Recents []string
VerifierKey string
GobuildVersion string
GobuildPlatform string
InstanceNotes string
}{
"favicon.ico",
recentLinks,
config.VerifierKey,
gobuildVersion,
gobuildPlatform,
readInstanceNotes(),
}
if err := homeTemplate.Execute(w, args); err != nil {
failf(w, "%w: executing template: %v", errServer, err)
}
return
}
t := strings.Split(r.URL.Path[1:], "/")
if !strings.Contains(t[0], ".") {
http.NotFound(w, r)
return
}
if !strings.Contains(r.URL.Path, "@") {
if r.Method != "GET" {
http.Error(w, "405 - Method Not Allowed", http.StatusMethodNotAllowed)
return
}
if !checkAllowedRespond(w, r.URL.Path[1:]) {
return
}
serveModules(w, r)
return
}
// If last part contains an "@" and no part before it does, and last part doesn't
// have a slash, we'll assume a path like /github.com/mjl-/[email protected] and
// redirect to a path with guessed goos/goarch and latest goversion.
if mod, version, _ := strings.Cut(r.URL.Path[1:], "@"); mod != "" && version != "" && !strings.Contains(version, "@") && !strings.Contains(version, "/") {
goversion, err := ensureMostRecentSDK()
if err != nil {
http.Error(w, "500 - Internal Server Error - "+err.Error(), http.StatusInternalServerError)
return
}
// Resolve module version. Could be a git hash.
info, err := resolveModuleVersion(r.Context(), mod, version)
if err != nil {
http.Error(w, "400 - bad request - resolving module version: "+err.Error(), http.StatusBadRequest)
return
}
version = info.Version
goos, goarch := autodetectTarget(r)
bs := buildSpec{mod, version, "/", goos, goarch, goversion.String(), false}
req := request{bs, "", pageIndex}
http.Redirect(w, r, req.link(), http.StatusTemporaryRedirect)
return
}
req, hint, ok := parseRequest(r.URL.Path)
if !ok {
if hint != "" {
http.Error(w, fmt.Sprintf("404 - File Not Found\n\n%s\n", hint), http.StatusNotFound)
} else {
http.NotFound(w, r)
}
return
}
if req.Page != pageRetry && r.Method != "GET" || req.Page == pageRetry && r.Method != "POST" {
http.Error(w, "405 - Method Not Allowed", http.StatusMethodNotAllowed)
return
}
if !checkAllowedRespond(w, req.Mod) {
return
}
// Resolve module version. Could be a git hash.
info, err := resolveModuleVersion(r.Context(), req.Mod, req.Version)
if err != nil {
http.Error(w, "400 - bad request - resolving module version: "+err.Error(), http.StatusBadRequest)
return
}
if req.Version != info.Version {
req.Version = info.Version
http.Redirect(w, r, req.link(), http.StatusTemporaryRedirect)
req.link()
return
}
what := "build"
if req.Sum != "" {
what = "result"
}
defer observePage(what+" "+req.Page.String(), time.Now())
if req.Sum == "" {
serveBuild(w, r, req)
} else {
serveResult(w, r, req)
}
}
func checkAllowedRespond(w http.ResponseWriter, module string) bool {
if len(config.ModulePrefixes) == 0 {
return true
}
for _, prefix := range config.ModulePrefixes {
if strings.HasPrefix(module, prefix) {
return true
}
}
http.Error(w, "403 - Module path not allowed", http.StatusForbidden)
return false
}