2011-10-17 06:12:37 +02:00
|
|
|
/*
|
2012-06-04 22:31:44 +02:00
|
|
|
* Copyright 2012 The Netty Project
|
2011-10-17 06:12:37 +02:00
|
|
|
*
|
2011-12-09 06:18:34 +01:00
|
|
|
* The Netty Project licenses this file to you 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:
|
2011-10-17 06:12:37 +02:00
|
|
|
*
|
2020-10-23 14:44:18 +02:00
|
|
|
* https://www.apache.org/licenses/LICENSE-2.0
|
2011-10-17 06:12:37 +02:00
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
2011-12-09 06:18:34 +01:00
|
|
|
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
2011-10-17 06:12:37 +02:00
|
|
|
* License for the specific language governing permissions and limitations
|
|
|
|
* under the License.
|
|
|
|
*/
|
2017-02-20 14:06:48 +01:00
|
|
|
package io.netty.testsuite.autobahn;
|
2011-10-17 06:12:37 +02:00
|
|
|
|
2013-07-14 22:50:53 +02:00
|
|
|
import io.netty.buffer.ByteBuf;
|
2012-06-11 10:02:00 +02:00
|
|
|
import io.netty.buffer.Unpooled;
|
Clean up Future/Promises API (#11575)
Motivation:
The generics for the existing futures, promises, and listeners are too complicated.
This complication comes from the existence of `ChannelPromise` and `ChannelFuture`, which forces listeners to care about the particular _type_ of future being listened on.
Modification:
* Add a `FutureContextListener` which can take a context object as an additional argument. This allows our listeners to have the channel piped through to them, so they don't need to rely on the `ChannelFuture.channel()` method.
* Make the `FutureListener`, along with the `FutureContextListener` sibling, the default listener API, retiring the `GenericFutureListener` since we no longer need to abstract over the type of the future.
* Change all uses of `ChannelPromise` to `Promise<Void>`.
* Change all uses of `ChannelFuture` to `Future<Void>`.
* Change all uses of `GenericFutureListener` to either `FutureListener` or `FutureContextListener` as needed.
* Remove `ChannelFutureListener` and `GenericFutureListener`.
* Introduce a `ChannelFutureListeners` enum to house the constants that previously lived in `ChannelFutureListener`. These constants now implement `FutureContextListener` and take the `Channel` as a context.
* Remove `ChannelPromise` and `ChannelFuture` — all usages now rely on the plain `Future` and `Promise` APIs.
* Add static factory methods to `DefaultPromise` that allow us to create promises that are initialised as successful or failed.
* Remove `CompleteFuture`, `SucceededFuture`, `FailedFuture`, `CompleteChannelFuture`, `SucceededChannelFuture`, and `FailedChannelFuture`.
* Remove `ChannelPromiseNotifier`.
Result:
Cleaner generics and more straight forward code.
2021-08-20 09:55:16 +02:00
|
|
|
import io.netty.channel.ChannelFutureListeners;
|
2020-01-21 02:47:17 +01:00
|
|
|
import io.netty.channel.ChannelHandler;
|
2011-12-09 04:38:59 +01:00
|
|
|
import io.netty.channel.ChannelHandlerContext;
|
2013-01-16 05:22:50 +01:00
|
|
|
import io.netty.handler.codec.http.DefaultFullHttpResponse;
|
|
|
|
import io.netty.handler.codec.http.FullHttpResponse;
|
2014-10-31 08:48:28 +01:00
|
|
|
import io.netty.handler.codec.http.HttpHeaderNames;
|
2015-01-06 06:55:01 +01:00
|
|
|
import io.netty.handler.codec.http.HttpRequest;
|
2011-12-09 04:38:59 +01:00
|
|
|
import io.netty.handler.codec.http.websocketx.BinaryWebSocketFrame;
|
|
|
|
import io.netty.handler.codec.http.websocketx.CloseWebSocketFrame;
|
|
|
|
import io.netty.handler.codec.http.websocketx.ContinuationWebSocketFrame;
|
|
|
|
import io.netty.handler.codec.http.websocketx.PingWebSocketFrame;
|
|
|
|
import io.netty.handler.codec.http.websocketx.PongWebSocketFrame;
|
|
|
|
import io.netty.handler.codec.http.websocketx.TextWebSocketFrame;
|
|
|
|
import io.netty.handler.codec.http.websocketx.WebSocketFrame;
|
|
|
|
import io.netty.handler.codec.http.websocketx.WebSocketServerHandshaker;
|
|
|
|
import io.netty.handler.codec.http.websocketx.WebSocketServerHandshakerFactory;
|
|
|
|
import io.netty.util.CharsetUtil;
|
Clean up Future/Promises API (#11575)
Motivation:
The generics for the existing futures, promises, and listeners are too complicated.
This complication comes from the existence of `ChannelPromise` and `ChannelFuture`, which forces listeners to care about the particular _type_ of future being listened on.
Modification:
* Add a `FutureContextListener` which can take a context object as an additional argument. This allows our listeners to have the channel piped through to them, so they don't need to rely on the `ChannelFuture.channel()` method.
* Make the `FutureListener`, along with the `FutureContextListener` sibling, the default listener API, retiring the `GenericFutureListener` since we no longer need to abstract over the type of the future.
* Change all uses of `ChannelPromise` to `Promise<Void>`.
* Change all uses of `ChannelFuture` to `Future<Void>`.
* Change all uses of `GenericFutureListener` to either `FutureListener` or `FutureContextListener` as needed.
* Remove `ChannelFutureListener` and `GenericFutureListener`.
* Introduce a `ChannelFutureListeners` enum to house the constants that previously lived in `ChannelFutureListener`. These constants now implement `FutureContextListener` and take the `Channel` as a context.
* Remove `ChannelPromise` and `ChannelFuture` — all usages now rely on the plain `Future` and `Promise` APIs.
* Add static factory methods to `DefaultPromise` that allow us to create promises that are initialised as successful or failed.
* Remove `CompleteFuture`, `SucceededFuture`, `FailedFuture`, `CompleteChannelFuture`, `SucceededChannelFuture`, and `FailedChannelFuture`.
* Remove `ChannelPromiseNotifier`.
Result:
Cleaner generics and more straight forward code.
2021-08-20 09:55:16 +02:00
|
|
|
import io.netty.util.concurrent.Future;
|
2013-11-04 11:42:33 +01:00
|
|
|
import io.netty.util.internal.StringUtil;
|
2013-02-11 12:17:35 +01:00
|
|
|
|
|
|
|
import java.util.logging.Level;
|
|
|
|
import java.util.logging.Logger;
|
2011-10-17 06:12:37 +02:00
|
|
|
|
Clean up Future/Promises API (#11575)
Motivation:
The generics for the existing futures, promises, and listeners are too complicated.
This complication comes from the existence of `ChannelPromise` and `ChannelFuture`, which forces listeners to care about the particular _type_ of future being listened on.
Modification:
* Add a `FutureContextListener` which can take a context object as an additional argument. This allows our listeners to have the channel piped through to them, so they don't need to rely on the `ChannelFuture.channel()` method.
* Make the `FutureListener`, along with the `FutureContextListener` sibling, the default listener API, retiring the `GenericFutureListener` since we no longer need to abstract over the type of the future.
* Change all uses of `ChannelPromise` to `Promise<Void>`.
* Change all uses of `ChannelFuture` to `Future<Void>`.
* Change all uses of `GenericFutureListener` to either `FutureListener` or `FutureContextListener` as needed.
* Remove `ChannelFutureListener` and `GenericFutureListener`.
* Introduce a `ChannelFutureListeners` enum to house the constants that previously lived in `ChannelFutureListener`. These constants now implement `FutureContextListener` and take the `Channel` as a context.
* Remove `ChannelPromise` and `ChannelFuture` — all usages now rely on the plain `Future` and `Promise` APIs.
* Add static factory methods to `DefaultPromise` that allow us to create promises that are initialised as successful or failed.
* Remove `CompleteFuture`, `SucceededFuture`, `FailedFuture`, `CompleteChannelFuture`, `SucceededChannelFuture`, and `FailedChannelFuture`.
* Remove `ChannelPromiseNotifier`.
Result:
Cleaner generics and more straight forward code.
2021-08-20 09:55:16 +02:00
|
|
|
import static io.netty.handler.codec.http.HttpMethod.GET;
|
|
|
|
import static io.netty.handler.codec.http.HttpResponseStatus.BAD_REQUEST;
|
|
|
|
import static io.netty.handler.codec.http.HttpResponseStatus.FORBIDDEN;
|
|
|
|
import static io.netty.handler.codec.http.HttpUtil.isKeepAlive;
|
|
|
|
import static io.netty.handler.codec.http.HttpUtil.setContentLength;
|
|
|
|
import static io.netty.handler.codec.http.HttpVersion.HTTP_1_1;
|
2012-11-12 03:23:06 +01:00
|
|
|
|
2011-10-17 06:12:37 +02:00
|
|
|
/**
|
|
|
|
* Handles handshakes and messages
|
|
|
|
*/
|
2020-01-21 02:47:17 +01:00
|
|
|
public class AutobahnServerHandler implements ChannelHandler {
|
2013-02-11 12:17:35 +01:00
|
|
|
private static final Logger logger = Logger.getLogger(AutobahnServerHandler.class.getName());
|
2011-10-17 06:12:37 +02:00
|
|
|
|
2012-01-11 12:16:14 +01:00
|
|
|
private WebSocketServerHandshaker handshaker;
|
2011-10-17 06:12:37 +02:00
|
|
|
|
2011-12-15 12:25:40 +01:00
|
|
|
@Override
|
2013-07-09 16:09:28 +02:00
|
|
|
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
|
2015-01-06 06:55:01 +01:00
|
|
|
if (msg instanceof HttpRequest) {
|
|
|
|
handleHttpRequest(ctx, (HttpRequest) msg);
|
Remove MessageList from public API and change ChannelInbound/OutboundHandler accordingly
I must admit MesageList was pain in the ass. Instead of forcing a
handler always loop over the list of messages, this commit splits
messageReceived(ctx, list) into two event handlers:
- messageReceived(ctx, msg)
- mmessageReceivedLast(ctx)
When Netty reads one or more messages, messageReceived(ctx, msg) event
is triggered for each message. Once the current read operation is
finished, messageReceivedLast() is triggered to tell the handler that
the last messageReceived() was the last message in the current batch.
Similarly, for outbound, write(ctx, list) has been split into two:
- write(ctx, msg)
- flush(ctx, promise)
Instead of writing a list of message with a promise, a user is now
supposed to call write(msg) multiple times and then call flush() to
actually flush the buffered messages.
Please note that write() doesn't have a promise with it. You must call
flush() to get notified on completion. (or you can use writeAndFlush())
Other changes:
- Because MessageList is completely hidden, codec framework uses
List<Object> instead of MessageList as an output parameter.
2013-07-08 12:03:40 +02:00
|
|
|
} else if (msg instanceof WebSocketFrame) {
|
|
|
|
handleWebSocketFrame(ctx, (WebSocketFrame) msg);
|
2013-11-15 12:00:33 +01:00
|
|
|
} else {
|
2014-04-24 14:13:19 +02:00
|
|
|
throw new IllegalStateException("unknown message: " + msg);
|
2011-12-15 12:25:40 +01:00
|
|
|
}
|
Remove MessageList from public API and change ChannelInbound/OutboundHandler accordingly
I must admit MesageList was pain in the ass. Instead of forcing a
handler always loop over the list of messages, this commit splits
messageReceived(ctx, list) into two event handlers:
- messageReceived(ctx, msg)
- mmessageReceivedLast(ctx)
When Netty reads one or more messages, messageReceived(ctx, msg) event
is triggered for each message. Once the current read operation is
finished, messageReceivedLast() is triggered to tell the handler that
the last messageReceived() was the last message in the current batch.
Similarly, for outbound, write(ctx, list) has been split into two:
- write(ctx, msg)
- flush(ctx, promise)
Instead of writing a list of message with a promise, a user is now
supposed to call write(msg) multiple times and then call flush() to
actually flush the buffered messages.
Please note that write() doesn't have a promise with it. You must call
flush() to get notified on completion. (or you can use writeAndFlush())
Other changes:
- Because MessageList is completely hidden, codec framework uses
List<Object> instead of MessageList as an output parameter.
2013-07-08 12:03:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2013-07-09 16:09:28 +02:00
|
|
|
public void channelReadComplete(ChannelHandlerContext ctx) throws Exception {
|
Remove MessageList from public API and change ChannelInbound/OutboundHandler accordingly
I must admit MesageList was pain in the ass. Instead of forcing a
handler always loop over the list of messages, this commit splits
messageReceived(ctx, list) into two event handlers:
- messageReceived(ctx, msg)
- mmessageReceivedLast(ctx)
When Netty reads one or more messages, messageReceived(ctx, msg) event
is triggered for each message. Once the current read operation is
finished, messageReceivedLast() is triggered to tell the handler that
the last messageReceived() was the last message in the current batch.
Similarly, for outbound, write(ctx, list) has been split into two:
- write(ctx, msg)
- flush(ctx, promise)
Instead of writing a list of message with a promise, a user is now
supposed to call write(msg) multiple times and then call flush() to
actually flush the buffered messages.
Please note that write() doesn't have a promise with it. You must call
flush() to get notified on completion. (or you can use writeAndFlush())
Other changes:
- Because MessageList is completely hidden, codec framework uses
List<Object> instead of MessageList as an output parameter.
2013-07-08 12:03:40 +02:00
|
|
|
ctx.flush();
|
2011-12-15 12:25:40 +01:00
|
|
|
}
|
2011-10-17 06:12:37 +02:00
|
|
|
|
2015-01-06 06:55:01 +01:00
|
|
|
private void handleHttpRequest(ChannelHandlerContext ctx, HttpRequest req)
|
Revamp the core API to reduce memory footprint and consumption
The API changes made so far turned out to increase the memory footprint
and consumption while our intention was actually decreasing them.
Memory consumption issue:
When there are many connections which does not exchange data frequently,
the old Netty 4 API spent a lot more memory than 3 because it always
allocates per-handler buffer for each connection unless otherwise
explicitly stated by a user. In a usual real world load, a client
doesn't always send requests without pausing, so the idea of having a
buffer whose life cycle if bound to the life cycle of a connection
didn't work as expected.
Memory footprint issue:
The old Netty 4 API decreased overall memory footprint by a great deal
in many cases. It was mainly because the old Netty 4 API did not
allocate a new buffer and event object for each read. Instead, it
created a new buffer for each handler in a pipeline. This works pretty
well as long as the number of handlers in a pipeline is only a few.
However, for a highly modular application with many handlers which
handles connections which lasts for relatively short period, it actually
makes the memory footprint issue much worse.
Changes:
All in all, this is about retaining all the good changes we made in 4 so
far such as better thread model and going back to the way how we dealt
with message events in 3.
To fix the memory consumption/footprint issue mentioned above, we made a
hard decision to break the backward compatibility again with the
following changes:
- Remove MessageBuf
- Merge Buf into ByteBuf
- Merge ChannelInboundByte/MessageHandler and ChannelStateHandler into ChannelInboundHandler
- Similar changes were made to the adapter classes
- Merge ChannelOutboundByte/MessageHandler and ChannelOperationHandler into ChannelOutboundHandler
- Similar changes were made to the adapter classes
- Introduce MessageList which is similar to `MessageEvent` in Netty 3
- Replace inboundBufferUpdated(ctx) with messageReceived(ctx, MessageList)
- Replace flush(ctx, promise) with write(ctx, MessageList, promise)
- Remove ByteToByteEncoder/Decoder/Codec
- Replaced by MessageToByteEncoder<ByteBuf>, ByteToMessageDecoder<ByteBuf>, and ByteMessageCodec<ByteBuf>
- Merge EmbeddedByteChannel and EmbeddedMessageChannel into EmbeddedChannel
- Add SimpleChannelInboundHandler which is sometimes more useful than
ChannelInboundHandlerAdapter
- Bring back Channel.isWritable() from Netty 3
- Add ChannelInboundHandler.channelWritabilityChanges() event
- Add RecvByteBufAllocator configuration property
- Similar to ReceiveBufferSizePredictor in Netty 3
- Some existing configuration properties such as
DatagramChannelConfig.receivePacketSize is gone now.
- Remove suspend/resumeIntermediaryDeallocation() in ByteBuf
This change would have been impossible without @normanmaurer's help. He
fixed, ported, and improved many parts of the changes.
2013-05-28 13:40:19 +02:00
|
|
|
throws Exception {
|
2012-09-28 08:42:38 +02:00
|
|
|
// Handle a bad request.
|
2014-06-04 11:34:57 +02:00
|
|
|
if (!req.decoderResult().isSuccess()) {
|
2019-07-18 10:29:50 +02:00
|
|
|
sendHttpResponse(ctx, req, new DefaultFullHttpResponse(HTTP_1_1, BAD_REQUEST, ctx.alloc().buffer(0)));
|
2012-09-28 08:42:38 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2011-12-15 12:25:40 +01:00
|
|
|
// Allow only GET methods.
|
2019-01-30 21:17:00 +01:00
|
|
|
if (!GET.equals(req.method())) {
|
2019-07-18 10:29:50 +02:00
|
|
|
sendHttpResponse(ctx, req, new DefaultFullHttpResponse(HTTP_1_1, FORBIDDEN, ctx.alloc().buffer(0)));
|
2011-12-15 12:25:40 +01:00
|
|
|
return;
|
|
|
|
}
|
2011-10-17 06:12:37 +02:00
|
|
|
|
2011-12-15 12:25:40 +01:00
|
|
|
// Handshake
|
|
|
|
WebSocketServerHandshakerFactory wsFactory = new WebSocketServerHandshakerFactory(
|
2012-07-19 04:40:54 +02:00
|
|
|
getWebSocketLocation(req), null, false, Integer.MAX_VALUE);
|
2012-05-30 01:41:26 +02:00
|
|
|
handshaker = wsFactory.newHandshaker(req);
|
|
|
|
if (handshaker == null) {
|
2014-01-13 01:28:03 +01:00
|
|
|
WebSocketServerHandshakerFactory.sendUnsupportedVersionResponse(ctx.channel());
|
2011-12-15 12:25:40 +01:00
|
|
|
} else {
|
2012-05-30 01:41:26 +02:00
|
|
|
handshaker.handshake(ctx.channel(), req);
|
2011-12-15 12:25:40 +01:00
|
|
|
}
|
|
|
|
}
|
2011-10-17 06:12:37 +02:00
|
|
|
|
Remove MessageList from public API and change ChannelInbound/OutboundHandler accordingly
I must admit MesageList was pain in the ass. Instead of forcing a
handler always loop over the list of messages, this commit splits
messageReceived(ctx, list) into two event handlers:
- messageReceived(ctx, msg)
- mmessageReceivedLast(ctx)
When Netty reads one or more messages, messageReceived(ctx, msg) event
is triggered for each message. Once the current read operation is
finished, messageReceivedLast() is triggered to tell the handler that
the last messageReceived() was the last message in the current batch.
Similarly, for outbound, write(ctx, list) has been split into two:
- write(ctx, msg)
- flush(ctx, promise)
Instead of writing a list of message with a promise, a user is now
supposed to call write(msg) multiple times and then call flush() to
actually flush the buffered messages.
Please note that write() doesn't have a promise with it. You must call
flush() to get notified on completion. (or you can use writeAndFlush())
Other changes:
- Because MessageList is completely hidden, codec framework uses
List<Object> instead of MessageList as an output parameter.
2013-07-08 12:03:40 +02:00
|
|
|
private void handleWebSocketFrame(ChannelHandlerContext ctx, WebSocketFrame frame) {
|
2013-02-11 12:17:35 +01:00
|
|
|
if (logger.isLoggable(Level.FINE)) {
|
|
|
|
logger.fine(String.format(
|
2013-11-04 11:42:33 +01:00
|
|
|
"Channel %s received %s", ctx.channel().hashCode(), StringUtil.simpleClassName(frame)));
|
2012-02-17 10:37:41 +01:00
|
|
|
}
|
|
|
|
|
2011-12-15 12:25:40 +01:00
|
|
|
if (frame instanceof CloseWebSocketFrame) {
|
2021-04-21 15:16:10 +02:00
|
|
|
handshaker.close(ctx, (CloseWebSocketFrame) frame);
|
2011-12-15 12:25:40 +01:00
|
|
|
} else if (frame instanceof PingWebSocketFrame) {
|
2014-04-24 14:13:19 +02:00
|
|
|
ctx.write(new PongWebSocketFrame(frame.isFinalFragment(), frame.rsv(), frame.content()));
|
2014-06-04 11:34:57 +02:00
|
|
|
} else if (frame instanceof TextWebSocketFrame ||
|
|
|
|
frame instanceof BinaryWebSocketFrame ||
|
|
|
|
frame instanceof ContinuationWebSocketFrame) {
|
2014-04-24 14:13:19 +02:00
|
|
|
ctx.write(frame);
|
2011-12-15 12:25:40 +01:00
|
|
|
} else if (frame instanceof PongWebSocketFrame) {
|
Revamp the core API to reduce memory footprint and consumption
The API changes made so far turned out to increase the memory footprint
and consumption while our intention was actually decreasing them.
Memory consumption issue:
When there are many connections which does not exchange data frequently,
the old Netty 4 API spent a lot more memory than 3 because it always
allocates per-handler buffer for each connection unless otherwise
explicitly stated by a user. In a usual real world load, a client
doesn't always send requests without pausing, so the idea of having a
buffer whose life cycle if bound to the life cycle of a connection
didn't work as expected.
Memory footprint issue:
The old Netty 4 API decreased overall memory footprint by a great deal
in many cases. It was mainly because the old Netty 4 API did not
allocate a new buffer and event object for each read. Instead, it
created a new buffer for each handler in a pipeline. This works pretty
well as long as the number of handlers in a pipeline is only a few.
However, for a highly modular application with many handlers which
handles connections which lasts for relatively short period, it actually
makes the memory footprint issue much worse.
Changes:
All in all, this is about retaining all the good changes we made in 4 so
far such as better thread model and going back to the way how we dealt
with message events in 3.
To fix the memory consumption/footprint issue mentioned above, we made a
hard decision to break the backward compatibility again with the
following changes:
- Remove MessageBuf
- Merge Buf into ByteBuf
- Merge ChannelInboundByte/MessageHandler and ChannelStateHandler into ChannelInboundHandler
- Similar changes were made to the adapter classes
- Merge ChannelOutboundByte/MessageHandler and ChannelOperationHandler into ChannelOutboundHandler
- Similar changes were made to the adapter classes
- Introduce MessageList which is similar to `MessageEvent` in Netty 3
- Replace inboundBufferUpdated(ctx) with messageReceived(ctx, MessageList)
- Replace flush(ctx, promise) with write(ctx, MessageList, promise)
- Remove ByteToByteEncoder/Decoder/Codec
- Replaced by MessageToByteEncoder<ByteBuf>, ByteToMessageDecoder<ByteBuf>, and ByteMessageCodec<ByteBuf>
- Merge EmbeddedByteChannel and EmbeddedMessageChannel into EmbeddedChannel
- Add SimpleChannelInboundHandler which is sometimes more useful than
ChannelInboundHandlerAdapter
- Bring back Channel.isWritable() from Netty 3
- Add ChannelInboundHandler.channelWritabilityChanges() event
- Add RecvByteBufAllocator configuration property
- Similar to ReceiveBufferSizePredictor in Netty 3
- Some existing configuration properties such as
DatagramChannelConfig.receivePacketSize is gone now.
- Remove suspend/resumeIntermediaryDeallocation() in ByteBuf
This change would have been impossible without @normanmaurer's help. He
fixed, ported, and improved many parts of the changes.
2013-05-28 13:40:19 +02:00
|
|
|
frame.release();
|
2011-12-15 12:25:40 +01:00
|
|
|
// Ignore
|
|
|
|
} else {
|
|
|
|
throw new UnsupportedOperationException(String.format("%s frame types not supported", frame.getClass()
|
|
|
|
.getName()));
|
|
|
|
}
|
2013-02-20 07:05:35 +01:00
|
|
|
}
|
|
|
|
|
2013-01-16 05:22:50 +01:00
|
|
|
private static void sendHttpResponse(
|
2015-01-06 06:55:01 +01:00
|
|
|
ChannelHandlerContext ctx, HttpRequest req, FullHttpResponse res) {
|
2014-06-24 10:39:46 +02:00
|
|
|
// Generate an error page if response status code is not OK (200).
|
|
|
|
if (res.status().code() != 200) {
|
|
|
|
ByteBuf buf = Unpooled.copiedBuffer(res.status().toString(), CharsetUtil.UTF_8);
|
2013-07-14 22:50:53 +02:00
|
|
|
res.content().writeBytes(buf);
|
|
|
|
buf.release();
|
2013-05-01 10:04:43 +02:00
|
|
|
setContentLength(res, res.content().readableBytes());
|
2011-12-15 12:25:40 +01:00
|
|
|
}
|
2011-10-17 06:12:37 +02:00
|
|
|
|
2011-12-15 12:25:40 +01:00
|
|
|
// Send the response and close the connection if necessary.
|
2021-08-31 12:49:30 +02:00
|
|
|
Future<Void> f = ctx.writeAndFlush(res);
|
2014-06-24 10:39:46 +02:00
|
|
|
if (!isKeepAlive(req) || res.status().code() != 200) {
|
2021-08-31 12:49:30 +02:00
|
|
|
f.addListener(ctx, ChannelFutureListeners.CLOSE);
|
2011-12-15 12:25:40 +01:00
|
|
|
}
|
|
|
|
}
|
2011-10-17 06:12:37 +02:00
|
|
|
|
2011-12-15 12:25:40 +01:00
|
|
|
@Override
|
2012-06-07 07:52:33 +02:00
|
|
|
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
|
2012-05-30 01:41:26 +02:00
|
|
|
ctx.close();
|
2011-12-15 12:25:40 +01:00
|
|
|
}
|
2011-10-17 06:12:37 +02:00
|
|
|
|
2015-01-06 06:55:01 +01:00
|
|
|
private static String getWebSocketLocation(HttpRequest req) {
|
2014-10-31 08:48:28 +01:00
|
|
|
return "ws://" + req.headers().get(HttpHeaderNames.HOST);
|
2011-12-15 12:25:40 +01:00
|
|
|
}
|
2011-10-17 06:12:37 +02:00
|
|
|
}
|