Skip to content

Commit d0ab3b9

Browse files
committed
Extract empty body check to individual function
Signed-off-by: Arnau Mora <arnyminerz@proton.me>
1 parent c24c465 commit d0ab3b9

File tree

1 file changed

+20
-3
lines changed

1 file changed

+20
-3
lines changed

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

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -722,6 +722,24 @@ open class DavResource @JvmOverloads constructor(
722722
return response
723723
}
724724

725+
/**
726+
* Checks whether the body of a [response] is set or not. This is done by checking the `Content-Length` header.
727+
* - If it's `0`, and no `Content-Type` is set, no body is given.
728+
* - If it's `-1` (not set), and `Transfer-Encoding` is not `chunked`, no body is given.
729+
* Note that this last condition is necessary because chunked body requests doesn't have a `Content-Length`.
730+
*
731+
* This request will call [Response.body], but won't close it. It's responsibility of the caller to properly close it.
732+
* @return `true` if [response] has a valid body, `false` otherwise.
733+
*/
734+
private fun isEmptyResponseBody(response: Response): Boolean {
735+
val body = response.body
736+
// if content length is 0, and content type is not set, we assume no body was given
737+
if (body.contentLength() == 0L && body.contentType() == null) return false
738+
// if content length is -1, and Transfer-Encoding is not chunked, we assume no body is present
739+
if (body.contentLength() == -1L && !response.header("Transfer-Encoding").equals("chunked", true)) return false
740+
return true
741+
}
742+
725743
/**
726744
* Validates a 207 Multi-Status response.
727745
*
@@ -737,11 +755,10 @@ open class DavResource @JvmOverloads constructor(
737755

738756
val body = response.body
739757

740-
if ((body.contentLength() == 0L && body.contentType() == null) || (body.contentLength() == -1L && !response.header("Transfer-Encoding").equals("chunked", true))) {
741-
// if content length is 0, and content type is not set, we assume no body was given
742-
// if content length is -1, and Transfer-Encoding is not chunked, we assume no body is present
758+
if (isEmptyResponseBody(response)) {
743759
throw DavException("Received 207 Multi-Status without body", httpResponse = response)
744760
}
761+
745762
body.contentType()?.let { mimeType ->
746763
if (((mimeType.type != "application" && mimeType.type != "text")) || mimeType.subtype != "xml") {
747764
/* Content-Type is not application/xml or text/xml although that is expected here.

0 commit comments

Comments
 (0)