Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(android): avoid connection on proxy to check the content type #3078

Merged
merged 1 commit into from Jun 9, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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