clearance: Get a clearance for a host from a browser

View source: R/fetch.R

clearanceR Documentation

Get a clearance for a host from a browser

Description

Opens a Chrome on the host, lets any check run, and returns the browser's user-agent string and cookies, which clearance_handle() puts on a curl request. Whether the server then serves curl is the server's call: after a block page it usually does not, since the rule is about the client, and fetch() then downloads in the browser instead. Clearances are cached per host for the R session.

Usage

clearance(
  url,
  refresh = FALSE,
  quiet = FALSE,
  wait = NULL,
  headed = "auto",
  useragent = TRUE
)

Arguments

url

The file's URL.

refresh

Get a new clearance even when one is cached.

quiet

Suppress progress messages.

wait

Seconds to wait for the check to pass: by default two minutes with a window, so that a person can click, and thirty seconds headless.

headed

How to run the browser when one is needed. "auto", the default, runs headless and opens a window only when the headless run ends on a check or a block and there is a display to show it on; TRUE opens a window at once; FALSE never does. A second check, at the file's URL, takes headless Chrome about two minutes to pass unclicked, where a window is through as soon as its box is clicked: "auto" waits those minutes before opening a window, TRUE skips the wait.

useragent

The browser's user-agent string. TRUE, the default, drops the word Headless from Chrome's own string, which some hosts refuse on that word alone, and changes nothing else; NULL leaves Chrome's own; a string is used as given.

Value

A safeconduct_clearance: a list with host, useragent, cookie (one ⁠Cookie:⁠ header value) and time. For the errors raised, see "Errors" below.

Errors

Every error raised here is of class safeconduct_error, and one of the classes below. They are a stable interface: a calling package can catch them to fall back, to a manual download or a message of its own, without matching the message text, which may change.

  • safeconduct_unavailable: no browser can be opened here. No Chrome or Chromium was found, options(safeconduct.browser = FALSE) is set, Chrome would not start, or a browser_session() passed in has been closed. browser_available() says beforehand whether a browser can be opened, and browser_advice() gives a line to show the person when it cannot.

  • safeconduct_not_passed: a browser was opened but did not get through. Its reason field is "timeout" when the check was not passed in time and "blocked" when the server blocks the browser too.

  • safeconduct_download_error: the browser got through, but the transfer or the read failed.

For example:

tryCatch(
  fetch(url, destfile),
  safeconduct_unavailable = function(e) message(browser_advice()),
  safeconduct_not_passed = function(e) message("Download it by hand from ", url))

When a host tightens its check

A host behind Cloudflare judges the address a request comes from, not only the browser: every request from the same address counts, the browser's and the caller's own alike. After a burst of requests – some forty ranged requests in parallel to one host, in one case – the host may tighten its check for the address, so that a headless browser which got through a minute ago times out, or a window wants a click it did not need before. Keep plain requests to such a host to one or two at a time, make several reads through one browser_session() rather than a browser each, and when a check is not passed, try again in a while, or with headed = TRUE so that a person can click it.

Examples

## Not run: 
cl <- clearance("https://www12.statcan.gc.ca/")
curl::curl_fetch_memory("https://www12.statcan.gc.ca/", clearance_handle(cl))

## End(Not run)

safeconduct documentation built on Dec. 3, 2026, 1:07 a.m.