1
0
mirror of https://github.com/SquidDev-CC/CC-Tweaked synced 2025-01-27 01:14:46 +00:00

Various extensions to the HTTP API

- A response is returned on the event of a HTTP error (such as 404).
 - Responses include the response headers.
This commit is contained in:
SquidDev 2017-05-01 17:28:20 +01:00
parent ec7a251c09
commit 29952d5b4f
3 changed files with 49 additions and 9 deletions

View File

@ -52,12 +52,16 @@ public class HTTPAPI implements ILuaAPI
if( h.wasSuccessful() ) { if( h.wasSuccessful() ) {
// Queue the "http_success" event // Queue the "http_success" event
final BufferedReader contents = h.getContents(); final BufferedReader contents = h.getContents();
final int responseCode = h.getResponseCode(); final Object result = wrapBufferedReader( contents, h.getResponseCode(), h.getResponseHeaders() );
final Object result = wrapBufferedReader( contents, responseCode );
m_apiEnvironment.queueEvent( "http_success", new Object[] { url, result } ); m_apiEnvironment.queueEvent( "http_success", new Object[] { url, result } );
} else { } else {
// Queue the "http_failure" event // Queue the "http_failure" event
m_apiEnvironment.queueEvent( "http_failure", new Object[] { url, "Could not connect" } ); BufferedReader contents = h.getContents();
Object result = null;
if( contents != null ) {
result = wrapBufferedReader( contents, h.getResponseCode(), h.getResponseHeaders() );
}
m_apiEnvironment.queueEvent( "http_failure", new Object[]{ url, "Could not connect", result } );
} }
it.remove(); it.remove();
} }
@ -65,7 +69,7 @@ public class HTTPAPI implements ILuaAPI
} }
} }
private static ILuaObject wrapBufferedReader( final BufferedReader reader, final int responseCode ) private static ILuaObject wrapBufferedReader( final BufferedReader reader, final int responseCode, final Map<String, Map<Integer, String>> responseHeaders )
{ {
return new ILuaObject() { return new ILuaObject() {
@Override @Override
@ -75,7 +79,8 @@ public class HTTPAPI implements ILuaAPI
"readLine", "readLine",
"readAll", "readAll",
"close", "close",
"getResponseCode" "getResponseCode",
"getResponseHeaders",
}; };
} }
@ -131,6 +136,11 @@ public class HTTPAPI implements ILuaAPI
// getResponseCode // getResponseCode
return new Object[] { responseCode }; return new Object[] { responseCode };
} }
case 4:
{
// getResponseHeaders
return new Object[] { responseHeaders };
}
default: default:
{ {
return null; return null;

View File

@ -12,6 +12,8 @@ import java.io.*;
import java.net.HttpURLConnection; import java.net.HttpURLConnection;
import java.net.MalformedURLException; import java.net.MalformedURLException;
import java.net.URL; import java.net.URL;
import java.util.HashMap;
import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.regex.Pattern; import java.util.regex.Pattern;
@ -130,7 +132,16 @@ public class HTTPRequest
} }
// Read response // Read response
InputStream is = connection.getInputStream(); InputStream is;
int code = connection.getResponseCode();
boolean responseSuccess;
if (code >= 200 && code < 400) {
is = connection.getInputStream();
responseSuccess = true;
} else {
is = connection.getErrorStream();
responseSuccess = false;
}
InputStreamReader isr; InputStreamReader isr;
try try
{ {
@ -192,9 +203,21 @@ public class HTTPRequest
{ {
// We completed // We completed
m_complete = true; m_complete = true;
m_success = true; m_success = responseSuccess;
m_result = result.toString(); m_result = result.toString();
m_responseCode = connection.getResponseCode(); m_responseCode = connection.getResponseCode();
Map<String, Map<Integer, String>> headers = m_responseHeaders = new HashMap<String, Map<Integer, String>>();
for (Map.Entry<String, List<String>> header : connection.getHeaderFields().entrySet()) {
Map<Integer, String> values = new HashMap<Integer, String>();
int i = 0;
for (String value : header.getValue()) {
values.put(++i, value);
}
headers.put(header.getKey(), values);
}
} }
} }
@ -241,6 +264,12 @@ public class HTTPRequest
} }
} }
public Map<String, Map<Integer, String>> getResponseHeaders() {
synchronized (m_lock) {
return m_responseHeaders;
}
}
public boolean wasSuccessful() public boolean wasSuccessful()
{ {
synchronized(m_lock) { synchronized(m_lock) {
@ -270,4 +299,5 @@ public class HTTPRequest
private boolean m_success; private boolean m_success;
private String m_result; private String m_result;
private int m_responseCode; private int m_responseCode;
private Map<String, Map<Integer, String>> m_responseHeaders;
} }

View File

@ -643,11 +643,11 @@ if http then
local ok, err = nativeHTTPRequest( _url, _post, _headers ) local ok, err = nativeHTTPRequest( _url, _post, _headers )
if ok then if ok then
while true do while true do
local event, param1, param2 = os.pullEvent() local event, param1, param2, param3 = os.pullEvent()
if event == "http_success" and param1 == _url then if event == "http_success" and param1 == _url then
return param2 return param2
elseif event == "http_failure" and param1 == _url then elseif event == "http_failure" and param1 == _url then
return nil, param2 return nil, param2, param3
end end
end end
end end