Tencent/WeKnora · error

create request: %w

Error message

create request: %w

What it means

This error is returned when http.NewRequestWithContext fails to construct the GET request for a remote image download, before any network I/O occurs. It almost always indicates a malformed URL (unparseable scheme or host) or an invalid/nested context setup.

Source

Thrown at internal/infrastructure/docparser/image_resolver.go:1010

	markdown, htmlImages := resolveRemoteImagePass(ctx, markdown, remotePassSpec{
		Pattern:  imgHTMLSrc,
		URLGroup: searchutil.HTMLImageSrcURLGroup,
		Syntax:   "html",
		SrcOf:    htmlAttrSrc,
	}, client, fileSvc, tenantID)

	images = append(images, mdImages...)
	images = append(images, htmlImages...)
	return markdown, images, nil
}

// downloadImage fetches an image from remoteURL using the provided SSRF-safe
// client. It validates Content-Type and enforces maxRemoteImageSize.
func downloadImage(ctx context.Context, client *http.Client, remoteURL string) (data []byte, mimeType string, err error) {
	req, err := http.NewRequestWithContext(ctx, http.MethodGet, remoteURL, nil)
	if err != nil {
		return nil, "", fmt.Errorf("create request: %w", err)
	}
	// Some CDNs require a browser-like User-Agent.
	req.Header.Set("User-Agent", "Mozilla/5.0 (compatible; WeKnora/1.0)")

	resp, err := client.Do(req)
	if err != nil {
		return nil, "", fmt.Errorf("HTTP GET: %w", err)
	}
	defer resp.Body.Close()

	if resp.StatusCode != http.StatusOK {
		return nil, "", fmt.Errorf("unexpected status %d", resp.StatusCode)
	}

	// Determine MIME type from Content-Type header.
	ct := resp.Header.Get("Content-Type")
	mimeType, _, _ = mime.ParseMediaType(ct)
	if mimeType == "" {

View on GitHub (pinned to 988cbb0330)

Solutions

  1. Validate/clean the URL before calling downloadImage (trim whitespace, url.Parse check)
  2. Ensure the URL is absolute with http/https scheme
  3. Reject or skip images with malformed src attributes at extraction time
  4. Log the offending URL to identify the malformed source

Example fix

// before
req, err := http.NewRequestWithContext(ctx, http.MethodGet, remoteURL, nil)
if err != nil { return nil, "", fmt.Errorf("create request: %w", err) }
// after
u, perr := url.Parse(strings.TrimSpace(remoteURL))
if perr != nil || (u.Scheme != "http" && u.Scheme != "https") {
    return nil, "", fmt.Errorf("invalid remote image url: %q", remoteURL)
}
req, err := http.NewRequestWithContext(ctx, http.MethodGet, u.String(), nil)
if err != nil { return nil, "", fmt.Errorf("create request: %w", err) }
Defensive patterns

Strategy: validation

Validate before calling

func isFetchableURL(raw string) bool {
    u, err := url.Parse(strings.TrimSpace(raw))
    return err == nil && (u.Scheme == "http" || u.Scheme == "https") && u.Host != ""
}
// call before fetching: if !isFetchableURL(imgURL) { skip }

Prevention

When it happens

Trigger: downloadImage receives a remoteURL that url.Parse cannot handle — e.g. contains control characters, spaces, or is not a valid absolute URL.

Common situations: Unescaped characters extracted from document markup, URLs with stray whitespace or newline, partially formed href/src attributes scraped from documents.

Understand the failure class

Background: "Invalid URL" / "URL cannot be empty": fix the malformed or missing URL behind request-construction failures — this error's family across 50 libraries.

Related errors


AI-assisted analysis of Tencent/WeKnora@988cbb0330 (2026-09-02). Data as JSON: /api/errors/09e4a7c3addd99be. Report an issue: GitHub.