Jump to content

Internet: Difference between revisions

From Advanced Computers Wiki
Created page with "From https://ac.ghxx.dev/apidocs.txt, {{Todo|format this into a nice table with descriptions}}<syntaxhighlight lang="java" line="1"> @LuaCallable: public LuaObject get(String luaUrl) </syntaxhighlight> In the future we will also support sending http POST requests, etc."
 
No edit summary
 
(5 intermediate revisions by the same user not shown)
Line 1: Line 1:
From https://ac.ghxx.dev/apidocs.txt,  
The InternetUD component allows making real-world [https://simple.wikipedia.org/wiki/Hypertext_Transfer_Protocol HTTP(s)] requests. In order to be able to send HTTP(s) requests, make sure the current computer is connected to a [[WAN Router]] either directly via [[Network Cable]] or indirectly through [[Network Router|Network Routers]].
{{Todo|format this into a nice table with descriptions}}<syntaxhighlight lang="java" line="1">
@LuaCallable:
public LuaObject get(String luaUrl)


</syntaxhighlight>
== Security ==
By default, requests to local IP addresses are blocked (e.g. <code><nowiki>http://127.0.0.1</nowiki></code>). Domains are not filtered. You can allow access to local IPs by disabling the filtering in the config file.
{{Caution|Disabling this filtering will allow ingame computers to access webservices on the server's local machine or network. This could pose a security threat if any of those local webservices assume local traffic to be trustworthy. Be careful!}}
If you wish, HTTP(s) functionality can also be disabled entirely in the config file. This completely removes the ability to make any kind of HTTP(s) requests.


In the future we will also support sending http POST requests, etc.
== API ==
{| class="wikitable"
|+ InternetUD component properties
|-
! Name !! Type !! Read/Write !! Description
|-
| <code>isHttpEnabled</code> || <code>bool</code> || READ || Returns whether HTTP(s) is enabled at all. True by default, but can be changed in the server's config. '''Required to be true in order to use any methods.'''
|-
|<code>isConnectedToWan</code>
|<code>bool</code>
|READ
|Returns whether the current computer can reach a WAN router via the network. '''Required to be true in order to use any methods.'''
|}
 
{| class="wikitable"
|+ InternetUD component methods
|-
! Name !! Returns !! Description
|-
|<code>:sendHttpRequest(url:string</code><code>[, method:string</code><code>[, postData:string</code><code>[, headers:table]]])</code>
|<code>HttpResponseUD</code>
|Asynchronously sends an HTTP(s) request to the given URL with the given request method (e.g. GET, POST, etc.).
If the request method is set to something supporting a post body, you may provide one as a string in <code>postData</code>. Otherwise <code>postData</code> must be set to <code>""</code>.
Headers may be provided as a table consisting of string keys and string values.
'''The request method TRACE is not allowed for security reasons.'''
|}
 
=== Handling responses ===
Upon making an HTTP(s) request you are provided with an <code>HttpResponseUD</code> object offering the following functionality.
 
{| class="wikitable"
|+ HttpResponseUD properties
|-
! Name !! Type !! Read/Write !! Description
|-
| <code>responseAvailable</code> || <code>bool</code> || READ || Returns whether this HTTP(s) request has been completed and therefore a response is available.
|}
 
{| class="wikitable"
|+ HttpResponseUD methods
|-
! Name !! Returns !! Description
|-
|<code>:waitForCompletion()</code>
|<code>void</code>
|Blocks until <code>responseAvailable</code> is true. All other ''functions'' implicitly call this prior to executing and thus show the same behaviour.
|-
|<code>:getStatusCode()</code>
|<code>int</code>
|Returns the [[wikipedia:List_of_HTTP_status_codes|HTTP status]] code of the response. Response codes in the 2xx range typically indicate success.
|-
|<code>:getResponseBody()</code>
|<code>string</code>
|Returns the response body as a string.
|-
|<code>:getResponseBodySize()</code>
|<code>int</code>
|Returns the length of the string that will be returned by <code>:getResponseBody()</code>.
|-
|<code>:getResponseHeaders()</code>
|<code>table</code>
|Returns a table listing all response headers. Headers with multiple values are concatenated using commas as a delimeter.
|}
 
{{Navbox content}}

Latest revision as of 11:50, 18 August 2026

The InternetUD component allows making real-world HTTP(s) requests. In order to be able to send HTTP(s) requests, make sure the current computer is connected to a WAN Router either directly via Network Cable or indirectly through Network Routers.

Security

By default, requests to local IP addresses are blocked (e.g. http://127.0.0.1). Domains are not filtered. You can allow access to local IPs by disabling the filtering in the config file.

Disabling this filtering will allow ingame computers to access webservices on the server's local machine or network. This could pose a security threat if any of those local webservices assume local traffic to be trustworthy. Be careful!

If you wish, HTTP(s) functionality can also be disabled entirely in the config file. This completely removes the ability to make any kind of HTTP(s) requests.

API

InternetUD component properties
Name Type Read/Write Description
isHttpEnabled bool READ Returns whether HTTP(s) is enabled at all. True by default, but can be changed in the server's config. Required to be true in order to use any methods.
isConnectedToWan bool READ Returns whether the current computer can reach a WAN router via the network. Required to be true in order to use any methods.
InternetUD component methods
Name Returns Description
:sendHttpRequest(url:string[, method:string[, postData:string[, headers:table]]]) HttpResponseUD Asynchronously sends an HTTP(s) request to the given URL with the given request method (e.g. GET, POST, etc.).

If the request method is set to something supporting a post body, you may provide one as a string in postData. Otherwise postData must be set to "". Headers may be provided as a table consisting of string keys and string values. The request method TRACE is not allowed for security reasons.

Handling responses

Upon making an HTTP(s) request you are provided with an HttpResponseUD object offering the following functionality.

HttpResponseUD properties
Name Type Read/Write Description
responseAvailable bool READ Returns whether this HTTP(s) request has been completed and therefore a response is available.
HttpResponseUD methods
Name Returns Description
:waitForCompletion() void Blocks until responseAvailable is true. All other functions implicitly call this prior to executing and thus show the same behaviour.
:getStatusCode() int Returns the HTTP status code of the response. Response codes in the 2xx range typically indicate success.
:getResponseBody() string Returns the response body as a string.
:getResponseBodySize() int Returns the length of the string that will be returned by :getResponseBody().
:getResponseHeaders() table Returns a table listing all response headers. Headers with multiple values are concatenated using commas as a delimeter.