clean and add some docs

This commit is contained in:
dece 2022-02-15 17:09:11 +01:00
parent cd1732c599
commit 59e664ec5c
3 changed files with 22 additions and 5 deletions

View file

@ -13,15 +13,18 @@ class UriUtilsTest {
fun joinUrls() { fun joinUrls() {
assertEquals( assertEquals(
"gemini://dece.space/some-file.gmi", "gemini://dece.space/some-file.gmi",
dev.lowrespalmtree.comet.utils.joinUrls("gemini://dece.space/", "some-file.gmi").toString() dev.lowrespalmtree.comet.utils.joinUrls("gemini://dece.space/", "some-file.gmi")
.toString()
) )
assertEquals( assertEquals(
"gemini://dece.space/some-file.gmi", "gemini://dece.space/some-file.gmi",
dev.lowrespalmtree.comet.utils.joinUrls("gemini://dece.space/", "./some-file.gmi").toString() dev.lowrespalmtree.comet.utils.joinUrls("gemini://dece.space/", "./some-file.gmi")
.toString()
) )
assertEquals( assertEquals(
"gemini://dece.space/some-file.gmi", "gemini://dece.space/some-file.gmi",
dev.lowrespalmtree.comet.utils.joinUrls("gemini://dece.space/dir1", "/some-file.gmi").toString() dev.lowrespalmtree.comet.utils.joinUrls("gemini://dece.space/dir1", "/some-file.gmi")
.toString()
) )
assertEquals( assertEquals(
"gemini://dece.space/dir1/other-file.gmi", "gemini://dece.space/dir1/other-file.gmi",

View file

@ -195,8 +195,8 @@ class PageViewModel(@Suppress("unused") private val savedStateHandle: SavedState
// We record the history entry here: it's nice because we have the main title available // We record the history entry here: it's nice because we have the main title available
// and we're already in a coroutine for database access. // and we're already in a coroutine for database access.
History.record(uri.toString(), mainTitle) History.record(uriString, mainTitle)
event.postValue(SuccessEvent(uri.toString())) event.postValue(SuccessEvent(uriString))
state.postValue(State.IDLE) state.postValue(State.IDLE)
} }

View file

@ -19,6 +19,12 @@ import javax.net.ssl.*
class Request(private val uri: Uri, private val keyManager: KeyManager? = null) { class Request(private val uri: Uri, private val keyManager: KeyManager? = null) {
private val port get() = if (uri.port > 0) uri.port else 1965 private val port get() = if (uri.port > 0) uri.port else 1965
/**
* Open and return the TLS socket with the server.
*
* If the server certificate present is fine according to our TOFU settings, the app can
* continue by calling `proceed` which will retrieve the data.
*/
fun connect(protocol: String, connectionTimeout: Int, readTimeout: Int): SSLSocket { fun connect(protocol: String, connectionTimeout: Int, readTimeout: Int): SSLSocket {
Log.d( Log.d(
TAG, TAG,
@ -34,6 +40,7 @@ class Request(private val uri: Uri, private val keyManager: KeyManager? = null)
return socket return socket
} }
/** Return a byte array channel carrying the data chunks sent by the server. */
fun proceed(socket: SSLSocket, scope: CoroutineScope): Channel<ByteArray> { fun proceed(socket: SSLSocket, scope: CoroutineScope): Channel<ByteArray> {
Log.d(TAG, "proceed") Log.d(TAG, "proceed")
socket.outputStream.write("$uri\r\n".toByteArray()) socket.outputStream.write("$uri\r\n".toByteArray())
@ -63,6 +70,13 @@ class Request(private val uri: Uri, private val keyManager: KeyManager? = null)
return channel return channel
} }
/**
* Dummy KeyManager to be used when an client cert is to be used during the connection.
*
* This simply retrieves both the public cert and private key from the Android key store
* and implement dummy methods to return only this key pair. Some methods are left unimplemented
* because they should never be executed in the context we use the key manager in.
*/
class KeyManager( class KeyManager(
private val alias: String, private val alias: String,
private val cert: X509Certificate, private val cert: X509Certificate,