classify

package
v2.5.3 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 20, 2026 License: MIT Imports: 7 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package classify categorizes HTTP status codes, headers, and payload bytes into deterministic crawl outcomes before upstream HTML/JSON/PDF extractors inspect response bodies.

Outcomes & Host Short-Circuiting

Every fetched Kind (`scrape`, `render`, `pdf`, `json`, `feed`, `page`, `text`, `catalog`, `binary`) resolves to one Outcome:

  • OutcomeOK: valid 2xx payload matching the expected Kind invariants (for example `%PDF-` magic header on KindPDF or valid RFC 8259 JSON on KindJSON).
  • OutcomeChallenge: anti-bot edge mitigation (`cf-mitigated` header) — halts remaining queued URLs for the same host immediately and is never retried.
  • OutcomeStop: origin rate-limit or block (`403`, `429`, `503`) — halts remaining queued URLs for the same host (`not_tried`) and records cooldowns in `internal/politeness`.
  • OutcomeGone: permanent/missing document (`404`, `410`).
  • OutcomeSkip / OutcomeError / OutcomeNotTried: non-fatal skips, transport/schema errors, or short-circuited sibling requests.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func AcceptFor

func AcceptFor(kind Kind, requested string) (string, error)

AcceptFor validates `(kind, requestedAccept)` and returns the outbound `Accept` header value.

func StopsHost

func StopsHost(kind Kind, o Outcome) bool

StopsHost reports whether `outcome` on `kind` should leave remaining queued URLs for the same host `not_tried`.

Types

type Kind

type Kind string

Kind identifies what resource shape the caller expects from a URL.

const (
	KindFeed    Kind = "feed"    // Structured JSON product feed (`{"products": [...]}`)
	KindJSON    Kind = "json"    // Any valid 2xx JSON document
	KindPage    Kind = "page"    // Individual HTML document (`text/html`, 404/410 -> `gone`)
	KindText    Kind = "text"    // Auxiliary HTML, Markdown, or XML document (e.g., sitemap or llms.txt)
	KindCatalog Kind = "catalog" // Full-catalog HTML page where non-2xx fails the host batch
	KindScrape  Kind = "scrape"  // Universal smart scrape (HTML, Markdown, PDF, JSON, or XML -> Markdown + metadata)
	KindRender  Kind = "render"  // Headless Chromium JS-rendered HTML document (`edgefetch_fat`)
	KindPDF     Kind = "pdf"     // PDF document (`%PDF-` extracted via poppler `pdftotext` or pure-Go fallback)
	KindBinary  Kind = "binary"  // Binary resource returned as `bodyBase64` + `sha256`
)

type Outcome

type Outcome string

Outcome describes how the crawler and caller should act on an HTTP response.

const (
	OutcomeOK        Outcome = "ok"
	OutcomeChallenge Outcome = "challenge" // Edge anti-bot challenge (`cf-mitigated`) — never retried
	OutcomeStop      Outcome = "stop"      // Origin answered 403, 429, or 503 — halt remaining URLs for this host
	OutcomeGone      Outcome = "gone"      // Document answered 404 or 410
	OutcomeSkip      Outcome = "skip"      // Non-200 or non-HTML page that can be skipped without failing the host
	OutcomeError     Outcome = "error"     // Feed/transport failure
	OutcomeNotTried  Outcome = "not_tried" // Left unsent because an earlier request stopped the host or timed out
)

func Classify

func Classify(kind Kind, status int, header http.Header, body []byte) (Outcome, string)

Classify evaluates status code, response headers, and body bytes to determine `(Outcome, problem)`.

Note that Go `switch` cases do NOT fall through unless `fallthrough` is written explicitly, and multiple values can share one `case 403, 429, 503:` branch cleanly.

Example
package main

import (
	"fmt"
	"net/http"

	"github.com/bartrumb/edgefetch/v2/internal/classify"
)

func main() {
	htmlHdr := http.Header{"Content-Type": []string{"text/html; charset=utf-8"}}
	okOut, _ := classify.Classify(classify.KindScrape, 200, htmlHdr, []byte("<main><h1>Docs</h1></main>"))

	chalHdr := http.Header{
		"Content-Type": []string{"text/html"},
		"Cf-Mitigated": []string{"challenge"},
	}
	chalOut, prob := classify.Classify(classify.KindScrape, 403, chalHdr, []byte("<html></html>"))

	fmt.Println("200 scrape:", okOut)
	fmt.Println("challenge:", chalOut, "| stopsHost:", classify.StopsHost(classify.KindScrape, chalOut), "|", prob)
}
Output:
200 scrape: ok
challenge: challenge | stopsHost: true | Cloudflare challenge (cf-mitigated)

type WAFMatch

type WAFMatch struct {
	Vendor     string   `json:"vendor"`            // "cloudflare", "akamai", "datadome", "perimeterx", "aws-waf", "imperva", "vercel", "fastly", "kasada", "generic"
	Category   string   `json:"category"`          // "challenge" | "waf_block" | "rate_limit"
	Confidence float64  `json:"confidence"`        // 0.0 .. 1.0 (1.0 = authoritative HTTP header)
	Signals    []string `json:"signals,omitempty"` // Matched header/DOM indicators
	Summary    string   `json:"summary,omitempty"` // Human-readable diagnostic summary
}

WAFMatch describes a detected Web Application Firewall (WAF) or anti-bot challenge interstitial (`R2` & `R3`). EdgeFetch strictly halts on challenges and WAF blocks — never solving, clicking through, or bypassing them.

func ClassifyWAF

func ClassifyWAF(status int, header http.Header, body []byte) *WAFMatch

ClassifyWAF evaluates response status, HTTP headers, and HTML/JSON body against the 7-vendor WAF & Challenge Signature Catalog (`R2`) while enforcing all 4 Anti-False-Positive Guardrails (`R3`).

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL