Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add accesslog for HTTP/3 server support
Related to #1531
- Loading branch information
Showing
6 changed files
with
219 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
88 changes: 88 additions & 0 deletions
88
...or-netty-http/src/main/java/reactor/netty/http/server/logging/AccessLogArgProviderH3.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
/* | ||
* Copyright (c) 2024 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.netty.http.server.logging; | ||
|
||
import io.netty.incubator.codec.http3.Http3HeadersFrame; | ||
import reactor.util.annotation.Nullable; | ||
|
||
import java.net.SocketAddress; | ||
import java.util.Objects; | ||
|
||
final class AccessLogArgProviderH3 extends AbstractAccessLogArgProvider<AccessLogArgProviderH3> { | ||
|
||
static final String H3_PROTOCOL_NAME = "HTTP/3.0"; | ||
|
||
Http3HeadersFrame requestHeaders; | ||
Http3HeadersFrame responseHeaders; | ||
|
||
AccessLogArgProviderH3(@Nullable SocketAddress remoteAddress) { | ||
super(remoteAddress); | ||
} | ||
|
||
AccessLogArgProviderH3 requestHeaders(Http3HeadersFrame requestHeaders) { | ||
this.requestHeaders = Objects.requireNonNull(requestHeaders, "requestHeaders"); | ||
onRequest(); | ||
return get(); | ||
} | ||
|
||
AccessLogArgProviderH3 responseHeaders(Http3HeadersFrame responseHeaders) { | ||
this.responseHeaders = Objects.requireNonNull(responseHeaders, "responseHeaders"); | ||
return get(); | ||
} | ||
|
||
@Override | ||
@Nullable | ||
public CharSequence status() { | ||
return responseHeaders == null ? null : responseHeaders.headers().status(); | ||
} | ||
|
||
@Override | ||
@Nullable | ||
public CharSequence requestHeader(CharSequence name) { | ||
Objects.requireNonNull(name, "name"); | ||
return requestHeaders == null ? null : requestHeaders.headers().get(name); | ||
} | ||
|
||
@Override | ||
@Nullable | ||
public CharSequence responseHeader(CharSequence name) { | ||
Objects.requireNonNull(name, "name"); | ||
return responseHeaders == null ? null : responseHeaders.headers().get(name); | ||
} | ||
|
||
@Override | ||
void onRequest() { | ||
super.onRequest(); | ||
if (requestHeaders != null) { | ||
super.method = requestHeaders.headers().method(); | ||
super.uri = requestHeaders.headers().path(); | ||
super.protocol = H3_PROTOCOL_NAME; | ||
} | ||
} | ||
|
||
@Override | ||
void clear() { | ||
super.clear(); | ||
this.requestHeaders = null; | ||
this.responseHeaders = null; | ||
} | ||
|
||
@Override | ||
public AccessLogArgProviderH3 get() { | ||
return this; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
91 changes: 91 additions & 0 deletions
91
reactor-netty-http/src/main/java/reactor/netty/http/server/logging/AccessLogHandlerH3.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
/* | ||
* Copyright (c) 2024 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.netty.http.server.logging; | ||
|
||
import io.netty.channel.ChannelHandlerContext; | ||
import io.netty.channel.ChannelPromise; | ||
import io.netty.handler.codec.http.HttpResponseStatus; | ||
import io.netty.incubator.codec.http3.Http3DataFrame; | ||
import io.netty.incubator.codec.http3.Http3HeadersFrame; | ||
import io.netty.incubator.codec.quic.QuicChannel; | ||
import reactor.netty.channel.ChannelOperations; | ||
import reactor.netty.http.server.HttpServerInfos; | ||
import reactor.util.annotation.Nullable; | ||
|
||
import java.util.function.Function; | ||
|
||
final class AccessLogHandlerH3 extends BaseAccessLogHandler { | ||
|
||
AccessLogArgProviderH3 accessLogArgProvider; | ||
|
||
AccessLogHandlerH3(@Nullable Function<AccessLogArgProvider, AccessLog> accessLog) { | ||
super(accessLog); | ||
} | ||
|
||
@Override | ||
public void channelRead(ChannelHandlerContext ctx, Object msg) { | ||
if (msg instanceof Http3HeadersFrame) { | ||
Http3HeadersFrame requestHeaders = (Http3HeadersFrame) msg; | ||
|
||
if (accessLogArgProvider == null) { | ||
accessLogArgProvider = new AccessLogArgProviderH3(ctx.channel().parent() instanceof QuicChannel ? | ||
((QuicChannel) ctx.channel().parent()).remoteSocketAddress() : null); | ||
} | ||
accessLogArgProvider.requestHeaders(requestHeaders); | ||
|
||
ctx.channel() | ||
.closeFuture() | ||
.addListener(f -> { | ||
AccessLog log = accessLog.apply(accessLogArgProvider); | ||
if (log != null) { | ||
log.log(); | ||
} | ||
accessLogArgProvider.clear(); | ||
}); | ||
} | ||
ctx.fireChannelRead(msg); | ||
} | ||
|
||
@Override | ||
@SuppressWarnings("FutureReturnValueIgnored") | ||
public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) { | ||
if (msg instanceof Http3HeadersFrame) { | ||
Http3HeadersFrame responseHeaders = (Http3HeadersFrame) msg; | ||
|
||
if (HttpResponseStatus.CONTINUE.codeAsText().contentEquals(responseHeaders.headers().status())) { | ||
//"FutureReturnValueIgnored" this is deliberate | ||
ctx.write(msg, promise); | ||
return; | ||
} | ||
|
||
accessLogArgProvider.responseHeaders(responseHeaders) | ||
.chunked(true); | ||
|
||
ChannelOperations<?, ?> ops = ChannelOperations.get(ctx.channel()); | ||
if (ops instanceof HttpServerInfos) { | ||
super.applyServerInfos(accessLogArgProvider, (HttpServerInfos) ops); | ||
} | ||
} | ||
if (msg instanceof Http3DataFrame) { | ||
Http3DataFrame data = (Http3DataFrame) msg; | ||
|
||
accessLogArgProvider.increaseContentLength(data.content().readableBytes()); | ||
} | ||
|
||
//"FutureReturnValueIgnored" this is deliberate | ||
ctx.write(msg, promise); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters