follow redirects in posh

This commit is contained in:
Daniel Gultsch 2016-12-08 14:21:15 +01:00
parent 8eb292d16a
commit 8d127f70d0
1 changed files with 22 additions and 7 deletions

View File

@ -458,10 +458,14 @@ public class MemorizingTrustManager {
} }
private List<String> getPoshFingerprintsFromServer(String domain) { private List<String> getPoshFingerprintsFromServer(String domain) {
return getPoshFingerprintsFromServer(domain, "https://"+domain+"/.well-known/posh/xmpp-client.json",-1,true);
}
private List<String> getPoshFingerprintsFromServer(String domain, String url, int maxTtl, boolean followUrl) {
Log.d("mtm","downloading json for "+domain+" from "+url);
try { try {
List<String> results = new ArrayList<>(); List<String> results = new ArrayList<>();
URL url = new URL("https://"+domain+"/.well-known/posh/xmpp-client.json"); HttpsURLConnection connection = (HttpsURLConnection) new URL(url).openConnection();
HttpsURLConnection connection = (HttpsURLConnection) url.openConnection();
connection.setConnectTimeout(5000); connection.setConnectTimeout(5000);
connection.setReadTimeout(5000); connection.setReadTimeout(5000);
BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream())); BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
@ -472,6 +476,22 @@ public class MemorizingTrustManager {
} }
JSONObject jsonObject = new JSONObject(builder.toString()); JSONObject jsonObject = new JSONObject(builder.toString());
in.close(); in.close();
int expires = jsonObject.getInt("expires");
if (expires <= 0) {
return new ArrayList<>();
}
if (maxTtl >= 0) {
expires = Math.min(maxTtl,expires);
}
String redirect;
try {
redirect = jsonObject.getString("url");
} catch (JSONException e) {
redirect = null;
}
if (followUrl && redirect != null && redirect.toLowerCase().startsWith("https")) {
return getPoshFingerprintsFromServer(domain, redirect, expires, false);
}
JSONArray fingerprints = jsonObject.getJSONArray("fingerprints"); JSONArray fingerprints = jsonObject.getJSONArray("fingerprints");
for(int i = 0; i < fingerprints.length(); i++) { for(int i = 0; i < fingerprints.length(); i++) {
JSONObject fingerprint = fingerprints.getJSONObject(i); JSONObject fingerprint = fingerprints.getJSONObject(i);
@ -480,11 +500,6 @@ public class MemorizingTrustManager {
results.add(sha256); results.add(sha256);
} }
} }
int expires = jsonObject.getInt("expires");
if (expires <= 0) {
return new ArrayList<>();
}
in.close();
writeFingerprintsToCache(domain, results,1000L * expires+System.currentTimeMillis()); writeFingerprintsToCache(domain, results,1000L * expires+System.currentTimeMillis());
return results; return results;
} catch (Exception e) { } catch (Exception e) {