Skip to content

Commit 9b8689c

Browse files
committed
refactor whip auth logic
1 parent 58fc3ba commit 9b8689c

2 files changed

Lines changed: 19 additions & 33 deletions

File tree

whip/src/main/java/com/pedro/whip/WhipClient.kt

Lines changed: 14 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -230,39 +230,23 @@ class WhipClient(private val connectChecker: ConnectChecker) {
230230
Log.i(TAG, "found ${localCandidates.size} candidates")
231231
val offerResponse = commandsManager.writeOffer()
232232
Log.i(TAG, offerResponse.toString())
233-
when (offerResponse.statusCode) {
234-
403 -> {
235-
onMainThread {
236-
connectChecker.onConnectionFailed("Error configure stream, access denied")
237-
}
238-
Log.e(TAG, "Response 403, access denied")
239-
return@launch
240-
}
241-
401 -> {
242-
if (commandsManager.token == null) {
243-
onMainThread { connectChecker.onAuthError() }
233+
if (offerResponse.statusCode !in 200..299) {
234+
val needAuth = offerResponse.statusCode in 400..499
235+
if (needAuth && commandsManager.token != null) {
236+
val offerResponseAuth = commandsManager.writeOffer(sendAuth = true)
237+
Log.i(TAG, offerResponseAuth.toString())
238+
if (offerResponseAuth.statusCode !in 200..299) {
239+
onMainThread {
240+
connectChecker.onConnectionFailed("Error configure stream, offer failed: ${offerResponseAuth.statusCode}")
241+
}
244242
return@launch
245243
} else {
246-
val offerResponseAuth = commandsManager.writeOffer(sendAuth = true)
247-
Log.i(TAG, offerResponseAuth.toString())
248-
when (offerResponseAuth.statusCode) {
249-
401 -> {
250-
onMainThread { connectChecker.onAuthError() }
251-
return@launch
252-
}
253-
in 200..299 -> {
254-
onMainThread { connectChecker.onAuthSuccess() }
255-
}
256-
else -> {
257-
onMainThread {
258-
connectChecker.onConnectionFailed("Error configure stream, offer with auth failed: ${offerResponseAuth.statusCode}")
259-
}
260-
return@launch
261-
}
262-
}
244+
onMainThread { connectChecker.onAuthSuccess() }
263245
}
264-
}
265-
!in 200..299 -> {
246+
} else if (needAuth) {
247+
onMainThread { connectChecker.onAuthError() }
248+
return@launch
249+
} else {
266250
onMainThread {
267251
connectChecker.onConnectionFailed("Error configure stream, offer failed: ${offerResponse.statusCode}")
268252
}

whip/src/main/java/com/pedro/whip/utils/Requests.kt

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,16 +34,18 @@ object Requests {
3434
try {
3535
socket.connect()
3636
if (body != null) socket.outputStream.write(body.toByteArray())
37-
Log.i("Requests", "$method, code: ${socket.responseCode}\nheaders: $headers\nbody: $body")
38-
val bytes = socket.inputStream.readBytes()
37+
val code = socket.responseCode
38+
Log.i("Requests", "$method, code: $code\nheaders: $headers\nbody: $body")
39+
val stream = if (code in 200..399) socket.inputStream else socket.errorStream
40+
val bytes = stream?.readBytes() ?: ByteArray(0)
3941
val responseHeaders = mutableMapOf<String, String>()
4042
socket.headerFields.forEach {
4143
try {
4244
responseHeaders[it.key] = it.value.joinToString(", ")
4345
} catch (_: Exception){}
4446
}
4547
val bodyResult = String(bytes)
46-
return RequestResponse(socket.responseCode, responseHeaders, bodyResult)
48+
return RequestResponse(code, responseHeaders, bodyResult)
4749
} catch (e : Exception) {
4850
Log.e("Requests", "Error", e)
4951
return RequestResponse(-1, emptyMap(), "")

0 commit comments

Comments
 (0)