Upgrade to Pro — share decks privately, control downloads, hide ads and more …

たのしいOSSコードリーディング:Let's read "cookies"🍪

たのしいOSSコードリーディング:Let's read "cookies"🍪

More Decks by Misaki Shioi(塩井美咲/しおい)

Other Decks in Programming

Transcript

  1. "DUJPO%JTQBUDI$PPLJFT͸3BDLϛυϧ΢ΣΞ $PPLJFΛอଘ͢ΔͨΊͷ"DUJPO%JTQBUDI$PPLJFTΫϥε ʜ3BJMTΞϓϦέʔγϣϯ͕࢖͏3BDLϛυϧ΢ΣΞͷͻͱͭ $ rails middleware ɹ (ུ)... use ActionDispatch::Cookies

    use ActionDispatch::Session::CookieStore use ActionDispatch::Flash use ActionDispatch::ContentSecurityPolicy::Middleware use Rack::Head use Rack::ConditionalGet use Rack::ETag use Rack::TempfileReaper run Myapp::Application.routes
  2. DPPLJFTϝιουͷ࣮૷ DPPLJFT<VTFS@JE>VTFSJE ɹ BDUJPOQBDLMJCBDUJPO@DPOUSPMMFSNFUBMDPPLJFTSCDPPLJFT ɹ 3 module ActionController 4 module

    Cookies . . . 12 def cookies 13 request.cookie_jar 14 end  ΞϓϦέʔγϣϯ͔ΒDPPLJFTϝιουΛݺͿ ‎"DUJPO%JTQBUDI$PPLJFTϛυϧ΢ΣΞΛ࢖༻͢Δ DPPLJF@KBSϝιουͷ಺෦Ͱ "DUJPO%JTQBUDI$PPLJFTͷ ΠϯελϯεΛੜ੒
  3. શମͷྲྀΕ͸͜Μͳײ͡ ɹ ɹ <3BJMT>"DUJPO$POUSPMMFS.FUBMDPPLJFT ɹ <3BJMT>"DUJPO%JTQBUDI3FRVFTUDPPLJF@KBS ɹ <3BDL>3FRVFTU&OWGFUDI@IFBEFS΋͘͠͸ ɹɹ<3BDL>3FRVFTU&OWTFU@IFBEFS ɹ

    ɹɹɹɹ<3BJMT>"DUJPO%JTQBUDI$PPLJFT$PPLJF+BSCVJME <3BDL>3FRVFTU)FMQFSTDPPLJFT ɹ <3BJMT>"DUJPO%JTQBUDI$PPLJFT<> ɿ <3BJMT>"DUJPO%JTQBUDI$PPLJFTDBMM DPPLJFT<VTFS@JE>VTFSJE͢Δͱʜ
  4. શମͷྲྀΕ͸͜Μͳײ͡ ɹ ɹ <3BJMT><3BDL>ϦΫΤετ͔Βطଘͷ$PPLJFΛݟ͚ͭͯฦ͢ ݟ͔ͭΒͳ͔ͬͨ৔߹  <3BJMT><3BDL>ۭͷ$PPLJFΛϔομʹ௥Ճ ɹɹɹ"DUJPO%JTQBUDI$PPLJFT$PPLJF+BSͷΠϯελϯεԽ ɹɹɹΠϯελϯεม਺@cookiesͷॳظԽ 

    <3BJMT>Πϯελϯεม਺@cookiesʹ৽͍͠$PPLJFͷ஋Λ௥Ճ  <3BJMT>ϛυϧ΢ΣΞ͕ΞϓϦέʔγϣϯΛฦ͢λΠϛϯάͰ ɹɹɹΠϯελϯεม਺ͷ಺༰Λϔομʹॻ͖ࠐΉ DPPLJFT<VTFS@JE>VTFSJE͢Δͱʜ ‐͜͜·ͰಡΈ·͢
  5. BDUJPOQBDLMJCBDUJPO@DPOUSPMMFSNFUBMDPPLJFTSCDPPLJFT 12 def cookies 13 request.cookie_jar 14 end BDUJPOQBDLMJCBDUJPO@EJTQBUDINJEEMFXBSFDPPLJFTSC 9

    module ActionDispatch 10 module Request 11 def cookie_jar 12 fetch_header("action_dispatch.cookies") do 13 self.cookie_jar = Cookies::CookieJar.build(self, cookies) 14 end 15 end <3BJMT>ϦΫΤετ͔Βطଘͷ$PPLJFΛݟ͚ͭͯฦ͢
  6. BDUJPOQBDLMJCBDUJPO@EJTQBUDINJEEMFXBSFDPPLJFTSCDPPLJF@KBS 9 module ActionDispatch 10 module Request 11 def cookie_jar

    12 fetch_header("action_dispatch.cookies") do 13 self.cookie_jar = Cookies::CookieJar.build(self, cookies) 14 end 15 end <3BJMT>ϦΫΤετ͔Βطଘͷ$PPLJFΛݟ͚ͭͯฦ͢
  7. BDUJPOQBDLMJCBDUJPO@EJTQBUDINJEEMFXBSFDPPLJFTSCDPPLJF@KBS 9 module ActionDispatch 10 module Request 11 def cookie_jar

    12 fetch_header("action_dispatch.cookies") do 13 self.cookie_jar = Cookies::CookieJar.build(self, cookies) 14 end 15 end <3BDL>ϦΫΤετ͔Βطଘͷ$PPLJFΛݟ͚ͭͯฦ͢ SBDLMJCSBDLSFRVFTUSC 69 def fetch_header(name, block) 70 @env.fetch(name, &block) 71 end ϦΫΤετϔομ ˞)BTI\^ )BTIGFUDI
  8. BDUJPOQBDLMJCBDUJPO@EJTQBUDINJEEMFXBSFDPPLJFTSCDPPLJF@KBS 9 module ActionDispatch 10 module Request 11 def cookie_jar

    12 fetch_header("action_dispatch.cookies") do 13 self.cookie_jar = Cookies::CookieJar.build(self, cookies) 14 end 15 end <3BDL>ϦΫΤετ͔Βطଘͷ$PPLJFΛݟ͚ͭͯฦ͢ SBDLMJCSBDLSFRVFTUSC 69 def fetch_header(name, block) 70 @env.fetch(name, &block) 71 end BDUJPO@EJTQBUDIDPPLJFT
  9. BDUJPOQBDLMJCBDUJPO@EJTQBUDINJEEMFXBSFDPPLJFTSCDPPLJF@KBS 9 module ActionDispatch 10 module Request 11 def cookie_jar

    12 fetch_header("action_dispatch.cookies") do 13 self.cookie_jar = Cookies::CookieJar.build(self, cookies) 14 end 15 end <3BJMT><3BDL>ϦΫΤετ͔Βطଘͷ$PPLJFΛݟ͚ͭͯฦ͢ <3BJMT><3BDL>ϦΫΤετ͔Β طଘͷ$PPLJFΛݟ͚ͭͯฦ͢
  10. <3BJMT>ۭͷ$PPLJFΛϔομʹ௥Ճ BDUJPOQBDLMJCBDUJPO@EJTQBUDINJEEMFXBSFDPPLJFTSCDPPLJF@KBS 9 module ActionDispatch 10 module Request 11 def

    cookie_jar 12 fetch_header("action_dispatch.cookies") do 13 self.cookie_jar = Cookies::CookieJar.build(self, cookies) 14 end 15 end Ωʔ͕ݟ͔ͭΒͳ͔ͬͨ৔߹ɺ ϒϩοΫͷ಺෦͕ධՁ͞ΕΔ
  11. <3BJMT>ۭͷ$PPLJFΛϔομʹ௥Ճ BDUJPOQBDLMJCBDUJPO@EJTQBUDINJEEMFXBSFDPPLJFTSCDPPLJF@KBS 9 module ActionDispatch 10 module Request 11 def

    cookie_jar 12 fetch_header("action_dispatch.cookies") do 13 self.cookie_jar = Cookies::CookieJar.build(self, cookies) 14 end 15 end 28 def cookie_jar=(jar) 29 set_header "action_dispatch.cookies", jar 30 end
  12. <3BDL>ۭͷ$PPLJFΛϔομʹ௥Ճ BDUJPOQBDLMJCBDUJPO@EJTQBUDINJEEMFXBSFDPPLJFTSCDPPLJF@KBS 9 module ActionDispatch 10 module Request 11 def

    cookie_jar 12 fetch_header("action_dispatch.cookies") do 13 self.cookie_jar = Cookies::CookieJar.build(self, cookies) 14 end 15 end SBDLMJCSBDLSFRVFTUSC 78 def set_header(name, v) 79 @env[name] = v 80 end )BTI<> ϋογϡͷΩʔʹରͯ͠ ஋Λؔ࿈͚ͮΔ
  13. <3BDL>ۭͷ$PPLJFΛϔομʹ௥Ճ BDUJPOQBDLMJCBDUJPO@EJTQBUDINJEEMFXBSFDPPLJFTSCDPPLJF@KBS 9 module ActionDispatch 10 module Request 11 def

    cookie_jar 12 fetch_header("action_dispatch.cookies") do 13 self.cookie_jar = Cookies::CookieJar.build(self, cookies) 14 end 15 end SBDLMJCSBDLSFRVFTUSC 78 def set_header(name, v) 79 @env[name] = v 80 end $PPLJFT$PPLJF+BSCVJME BDUJPO@EJTQBUDIDPPLJFT
  14. <3BDL>ۭͷ$PPLJFΛϔομʹ௥Ճ BDUJPOQBDLMJCBDUJPO@EJTQBUDINJEEMFXBSFDPPLJFTSCDPPLJF@KBS 9 module ActionDispatch 10 module Request 11 def

    cookie_jar 12 fetch_header("action_dispatch.cookies") do 13 self.cookie_jar = Cookies::CookieJar.build(self, cookies) 14 end 15 end @env = { "action_dispatch.cookies": Cookies::CookieJar.build(self, cookies) } MJCSBDLSFRVFTUSCDPPLJFT ۭͷ$PPLJF
  15. <3BJMT><3BDL>ۭͷ$PPLJFΛϔομʹ௥Ճ BDUJPOQBDLMJCBDUJPO@EJTQBUDINJEEMFXBSFDPPLJFTSCDPPLJF@KBS 9 module ActionDispatch 10 module Request 11 def

    cookie_jar 12 fetch_header("action_dispatch.cookies") do 13 self.cookie_jar = Cookies::CookieJar.build(self, cookies) 14 end 15 end <3BJMT><3BDL>ۭͷ$PPLJFΛϔομʹ௥Ճ
  16. <3BJMT>"DUJPO%JTQBUDI$PPLJFT$PPLJF+BSΛΠϯελϯεԽ BDUJPOQBDLMJCBDUJPO@EJTQBUDINJEEMFXBSFDPPLJFTSCDPPLJF@KBS 9 module ActionDispatch 10 module Request 11 def

    cookie_jar 12 fetch_header("action_dispatch.cookies") do 13 self.cookie_jar = Cookies::CookieJar.build(self, cookies) 14 end 15 end @env = { "action_dispatch.cookies": Cookies::CookieJar.build(self, cookies) }
  17. BDUJPOQBDLMJCBDUJPO@EJTQBUDINJEEMFXBSFDPPLJFTSCCVJME 272 class CookieJar 289 def self.build(req, cookies) 290 new(req,

    cookies).tap do |jar| 291 jar.update(cookies) 292 end 293 end <3BJMT>"DUJPO%JTQBUDI$PPLJFT$PPLJF+BSΛΠϯελϯεԽ
  18. BDUJPOQBDLMJCBDUJPO@EJTQBUDINJEEMFXBSFDPPLJFTSCCVJME 272 class CookieJar 289 def self.build(req, cookies) 290 new(req,

    cookies).tap do |jar| 291 jar.update(cookies) 292 end 293 end <3BJMT>"DUJPO%JTQBUDI$PPLJFT$PPLJF+BSΛΠϯελϯεԽ <3BJMT>"DUJPO%JTQBUDI$PPLJFT$PPLJF+BS ΛΠϯελϯεԽ
  19. <3BJMT>Πϯελϯεม਺@cookiesͷॳظԽ BDUJPOQBDLMJCBDUJPO@EJTQBUDINJEEMFXBSFDPPLJFTSCCVJME 272 class CookieJar 289 def self.build(req, cookies) 290

    new(req, cookies).tap do |jar| 291 jar.update(cookies) 292 end 293 end 297 def initialize(request) ... 301 @cookies = {}
  20. <3BJMT>Πϯελϯεม਺@cookiesͷॳظԽ BDUJPOQBDLMJCBDUJPO@EJTQBUDINJEEMFXBSFDPPLJFTSCCVJME 272 class CookieJar 289 def self.build(req, cookies) 290

    new(req, cookies).tap do |jar| 291 jar.update(cookies) 292 end 293 end <3BJMT>Πϯελϯεม਺ @cookiesͷॳظԽ
  21. <3BJMT>Πϯελϯεม਺@cookiesͷॳظԽ BDUJPOQBDLMJCBDUJPO@EJTQBUDINJEEMFXBSFDPPLJFTSCCVJME 272 class CookieJar 289 def self.build(req, cookies) 290

    new(req, cookies).tap do |jar| 291 jar.update(cookies) 292 end 293 end <3BJMT>Πϯελϯεม਺ @cookiesͷॳظԽ 334 def update(other_hash) 335 @cookies.update other_hash.stringify_keys 336 self 337 end )BTINFSHFͷΤΠϦΞε ۭͷ$PPLJF
  22. <3BJMT>Πϯελϯεม਺@cookiesͷॳظԽ BDUJPOQBDLMJCBDUJPO@EJTQBUDINJEEMFXBSFDPPLJFTSCCVJME 272 class CookieJar 289 def self.build(req, cookies) 290

    new(req, cookies).tap do |jar| 291 jar.update(cookies) 292 end 293 end <3BJMT>Πϯελϯεม਺ !DPPLJFTͷॳظԽ <3BJMT>Πϯελϯεม਺@cookiesͷॳظԽ <3BJMT>Πϯελϯεม਺ @cookiesͷॳظԽ
  23. <3BJMT>Πϯελϯεม਺@cookiesʹ৽͍͠$PPLJFͷ஋Λ௥Ճ BDUJPOQBDLMJCBDUJPO@EJTQBUDINJEEMFXBSFDPPLJFTSC<> 374 def [ ]=(name, options) 375 if options.is_a?(Hash)

    ... 378 else 379 value = options 380 options = { value: value } 381 end 382 383 handle_options(options) 384 385 if @cookies[name.to_s] != value || options[:expires] 386 @cookies[name.to_s] = value ஫໨
  24. <3BJMT>Πϯελϯεม਺@cookiesʹ৽͍͠$PPLJFͷ஋Λ௥Ճ BDUJPOQBDLMJCBDUJPO@EJTQBUDINJEEMFXBSFDPPLJFTSC<> 374 def [ ]=(name, options) 375 if options.is_a?(Hash)

    ... 378 else 379 value = options 380 options = { value: value } 381 end 382 383 handle_options(options) 384 385 if @cookies[name.to_s] != value || options[:expires] 386 @cookies[name.to_s] = value
  25. BDUJPOQBDLMJCBDUJPO@EJTQBUDINJEEMFXBSFDPPLJFTSC<> 379 value = options 386 @cookies[name.to_s] = value <3BJMT>Πϯελϯεม਺@cookiesʹ৽͍͠$PPLJFͷ஋Λ௥Ճ

    ɹ DPPLJFT<VTFS@JE>VTFSJE ‐OBNF ‐PQUJPOT <3BJMT>Πϯελϯεม਺@cookiesʹ ৽͍͠$PPLJFͷ஋Λ௥Ճ
  26. શମͷྲྀΕΛ͓͞Β͍ ɹ ɹ <3BJMT>"DUJPO$POUSPMMFS.FUBMDPPLJFT ɹ <3BJMT>"DUJPO%JTQBUDI3FRVFTUDPPLJF@KBS ɹ <3BDL>3FRVFTU&OWGFUDI@IFBEFS΋͘͠͸ ɹɹ<3BDL>3FRVFTU&OWTFU@IFBEFS ɹ

    ɹɹɹɹ<3BJMT>"DUJPO%JTQBUDI$PPLJFT$PPLJF+BSCVJME <3BDL>3FRVFTU)FMQFSTDPPLJFT ɹ <3BJMT>"DUJPO%JTQBUDI$PPLJFT<> ɿɹ <3BJMT>"DUJPO%JTQBUDI$PPLJFTDBMM DPPLJFT<VTFS@JE>VTFSJE͢Δͱʜ
  27. શମͷྲྀΕΛ͓͞Β͍ ɹ ɹ <3BJMT><3BDL>ϦΫΤετ͔Βطଘͷ$PPLJFΛݟ͚ͭͯฦ͢ ݟ͔ͭΒͳ͔ͬͨ৔߹  <3BJMT><3BDL>ۭͷ$PPLJFΛϔομʹ௥Ճ ɹɹɹ"DUJPO%JTQBUDI$PPLJFT$PPLJF+BSͷΠϯελϯεԽ ɹɹɹΠϯελϯεม਺@cookiesͷॳظԽ 

    <3BJMT>Πϯελϯεม਺@cookiesʹ৽͍͠$PPLJFͷ஋Λ௥Ճ  <3BJMT>ϛυϧ΢ΣΞ͕ΞϓϦέʔγϣϯΛฦ͢λΠϛϯάͰ ɹɹɹΠϯελϯεม਺ͷ಺༰Λϔομʹॻ͖ࠐΉ DPPLJFT<VTFS@JE>VTFSJE͢Δͱʜ ‐͜͜·ͰಡΈ·ͨ͠