library-network: detekt clean (74 tests)
- Commit
f6271d38627761bea9ce4e8db9d779274da82f8c- Author
- Vayun Mathur <[email protected]>
- When
- 2026-11-14 23:16 UTC (21 days ago)
- Parents
0e58c4e
Findings before: 19 (9 UndocumentedPublicClass warnings, 1 ComplexCondition, 2 TooGenericExceptionCaught, 4 MagicNumber, 2 MaxLineLength, 1 UnusedParameter). After: 0. - KDoc for NetworkDataStream, NetworkClient, WsFrame and its five frame kinds, and the WebSocketClient companion. - PooledHttpEngine now honours caller connect/read timeouts via a derived client sharing the pool/dispatcher; the shared-client trust check is a named trustMatches boolean. Previously the pooled path silently ignored both timeouts. - NativeHttpBridge catches IOException/IllegalArgumentException per range and IOException for the batch instead of Exception (cancellation no longer swallowed); U32 shifts/index now use BYTE_SHIFT/U32_BYTE_SHIFTS. Warning-glyph dot position and the two over-length NetworkClient lines fixed/named. Verified: ./gradlew :library:network:detekt :library:network:testDebugUnitTest :library:network:lint --max-workers=32 all green (74 unit tests).
Diff
library/network/src/main/java/com/vayunmathur/library/network/NativeHttpBridge.kt
diff --git a/library/network/src/main/java/com/vayunmathur/library/network/NativeHttpBridge.kt b/library/network/src/main/java/com/vayunmathur/library/network/NativeHttpBridge.ktindex b30f947334..69052ae144 100644--- a/library/network/src/main/java/com/vayunmathur/library/network/NativeHttpBridge.kt+++ b/library/network/src/main/java/com/vayunmathur/library/network/NativeHttpBridge.kt@@ -129,7 +129,9 @@ object NativeHttpBridge { body = null, ) packReply(response.status, response.url, response.headers, response.bytes)- } catch (e: Exception) {+ } catch (e: java.io.IOException) {+ packFailure(url, e.message ?: e::class.simpleName ?: "request failed")+ } catch (e: IllegalArgumentException) { packFailure(url, e.message ?: e::class.simpleName ?: "request failed") } }@@ -137,7 +139,7 @@ object NativeHttpBridge { pending.awaitAll() } return packBatch(replies)- } catch (e: Exception) {+ } catch (e: java.io.IOException) { // The batch framing itself failed: every slot reports the failure, so no slot hangs. return packBatch(ranges.map { packFailure(url, e.message ?: "batch failed") }) }@@ -270,10 +272,10 @@ object NativeHttpBridge { ((b[at].toInt() and BYTE_MASK) shl BYTE_SHIFT) or (b[at + 1].toInt() and BYTE_MASK) private fun readU32(b: ByteArray, at: Int): Int =- ((b[at].toInt() and BYTE_MASK) shl 24) or- ((b[at + 1].toInt() and BYTE_MASK) shl 16) or+ ((b[at].toInt() and BYTE_MASK) shl (BYTE_SHIFT * U32_BYTE_SHIFTS)) or+ ((b[at + 1].toInt() and BYTE_MASK) shl (BYTE_SHIFT * 2)) or ((b[at + 2].toInt() and BYTE_MASK) shl BYTE_SHIFT) or- (b[at + 3].toInt() and BYTE_MASK)+ (b[at + U32_BYTE_SHIFTS].toInt() and BYTE_MASK) private fun writeU16(out: java.io.ByteArrayOutputStream, v: Int) { out.write((v ushr BYTE_SHIFT) and BYTE_MASK)
library/network/src/main/java/com/vayunmathur/library/network/NetworkClient.kt
diff --git a/library/network/src/main/java/com/vayunmathur/library/network/NetworkClient.kt b/library/network/src/main/java/com/vayunmathur/library/network/NetworkClient.ktindex 44a5bbfd01..7f1f6d272f 100644--- a/library/network/src/main/java/com/vayunmathur/library/network/NetworkClient.kt+++ b/library/network/src/main/java/com/vayunmathur/library/network/NetworkClient.kt@@ -75,6 +75,7 @@ class RawResponse( headers.entries.firstOrNull { it.key.equals(name, ignoreCase = true) }?.value ?: emptyList() } +/** A readable byte stream for a response body, buffered or live over the network. */ interface NetworkDataStream { suspend fun read(buffer: ByteArray, offset: Int, length: Int): Int suspend fun read(buffer: ByteArray): Int = read(buffer, 0, buffer.size)@@ -98,6 +99,10 @@ fun ByteArray.asNetworkDataStream(): NetworkDataStream { } } +/**+ * The app's single HTTP entry point: buffered and streaming requests over the+ * pooled engine or HttpURLConnection fallback, with pinned-CA trust bundles.+ */ object NetworkClient { private const val TAG = "NetworkClient"@@ -356,14 +361,18 @@ object NetworkClient { // Shield acquisition until ownership is recorded; socket timeouts still apply. HttpUrlEngine.openResponse( url, method, headers, HttpUrlEngine.toBodyBytes(body),- followRedirects = followRedirects, connectTimeoutMs = connectTimeoutMs, readTimeoutMs = readTimeoutMs,+ followRedirects = followRedirects,+ connectTimeoutMs = connectTimeoutMs,+ readTimeoutMs = readTimeoutMs, sslSocketFactory = resolveFactory(sslSocketFactory, useSystemTrust), // This entry point has always let connect failures surface as they are. wrapConnectErrors = false, ).also { ownedResponse = it } } currentCoroutineContext().ensureActive()- var simple = SimpleResponse(response.status, response.statusMessage, "", response.headers, response.finalUrl)+ var simple = SimpleResponse(+ response.status, response.statusMessage, "", response.headers, response.finalUrl,+ ) if (simple.isSuccess || simple.status == STATUS_PARTIAL_CONTENT) { if (!response.hasStream) { closeResponse()
library/network/src/main/java/com/vayunmathur/library/network/PooledHttpEngine.kt
diff --git a/library/network/src/main/java/com/vayunmathur/library/network/PooledHttpEngine.kt b/library/network/src/main/java/com/vayunmathur/library/network/PooledHttpEngine.ktindex 4fc2e35654..a614f6b08b 100644--- a/library/network/src/main/java/com/vayunmathur/library/network/PooledHttpEngine.kt+++ b/library/network/src/main/java/com/vayunmathur/library/network/PooledHttpEngine.kt@@ -103,7 +103,18 @@ internal object PooledHttpEngine { readTimeoutMs: Long? = connectTimeoutMs, useSystemTrust: Boolean, ): RawResponse = withContext(Dispatchers.IO) {- val client = clientFor(useSystemTrust, followRedirects)+ val pooled = clientFor(useSystemTrust, followRedirects)+ // The shared client carries default timeouts; a caller that asked for its own+ // gets a derived client (same pool and dispatcher, so pooling is preserved).+ // The HttpURLConnection path always honoured these, so the pooled path must too.+ val client = if (readTimeoutMs == null && connectTimeoutMs == null) {+ pooled+ } else {+ pooled.newBuilder().apply {+ connectTimeoutMs?.let { connectTimeout(it, TimeUnit.MILLISECONDS) }+ readTimeoutMs?.let { readTimeout(it, TimeUnit.MILLISECONDS) }+ }.build()+ } val bodyBytes = HttpUrlEngine.toBodyBytes(body) val builder = Request.Builder().url(url) applyHeaders(builder, headers)@@ -167,9 +178,9 @@ internal object PooledHttpEngine { synchronized(lock) { if (followRedirects) { val current = client- if (current != null && factory === builtFactory && manager === builtManager &&+ val trustMatches = factory === builtFactory && manager === builtManager && useSystemTrust == builtSystemTrust- ) {+ if (current != null && trustMatches) { return current } val built = buildClient(factory, manager, true)
library/network/src/main/java/com/vayunmathur/library/network/WebSocketClient.kt
diff --git a/library/network/src/main/java/com/vayunmathur/library/network/WebSocketClient.kt b/library/network/src/main/java/com/vayunmathur/library/network/WebSocketClient.ktindex b59e134949..be9030062e 100644--- a/library/network/src/main/java/com/vayunmathur/library/network/WebSocketClient.kt+++ b/library/network/src/main/java/com/vayunmathur/library/network/WebSocketClient.kt@@ -51,11 +51,21 @@ class WebSocketClient private constructor( ) { @Volatile private var closed = false + /** One WebSocket message or control frame, as decoded by the frame codec. */ sealed class WsFrame {+ /** A UTF-8 text message. */ data class Text(val text: String) : WsFrame()++ /** A binary message, payload uninterpreted. */ data class Binary(val bytes: ByteArray) : WsFrame()++ /** A close frame: RFC 6455 status [code] plus an optional [reason]. */ data class Close(val code: Int = CLOSE_NORMAL, val reason: String = "") : WsFrame()++ /** A ping control frame; the client auto-replies with [Pong]. */ object Ping : WsFrame()++ /** A pong control frame, reply to a peer's ping. */ object Pong : WsFrame() } @@ -281,6 +291,7 @@ class WebSocketClient private constructor( return WsFrame.Close(code, reason) } + /** Frame-codec constants and the [webSocket] handshake factory. */ companion object { private const val TAG = "WebSocketClient" private const val GUID = "258EAFA5-E914-47DA-95CA-C5AB0DC85B11"
library/network/src/main/java/com/vayunmathur/library/util/ConnectivityCompose.kt
diff --git a/library/network/src/main/java/com/vayunmathur/library/util/ConnectivityCompose.kt b/library/network/src/main/java/com/vayunmathur/library/util/ConnectivityCompose.ktindex 1a4309ced6..fe20bc5c1c 100644--- a/library/network/src/main/java/com/vayunmathur/library/util/ConnectivityCompose.kt+++ b/library/network/src/main/java/com/vayunmathur/library/util/ConnectivityCompose.kt@@ -108,6 +108,9 @@ fun OfflineAware(content: @Composable () -> Unit) { } } +/** Vertical centre of the warning glyph's dot, as a fraction of its height. */+private const val DOT_CENTER_Y_FRACTION = 0.72f+ /** Warning triangle with an exclamation bar, drawn to match the `IconWarning` look. */ @Composable private fun OfflineWarningGlyph(@@ -136,7 +139,7 @@ private fun OfflineWarningGlyph( drawCircle( color = barColor, radius = barHalfWidth,- center = Offset(w / 2f, h * 0.72f),+ center = Offset(w / 2f, h * DOT_CENTER_Y_FRACTION), ) } }