diff --git a/http-core/src/main/scala/org/apache/pekko/http/scaladsl/model/HttpCharset.scala b/http-core/src/main/scala/org/apache/pekko/http/scaladsl/model/HttpCharset.scala index 46967e1ed..c74ec3384 100644 --- a/http-core/src/main/scala/org/apache/pekko/http/scaladsl/model/HttpCharset.scala +++ b/http-core/src/main/scala/org/apache/pekko/http/scaladsl/model/HttpCharset.scala @@ -66,6 +66,19 @@ final case class HttpCharset private[http] (override val value: String)(val alia /** Returns the Charset for this charset if available or throws an exception otherwise */ def nioCharset: Charset = _nioCharset.get + /** + * @return this HttpCharset instance if this charset can be parsed to a + * java.nio.charset.Charset instance, otherwise returns the UTF-8 charset. + * @since 1.1.0 + */ + def charsetWithUtf8Failover: HttpCharset = { + if (_nioCharset.isSuccess) { + this + } else { + HttpCharsets.`UTF-8` + } + } + private def readObject(in: java.io.ObjectInputStream): Unit = { in.defaultReadObject() _nioCharset = HttpCharset.findNioCharset(value) diff --git a/http/src/main/scala/org/apache/pekko/http/scaladsl/marshalling/PredefinedToEntityMarshallers.scala b/http/src/main/scala/org/apache/pekko/http/scaladsl/marshalling/PredefinedToEntityMarshallers.scala index 78fdaa98d..0a5ee275d 100644 --- a/http/src/main/scala/org/apache/pekko/http/scaladsl/marshalling/PredefinedToEntityMarshallers.scala +++ b/http/src/main/scala/org/apache/pekko/http/scaladsl/marshalling/PredefinedToEntityMarshallers.scala @@ -14,7 +14,6 @@ package org.apache.pekko.http.scaladsl.marshalling import java.nio.CharBuffer -import java.nio.charset.UnsupportedCharsetException import org.apache.pekko import pekko.http.scaladsl.model.MediaTypes._ @@ -37,12 +36,7 @@ trait PredefinedToEntityMarshallers extends MultipartMarshallers { Marshaller.withOpenCharset(mediaType) { (value, charset) => // https://github.com/apache/pekko-http/issues/300 // ignore issues with invalid charset - use UTF-8 instead - try { - marshalCharArray(value, mediaType.withCharset(charset)) - } catch { - case _: UnsupportedCharsetException => - marshalCharArray(value, mediaType.withCharset(HttpCharsets.`UTF-8`)) - } + marshalCharArray(value, mediaType.withCharset(charset.charsetWithUtf8Failover)) } def charArrayMarshaller(mediaType: MediaType.WithFixedCharset): ToEntityMarshaller[Array[Char]] = Marshaller.withFixedContentType(mediaType) { value => marshalCharArray(value, mediaType) } @@ -66,12 +60,7 @@ trait PredefinedToEntityMarshallers extends MultipartMarshallers { Marshaller.withOpenCharset(mediaType) { (s, cs) => // https://github.com/apache/pekko-http/issues/300 // ignore issues with invalid charset - use UTF-8 instead - try { - HttpEntity(mediaType.withCharset(cs), s) - } catch { - case _: UnsupportedCharsetException => - HttpEntity(mediaType.withCharset(HttpCharsets.`UTF-8`), s) - } + HttpEntity(mediaType.withCharset(cs.charsetWithUtf8Failover), s) } def stringMarshaller(mediaType: MediaType.WithFixedCharset): ToEntityMarshaller[String] = Marshaller.withFixedContentType(mediaType) { s => HttpEntity(mediaType, s) }