couchbaselabs / Couchbase-Lite-PhoneGap-Plugin

Plugin to install Couchbase Lite in your PhoneGap app on iOS or Android
183 stars 67 forks source link

Cross-origin access errors with GapReload and Phonegap Developer App #38

Closed AdamDz closed 7 years ago

AdamDz commented 9 years ago

Couchbase Lite does not work with rapid development tools that implement live app reload and eliminate the compile and install step (i.e. calling cordova run every JS/HTML change). These tools include: GapReload and Adobe's PhoneGap Developer App. They work by redirecting the webview from a file:// location to a server on the developer's machine started by cordova serve. When we try to connect to Couchbase Lite in such setup, an error is thrown:

XMLHttpRequest cannot load http://lite.couchbase./temp. Origin http://192.168.1.104:8000 is not allowed by Access-Control-Allow-Origin.

The cross-resource origin policy is not applied when the embedded web application is served from the standard location using file://, but it is after the redirection to http://

A solution would be to add a header to HTTP responses returned by Couchbase Lite:

Access-Control-Allow-Origin: *
snej commented 9 years ago

CBL has support for adding custom HTTP headers to return; see https://github.com/couchbase/couchbase-lite-ios/pull/203 . The PhoneGap plugin just needs to add this line:

[[CBLManager sharedInstance].customHTTPHeaders setObject:@"*" forKey:@"Access-Control-Allow-Origin"];
AdamDz commented 9 years ago

The ideal solution would be to add support for setting custom HTTP headers using the REST API. The PhoneGap plugin would then have the same feature set.

AdamDz commented 9 years ago

I need to admit that your solution works well and doesn't require to recompile CBL binaries. The mentioned line can be added in src/ios/CBLine.m and it works. Thanks.

stenit commented 9 years ago

I had the same issue on Android. For debugging purpose, I wanted to access my cblite on the mobile device from my local web application (using "ionic serve"). You need to do two things: 1) Tunnel the cblite port from your local machine to your phone: "adb forward tcp:5984 tcp:5984" 2) Recompile the couchbase-light-java-listener JAR. Add the following lines to the LiteServlet.java file:

@Override
public void service(HttpServletRequest request, final HttpServletResponse response)
        throws ServletException, IOException {

    // Handle OPTIONS Request in order to respond to CORS
    response.setHeader("Access-Control-Allow-Origin", "*");
    response.setHeader("Access-Control-Allow-Methods", "GET, PUT, POST, DELETE");
    response.setHeader("Access-Control-Allow-Headers", "content-type, accept");
    if("OPTIONS".equals(request.getMethod())){
        Log.v(Log.TAG_LISTENER, "Handle OPTIONS Request in order to respond to CORS");
        response.setStatus(200);
        return;
    } 
  /* REST OF THE service() METHOD */
  }

Thats it.

See also https://groups.google.com/forum/#!msg/mobile-couchbase/a80GfiBwkuI/MoBtQNT8zPUJ where I started the discussion month's ago.

jamesnocentini commented 7 years ago

we now have a version of LiteServ with CORS available for development purposes https://developer.couchbase.com/documentation/mobile/1.4/installation/phonegap/index.html#development-environment. Closing this ticket.

swavans commented 5 years ago

I'm still having this issue, is there a way to fix this?