mirror of
https://github.com/SquidDev-CC/CC-Tweaked
synced 2025-01-11 18:00:29 +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:
parent
ec7a251c09
commit
29952d5b4f
@ -52,12 +52,16 @@ public class HTTPAPI implements ILuaAPI
|
||||
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 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() {
|
||||
@Override
|
||||
@ -75,7 +79,8 @@ public class HTTPAPI implements ILuaAPI
|
||||
"readLine",
|
||||
"readAll",
|
||||
"close",
|
||||
"getResponseCode"
|
||||
"getResponseCode",
|
||||
"getResponseHeaders",
|
||||
};
|
||||
}
|
||||
|
||||
@ -131,6 +136,11 @@ public class HTTPAPI implements ILuaAPI
|
||||
// getResponseCode
|
||||
return new Object[] { responseCode };
|
||||
}
|
||||
case 4:
|
||||
{
|
||||
// getResponseHeaders
|
||||
return new Object[] { responseHeaders };
|
||||
}
|
||||
default:
|
||||
{
|
||||
return null;
|
||||
|
@ -12,6 +12,8 @@ import java.io.*;
|
||||
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 class HTTPRequest
|
||||
}
|
||||
|
||||
// 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 class HTTPRequest
|
||||
{
|
||||
// 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 class HTTPRequest
|
||||
}
|
||||
}
|
||||
|
||||
public Map<String, Map<Integer, String>> getResponseHeaders() {
|
||||
synchronized (m_lock) {
|
||||
return m_responseHeaders;
|
||||
}
|
||||
}
|
||||
|
||||
public boolean wasSuccessful()
|
||||
{
|
||||
synchronized(m_lock) {
|
||||
@ -270,4 +299,5 @@ public class HTTPRequest
|
||||
private boolean m_success;
|
||||
private String m_result;
|
||||
private int m_responseCode;
|
||||
private Map<String, Map<Integer, String>> m_responseHeaders;
|
||||
}
|
||||
|
@ -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
|
||||
|
Loading…
Reference in New Issue
Block a user