@@ -111,8 +111,12 @@ def [] key
111111 else
112112 if key . is_a? ( String ) && key . start_with? ( "HTTP_" )
113113 self . headers [ key [ 5 ..] . downcase . tr ( "_" , "-" ) ]
114- else
114+ elsif @attributes . key? ( key )
115115 @attributes [ key ]
116+ elsif key . is_a? ( Symbol ) && @attributes . key? ( key . to_s )
117+ @attributes [ key . to_s ]
118+ else
119+ self . arguments [ key . to_s ]
116120 end
117121 end
118122 end
@@ -166,6 +170,42 @@ def with(method: self.method, path: self.path, path_info: nil, attributes: {})
166170 def method
167171 @http . method
168172 end
173+ alias request_method method
174+
175+ # @returns [Boolean] Whether the HTTP request method is GET.
176+ def get?
177+ @http . method == "GET"
178+ end
179+
180+ # @returns [Boolean] Whether the HTTP request method is HEAD.
181+ def head?
182+ @http . method == "HEAD"
183+ end
184+
185+ # @returns [Boolean] Whether the HTTP request method is POST.
186+ def post?
187+ @http . method == "POST"
188+ end
189+
190+ # @returns [Boolean] Whether the HTTP request method is PUT.
191+ def put?
192+ @http . method == "PUT"
193+ end
194+
195+ # @returns [Boolean] Whether the HTTP request method is PATCH.
196+ def patch?
197+ @http . method == "PATCH"
198+ end
199+
200+ # @returns [Boolean] Whether the HTTP request method is DELETE.
201+ def delete?
202+ @http . method == "DELETE"
203+ end
204+
205+ # @returns [Boolean] Whether the HTTP request method is OPTIONS.
206+ def options?
207+ @http . method == "OPTIONS"
208+ end
169209
170210 # @returns [String] The full request path, including query string.
171211 def path
@@ -213,6 +253,29 @@ def cookies
213253 def host
214254 @http . authority || @http . headers [ "host" ]
215255 end
256+ alias host_with_port host
257+
258+ # @returns [String | Nil] The request URL scheme.
259+ def scheme
260+ @http . scheme
261+ end
262+
263+ # @returns [Boolean] Whether the request URL scheme is HTTPS.
264+ def ssl?
265+ self . scheme == "https"
266+ end
267+
268+ # @returns [String] The request base URL if scheme and host are available.
269+ def base_url
270+ scheme = self . scheme
271+ host = self . host
272+
273+ if scheme && host
274+ "#{ scheme } ://#{ host } "
275+ else
276+ ""
277+ end
278+ end
216279
217280 # @returns [String | Nil] The request user agent.
218281 def user_agent
@@ -223,6 +286,12 @@ def user_agent
223286 def referrer
224287 @http . headers [ "referer" ]
225288 end
289+ alias referer referrer
290+
291+ # @returns [Hash | Nil] The request session, if installed by Utopia::Session.
292+ def session
293+ @attributes [ "utopia.session" ] || @attributes [ "rack.session" ]
294+ end
226295
227296 # @returns [String | Nil] The remote peer address, if available.
228297 def ip
@@ -231,11 +300,10 @@ def ip
231300
232301 # @returns [String] The full request URL if scheme and host are available.
233302 def url
234- scheme = @http . scheme
235- host = self . host
303+ base_url = self . base_url
236304
237- if scheme && host
238- "#{ scheme } :// #{ host } #{ self . path } "
305+ if ! base_url . empty?
306+ "#{ base_url } #{ self . path } "
239307 else
240308 self . path
241309 end
0 commit comments