2016-07-17 11:26:22 +00:00
|
|
|
# HTTP::Server::Context is the class which holds HTTP::Request and HTTP::Server::Response alongside with
|
|
|
|
# information such as request params, request/response content_type, session e.g
|
|
|
|
#
|
|
|
|
# Instances of this class are passed to an `HTTP::Server` handler.
|
2016-01-24 10:22:25 +00:00
|
|
|
class HTTP::Server
|
|
|
|
class Context
|
2017-04-06 18:43:41 +00:00
|
|
|
# :nodoc:
|
|
|
|
STORE_MAPPINGS = [ Nil, String, Int32, Int64, Float64, Bool ]
|
|
|
|
|
|
|
|
macro finished
|
|
|
|
alias StoreTypes = Union({{ *STORE_MAPPINGS }})
|
|
|
|
getter store = {} of String => StoreTypes
|
|
|
|
end
|
2016-07-14 18:56:01 +00:00
|
|
|
|
2016-01-24 10:22:25 +00:00
|
|
|
def params
|
2016-05-21 12:00:44 +00:00
|
|
|
@request.url_params ||= route_lookup.params
|
2016-11-24 09:07:39 +00:00
|
|
|
@params ||= if @request.param_parser
|
|
|
|
@request.param_parser.not_nil!
|
|
|
|
else
|
|
|
|
Kemal::ParamParser.new(@request)
|
|
|
|
end
|
2016-01-24 10:22:25 +00:00
|
|
|
end
|
2016-01-24 10:52:41 +00:00
|
|
|
|
2016-02-09 15:25:01 +00:00
|
|
|
def redirect(url, status_code = 302)
|
2016-01-24 10:52:41 +00:00
|
|
|
@response.headers.add "Location", url
|
2016-02-09 15:25:01 +00:00
|
|
|
@response.status_code = status_code
|
2016-01-24 10:52:41 +00:00
|
|
|
end
|
2016-02-16 20:07:58 +00:00
|
|
|
|
|
|
|
def route_lookup
|
2016-06-15 05:58:14 +00:00
|
|
|
Kemal::RouteHandler::INSTANCE.lookup_route(@request.override_method.as(String), @request.path)
|
2016-02-16 20:07:58 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def route_defined?
|
|
|
|
route_lookup.found?
|
|
|
|
end
|
2016-06-27 21:37:40 +00:00
|
|
|
|
2016-07-14 18:56:01 +00:00
|
|
|
def get(name)
|
|
|
|
@store[name]
|
|
|
|
end
|
|
|
|
|
|
|
|
def set(name, value)
|
|
|
|
@store[name] = value
|
|
|
|
end
|
2014-07-30 23:50:14 +00:00
|
|
|
end
|
|
|
|
end
|