redirectcheck

package
v1.0.0-beta.2 Latest Latest
Warning

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

Go to latest
Published: Nov 16, 2026 License: MIT Imports: 11 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 redirectcheck verifies a redirect-rule CSV export (HubSpot's URL Redirects tool schema) against a live site: whether each rule's source still redirects correctly, whether its target is a live page, and whether both sides agree with the site's current sitemap.xml.

Index

Constants

View Source
const DefaultSitemapHint = "pass --sitemap-url to point at the right location"

DefaultSitemapHint is the remedy the "no sitemap found" error suggests, worded for the CLI. A caller without that flag (the web API) passes its own through RunOptions.SitemapHint.

View Source
const DefaultTimeout = 15 * time.Second

DefaultTimeout is the per-request timeout of a redirect check.

View Source
const DefaultUserAgent = "gocrawl/0.1 (+https://github.com/Patience-dot-devl/gocrawl)"

DefaultUserAgent is the User-Agent a redirect check sends unless told otherwise.

Variables

This section is empty.

Functions

func DiscoverSitemap

func DiscoverSitemap(ctx context.Context, fetcher crawler.Fetcher, domain, override string) (map[string]bool, error)

DiscoverSitemap fetches and parses the sitemap for domain, returning the normalized set of <loc> URLs it declares. If override is non-empty it is used as the only candidate; otherwise the conventional /sitemap.xml and /sitemap_index.xml locations are tried. An error is returned if nothing usable is found at any candidate — callers should treat this as fatal, since sitemap-membership columns would otherwise be silently wrong for every row.

func NewFetcher

func NewFetcher(timeout time.Duration, userAgent string) *crawler.HTTPFetcher

NewFetcher builds the HTTP fetcher a redirect check runs on. It follows up to 10 redirects, recording each hop, and reads at most 5 MiB per body. It has no crawl scope or credentials: a redirect check fetches exactly the URLs in the rules and the sitemap. The CLI (`gocrawl check-redirects`) and the web API share it so the two behave the same.

func WriteCSV

func WriteCSV(w io.Writer, rules []Rule, results []RowResult) error

WriteCSV writes rules and their check results as a CSV: the original columns verbatim, plus the appended verdict columns. Every input row is present in the output, in order.

Types

type RowResult

type RowResult struct {
	Scope               Scope    `json:"scope"`
	SourceStatus        int      `json:"source_status"`
	SourceFinalURL      string   `json:"source_final_url"`
	SourceMatchesTarget bool     `json:"source_matches_target"`
	TargetStatus        int      `json:"target_status"`
	OriginalInSitemap   bool     `json:"original_in_sitemap"`
	TargetInSitemap     bool     `json:"target_in_sitemap"`
	Verdict             Verdict  `json:"verdict"`
	Notes               []string `json:"notes,omitempty"`
}

RowResult is what checking one rule against the live site produced. The JSON names are what the web API returns for each row.

func CheckRule

func CheckRule(ctx context.Context, fetcher crawler.Fetcher, domain string, rule Rule, sitemapURLs map[string]bool) RowResult

CheckRule fetches rule's Original and Target URLs against the live site and returns the resulting verdict. sitemapURLs is the set discovered by DiscoverSitemap. Callers should only call this for rules classified ScopeInScope.

func Run

func Run(ctx context.Context, rules []Rule, opts RunOptions) ([]RowResult, error)

Run classifies every rule, fetches the site's sitemap once, then checks each in-scope rule concurrently (bounded by opts.Concurrency and opts.RatePerSecond). Every fetch — sitemap discovery included — goes through a shared AdaptiveLimiter, so a run that starts too aggressively for the target site backs itself off instead of hammering it into a bot-mitigation block. Results are returned in the same order as rules. Rows classified external or dynamic-pattern are not fetched.

type Rule

type Rule struct {
	Original            string `json:"original"`
	Target              string `json:"target"`
	RedirectType        string `json:"redirect_type"`
	RedirectStyle       string `json:"redirect_style"`
	Priority            string `json:"priority"`
	MatchQueryStrings   string `json:"match_query_strings"`
	IgnoreTrailingSlash string `json:"ignore_trailing_slash"`
	IgnoreProtocol      string `json:"ignore_protocol"`
	DisableIfPageExists string `json:"disable_if_page_exists"`
	Note                string `json:"note"`
}

Rule is one row of the redirect-rule CSV. Columns are kept as raw strings so they can be echoed back verbatim in the output report.

func ParseCSV

func ParseCSV(r io.Reader) ([]Rule, error)

ParseCSV reads a HubSpot-format redirect-rule export. It returns an error if the header doesn't match the expected column schema.

type RunOptions

type RunOptions struct {
	Domain     string
	SitemapURL string
	// SitemapHint is the remedy named in the "no sitemap found" error. Empty means
	// DefaultSitemapHint, which names the CLI flag.
	SitemapHint   string
	Fetcher       crawler.Fetcher
	Concurrency   int
	RatePerSecond float64
	// AdaptiveDelay automatically slows requests (halving requests-per-second, honoring any
	// Retry-After header) when the site responds with HTTP 429/503, the same way the crawl
	// engine does. Without it, a fixed concurrency/rate that's too aggressive for the target
	// can trip bot-mitigation (e.g. Cloudflare) partway through a run.
	AdaptiveDelay bool
	// Stats, if non-nil, is filled in with adaptive-delay activity once Run returns.
	Stats *RunStats
	// OnRow, if non-nil, is called once for every rule as its row completes. The arguments are
	// the number of rows done so far and the total number of rules. Calls are serialized
	// (never concurrent), and done rises by one per call, ending at total. Rows skipped
	// without a fetch (external, dynamic pattern, unclassifiable) count too. OnRow is not
	// called when Run fails before checking rows (no sitemap). It runs on a worker goroutine
	// and holds up the other workers' progress reports, so keep it short.
	OnRow func(done, total int)
}

RunOptions configures a check-redirects run.

type RunStats

type RunStats struct {
	ThrottleEvents int
	FinalRate      float64
}

RunStats reports adaptive-delay activity for a Run call.

type Scope

type Scope string

Scope classifies a rule by where its target points, relative to the configured main domain.

const (
	ScopeInScope  Scope = "in-scope"
	ScopeExternal Scope = "external"
	ScopeDynamic  Scope = "dynamic-pattern"
)

func Classify

func Classify(rule Rule, domain string) (Scope, error)

Classify determines a rule's Scope. An error is returned only if the target can't be parsed as a URL at all.

type Verdict

type Verdict string

Verdict is the overall pass/fail assessment of one row.

const (
	VerdictOK              Verdict = "ok"
	VerdictWarning         Verdict = "warning"
	VerdictError           Verdict = "error"
	VerdictSkippedExternal Verdict = "skipped-external"
	VerdictSkippedDynamic  Verdict = "skipped-dynamic"
)

Jump to

Keyboard shortcuts

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