-
Notifications
You must be signed in to change notification settings - Fork 1
/
unit.go
322 lines (280 loc) · 11.2 KB
/
unit.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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
// Copyright 2020 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package frontend
import (
"context"
"encoding/json"
"errors"
"fmt"
"net/http"
"strings"
"time"
"github.com/google/safehtml"
"github.com/google/safehtml/uncheckedconversions"
"github.com/khulnasoft-lab/godep/internal"
"github.com/khulnasoft-lab/godep/internal/cookie"
"github.com/khulnasoft-lab/godep/internal/derrors"
"github.com/khulnasoft-lab/godep/internal/frontend/page"
"github.com/khulnasoft-lab/godep/internal/frontend/serrors"
"github.com/khulnasoft-lab/godep/internal/frontend/urlinfo"
"github.com/khulnasoft-lab/godep/internal/frontend/versions"
"github.com/khulnasoft-lab/godep/internal/log"
"github.com/khulnasoft-lab/godep/internal/middleware/stats"
"github.com/khulnasoft-lab/godep/internal/stdlib"
"github.com/khulnasoft-lab/godep/internal/version"
"github.com/khulnasoft-lab/godep/internal/vuln"
)
// UnitPage contains data needed to render the unit template.
type UnitPage struct {
page.BasePage
// Unit is the unit for this page.
Unit *internal.UnitMeta
// Breadcrumb contains data used to render breadcrumb UI elements.
Breadcrumb breadcrumb
// Title is the title of the page.
Title string
// URLPath is the path suitable for links on the page.
// See the unitURLPath for details.
URLPath string
// CanonicalURLPath is a permanent representation of the URL path for a
// unit.
// It uses the resolved module path and version.
// For example, if the latest version of /my.module/pkg is version v1.5.2,
// the canonical URL path for that unit would be /my.module@v1.5.2/pkg
CanonicalURLPath string
// The version string formatted for display.
DisplayVersion string
// LinkVersion is version string suitable for links used to compute
// latest badges.
LinkVersion string
// LatestURL is a url pointing to the latest version of a unit.
LatestURL string
// IsLatestMinor is true if the version displayed is the latest minor of the unit.
// Used to determine the canonical URL for search engines and robots meta directives.
IsLatestMinor bool
// LatestMinorClass is the CSS class that describes the current unit's minor
// version in relationship to the latest version of the unit.
LatestMinorClass string
// Information about the latest major version of the module.
LatestMajorVersion string
LatestMajorVersionURL string
// PageType is the type of page (pkg, cmd, dir, std, or mod).
PageType string
// PageLabels are the labels that will be displayed
// for a given page.
PageLabels []string
// CanShowDetails indicates whether details can be shown or must be
// hidden due to issues like license restrictions.
CanShowDetails bool
// Settings contains settings for the selected tab.
SelectedTab TabSettings
// RedirectedFromPath is the path that redirected to the current page.
// If non-empty, a "redirected from" banner will be displayed
// (see static/frontend/unit/_header.tmpl).
RedirectedFromPath string
// Details contains data specific to the type of page being rendered.
Details any
// Vulns holds vulnerability information.
Vulns []vuln.Vuln
// DepsDevURL holds the full URL to this module version on deps.dev.
DepsDevURL string
// IsGoProject is true if the package is from the standard library or a
// golang.org sub-repository.
IsGoProject bool
}
// serveUnitPage serves a unit page for a path.
func (s *Server) serveUnitPage(ctx context.Context, w http.ResponseWriter, r *http.Request,
ds internal.DataSource, info *urlinfo.URLPathInfo) (err error) {
defer derrors.Wrap(&err, "serveUnitPage(ctx, w, r, ds, %v)", info)
defer stats.Elapsed(ctx, "serveUnitPage")()
tab := r.FormValue("tab")
if tab == "" {
// Default to details tab when there is no tab param.
tab = tabMain
}
// Redirect to clean URL path when tab param is invalid.
if _, ok := unitTabLookup[tab]; !ok {
http.Redirect(w, r, r.URL.Path, http.StatusFound)
return nil
}
um, err := ds.GetUnitMeta(ctx, info.FullPath, info.ModulePath, info.RequestedVersion)
if err != nil {
if !errors.Is(err, derrors.NotFound) {
return err
}
db, ok := ds.(internal.PostgresDB)
if !ok || s.fetchServer == nil {
return serrors.DatasourceNotSupportedError()
}
return s.fetchServer.ServePathNotFoundPage(w, r, db, info.FullPath, info.ModulePath, info.RequestedVersion)
}
makeDepsDevURL := depsDevURLGenerator(ctx, s.depsDevHTTPClient, um)
// Use GOOS and GOARCH query parameters to create a build context, which
// affects the documentation and synopsis. Omitting both results in an empty
// build context, which will match the first (and preferred) build context.
// It's also okay to provide just one (e.g. GOOS=windows), which will select
// the first doc with that value, ignoring the other one.
bc := internal.BuildContext{GOOS: r.FormValue("GOOS"), GOARCH: r.FormValue("GOARCH")}
d, err := fetchDetailsForUnit(ctx, r, tab, ds, um, info.RequestedVersion, bc, s.vulnClient)
if err != nil {
return err
}
if s.shouldServeJSON(r) {
return s.serveJSONPage(w, r, d)
}
if _, ok := internal.DefaultBranches[info.RequestedVersion]; ok {
// Since path@master is a moving target, we don't want it to be stale.
// As a result, we enqueue every request of path@master to the frontend
// task queue, which will initiate a fetch request depending on the
// last time we tried to fetch this module version.
//
// Use a separate context here to prevent the context from being canceled
// elsewhere before a task is enqueued.
if s.queue == nil {
return &serrors.ServerError{
Status: http.StatusBadRequest,
Err: err,
Epage: &page.ErrorPage{
MessageData: fmt.Sprintf(`Default branches like "@%s" are not supported. Omit to get the current version.`,
info.RequestedVersion),
},
}
}
go func() {
ctx, cancel := context.WithTimeout(context.Background(), 1*time.Minute)
defer cancel()
log.Infof(ctx, "serveUnitPage: Scheduling %q@%q to be fetched", um.ModulePath, info.RequestedVersion)
if _, err := s.queue.ScheduleFetch(ctx, um.ModulePath, info.RequestedVersion, nil); err != nil {
log.Errorf(ctx, "serveUnitPage(%q): scheduling fetch for %q@%q: %v",
r.URL.Path, um.ModulePath, info.RequestedVersion, err)
}
}()
}
if !isValidTabForUnit(tab, um, d) {
// Redirect to clean URL path when tab param is invalid for the unit
// type.
http.Redirect(w, r, r.URL.Path, http.StatusFound)
return nil
}
// If we've already called GetUnitMeta for an unknown module path and the latest version, pass
// it to GetLatestInfo to avoid a redundant call.
var latestUnitMeta *internal.UnitMeta
if info.ModulePath == internal.UnknownModulePath && info.RequestedVersion == version.Latest {
latestUnitMeta = um
}
latestInfo := s.GetLatestInfo(ctx, um.Path, um.ModulePath, latestUnitMeta)
var redirectPath string
redirectPath, err = cookie.Extract(w, r, cookie.AlternativeModuleFlash)
if err != nil {
// Don't fail, but don't display a banner either.
log.Errorf(ctx, "extracting AlternativeModuleFlash cookie: %v", err)
}
title := pageTitle(um)
basePage := s.newBasePage(r, title)
tabSettings := unitTabLookup[tab]
basePage.AllowWideContent = true
if tabSettings.Name == "" {
basePage.UseResponsiveLayout = true
}
lv := versions.LinkVersion(um.ModulePath, info.RequestedVersion, um.Version)
page := UnitPage{
BasePage: basePage,
Unit: um,
Breadcrumb: displayBreadcrumb(um, info.RequestedVersion),
Title: title,
SelectedTab: tabSettings,
URLPath: versions.ConstructUnitURL(um.Path, um.ModulePath, info.RequestedVersion),
CanonicalURLPath: canonicalURLPath(um.Path, um.ModulePath, info.RequestedVersion, um.Version),
DisplayVersion: versions.DisplayVersion(um.ModulePath, info.RequestedVersion, um.Version),
LinkVersion: lv,
LatestURL: versions.ConstructUnitURL(um.Path, um.ModulePath, version.Latest),
LatestMinorClass: latestMinorClass(lv, latestInfo),
LatestMajorVersionURL: latestInfo.MajorUnitPath,
PageLabels: pageLabels(um),
PageType: pageType(um),
RedirectedFromPath: redirectPath,
DepsDevURL: makeDepsDevURL(),
IsGoProject: isGoProject(um.ModulePath),
IsLatestMinor: lv == latestInfo.MinorVersion,
}
// Show the banner if there was no error getting the latest major version,
// and it is different from the major version of the current module path.
latestMajor := internal.MajorVersionForModule(latestInfo.MajorModulePath)
if latestMajor != "" && latestMajor != internal.MajorVersionForModule(um.ModulePath) {
page.LatestMajorVersion = latestMajor
}
page.Details = d
main, ok := d.(*MainDetails)
if ok {
page.MetaDescription = metaDescription(main.DocSynopsis)
}
// Get vulnerability information.
page.Vulns = vuln.VulnsForPackage(ctx, um.ModulePath, um.Version, um.Path, s.vulnClient)
s.servePage(ctx, w, tabSettings.TemplateName, page)
return nil
}
func (s *Server) shouldServeJSON(r *http.Request) bool {
return s.serveStats && r.FormValue("content") == "json"
}
func (s *Server) serveJSONPage(w http.ResponseWriter, r *http.Request, d any) (err error) {
defer derrors.Wrap(&err, "serveJSONPage(ctx, w, r)")
if !s.shouldServeJSON(r) {
return derrors.NotFound
}
data, err := json.Marshal(d)
if err != nil {
return fmt.Errorf("json.Marshal: %v", err)
}
if _, err := w.Write(data); err != nil {
return fmt.Errorf("w.Write: %v", err)
}
return nil
}
func latestMinorClass(version string, latest internal.LatestInfo) string {
c := "DetailsHeader-badge"
switch {
case latest.MinorVersion == "":
c += "--unknown"
case latest.MinorVersion == version && !latest.UnitExistsAtMinor:
c += "--notAtLatest"
case latest.MinorVersion == version:
c += "--latest"
default:
c += "--goToLatest"
}
return c
}
// metaDescription uses a safehtml escape hatch to build HTML used
// to render the <meta name="Description"> for unit pages as a
// workaround for https://github.com/google/safehtml/issues/6.
func metaDescription(synopsis string) safehtml.HTML {
if synopsis == "" {
return safehtml.HTML{}
}
return safehtml.HTMLConcat(
uncheckedconversions.HTMLFromStringKnownToSatisfyTypeContract(`<meta name="Description" content="`),
safehtml.HTMLEscaped(synopsis),
uncheckedconversions.HTMLFromStringKnownToSatisfyTypeContract(`">`),
)
}
// isValidTabForUnit reports whether the tab is valid for the given unit.
// It is assumed that tab is a key in unitTabLookup.
func isValidTabForUnit(tab string, um *internal.UnitMeta, details any) bool {
if tab == tabLicenses && !(details.(*LicensesDetails).IsRedistributable) {
return false
}
if !um.IsPackage() && (tab == tabImports || tab == tabImportedBy) {
return false
}
return true
}
// canonicalURLPath constructs a URL path to the unit that always includes the
// resolved version.
func canonicalURLPath(fullPath, modulePath, requestedVersion, resolvedVersion string) string {
return versions.ConstructUnitURL(fullPath, modulePath,
versions.LinkVersion(modulePath, requestedVersion, resolvedVersion))
}
func isGoProject(modulePath string) bool {
return modulePath == stdlib.ModulePath || strings.HasPrefix(modulePath, "golang.org")
}