rack-cors icon indicating copy to clipboard operation
rack-cors copied to clipboard

Support for asynchonous request handling

Open ept opened this issue 13 years ago • 8 comments

Hi Calvin, thanks for rack-cors, loving it!

We're using asynchonous request processing in our app — you throw :async to tell the web server (e.g. thin) that the response will be sent later; then the web server can serve other requests, and once the response is ready, it is sent out via env['async.callback'].call([status, headers, body]).

I've extended rack-cors to support adding the CORS response headers to asynchonous responses as well. This is done by catching and re-throwing the throw :async, and by wrapping Rack's async callback in a proxy which adds the appropriate headers.

Feel free to use this change if you want :)

Martin

ept avatar Aug 10 '11 01:08 ept

Hey folks, I've forked cyu's master branch and merged in this request. I then modified it to add support for Goliath (and any other server, I believe, that returns a status of -1 instead of throwing :async. My branch is here: https://github.com/sujal/rack-cors/tree/async_support.

Let me know what you think. I don't have a handy test case for the "throw" case, so if you could make sure I didn't break anything, that would help.

I do need think of a way to write a test for this. Not sure yet how to tackle that.

sujal avatar Nov 26 '11 08:11 sujal

Replacing env['async.callback'] should happen before calling original app inside of catch(:async){} cause application could save it to local variable before throwing :async

funny-falcon avatar Apr 04 '12 12:04 funny-falcon

Would love to see @sujal's fork make its way into rack-cors. :)

karlfreeman avatar Oct 02 '12 09:10 karlfreeman

Why will you not merge this?

quezacoatl avatar Oct 31 '13 12:10 quezacoatl

Actually it works to use async-rack instead:

require 'async-rack'

module AsyncRack
  class Cors < AsyncCallback(:Cors)
    include AsyncRack::AsyncCallback::SimpleWrapper
  end
end

quezacoatl avatar Oct 31 '13 12:10 quezacoatl

I haven't had the opportunity to understand async handling in Rack works. I'll try to block off some time to look into this.

cyu avatar Oct 31 '13 12:10 cyu

Any update on this please? Will this be merged?

demisx avatar Feb 14 '14 18:02 demisx

:+1:

zabolotnov87 avatar Feb 17 '15 14:02 zabolotnov87