Skip to content

Commit bb8d486

Browse files
author
Eduardo Fernandes
committed
Trying to proxy the requests
1 parent 77d7561 commit bb8d486

File tree

1 file changed

+18
-6
lines changed

1 file changed

+18
-6
lines changed

src/android/com/ionicframework/cordova/webview/WebViewLocalServer.java

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,10 @@
3434
import java.util.Map;
3535
import java.util.UUID;
3636

37+
import java.net.URL;
38+
import java.net.URLConnection;
39+
40+
3741
/**
3842
* Helper class meant to be used with the android.webkit.WebView class to enable hosting assets,
3943
* resources and other data on 'virtual' http(s):// URL.
@@ -217,14 +221,22 @@ private static WebResourceResponse createWebResourceResponse(String mimeType, St
217221
*/
218222
public WebResourceResponse shouldInterceptRequest(Uri uri, WebResourceRequest request) {
219223
if (isProxySource(uri)) {
220-
String fixedUri = uri.toString().replaceFirst("http://localhost/_app_proxy_/", "");
221-
222-
InputStream responseStream = new LollipopLazyInputStream(handler, Uri.parse(fixedUri));
223-
String mimeType = getMimeType(path, responseStream);
224-
return createWebResourceResponse(mimeType, handler.getEncoding(),
225-
handler.getStatusCode(), handler.getReasonPhrase(), handler.getResponseHeaders(), responseStream);
224+
try {
225+
String fixedUri = uri.toString().replaceFirst("http://localhost/_app_proxy_/", "");
226+
URL httpsUrl = new URL(fixedUri);
227+
URLConnection connection = httpsUrl.openConnection();
228+
connection.setRequestProperty("Access-Control-Allow-Origin", "*");
229+
connection.setRequestProperty("Access-Control-Allow-Methods", "GET, POST, DELETE, PUT, OPTIONS");
230+
connection.setRequestProperty("Access-Control-Allow-Headers", "agent, user-data, Access-Control-Allow-Headers, Origin, Accept, X-Requested-With, Content-Type, Access-Control-Request-Method, Access-Control-Request-Headers");
231+
232+
return new WebResourceResponse(connection.getContentType(), connection.getContentEncoding(), connection.getInputStream());
233+
} catch (Exception e) {
234+
//an error occurred
235+
return null;
236+
}
226237
}
227238

239+
228240
PathHandler handler;
229241
synchronized (uriMatcher) {
230242
handler = (PathHandler) uriMatcher.match(uri);

0 commit comments

Comments
 (0)