我在EventMachine.run
循環內運行Sinatra
應用程序,在我的ws.onopen
方法中,我希望檢查handshake
標頭的cookie以確保傳入的請求來自我的webapp的註冊用戶。如何解碼來自websocket連接握手頭的cookie? (Ruby)
我Sinatra
應用程序包括以下內容:
use Rack::Session::Cookie, :key => COOKIE_KEY,
:path => '/',
:expire_after => 2592000, #30 days
:secret => COOKIE_SECRET
和我ws.onopen
方法看起來像這樣(修剪)
ws.onopen { |handshake|
cookie, bakesale = handshake.headers['Cookie'].split('=')
rack_cookie = Rack::Session::Cookie.new(MyApp, {
:key => COOKIE_KEY,
:path => '/',
:expire_after => 2592000, #30 days
:secret => COOKIE_SECRET
})
decoded = rack_cookie.coder.decode(bakesale)
puts "decoded: #{decoded}"
}
的cookie
值我COOKIE_KEY
就好了,但是的decoded
值相匹配是nil
我應該如何ecode傳入的cookie數據?
- 一段時間後 -
我已經改變了上面稍微
ws.onopen { |handshake|
cookie, bakesale = handshake.headers['Cookie'].split('=')
rack_cookie = Rack::Session::Cookie.new(MyApp, {
:key => COOKIE_KEY,
:path => '/',
:expire_after => 2592000, #30 days
:secret => COOKIE_SECRET,
:coder => Rack::Session::Cookie::Base64.new
})
puts rack_cookie.coder.decode(bakesale)
}
,並輸出
?q?[?????ov??????to?Z???294cb6e2b95e9?##v3???#c&F3#SC?CSC#CSs?c3sSCCs?cCm;FI"__FLASH__;F{I" user;FU:Moped::BSO?㣤?&?V7D?B!
它看起來像它需要編組。
然而Marshal.load (rack_cookie.coder.decode(bakesale))
拋出一個異常,說dump format error for symbol(0x10)
- 甚至更多的時間後 -
我也試過rack_cookie.coder.decode(bakesale.split('--').first)
這就造成了
??H?d????=?d:ETI"E7ce599b294cb6e2b95e9?##v3???#c&F3#SC?CSC#CSs?c3sSCCs?cCm;FI"__FLASH__;F{I" user;FU:Moped::BSO?㣤?&?V7D?B!
所以正如你所看到的,有一個小小的區別,但無論哪種方式,我需要以某種方式把它變成一個有效的散列。
Marshal.load(rack_cookie.coder.decode(bakesale.split(' - ')。first))無論如何都會導致dump format error for symbol(0x10)
。
所以我覺得我更接近,但沒有雪茄。
我已經將整個示例框架放在了要點中以幫助澄清此問題。 https://gist.github.com/davesag/5506864 – 2013-05-03 02:46:23