UrlSource
A ContentSource that reads content from a URL.
content is fetched from source on every access; it is not cached.
Redirects. An HTTP redirect is not followed unless followRedirects is true (since 6.0.0; earlier releases followed them): content throws a java.net.ProtocolException naming the 3xx status instead of returning the redirect's own page, so a redirect cannot take a URL you validated to another host or port. With followRedirects true, java.net.HttpURLConnection follows a redirect to any host that keeps the protocol, and one it does not follow, between http and https or with the status 308, which it never follows, fails the same way. So does any other 3xx response, such as a 304 or one without a Location, which is not content.
Limits. connectTimeout and readTimeout bound each connection attempt and each read, not the whole fetch: a server that sends a byte now and then can keep a fetch going for as long as it likes, and the whole body is read into memory. fetchTimeout bounds the whole fetch and maxBytes the body. Both are unbounded by default, so set them when the URL or the server is not trusted.
The timeouts are applied in whole milliseconds, as java.net.URLConnection requires. A positive timeout is rounded up and capped at Int.MAX_VALUE milliseconds; Duration.INFINITE or Duration.ZERO means no timeout.
With a fetchTimeout, the fetch runs on a daemon thread of its own while the caller waits. Interrupting the caller abandons the fetch, and content throws a java.io.InterruptedIOException with the caller's interrupt status still set, so that, like every failure of content, it is an IOException. When the deadline passes, content throws a java.net.SocketTimeoutException and the fetch is abandoned: its connection is closed, or, if the fetch is in the middle of a read, the fetch stops when that read returns. So that such a read always ends, a fetchTimeout also caps connectTimeout and readTimeout, an infinite one included: no connection attempt or read waits longer than the whole fetch may take.
The exceptions UrlSource throws itself name the URL without its user info, query or fragment, which can hold credentials or tokens; those java.net.HttpURLConnection throws, such as the IOException for a 5xx status, can name the whole URL.
Parameters
the URL to read content from
the maximum time to wait for a connection (default 10 seconds)
the maximum time to wait for data once connected (default 30 seconds)
the maximum time for the whole fetch, from connecting to reading the last byte (default none); when it passes, content throws a java.net.SocketTimeoutException
the largest body accepted, in bytes (default no limit); a larger one, or a Content-Length that announces one, makes content throw an java.io.IOException
whether to follow HTTP redirects (default false); a 3xx response that is not followed, every one when false, makes content throw a java.net.ProtocolException
Throws
if a timeout or maxBytes is negative.
Inheritors
Constructors
Creates a UrlSource with the default timeouts and limits.
Creates a UrlSource with timeouts given as java.time.Duration, for Java callers: the constructor taking Kotlin durations cannot be called from Java.
Creates a UrlSource with timeouts given as java.time.Duration and every limit, for Java callers: the constructor taking Kotlin durations cannot be called from Java.