How to return from a Rails rack call?

310 Views Asked by At

I am using Faye websocket implementation. When a websocket is detected I want to make sure the user sent a given header before I open the connection, or else I want to return an error. I tried returning error 401 using [] but I keep oberving that the code continues execution and the websocket is created anyway. I managed to prevent it by adding return after it, but I am not sure this is the right way. On the client side I am implementing it in python using ws4py and when I return I get an exception raised indicating it received a 302 code, not the 401 I was expecting to send.

My ruby code (without the websocket events code) follows:

  App = lambda do |env|
    if Faye::WebSocket.websocket?(env)
      unless env['HTTP_MY_HEADER']
        [401, {'Content-Type' => 'application/json'}, '{"message": "I was expecting a header here"}']
        #return (??)
      end

      ws = Faye::WebSocket.new(env)
      # Rest of websocket call handling
    else
      # Normal HTTP request
      [200, {'Content-Type' => 'text/plain'}, ['Hello']]
    end
  end
0

There are 0 best solutions below