# Author:: Eric Crane (mailto:eric.crane@mac.com) # Copyright:: Copyright (c) 2024 Eric Crane. All rights reserved. # # A web Request for a page, action, or static resource. # # Kinds of Resources # Web Page # Action - does something and redirects to a page (or returns nothing) # API - returns JSON instead of HTML (but is that different from Web Page?) # Static Resource - File, PDF, Image, etc. # # # See More doc here: # https://www.rubydoc.info/gems/rack/Rack/Request/Helpers#path-instance_method # module Gloo module WebSvr class Request REQUEST_METHOD = 'REQUEST_METHOD'.freeze REQUEST_PATH = 'REQUEST_PATH'.freeze HTTP_HOST = 'HTTP_HOST'.freeze QUERY_STRING = 'QUERY_STRING'.freeze attr_reader :method, :host, :path, :query, :body, :ip attr_reader :db, :elapsed attr_accessor :id # --------------------------------------------------------------------- # Initialization # --------------------------------------------------------------------- # # Set up the web server. # def initialize( engine, handler, env = nil ) @engine = engine @log = @engine.log @handler = handler @env = env detect_env end # --------------------------------------------------------------------- # Process Request # --------------------------------------------------------------------- # # Process the request and return a result. # def process start_timer # Run the on_request script if there is one. @handler.server_obj.set_request_data self @handler.server_obj.run_on_request result = @handler.handle self finish_timer # Run the on_response script if there is one. @handler.server_obj.set_response_data self, result @handler.server_obj.run_on_response return result end # --------------------------------------------------------------------- # ENV # --------------------------------------------------------------------- # # Write the request information to the log. # def detect_env req = Rack::Request.new( @env ) @method = req.request_method @path = req.path @host = req.host_with_port @query = req.query_string @ip = req.ip # @method = @env[ REQUEST_METHOD ] # @path = @env[ REQUEST_PATH ] # @host = @env[ HTTP_HOST ] # @query = @env[ QUERY_STRING ] @handler.server_obj.session.set_session_data_for_request( @env ) @body = @env[ 'rack.input' ].read @body = Rack::Utils.parse_query @body check_body_method end # --------------------------------------------------------------------- # Request timer # --------------------------------------------------------------------- # # Keep track of the request start time. # def start_timer @start = Time.now @engine.running_app.reset_db_time end # # Write the request completion time to the log. # def finish_timer @finish = Time.now @elapsed = ( ( @finish - @start ) * 1000.0 ).round(2) @db = @engine.running_app.db_time @log.info "*** Web request complete. DB: #{@db} ms. Elapsed time: #{@elapsed} ms" end # --------------------------------------------------------------------- # Helper functions # --------------------------------------------------------------------- # # Check the body to see if there is a PATCH or a PUT in # the method override. # def check_body_method if @body[ '_method' ] @method = @body[ '_method' ].upcase end end # # Get the hash of query parameters. # def query_params return {} unless @query return Rack::Utils.parse_query( @query ) end # # Get the hash of body parameters. # def body_params return @body ? @body : {} end # # Write the request information to the log. # def log @log.info "#{@method} #{@host}#{@path}" @log.info "Parameters: #{@query}" @log.info "Body: #{@body}" unless @body.empty? end end end end