Package com.ultikits.ultitools.utils
Class CatalogueHttp
java.lang.Object
com.ultikits.ultitools.utils.CatalogueHttp
The HTTP the module catalogue is asked with (plan 22-18, S-27, S-61).
The catalogue is public, so no request carries an Authorization header, and no
redirect is followed: a 3xx is reported as such, so a compromised catalogue cannot send the
client to another host. A network failure and an HTTP status stay distinct: CatalogueHttp.Kind.OK
means an exchange completed, whatever its status.
download(java.lang.String, java.nio.file.Path, long) streams into a file while hashing what it wrote and keeps the stream open
only while the body is at most the caller's limit; a body beyond it is cut at the limit, which
is how a download larger than its declared sizeBytes is aborted while streaming rather
than after. Timeouts: connect 10 s, read 15 s.
- Since:
- 6.3.1
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescription(package private) static final classA finished download.(package private) static final classA JSON answer.(package private) static enumWhat happened to one request. -
Field Summary
FieldsModifier and TypeFieldDescriptionprivate static final intprivate static final intprivate static final Loggerprivate static final intThe JSON reader keeps its 4 MiB (4,194,304-byte) limit under catalogue contract 1.1.0.private static final int(package private) static final StringThe response header carrying the SHA-256 of the body the catalogue served. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescription(package private) static StringbaseUrl()private static CatalogueHttp.Downloadcopy(HttpURLConnection connection, Path target, long maxBytes, String sha256Header, long declared) private static MessageDigestdigest()private static voiddisconnect(HttpURLConnection connection) (package private) static CatalogueHttp.DownloadStreamsurlintotarget, keeping at mostmaxBytesbytes of it.private static voiddrain(HttpURLConnection connection, int status) (package private) static CatalogueHttp.JsonSendsGET <api-url><pathAndQuery>.private static Stringhex(byte[] digest) private static booleanisRedirect(int status) private static HttpURLConnectionprivate static StringreadBody(HttpURLConnection connection, int status) (package private) static Stringsha256Hex(byte[] bytes)
-
Field Details
-
SHA256_HEADER
The response header carrying the SHA-256 of the body the catalogue served.- See Also:
-
CONNECT_TIMEOUT_MS
private static final int CONNECT_TIMEOUT_MS- See Also:
-
READ_TIMEOUT_MS
private static final int READ_TIMEOUT_MS- See Also:
-
MAX_JSON_BYTES
private static final int MAX_JSON_BYTESThe JSON reader keeps its 4 MiB (4,194,304-byte) limit under catalogue contract 1.1.0. Every version-list request explicitly asks for five versions, not the default twenty: a changelog allows 64,000 characters and JSON can spend six bytes per character on a Unicode escape. Five worst-case changelogs take 5 * 64,000 * 6 = 1,920,000 bytes, leaving about 2.2 MB for the envelope and other fields. Twenty would need 7,680,000 bytes. The limit is not enlarged to the 38,400,000 bytes needed for one hundred versions: this plugin shares a Minecraft server's process. A module detail needs at most 50,000 * 6 = 300,000 description bytes plus 384,000 bytes for its latest version's changelog; one version needs at most 384,000 changelog bytes. Across pages the client retains only the fields its Version uses, never the changelogs, so their large bodies do not accumulate. An answer beyond the limit is refused as too large, never truncated and treated as malformed.- See Also:
-
CHUNK
private static final int CHUNK- See Also:
-
LOGGER
-
-
Constructor Details
-
CatalogueHttp
private CatalogueHttp()
-
-
Method Details
-
getJson
SendsGET <api-url><pathAndQuery>.- Parameters:
pathAndQuery- a path beginning with/, with its query when it has one- Returns:
- the answer; never
null
-
download
Streamsurlintotarget, keeping at mostmaxBytesbytes of it.- Parameters:
url- the absolute URL to readtarget- the file to write; created or replacedmaxBytes- the most bytes to keep- Returns:
- the outcome; never
null
-
sha256Hex
- Parameters:
bytes- any bytes- Returns:
- the lower-case hex SHA-256 of them
-
copy
private static CatalogueHttp.Download copy(HttpURLConnection connection, Path target, long maxBytes, String sha256Header, long declared) throws IOException - Throws:
IOException
-
drain
-
open
- Throws:
IOException
-
disconnect
-
isRedirect
private static boolean isRedirect(int status) -
readBody
- Parameters:
connection- the open connectionstatus- its status- Returns:
- the body as UTF-8 text, or
nullwhen it is longer thanMAX_JSON_BYTES(nothing of it is then kept) - Throws:
IOException- when the body cannot be read
-
baseUrl
- Returns:
- the framework's catalogue base URL without trailing slashes, or
null
-
digest
-
hex
-