Compare commits
40
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
e867840994 | ||
|
|
5813e948f0 | ||
|
|
e3556df1b1 | ||
|
|
b159dbf636 | ||
|
|
bdc75eca4a | ||
|
|
c2b2897f5d | ||
|
|
569037d29f | ||
|
|
f3152b6f44 | ||
|
|
f9fd0955e8 | ||
|
|
2f2d1036f6 | ||
|
|
148e833454 | ||
|
|
7498b648d7 | ||
|
|
794f586979 | ||
|
|
4598e6d8fe | ||
|
|
2ae0502aba | ||
|
|
be11b13443 | ||
|
|
7d9bd98488 | ||
|
|
8a6cac5df0 | ||
|
|
828d776b94 | ||
|
|
1e54b04490 | ||
|
|
ccc65f9715 | ||
|
|
e4ce693396 | ||
|
|
cca2bcaf58 | ||
|
|
f058de8e6f | ||
|
|
8a71e4cb5c | ||
|
|
c95ad7ab95 | ||
|
|
0c48b94ece | ||
|
|
614f05f1d7 | ||
|
|
9fb19b0067 | ||
|
|
d8b47abf6e | ||
|
|
67817048f9 | ||
|
|
12cd121271 | ||
|
|
dbf49d259b | ||
|
|
0456723f0b | ||
|
|
7f6978ad27 | ||
|
|
d60700bf1a | ||
|
|
2b58869a04 | ||
|
|
d1f14120df | ||
|
|
5ef46be8c4 | ||
|
|
249aca229b |
No files matched your search
Binary file not shown.
@@ -3,18 +3,20 @@
|
||||
# derived from: http://zduck.com/2014/go-project-structure-and-dependencies/
|
||||
|
||||
# build targets (which aren't files)
|
||||
.PHONY: build fmt lint vet test autotest run clean env vendor
|
||||
.PHONY: build fmt lint vet test autotest cover doc run clean env stats vendor
|
||||
|
||||
# configuration
|
||||
APPNAME := docrawler
|
||||
VENDOR := .vendor
|
||||
PACKAGES := \
|
||||
golang.org/x/tools/cmd/cover \
|
||||
golang.org/x/tools/cmd/godoc \
|
||||
golang.org/x/tools/cmd/vet \
|
||||
github.com/golang/lint/golint
|
||||
|
||||
# override GOPATH to be our vendor directory
|
||||
export GOPATH := ${PWD}/${VENDOR}
|
||||
export PATH := ${PWD}/${VENDOR}/bin:${PATH}
|
||||
export GOPATH := ${PWD}/${VENDOR}:${PWD}
|
||||
export PATH := ${PWD}/${VENDOR}/bin:${PWD}/bin:${PATH}
|
||||
|
||||
default: build
|
||||
|
||||
@@ -24,8 +26,9 @@ build: fmt lint vet
|
||||
fmt:
|
||||
go fmt ./src/...
|
||||
|
||||
# TODO golint is not accepting the "./src/..." for some reason (from within make)
|
||||
lint:
|
||||
golint ./src
|
||||
golint ./src/${APPNAME}/*.go
|
||||
|
||||
vet:
|
||||
go vet ./src/...
|
||||
@@ -35,24 +38,33 @@ test:
|
||||
|
||||
autotest:
|
||||
fswatch -or ./src | \
|
||||
xargs -n1 -I{} ${SHELL} -c 'echo ; date ; echo ----------------------------; go test ./src/... | ./util/testfilter.sh'
|
||||
xargs -n1 -I{} ${SHELL} -c 'echo ; date ; echo ----------------------------; go test ./src/... | ./util/testfilter.sh'
|
||||
|
||||
cover:
|
||||
go test -coverprofile cover.out ./src/... && go tool cover -html cover.out
|
||||
|
||||
doc:
|
||||
godoc -http=:6060 -index
|
||||
|
||||
run: build
|
||||
./bin/${APPNAME}
|
||||
|
||||
clean:
|
||||
rm -f ./bin/${APPNAME}
|
||||
rm -f ./bin/${APPNAME} ./cover.out
|
||||
|
||||
env:
|
||||
go env
|
||||
|
||||
stats:
|
||||
cloc ./src
|
||||
|
||||
# vendor
|
||||
# * wipes our vendor directory completely
|
||||
# * fetches all packages in ${PACKAGES}
|
||||
# * fetches all packages in ${PACKAGES} (GOPATH hardcoded to vendor directory only)
|
||||
# * removes source control dirs so packages are vendorable (commitable)
|
||||
vendor:
|
||||
rm -dRf ./${VENDOR}/src && \
|
||||
go get -u ${PACKAGES} && \
|
||||
rm -dRf ./${VENDOR} && \
|
||||
GOPATH=${PWD}/${VENDOR} go get -u ${PACKAGES} && \
|
||||
rm -rf `find ./${VENDOR}/src -type d -name .git` && \
|
||||
rm -rf `find ./${VENDOR}/src -type d -name .hg` && \
|
||||
rm -rf `find ./${VENDOR}/src -type d -name .bzr` && \
|
||||
|
||||
@@ -44,7 +44,6 @@ These are features I'd like to include.
|
||||
* launch goroutine per fetch
|
||||
* when there's no Jobs left, and nothing being fetched or parsed, convert the data struct into something useful and output it
|
||||
|
||||
|
||||
## Wishlist
|
||||
|
||||
These are features that sound cool but are probably out of scope.
|
||||
|
||||
@@ -1,6 +1,93 @@
|
||||
package main
|
||||
|
||||
import ()
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
)
|
||||
|
||||
// docrawl begins crawling the site at "url"
|
||||
func docrawl(url string) []*Page {
|
||||
// our result structure
|
||||
var sitemap []*Page
|
||||
|
||||
// what we've already parsed, what we still need to parse
|
||||
crawled := make(map[string]*Page)
|
||||
queued := make(map[string]*Page)
|
||||
|
||||
// create first page and add to the queue
|
||||
homepage, _ := NewPage(nil, url) // TODO check error
|
||||
queued[homepage.URL.String()] = homepage
|
||||
|
||||
// main loop
|
||||
for len(queued) > 0 {
|
||||
// pop a page off of queued
|
||||
var dest *Page
|
||||
var destURL string
|
||||
for destURL, dest = range queued {
|
||||
break
|
||||
}
|
||||
delete(queued, destURL)
|
||||
|
||||
// mark this page as complete
|
||||
crawled[dest.URL.String()] = dest
|
||||
|
||||
// add page to list
|
||||
sitemap = append(sitemap, dest)
|
||||
|
||||
// make sure this URL has the same hostname as our first page
|
||||
if dest.URL.Host != homepage.URL.Host {
|
||||
// skip URLs associated with other Hosts
|
||||
dest.Skipped = true
|
||||
continue
|
||||
}
|
||||
|
||||
// fetch page
|
||||
text, err := fetchPage(dest)
|
||||
if err != nil {
|
||||
dest.Broken = true
|
||||
continue
|
||||
}
|
||||
|
||||
// parse links
|
||||
title, links, err := parseLinks(text)
|
||||
if err != nil {
|
||||
dest.Broken = true
|
||||
continue
|
||||
}
|
||||
dest.Title = title
|
||||
for _, l := range links {
|
||||
p, err := NewPage(dest, l)
|
||||
if err != nil {
|
||||
continue // TODO check error
|
||||
}
|
||||
|
||||
// see if we already know about this page
|
||||
pCrawled, haveCrawled := crawled[p.URL.String()]
|
||||
pQueued, haveQueued := queued[p.URL.String()]
|
||||
if haveCrawled {
|
||||
// add the previously crawled page to children
|
||||
dest.Children = append(dest.Children, pCrawled)
|
||||
} else if haveQueued {
|
||||
// add the previously queued page to children
|
||||
dest.Children = append(dest.Children, pQueued)
|
||||
} else {
|
||||
// add this new page (never seen) to children
|
||||
dest.Children = append(dest.Children, p)
|
||||
// .. and queue it up for crawling
|
||||
queued[p.URL.String()] = p
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return sitemap
|
||||
}
|
||||
|
||||
func main() {
|
||||
// crawl each URL on the command line
|
||||
for _, u := range os.Args[1:] {
|
||||
pages := docrawl(u)
|
||||
l := sitemapToLocations(pages)
|
||||
j, _ := locationsToJSON(l)
|
||||
fmt.Println(j)
|
||||
}
|
||||
}
|
||||
+180
-11
@@ -1,7 +1,6 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net"
|
||||
"net/http"
|
||||
"os"
|
||||
@@ -19,13 +18,13 @@ func TestMain(m *testing.M) {
|
||||
// start our simple web server
|
||||
l, err := net.Listen("tcp", ":"+strconv.Itoa(port))
|
||||
if err != nil {
|
||||
panic("unable to open port for http server") // TODO remove panic?
|
||||
panic("unable to open port for http server")
|
||||
}
|
||||
http.Handle("/", http.FileServer(http.Dir("./testsite")))
|
||||
go func() {
|
||||
err = http.Serve(l, nil)
|
||||
if err != nil {
|
||||
panic("unable to start http server") // TODO remove panic?
|
||||
panic("unable to start http server")
|
||||
}
|
||||
}()
|
||||
|
||||
@@ -43,8 +42,8 @@ func TestMain(m *testing.M) {
|
||||
func TestServerRunning(t *testing.T) {
|
||||
resp, err := http.Get(baseURL)
|
||||
if resp.StatusCode != 200 || err != nil {
|
||||
fmt.Printf("got %v, err = %v\n", resp.StatusCode, err)
|
||||
t.Error(err)
|
||||
t.Logf("got %v, err = %v\n", resp.StatusCode, err)
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -52,21 +51,191 @@ func TestServerRunning(t *testing.T) {
|
||||
func TestBadRequest(t *testing.T) {
|
||||
resp, err := http.Get(baseURL + "doesnotexist.html")
|
||||
if resp.StatusCode != 404 || err != nil {
|
||||
fmt.Printf("got %v, err = %v\n", resp.StatusCode, err)
|
||||
t.Error("request for non existent file didn't fail properly")
|
||||
t.Logf("got %v, err = %v\n", resp.StatusCode, err)
|
||||
t.Fatal("request for non existent file didn't fail properly")
|
||||
}
|
||||
}
|
||||
|
||||
// TestSimpleFetch makes sure we can fetch a file and we get exactly what we expect
|
||||
func TestSimpleFetch(t *testing.T) {
|
||||
body, err := fetch(baseURL + "fetch_test.html")
|
||||
page, err := NewPage(nil, baseURL+"fetch_test.html")
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
t.Error("problem creating New Page struct")
|
||||
}
|
||||
body, err := fetchPage(page)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
desired := "<html><head></head><body></body></html>\n"
|
||||
if body != desired {
|
||||
fmt.Printf(" Got: '%v'\n", body)
|
||||
fmt.Printf("Wanted: '%v'\n", desired)
|
||||
t.Logf(" Got: '%v'\n", body)
|
||||
t.Logf("Wanted: '%v'\n", desired)
|
||||
t.Error("fetch succeeded, but content mismatched")
|
||||
}
|
||||
}
|
||||
|
||||
// TestSimpleMap figures out the site map for the site in baseURL
|
||||
func TestSimpleMap(t *testing.T) {
|
||||
pages := docrawl(baseURL)
|
||||
|
||||
// because our crawl is non-deterministic, we have to do a complete
|
||||
// cycle through every page, counting stuff, finding specific pages
|
||||
// so that we can then verify certain aspects of each page
|
||||
skipped, children := 0, 0
|
||||
var homepage, aboutpage, skippage *Page
|
||||
for _, p := range pages {
|
||||
if p.Title == "Home" {
|
||||
homepage = p
|
||||
} else if p.Title == "About Test" {
|
||||
aboutpage = p
|
||||
} else if p.URL.String() == "http://doesntexist23492387492837492374982734.com/" {
|
||||
skippage = p
|
||||
}
|
||||
if p.Skipped {
|
||||
skipped++
|
||||
}
|
||||
children += len(p.Children)
|
||||
}
|
||||
|
||||
// check page counts
|
||||
if len(pages) != 6 {
|
||||
t.Logf("got %v, wanted %v\n", len(pages), 6)
|
||||
t.Fatal("got wrong number of pages")
|
||||
}
|
||||
if children != 8 {
|
||||
t.Logf("got %v, wanted %v\n", children, 8)
|
||||
t.Fatal("got wrong number of total children")
|
||||
}
|
||||
if skipped != 1 {
|
||||
t.Logf("got %v, wanted %v\n", skipped, 1)
|
||||
t.Fatal("got wrong number of total skipped")
|
||||
}
|
||||
|
||||
// check first page URL
|
||||
if homepage.URL.String() != baseURL {
|
||||
t.Error("page URL is invalid")
|
||||
}
|
||||
|
||||
// specific page number of children
|
||||
if len(homepage.Children) != 4 {
|
||||
t.Logf("got %v, wanted %v\n", len(homepage.Children), 4)
|
||||
t.Fatal("got wrong number of links")
|
||||
}
|
||||
if len(aboutpage.Children) != 4 {
|
||||
t.Logf("got %v, wanted %v\n", len(aboutpage.Children), 4)
|
||||
t.Fatal("got wrong number of links")
|
||||
}
|
||||
|
||||
// make sure remote pages were skipped
|
||||
if homepage.Skipped != false {
|
||||
t.Logf("found a page that should NOT have been skipped %q", homepage.Children[0].URL.String())
|
||||
t.Error("child was skipped")
|
||||
}
|
||||
if skippage.Skipped != true {
|
||||
t.Logf("found a page that should have been skipped %q", pages[3].Children[2].URL.String())
|
||||
t.Error("child wasn't skipped")
|
||||
}
|
||||
|
||||
// verify page content
|
||||
// TODO find a better way to do this
|
||||
//if homepage.Children[0].URL.String() != baseURL+"about.html" {
|
||||
//t.Logf(" Got: %q\n", homepage.Children[0].URL.String())
|
||||
//t.Logf("Wanted: %q\n", baseURL+"about.html")
|
||||
//t.Error("link name is incorrect")
|
||||
//}
|
||||
//if homepage.Children[1].URL.String() != baseURL+"assets/image.png" {
|
||||
//t.Logf(" Got: %q\n", homepage.Children[1].URL.String())
|
||||
//t.Logf("Wanted: %q\n", baseURL+"assets/image.png")
|
||||
//t.Error("link name is incorrect")
|
||||
//}
|
||||
//if homepage.Children[3].URL.String() != baseURL+"scripts/blah.js" {
|
||||
//t.Logf(" Got: %q\n", homepage.Children[3].URL.String())
|
||||
//t.Logf("Wanted: %q\n", baseURL+"scripts/blah.js")
|
||||
//t.Error("link name is incorrect")
|
||||
//}
|
||||
}
|
||||
|
||||
// TestHeaderFetching tests fetchFiletype() to see if we are getting expected results
|
||||
func TestHeaderFetching(t *testing.T) {
|
||||
basepage, err := NewPage(nil, baseURL)
|
||||
if err != nil {
|
||||
t.Error("problem creating New Page struct")
|
||||
}
|
||||
if err := fetchFiletype(basepage); err != nil || basepage.MediaType != "text/html" {
|
||||
t.Error("problem fetching filetype")
|
||||
}
|
||||
|
||||
page, err := NewPage(basepage, "about.html")
|
||||
if err != nil {
|
||||
t.Error("problem creating New Page struct")
|
||||
}
|
||||
if err := fetchFiletype(page); err != nil || page.MediaType != "text/html" {
|
||||
t.Error("problem fetching filetype")
|
||||
}
|
||||
|
||||
page, err = NewPage(basepage, "assets/image.png")
|
||||
if err != nil {
|
||||
t.Error("problem creating New Page struct")
|
||||
}
|
||||
if err := fetchFiletype(page); err != nil || page.MediaType != "image/png" {
|
||||
t.Logf("got %q, wanted %q", page.MediaType, "image/png")
|
||||
t.Error("problem fetching filetype")
|
||||
}
|
||||
}
|
||||
|
||||
// TestJsonOutput gets a sitemap and then converts it to json
|
||||
func TestJsonOutput(t *testing.T) {
|
||||
pages := docrawl(baseURL)
|
||||
l := sitemapToLocations(pages)
|
||||
if len(l) != 2 {
|
||||
t.Error("sitemapToLocations has the wrong number of locations")
|
||||
}
|
||||
if l[0].URL != baseURL {
|
||||
t.Error("got an incorrect location")
|
||||
}
|
||||
if len(l[0].Links) != 1 {
|
||||
t.Error("location 0 has wrong number of links")
|
||||
}
|
||||
if len(l[0].Assets) != 2 {
|
||||
t.Logf("got %v", len(l[0].Assets))
|
||||
t.Error("location 0 has wrong number of assets")
|
||||
}
|
||||
if len(l[0].Broken) != 1 {
|
||||
t.Logf("got %v", len(l[0].Broken))
|
||||
t.Error("location 0 has wrong number of broken urls")
|
||||
}
|
||||
if len(l[0].Remote) != 0 {
|
||||
t.Logf("got %v", len(l[1].Remote))
|
||||
t.Error("location 0 has wrong number of remote urls")
|
||||
}
|
||||
if l[1].URL != baseURL+"about.html" {
|
||||
t.Error("got an incorrect location")
|
||||
}
|
||||
if len(l[1].Links) != 1 {
|
||||
t.Error("location 1 has wrong number of links")
|
||||
}
|
||||
if len(l[1].Assets) != 2 {
|
||||
t.Error("location 1 has wrong number of assets")
|
||||
}
|
||||
if len(l[1].Broken) != 0 {
|
||||
t.Logf("got %v", len(l[1].Broken))
|
||||
t.Error("location 1 has wrong number of broken urls")
|
||||
}
|
||||
if len(l[1].Remote) != 1 {
|
||||
t.Logf("got %v", len(l[1].Remote))
|
||||
t.Error("location 1 has wrong number of remote urls")
|
||||
}
|
||||
|
||||
// this is a cheater test because the output is from a run of the code being
|
||||
// test itself. but, it's been examined and I think it's right. and, there's not
|
||||
// many other ways to test this without some significant pain
|
||||
cheaterTest := `[{"URL":"http://localhost:8765/","Title":"Home","Links":["http://localhost:8765/about.html"],"Assets":["http://localhost:8765/assets/image.png","http://localhost:8765/scripts/blah.js"],"Broken":["http://localhost:8765/zzzbroken.html"],"Remote":null},{"URL":"http://localhost:8765/about.html","Title":"About Test","Links":["http://localhost:8765/"],"Assets":["http://localhost:8765/assets/image.png","http://localhost:8765/scripts/blah.js"],"Broken":null,"Remote":["http://doesntexist23492387492837492374982734.com/"]}]`
|
||||
j, err := locationsToJSON(l)
|
||||
if err != nil {
|
||||
t.Error("locationsToJSON failed")
|
||||
}
|
||||
if j != cheaterTest {
|
||||
t.Error("locationsToJSON text doesn't match")
|
||||
}
|
||||
t.Log(j)
|
||||
}
|
||||
+58
-4
@@ -1,18 +1,72 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"io/ioutil"
|
||||
"mime"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
// fetch takes a URL, GETs it, and returns the body as a string
|
||||
func fetch(url string) (string, error) {
|
||||
// GET the url
|
||||
resp, err := http.Get(url)
|
||||
var (
|
||||
errContentTypeNotFound = errors.New("no Content-Type header found")
|
||||
errFetchError = errors.New("couldn't fetch page")
|
||||
)
|
||||
|
||||
// fetchFiletype performs an http HEAD to get the media type, and sets it
|
||||
// directly in Page.MediaType
|
||||
func fetchFiletype(page *Page) error {
|
||||
resp, err := http.Head(page.URL.String())
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// check response code
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
return errFetchError
|
||||
}
|
||||
|
||||
// pull the content type out of the http header
|
||||
contentType, ok := resp.Header["Content-Type"]
|
||||
if !ok {
|
||||
// no Content-Type header found
|
||||
return errContentTypeNotFound
|
||||
}
|
||||
|
||||
// parse mime type
|
||||
mediatype, _, err := mime.ParseMediaType(contentType[0])
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// set and return
|
||||
page.MediaType = mediatype
|
||||
return nil
|
||||
}
|
||||
|
||||
// fetchPage takes a Page, GETs it, and returns the body as a string
|
||||
func fetchPage(page *Page) (string, error) {
|
||||
// figure out the file type
|
||||
err := fetchFiletype(page)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
// we only want to fetch html
|
||||
if page.MediaType != "text/html" {
|
||||
return "", nil // but this isn't an error!
|
||||
}
|
||||
|
||||
// GET the url
|
||||
resp, err := http.Get(page.URL.String())
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
// check response code
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
return "", errFetchError
|
||||
}
|
||||
|
||||
// read the body
|
||||
body, err := ioutil.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
|
||||
@@ -0,0 +1,27 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"testing"
|
||||
)
|
||||
|
||||
// TestFailedHeaderFetching tests fetchFiletype() to see if we are getting expected failures
|
||||
func TestFailedHeaderFetching(t *testing.T) {
|
||||
page, err := NewPage(nil, "http://doesntexist23492387492837492374982734.com/")
|
||||
if err != nil {
|
||||
t.Error("problem creating New Page struct")
|
||||
}
|
||||
if err := fetchFiletype(page); err == nil {
|
||||
t.Error("tired fetching bogus page but didn't get nil back from fetchFiletype")
|
||||
}
|
||||
}
|
||||
|
||||
// TestFailedFetching tests fetchFiletype() to see if we are getting expected failures
|
||||
func TestFailedFetching(t *testing.T) {
|
||||
page, err := NewPage(nil, "http://doesntexist23492387492837492374982734.com/")
|
||||
if err != nil {
|
||||
t.Error("problem creating New Page struct")
|
||||
}
|
||||
if _, err := fetchPage(page); err == nil {
|
||||
t.Error("tired fetching bogus page but didn't get nil back from fetchPage")
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,67 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"sort"
|
||||
)
|
||||
|
||||
// Location is a struct which defines a single URL, which URLs (links and assets) it contains, etc.
|
||||
type Location struct {
|
||||
URL string
|
||||
Title string
|
||||
Links []string
|
||||
Assets []string
|
||||
Broken []string
|
||||
Remote []string
|
||||
}
|
||||
|
||||
func sitemapToLocations(pages []*Page) []*Location {
|
||||
// build a map of our pages
|
||||
pageMap := make(map[string]*Page)
|
||||
for _, p := range pages {
|
||||
pageMap[p.URL.String()] = p
|
||||
}
|
||||
|
||||
// build a slice of locations (one per page)
|
||||
var locations []*Location
|
||||
for _, p := range pages {
|
||||
if p.MediaType == "text/html" {
|
||||
// create a location for this page
|
||||
l := &Location{URL: p.URL.String(), Title: p.Title}
|
||||
|
||||
// add its children
|
||||
for _, c := range p.Children {
|
||||
// look up this child's media type from the root list of pages
|
||||
//mediaType := pageMap[c.URL.String()].MediaType
|
||||
if c.Skipped {
|
||||
l.Remote = append(l.Remote, c.URL.String())
|
||||
} else if c.MediaType == "text/html" {
|
||||
l.Links = append(l.Links, c.URL.String())
|
||||
} else if c.Broken {
|
||||
l.Broken = append(l.Broken, c.URL.String())
|
||||
} else {
|
||||
l.Assets = append(l.Assets, c.URL.String())
|
||||
}
|
||||
}
|
||||
|
||||
// now sort the children slices
|
||||
sort.Strings(l.Remote)
|
||||
sort.Strings(l.Links)
|
||||
sort.Strings(l.Broken)
|
||||
sort.Strings(l.Assets)
|
||||
|
||||
// and add this location to our slice
|
||||
locations = append(locations, l)
|
||||
}
|
||||
}
|
||||
|
||||
return locations
|
||||
}
|
||||
|
||||
func locationsToJSON(locations []*Location) (string, error) {
|
||||
b, err := json.Marshal(locations)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
return string(b), nil
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"net/url"
|
||||
)
|
||||
|
||||
// Page is a struct which defines a single page, which URLs (links and assets) it contains, etc.
|
||||
type Page struct {
|
||||
URL *url.URL
|
||||
Title string
|
||||
MediaType string
|
||||
Skipped bool
|
||||
Broken bool
|
||||
Children []*Page
|
||||
}
|
||||
|
||||
// NewPage takes a referring Page + a URL and returns a new &Page{}
|
||||
func NewPage(referrer *Page, rawurl string) (*Page, error) {
|
||||
// determine the base URL so we can resulve this into a full URL
|
||||
baseURL := ""
|
||||
if referrer != nil {
|
||||
baseURL = referrer.URL.String()
|
||||
}
|
||||
|
||||
// resolve URL into a full absolute URL (non-relative)
|
||||
u, err := resolveURL(baseURL, rawurl)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// create struct and return
|
||||
return &Page{URL: u, Skipped: false, Broken: false}, nil
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"testing"
|
||||
)
|
||||
|
||||
// TestBadNewPage tests NewPage to see if we are getting expected failures
|
||||
func TestBadNewPage(t *testing.T) {
|
||||
_, err := NewPage(nil, "h%20t\tp://doesntexist23492387492837492374982734.com/")
|
||||
if err == nil {
|
||||
t.Error("tried creating a NewPage with an invalid URL but didn't get an error")
|
||||
}
|
||||
}
|
||||
@@ -2,7 +2,6 @@ package main
|
||||
|
||||
import (
|
||||
"regexp"
|
||||
"sort"
|
||||
"strings"
|
||||
)
|
||||
|
||||
@@ -10,9 +9,9 @@ import (
|
||||
var reURL = regexp.MustCompile(`(?i)(src|href|xhref)\s*=\s*"([^"]+)"`) // TODO make RFC compliant
|
||||
var reTitle = regexp.MustCompile(`(?i)<\s*title\s*>([^<]*)<\s*\/\s*title`)
|
||||
|
||||
// parse takes a string and attempts to parse any html title and all links out of it,
|
||||
// and returns a sorted slice of the captures found
|
||||
func parse(s string) (string, []string, error) {
|
||||
// parseLinks takes a string and attempts to parse any html title and all links out of it,
|
||||
// and returns a slice of the captures found
|
||||
func parseLinks(s string) (string, []string, error) {
|
||||
// find the title, or default to empty
|
||||
title := ""
|
||||
titleMatches := reTitle.FindStringSubmatch(s)
|
||||
@@ -26,6 +25,5 @@ func parse(s string) (string, []string, error) {
|
||||
for _, match := range reURL.FindAllStringSubmatch(s, -1) {
|
||||
results = append(results, match[2])
|
||||
}
|
||||
sort.Strings(results)
|
||||
return title, results, nil
|
||||
}
|
||||
@@ -12,23 +12,27 @@ func TestSimpleParse(t *testing.T) {
|
||||
</head>
|
||||
<body>
|
||||
<img src="/assets/image.png"/>
|
||||
<a href="/about.html">
|
||||
<a href="/about.html">fooey!</a>
|
||||
<script href="scripts/blah.js"/>
|
||||
</body>
|
||||
</html>`
|
||||
title, matches, err := parse(doc)
|
||||
title, matches, err := parseLinks(doc)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
if title != "Test Page" {
|
||||
t.Error("got wrong title")
|
||||
}
|
||||
if len(matches) != 2 {
|
||||
if len(matches) != 3 {
|
||||
t.Error("invalid number of matches in parse")
|
||||
}
|
||||
if matches[0] != "/about.html" {
|
||||
if matches[0] != "/assets/image.png" {
|
||||
t.Error("match text is invalid")
|
||||
}
|
||||
if matches[1] != "/assets/image.png" {
|
||||
if matches[1] != "/about.html" {
|
||||
t.Error("match text is invalid")
|
||||
}
|
||||
if matches[2] != "scripts/blah.js" {
|
||||
t.Error("match text is invalid")
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
<html>
|
||||
<head>
|
||||
<title>About Test</title>
|
||||
<script src="scripts/blah.js"/>
|
||||
</head>
|
||||
<body>
|
||||
<img src="/assets/image.png"/>
|
||||
<a href="/">fooey!</a>
|
||||
<a href="http://doesntexist23492387492837492374982734.com/">bad one</a>
|
||||
</body>
|
||||
</html>
|
||||
Binary file not shown.
|
After Width: | Height: | Size: 938 B |
@@ -1,8 +1,11 @@
|
||||
<html>
|
||||
<head>
|
||||
<title>Home</title>
|
||||
</head>
|
||||
<body>
|
||||
<img src="/assets/image.png"/>
|
||||
<a href="/about.html">
|
||||
<a href="/about.html">fooey!</a>
|
||||
<script src="scripts/blah.js"/>
|
||||
<a href="/zzzbroken.html">oops!</a>
|
||||
</body>
|
||||
</html>
|
||||
@@ -0,0 +1 @@
|
||||
|
||||
@@ -7,7 +7,7 @@ import (
|
||||
)
|
||||
|
||||
var (
|
||||
ErrInvalidURL = errors.New("this URL can't be parsed successfully")
|
||||
errInvalidURL = errors.New("this URL can't be parsed successfully")
|
||||
)
|
||||
|
||||
// cleanURL takes a URL and normalizes it by downcasing the host part
|
||||
@@ -21,7 +21,7 @@ func checkURL(u *url.URL) error {
|
||||
// check for non-empty Host and Scheme
|
||||
if len(u.Host) == 0 || len(u.Scheme) == 0 {
|
||||
// something about this URL doesn't meet our spec
|
||||
return ErrInvalidURL
|
||||
return errInvalidURL
|
||||
}
|
||||
return nil // success!
|
||||
}
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/url"
|
||||
"testing"
|
||||
)
|
||||
@@ -14,8 +13,8 @@ func TestURLCleaner(t *testing.T) {
|
||||
}
|
||||
cleanURL(u)
|
||||
if u.Host != "somehost.com:8765" {
|
||||
fmt.Printf(" Got: %q\n", u.Host)
|
||||
fmt.Printf("Wanted: %q\n", "somehost.com:8765")
|
||||
t.Logf(" Got: %q\n", u.Host)
|
||||
t.Logf("Wanted: %q\n", "somehost.com:8765")
|
||||
t.Error("host extraction failed")
|
||||
}
|
||||
if u.String() != "http://swaits:pass@somehost.com:8765/blah/blah.html?x=y#foo" {
|
||||
@@ -41,8 +40,8 @@ func doResolveTest(t *testing.T, wanted, referrer, current string) {
|
||||
t.Error("resolveURL failed (probably parsing)")
|
||||
}
|
||||
if u.String() != wanted {
|
||||
fmt.Printf(" Got: %q\n", u)
|
||||
fmt.Printf("Wanted: %q\n", wanted)
|
||||
t.Logf(" Got: %q\n", u)
|
||||
t.Logf("Wanted: %q\n", wanted)
|
||||
t.Error("url resolution with base URL failed")
|
||||
}
|
||||
}
|
||||
@@ -51,4 +50,21 @@ func TestURLResolution(t *testing.T) {
|
||||
doResolveTest(t, "http://base.com/some/crazy/path/test.png", "http://BaSe.CoM/some/crazy/path/index.html?parm=x&blah=foo", "test.png")
|
||||
doResolveTest(t, "http://S:D@base.com:1/test.png", "http://S:D@BaSe.CoM:1/some/crazy/path/index.html?parm=x&blah=foo", "/test.png")
|
||||
doResolveTest(t, "http://another.com/blah.html", "http://S:D@BaSe.CoM:1/some/crazy/path/index.html?parm=x&blah=foo", "http://aNOThER.CoM/blah.html")
|
||||
doResolveTest(t, "http://empty.com/blah.html", "", "http://empty.com/blah.html")
|
||||
}
|
||||
|
||||
// TestBadResolve tries to resolve bogus URLs to be sure we're getting errors
|
||||
/* TODO not sure how to make url.Parse fail in these cases!!!
|
||||
func TestBadResolve(t* testing.T) {
|
||||
u, err := resolveURL("http://a.com/", "h%20t\tp://doesnte%20xist23492387492837492374982734.com/")
|
||||
if err == nil {
|
||||
t.Logf("got %q", u.String())
|
||||
t.Error("tried resolveURL with an invalid URL but didn't get an error")
|
||||
}
|
||||
u, err = resolveURL("h%20t\tp://doesnte%20xist23492387492837492374982734.com/", "http://a.com/")
|
||||
if err == nil {
|
||||
t.Logf("got %q", u.String())
|
||||
t.Error("tried resolveURL with an invalid URL but didn't get an error")
|
||||
}
|
||||
}
|
||||
*/
|
||||
Reference in new issue
Block a user