From c5e73706459533576a400661f97dbec39576db46 Mon Sep 17 00:00:00 2001 From: Azeem Muzammil Date: Mon, 3 Jun 2024 11:54:55 +0530 Subject: [PATCH] Implement HTTP/1.1 access logging --- ballerina/Ballerina.toml | 6 +- ballerina/CompilerPlugin.toml | 2 +- ballerina/Dependencies.toml | 6 +- .../DefaultHttpClientConnector.java | 1 - .../HttpOutboundRespListener.java | 13 ++++ .../HttpServerChannelInitializer.java | 16 ++-- .../contractimpl/listener/SourceHandler.java | 40 +++++++++- .../listener/states/SendingEntityBody.java | 77 +++++++++++++++++-- .../listener/states/SendingHeaders.java | 4 +- .../sender/states/ReceivingEntityBody.java | 15 +++- .../states/http2/ReceivingEntityBody.java | 15 +++- 11 files changed, 158 insertions(+), 37 deletions(-) diff --git a/ballerina/Ballerina.toml b/ballerina/Ballerina.toml index eb3d75f969..8c4a8d2c71 100644 --- a/ballerina/Ballerina.toml +++ b/ballerina/Ballerina.toml @@ -1,7 +1,7 @@ [package] org = "ballerina" name = "http" -version = "2.10.13" +version = "2.10.14" authors = ["Ballerina"] keywords = ["http", "network", "service", "listener", "client"] repository = "https://github.com/ballerina-platform/module-ballerina-http" @@ -16,8 +16,8 @@ graalvmCompatible = true [[platform.java17.dependency]] groupId = "io.ballerina.stdlib" artifactId = "http-native" -version = "2.10.13" -path = "../native/build/libs/http-native-2.10.13.jar" +version = "2.10.14" +path = "../native/build/libs/http-native-2.10.14-SNAPSHOT.jar" [[platform.java17.dependency]] groupId = "io.ballerina.stdlib" diff --git a/ballerina/CompilerPlugin.toml b/ballerina/CompilerPlugin.toml index d6edd0008f..f8e94d35eb 100644 --- a/ballerina/CompilerPlugin.toml +++ b/ballerina/CompilerPlugin.toml @@ -3,4 +3,4 @@ id = "http-compiler-plugin" class = "io.ballerina.stdlib.http.compiler.HttpCompilerPlugin" [[dependency]] -path = "../compiler-plugin/build/libs/http-compiler-plugin-2.10.13.jar" +path = "../compiler-plugin/build/libs/http-compiler-plugin-2.10.14-SNAPSHOT.jar" diff --git a/ballerina/Dependencies.toml b/ballerina/Dependencies.toml index 0acc473581..3161ab185f 100644 --- a/ballerina/Dependencies.toml +++ b/ballerina/Dependencies.toml @@ -25,7 +25,7 @@ modules = [ [[package]] org = "ballerina" name = "cache" -version = "3.7.0" +version = "3.7.1" dependencies = [ {org = "ballerina", name = "constraint"}, {org = "ballerina", name = "jballerina.java"}, @@ -76,7 +76,7 @@ modules = [ [[package]] org = "ballerina" name = "http" -version = "2.10.13" +version = "2.10.14" dependencies = [ {org = "ballerina", name = "auth"}, {org = "ballerina", name = "cache"}, @@ -283,7 +283,7 @@ modules = [ [[package]] org = "ballerina" name = "observe" -version = "1.2.0" +version = "1.2.3" dependencies = [ {org = "ballerina", name = "jballerina.java"} ] diff --git a/native/src/main/java/io/ballerina/stdlib/http/transport/contractimpl/DefaultHttpClientConnector.java b/native/src/main/java/io/ballerina/stdlib/http/transport/contractimpl/DefaultHttpClientConnector.java index d570ef4619..ba92ddfa24 100644 --- a/native/src/main/java/io/ballerina/stdlib/http/transport/contractimpl/DefaultHttpClientConnector.java +++ b/native/src/main/java/io/ballerina/stdlib/http/transport/contractimpl/DefaultHttpClientConnector.java @@ -198,7 +198,6 @@ public HttpResponseFuture send(OutboundMsgHolder outboundMsgHolder, HttpCarbonMe new RequestWriteStarter(outboundMsgHolder, activeHttp2ClientChannel).startWritingContent(); httpResponseFuture = outboundMsgHolder.getResponseFuture(); httpResponseFuture.notifyResponseHandle(new ResponseHandle(outboundMsgHolder)); - return httpResponseFuture; } } diff --git a/native/src/main/java/io/ballerina/stdlib/http/transport/contractimpl/HttpOutboundRespListener.java b/native/src/main/java/io/ballerina/stdlib/http/transport/contractimpl/HttpOutboundRespListener.java index 9cf1499b9f..932f39e741 100644 --- a/native/src/main/java/io/ballerina/stdlib/http/transport/contractimpl/HttpOutboundRespListener.java +++ b/native/src/main/java/io/ballerina/stdlib/http/transport/contractimpl/HttpOutboundRespListener.java @@ -35,6 +35,7 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import java.util.Calendar; import java.util.Locale; /** @@ -53,6 +54,8 @@ public class HttpOutboundRespListener implements HttpConnectorListener { private ChunkConfig chunkConfig; private KeepAliveConfig keepAliveConfig; private String serverName; + private Calendar inboundRequestArrivalTime; + private String remoteAddress = "-"; public HttpOutboundRespListener(HttpCarbonMessage requestMsg, SourceHandler sourceHandler) { this.requestDataHolder = new RequestDataHolder(requestMsg); @@ -64,6 +67,8 @@ public HttpOutboundRespListener(HttpCarbonMessage requestMsg, SourceHandler sour this.handlerExecutor = HttpTransportContextHolder.getInstance().getHandlerExecutor(); this.serverName = sourceHandler.getServerName(); this.listenerReqRespStateManager = requestMsg.listenerReqRespStateManager; + this.remoteAddress = sourceHandler.getRemoteHost(); + this.inboundRequestArrivalTime = Calendar.getInstance(); setBackPressureObservableToHttpResponseFuture(); } @@ -146,4 +151,12 @@ public void setKeepAliveConfig(KeepAliveConfig config) { public SourceHandler getSourceHandler() { return sourceHandler; } + + public Calendar getInboundRequestArrivalTime() { + return inboundRequestArrivalTime; + } + + public String getRemoteAddress() { + return remoteAddress; + } } diff --git a/native/src/main/java/io/ballerina/stdlib/http/transport/contractimpl/listener/HttpServerChannelInitializer.java b/native/src/main/java/io/ballerina/stdlib/http/transport/contractimpl/listener/HttpServerChannelInitializer.java index 184731c93b..a18998bdcd 100644 --- a/native/src/main/java/io/ballerina/stdlib/http/transport/contractimpl/listener/HttpServerChannelInitializer.java +++ b/native/src/main/java/io/ballerina/stdlib/http/transport/contractimpl/listener/HttpServerChannelInitializer.java @@ -66,8 +66,6 @@ import javax.net.ssl.SSLContext; import javax.net.ssl.SSLEngine; -import static io.ballerina.stdlib.http.transport.contract.Constants.ACCESS_LOG; -import static io.ballerina.stdlib.http.transport.contract.Constants.HTTP_ACCESS_LOG_HANDLER; import static io.ballerina.stdlib.http.transport.contract.Constants.HTTP_TRACE_LOG_HANDLER; import static io.ballerina.stdlib.http.transport.contract.Constants.MAX_ENTITY_BODY_VALIDATION_HANDLER; import static io.ballerina.stdlib.http.transport.contract.Constants.SECURITY; @@ -220,9 +218,9 @@ public void configureHttpPipeline(ChannelPipeline serverPipeline, String initial if (httpTraceLogEnabled) { serverPipeline.addLast(HTTP_TRACE_LOG_HANDLER, new HttpTraceLoggingHandler(TRACE_LOG_DOWNSTREAM)); } - if (httpAccessLogEnabled) { - serverPipeline.addLast(HTTP_ACCESS_LOG_HANDLER, new HttpAccessLoggingHandler(ACCESS_LOG)); - } +// if (httpAccessLogEnabled) { +// serverPipeline.addLast(HTTP_ACCESS_LOG_HANDLER, new HttpAccessLoggingHandler(ACCESS_LOG)); +// } } serverPipeline.addLast(URI_HEADER_LENGTH_VALIDATION_HANDLER, new UriAndHeaderLengthValidator(this.serverName)); if (reqSizeValidationConfig.getMaxEntityBodySize() > -1) { @@ -235,7 +233,7 @@ public void configureHttpPipeline(ChannelPipeline serverPipeline, String initial webSocketCompressionEnabled)); serverPipeline.addLast(Constants.BACK_PRESSURE_HANDLER, new BackPressureHandler()); serverPipeline.addLast(Constants.HTTP_SOURCE_HANDLER, - new SourceHandler(this.serverConnectorFuture, this.interfaceId, this.chunkConfig, + new SourceHandler(this.serverConnectorFuture, this, this.interfaceId, this.chunkConfig, keepAliveConfig, this.serverName, this.allChannels, this.listenerChannels, this.pipeliningEnabled, this.pipeliningLimit, this.pipeliningGroup)); @@ -267,9 +265,9 @@ private void configureH2cPipeline(ChannelPipeline pipeline) { pipeline.addLast(HTTP_TRACE_LOG_HANDLER, new HttpTraceLoggingHandler(TRACE_LOG_DOWNSTREAM)); } - if (httpAccessLogEnabled) { - pipeline.addLast(HTTP_ACCESS_LOG_HANDLER, new HttpAccessLoggingHandler(ACCESS_LOG)); - } +// if (httpAccessLogEnabled) { +// pipeline.addLast(HTTP_ACCESS_LOG_HANDLER, new HttpAccessLoggingHandler(ACCESS_LOG)); +// } final HttpServerUpgradeHandler.UpgradeCodecFactory upgradeCodecFactory = protocol -> { if (AsciiString.contentEquals(Http2CodecUtil.HTTP_UPGRADE_PROTOCOL_NAME, protocol)) { diff --git a/native/src/main/java/io/ballerina/stdlib/http/transport/contractimpl/listener/SourceHandler.java b/native/src/main/java/io/ballerina/stdlib/http/transport/contractimpl/listener/SourceHandler.java index 9e88ef6a97..75eb3c1dd0 100644 --- a/native/src/main/java/io/ballerina/stdlib/http/transport/contractimpl/listener/SourceHandler.java +++ b/native/src/main/java/io/ballerina/stdlib/http/transport/contractimpl/listener/SourceHandler.java @@ -19,6 +19,7 @@ package io.ballerina.stdlib.http.transport.contractimpl.listener; +import io.ballerina.stdlib.http.api.logging.accesslog.HttpAccessLogMessage; import io.ballerina.stdlib.http.transport.contract.Constants; import io.ballerina.stdlib.http.transport.contract.ServerConnectorFuture; import io.ballerina.stdlib.http.transport.contract.config.ChunkConfig; @@ -46,7 +47,10 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import java.net.InetSocketAddress; import java.net.SocketAddress; +import java.util.ArrayList; +import java.util.List; import java.util.Map; import java.util.PriorityQueue; import java.util.Queue; @@ -73,8 +77,10 @@ public class SourceHandler extends ChannelInboundHandlerAdapter { private KeepAliveConfig keepAliveConfig; private ServerConnectorFuture serverConnectorFuture; + private HttpServerChannelInitializer serverChannelInitializer; private String interfaceId; private String serverName; + private String remoteHost; private boolean idleTimeout; private ChannelGroup allChannels; private ChannelGroup listenerChannels; @@ -87,12 +93,15 @@ public class SourceHandler extends ChannelInboundHandlerAdapter { private long sequenceId = 1L; //Keep track of the request order for http 1.1 pipelining private final Queue holdingQueue = new PriorityQueue<>(NUMBER_OF_INITIAL_EVENTS_HELD); private EventExecutorGroup pipeliningGroup; + private List httpAccessLogMessages; - public SourceHandler(ServerConnectorFuture serverConnectorFuture, String interfaceId, ChunkConfig chunkConfig, - KeepAliveConfig keepAliveConfig, String serverName, ChannelGroup allChannels, - ChannelGroup listenerChannels, boolean pipeliningEnabled, long pipeliningLimit, - EventExecutorGroup pipeliningGroup) { + public SourceHandler(ServerConnectorFuture serverConnectorFuture, + HttpServerChannelInitializer serverChannelInitializer, String interfaceId, + ChunkConfig chunkConfig, KeepAliveConfig keepAliveConfig, String serverName, + ChannelGroup allChannels, ChannelGroup listenerChannels, boolean pipeliningEnabled, + long pipeliningLimit, EventExecutorGroup pipeliningGroup) { this.serverConnectorFuture = serverConnectorFuture; + this.serverChannelInitializer = serverChannelInitializer; this.interfaceId = interfaceId; this.chunkConfig = chunkConfig; this.keepAliveConfig = keepAliveConfig; @@ -104,6 +113,7 @@ public SourceHandler(ServerConnectorFuture serverConnectorFuture, String interfa this.pipeliningEnabled = pipeliningEnabled; this.pipeliningLimit = pipeliningLimit; this.pipeliningGroup = pipeliningGroup; + this.httpAccessLogMessages = new ArrayList<>(); } @SuppressWarnings("unchecked") @@ -156,6 +166,12 @@ public void channelActive(final ChannelHandlerContext ctx) { handlerExecutor.executeAtSourceConnectionInitiation(Integer.toString(ctx.hashCode())); } this.remoteAddress = ctx.channel().remoteAddress(); + if (this.remoteAddress instanceof InetSocketAddress) { + remoteHost = ((InetSocketAddress) this.remoteAddress).getAddress().toString(); + if (remoteHost.startsWith("/")) { + remoteHost = remoteHost.substring(1); + } + } } @Override @@ -317,6 +333,10 @@ public ServerConnectorFuture getServerConnectorFuture() { return serverConnectorFuture; } + public HttpServerChannelInitializer getServerChannelInitializer() { + return serverChannelInitializer; + } + public ChunkConfig getChunkConfig() { return chunkConfig; } @@ -329,6 +349,10 @@ public String getServerName() { return serverName; } + public String getRemoteHost() { + return remoteHost; + } + public void setConnectedState(boolean connectedState) { this.connectedState = connectedState; } @@ -340,4 +364,12 @@ public void removeRequestEntry(HttpCarbonMessage inboundRequestMsg) { public void resetInboundRequestMsg() { this.inboundRequestMsg = null; } + + public void addHttpAccessLogMessage(HttpAccessLogMessage httpAccessLogMessage) { + this.httpAccessLogMessages.add(httpAccessLogMessage); + } + + public List getHttpAccessLogMessages() { + return this.httpAccessLogMessages; + } } diff --git a/native/src/main/java/io/ballerina/stdlib/http/transport/contractimpl/listener/states/SendingEntityBody.java b/native/src/main/java/io/ballerina/stdlib/http/transport/contractimpl/listener/states/SendingEntityBody.java index edbfdfd102..2c1fc3e873 100644 --- a/native/src/main/java/io/ballerina/stdlib/http/transport/contractimpl/listener/states/SendingEntityBody.java +++ b/native/src/main/java/io/ballerina/stdlib/http/transport/contractimpl/listener/states/SendingEntityBody.java @@ -18,11 +18,14 @@ package io.ballerina.stdlib.http.transport.contractimpl.listener.states; +import io.ballerina.stdlib.http.api.logging.accesslog.HttpAccessLogMessage; +import io.ballerina.stdlib.http.api.logging.accesslog.HttpAccessLogger; import io.ballerina.stdlib.http.transport.contract.Constants; import io.ballerina.stdlib.http.transport.contract.HttpResponseFuture; import io.ballerina.stdlib.http.transport.contract.ServerConnectorFuture; import io.ballerina.stdlib.http.transport.contract.exceptions.ServerConnectorException; import io.ballerina.stdlib.http.transport.contractimpl.HttpOutboundRespListener; +import io.ballerina.stdlib.http.transport.contractimpl.common.Util; import io.ballerina.stdlib.http.transport.contractimpl.listener.SourceHandler; import io.ballerina.stdlib.http.transport.internal.HandlerExecutor; import io.ballerina.stdlib.http.transport.internal.HttpTransportContextHolder; @@ -34,6 +37,9 @@ import io.netty.channel.ChannelHandlerContext; import io.netty.handler.codec.http.DefaultLastHttpContent; import io.netty.handler.codec.http.HttpContent; +import io.netty.handler.codec.http.HttpHeaderNames; +import io.netty.handler.codec.http.HttpHeaders; +import io.netty.handler.codec.http.HttpMessage; import io.netty.handler.codec.http.HttpRequest; import io.netty.handler.codec.http.HttpResponse; import io.netty.handler.codec.http.LastHttpContent; @@ -44,13 +50,16 @@ import java.io.IOException; import java.nio.channels.ClosedChannelException; import java.util.ArrayList; +import java.util.Calendar; import java.util.List; import java.util.Queue; import static io.ballerina.stdlib.http.transport.contract.Constants.HTTP_HEAD_METHOD; +import static io.ballerina.stdlib.http.transport.contract.Constants.HTTP_X_FORWARDED_FOR; import static io.ballerina.stdlib.http.transport.contract.Constants.IDLE_TIMEOUT_TRIGGERED_WHILE_WRITING_OUTBOUND_RESPONSE_BODY; import static io.ballerina.stdlib.http.transport.contract.Constants.REMOTE_CLIENT_CLOSED_WHILE_WRITING_OUTBOUND_RESPONSE_BODY; import static io.ballerina.stdlib.http.transport.contract.Constants.REMOTE_CLIENT_TO_HOST_CONNECTION_CLOSED; +import static io.ballerina.stdlib.http.transport.contract.Constants.TO; import static io.ballerina.stdlib.http.transport.contractimpl.common.Util.createFullHttpResponse; import static io.ballerina.stdlib.http.transport.contractimpl.common.Util.setupContentLengthRequest; import static io.ballerina.stdlib.http.transport.contractimpl.common.states.StateUtil.ILLEGAL_STATE_ERROR; @@ -63,6 +72,7 @@ public class SendingEntityBody implements ListenerState { private static final Logger LOG = LoggerFactory.getLogger(SendingEntityBody.class); private final HandlerExecutor handlerExecutor; + private final HttpOutboundRespListener outboundRespListener; private final HttpResponseFuture outboundRespStatusFuture; private final ListenerReqRespStateManager listenerReqRespStateManager; private boolean headersWritten; @@ -73,13 +83,24 @@ public class SendingEntityBody implements ListenerState { private HttpCarbonMessage outboundResponseMsg; private ChannelHandlerContext sourceContext; private SourceHandler sourceHandler; + private final Calendar inboundRequestArrivalTime; + private String remoteAddress = "-"; - SendingEntityBody(ListenerReqRespStateManager listenerReqRespStateManager, + SendingEntityBody(HttpOutboundRespListener outboundRespListener, + ListenerReqRespStateManager listenerReqRespStateManager, HttpResponseFuture outboundRespStatusFuture, boolean headersWritten) { + this.outboundRespListener = outboundRespListener; this.listenerReqRespStateManager = listenerReqRespStateManager; this.outboundRespStatusFuture = outboundRespStatusFuture; this.headersWritten = headersWritten; this.handlerExecutor = HttpTransportContextHolder.getInstance().getHandlerExecutor(); + this.headRequest = + outboundRespListener.getRequestDataHolder().getHttpMethod().equalsIgnoreCase(HTTP_HEAD_METHOD); + this.inboundRequestMsg = outboundRespListener.getInboundRequestMsg(); + this.sourceContext = outboundRespListener.getSourceContext(); + this.sourceHandler = outboundRespListener.getSourceHandler(); + this.inboundRequestArrivalTime = outboundRespListener.getInboundRequestArrivalTime(); + this.remoteAddress = outboundRespListener.getRemoteAddress(); } @Override @@ -100,11 +121,6 @@ public void writeOutboundResponseHeaders(HttpCarbonMessage outboundResponseMsg, @Override public void writeOutboundResponseBody(HttpOutboundRespListener outboundRespListener, HttpCarbonMessage outboundResponseMsg, HttpContent httpContent) { - - headRequest = outboundRespListener.getRequestDataHolder().getHttpMethod().equalsIgnoreCase(HTTP_HEAD_METHOD); - inboundRequestMsg = outboundRespListener.getInboundRequestMsg(); - sourceContext = outboundRespListener.getSourceContext(); - sourceHandler = outboundRespListener.getSourceHandler(); this.outboundResponseMsg = outboundResponseMsg; ChannelFuture outboundChannelFuture; @@ -221,6 +237,9 @@ private void checkForResponseWriteStatus(HttpCarbonMessage inboundRequestMsg, } else { outboundRespStatusFuture.notifyHttpListener(inboundRequestMsg); } + if (sourceHandler.getServerChannelInitializer().isHttpAccessLogEnabled()) { + logAccessInfo(inboundRequestMsg, outboundResponseMsg); + } resetOutboundListenerState(); }); } @@ -270,6 +289,52 @@ private void triggerPipeliningLogic(HttpCarbonMessage outboundResponseMsg) { } } + private void logAccessInfo(HttpCarbonMessage inboundRequestMsg, HttpCarbonMessage outboundResponseMsg) { + if (!HttpAccessLogger.isEnabled()) { + return; + } + + HttpHeaders headers = inboundRequestMsg.getHeaders(); + if (headers.contains(HTTP_X_FORWARDED_FOR)) { + String forwardedHops = headers.get(HTTP_X_FORWARDED_FOR); + // If multiple IPs available, the first ip is the client + int firstCommaIndex = forwardedHops.indexOf(','); + remoteAddress = firstCommaIndex != -1 ? forwardedHops.substring(0, firstCommaIndex) : forwardedHops; + } + + // Populate request parameters + String userAgent = "-"; + if (headers.contains(HttpHeaderNames.USER_AGENT)) { + userAgent = headers.get(HttpHeaderNames.USER_AGENT); + } + String referrer = "-"; + if (headers.contains(HttpHeaderNames.REFERER)) { + referrer = headers.get(HttpHeaderNames.REFERER); + } + String method = inboundRequestMsg.getHttpMethod(); + String uri = (String) inboundRequestMsg.getProperty(TO); + HttpMessage request = inboundRequestMsg.getNettyHttpRequest(); + String protocol; + if (request != null) { + protocol = request.protocolVersion().toString(); + } else { + protocol = inboundRequestMsg.getHttpVersion(); + } + + // Populate response parameters + int statusCode = Util.getHttpResponseStatus(outboundResponseMsg).code(); + + long requestTime = Calendar.getInstance().getTimeInMillis() - inboundRequestArrivalTime.getTimeInMillis(); + HttpAccessLogMessage inboundMessage = new HttpAccessLogMessage(remoteAddress, + inboundRequestArrivalTime, method, uri, protocol, statusCode, contentLength, referrer, userAgent); + inboundMessage.setRequestBodySize((long) inboundRequestMsg.getContentSize()); + inboundMessage.setRequestTime(requestTime); + + List outboundMessages = new ArrayList<>(sourceHandler.getHttpAccessLogMessages()); + + HttpAccessLogger.log(inboundMessage, outboundMessages); + } + private void resetOutboundListenerState() { contentList.clear(); contentLength = 0; diff --git a/native/src/main/java/io/ballerina/stdlib/http/transport/contractimpl/listener/states/SendingHeaders.java b/native/src/main/java/io/ballerina/stdlib/http/transport/contractimpl/listener/states/SendingHeaders.java index 2cf7c3dc14..047cca9212 100644 --- a/native/src/main/java/io/ballerina/stdlib/http/transport/contractimpl/listener/states/SendingHeaders.java +++ b/native/src/main/java/io/ballerina/stdlib/http/transport/contractimpl/listener/states/SendingHeaders.java @@ -123,8 +123,8 @@ public void writeOutboundResponseHeaders(HttpCarbonMessage outboundResponseMsg, } private void writeResponse(HttpCarbonMessage outboundResponseMsg, HttpContent httpContent, boolean headersWritten) { - listenerReqRespStateManager.state - = new SendingEntityBody(listenerReqRespStateManager, outboundRespStatusFuture, headersWritten); + listenerReqRespStateManager.state = new SendingEntityBody(outboundResponseListener, listenerReqRespStateManager, + outboundRespStatusFuture, headersWritten); listenerReqRespStateManager.writeOutboundResponseBody(outboundResponseListener, outboundResponseMsg, httpContent); } diff --git a/native/src/main/java/io/ballerina/stdlib/http/transport/contractimpl/sender/states/ReceivingEntityBody.java b/native/src/main/java/io/ballerina/stdlib/http/transport/contractimpl/sender/states/ReceivingEntityBody.java index 21313ad909..e696febcc0 100644 --- a/native/src/main/java/io/ballerina/stdlib/http/transport/contractimpl/sender/states/ReceivingEntityBody.java +++ b/native/src/main/java/io/ballerina/stdlib/http/transport/contractimpl/sender/states/ReceivingEntityBody.java @@ -24,6 +24,7 @@ import io.ballerina.stdlib.http.transport.contract.HttpResponseFuture; import io.ballerina.stdlib.http.transport.contractimpl.common.states.SenderReqRespStateManager; import io.ballerina.stdlib.http.transport.contractimpl.common.states.StateUtil; +import io.ballerina.stdlib.http.transport.contractimpl.listener.SourceHandler; import io.ballerina.stdlib.http.transport.contractimpl.listener.http2.Http2SourceHandler; import io.ballerina.stdlib.http.transport.contractimpl.sender.TargetHandler; import io.ballerina.stdlib.http.transport.message.HttpCarbonMessage; @@ -130,9 +131,7 @@ private void updateAccessLogInfo(TargetHandler targetHandler, HttpCarbonMessage httpOutboundRequest = targetHandler.getOutboundRequestMsg(); HttpAccessLogMessage outboundAccessLogMessage = getTypedProperty(httpOutboundRequest, OUTBOUND_ACCESS_LOG_MESSAGE, HttpAccessLogMessage.class); - Http2SourceHandler http2SourceHandler = - getTypedProperty(httpOutboundRequest, Constants.SRC_HANDLER, Http2SourceHandler.class); - if (outboundAccessLogMessage == null || http2SourceHandler == null) { + if (outboundAccessLogMessage == null) { return; } @@ -182,7 +181,15 @@ private void updateAccessLogInfo(TargetHandler targetHandler, outboundAccessLogMessage.getDateTime().getTimeInMillis(); outboundAccessLogMessage.setRequestTime(requestTime); - http2SourceHandler.addHttpAccessLogMessage(outboundAccessLogMessage); + SourceHandler srcHandler = getTypedProperty(httpOutboundRequest, Constants.SRC_HANDLER, SourceHandler.class); + Http2SourceHandler http2SourceHandler = + getTypedProperty(httpOutboundRequest, Constants.SRC_HANDLER, Http2SourceHandler.class); + + if (srcHandler != null) { + srcHandler.addHttpAccessLogMessage(outboundAccessLogMessage); + } else if (http2SourceHandler != null) { + http2SourceHandler.addHttpAccessLogMessage(outboundAccessLogMessage); + } } private T getTypedProperty(HttpCarbonMessage request, String propertyName, Class type) { diff --git a/native/src/main/java/io/ballerina/stdlib/http/transport/contractimpl/sender/states/http2/ReceivingEntityBody.java b/native/src/main/java/io/ballerina/stdlib/http/transport/contractimpl/sender/states/http2/ReceivingEntityBody.java index 7ae278a2f0..0783b58930 100644 --- a/native/src/main/java/io/ballerina/stdlib/http/transport/contractimpl/sender/states/http2/ReceivingEntityBody.java +++ b/native/src/main/java/io/ballerina/stdlib/http/transport/contractimpl/sender/states/http2/ReceivingEntityBody.java @@ -22,6 +22,7 @@ import io.ballerina.stdlib.http.api.logging.accesslog.HttpAccessLogMessage; import io.ballerina.stdlib.http.transport.contract.Constants; import io.ballerina.stdlib.http.transport.contractimpl.common.states.Http2MessageStateContext; +import io.ballerina.stdlib.http.transport.contractimpl.listener.SourceHandler; import io.ballerina.stdlib.http.transport.contractimpl.listener.http2.Http2SourceHandler; import io.ballerina.stdlib.http.transport.contractimpl.sender.http2.Http2ClientChannel; import io.ballerina.stdlib.http.transport.contractimpl.sender.http2.Http2TargetHandler; @@ -193,9 +194,7 @@ private void updateAccessLogInfo(OutboundMsgHolder outboundMsgHolder) { HttpCarbonMessage httpOutboundRequest = outboundMsgHolder.getRequest(); HttpAccessLogMessage outboundAccessLogMessage = getTypedProperty(httpOutboundRequest, OUTBOUND_ACCESS_LOG_MESSAGE, HttpAccessLogMessage.class); - Http2SourceHandler http2SourceHandler = - getTypedProperty(httpOutboundRequest, Constants.SRC_HANDLER, Http2SourceHandler.class); - if (outboundAccessLogMessage == null || http2SourceHandler == null) { + if (outboundAccessLogMessage == null) { return; } @@ -245,7 +244,15 @@ private void updateAccessLogInfo(OutboundMsgHolder outboundMsgHolder) { outboundAccessLogMessage.getDateTime().getTimeInMillis(); outboundAccessLogMessage.setRequestTime(requestTime); - http2SourceHandler.addHttpAccessLogMessage(outboundAccessLogMessage); + SourceHandler srcHandler = getTypedProperty(httpOutboundRequest, Constants.SRC_HANDLER, SourceHandler.class); + Http2SourceHandler http2SourceHandler = + getTypedProperty(httpOutboundRequest, Constants.SRC_HANDLER, Http2SourceHandler.class); + + if (srcHandler != null) { + srcHandler.addHttpAccessLogMessage(outboundAccessLogMessage); + } else if (http2SourceHandler != null) { + http2SourceHandler.addHttpAccessLogMessage(outboundAccessLogMessage); + } } private T getTypedProperty(HttpCarbonMessage request, String propertyName, Class type) {