@@ -107,15 +107,22 @@ defmodule Plug.Conn.Cookies do
107107 value = Map . get ( opts , :value )
108108 path = Map . get ( opts , :path , "/" )
109109
110- IO . iodata_to_binary ( [
111- "#{ key } =#{ value } ; path=#{ path } " ,
112- emit_if ( opts [ :domain ] , & [ "; domain=" , & 1 ] ) ,
113- emit_if ( opts [ :max_age ] , & encode_max_age ( & 1 , opts ) ) ,
114- emit_if ( Map . get ( opts , :secure , false ) , "; secure" ) ,
115- emit_if ( Map . get ( opts , :http_only , true ) , "; HttpOnly" ) ,
116- emit_if ( Map . get ( opts , :same_site , nil ) , & encode_same_site / 1 ) ,
117- emit_if ( opts [ :extra ] , & [ "; " , & 1 ] )
118- ] )
110+ key = to_string ( key )
111+ value = to_string ( value )
112+ path = to_string ( path )
113+
114+ acc = [ key , ?= , value , "; path=" , path ]
115+ acc = if domain = opts [ :domain ] , do: [ acc , "; domain=" , domain ] , else: acc
116+ acc = if max_age = opts [ :max_age ] , do: [ acc | encode_max_age ( max_age , opts ) ] , else: acc
117+ acc = if Map . get ( opts , :secure , false ) , do: [ acc | "; secure" ] , else: acc
118+ acc = if Map . get ( opts , :http_only , true ) , do: [ acc | "; HttpOnly" ] , else: acc
119+
120+ acc =
121+ if same_site = Map . get ( opts , :same_site ) , do: [ acc | encode_same_site ( same_site ) ] , else: acc
122+
123+ acc = if extra = opts [ :extra ] , do: [ acc , "; " , extra ] , else: acc
124+
125+ IO . iodata_to_binary ( acc )
119126 end
120127
121128 defp encode_max_age ( max_age , opts ) do
@@ -124,23 +131,10 @@ defmodule Plug.Conn.Cookies do
124131 [ "; expires=" , rfc2822 ( time ) , "; max-age=" , Integer . to_string ( max_age ) ]
125132 end
126133
127- defp encode_same_site ( value ) when is_binary ( value ) , do: "; SameSite=#{ value } "
128-
129- defp emit_if ( value , fun_or_string ) do
130- cond do
131- ! value ->
132- [ ]
133-
134- is_function ( fun_or_string ) ->
135- fun_or_string . ( value )
136-
137- is_binary ( fun_or_string ) ->
138- fun_or_string
139- end
140- end
134+ defp encode_same_site ( value ) when is_binary ( value ) , do: [ "; SameSite=" , value ]
141135
142- defp pad ( number ) when number in 0 .. 9 , do: << ?0 , ?0 + number >>
143- defp pad ( number ) , do: Integer . to_string ( number )
136+ defp pad ( n ) when n < 10 , do: << ?0 , ?0 + n >>
137+ defp pad ( n ) , do: << ?0 + div ( n , 10 ) , ?0 + rem ( n , 10 ) >>
144138
145139 defp rfc2822 ( { { year , month , day } = date , { hour , minute , second } } ) do
146140 # Sat, 17 Apr 2010 14:00:00 GMT
0 commit comments