Re: [stardog-users] CORS support

8 views
Skip to first unread message

Mike Grove

unread,
Feb 20, 2013, 2:05:41 PM2/20/13
to stardog
Did you have an issue using stardog.js [1]?

Cheers,

Mike

[1] https://github.com/clarkparsia/stardog.js

On Wed, Feb 20, 2013 at 1:58 PM, Alex Stolz <stalso...@gmail.com> wrote:
Hi,

I downloaded Stardog 1.1.3 and was wondering how to enable CORS[1] in order to be able to query from Javascript? Is there a configuration option that I may have overlooked?

Thanks in advance.

Best

--
-- --
You received this message because you are subscribed to the C&P "Stardog" group.
To post to this group, send email to sta...@clarkparsia.com
To unsubscribe from this group, send email to
stardog+u...@clarkparsia.com
For more options, visit this group at
http://groups.google.com/a/clarkparsia.com/group/stardog?hl=en
 
 

Alex Stolz

unread,
Feb 20, 2013, 2:23:20 PM2/20/13
to sta...@clarkparsia.com
I tried with my own JS code that I used to query a different, CORS-aware endpoint. Now, however, I'd rather give a try to stardog.js. Thanks!

Cheers
Alex

Edgar Rodriguez

unread,
Feb 20, 2013, 2:59:16 PM2/20/13
to sta...@clarkparsia.com
Hi Alex,

The best way to try stardog with JavaScript is stardog.js running on Node.js as pointed out. 
If however you want to try to query a stardog SPARQL enpoint in JS directly from the browser, you could create a "proxy" servlet to redirect the request (copying all the headers, etc) and getting the results back, using a CORS filter [1], which eliminates the need to have a CORS-aware endpoint.

Cheers,
Edgar

Alex Stolz

unread,
Feb 21, 2013, 5:31:36 AM2/21/13
to sta...@clarkparsia.com
Hi Edgar,

thanks a lot for your suggestion. I'll keep CORS filter in mind for future projects. However, I got it working now, essentially by replacing the JS request to an endpoint by a request to a python script that acts as a proxy and serves the necessary CORS http header. This seems to fulfill my requirements.

Best
Alex

László Török

unread,
Feb 27, 2013, 6:51:35 AM2/27/13
to sta...@clarkparsia.com
Hi Mike,

I just submitted a little patch to stardog.js as it's browser support is currently broken.

However, the Stardog server seems not to provide the correct CORS header, as I'm getting the following error:

XMLHttpRequest cannot load http://localhost:5822/d1/query?query=select+distinct+*+where+%7B+%3Fs+%3Fp+%3Fo+%7D&limit=5. Origin http://sparql-demo is not allowed by Access-Control-Allow-Origin. 

I setup a proxy as a workaround, but it would be nice to get regular CORS support, as I believe it is a very cheap change with great leverage.

Regards,

Laszlo

2013/2/21 Alex Stolz <stalso...@gmail.com>



--
László Török

László Török

unread,
Feb 27, 2013, 2:10:45 PM2/27/13
to sta...@clarkparsia.com
Finally, I managed to fix this. For those of you, who prefer to use Apache as a proxy (until this gets fixed in Stardog directly) here is how you can set up a CORS-aware proxy:

SetEnvIf Origin (.*) allow-origin-host=$1
Header set Access-Control-Allow-Origin "%{allow-origin-host}e"
# this workaround is necessary if the the endpoint is password protected
# if withCredential is set on httpXmlRequest (in the browser) it will not accept Access-Control-Allow-Origin set to wildcard (*)
# this hack will set Access-Control-Allow-Origin dynamically to whatever the Origin request header's value is
Header set Access-Control-Allow-Headers "accept, origin, sd-connection-string"
Header set Access-Control-Allow-Methods "GET"
Header set Access-Control-Allow-Credentials "true"

ProxyRequests Off
ProxyPassReverse / http://localhost:5822/

then you can use the patched stardog.js library in-browser from an arbitrary domain.

Best,

Laszlo

2013/2/27 László Török <ltor...@gmail.com>



--
László Török

László Török

unread,
Mar 1, 2013, 11:21:53 AM3/1/13
to sta...@clarkparsia.com
Hi,

my previous assessment of the CORS issue turned out to be slightly premature.

The issue can be summarized as follows:

ISSUE: Stardog (up to v1.1.3) does not properly support CORS[1] interaction.
The issue is two-fold:
1) Stardog tries to authenticated the OPTIONS "pre-flight" request.
2) Stardog doesn't return CORS headers, hence can only be accessed
by browser web apps running in a different domain via a proxy.

Initially, I suspected the browser's to be not compliant with the CORS spec, however, as it turned out, the issue can be resolved by a small change in Stardog.

I created a node.js reference implementation to demonstrate, how Stardog SHOULD behave in a CORS scenario.
I tested it with Firefox and it seems to work fine. I've included more detailed analysis of the issue as well as references to relevant discussions.

I've posted a patch to stardog.js with detailed comments. This will get things sorted on Safari and Chrome, if you use a proxy to inject the appropriate CORS headers.

As soon as 1) gets fixed, Firefox (and likely IE) will work too, but you still need the proxy to supply the CORS headers.

Fixing 2) will make the proxy redundant.

To be fair, I understand that this issue is probably low priority, however, I'd think there are people who would appreciate to have at least 1) fixed.

Thanks,

Laszlo
--
László Török
Reply all
Reply to author
Forward
0 new messages