module BBS::HTTP
HTTP requests of a board to the services it shows (an encyclopedia, a feed, a catalog), bounded so a slow or huge answer cannot hold a caller’s thread or fill the memory: a deadline for the whole request, a cap on the body, a few redirects to http and https only, and by default no request to an address that is not on the public internet.
response = BBS::HTTP.get('https://en.wikipedia.org/w/api.php?action=query&format=json') JSON.parse(response.body) if response.ok?
Every failure (no answer, a timeout, a body over the cap, a refused address, a bad redirect) raises BBS::HTTP::Error, with a message that names the address without its query string, so no key or search term lands in a log. A body over the cap raises its subclass TooLarge, as soon as the Content-Length says so.
A big download goes to an IO instead of the memory:
File.open('book.txt', 'wb') { |file| BBS::HTTP.get(url, to: file, max_bytes: 64 * 1024 * 1024) }
Constants
- FAILURES
-
The errors of a request that become an
Error. - MAX_BYTES
-
The default cap on the body: 4 MiB.
- REDIRECTS
-
The status codes of a redirect.
- Response
-
The answer to a request.
- status
-
the
HTTPstatus code, an Integer - body
-
the body, a binary String (
force_encodingit as needed) - headers
-
the header fields, a Hash of lowercase name => value String
The body is
nilwhen a 2xx body went to thetoIO ofHTTP.get. - SCHEMES
-
The URI schemes a request and its redirects may use.
Public Instance Methods
Source
# File lib/bbs/http.rb, line 77 def get(url, **) = Request.new(:get, url, **).run
Sends a GET request to url and returns its Response (a non-2xx one too). Raises Error when it fails.
- headers
-
extra request header fields, a Hash
- open_timeout
-
seconds to wait for the connection
- read_timeout
-
seconds to wait for each read
- deadline
-
seconds the whole request may take, redirects included
- max_bytes
-
the most bytes of body read; more is a
TooLarge - to
-
an IO the body of a 2xx answer is written to as it comes, instead of the Response’s
body(nilthen); still capped bymax_bytes, and a failure halfway leaves what came in the IO - redirects
-
the most redirects followed (to http or https only)
- user_agent
-
the User-Agent field; default
BBS.config.user_agent - public_only
-
refuse a host that is not on the public internet (
BBS::Address.public?); the address checked is the one connected to, so a name cannot resolve twice
Source
# File lib/bbs/http.rb, line 85 def post(url, body:, **) = Request.new(:post, url, body: body, redirects: 0, **).run