@@ -46,53 +46,63 @@ defmodule Plug.Conn.Cookies do
4646
4747 """
4848 def decode ( cookie ) when is_binary ( cookie ) do
49- Map . new ( decode_kv ( cookie , [ ] ) )
49+ Map . new ( decode_kv ( cookie , cookie , 0 , [ ] ) )
5050 end
5151
52- defp decode_kv ( "" , acc ) , do: acc
53- defp decode_kv ( << h , t :: binary >> , acc ) when h in [ ?\s , ?\t ] , do: decode_kv ( t , acc )
54- defp decode_kv ( kv , acc ) when is_binary ( kv ) , do: decode_key ( kv , kv , 0 , acc )
52+ defp decode_kv ( << h , t :: binary >> , cookie , pos , acc ) when h in [ ?\s , ?\t , ?; ] ,
53+ do: decode_kv ( t , cookie , pos + 1 , acc )
5554
56- defp decode_key ( << h , t :: binary >> , _key_rest , _len , acc )
55+ defp decode_kv ( << h , t :: binary >> , cookie , pos , acc ) when h in [ ?\r , ?\n , ?\v , ?\f , ?= ] ,
56+ do: skip_until_cc ( t , cookie , pos + 1 , acc )
57+
58+ defp decode_kv ( << _ , t :: binary >> , cookie , pos , acc ) , do: decode_key ( t , cookie , pos , 1 , acc )
59+ defp decode_kv ( << >> , _cookie , _pos , acc ) , do: acc
60+
61+ defp decode_key ( << h , t :: binary >> , cookie , pos , len , acc )
5762 when h in [ ?\s , ?\t , ?\r , ?\n , ?\v , ?\f ] ,
58- do: skip_until_cc ( t , acc )
63+ do: skip_until_cc ( t , cookie , pos + len + 1 , acc )
5964
60- defp decode_key ( << ?; , t :: binary >> , _key_rest , _len , acc ) , do: decode_kv ( t , acc )
61- defp decode_key ( << ?= , t :: binary >> , _key_rest , 0 , acc ) , do: skip_until_cc ( t , acc )
65+ defp decode_key ( << ?; , t :: binary >> , cookie , pos , len , acc ) ,
66+ do: decode_kv ( t , cookie , pos + len + 1 , acc )
6267
63- defp decode_key ( << ?= , t :: binary >> , key_rest , len , acc ) do
64- key = binary_part ( key_rest , 0 , len )
65- decode_value ( t , t , 0 , 0 , key , acc )
68+ defp decode_key ( << ?= , t :: binary >> , cookie , pos , len , acc ) do
69+ key = binary_part ( cookie , pos , len )
70+ decode_value ( t , cookie , pos + len + 1 , 0 , 0 , key , acc )
6671 end
6772
68- defp decode_key ( << _ , t :: binary >> , key_rest , len , acc ) , do: decode_key ( t , key_rest , len + 1 , acc )
69- defp decode_key ( << >> , _key_rest , _len , acc ) , do: acc
73+ defp decode_key ( << _ , t :: binary >> , cookie , pos , len , acc ) ,
74+ do: decode_key ( t , cookie , pos , len + 1 , acc )
75+
76+ defp decode_key ( << >> , _cookie , _pos , _len , acc ) , do: acc
7077
71- defp decode_value ( << ?; , t :: binary >> , val_rest , len , spaces , key , acc ) do
72- value = binary_part ( val_rest , 0 , len - spaces )
73- decode_kv ( t , [ { key , value } | acc ] )
78+ defp decode_value ( << ?; , t :: binary >> , cookie , pos , len , spaces , key , acc ) do
79+ value = binary_part ( cookie , pos , len - spaces )
80+ decode_kv ( t , cookie , pos + len + 1 , [ { key , value } | acc ] )
7481 end
7582
76- defp decode_value ( << ?\s , t :: binary >> , val_rest , len , spaces , key , acc ) do
77- decode_value ( t , val_rest , len + 1 , spaces + 1 , key , acc )
83+ defp decode_value ( << ?\s , t :: binary >> , cookie , pos , len , spaces , key , acc ) do
84+ decode_value ( t , cookie , pos , len + 1 , spaces + 1 , key , acc )
7885 end
7986
80- defp decode_value ( << h , t :: binary >> , _val_rest , _len , _spaces , _key , acc )
87+ defp decode_value ( << h , t :: binary >> , cookie , pos , len , _spaces , _key , acc )
8188 when h in [ ?\t , ?\r , ?\n , ?\v , ?\f ] ,
82- do: skip_until_cc ( t , acc )
89+ do: skip_until_cc ( t , cookie , pos + len + 1 , acc )
8390
84- defp decode_value ( << _ , t :: binary >> , val_rest , len , _spaces , key , acc ) do
85- decode_value ( t , val_rest , len + 1 , 0 , key , acc )
91+ defp decode_value ( << _ , t :: binary >> , cookie , pos , len , _spaces , key , acc ) do
92+ decode_value ( t , cookie , pos , len + 1 , 0 , key , acc )
8693 end
8794
88- defp decode_value ( << >> , val_rest , len , spaces , key , acc ) do
89- value = binary_part ( val_rest , 0 , len - spaces )
95+ defp decode_value ( << >> , cookie , pos , len , spaces , key , acc ) do
96+ value = binary_part ( cookie , pos , len - spaces )
9097 [ { key , value } | acc ]
9198 end
9299
93- defp skip_until_cc ( << ?; , t :: binary >> , acc ) , do: decode_kv ( t , acc )
94- defp skip_until_cc ( << _ , t :: binary >> , acc ) , do: skip_until_cc ( t , acc )
95- defp skip_until_cc ( << >> , acc ) , do: acc
100+ defp skip_until_cc ( << ?; , t :: binary >> , cookie , pos , acc ) , do: decode_kv ( t , cookie , pos + 1 , acc )
101+
102+ defp skip_until_cc ( << _ , t :: binary >> , cookie , pos , acc ) ,
103+ do: skip_until_cc ( t , cookie , pos + 1 , acc )
104+
105+ defp skip_until_cc ( << >> , _cookie , _pos , acc ) , do: acc
96106
97107 @ doc ~S"""
98108 Encodes the given cookies as expected in a response header.
0 commit comments