delagoya (owner)

Revisions

gist: 33471 Download_button fork
public
Description:
Setting session cookies from flash in Merb
Public Clone URL: git://gist.github.com/33471.git
Embed All Files: show embed
README #
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
This is Merb middleware to set a session cookie when the request comes from flash.
 
You must first require the module, then invoke it's use in the call chain in either rack.rb or config.ru.
 
Examples below. Note that SWFUPload by default adds a lot of post parameters with non-standard
ruby variable names, so merb-action-args will choke a bit on that unless you customize SWFupload
or just not use action-args.
 
Also note that the session information must be set as a GET parameters, e.g. in the URL request like so:
 
SWFUpload.onload = function () {
      var settings = {
        flash_url : "/flash/swfupload.swf",
        file_post_name: "attachment",
        upload_url: "#{url(:upload_attachments,@attachment)}?_session_id=#{cookies[:_session_id]}",
...
config.ru #
1
2
3
4
5
6
7
## Phusion passenger rack up file, as an alternate to the Merb's rack.rb
## ... other code
 
Merb::BootLoader.run
use Merb::Rack::SwfSetSessionCookie, Merb::Config[:session_id_key]
run Merb::Rack::Application.new
 
rack.rb #
1
2
3
4
5
6
7
## Merb rack.rb file, set the use of this middleware. Make sure it has been required already
 
## ... other code ...
 
use Merb::Rack::SwfSetSessionCookie,Merb::Config[:session_id_key]
 
run Merb::Rack::Application.new
swf_set_session_cookie.rb #
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
module Merb
  module Rack
    class SwfSetSessionCookie < Merb::Rack::Middleware
      # :api: private
      def initialize(app, session_key = '_session_id')
        super(app)
        @session_key = session_key
      end
      # :api: plugin
      def call(env)
        if env["HTTP_USER_AGENT"] =~ /Adobe Flash/
          prms = Merb::Parse.query(env['QUERY_STRING'])
          env['HTTP_COOKIE'] = [@session_key,prms[@session_key]].join('=').freeze
        end
        @app.call(env)
      end
    end
  end
end