Skip to content

Commit

Permalink
fix(android-livereload-localhost):
Browse files Browse the repository at this point in the history
* Use request proxy handler when livereload and appUrl localhost:8100
* Reconnect when connection has not been established
  • Loading branch information
luisvargastije committed Jul 19, 2019
1 parent 76f6624 commit a32fa60
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 10 deletions.
3 changes: 3 additions & 0 deletions android/capacitor/src/main/java/com/getcapacitor/Bridge.java
Original file line number Diff line number Diff line change
Expand Up @@ -821,6 +821,9 @@ public void run() {
});
}

public String getAppUrl() {
return appUrl;
}

public String getLocalUrl() {
return localUrl;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -173,10 +173,11 @@ public WebResourceResponse shouldInterceptRequest(WebResourceRequest request) {
return null;
}

if (isLocalFile(loadingUrl) || loadingUrl.toString().startsWith(bridge.getLocalUrl())) {
if (isLocalFile(loadingUrl) || !bridge.getAppUrl().contains("localhost:8100") && loadingUrl.toString().startsWith(bridge.getLocalUrl())) {
Log.d(LogUtils.getCoreTag(), "Handling local request: " + request.getUrl().toString());
return handleLocalRequest(request, handler);
} else {
Log.d(LogUtils.getCoreTag(), "Handling proxy request: " + request.getUrl().toString());
return handleProxyRequest(request, handler);
}
}
Expand Down Expand Up @@ -294,22 +295,32 @@ private WebResourceResponse handleProxyRequest(WebResourceRequest request, PathH
String path = request.getUrl().getPath();
URL url = new URL(request.getUrl().toString());
Map<String, String> headers = request.getRequestHeaders();
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
for (Map.Entry<String, String> header : headers.entrySet()) {
conn.setRequestProperty(header.getKey(), header.getValue());
}
conn.setRequestMethod(method);
conn.setReadTimeout(30 * 1000);
conn.setConnectTimeout(30 * 1000);

HttpURLConnection conn;
boolean isContentTypeNull;
do {
conn = (HttpURLConnection) url.openConnection();
for (Map.Entry<String, String> header : headers.entrySet()) {
conn.setRequestProperty(header.getKey(), header.getValue());
}
conn.setRequestMethod(method);
conn.setReadTimeout(30 * 1000);
conn.setConnectTimeout(30 * 1000);

isContentTypeNull = conn.getContentType() == null;
if (isContentTypeNull) {
conn.disconnect();
Thread.sleep(1000);
}
} while (isContentTypeNull);

if (conn.getContentType().contains("text/html")) {
InputStream responseStream = conn.getInputStream();
responseStream = jsInjector.getInjectedStream(responseStream);
bridge.reset();
return new WebResourceResponse("text/html", handler.getEncoding(),
handler.getStatusCode(), handler.getReasonPhrase(), handler.getResponseHeaders(), responseStream);
handler.getStatusCode(), handler.getReasonPhrase(), handler.getResponseHeaders(), responseStream);
}

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

0 comments on commit a32fa60

Please sign in to comment.