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 void advance( double _dt )
if( h.wasSuccessful() ) {
// Queue the "http_success" event
final BufferedReader contents = h.getContents();
final int responseCode = h.getResponseCode();
final Object result = wrapBufferedReader( contents, responseCode );
final Object result = wrapBufferedReader( contents, h.getResponseCode(), h.getResponseHeaders() );
m_apiEnvironment.queueEvent( "http_success", new Object[] { url, result } );
} else {
// 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();
}
@ -65,7 +69,7 @@ public void advance( double _dt )
}
}
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() {
@Override
@ -75,7 +79,8 @@ public String[] getMethodNames()
"readLine",
"readAll",
"close",
"getResponseCode"
"getResponseCode",
"getResponseHeaders",
};
}
@ -131,6 +136,11 @@ public Object[] callMethod( ILuaContext context, int method, Object[] args ) thr
// getResponseCode
return new Object[] { responseCode };
}
case 4:
{
// getResponseHeaders
return new Object[] { responseHeaders };
}
default:
{
return null;

View File

@ -12,6 +12,8 @@
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Pattern;
@ -130,7 +132,16 @@ public void run()
}
// 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;
try
{
@ -192,9 +203,21 @@ public void run()
{
// We completed
m_complete = true;
m_success = true;
m_success = responseSuccess;
m_result = result.toString();
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 int getResponseCode() {
}
}
public Map<String, Map<Integer, String>> getResponseHeaders() {
synchronized (m_lock) {
return m_responseHeaders;
}
}
public boolean wasSuccessful()
{
synchronized(m_lock) {
@ -270,4 +299,5 @@ public BufferedReader getContents()
private boolean m_success;
private String m_result;
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 )
if ok then
while true do
local event, param1, param2 = os.pullEvent()
local event, param1, param2, param3 = os.pullEvent()
if event == "http_success" and param1 == _url then
return param2
elseif event == "http_failure" and param1 == _url then
return nil, param2
return nil, param2, param3
end
end
end