mirror of
https://github.com/gocsaf/csaf.git
synced 2025-12-22 18:15:42 +01:00
Trimmed redirects (#230)
* Changes phrasing of redirects to be clearer. Now omits redirects if they are already listed as part of a larger redirect chain * Rebuilt how the redirection string is built. Now checks for duplicate redirections after all redirections have been read * Fixes intendation error * Fixed redirect output. * Fixed recording redirects. Co-authored-by: Jan Höfelmeyer <Jan Höfelmeyer jhoefelmeyer@intevation.de> Co-authored-by: Sascha L. Teichmann <sascha.teichmann@intevation.de>
This commit is contained in:
parent
56a047cdde
commit
c00b8b37bb
2 changed files with 40 additions and 17 deletions
|
|
@ -43,7 +43,7 @@ type processor struct {
|
||||||
opts *options
|
opts *options
|
||||||
client util.Client
|
client util.Client
|
||||||
|
|
||||||
redirects map[string]string
|
redirects map[string][]string
|
||||||
noneTLS map[string]struct{}
|
noneTLS map[string]struct{}
|
||||||
alreadyChecked map[string]whereType
|
alreadyChecked map[string]whereType
|
||||||
pmdURL string
|
pmdURL string
|
||||||
|
|
@ -259,19 +259,19 @@ func (p *processor) markChecked(s string, mask whereType) bool {
|
||||||
|
|
||||||
func (p *processor) checkRedirect(r *http.Request, via []*http.Request) error {
|
func (p *processor) checkRedirect(r *http.Request, via []*http.Request) error {
|
||||||
|
|
||||||
var path strings.Builder
|
|
||||||
for i, v := range via {
|
|
||||||
if i > 0 {
|
|
||||||
path.WriteString(", ")
|
|
||||||
}
|
|
||||||
path.WriteString(v.URL.String())
|
|
||||||
}
|
|
||||||
url := r.URL.String()
|
url := r.URL.String()
|
||||||
p.checkTLS(url)
|
p.checkTLS(url)
|
||||||
if p.redirects == nil {
|
if p.redirects == nil {
|
||||||
p.redirects = map[string]string{}
|
p.redirects = map[string][]string{}
|
||||||
|
}
|
||||||
|
|
||||||
|
if redirects := p.redirects[url]; len(redirects) == 0 {
|
||||||
|
redirects = make([]string, len(via))
|
||||||
|
for i, v := range via {
|
||||||
|
redirects[i] = v.URL.String()
|
||||||
|
}
|
||||||
|
p.redirects[url] = redirects
|
||||||
}
|
}
|
||||||
p.redirects[url] = path.String()
|
|
||||||
|
|
||||||
if len(via) > 10 {
|
if len(via) > 10 {
|
||||||
return errors.New("too many redirections")
|
return errors.New("too many redirections")
|
||||||
|
|
|
||||||
|
|
@ -11,6 +11,7 @@ package main
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"sort"
|
"sort"
|
||||||
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
type (
|
type (
|
||||||
|
|
@ -76,19 +77,41 @@ func (r *redirectsReporter) report(p *processor, domain *Domain) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
keys := make([]string, len(p.redirects))
|
keys := keysNotInValues(p.redirects)
|
||||||
var i int
|
|
||||||
for k := range p.redirects {
|
first := func(i int) string {
|
||||||
keys[i] = k
|
if vs := p.redirects[keys[i]]; len(vs) > 0 {
|
||||||
i++
|
return vs[0]
|
||||||
}
|
}
|
||||||
sort.Strings(keys)
|
return ""
|
||||||
|
}
|
||||||
|
|
||||||
|
sort.Slice(keys, func(i, j int) bool { return first(i) < first(j) })
|
||||||
|
|
||||||
for i, k := range keys {
|
for i, k := range keys {
|
||||||
keys[i] = fmt.Sprintf("Redirect %s: %s", k, p.redirects[k])
|
keys[i] = fmt.Sprintf("Redirect %s -> %s", strings.Join(p.redirects[k], " -> "), k)
|
||||||
}
|
}
|
||||||
req.message(WarnType, keys...)
|
req.message(WarnType, keys...)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// keysNotInValues returns a slice of keys which are not in the values
|
||||||
|
// of the given map.
|
||||||
|
func keysNotInValues(m map[string][]string) []string {
|
||||||
|
values := map[string]bool{}
|
||||||
|
for _, vs := range m {
|
||||||
|
for _, v := range vs {
|
||||||
|
values[v] = true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
keys := make([]string, 0, len(m))
|
||||||
|
for k := range m {
|
||||||
|
if !values[k] {
|
||||||
|
keys = append(keys, k)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return keys
|
||||||
|
}
|
||||||
|
|
||||||
// report tests if an provider-metadata.json are available and sets the
|
// report tests if an provider-metadata.json are available and sets the
|
||||||
// "message" field value of the "Requirement" struct as a result of that.
|
// "message" field value of the "Requirement" struct as a result of that.
|
||||||
func (r *providerMetadataReport) report(p *processor, domain *Domain) {
|
func (r *providerMetadataReport) report(p *processor, domain *Domain) {
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue