Skip to content

Commit 40999af

Browse files
committed
Improve handling of weak etags with deflate (#2758)
1 parent 5e579db commit 40999af

File tree

2 files changed

+62
-4
lines changed

2 files changed

+62
-4
lines changed

server/webapp/WEB-INF/rails.new/lib/jetty_weak_etag_middleware.rb

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,35 @@
1+
##########################################################################
2+
# Copyright 2016 ThoughtWorks, Inc.
3+
#
4+
# Licensed under the Apache License, Version 2.0 (the "License");
5+
# you may not use this file except in compliance with the License.
6+
# You may obtain a copy of the License at
7+
#
8+
# http://www.apache.org/licenses/LICENSE-2.0
9+
#
10+
# Unless required by applicable law or agreed to in writing, software
11+
# distributed under the License is distributed on an "AS IS" BASIS,
12+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
# See the License for the specific language governing permissions and
14+
# limitations under the License.
15+
##########################################################################
16+
117
class JettyWeakEtagMiddleware
218
def initialize(app)
319
@app = app
420
end
521

22+
MATCH_REGEX = /--(gzip|deflate)"$/
23+
REPLACE_REGEX = /--(gzip|deflate)"?$/
24+
625
def call(env)
726
# make weak etags sent by jetty strong see: http://stackoverflow.com/questions/18693718/weak-etags-in-rails
8-
if env['HTTP_IF_MATCH'] =~ /--gzip"$/
9-
env['HTTP_IF_MATCH'] = env['HTTP_IF_MATCH'].gsub(/--gzip"?$/, '"')
27+
if env['HTTP_IF_MATCH'] =~ MATCH_REGEX
28+
env['HTTP_IF_MATCH'] = env['HTTP_IF_MATCH'].gsub(REPLACE_REGEX, '"')
1029
end
1130

12-
if env['HTTP_IF_NONE_MATCH'] =~ /--gzip"$/
13-
env['HTTP_IF_NONE_MATCH'] = env['HTTP_IF_NONE_MATCH'].gsub(/--gzip"?$/, '"')
31+
if env['HTTP_IF_NONE_MATCH'] =~ MATCH_REGEX
32+
env['HTTP_IF_NONE_MATCH'] = env['HTTP_IF_NONE_MATCH'].gsub(REPLACE_REGEX, '"')
1433
end
1534

1635
status, headers, body = @app.call(env)
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
##########################################################################
2+
# Copyright 2016 ThoughtWorks, Inc.
3+
#
4+
# Licensed under the Apache License, Version 2.0 (the "License");
5+
# you may not use this file except in compliance with the License.
6+
# You may obtain a copy of the License at
7+
#
8+
# http://www.apache.org/licenses/LICENSE-2.0
9+
#
10+
# Unless required by applicable law or agreed to in writing, software
11+
# distributed under the License is distributed on an "AS IS" BASIS,
12+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
# See the License for the specific language governing permissions and
14+
# limitations under the License.
15+
##########################################################################
16+
17+
require 'spec_helper'
18+
19+
describe JettyWeakEtagMiddleware do
20+
before(:each) do
21+
@app = double('rack-app')
22+
@middleware = JettyWeakEtagMiddleware.new(@app)
23+
end
24+
25+
%w(gzip deflate).each do |compression_type|
26+
it "should remove --#{compression_type} suffix inserted by GzipFilter from jetty" do
27+
env = {
28+
'HTTP_IF_MATCH' => %Q{"foobar--#{compression_type}"},
29+
'HTTP_IF_NONE_MATCH' => %Q{"foobar--#{compression_type}"}
30+
}
31+
32+
@app.should_receive(:call).with({
33+
'HTTP_IF_MATCH' => '"foobar"',
34+
'HTTP_IF_NONE_MATCH' => '"foobar"'
35+
})
36+
@middleware.call(env)
37+
end
38+
end
39+
end

0 commit comments

Comments
 (0)