mirror of
https://github.com/SquidDev-CC/CC-Tweaked
synced 2025-07-03 02:22:51 +00:00

- Move all HTTP tasks to a unified "MonitoredResource" model. This provides a uniform way of tracking object's lifetimes and disposing of them when complete. - Rewrite HTTP requests to use Netty instead of standard Java. This offers several advantages: - We have access to more HTTP verbs (mostly PATCH). - We can now do http -> https redirects. - We no longer need to spawn in a new thread for each HTTP request. While we do need to run some tasks off-thread in order to resolve IPs, it's generally a much shorter task, and so is less likely to inflate the thread pool. - Introduce several limits for the http API: - There's a limit on how many HTTP requests and websockets may exist at the same time. If the limit is reached, additional ones will be queued up until pending requests have finished. - HTTP requests may upload a maximum of 4Mib and download a maximum of 16Mib (configurable). - .getResponseCode now returns the status text, as well as the status code.
114 lines
4.0 KiB
Java
114 lines
4.0 KiB
Java
/*
|
|
* This file is part of ComputerCraft - http://www.computercraft.info
|
|
* Copyright Daniel Ratcliffe, 2011-2019. Do not distribute without permission.
|
|
* Send enquiries to dratcliffe@gmail.com
|
|
*/
|
|
|
|
package dan200.computercraft.core.apis.http.websocket;
|
|
|
|
import dan200.computercraft.core.apis.http.HTTPRequestException;
|
|
import dan200.computercraft.core.apis.http.NetworkUtils;
|
|
import dan200.computercraft.core.tracking.TrackingField;
|
|
import io.netty.channel.ChannelHandlerContext;
|
|
import io.netty.channel.ConnectTimeoutException;
|
|
import io.netty.channel.SimpleChannelInboundHandler;
|
|
import io.netty.handler.codec.TooLongFrameException;
|
|
import io.netty.handler.codec.http.FullHttpResponse;
|
|
import io.netty.handler.codec.http.websocketx.*;
|
|
import io.netty.handler.timeout.ReadTimeoutException;
|
|
import io.netty.util.CharsetUtil;
|
|
|
|
import static dan200.computercraft.core.apis.http.websocket.Websocket.MESSAGE_EVENT;
|
|
|
|
public class WebsocketHandler extends SimpleChannelInboundHandler<Object>
|
|
{
|
|
private final Websocket websocket;
|
|
private final WebSocketClientHandshaker handshaker;
|
|
|
|
public WebsocketHandler( Websocket websocket, WebSocketClientHandshaker handshaker )
|
|
{
|
|
this.handshaker = handshaker;
|
|
this.websocket = websocket;
|
|
}
|
|
|
|
@Override
|
|
public void channelActive( ChannelHandlerContext ctx ) throws Exception
|
|
{
|
|
handshaker.handshake( ctx.channel() );
|
|
super.channelActive( ctx );
|
|
}
|
|
|
|
@Override
|
|
public void channelInactive( ChannelHandlerContext ctx ) throws Exception
|
|
{
|
|
websocket.close( -1, "Websocket is inactive" );
|
|
super.channelInactive( ctx );
|
|
}
|
|
|
|
@Override
|
|
public void channelRead0( ChannelHandlerContext ctx, Object msg )
|
|
{
|
|
if( websocket.isClosed() ) return;
|
|
|
|
if( !handshaker.isHandshakeComplete() )
|
|
{
|
|
handshaker.finishHandshake( ctx.channel(), (FullHttpResponse) msg );
|
|
websocket.success( ctx.channel() );
|
|
return;
|
|
}
|
|
|
|
if( msg instanceof FullHttpResponse )
|
|
{
|
|
FullHttpResponse response = (FullHttpResponse) msg;
|
|
throw new IllegalStateException( "Unexpected FullHttpResponse (getStatus=" + response.status() + ", content=" + response.content().toString( CharsetUtil.UTF_8 ) + ')' );
|
|
}
|
|
|
|
WebSocketFrame frame = (WebSocketFrame) msg;
|
|
if( frame instanceof TextWebSocketFrame )
|
|
{
|
|
String data = ((TextWebSocketFrame) frame).text();
|
|
|
|
websocket.environment().addTrackingChange( TrackingField.WEBSOCKET_INCOMING, data.length() );
|
|
websocket.environment().queueEvent( MESSAGE_EVENT, new Object[] { websocket.address(), data, false } );
|
|
}
|
|
else if( frame instanceof BinaryWebSocketFrame )
|
|
{
|
|
byte[] converted = NetworkUtils.toBytes( frame.content() );
|
|
|
|
websocket.environment().addTrackingChange( TrackingField.WEBSOCKET_INCOMING, converted.length );
|
|
websocket.environment().queueEvent( MESSAGE_EVENT, new Object[] { websocket.address(), converted, true } );
|
|
}
|
|
else if( frame instanceof CloseWebSocketFrame )
|
|
{
|
|
CloseWebSocketFrame closeFrame = (CloseWebSocketFrame) frame;
|
|
websocket.close( closeFrame.statusCode(), closeFrame.reasonText() );
|
|
}
|
|
}
|
|
|
|
@Override
|
|
public void exceptionCaught( ChannelHandlerContext ctx, Throwable cause )
|
|
{
|
|
ctx.close();
|
|
|
|
String message;
|
|
if( cause instanceof WebSocketHandshakeException || cause instanceof HTTPRequestException )
|
|
{
|
|
message = cause.getMessage();
|
|
}
|
|
else if( cause instanceof TooLongFrameException )
|
|
{
|
|
message = "Message is too large";
|
|
}
|
|
else if( cause instanceof ReadTimeoutException || cause instanceof ConnectTimeoutException )
|
|
{
|
|
message = "Timed out";
|
|
}
|
|
else
|
|
{
|
|
message = "Could not connect";
|
|
}
|
|
|
|
websocket.failure( message );
|
|
}
|
|
}
|