@@ -11,10 +11,12 @@ import io.em2m.actions.model.TypedActionFlow
1111import io.em2m.problem.Problem
1212import io.em2m.simplex.evalPath
1313import io.em2m.utils.coerce
14+ import io.em2m.utils.parseCharset
1415import org.xerial.snappy.SnappyInputStream
1516import java.io.ByteArrayOutputStream
1617import java.io.IOException
1718import java.io.InputStream
19+ import java.nio.charset.Charset
1820import java.util.*
1921import java.util.zip.DeflaterInputStream
2022import java.util.zip.GZIPInputStream
@@ -33,6 +35,8 @@ class JacksonRequestTransformer(
3335
3436 val contentType = (ctx.environment[" ContentType" ] as ? String ? : " " ).lowercase(Locale .getDefault())
3537
38+ val charset = parseCharset(contentType)
39+
3640 if (contentType.contains(" xml" )) return
3741
3842 try {
@@ -43,7 +47,7 @@ class JacksonRequestTransformer(
4347 " snappy" -> SnappyInputStream (ctx.inputStream)
4448 else -> ctx.inputStream
4549 }
46- val sanitizedInputStream = sanitizeInputStream(inputStream)
50+ val sanitizedInputStream = sanitizeInputStream(inputStream, charset )
4751 val obj = objectMapper.readValue(sanitizedInputStream, type)
4852// val obj = objectMapper.readValue(inputStream, type)
4953 ctx.request = obj
@@ -92,25 +96,14 @@ class JacksonRequestTransformer(
9296 }
9397 }
9498
95- private fun sanitizeInputStream (inputStream : InputStream ? ): InputStream ? {
99+ private fun sanitizeInputStream (inputStream : InputStream ? , charset : Charset = Charsets . UTF_8 ): InputStream ? {
96100 if (inputStream == null ) {
97101 return null
98102 }
99103
100- val buffer = ByteArray (8192 )
101- val outputStream = ByteArrayOutputStream ()
102-
103- inputStream.buffered().use { input ->
104- outputStream.use { output ->
105- var bytesRead: Int
106- while (input.read(buffer).also { bytesRead = it } != - 1 ) {
107- val sanitizedChunk = removeScriptTags(String (buffer, 0 , bytesRead))
108- output.write(sanitizedChunk.toByteArray())
109- }
110- }
111- }
112-
113- return outputStream.toByteArray().inputStream()
104+ val request = inputStream.reader(charset).use { it.readText() }
105+ val sanitized = removeScriptTags(request)
106+ return sanitized.byteInputStream(charset)
114107 }
115108
116109 private fun removeScriptTags (input : String ): String {
0 commit comments