mirror of
https://github.com/gocsaf/csaf.git
synced 2025-12-22 05:40:11 +01:00
Implemented links on directory listings checking
This commit is contained in:
parent
32be3602b6
commit
3bbd37c441
4 changed files with 92 additions and 9 deletions
|
|
@ -10,11 +10,49 @@ package main
|
|||
|
||||
import (
|
||||
"io"
|
||||
"net/http"
|
||||
"net/url"
|
||||
|
||||
"github.com/PuerkitoBio/goquery"
|
||||
)
|
||||
|
||||
func linksOnPage(r io.Reader) ([]string, error) {
|
||||
func (p *processor) linksOnPageURL(baseDir string) ([]string, error) {
|
||||
|
||||
base, err := url.Parse(baseDir)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
client := p.httpClient()
|
||||
p.checkTLS(baseDir)
|
||||
res, err := client.Get(baseDir)
|
||||
|
||||
p.badDirListings.use()
|
||||
|
||||
if err != nil {
|
||||
p.badDirListings.add("Fetching %s failed: %v", base, err)
|
||||
return nil, errContinue
|
||||
}
|
||||
if res.StatusCode != http.StatusOK {
|
||||
p.badDirListings.add("Fetching %s failed. Status code %d (%s)",
|
||||
base, res.StatusCode, res.Status)
|
||||
return nil, errContinue
|
||||
}
|
||||
|
||||
defer res.Body.Close()
|
||||
|
||||
// Links may be relative
|
||||
return linksOnPage(res.Body, func(link string) (string, error) {
|
||||
u, err := url.Parse(link)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
return base.ResolveReference(u).String(), nil
|
||||
})
|
||||
|
||||
}
|
||||
|
||||
func linksOnPage(r io.Reader, resolve func(string) (string, error)) ([]string, error) {
|
||||
|
||||
doc, err := goquery.NewDocumentFromReader(r)
|
||||
if err != nil {
|
||||
|
|
@ -23,11 +61,16 @@ func linksOnPage(r io.Reader) ([]string, error) {
|
|||
|
||||
var links []string
|
||||
|
||||
doc.Find("a").Each(func(i int, s *goquery.Selection) {
|
||||
doc.Find("a").Each(func(_ int, s *goquery.Selection) {
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
if link, ok := s.Attr("href"); ok {
|
||||
links = append(links, link)
|
||||
if link, err = resolve(link); err == nil {
|
||||
links = append(links, link)
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
return links, nil
|
||||
return links, err
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue