()
| 32 | ) |
| 33 | |
| 34 | func main() { |
| 35 | flag.Parse() |
| 36 | |
| 37 | // Parse the provided seed |
| 38 | u, err := url.Parse(*seed) |
| 39 | if err != nil { |
| 40 | log.Fatal(err) |
| 41 | } |
| 42 | |
| 43 | // Create the muxer |
| 44 | mux := fetchbot.NewMux() |
| 45 | |
| 46 | // Handle all errors the same |
| 47 | mux.HandleErrors(fetchbot.HandlerFunc(func(ctx *fetchbot.Context, res *http.Response, err error) { |
| 48 | fmt.Printf("[ERR] %s %s - %s\n", ctx.Cmd.Method(), ctx.Cmd.URL(), err) |
| 49 | })) |
| 50 | |
| 51 | // Handle GET requests for html responses, to parse the body and enqueue all links as HEAD |
| 52 | // requests. |
| 53 | mux.Response().Method("GET").ContentType("text/html").Handler(fetchbot.HandlerFunc( |
| 54 | func(ctx *fetchbot.Context, res *http.Response, err error) { |
| 55 | // Process the body to find the links |
| 56 | doc, err := goquery.NewDocumentFromResponse(res) |
| 57 | if err != nil { |
| 58 | fmt.Printf("[ERR] %s %s - %s\n", ctx.Cmd.Method(), ctx.Cmd.URL(), err) |
| 59 | return |
| 60 | } |
| 61 | // Enqueue all links as HEAD requests |
| 62 | enqueueLinks(ctx, doc) |
| 63 | })) |
| 64 | |
| 65 | // Handle HEAD requests for html responses coming from the source host - we don't want |
| 66 | // to crawl links from other hosts. |
| 67 | mux.Response().Method("HEAD").Host(u.Host).ContentType("text/html").Handler(fetchbot.HandlerFunc( |
| 68 | func(ctx *fetchbot.Context, res *http.Response, err error) { |
| 69 | if _, err := ctx.Q.SendStringGet(ctx.Cmd.URL().String()); err != nil { |
| 70 | fmt.Printf("[ERR] %s %s - %s\n", ctx.Cmd.Method(), ctx.Cmd.URL(), err) |
| 71 | } |
| 72 | })) |
| 73 | |
| 74 | // Create the Fetcher, handle the logging first, then dispatch to the Muxer |
| 75 | h := logHandler(mux) |
| 76 | if *stopAtURL != "" || *cancelAtURL != "" { |
| 77 | stopURL := *stopAtURL |
| 78 | if *cancelAtURL != "" { |
| 79 | stopURL = *cancelAtURL |
| 80 | } |
| 81 | h = stopHandler(stopURL, *cancelAtURL != "", logHandler(mux)) |
| 82 | } |
| 83 | f := fetchbot.New(h) |
| 84 | |
| 85 | // First mem stat print must be right after creating the fetchbot |
| 86 | if *memStats > 0 { |
| 87 | // Print starting stats |
| 88 | printMemStats(nil) |
| 89 | // Run at regular intervals |
| 90 | runMemStats(f, *memStats) |
| 91 | // On exit, print ending stats after a GC |
nothing calls this directly
no test coverage detected