mirror of
				https://github.com/SquidDev-CC/CC-Tweaked
				synced 2025-10-26 03:17:38 +00:00 
			
		
		
		
	| @@ -11,12 +11,19 @@ import dan200.computercraft.core.apis.http.options.AddressRule; | |||||||
| import dan200.computercraft.core.apis.http.options.Options; | import dan200.computercraft.core.apis.http.options.Options; | ||||||
| import dan200.computercraft.shared.util.ThreadUtils; | import dan200.computercraft.shared.util.ThreadUtils; | ||||||
| import io.netty.buffer.ByteBuf; | import io.netty.buffer.ByteBuf; | ||||||
|  | import io.netty.channel.ConnectTimeoutException; | ||||||
| import io.netty.channel.EventLoopGroup; | import io.netty.channel.EventLoopGroup; | ||||||
| import io.netty.channel.nio.NioEventLoopGroup; | import io.netty.channel.nio.NioEventLoopGroup; | ||||||
|  | import io.netty.handler.codec.DecoderException; | ||||||
|  | import io.netty.handler.codec.TooLongFrameException; | ||||||
|  | import io.netty.handler.codec.http.websocketx.WebSocketHandshakeException; | ||||||
| import io.netty.handler.ssl.SslContext; | import io.netty.handler.ssl.SslContext; | ||||||
| import io.netty.handler.ssl.SslContextBuilder; | import io.netty.handler.ssl.SslContextBuilder; | ||||||
|  | import io.netty.handler.timeout.ReadTimeoutException; | ||||||
|  |  | ||||||
|  | import javax.annotation.Nonnull; | ||||||
| import javax.net.ssl.SSLException; | import javax.net.ssl.SSLException; | ||||||
|  | import javax.net.ssl.SSLHandshakeException; | ||||||
| import javax.net.ssl.TrustManagerFactory; | import javax.net.ssl.TrustManagerFactory; | ||||||
| import java.net.InetSocketAddress; | import java.net.InetSocketAddress; | ||||||
| import java.net.URI; | import java.net.URI; | ||||||
| @@ -161,4 +168,29 @@ public final class NetworkUtils | |||||||
|         buffer.readBytes( bytes ); |         buffer.readBytes( bytes ); | ||||||
|         return bytes; |         return bytes; | ||||||
|     } |     } | ||||||
|  |  | ||||||
|  |     @Nonnull | ||||||
|  |     public static String toFriendlyError( @Nonnull Throwable cause ) | ||||||
|  |     { | ||||||
|  |         if( cause instanceof WebSocketHandshakeException || cause instanceof HTTPRequestException ) | ||||||
|  |         { | ||||||
|  |             return cause.getMessage(); | ||||||
|  |         } | ||||||
|  |         else if( cause instanceof TooLongFrameException ) | ||||||
|  |         { | ||||||
|  |             return "Message is too large"; | ||||||
|  |         } | ||||||
|  |         else if( cause instanceof ReadTimeoutException || cause instanceof ConnectTimeoutException ) | ||||||
|  |         { | ||||||
|  |             return "Timed out"; | ||||||
|  |         } | ||||||
|  |         else if( cause instanceof SSLHandshakeException || (cause instanceof DecoderException && cause.getCause() instanceof SSLHandshakeException) ) | ||||||
|  |         { | ||||||
|  |             return "Could not create a secure connection"; | ||||||
|  |         } | ||||||
|  |         else | ||||||
|  |         { | ||||||
|  |             return "Could not connect"; | ||||||
|  |         } | ||||||
|  |     } | ||||||
| } | } | ||||||
|   | |||||||
| @@ -19,13 +19,10 @@ import io.netty.buffer.Unpooled; | |||||||
| import io.netty.channel.ChannelFuture; | import io.netty.channel.ChannelFuture; | ||||||
| import io.netty.channel.ChannelInitializer; | import io.netty.channel.ChannelInitializer; | ||||||
| import io.netty.channel.ChannelPipeline; | import io.netty.channel.ChannelPipeline; | ||||||
| import io.netty.channel.ConnectTimeoutException; |  | ||||||
| import io.netty.channel.socket.SocketChannel; | import io.netty.channel.socket.SocketChannel; | ||||||
| import io.netty.channel.socket.nio.NioSocketChannel; | import io.netty.channel.socket.nio.NioSocketChannel; | ||||||
| import io.netty.handler.codec.TooLongFrameException; |  | ||||||
| import io.netty.handler.codec.http.*; | import io.netty.handler.codec.http.*; | ||||||
| import io.netty.handler.ssl.SslContext; | import io.netty.handler.ssl.SslContext; | ||||||
| import io.netty.handler.timeout.ReadTimeoutException; |  | ||||||
| import io.netty.handler.timeout.ReadTimeoutHandler; | import io.netty.handler.timeout.ReadTimeoutHandler; | ||||||
|  |  | ||||||
| import java.net.InetSocketAddress; | import java.net.InetSocketAddress; | ||||||
| @@ -190,7 +187,7 @@ public class HttpRequest extends Resource<HttpRequest> | |||||||
|                 .remoteAddress( socketAddress ) |                 .remoteAddress( socketAddress ) | ||||||
|                 .connect() |                 .connect() | ||||||
|                 .addListener( c -> { |                 .addListener( c -> { | ||||||
|                     if( !c.isSuccess() ) failure( c.cause() ); |                     if( !c.isSuccess() ) failure( NetworkUtils.toFriendlyError( c.cause() ) ); | ||||||
|                 } ); |                 } ); | ||||||
|  |  | ||||||
|             // Do an additional check for cancellation |             // Do an additional check for cancellation | ||||||
| @@ -202,7 +199,7 @@ public class HttpRequest extends Resource<HttpRequest> | |||||||
|         } |         } | ||||||
|         catch( Exception e ) |         catch( Exception e ) | ||||||
|         { |         { | ||||||
|             failure( "Could not connect" ); |             failure( NetworkUtils.toFriendlyError( e ) ); | ||||||
|             if( ComputerCraft.logComputerErrors ) ComputerCraft.log.error( "Error in HTTP request", e ); |             if( ComputerCraft.logComputerErrors ) ComputerCraft.log.error( "Error in HTTP request", e ); | ||||||
|         } |         } | ||||||
|     } |     } | ||||||
| @@ -212,29 +209,6 @@ public class HttpRequest extends Resource<HttpRequest> | |||||||
|         if( tryClose() ) environment.queueEvent( FAILURE_EVENT, address, message ); |         if( tryClose() ) environment.queueEvent( FAILURE_EVENT, address, message ); | ||||||
|     } |     } | ||||||
|  |  | ||||||
|     void failure( Throwable cause ) |  | ||||||
|     { |  | ||||||
|         String message; |  | ||||||
|         if( cause instanceof HTTPRequestException ) |  | ||||||
|         { |  | ||||||
|             message = cause.getMessage(); |  | ||||||
|         } |  | ||||||
|         else if( cause instanceof TooLongFrameException ) |  | ||||||
|         { |  | ||||||
|             message = "Response is too large"; |  | ||||||
|         } |  | ||||||
|         else if( cause instanceof ReadTimeoutException || cause instanceof ConnectTimeoutException ) |  | ||||||
|         { |  | ||||||
|             message = "Timed out"; |  | ||||||
|         } |  | ||||||
|         else |  | ||||||
|         { |  | ||||||
|             message = "Could not connect"; |  | ||||||
|         } |  | ||||||
|  |  | ||||||
|         failure( message ); |  | ||||||
|     } |  | ||||||
|  |  | ||||||
|     void failure( String message, HttpResponseHandle object ) |     void failure( String message, HttpResponseHandle object ) | ||||||
|     { |     { | ||||||
|         if( tryClose() ) environment.queueEvent( FAILURE_EVENT, address, message, object ); |         if( tryClose() ) environment.queueEvent( FAILURE_EVENT, address, message, object ); | ||||||
|   | |||||||
| @@ -183,7 +183,7 @@ public final class HttpRequestHandler extends SimpleChannelInboundHandler<HttpOb | |||||||
|     public void exceptionCaught( ChannelHandlerContext ctx, Throwable cause ) |     public void exceptionCaught( ChannelHandlerContext ctx, Throwable cause ) | ||||||
|     { |     { | ||||||
|         if( ComputerCraft.logComputerErrors ) ComputerCraft.log.error( "Error handling HTTP response", cause ); |         if( ComputerCraft.logComputerErrors ) ComputerCraft.log.error( "Error handling HTTP response", cause ); | ||||||
|         request.failure( cause ); |         request.failure( NetworkUtils.toFriendlyError( cause ) ); | ||||||
|     } |     } | ||||||
|  |  | ||||||
|     private void sendResponse() |     private void sendResponse() | ||||||
|   | |||||||
| @@ -166,7 +166,7 @@ public class Websocket extends Resource<Websocket> | |||||||
|                 .remoteAddress( socketAddress ) |                 .remoteAddress( socketAddress ) | ||||||
|                 .connect() |                 .connect() | ||||||
|                 .addListener( c -> { |                 .addListener( c -> { | ||||||
|                     if( !c.isSuccess() ) failure( c.cause().getMessage() ); |                     if( !c.isSuccess() ) failure( NetworkUtils.toFriendlyError( c.cause() ) ); | ||||||
|                 } ); |                 } ); | ||||||
|  |  | ||||||
|             // Do an additional check for cancellation |             // Do an additional check for cancellation | ||||||
| @@ -178,7 +178,7 @@ public class Websocket extends Resource<Websocket> | |||||||
|         } |         } | ||||||
|         catch( Exception e ) |         catch( Exception e ) | ||||||
|         { |         { | ||||||
|             failure( "Could not connect" ); |             failure( NetworkUtils.toFriendlyError( e ) ); | ||||||
|             if( ComputerCraft.logComputerErrors ) ComputerCraft.log.error( "Error in websocket", e ); |             if( ComputerCraft.logComputerErrors ) ComputerCraft.log.error( "Error in websocket", e ); | ||||||
|         } |         } | ||||||
|     } |     } | ||||||
|   | |||||||
| @@ -5,17 +5,13 @@ | |||||||
|  */ |  */ | ||||||
| package dan200.computercraft.core.apis.http.websocket; | 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.apis.http.NetworkUtils; | ||||||
| import dan200.computercraft.core.apis.http.options.Options; | import dan200.computercraft.core.apis.http.options.Options; | ||||||
| import dan200.computercraft.core.tracking.TrackingField; | import dan200.computercraft.core.tracking.TrackingField; | ||||||
| import io.netty.channel.ChannelHandlerContext; | import io.netty.channel.ChannelHandlerContext; | ||||||
| import io.netty.channel.ConnectTimeoutException; |  | ||||||
| import io.netty.channel.SimpleChannelInboundHandler; | import io.netty.channel.SimpleChannelInboundHandler; | ||||||
| import io.netty.handler.codec.TooLongFrameException; |  | ||||||
| import io.netty.handler.codec.http.FullHttpResponse; | import io.netty.handler.codec.http.FullHttpResponse; | ||||||
| import io.netty.handler.codec.http.websocketx.*; | import io.netty.handler.codec.http.websocketx.*; | ||||||
| import io.netty.handler.timeout.ReadTimeoutException; |  | ||||||
| import io.netty.util.CharsetUtil; | import io.netty.util.CharsetUtil; | ||||||
|  |  | ||||||
| import static dan200.computercraft.core.apis.http.websocket.Websocket.MESSAGE_EVENT; | import static dan200.computercraft.core.apis.http.websocket.Websocket.MESSAGE_EVENT; | ||||||
| @@ -97,24 +93,7 @@ public class WebsocketHandler extends SimpleChannelInboundHandler<Object> | |||||||
|     { |     { | ||||||
|         ctx.close(); |         ctx.close(); | ||||||
|  |  | ||||||
|         String message; |         String message = NetworkUtils.toFriendlyError( cause ); | ||||||
|         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"; |  | ||||||
|         } |  | ||||||
|  |  | ||||||
|         if( handshaker.isHandshakeComplete() ) |         if( handshaker.isHandshakeComplete() ) | ||||||
|         { |         { | ||||||
|             websocket.close( -1, message ); |             websocket.close( -1, message ); | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user
	 Jonathan Coates
					Jonathan Coates