ProcessURLs will fetch the HTTP response for all URLs in the provided reader and stream the extracted sources to the runner's Results channel.
(data io.Reader)
| 83 | // ProcessURLs will fetch the HTTP response for all URLs in the provided reader |
| 84 | // and stream the extracted sources to the runner's Results channel. |
| 85 | func (r *runner) ProcessURLs(data io.Reader) { |
| 86 | var ( |
| 87 | next = Read(data) |
| 88 | wg = sync.WaitGroup{} |
| 89 | |
| 90 | throttle = make(chan struct{}, r.Options.Threads) |
| 91 | ) |
| 92 | |
| 93 | for i := 0; i < r.Options.Threads; i++ { |
| 94 | throttle <- struct{}{} |
| 95 | } |
| 96 | |
| 97 | for { |
| 98 | u, err := next() |
| 99 | if errors.Is(err, io.EOF) { |
| 100 | break |
| 101 | } |
| 102 | if err != nil { |
| 103 | log.Println(fmt.Errorf("[error] parsing url %v: %w", u, err)) |
| 104 | continue |
| 105 | } |
| 106 | |
| 107 | wg.Add(1) |
| 108 | go func(u *url.URL) { |
| 109 | defer func() { |
| 110 | throttle <- struct{}{} |
| 111 | wg.Done() |
| 112 | }() |
| 113 | |
| 114 | resp, err := extractor.FetchResponse(u.String(), r.Options.Request.Method, r.Options.Request.Headers) |
| 115 | if err != nil { |
| 116 | log.Println(fmt.Errorf("[error] fetching response for url %s: %w", u.String(), err)) |
| 117 | return |
| 118 | } |
| 119 | defer resp.Body.Close() |
| 120 | |
| 121 | sources, err := extractor.ExtractSources(resp.Body) |
| 122 | if err != nil { |
| 123 | log.Println(fmt.Errorf("[error] extracting sources from response for url %s: %w", u.String(), err)) |
| 124 | return |
| 125 | } |
| 126 | |
| 127 | filtered, err := extractor.Filter(sources, r.filters(u)...) |
| 128 | if err != nil { |
| 129 | log.Println(fmt.Errorf("[error] filtering sources for url %s: %w", u.String(), err)) |
| 130 | return |
| 131 | } |
| 132 | |
| 133 | for source := range filtered { |
| 134 | r.Results <- source |
| 135 | } |
| 136 | }(u) |
| 137 | |
| 138 | <-throttle |
| 139 | } |
| 140 | |
| 141 | wg.Wait() |
| 142 | } |
no test coverage detected