diff options
| author | Johannes Hofmann <Johannes.Hofmann@gmx.de> | 2026-11-10 09:23:16 +0100 |
|---|---|---|
| committer | Johannes Hofmann <Johannes.Hofmann@gmx.de> | 2026-11-10 09:23:23 +0100 |
| commit | 1c5ba71b7c696c2e0479326b6ec3bcc8e4bd23a1 (patch) | |
| tree | beea6e13b3aa1577ebb796b3c39b4671f65a9149 /wipfel-droid/app/src/main/kotlin/org/flpsed/wipfel/AssetServer.kt | |
| parent | 45a4b80a3d44f054a471cd417c7c2698f4770599 (diff) | |
gipfel-droid: rename from wipfel-droid
Directory, Kotlin package (org.flpsed.gipfel), applicationId/namespace,
app label and gradle project name — the wipfel working name is gone
from anything that ships.
Diffstat (limited to 'wipfel-droid/app/src/main/kotlin/org/flpsed/wipfel/AssetServer.kt')
| -rw-r--r-- | wipfel-droid/app/src/main/kotlin/org/flpsed/wipfel/AssetServer.kt | 102 |
1 files changed, 0 insertions, 102 deletions
diff --git a/wipfel-droid/app/src/main/kotlin/org/flpsed/wipfel/AssetServer.kt b/wipfel-droid/app/src/main/kotlin/org/flpsed/wipfel/AssetServer.kt deleted file mode 100644 index 8f11f50..0000000 --- a/wipfel-droid/app/src/main/kotlin/org/flpsed/wipfel/AssetServer.kt +++ /dev/null @@ -1,102 +0,0 @@ -package org.flpsed.wipfel - -import java.io.ByteArrayOutputStream -import java.io.IOException -import java.io.InputStream -import java.io.OutputStream -import java.net.ServerSocket -import java.net.Socket -import kotlin.concurrent.thread - -/** - * Minimal loopback HTTP server that serves the bundled assets. - * - * The WebView refuses fetch() on file:// origins, and WASM streaming - * instantiation needs a real HTTP origin, so we serve the site from - * 127.0.0.1 on an ephemeral port. Loopback only; nothing is exposed. - */ -class AssetServer(private val assetManager: android.content.res.AssetManager) : AutoCloseable { - private val server = ServerSocket(0, 4, java.net.InetAddress.getByName("127.0.0.1")) - val port: Int get() = server.localPort - - @Volatile - private var running = true - - init { - thread(isDaemon = true, name = "wipfel-http") { - while (running) { - try { - val socket = server.accept() - thread(isDaemon = true) { handle(socket) } - } catch (_: IOException) { - running = false - } - } - } - } - - private fun handle(socket: Socket) { - socket.use { s -> - try { - val input = s.getInputStream() - val requestLine = input.readLine() ?: return - val path = requestLine.split(" ").getOrNull(1) ?: "/" - // read (and ignore) the remaining headers so the client is happy - while (true) { - val line = input.readLine() ?: break - if (line.isEmpty()) break - } - val asset = if (path == "/" || path == "/index.html") "www/index.html" - else "www/" + path.trimStart('/') - val data = try { - assetManager.open(asset).use { it.readBytes() } - } catch (_: IOException) { - respond(s, 404, "text/plain", "not found".toByteArray()) - return - } - respond(s, 200, mime(asset), data) - } catch (_: IOException) { - } - } - } - - private fun respond(s: Socket, code: Int, ctype: String, body: ByteArray) { - val out: OutputStream = s.getOutputStream() - val header = buildString { - append("HTTP/1.1 $code OK\r\n") - append("Content-Type: $ctype\r\n") - append("Content-Length: ${body.size}\r\n") - append("Cache-Control: no-cache\r\n") - append("Access-Control-Allow-Origin: *\r\n") - append("Connection: close\r\n") - append("\r\n") - } - out.write(header.toByteArray()) - out.write(body) - out.flush() - } - - private fun mime(path: String): String = when { - path.endsWith(".wasm") -> "application/wasm" - path.endsWith(".js") -> "application/javascript" - path.endsWith(".css") -> "text/css" - path.endsWith(".html") -> "text/html" - path.endsWith(".gz") -> "application/gzip" - else -> "application/octet-stream" - } - - private fun InputStream.readLine(): String? { - val buf = ByteArrayOutputStream(64) - while (true) { - val b = read() - if (b == -1) return if (buf.size() == 0) null else buf.toString("ISO-8859-1") - if (b == '\n'.code) return buf.toString("ISO-8859-1").trimEnd('\r') - buf.write(b) - } - } - - override fun close() { - running = false - try { server.close() } catch (_: IOException) {} - } -} |
