Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
sdogruyol committed Dec 17, 2018
1 parent 0dd9bff commit aab22b1
Show file tree
Hide file tree
Showing 10 changed files with 27 additions and 26 deletions.
2 changes: 1 addition & 1 deletion spec/param_parser_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ describe "ParamParser" do
body_params.to_s.should eq("")

json_params = Kemal::ParamParser.new(request).json
json_params.should eq({} of String => Nil | String | Int64 | Float64 | Bool | Hash(String, JSON::Type) | Array(JSON::Type))
json_params.should eq({} of String => Nil | String | Int64 | Float64 | Bool | Hash(String, JSON::Any) | Array(JSON::Any))
end
end

Expand Down
1 change: 0 additions & 1 deletion spec/route_handler_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,6 @@ describe "Kemal::RouteHandler" do
post "/" do |env|
skills = env.params.json["skills"].as(Array)
skills_from_languages = skills.map do |skill|
skill = skill.as(Hash)
skill["language"]
end
"Skills #{skills_from_languages.each.join(',')}"
Expand Down
4 changes: 2 additions & 2 deletions spec/static_file_handler_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ describe Kemal::StaticFileHandler do

it "should handle setting custom headers" do
config = default_config
config.static_headers = Proc(HTTP::Server::Response, String, File::Stat, Void).new do |response, path, stat|
config.static_headers = Proc(HTTP::Server::Response, String, File::Info, Void).new do |response, path, stat|
if path =~ /\.html$/
response.headers.add("Access-Control-Allow-Origin", "*")
end
Expand All @@ -154,7 +154,7 @@ describe Kemal::StaticFileHandler do
response = handle HTTP::Request.new("GET", "/dir/test.txt"), config
response.headers.has_key?("Access-Control-Allow-Origin").should be_false
response.headers["Content-Size"].should eq(
File.stat("#{__DIR__}/static/dir/test.txt").size.to_s
File.info("#{__DIR__}/static/dir/test.txt").size.to_s
)

response = handle HTTP::Request.new("GET", "/dir/index.html"), config
Expand Down
14 changes: 10 additions & 4 deletions src/kemal/base.cr
Original file line number Diff line number Diff line change
Expand Up @@ -80,12 +80,18 @@ class Kemal::Base
end

private def start_server(port)
@server = server = HTTP::Server.new(@config.host_binding, port || @config.port, @handlers)
{% if !flag?(:without_openssl) %}
server.tls = config.ssl
@server = server = HTTP::Server.new(@handlers)

{% if flag?(:without_openssl) %}
server.bind_tcp(@config.host_binding, port || @config.port)
{% else %}
if ssl = config.ssl
server.bind_tls(@config.host_binding, port || @config.port, ssl)
else
server.bind_tcp(@config.host_binding, port || @config.port)
end
{% end %}

server.bind
@running = true

yield
Expand Down
2 changes: 1 addition & 1 deletion src/kemal/config.cr
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ module Kemal
property? always_rescue = true
property? shutdown_message = true
property extra_options : (OptionParser ->)?
property static_headers : (HTTP::Server::Response, String, File::Stat -> Void)?
property static_headers : (HTTP::Server::Response, String, File::Info -> Void)?

# Creates a config with default values.
def initialize(
Expand Down
4 changes: 2 additions & 2 deletions src/kemal/file_upload.cr
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
module Kemal
# :nodoc:
struct FileUpload
getter tmpfile : Tempfile
getter tmpfile : File
getter filename : String?
getter headers : HTTP::Headers
getter creation_time : Time?
Expand All @@ -10,7 +10,7 @@ module Kemal
getter size : UInt64?

def initialize(upload)
@tmpfile = Tempfile.new(filename)
@tmpfile = File.tempfile
::File.open(@tmpfile.path, "w") do |file|
IO.copy(upload.body, file)
end
Expand Down
2 changes: 1 addition & 1 deletion src/kemal/helpers/file_helpers.cr
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ module Kemal::FileHelpers
minsize = 860 # http://webmasters.stackexchange.com/questions/31750/what-is-recommended-minimum-object-size-for-gzip-performance-benefits ??
request_headers = env.request.headers
filesize = File.size(file_path)
filestat = File.stat(file_path)
filestat = File.info(file_path)

config.static_headers.try(&.call(env.response, file_path, filestat))
gzip = config.serve_static?("gzip")
Expand Down
1 change: 0 additions & 1 deletion src/kemal/main.cr
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
require "http"
require "json"
require "uri"
require "tempfile"
require "./application"
require "./base_log_handler"
require "./cli"
Expand Down
4 changes: 2 additions & 2 deletions src/kemal/param_parser.cr
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ module Kemal
APPLICATION_JSON = "application/json"
MULTIPART_FORM = "multipart/form-data"
# :nodoc:
alias AllParamTypes = Nil | String | Int64 | Float64 | Bool | Hash(String, JSON::Type) | Array(JSON::Type)
alias AllParamTypes = Nil | String | Int64 | Float64 | Bool | Hash(String, JSON::Any) | Array(JSON::Any)
getter files

def initialize(@request : HTTP::Request)
Expand Down Expand Up @@ -88,7 +88,7 @@ module Kemal
case json = JSON.parse(body).raw
when Hash
json.each do |key, value|
@json[key] = value.as(AllParamTypes)
@json[key] = value.raw
end
when Array
@json["_json"] = json
Expand Down
19 changes: 8 additions & 11 deletions src/kemal/static_file_handler.cr
Original file line number Diff line number Diff line change
Expand Up @@ -55,21 +55,18 @@ module Kemal
return call_next(context)
end
elsif File.exists?(file_path)
return if etag(context, file_path)
last_modified = modification_time(file_path)
add_cache_headers(context.response.headers, last_modified)

if cache_request?(context, last_modified)
context.response.status_code = 304
return
end

FileHelpers.send_file(context, file_path, config)
else
call_next(context)
end
end

private def etag(context : HTTP::Server::Context, file_path : String)
etag = %{W/"#{File.lstat(file_path).mtime.epoch.to_s}"}
context.response.headers["ETag"] = etag
return false if !context.request.headers["If-None-Match"]? || context.request.headers["If-None-Match"] != etag
context.response.headers.delete "Content-Type"
context.response.content_length = 0
context.response.status_code = 304 # not modified
return true
end
end
end

0 comments on commit aab22b1

Please sign in to comment.