Request: use timeout values from settings

This commit is contained in:
dece 2022-01-10 16:34:53 +01:00
parent 70beae84e7
commit d47d67be1a
3 changed files with 17 additions and 7 deletions

View file

@ -20,6 +20,7 @@ import androidx.appcompat.app.AlertDialog
import androidx.core.view.setMargins
import androidx.fragment.app.Fragment
import androidx.lifecycle.ViewModelProvider
import androidx.preference.PreferenceManager
import androidx.recyclerview.widget.LinearLayoutManager
import dev.lowrespalmtree.comet.databinding.FragmentPageViewBinding
import kotlinx.coroutines.ExperimentalCoroutinesApi
@ -112,7 +113,14 @@ class PageViewFragment : Fragment(), ContentAdapter.ContentAdapterListener {
}
when (uri.scheme) {
"gemini" -> pageViewModel.sendGeminiRequest(uri)
"gemini" -> {
val prefs = PreferenceManager.getDefaultSharedPreferences(requireContext())
val connectionTimeout =
prefs.getInt("connection_timeout", Request.DEFAULT_CONNECTION_TIMEOUT_SEC)
val readTimeout =
prefs.getInt("read_timeout", Request.DEFAULT_READ_TIMEOUT_SEC)
pageViewModel.sendGeminiRequest(uri, connectionTimeout, readTimeout)
}
else -> openUnknownScheme(uri)
}
}

View file

@ -39,14 +39,14 @@ class PageViewModel : ViewModel() {
*
* The URI must be valid, absolute and with a gemini scheme.
*/
fun sendGeminiRequest(uri: Uri, redirects: Int = 0) {
fun sendGeminiRequest(uri: Uri, connectionTimeout: Int, readTimeout: Int, redirects: Int = 0) {
Log.d(TAG, "sendRequest: URI \"$uri\"")
state.postValue(State.CONNECTING)
requestJob?.apply { if (isActive) cancel() }
requestJob = viewModelScope.launch(Dispatchers.IO) {
val response = try {
val request = Request(uri)
val socket = request.connect()
val socket = request.connect(connectionTimeout, readTimeout)
val channel = request.proceed(socket, this)
Response.from(channel, viewModelScope)
} catch (e: Exception) {

View file

@ -17,13 +17,13 @@ import javax.net.ssl.X509TrustManager
class Request(private val uri: Uri) {
private val port get() = if (uri.port > 0) uri.port else 1965
fun connect(): SSLSocket {
fun connect(connectionTimeout: Int, readTimeout: Int): SSLSocket {
Log.d(TAG, "connect")
val context = SSLContext.getInstance("TLSv1.2")
context.init(null, arrayOf(TrustManager()), null)
val socket = context.socketFactory.createSocket() as SSLSocket
socket.soTimeout = 10000
socket.connect(InetSocketAddress(uri.host, port), 10000)
socket.soTimeout = readTimeout * 1000
socket.connect(InetSocketAddress(uri.host, port), connectionTimeout * 1000)
socket.startHandshake()
return socket
}
@ -69,6 +69,8 @@ class Request(private val uri: Uri) {
}
companion object {
const val TAG = "Request"
private const val TAG = "Request"
const val DEFAULT_CONNECTION_TIMEOUT_SEC = 10
const val DEFAULT_READ_TIMEOUT_SEC = 10
}
}