Skip to content

Commit 516ee0d

Browse files
committed
Refactor
Signed-off-by: Arnau Mora <arnyminerz@proton.me>
1 parent 9618eff commit 516ee0d

File tree

1 file changed

+44
-33
lines changed

1 file changed

+44
-33
lines changed

src/main/kotlin/at/bitfire/dav4jvm/DavResource.kt

Lines changed: 44 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,24 @@ package at.bitfire.dav4jvm
1313
import at.bitfire.dav4jvm.DavResource.Companion.MAX_REDIRECTS
1414
import at.bitfire.dav4jvm.XmlUtils.insertTag
1515
import at.bitfire.dav4jvm.XmlUtils.propertyName
16-
import at.bitfire.dav4jvm.exception.*
16+
import at.bitfire.dav4jvm.exception.ConflictException
17+
import at.bitfire.dav4jvm.exception.DavException
18+
import at.bitfire.dav4jvm.exception.ForbiddenException
19+
import at.bitfire.dav4jvm.exception.HttpException
20+
import at.bitfire.dav4jvm.exception.NotFoundException
21+
import at.bitfire.dav4jvm.exception.PreconditionFailedException
22+
import at.bitfire.dav4jvm.exception.ServiceUnavailableException
23+
import at.bitfire.dav4jvm.exception.UnauthorizedException
1724
import at.bitfire.dav4jvm.property.caldav.NS_CALDAV
1825
import at.bitfire.dav4jvm.property.carddav.NS_CARDDAV
1926
import at.bitfire.dav4jvm.property.webdav.NS_WEBDAV
2027
import at.bitfire.dav4jvm.property.webdav.SyncToken
21-
import okhttp3.*
28+
import okhttp3.Headers
29+
import okhttp3.HttpUrl
2230
import okhttp3.MediaType.Companion.toMediaType
31+
import okhttp3.OkHttpClient
32+
import okhttp3.Request
33+
import okhttp3.RequestBody
2334
import okhttp3.RequestBody.Companion.toRequestBody
2435
import okhttp3.Response
2536
import org.xmlpull.v1.XmlPullParser
@@ -714,44 +725,44 @@ open class DavResource @JvmOverloads constructor(
714725
/**
715726
* Validates a 207 Multi-Status response.
716727
*
728+
* This function fetches [response]'s [Response.body], and doesn't close it.
729+
*
717730
* @param response will be checked for Multi-Status response
718731
*
719732
* @throws DavException if the response is not a Multi-Status response
720733
*/
721-
inline fun assertMultiStatus(response: Response, handleBody: (ResponseBody) -> Unit = {}) {
734+
fun assertMultiStatus(response: Response) {
722735
if (response.code != HTTP_MULTISTATUS)
723736
throw DavException("Expected 207 Multi-Status, got ${response.code} ${response.message}", httpResponse = response)
724737

725-
response.body.use { body ->
726-
if (body.contentLength() <= 0 && body.contentType() == null) {
727-
// if content length is 0 or less, and no Content-Type is set,
728-
// we assume body was not given, but we expect a body for Multi-Status
729-
throw DavException("Received 207 Multi-Status without body", httpResponse = response)
730-
}
731-
body.contentType()?.let { mimeType ->
732-
if (((mimeType.type != "application" && mimeType.type != "text")) || mimeType.subtype != "xml") {
733-
/* Content-Type is not application/xml or text/xml although that is expected here.
734-
Some broken servers return an XML response with some other MIME type. So we try to see
735-
whether the response is maybe XML although the Content-Type is something else. */
736-
try {
737-
val firstBytes = ByteArray(XML_SIGNATURE.size)
738-
body.source().peek().readFully(firstBytes)
739-
if (XML_SIGNATURE.contentEquals(firstBytes)) {
740-
logger.warning("Received 207 Multi-Status that seems to be XML but has MIME type $mimeType")
741-
742-
// response is OK, return and do not throw Exception below
743-
return
744-
}
745-
} catch (e: Exception) {
746-
logger.log(Level.WARNING, "Couldn't scan for XML signature", e)
747-
}
738+
val body = response.body
748739

749-
throw DavException("Received non-XML 207 Multi-Status", httpResponse = response)
740+
if (body.contentLength() == 0L && body.contentType() == null || body.contentLength() == -1L) {
741+
// if content length is 0, and content type is not set, we assume no body was given
742+
// if content length is -1, its associated header was not given, and as such, body is not present
743+
throw DavException("Received 207 Multi-Status without body", httpResponse = response)
744+
}
745+
body.contentType()?.let { mimeType ->
746+
if (((mimeType.type != "application" && mimeType.type != "text")) || mimeType.subtype != "xml") {
747+
/* Content-Type is not application/xml or text/xml although that is expected here.
748+
Some broken servers return an XML response with some other MIME type. So we try to see
749+
whether the response is maybe XML although the Content-Type is something else. */
750+
try {
751+
val firstBytes = ByteArray(XML_SIGNATURE.size)
752+
body.source().peek().readFully(firstBytes)
753+
if (XML_SIGNATURE.contentEquals(firstBytes)) {
754+
logger.warning("Received 207 Multi-Status that seems to be XML but has MIME type $mimeType")
755+
756+
// response is OK, return and do not throw Exception below
757+
return
758+
}
759+
} catch (e: Exception) {
760+
logger.log(Level.WARNING, "Couldn't scan for XML signature", e)
750761
}
751-
} ?: logger.warning("Received 207 Multi-Status without Content-Type, assuming XML")
752762

753-
handleBody(body)
754-
}
763+
throw DavException("Received non-XML 207 Multi-Status", httpResponse = response)
764+
}
765+
} ?: logger.warning("Received 207 Multi-Status without Content-Type, assuming XML")
755766
}
756767

757768

@@ -772,10 +783,10 @@ open class DavResource @JvmOverloads constructor(
772783
*/
773784
protected fun processMultiStatus(response: Response, callback: MultiResponseCallback): List<Property> {
774785
checkStatus(response)
775-
assertMultiStatus(response) {
776-
return processMultiStatus(it.charStream(), callback)
786+
assertMultiStatus(response)
787+
return response.body.use {
788+
processMultiStatus(it.charStream(), callback)
777789
}
778-
return emptyList()
779790
}
780791

781792
/**

0 commit comments

Comments
 (0)