MCPcopy Index your code
hub / github.com/docker/docker-agent / fetchURL

Method fetchURL

pkg/tools/builtin/fetch/fetch.go:181–269  ·  view source on GitHub ↗
(ctx context.Context, client *http.Client, urlStr, format string, headers map[string]string, robotsCache map[string]*robotstxt.RobotsData)

Source from the content-addressed store, hash-verified

179}
180
181func (h *fetchHandler) fetchURL(ctx context.Context, client *http.Client, urlStr, format string, headers map[string]string, robotsCache map[string]*robotstxt.RobotsData) Result {
182 result := Result{URL: urlStr}
183
184 // Validate URL
185 parsedURL, err := url.Parse(urlStr)
186 if err != nil {
187 result.Error = fmt.Sprintf("invalid URL: %v", err)
188 return result
189 }
190
191 // Check for valid URL structure
192 if parsedURL.Scheme == "" || parsedURL.Host == "" {
193 result.Error = "invalid URL: missing scheme or host"
194 return result
195 }
196
197 // Only allow HTTP and HTTPS
198 if parsedURL.Scheme != "http" && parsedURL.Scheme != "https" {
199 result.Error = "only HTTP and HTTPS URLs are supported"
200 return result
201 }
202
203 // Enforce domain allow/deny lists configured on the toolset.
204 if err := h.checkDomainAllowed(parsedURL); err != nil {
205 result.Error = err.Error()
206 return result
207 }
208
209 // Check robots.txt (with caching per host)
210 host := parsedURL.Host
211 robots, cached := robotsCache[host]
212 if !cached {
213 var err error
214 robots, err = h.fetchRobots(ctx, client, parsedURL, headers)
215 if err != nil {
216 result.Error = fmt.Sprintf("robots.txt check failed: %v", err)
217 return result
218 }
219 robotsCache[host] = robots
220 }
221
222 if robots != nil && !robots.TestAgent(parsedURL.Path, useragent.Header) {
223 result.Error = "URL blocked by robots.txt"
224 return result
225 }
226
227 fmtHandler := formatHandlerFor(format)
228
229 req, err := http.NewRequestWithContext(ctx, http.MethodGet, urlStr, http.NoBody)
230 if err != nil {
231 result.Error = fmt.Sprintf("failed to create request: %v", err)
232 return result
233 }
234 req.Header.Set("Accept", fmtHandler.accept)
235 useragent.SetIdentity(req)
236 // Apply caller-configured headers last so an operator-supplied
237 // Authorization, User-Agent, Accept, ... wins over the defaults set above.
238 for k, v := range headers {

Callers 1

CallToolMethod · 0.95

Calls 10

checkDomainAllowedMethod · 0.95
fetchRobotsMethod · 0.95
SetIdentityFunction · 0.92
formatHandlerForFunction · 0.85
ParseMethod · 0.80
DoMethod · 0.65
CloseMethod · 0.65
GetMethod · 0.65
ErrorMethod · 0.45
SetMethod · 0.45

Tested by

no test coverage detected