server: add Server::ETag

This commit is contained in:
0x1eef 2023-07-05 13:58:31 -03:00
parent f9f896189d
commit b048016dc9
3 changed files with 19 additions and 1 deletions

View file

@ -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

View file

@ -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

View 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