/
redirect.go
479 lines (424 loc) · 11.8 KB
/
redirect.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
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
package redirect
import (
"fmt"
"io/ioutil"
"log"
"net/http"
"path/filepath"
"sort"
"strings"
pb "github.com/Debian/debiman/internal/proto"
"github.com/Debian/debiman/internal/tag"
"github.com/golang/protobuf/proto"
"golang.org/x/text/language"
)
type IndexEntry struct {
Name string // TODO: string pool
Suite string // TODO: enum to save space
Binarypkg string // TODO: sort by popcon, TODO: use a string pool
Section string // TODO: use a string pool
Language string // TODO: type: would it make sense to use language.Tag?
}
func (e IndexEntry) ServingPath(suffix string) string {
return "/" + e.Suite + "/" + e.Binarypkg + "/" + e.Name + "." + e.Section + "." + e.Language + suffix
}
type Index struct {
Entries map[string][]IndexEntry
Suites map[string]string
Langs map[string]bool
Sections map[string]bool
}
// TODO(later): the default suite should be the latest stable release
const defaultSuite = "buster"
const defaultLanguage = "en"
// bestLanguageMatch is like bestLanguageMatch in rendermanpage.go, but for the redirector index. TODO: can we de-duplicate the code?
func bestLanguageMatch(t []language.Tag, options []IndexEntry) IndexEntry {
// ensure that en comes first, so that language.Matcher treats it as default
if options[0].Language != "en" {
for i := 1; i < len(options); i++ {
if options[i].Language == "en" {
options = append([]IndexEntry{options[i]}, options...)
break
}
}
}
if t == nil {
return options[0]
}
tags := make([]language.Tag, len(options))
for idx, m := range options {
tag, err := tag.FromLocale(m.Language)
if err != nil {
panic(fmt.Sprintf("Cannot get language.Tag from locale %q: %v", m.Language, err))
}
tags[idx] = tag
}
matcher := language.NewMatcher(tags)
tag, _, _ := matcher.Match(t...)
for idx, t := range tags {
if t == tag {
return options[idx]
}
}
return options[0]
}
func (i Index) split(path string) (suite string, binarypkg string, name string, section string, lang string) {
dir := strings.TrimPrefix(filepath.Dir(path), "/")
base := strings.TrimSpace(filepath.Base(path))
base = strings.Replace(base, " ", ".", -1)
parts := strings.Split(dir, "/")
if len(parts) > 0 {
if len(parts) == 1 {
if _, ok := i.Suites[parts[0]]; ok {
suite = parts[0]
} else if i.Sections[parts[0]] {
// legacy manpages.debian.org
section = parts[0]
} else {
if i.Sections[base] {
// man.freebsd.org
section = base
base = parts[0]
} else {
binarypkg = parts[0]
}
}
} else if len(parts) == 2 && strings.HasPrefix(parts[1], "man") && i.Sections[strings.TrimPrefix(parts[1], "man")] {
// legacy manpages.debian.org
lang = parts[0]
section = strings.TrimPrefix(parts[1], "man")
} else if len(parts) == 2 {
suite = parts[0]
binarypkg = parts[1]
}
}
// the first part can contain dots, so we need to “split from the right”
parts = strings.Split(base, ".")
if len(parts) == 1 {
return suite, binarypkg, base, section, lang
}
// The last part can either be a language or a section
consumed := 0
if l := parts[len(parts)-1]; i.Langs[l] {
lang = l
consumed++
} else if l := parts[len(parts)-1]; i.Sections[l] {
section = l
consumed++
}
// The second to last part (if enough parts are present) can
// be a section (because the language was already specified).
if len(parts) > 1+consumed {
if s := parts[len(parts)-1-consumed]; i.Sections[s] {
section = s
consumed++
}
}
return suite,
binarypkg,
strings.Join(parts[:len(parts)-consumed], "."),
section,
lang
}
type byMainSection []IndexEntry
func (p byMainSection) Len() int { return len(p) }
func (p byMainSection) Swap(i, j int) { p[i], p[j] = p[j], p[i] }
func (p byMainSection) Less(i, j int) bool {
// Compare main sections first
mi := p[i].Section[:1]
mj := p[j].Section[:1]
if mi < mj {
return true
}
if mi > mj {
return false
}
return len(p[i].Section) > len(p[j].Section)
}
// Default taken from man(1):
var mansect = searchOrder(strings.Split("1 n l 8 3 2 3posix 3pm 3perl 3am 5 4 9 6 7", " "))
func searchOrder(sections []string) map[string]int {
order := make(map[string]int)
for idx, section := range sections {
order[section] = idx
}
return order
}
type bySection []IndexEntry
func (p bySection) Len() int { return len(p) }
func (p bySection) Swap(i, j int) { p[i], p[j] = p[j], p[i] }
func (p bySection) Less(i, j int) bool {
oI, okI := mansect[p[i].Section]
oJ, okJ := mansect[p[j].Section]
if okI && okJ { // both sections are in mansect
return oI < oJ
}
if !okI && okJ {
return false // sort all mansect sections before custom sections
}
if okI && !okJ {
return true // sort all mansect sections before custom sections
}
return p[i].Section < p[j].Section // neither are in mansect
}
func (i Index) Narrow(acceptLang string, template, ref IndexEntry, entries []IndexEntry) []IndexEntry {
t := template // for convenience
fullyQualified := func() bool {
if t.Suite == "" || t.Binarypkg == "" || t.Section == "" || t.Language == "" {
return false
}
// Verify validity
for _, e := range entries {
if t.Suite == e.Suite &&
t.Binarypkg == e.Binarypkg &&
t.Section == e.Section &&
t.Language == e.Language {
return true
}
}
return false
}
filtered := make([]IndexEntry, len(entries))
copy(filtered, entries)
filter := func(keep func(e IndexEntry) bool) {
tmp := filtered[:0]
for _, e := range filtered {
if !keep(e) {
continue
}
tmp = append(tmp, e)
}
filtered = tmp
}
if t.Language != "" {
// Verify the specified language is a valid choice
var found bool
for _, e := range filtered {
if e.Language == t.Language {
found = true
break
}
}
if !found {
t.Language = ""
}
}
if t.Suite != "" {
// Verify the specified suite is a valid choice
var found bool
for _, e := range filtered {
if e.Suite == t.Suite {
found = true
break
}
}
if !found {
t.Suite = ""
}
}
// Narrow down as much as possible upfront. The keep callback is
// the logical and of all the keep callbacks below:
filter(func(e IndexEntry) bool {
return (t.Suite == "" || e.Suite == t.Suite) &&
(t.Section == "" || e.Section[:1] == t.Section[:1]) &&
(t.Language == "" || e.Language == t.Language) &&
(t.Binarypkg == "" || e.Binarypkg == t.Binarypkg)
})
// suite
if t.Suite == "" {
// Prefer redirecting to the suite from the referrer
for _, e := range filtered {
if e.Suite == ref.Suite {
t.Suite = ref.Suite
break
}
}
// Default to defaultSuite
if t.Suite == "" {
for _, e := range filtered {
if e.Suite == defaultSuite {
t.Suite = defaultSuite
break
}
}
}
// If the manpage is not contained in defaultSuite, use the
// first suite we can find for which the manpage is available.
if t.Suite == "" {
for _, e := range filtered {
t.Suite = e.Suite
break
}
}
}
filter(func(e IndexEntry) bool { return t.Suite == "" || e.Suite == t.Suite })
if len(filtered) == 0 {
return nil
}
if fullyQualified() {
return filtered
}
// section
if len(t.Section) > 1 {
// A subsection was specified. Sort by section, but prefer
// subsections so that they get matched first (e.g. “3” will
// come after “3edit”).
sort.Stable(byMainSection(filtered))
} else {
// No subsection was specified. Sort by section so that
// subsections are matched later (e.g. “3edit” will come after
// “3”).
sort.Stable(bySection(filtered))
}
if t.Section == "" {
// TODO(later): respect the section preference cookie (+test)
t.Section = filtered[0].Section
}
filter(func(e IndexEntry) bool { return t.Section == "" || e.Section[:1] == t.Section[:1] })
if len(filtered) == 0 {
return nil
}
if fullyQualified() {
return filtered
}
// language
if t.Language == "" {
tags, _, _ := language.ParseAcceptLanguage(acceptLang)
// ignore err: tags == nil results in the default language
best := bestLanguageMatch(tags, filtered)
t.Language = best.Language
}
filter(func(e IndexEntry) bool { return t.Language == "" || e.Language == t.Language })
if len(filtered) == 0 {
return nil
}
if fullyQualified() {
return filtered
}
// binarypkg
if t.Binarypkg == "" {
t.Binarypkg = filtered[0].Binarypkg
}
filter(func(e IndexEntry) bool { return t.Binarypkg == "" || e.Binarypkg == t.Binarypkg })
if len(filtered) == 0 {
return nil
}
return filtered
}
type NotFoundError struct {
Manpage string
BestChoice IndexEntry
}
func (e *NotFoundError) Error() string {
return "No such man page"
}
func (i Index) Redirect(r *http.Request) (string, error) {
path := r.URL.Path
if strings.HasSuffix(path, "/") ||
strings.HasSuffix(path, "/index.html") ||
strings.HasPrefix(path, "/contents-") {
return "", &NotFoundError{}
}
suffix := ".html"
// If a raw manpage was requested, redirect to raw, not HTML
if strings.HasSuffix(path, ".gz") && !strings.HasSuffix(path, ".html.gz") {
suffix = ".gz"
}
for strings.HasSuffix(path, ".html") || strings.HasSuffix(path, ".gz") {
path = strings.TrimSuffix(path, ".gz")
path = strings.TrimSuffix(path, ".html")
}
// Parens are converted into dots, so that “i3(1)” becomes
// “i3.1.”. Trailing dots are stripped and two dots next to each
// other are converted into one.
path = strings.Replace(path, "(", ".", -1)
path = strings.Replace(path, ")", ".", -1)
path = strings.Replace(path, "..", ".", -1)
path = strings.TrimSuffix(path, ".")
var suite, binarypkg, name, section, lang string
if strings.HasPrefix(path, "/man") && strings.Index(path[1:], "/") > -1 {
suite, binarypkg, name, section, lang = i.splitLegacy(path)
} else {
suite, binarypkg, name, section, lang = i.split(path)
}
if rewrite, ok := i.Suites[suite]; ok {
suite = rewrite
}
if section == "0" {
// legacy manpages.debian.org
section = ""
}
log.Printf("path %q -> suite = %q, binarypkg = %q, name = %q, section = %q, lang = %q", path, suite, binarypkg, name, section, lang)
lname := strings.ToLower(name)
entries, ok := i.Entries[lname]
if !ok {
// Fall back to joining (originally) whitespace-separated
// parts by dashes and underscores, like man(1).
entries, ok = i.Entries[strings.Replace(lname, ".", "-", -1)]
if !ok {
entries, ok = i.Entries[strings.Replace(lname, ".", "_", -1)]
if !ok {
return "", &NotFoundError{Manpage: name}
}
}
}
acceptLang := r.Header.Get("Accept-Language")
ref := IndexEntry{
Suite: r.FormValue("suite"),
Binarypkg: r.FormValue("binarypkg"),
Section: r.FormValue("section"),
Language: r.FormValue("language"),
}
filtered := i.Narrow(acceptLang, IndexEntry{
Suite: suite,
Binarypkg: binarypkg,
Section: section,
Language: lang,
}, ref, entries)
if len(filtered) == 0 {
// Present the user with another choice for this manpage.
var best IndexEntry
if name != "index" && name != "favicon" {
best = i.Narrow(acceptLang, IndexEntry{}, ref, entries)[0]
}
return "", &NotFoundError{
Manpage: name,
BestChoice: best}
}
return filtered[0].ServingPath(suffix), nil
}
func IndexFromProto(path string) (Index, error) {
index := Index{
Langs: make(map[string]bool),
Sections: make(map[string]bool),
Suites: make(map[string]string),
}
b, err := ioutil.ReadFile(path)
if err != nil {
return index, err
}
var idx pb.Index
if err := proto.Unmarshal(b, &idx); err != nil {
return index, err
}
index.Entries = make(map[string][]IndexEntry, len(idx.Entry))
for _, e := range idx.Entry {
name := strings.ToLower(e.Name)
index.Entries[name] = append(index.Entries[name], IndexEntry{
Name: e.Name,
Suite: e.Suite,
Binarypkg: e.Binarypkg,
Section: e.Section,
Language: e.Language,
})
}
for _, l := range idx.Language {
index.Langs[l] = true
}
index.Suites = idx.Suite
for _, l := range idx.Section {
index.Sections[l] = true
}
index.Sections["0"] = true
return index, nil
}