Commit b9e58df7 authored by Ricki Hirner's avatar Ricki Hirner

HttpException body formatting

* don't rely on InputStream.available()
* don't dump more than 20 kB
parent 28d41455
Pipeline #11610052 failed with stage
in 1 minute and 11 seconds
......@@ -15,6 +15,9 @@ import java.io.*
open class HttpException: Exception, Serializable {
// don't dump more than 10 kB
private val MAX_DUMP_SIZE = 20*1024
val status: Int
val request: String?
val response: String?
......@@ -73,8 +76,7 @@ open class HttpException: Exception, Serializable {
formatted.append(name).append(": ").append(value).append("\n")
response.body()?.use {
formatted.append("Content-Type: ").append(it.contentType()).append("\n")
formatted.append("Content-Length: ").append(it.contentLength()).append("\n")
formatted.append("[body length: ").append(it.contentLength()).append(" bytes]").append("\n")
try {
val baos = ByteArrayOutputStream()
formatByteStream(it.byteStream(), baos)
......@@ -88,14 +90,21 @@ open class HttpException: Exception, Serializable {
private fun formatByteStream(input: InputStream, output: ByteArrayOutputStream) {
OutputStreamWriter(output).use { writer ->
while (input.available() != 0) {
val b = input.read()
var b = input.read()
var written = 0
while (b != -1) {
if (written++ >= MAX_DUMP_SIZE) {
writer.append("[…]")
break
}
when (b) {
'\r'.toInt() -> writer.append("[CR]")
'\n'.toInt() -> writer.append("[LF]\n")
'\t'.toInt() -> writer.append('↦')
'\r'.toInt() -> writer.append('↵')
'\n'.toInt() -> writer.append('\n')
in 0x20..0x7E -> writer.write(b) // printable ASCII
else -> writer.append("[${String.format("%02x", b)}]")
else -> writer.append("[${String.format("%02x", b)}]")
}
b = input.read()
}
}
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment