Skip to content

Commit 7843fb4

Browse files
committed
Use peekBody
Signed-off-by: Arnau Mora <arnyminerz@proton.me>
1 parent c26160f commit 7843fb4

File tree

1 file changed

+21
-22
lines changed

1 file changed

+21
-22
lines changed

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

Lines changed: 21 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -725,8 +725,6 @@ open class DavResource @JvmOverloads constructor(
725725
/**
726726
* Validates a 207 Multi-Status response.
727727
*
728-
* This function fetches [response]'s [Response.body], and doesn't close it.
729-
*
730728
* @param response will be checked for Multi-Status response
731729
*
732730
* @throws DavException if the response is not a Multi-Status response
@@ -735,28 +733,29 @@ open class DavResource @JvmOverloads constructor(
735733
if (response.code != HTTP_MULTISTATUS)
736734
throw DavException("Expected 207 Multi-Status, got ${response.code} ${response.message}", httpResponse = response)
737735

738-
val body = response.body
739-
body.contentType()?.let { mimeType ->
740-
if (((mimeType.type != "application" && mimeType.type != "text")) || mimeType.subtype != "xml") {
741-
/* Content-Type is not application/xml or text/xml although that is expected here.
742-
Some broken servers return an XML response with some other MIME type. So we try to see
743-
whether the response is maybe XML although the Content-Type is something else. */
744-
try {
745-
val firstBytes = ByteArray(XML_SIGNATURE.size)
746-
body.source().peek().readFully(firstBytes)
747-
if (XML_SIGNATURE.contentEquals(firstBytes)) {
748-
logger.warning("Received 207 Multi-Status that seems to be XML but has MIME type $mimeType")
749-
750-
// response is OK, return and do not throw Exception below
751-
return
736+
response.peekBody(XML_SIGNATURE.size.toLong()).use { body ->
737+
body.contentType()?.let { mimeType ->
738+
if (((mimeType.type != "application" && mimeType.type != "text")) || mimeType.subtype != "xml") {
739+
/* Content-Type is not application/xml or text/xml although that is expected here.
740+
Some broken servers return an XML response with some other MIME type. So we try to see
741+
whether the response is maybe XML although the Content-Type is something else. */
742+
try {
743+
response.peekBody(XML_SIGNATURE.size.toLong()).use { body ->
744+
if (XML_SIGNATURE.contentEquals(body.bytes())) {
745+
logger.warning("Received 207 Multi-Status that seems to be XML but has MIME type $mimeType")
746+
747+
// response is OK, return and do not throw Exception below
748+
return
749+
}
750+
}
751+
} catch (e: Exception) {
752+
logger.log(Level.WARNING, "Couldn't scan for XML signature", e)
752753
}
753-
} catch (e: Exception) {
754-
logger.log(Level.WARNING, "Couldn't scan for XML signature", e)
755-
}
756754

757-
throw DavException("Received non-XML 207 Multi-Status", httpResponse = response)
758-
}
759-
} ?: logger.warning("Received 207 Multi-Status without Content-Type, assuming XML")
755+
throw DavException("Received non-XML 207 Multi-Status", httpResponse = response)
756+
}
757+
} ?: logger.warning("Received 207 Multi-Status without Content-Type, assuming XML")
758+
}
760759
}
761760

762761

0 commit comments

Comments
 (0)