Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix: rework of HTTP::Server of Crystal 0.11
- Loading branch information
1 parent
d29f53c
commit 8cfa856
Showing
27 changed files
with
455 additions
and
238 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,42 +1,20 @@ | ||
require "http/server" | ||
require "frost/server/handlers/log_handler" | ||
require "frost/server/handlers/public_file_handler" | ||
require "frost/server/handlers/deflate_handler" | ||
require "frost/server/handlers/https_everywhere_handler" | ||
require "option_parser" | ||
require "frost/server" | ||
require "./config/bootstrap" | ||
|
||
host = "localhost" | ||
port = 9292 | ||
module <%= name.camelcase %> | ||
class Server < Frost::Server | ||
#def handlers | ||
# [ | ||
# Frost::Server::LogHandler.new, | ||
# Frost::Server::HttpsEverywhereHandler.new(308), | ||
# Frost::Server::PublicFileHandler.new(File.join(Frost.root, "public")) | ||
# ] | ||
#end | ||
|
||
opts = OptionParser.new | ||
opts.on("-b HOST", "--bind=HOST", "Bind to host (defaults to #{ host })") { |value| host = value } | ||
opts.on("-p PORT", "--port=PORT", "Bind to port (defaults to #{ port })") { |value| port = value.to_i } | ||
opts.on("-h", "--help", "Show this help") { puts opts; exit } | ||
|
||
begin | ||
opts.parse(ARGV) | ||
rescue ex : OptionParser::InvalidOption | ||
STDERR.puts ex.message | ||
STDERR.puts | ||
STDERR.puts "Available options:" | ||
STDERR.puts opts | ||
exit | ||
end | ||
|
||
begin | ||
handlers = [ | ||
Frost::Server::LogHandler.new, | ||
Frost::Server::HttpsEverywhereHandler.new(308), | ||
HTTP::DeflateHandler.new, | ||
Frost::Server::PublicFileHandler.new(File.join(Frost.root, "public")) | ||
] | ||
dispatcher = <%= name.camelcase %>::Dispatcher.new | ||
|
||
server = HTTP::Server.new(host, port, handlers) do |context| | ||
dispatcher.call(context) | ||
def dispatcher | ||
@dispatcher ||= <%= name.camelcase %>::Dispatcher.new | ||
end | ||
end | ||
|
||
puts "Listening on http://#{ host }:#{ port }" | ||
server.listen | ||
Server.run | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,36 +1,24 @@ | ||
require "http/server" | ||
require "http/client/response" | ||
|
||
class Zlib::Deflate | ||
def close | ||
input = @input | ||
abstract class Frost::Server | ||
class DeflateHandler < HTTP::DeflateHandler | ||
DEFAULT_DEFLATE_TYPES = %w(text/html text/plain text/xml text/css text/javascript application/javascript application/json) | ||
|
||
if input.responds_to?(:close) | ||
input.close | ||
end | ||
end | ||
end | ||
|
||
class HTTP::Response | ||
def body_io=(@body_io) | ||
@body = nil | ||
headers.delete("Content-length") | ||
end | ||
end | ||
property :deflate_types | ||
|
||
class HTTP::DeflateHandler < HTTP::Handler | ||
def call(request) | ||
response = call_next(request) | ||
def initialize(@deflate_types = DEFAULT_DEFLATE_TYPES) | ||
end | ||
|
||
if should_deflate?(request, response) | ||
io = if response.body? | ||
MemoryIO.new(response.body) | ||
else | ||
response.body_io | ||
end | ||
response.body_io = Zlib::Deflate.new(io) | ||
response.headers["Content-Encoding"] = "deflate" | ||
def call(context) | ||
super if deflate?(context.response) | ||
end | ||
|
||
response | ||
def deflate?(response) | ||
return false unless HTTP::Client::Response.mandatory_body?(response.status_code) | ||
return false if response.headers["Cache-Control"]? =~ /\bno-transform\b/ | ||
return false unless content_type = response.headers["Content-Type"]? | ||
deflate_types.includes?(content_type) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.