1
0
mirror of https://github.com/SquidDev-CC/CC-Tweaked synced 2025-11-06 00:22:58 +00:00

WIP: Http rework (#98)

- 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.
This commit is contained in:
SquidDev
2019-01-11 11:33:05 +00:00
committed by GitHub
parent 101b3500cc
commit 932f8a44fc
28 changed files with 1719 additions and 939 deletions

View File

@@ -11,6 +11,7 @@ import com.google.common.base.Converter;
import dan200.computercraft.ComputerCraft;
import dan200.computercraft.api.turtle.event.TurtleAction;
import dan200.computercraft.core.apis.AddressPredicate;
import dan200.computercraft.core.apis.http.websocket.Websocket;
import net.minecraftforge.common.config.ConfigCategory;
import net.minecraftforge.common.config.ConfigElement;
import net.minecraftforge.common.config.Configuration;
@@ -35,11 +36,6 @@ public class Config
private static Configuration config;
private static Property httpEnable;
private static Property httpWebsocketEnable;
private static Property httpWhitelist;
private static Property httpBlacklist;
private static Property computerSpaceLimit;
private static Property floppySpaceLimit;
private static Property maximumFilesOpen;
@@ -49,12 +45,16 @@ public class Config
private static Property computerThreads;
private static Property logComputerErrors;
private static Property turtlesNeedFuel;
private static Property turtleFuelLimit;
private static Property advancedTurtleFuelLimit;
private static Property turtlesObeyBlockProtection;
private static Property turtlesCanPush;
private static Property turtleDisabledActions;
private static Property httpEnable;
private static Property httpWebsocketEnable;
private static Property httpWhitelist;
private static Property httpBlacklist;
private static Property httpMaxRequests;
private static Property httpMaxDownload;
private static Property httpMaxUpload;
private static Property httpMaxWebsockets;
private static Property httpMaxWebsocketMessage;
private static Property commandBlockEnabled;
private static Property modemRange;
@@ -63,6 +63,13 @@ public class Config
private static Property modemHighAltitudeRangeDuringStorm;
private static Property maxNotesPerTick;
private static Property turtlesNeedFuel;
private static Property turtleFuelLimit;
private static Property advancedTurtleFuelLimit;
private static Property turtlesObeyBlockProtection;
private static Property turtlesCanPush;
private static Property turtleDisabledActions;
public static void load( File configFile )
{
config = new Configuration( configFile );
@@ -135,9 +142,31 @@ public class Config
"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\")." );
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 );
httpMaxDownload = config.get( CATEGORY_HTTP, "max_download", (int) ComputerCraft.httpMaxDownload );
httpMaxDownload.setComment( "The maximum size (in bytes) that a computer can download in a single request. Note that responses may receive more data than allowed, but this data will not be returned to the client." );
httpMaxDownload.setMinValue( 0 );
httpMaxUpload = config.get( CATEGORY_HTTP, "max_upload", (int) ComputerCraft.httpMaxUpload );
httpMaxUpload.setComment( "The maximum size (in bytes) that a computer can upload in a single request. This includes headers and POST text." );
httpMaxUpload.setMinValue( 0 );
httpMaxWebsockets = config.get( CATEGORY_HTTP, "max_websockets", ComputerCraft.httpMaxWebsockets );
httpMaxWebsockets.setComment( "The number of websockets a computer can have open at one time. Set to 0 for unlimited." );
httpMaxWebsockets.setMinValue( 1 );
httpMaxWebsocketMessage = config.get( CATEGORY_HTTP, "max_websocket_message", ComputerCraft.httpMaxWebsocketMessage );
httpMaxWebsocketMessage.setComment( "The maximum size (in bytes) that a computer can send or receive in one websocket packet." );
httpMaxWebsocketMessage.setMinValue( 0 );
httpMaxWebsocketMessage.setMaxValue( Websocket.MAX_MESSAGE_SIZE );
setOrder(
CATEGORY_HTTP,
httpEnable, httpWebsocketEnable, httpWhitelist, httpBlacklist
httpEnable, httpWebsocketEnable, httpWhitelist, httpBlacklist,
httpMaxRequests, httpMaxDownload, httpMaxUpload, httpMaxWebsockets, httpMaxWebsocketMessage
);
}
@@ -286,6 +315,12 @@ public class Config
ComputerCraft.http_whitelist = new AddressPredicate( httpWhitelist.getStringList() );
ComputerCraft.http_blacklist = new AddressPredicate( httpBlacklist.getStringList() );
ComputerCraft.httpMaxRequests = Math.max( 1, httpMaxRequests.getInt() );
ComputerCraft.httpMaxDownload = Math.max( 0, httpMaxDownload.getLong() );
ComputerCraft.httpMaxUpload = Math.max( 0, httpMaxUpload.getLong() );
ComputerCraft.httpMaxWebsockets = Math.max( 1, httpMaxWebsockets.getInt() );
ComputerCraft.httpMaxWebsocketMessage = Math.max( 0, httpMaxWebsocketMessage.getInt() );
// Peripheral
ComputerCraft.enableCommandBlock = commandBlockEnabled.getBoolean();
ComputerCraft.maxNotesPerTick = Math.max( 1, maxNotesPerTick.getInt() );

View File

@@ -64,6 +64,13 @@ public class ClientComputer extends ClientTerminal implements IComputer
return m_instanceID;
}
@Override
@Deprecated
public int getID()
{
return -1;
}
@Override
@Deprecated
public String getLabel()

View File

@@ -13,6 +13,9 @@ public interface IComputer extends ITerminal
{
int getInstanceID();
@Deprecated
int getID();
@Deprecated
String getLabel();

View File

@@ -226,6 +226,8 @@ public class ServerComputer extends ServerTerminal implements IComputer, IComput
return m_instanceID;
}
@Override
@SuppressWarnings( "deprecation" )
public int getID()
{
return m_computer.getID();

View File

@@ -9,6 +9,7 @@ package dan200.computercraft.shared.util;
import dan200.computercraft.ComputerCraft;
import java.io.*;
import java.nio.charset.StandardCharsets;
public class IDAssigner
{
@@ -71,14 +72,7 @@ public class IDAssigner
{
FileInputStream in = new FileInputStream( lastidFile );
InputStreamReader isr;
try
{
isr = new InputStreamReader( in, "UTF-8" );
}
catch( UnsupportedEncodingException e )
{
isr = new InputStreamReader( in );
}
isr = new InputStreamReader( in, StandardCharsets.UTF_8 );
try( BufferedReader br = new BufferedReader( isr ) )
{
idString = br.readLine();

View File

@@ -0,0 +1,24 @@
/*
* 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.shared.util;
import java.io.Closeable;
import java.io.IOException;
public class IoUtil
{
public static void closeQuietly( Closeable closeable )
{
try
{
closeable.close();
}
catch( IOException ignored )
{
}
}
}