@@ -2,6 +2,7 @@ package io.github.dmitrysulman.logback.access.reactor.netty
22
33import ch.qos.logback.access.common.spi.AccessContext
44import ch.qos.logback.access.common.spi.IAccessEvent
5+ import jakarta.servlet.http.Cookie
56import reactor.netty.http.server.logging.AccessLogArgProvider
67import java.io.Serializable
78import java.net.InetSocketAddress
@@ -37,7 +38,7 @@ class AccessEvent(
3738 private val _sequenceNumber = context.sequenceNumberGenerator?.nextSequenceNumber() ? : 0
3839 private val _elapsedTime = argProvider.duration()
3940 private val _elapsedTimeSeconds = _elapsedTime / 1000
40- private val _requestUri by lazy { argProvider.uri()?.toString()?.substringBefore(" ?" ) ? : NA }
41+ private val _requestPath by lazy { argProvider.uri()?.toString()?.substringBefore(" ?" ) ? : NA }
4142 private val _queryString by lazy {
4243 argProvider.uri()?.let { uri ->
4344 uri
@@ -47,6 +48,7 @@ class AccessEvent(
4748 .orEmpty()
4849 } ? : NA
4950 }
51+ private val _requestUrl by lazy { " $_method ${argProvider.uri()?.toString() ? : NA } $_protocol " }
5052 private val _remoteHost by lazy {
5153 val remoteAddress = argProvider.connectionInformation()?.connectionRemoteAddress()
5254 if (remoteAddress is InetSocketAddress ) {
@@ -58,22 +60,19 @@ class AccessEvent(
5860 private val _remoteUser by lazy { argProvider.user() ? : NA }
5961 private val _protocol by lazy { argProvider.protocol() ? : NA }
6062 private val _method by lazy { argProvider.method()?.toString() ? : NA }
61- private lateinit var _threadName : String
63+ private var _threadName : String? = null
6264 private val _requestParameterMap by lazy {
6365 _queryString
64- .takeIf { it.isNotEmpty() && it != NA }
66+ .takeIf { it.length > 1 }
6567 ?.substring(1 )
6668 ?.split(" &" )
69+ ?.asSequence()
6770 ?.mapNotNull {
6871 val index = it.indexOf(" =" )
69- if (index in 1 .. it.length - 2 ) {
70- it.substring(0 , index) to it.substring(index + 1 )
71- } else {
72- null
73- }
74- }?.groupBy({ URLDecoder .decode(it.first, StandardCharsets .UTF_8 ) }) {
75- URLDecoder .decode(it.second, StandardCharsets .UTF_8 )
76- }?.mapValues { it.value.toTypedArray() }
72+ if (index !in 1 .. it.length - 2 ) return @mapNotNull null
73+ it.substring(0 , index) to it.substring(index + 1 )
74+ }?.groupBy({ it.first.decodeCatching() }) { it.second.decodeCatching() }
75+ ?.mapValues { it.value.toTypedArray() }
7776 ? : emptyMap()
7877 }
7978 private val _remoteAddr by lazy {
@@ -85,9 +84,27 @@ class AccessEvent(
8584 } ? : NA
8685 }
8786 private val _cookieMap by lazy {
88- argProvider.cookies()?.entries?.associate {
89- it.key.toString() to (it.value.firstOrNull()?.value() ? : NA )
90- } ? : emptyMap()
87+ argProvider
88+ .cookies()
89+ ?.asSequence()
90+ ?.mapNotNull { (name, values) ->
91+ if (name.isNullOrBlank()) return @mapNotNull null
92+ val value = values.firstOrNull()?.value() ? : return @mapNotNull null
93+ name.toString() to value
94+ }?.toMap() ? : emptyMap()
95+ }
96+ private val _cookieList by lazy {
97+ argProvider
98+ .cookies()
99+ ?.mapNotNull { (name, values) ->
100+ if (name.isNullOrBlank()) return @mapNotNull null
101+ val value = values.firstOrNull()?.value() ? : return @mapNotNull null
102+ try {
103+ Cookie (name.toString(), value)
104+ } catch (_: Exception ) {
105+ null
106+ }
107+ } ? : emptyList()
91108 }
92109 private val _contentLength by lazy { _serverAdapter .contentLength }
93110 private val _statusCode by lazy { _serverAdapter .statusCode }
@@ -106,6 +123,7 @@ class AccessEvent(
106123
107124 override fun prepareForDeferredProcessing () {
108125 requestURI
126+ requestURL
109127 queryString
110128 remoteHost
111129 remoteUser
@@ -114,11 +132,13 @@ class AccessEvent(
114132 requestParameterMap
115133 remoteAddr
116134 getCookieMap()
135+ cookies
117136 contentLength
118137 statusCode
119138 localPort
120139 responseHeaderMap
121140 requestHeaderMap
141+ threadName
122142 }
123143
124144 override fun getRequest () = null
@@ -133,9 +153,9 @@ class AccessEvent(
133153
134154 override fun getElapsedSeconds () = _elapsedTimeSeconds
135155
136- override fun getRequestURI () = _requestUri
156+ override fun getRequestURI () = _requestPath
137157
138- override fun getRequestURL () = " $_method $_requestUri$_queryString $_protocol "
158+ override fun getRequestURL () = _requestUrl
139159
140160 override fun getRemoteHost () = _remoteHost
141161
@@ -171,6 +191,8 @@ class AccessEvent(
171191
172192 override fun getRequestParameter (key : String ) = _requestParameterMap [key] ? : NA_ARRAY
173193
194+ override fun getCookies () = _cookieList
195+
174196 private fun getCookieMap () = _cookieMap
175197
176198 override fun getCookie (key : String ) = getCookieMap()[key] ? : NA
@@ -200,4 +222,11 @@ class AccessEvent(
200222
201223 private val NA_ARRAY = arrayOf(NA )
202224 }
225+
226+ private fun String.decodeCatching (): String =
227+ try {
228+ URLDecoder .decode(this , StandardCharsets .UTF_8 )
229+ } catch (_: Exception ) {
230+ this
231+ }
203232}
0 commit comments