Perform HTTP request as late as possible

This commit is contained in:
Alexey Andreev 2018-01-14 22:51:02 +03:00
parent bbf9faee5b
commit ec3724b2bc
2 changed files with 26 additions and 4 deletions

View File

@ -59,7 +59,7 @@ after_success: >
export TEAVM_FTP_HOST export TEAVM_FTP_HOST
export TEAVM_FTP_LOGIN export TEAVM_FTP_LOGIN
export TEAVM_FTP_PASSWORD export TEAVM_FTP_PASSWORD
mvn deploy -Dteavm.build.all=false -DskipTests --settings travis-settings.xml -P deploy-to-bintray && \ mvn deploy -Dteavm.build.all=false -DskipTests --settings travis-settings.xml -P deploy-to-bintray -P with-idea && \
./upload-files.sh ./upload-files.sh
fi fi
after_script: after_script:

View File

@ -41,6 +41,7 @@ public class TXHRURLConnection extends THttpURLConnection {
private String[] responseHeaderKeys; private String[] responseHeaderKeys;
private String[] responseHeaderValues; private String[] responseHeaderValues;
private Map<String, List<String>> headerFields = new HashMap<>(); private Map<String, List<String>> headerFields = new HashMap<>();
private boolean requestPerformed;
public TXHRURLConnection(TURL url) { public TXHRURLConnection(TURL url) {
super(url); super(url);
@ -72,11 +73,14 @@ public class TXHRURLConnection extends THttpURLConnection {
} }
} }
xhr.setResponseType("arraybuffer"); xhr.setResponseType("arraybuffer");
try {
performRequest();
} finally {
connected = true; connected = true;
} }
private void performRequestIfNecessary() {
if (!requestPerformed) {
requestPerformed = true;
performRequest();
}
} }
@Async @Async
@ -166,6 +170,7 @@ public class TXHRURLConnection extends THttpURLConnection {
@Override @Override
public String getHeaderFieldKey(int posn) { public String getHeaderFieldKey(int posn) {
performRequestIfNecessary();
if (responseHeaderKeys == null || posn >= responseHeaderKeys.length) { if (responseHeaderKeys == null || posn >= responseHeaderKeys.length) {
return null; return null;
} }
@ -174,6 +179,7 @@ public class TXHRURLConnection extends THttpURLConnection {
@Override @Override
public String getHeaderField(int pos) { public String getHeaderField(int pos) {
performRequestIfNecessary();
if (responseHeaderValues == null || pos >= responseHeaderValues.length) { if (responseHeaderValues == null || pos >= responseHeaderValues.length) {
return null; return null;
} }
@ -182,6 +188,7 @@ public class TXHRURLConnection extends THttpURLConnection {
@Override @Override
public String getHeaderField(String key) { public String getHeaderField(String key) {
performRequestIfNecessary();
return responseHeaders != null ? responseHeaders.get(key.toLowerCase()) : null; return responseHeaders != null ? responseHeaders.get(key.toLowerCase()) : null;
} }
@ -193,6 +200,7 @@ public class TXHRURLConnection extends THttpURLConnection {
@Override @Override
public InputStream getInputStream() throws IOException { public InputStream getInputStream() throws IOException {
connect(); connect();
performRequestIfNecessary();
int responseGroup = responseCode / 100; int responseGroup = responseCode / 100;
if (responseGroup == 4 || responseGroup == 5) { if (responseGroup == 4 || responseGroup == 5) {
@ -203,6 +211,20 @@ public class TXHRURLConnection extends THttpURLConnection {
return inputStream; return inputStream;
} }
@Override
public int getResponseCode() throws IOException {
connect();
performRequestIfNecessary();
return super.getResponseCode();
}
@Override
public String getResponseMessage() throws IOException {
connect();
performRequestIfNecessary();
return super.getResponseMessage();
}
@Override @Override
public InputStream getErrorStream() { public InputStream getErrorStream() {
return errorStream; return errorStream;