Skip to content

Commit

Permalink
chore(android): avoid connection on proxy to check the content type (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
jcesarmobile committed Jun 9, 2020
1 parent 766a61d commit 0d2894c
Showing 1 changed file with 21 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -290,33 +290,37 @@ private WebResourceResponse handleProxyRequest(WebResourceRequest request, PathH
final String method = request.getMethod();
if (method.equals("GET")) {
try {
String path = request.getUrl().getPath();
String url = request.getUrl().toString();
Map<String, String> headers = request.getRequestHeaders();
HttpURLConnection conn = (HttpURLConnection) new URL(url).openConnection();
boolean isHtmlText = false;
for (Map.Entry<String, String> header : headers.entrySet()) {
conn.setRequestProperty(header.getKey(), header.getValue());
}
String getCookie = CookieManager.getInstance().getCookie(url);
if (getCookie != null) {
conn.setRequestProperty("Cookie", getCookie);
}
conn.setRequestMethod(method);
conn.setReadTimeout(30 * 1000);
conn.setConnectTimeout(30 * 1000);
String cookie = conn.getHeaderField("Set-Cookie");
if (cookie != null) {
CookieManager.getInstance().setCookie(url, cookie);
if (header.getKey().equalsIgnoreCase("Accept") && header.getValue().toLowerCase().contains("text/html")) {
isHtmlText = true;
break;
}
}

if (conn.getContentType().contains("text/html")) {
if (isHtmlText) {
HttpURLConnection conn = (HttpURLConnection) new URL(url).openConnection();
for (Map.Entry<String, String> header : headers.entrySet()) {
conn.setRequestProperty(header.getKey(), header.getValue());
}
String getCookie = CookieManager.getInstance().getCookie(url);
if (getCookie != null) {
conn.setRequestProperty("Cookie", getCookie);
}
conn.setRequestMethod(method);
conn.setReadTimeout(30 * 1000);
conn.setConnectTimeout(30 * 1000);
String cookie = conn.getHeaderField("Set-Cookie");
if (cookie != null) {
CookieManager.getInstance().setCookie(url, cookie);
}
InputStream responseStream = conn.getInputStream();
responseStream = jsInjector.getInjectedStream(responseStream);
bridge.reset();
return new WebResourceResponse("text/html", handler.getEncoding(),
handler.getStatusCode(), handler.getReasonPhrase(), handler.getResponseHeaders(), responseStream);
}

} catch (SocketTimeoutException ex) {
bridge.handleAppUrlLoadError(ex);
} catch (Exception ex) {
Expand Down

0 comments on commit 0d2894c

Please sign in to comment.