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
- func DiscoverSitemap(ctx context.Context, fetcher crawler.Fetcher, domain, override string) (map[string]bool, error)
- func NewFetcher(timeout time.Duration, userAgent string) *crawler.HTTPFetcher
- func WriteCSV(w io.Writer, rules []Rule, results []RowResult) error
- type RowResult
- type Rule
- type RunOptions
- type RunStats
- type Scope
- type Verdict
Constants ¶
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.
const DefaultTimeout = 15 * time.Second
DefaultTimeout is the per-request timeout of a redirect check.
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.
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 ¶
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.
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.