@@ -13,6 +13,7 @@ import kotlinx.serialization.descriptors.PrimitiveSerialDescriptor
1313import kotlinx.serialization.descriptors.SerialDescriptor
1414import kotlinx.serialization.encoding.Decoder
1515import kotlinx.serialization.json.Json
16+ import kotlinx.serialization.serializer
1617
1718object HttpRequestStateSerializer : KSerializer<HttpRequestState> {
1819
@@ -26,9 +27,10 @@ object HttpRequestStateSerializer : KSerializer<HttpRequestState> {
2627 " id" to value.id.toString(),
2728 " timeSignMillis" to value.timeSignMillis.toString(),
2829 " url" to value.url.toString(),
29- " header" to value.header.toString(),
30- " request" to value.request.toString(),
31- " response" to value.response.toString(),
30+ " requestHeader" to value.requestHeader.toString(),
31+ " requestBody" to value.requestBody.toString(),
32+ " responseHeader" to value.responseHeader.toString(),
33+ " responseBody" to value.responseBody.toString(),
3234 " status" to value.status.toString(),
3335 " method" to value.method.toString()
3436 )
@@ -44,8 +46,8 @@ object HttpRequestStateSerializer : KSerializer<HttpRequestState> {
4446 id = data[" id" ] ? : throw IllegalStateException (" Data corrupted" ),
4547 timeSignMillis = data[" timeSignMillis" ]?.toLong() ? : throw IllegalStateException (" Data corrupted" ),
4648 url = data[" url" ],
47- header = data[" header " ],
48- request = data[" request " ],
49+ responseHeader = data[" responseHeader " ],
50+ requestBody = data[" requestBody " ],
4951 method = data[" method" ],
5052 )
5153 }
@@ -55,9 +57,10 @@ object HttpRequestStateSerializer : KSerializer<HttpRequestState> {
5557 id = data[" id" ] ? : throw IllegalStateException (" Data corrupted" ),
5658 timeSignMillis = data[" timeSignMillis" ]?.toLong() ? : throw IllegalStateException (" Data corrupted" ),
5759 url = data[" url" ],
58- header = data[" header" ],
59- request = data[" request" ],
60- response = data[" response" ],
60+ requestHeader = data[" requestHeader" ],
61+ requestBody = data[" requestBody" ],
62+ responseHeader = data[" responseHeader" ],
63+ responseBody = data[" responseBody" ],
6164 status = data[" status" ]?.toInt(),
6265 method = data[" method" ],
6366 )
@@ -68,9 +71,10 @@ object HttpRequestStateSerializer : KSerializer<HttpRequestState> {
6871 id = data[" id" ] ? : throw IllegalStateException (" Data corrupted" ),
6972 timeSignMillis = data[" timeSignMillis" ]?.toLong() ? : throw IllegalStateException (" Data corrupted" ),
7073 url = data[" url" ],
71- header = data[" header" ],
72- request = data[" request" ],
73- response = data[" response" ],
74+ requestHeader = data[" requestHeader" ],
75+ requestBody = data[" requestBody" ],
76+ responseHeader = data[" responseHeader" ],
77+ responseBody = data[" responseBody" ],
7478 status = data[" status" ]?.toInt(),
7579 method = data[" method" ],
7680 )
@@ -81,9 +85,10 @@ object HttpRequestStateSerializer : KSerializer<HttpRequestState> {
8185 id = data[" id" ] ? : throw IllegalStateException (" Data corrupted" ),
8286 timeSignMillis = data[" timeSignMillis" ]?.toLong() ? : throw IllegalStateException (" Data corrupted" ),
8387 url = data[" url" ],
84- header = data[" header" ],
85- request = data[" request" ],
86- response = data[" response" ],
88+ requestHeader = data[" requestHeader" ],
89+ requestBody = data[" requestBody" ],
90+ responseHeader = data[" responseHeader" ],
91+ responseBody = data[" responseBody" ],
8792 status = data[" status" ]?.toInt(),
8893 method = data[" method" ],
8994 )
@@ -99,9 +104,10 @@ sealed class HttpRequestState(
99104 open val id : String ,
100105 open val timeSignMillis : Long ,
101106 open val url : String? = null ,
102- open val header : String? = null ,
103- open val request : String? = null ,
104- open val response : String? = null ,
107+ open val requestHeader : String? = null ,
108+ open val requestBody : String? = null ,
109+ open val responseHeader : String? = null ,
110+ open val responseBody : String? = null ,
105111 open val status : Int? = null ,
106112 open val method : String? = null
107113) {
@@ -110,45 +116,52 @@ sealed class HttpRequestState(
110116 override val id : String ,
111117 override val timeSignMillis : Long = getTimeMillis(),
112118 override val url : String? ,
113- override val header : String? ,
114- override val request : String? ,
119+ override val responseHeader : String? ,
120+ override val requestBody : String? ,
115121 override val method : String? ,
116- ) : HttpRequestState(id, timeSignMillis, url, header, request )
122+ ) : HttpRequestState(id, timeSignMillis, url, responseHeader, requestBody )
117123
118124 data class Success (
119125 override val id : String ,
120126 override val timeSignMillis : Long = getTimeMillis(),
121127 override val url : String? ,
122- override val header : String? ,
123- override val request : String? ,
124- override val response : String? ,
128+ override val requestHeader : String? = null ,
129+ override val requestBody : String? ,
130+ override val responseHeader : String? ,
131+ override val responseBody : String? ,
125132 override val status : Int? ,
126133 override val method : String? ,
127- ) : HttpRequestState(id, timeSignMillis, url, header, request, response )
134+ ) : HttpRequestState(id, timeSignMillis, url, responseHeader, requestBody, responseBody )
128135
129136 data class Error (
130137 override val id : String ,
131138 override val timeSignMillis : Long = getTimeMillis(),
132139 override val url : String? ,
133- override val header : String? ,
134- override val request : String? ,
135- override val response : String? ,
140+ override val requestHeader : String? = null ,
141+ override val requestBody : String? ,
142+ override val responseHeader : String? ,
143+ override val responseBody : String? ,
136144 override val status : Int? ,
137145 override val method : String? ,
138- ) : HttpRequestState(id, timeSignMillis, url, header, request, response )
146+ ) : HttpRequestState(id, timeSignMillis, url, responseHeader, requestBody, responseBody )
139147
140148 data class Spoofed (
141149 override val id : String ,
142150 override val timeSignMillis : Long = getTimeMillis(),
143151 override val url : String? ,
144- override val header : String? ,
145- override val request : String? ,
146- override val response : String? ,
152+ override val requestHeader : String? = null ,
153+ override val requestBody : String? ,
154+ override val responseHeader : String? ,
155+ override val responseBody : String? ,
147156 override val status : Int? ,
148157 override val method : String? ,
149- ) : HttpRequestState(id, timeSignMillis, url, header, request, response )
158+ ) : HttpRequestState(id, timeSignMillis, url, responseHeader, requestBody, responseBody )
150159
151160 companion object {
161+ private val json = Json {
162+ prettyPrint = true
163+ }
164+
152165 fun beginWith (pipeline : PipelineContext <Any , HttpRequestBuilder >, data : Any ): HttpRequestState {
153166 // inject RequestId to attribute
154167 val requestId = with (pipeline.context.attributes) {
@@ -163,8 +176,8 @@ sealed class HttpRequestState(
163176 return Executing (
164177 id = requestId.toString(),
165178 url = pipeline.context.url.buildString(),
166- header = pipeline.context.headers.build().toString(),
167- request = pipeline.context.body.toString(),
179+ responseHeader = pipeline.context.headers.build().toString(),
180+ requestBody = pipeline.context.body.toString(),
168181 method = pipeline.context.method.value,
169182 )
170183 }
@@ -174,13 +187,39 @@ sealed class HttpRequestState(
174187 data : HttpResponseContainer
175188 ): HttpRequestState {
176189 val requestId = pipeline.context.attributes[AttributeKey <Long >(" RequestId" )]
177-
190+ val requestHeader: String = run {
191+ pipeline.context.request.headers.toString()
192+ .replace(" [" , " " )
193+ .replace(" ]" , " " )
194+ .split(" ," )
195+ .joinToString(" \n " ) {
196+ it.replaceFirst(" ^\\ s+" .toRegex(), " " )
197+ }
198+ }
199+ val requestBody: String = run {
200+ pipeline.context.request.content.toString()
201+ }
202+ val responseHeader: String = run {
203+ pipeline.context.response.headers.toString()
204+ .replace(" [" , " " )
205+ .replace(" ]" , " " )
206+ .split(" ," )
207+ .joinToString(" \n " ) {
208+ it.replaceFirst(" ^\\ s+" .toRegex(), " " )
209+ }
210+ }
211+ val response: String = run {
212+ val type = data.expectedType.kotlinType ? : return @run data.response.toString()
213+ val serializer = Json .serializersModule.serializer(type)
214+ json.encodeToString(serializer, data.response)
215+ }
178216 return Success (
179217 id = requestId.toString(),
180218 url = pipeline.context.request.url.toString(),
181- header = pipeline.context.request.headers.toString(),
182- request = pipeline.context.request.content.toString(),
183- response = data.response.toString(),
219+ requestHeader = requestHeader,
220+ requestBody = requestBody,
221+ responseHeader = responseHeader,
222+ responseBody = response,
184223 status = pipeline.context.response.status.value,
185224 method = pipeline.context.request.method.value,
186225 )
0 commit comments