2828import java .io .InputStream ;
2929import java .net .HttpURLConnection ;
3030import java .net .SocketTimeoutException ;
31- import java .net .URL ;
32- import java .net .URLConnection ;
3331import java .util .HashMap ;
3432import java .util .Map ;
3533import java .util .UUID ;
34+ import java .util .List ;
3635
3736import java .net .URL ;
3837import java .net .URLConnection ;
3938
40-
4139/**
4240 * Helper class meant to be used with the android.webkit.WebView class to enable hosting assets,
4341 * resources and other data on 'virtual' http(s):// URL.
@@ -57,7 +55,7 @@ public class WebViewLocalServer {
5755 public final static String httpsScheme = "https" ;
5856 public final static String fileStart = "/_app_file_" ;
5957 public final static String contentStart = "/_app_content_" ;
60- public final static String proxyStart = "/_app_proxy_ " ;
58+ public final static String proxyStart = "/_local_proxy_ " ;
6159
6260 private final UriMatcher uriMatcher ;
6361 private final AndroidProtocolHandler protocolHandler ;
@@ -220,23 +218,6 @@ private static WebResourceResponse createWebResourceResponse(String mimeType, St
220218 * @return a response if the request URL had a matching handler, null if no handler was found.
221219 */
222220 public WebResourceResponse shouldInterceptRequest (Uri uri , WebResourceRequest request ) {
223- if (isProxySource (uri )) {
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- }
237- }
238-
239-
240221 PathHandler handler ;
241222 synchronized (uriMatcher ) {
242223 handler = (PathHandler ) uriMatcher .match (uri );
@@ -248,13 +229,18 @@ public WebResourceResponse shouldInterceptRequest(Uri uri, WebResourceRequest re
248229
249230 if (isLocalFile (uri ) || uri .getAuthority ().equals (this .authority )) {
250231 Log .d ("SERVER" , "Handling local request: " + uri .toString ());
251- return handleLocalRequest (uri , handler , request );
232+
233+ if (isLocalProxySource (uri )) {
234+ return handleLocalProxyRequest (uri , request );
235+ } else {
236+ return handleLocalRequest (uri , handler , request );
237+ }
252238 } else {
253239 return handleProxyRequest (uri , handler );
254240 }
255241 }
256242
257- private boolean isProxySource (Uri uri ) {
243+ private boolean isLocalProxySource (Uri uri ) {
258244 String path = uri .getPath ();
259245 return path .startsWith (proxyStart );
260246 }
@@ -267,6 +253,38 @@ private boolean isLocalFile(Uri uri) {
267253 return false ;
268254 }
269255
256+ private WebResourceResponse handleLocalProxyRequest (Uri uri , WebResourceRequest request ) {
257+ String fixedUri = uri .toString ().replaceFirst ("http://localhost/_local_proxy_/" , "" ); // Fix the url by removing the proxy schema
258+
259+ try {
260+ URL httpsUrl = new URL (fixedUri );
261+ URLConnection connection = httpsUrl .openConnection ();
262+ HttpURLConnection httpConnection = (HttpURLConnection )connection ;
263+
264+ httpConnection .setRequestMethod (request .getMethod ());
265+ for (Map .Entry <String , String > entry : request .getRequestHeaders ().entrySet ()) {
266+ httpConnection .setRequestProperty (entry .getKey (), entry .getValue ());
267+ }
268+
269+ httpConnection .connect ();
270+
271+ // Pass them trough (Convert String,List<String> to String,String)
272+ Map <String , String > headers = new HashMap <String , String >();
273+ for (Map .Entry <String , List <String >> entry : connection .getHeaderFields ().entrySet ()) {
274+ headers .put (entry .getKey (), entry .getValue ().get (0 ));
275+ }
276+
277+ // Bypass CORS
278+ headers .put ("Access-Control-Allow-Origin" , "*" );
279+ headers .put ("Access-Control-Allow-Methods" , "GET, POST, DELETE, PUT, OPTIONS" );
280+ headers .put ("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" );
281+
282+ return new WebResourceResponse (connection .getContentType (), connection .getContentEncoding (), httpConnection .getResponseCode (), httpConnection .getResponseMessage (), headers , connection .getInputStream ());
283+ } catch (Exception e ) {
284+ //an error occurred
285+ return null ;
286+ }
287+ }
270288
271289 private WebResourceResponse handleLocalRequest (Uri uri , PathHandler handler , WebResourceRequest request ) {
272290 String path = uri .getPath ();
0 commit comments