Parse turns HTML for a directory into names base should be the base URL to resolve any relative names from
(base *url.URL, in io.Reader)
| 445 | // Parse turns HTML for a directory into names |
| 446 | // base should be the base URL to resolve any relative names from |
| 447 | func parse(base *url.URL, in io.Reader) (names []string, err error) { |
| 448 | doc, err := html.Parse(in) |
| 449 | if err != nil { |
| 450 | return nil, err |
| 451 | } |
| 452 | var ( |
| 453 | walk func(*html.Node) |
| 454 | seen = make(map[string]struct{}) |
| 455 | ) |
| 456 | walk = func(n *html.Node) { |
| 457 | if n.Type == html.ElementNode && n.Data == "a" { |
| 458 | for _, a := range n.Attr { |
| 459 | if a.Key == "href" { |
| 460 | name, err := parseName(base, a.Val) |
| 461 | if err == nil { |
| 462 | if _, found := seen[name]; !found { |
| 463 | names = append(names, name) |
| 464 | seen[name] = struct{}{} |
| 465 | } |
| 466 | } |
| 467 | break |
| 468 | } |
| 469 | } |
| 470 | } |
| 471 | for c := n.FirstChild; c != nil; c = c.NextSibling { |
| 472 | walk(c) |
| 473 | } |
| 474 | } |
| 475 | walk(doc) |
| 476 | return names, nil |
| 477 | } |
| 478 | |
| 479 | // parseFilename extracts the filename from a Content-Disposition header |
| 480 | func parseFilename(contentDisposition string) (string, error) { |