lib/gloo/web_svr/request.rb in gloo-3.1.1 vs lib/gloo/web_svr/request.rb in gloo-3.2.0

- old
+ new

@@ -17,11 +17,12 @@ 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 + attr_reader :method, :host, :path, :query, :body + attr_accessor :id # --------------------------------------------------------------------- # Initialization # --------------------------------------------------------------------- @@ -64,10 +65,14 @@ def detect_env @method = @env[ REQUEST_METHOD ] @path = @env[ REQUEST_PATH ] @host = @env[ HTTP_HOST ] @query = @env[ QUERY_STRING ] + + @body = @env[ 'rack.input' ].read + @body = Rack::Utils.parse_query @body + check_body_method end # --------------------------------------------------------------------- # Request timer @@ -76,31 +81,59 @@ # # 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) - @log.info "Web request complete. Elapsed time: #{@elapsed} ms" + 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