@@ -84,17 +84,29 @@ def etag=(etag)
84
84
LAST_MODIFIED = "Last-Modified" . freeze
85
85
ETAG = "ETag" . freeze
86
86
CACHE_CONTROL = "Cache-Control" . freeze
87
+ SPESHUL_KEYS = %w[ extras no-cache max-age public must-revalidate ]
88
+
89
+ def cache_control_headers
90
+ cache_control = { }
91
+ if cc = self [ CACHE_CONTROL ]
92
+ cc . delete ( ' ' ) . split ( ',' ) . each do |segment |
93
+ directive , argument = segment . split ( '=' , 2 )
94
+ case directive
95
+ when *SPESHUL_KEYS
96
+ key = directive . tr ( '-' , '_' )
97
+ cache_control [ key . to_sym ] = argument || true
98
+ else
99
+ cache_control [ :extras ] ||= [ ]
100
+ cache_control [ :extras ] << segment
101
+ end
102
+ end
103
+ end
104
+ cache_control
105
+ end
87
106
88
107
def prepare_cache_control!
89
- @cache_control = { }
108
+ @cache_control = cache_control_headers
90
109
@etag = self [ ETAG ]
91
-
92
- if cache_control = self [ CACHE_CONTROL ]
93
- cache_control . split ( /,\s */ ) . each do |segment |
94
- first , last = segment . split ( "=" )
95
- @cache_control [ first . to_sym ] = last || true
96
- end
97
- end
98
110
end
99
111
100
112
def handle_conditional_get!
@@ -110,14 +122,24 @@ def handle_conditional_get!
110
122
MUST_REVALIDATE = "must-revalidate" . freeze
111
123
112
124
def set_conditional_cache_control!
113
- return if self [ CACHE_CONTROL ] . present?
125
+ control = { }
126
+ cc_headers = cache_control_headers
127
+ if extras = cc_headers . delete ( :extras )
128
+ @cache_control [ :extras ] ||= [ ]
129
+ @cache_control [ :extras ] += extras
130
+ @cache_control [ :extras ] . uniq!
131
+ end
114
132
115
- control = @cache_control
133
+ control . merge! cc_headers
134
+ control . merge! @cache_control
116
135
117
136
if control . empty?
118
137
headers [ CACHE_CONTROL ] = DEFAULT_CACHE_CONTROL
119
138
elsif control [ :no_cache ]
120
139
headers [ CACHE_CONTROL ] = NO_CACHE
140
+ if control [ :extras ]
141
+ headers [ CACHE_CONTROL ] += ", #{ control [ :extras ] . join ( ', ' ) } "
142
+ end
121
143
else
122
144
extras = control [ :extras ]
123
145
max_age = control [ :max_age ]
0 commit comments