From 66b61d4e9e3489cdca577852541900de262a02e0 Mon Sep 17 00:00:00 2001 From: SquidDev Date: Fri, 11 Jan 2019 21:11:22 +0000 Subject: [PATCH] Add a config option for HTTP timeout too --- .../java/dan200/computercraft/ComputerCraft.java | 1 + .../core/apis/http/request/HttpRequest.java | 14 ++++++++++---- .../java/dan200/computercraft/shared/Config.java | 10 ++++++++-- .../resources/assets/computercraft/lang/en_us.lang | 1 + 4 files changed, 20 insertions(+), 6 deletions(-) diff --git a/src/main/java/dan200/computercraft/ComputerCraft.java b/src/main/java/dan200/computercraft/ComputerCraft.java index 7384f012c..96f777f98 100644 --- a/src/main/java/dan200/computercraft/ComputerCraft.java +++ b/src/main/java/dan200/computercraft/ComputerCraft.java @@ -138,6 +138,7 @@ public class ComputerCraft public static AddressPredicate http_whitelist = new AddressPredicate( DEFAULT_HTTP_WHITELIST ); public static AddressPredicate http_blacklist = new AddressPredicate( DEFAULT_HTTP_BLACKLIST ); + public static int httpTimeout = 30000; public static int httpMaxRequests = 16; public static long httpMaxDownload = 16 * 1024 * 1024; public static long httpMaxUpload = 4 * 1024 * 1024; diff --git a/src/main/java/dan200/computercraft/core/apis/http/request/HttpRequest.java b/src/main/java/dan200/computercraft/core/apis/http/request/HttpRequest.java index d23673c74..779e3f5fa 100644 --- a/src/main/java/dan200/computercraft/core/apis/http/request/HttpRequest.java +++ b/src/main/java/dan200/computercraft/core/apis/http/request/HttpRequest.java @@ -49,8 +49,6 @@ public class HttpRequest extends Resource private static final int MAX_REDIRECTS = 16; - private static final int TIMEOUT = 30000; - private Future executorFuture; private ChannelFuture connectFuture; private HttpRequestHandler currentRequest; @@ -160,15 +158,23 @@ private void doRequest( URI uri, HttpMethod method ) protected void initChannel( SocketChannel ch ) { - ch.config().setConnectTimeoutMillis( TIMEOUT ); + if( ComputerCraft.httpTimeout > 0 ) + { + ch.config().setConnectTimeoutMillis( ComputerCraft.httpTimeout ); + } ChannelPipeline p = ch.pipeline(); if( sslContext != null ) { p.addLast( sslContext.newHandler( ch.alloc(), uri.getHost(), socketAddress.getPort() ) ); } + + if( ComputerCraft.httpTimeout > 0 ) + { + p.addLast( new ReadTimeoutHandler( ComputerCraft.httpTimeout, TimeUnit.MILLISECONDS ) ); + } + p.addLast( - new ReadTimeoutHandler( TIMEOUT, TimeUnit.MILLISECONDS ), new HttpClientCodec(), new HttpContentDecompressor(), handler diff --git a/src/main/java/dan200/computercraft/shared/Config.java b/src/main/java/dan200/computercraft/shared/Config.java index 22af3742f..78577e75e 100644 --- a/src/main/java/dan200/computercraft/shared/Config.java +++ b/src/main/java/dan200/computercraft/shared/Config.java @@ -50,6 +50,7 @@ public class Config private static Property httpWhitelist; private static Property httpBlacklist; + private static Property httpTimeout; private static Property httpMaxRequests; private static Property httpMaxDownload; private static Property httpMaxUpload; @@ -142,6 +143,10 @@ public static void load( File configFile ) "If this is empty then all whitelisted domains will be accessible. Example: \"*.github.com\" will block access to all subdomains of github.com.\n" + "You can use domain names (\"pastebin.com\"), wilcards (\"*.pastebin.com\") or CIDR notation (\"127.0.0.0/8\")." ); + httpTimeout = config.get( CATEGORY_HTTP, "timeout", ComputerCraft.httpTimeout ); + httpTimeout.setComment( "The period of time (in milliseconds) to wait before a HTTP request times out. Set to 0 for unlimited." ); + httpTimeout.setMinValue( 0 ); + httpMaxRequests = config.get( CATEGORY_HTTP, "max_requests", ComputerCraft.httpMaxRequests ); httpMaxRequests.setComment( "The number of http requests a computer can make at one time. Additional requests will be queued, and sent when the running requests have finished. Set to 0 for unlimited." ); httpMaxRequests.setMinValue( 0 ); @@ -166,7 +171,7 @@ public static void load( File configFile ) setOrder( CATEGORY_HTTP, httpEnable, httpWebsocketEnable, httpWhitelist, httpBlacklist, - httpMaxRequests, httpMaxDownload, httpMaxUpload, httpMaxWebsockets, httpMaxWebsocketMessage + httpTimeout, httpMaxRequests, httpMaxDownload, httpMaxUpload, httpMaxWebsockets, httpMaxWebsocketMessage ); } @@ -282,7 +287,7 @@ private static void setupLanguage( ConfigCategory category, String key ) category.setLanguageKey( key ); for( Property property : category.getOrderedValues() ) { - property.setLanguageKey( key + "." + CaseFormat.LOWER_CAMEL.to( CaseFormat.LOWER_UNDERSCORE, property.getName() ) ); + property.setLanguageKey( key + "." + property.getName() ); } for( ConfigCategory child : category.getChildren() ) @@ -315,6 +320,7 @@ public static void sync() ComputerCraft.http_whitelist = new AddressPredicate( httpWhitelist.getStringList() ); ComputerCraft.http_blacklist = new AddressPredicate( httpBlacklist.getStringList() ); + ComputerCraft.httpTimeout = Math.max( 0, httpTimeout.getInt() ); ComputerCraft.httpMaxRequests = Math.max( 1, httpMaxRequests.getInt() ); ComputerCraft.httpMaxDownload = Math.max( 0, httpMaxDownload.getLong() ); ComputerCraft.httpMaxUpload = Math.max( 0, httpMaxUpload.getLong() ); diff --git a/src/main/resources/assets/computercraft/lang/en_us.lang b/src/main/resources/assets/computercraft/lang/en_us.lang index c8c353162..bbc526999 100644 --- a/src/main/resources/assets/computercraft/lang/en_us.lang +++ b/src/main/resources/assets/computercraft/lang/en_us.lang @@ -59,6 +59,7 @@ gui.computercraft:config.http.websocket_enabled=Enable websockets gui.computercraft:config.http.whitelist=HTTP whitelist gui.computercraft:config.http.blacklist=HTTP blacklist +gui.computercraft:config.http.timeout=Timeout gui.computercraft:config.http.max_requests=Maximum concurrent requests gui.computercraft:config.http.max_download=Maximum response size gui.computercraft:config.http.max_upload=Maximum request