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_LOGIN
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
fi
after_script:

View File

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