Skip to content

Commit

Permalink
fix(android): don't proxy requests if no jsInjector (#7199)
Browse files Browse the repository at this point in the history
  • Loading branch information
jcesarmobile authored Jan 22, 2024
1 parent ccc8858 commit 7dc5585
Showing 1 changed file with 45 additions and 44 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -334,57 +334,58 @@ private WebResourceResponse handleLocalRequest(WebResourceRequest request, PathH
* @return
*/
private WebResourceResponse handleProxyRequest(WebResourceRequest request, PathHandler handler) {
final String method = request.getMethod();
if (method.equals("GET")) {
try {
String url = request.getUrl().toString();
Map<String, String> headers = request.getRequestHeaders();
boolean isHtmlText = false;
for (Map.Entry<String, String> header : headers.entrySet()) {
if (header.getKey().equalsIgnoreCase("Accept") && header.getValue().toLowerCase().contains("text/html")) {
isHtmlText = true;
break;
}
}
if (isHtmlText) {
HttpURLConnection conn = (HttpURLConnection) new URL(url).openConnection();
if (jsInjector != null) {
final String method = request.getMethod();
if (method.equals("GET")) {
try {
String url = request.getUrl().toString();
Map<String, String> headers = request.getRequestHeaders();
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);
if (request.getUrl().getUserInfo() != null) {
byte[] userInfoBytes = request.getUrl().getUserInfo().getBytes(StandardCharsets.UTF_8);
String base64 = Base64.encodeToString(userInfoBytes, Base64.NO_WRAP);
conn.setRequestProperty("Authorization", "Basic " + base64);
if (header.getKey().equalsIgnoreCase("Accept") && header.getValue().toLowerCase().contains("text/html")) {
isHtmlText = true;
break;
}
}
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);
if (request.getUrl().getUserInfo() != null) {
byte[] userInfoBytes = request.getUrl().getUserInfo().getBytes(StandardCharsets.UTF_8);
String base64 = Base64.encodeToString(userInfoBytes, Base64.NO_WRAP);
conn.setRequestProperty("Authorization", "Basic " + base64);
}

List<String> cookies = conn.getHeaderFields().get("Set-Cookie");
if (cookies != null) {
for (String cookie : cookies) {
CookieManager.getInstance().setCookie(url, cookie);
List<String> cookies = conn.getHeaderFields().get("Set-Cookie");
if (cookies != null) {
for (String cookie : cookies) {
CookieManager.getInstance().setCookie(url, cookie);
}
}
}
InputStream responseStream = conn.getInputStream();
if (jsInjector != null) {
InputStream responseStream = conn.getInputStream();
responseStream = jsInjector.getInjectedStream(responseStream);

return new WebResourceResponse(
"text/html",
handler.getEncoding(),
handler.getStatusCode(),
handler.getReasonPhrase(),
handler.getResponseHeaders(),
responseStream
);
}
return new WebResourceResponse(
"text/html",
handler.getEncoding(),
handler.getStatusCode(),
handler.getReasonPhrase(),
handler.getResponseHeaders(),
responseStream
);
} catch (Exception ex) {
bridge.handleAppUrlLoadError(ex);
}
} catch (Exception ex) {
bridge.handleAppUrlLoadError(ex);
}
}
return null;
Expand Down

0 comments on commit 7dc5585

Please sign in to comment.