2017-10-06 11:53:53 +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 data and alike.
|
2016-07-17 11:26:22 +00:00
|
|
|
#
|
|
|
|
# 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:
|
2017-05-08 14:33:19 +00:00
|
|
|
STORE_MAPPINGS = [Nil, String, Int32, Int64, Float64, Bool]
|
2017-04-06 18:43:41 +00:00
|
|
|
|
|
|
|
macro finished
|
|
|
|
alias StoreTypes = Union({{ *STORE_MAPPINGS }})
|
2018-01-26 15:30:09 +00:00
|
|
|
@store = {} of String => StoreTypes
|
2017-04-06 18:43:41 +00:00
|
|
|
end
|
2016-07-14 18:56:01 +00:00
|
|
|
|
2016-01-24 10:22:25 +00:00
|
|
|
def params
|
2018-06-30 13:49:04 +00:00
|
|
|
@params ||= Kemal::ParamParser.new(@request, route_lookup.params)
|
2016-01-24 10:22:25 +00:00
|
|
|
end
|
2016-01-24 10:52:41 +00:00
|
|
|
|
2022-06-29 10:25:40 +00:00
|
|
|
def redirect(url : String, status_code : Int32 = 302, *, body : String? = nil, close : Bool = true)
|
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
|
2020-04-04 18:25:35 +00:00
|
|
|
@response.print(body) if body
|
2022-06-29 10:25:40 +00:00
|
|
|
@response.close if close
|
2016-01-24 10:52:41 +00:00
|
|
|
end
|
2016-02-16 20:07:58 +00:00
|
|
|
|
2017-09-04 20:46:53 +00:00
|
|
|
def route
|
2017-09-05 09:03:37 +00:00
|
|
|
route_lookup.payload
|
2017-09-04 20:46:53 +00:00
|
|
|
end
|
|
|
|
|
2017-09-10 11:41:07 +00:00
|
|
|
def websocket
|
|
|
|
ws_route_lookup.payload
|
|
|
|
end
|
|
|
|
|
2016-02-16 20:07:58 +00:00
|
|
|
def route_lookup
|
2018-06-30 12:12:13 +00:00
|
|
|
Kemal::RouteHandler::INSTANCE.lookup_route(@request.method.as(String), @request.path)
|
2016-02-16 20:07:58 +00:00
|
|
|
end
|
|
|
|
|
2018-08-13 16:21:18 +00:00
|
|
|
def route_found?
|
2016-02-16 20:07:58 +00:00
|
|
|
route_lookup.found?
|
|
|
|
end
|
2016-06-27 21:37:40 +00:00
|
|
|
|
2017-08-20 17:01:49 +00:00
|
|
|
def ws_route_lookup
|
2017-09-10 11:41:07 +00:00
|
|
|
Kemal::WebSocketHandler::INSTANCE.lookup_ws_route(@request.path)
|
2017-08-20 17:01:49 +00:00
|
|
|
end
|
|
|
|
|
2018-08-13 16:21:18 +00:00
|
|
|
def ws_route_found?
|
2017-08-20 17:01:49 +00:00
|
|
|
ws_route_lookup.found?
|
|
|
|
end
|
|
|
|
|
2017-08-24 20:32:43 +00:00
|
|
|
def get(name : String)
|
2016-07-14 18:56:01 +00:00
|
|
|
@store[name]
|
|
|
|
end
|
|
|
|
|
2017-08-24 20:32:43 +00:00
|
|
|
def set(name : String, value : StoreTypes)
|
2016-07-14 18:56:01 +00:00
|
|
|
@store[name] = value
|
|
|
|
end
|
2018-01-26 15:30:09 +00:00
|
|
|
|
|
|
|
def get?(name : String)
|
|
|
|
@store[name]?
|
|
|
|
end
|
2014-07-30 23:50:14 +00:00
|
|
|
end
|
|
|
|
end
|