server: add Server::ETag
This commit is contained in:
parent
f9f896189d
commit
b048016dc9
3 changed files with 19 additions and 1 deletions
|
@ -4,10 +4,12 @@ class Server
|
|||
require "rack"
|
||||
require_relative "server/puma"
|
||||
require_relative "server/gzip"
|
||||
require_relative "server/etag"
|
||||
require_relative "server/dir"
|
||||
|
||||
def self.app(path)
|
||||
Rack::Builder.app do
|
||||
use Server::ETag
|
||||
run Server::Dir.new(path)
|
||||
end
|
||||
end
|
||||
|
|
|
@ -32,7 +32,7 @@ class Server::Dir
|
|||
200,
|
||||
{"content-type" => mime_types[extn] || Rack::Mime.mime_type(extn),
|
||||
"content-length" => body.bytesize},
|
||||
body.each_line
|
||||
body.each_line.to_a
|
||||
]
|
||||
end
|
||||
|
||||
|
|
16
packages/ruby/server/lib/server/etag.rb
Normal file
16
packages/ruby/server/lib/server/etag.rb
Normal file
|
@ -0,0 +1,16 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
class Server::ETag < Rack::ETag
|
||||
def initialize(app)
|
||||
@app = app
|
||||
end
|
||||
|
||||
def call(env)
|
||||
status, headers, body = super(env)
|
||||
if env["HTTP_IF_NONE_MATCH"] == headers["etag"]
|
||||
[304, headers, [""]]
|
||||
else
|
||||
[status, headers, body]
|
||||
end
|
||||
end
|
||||
end
|
Loading…
Reference in a new issue