From b2ccbad03162d568c3f4728ed6e0de0d2c3e0f1d Mon Sep 17 00:00:00 2001 From: Graham Date: Sun, 21 Feb 2021 11:06:58 +0000 Subject: [PATCH] Add contentType and charset extension properties to HttpResponse Signed-off-by: Graham --- .../org/openrs2/http/HttpResponseExtensions.kt | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/http/src/main/kotlin/org/openrs2/http/HttpResponseExtensions.kt b/http/src/main/kotlin/org/openrs2/http/HttpResponseExtensions.kt index bf34c2596c..690b287476 100644 --- a/http/src/main/kotlin/org/openrs2/http/HttpResponseExtensions.kt +++ b/http/src/main/kotlin/org/openrs2/http/HttpResponseExtensions.kt @@ -1,7 +1,10 @@ package org.openrs2.http +import com.google.common.net.HttpHeaders +import com.google.common.net.MediaType import java.io.IOException import java.net.http.HttpResponse +import java.nio.charset.Charset private val DEFAULT_EXPECTED_STATUS_CODES = setOf(200) @@ -11,3 +14,12 @@ public fun HttpResponse.checkStatusCode(expectedStatusCodes: Set = D throw IOException("Unexpected status code: $status") } } + +public val HttpResponse.contentType: MediaType? + get() = headers().firstValue(HttpHeaders.CONTENT_TYPE).map(MediaType::parse).orElse(null) + +public val HttpResponse.charset: Charset? + get() { + val contentType = contentType ?: return null + return contentType.charset().orNull() + }