-
Notifications
You must be signed in to change notification settings - Fork 629
/
AccessLogHandlerH1.java
102 lines (91 loc) · 3.42 KB
/
AccessLogHandlerH1.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
/*
* Copyright (c) 2018-2022 VMware, Inc. or its affiliates, All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package reactor.netty5.http.server.logging;
import io.netty5.buffer.Buffer;
import io.netty5.channel.ChannelHandler;
import io.netty5.channel.ChannelHandlerContext;
import io.netty5.handler.codec.http.HttpContent;
import io.netty5.handler.codec.http.HttpRequest;
import io.netty5.handler.codec.http.HttpResponse;
import io.netty5.handler.codec.http.HttpResponseStatus;
import io.netty5.handler.codec.http.HttpUtil;
import io.netty5.handler.codec.http.LastHttpContent;
import io.netty5.util.concurrent.Future;
import reactor.netty5.channel.ChannelOperations;
import reactor.netty5.http.server.HttpServerInfos;
import reactor.util.annotation.Nullable;
import java.util.function.Function;
/**
* {@link ChannelHandler} for access log of HTTP/1.1.
*
* @author Violeta Georgieva
* @author limaoning
*/
final class AccessLogHandlerH1 extends BaseAccessLogHandler {
AccessLogArgProviderH1 accessLogArgProvider;
AccessLogHandlerH1(@Nullable Function<AccessLogArgProvider, AccessLog> accessLog) {
super(accessLog);
}
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) {
if (msg instanceof HttpRequest request) {
if (accessLogArgProvider == null) {
accessLogArgProvider = new AccessLogArgProviderH1(ctx.channel().remoteAddress());
}
accessLogArgProvider.request(request);
}
ctx.fireChannelRead(msg);
}
@Override
public Future<Void> write(ChannelHandlerContext ctx, Object msg) {
if (msg instanceof HttpResponse response) {
final HttpResponseStatus status = response.status();
if (status.equals(HttpResponseStatus.CONTINUE)) {
return ctx.write(msg);
}
final boolean chunked = HttpUtil.isTransferEncodingChunked(response);
accessLogArgProvider.response(response)
.chunked(chunked);
if (!chunked) {
accessLogArgProvider.contentLength(HttpUtil.getContentLength(response, -1));
}
ChannelOperations<?, ?> ops = ChannelOperations.get(ctx.channel());
if (ops instanceof HttpServerInfos httpInfos) {
accessLogArgProvider.cookies(httpInfos.cookies());
}
}
if (msg instanceof LastHttpContent<?> lastHttpContent) {
accessLogArgProvider.increaseContentLength(lastHttpContent.payload().readableBytes());
return ctx.write(msg)
.addListener(future -> {
if (future.isSuccess()) {
AccessLog log = accessLog.apply(accessLogArgProvider);
if (log != null) {
log.log();
}
accessLogArgProvider.clear();
}
});
}
if (msg instanceof Buffer buffer) {
accessLogArgProvider.increaseContentLength(buffer.readableBytes());
}
else if (msg instanceof HttpContent<?> httpContent) {
accessLogArgProvider.increaseContentLength(httpContent.payload().readableBytes());
}
return ctx.write(msg);
}
}