Class: Gloo::WebSvr::Request

Inherits:
Object
  • Object
show all
Defined in:
lib/gloo/web_svr/request.rb

Constant Summary collapse

REQUEST_METHOD =
'REQUEST_METHOD'.freeze
REQUEST_PATH =
'REQUEST_PATH'.freeze
HTTP_HOST =
'HTTP_HOST'.freeze
QUERY_STRING =
'QUERY_STRING'.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(engine, handler, env = nil) ⇒ Request

Set up the web server.



38
39
40
41
42
43
44
45
46
# File 'lib/gloo/web_svr/request.rb', line 38

def initialize( engine, handler, env = nil )
  @engine = engine
  @log = @engine.log

  @handler = handler

  @env = env
  detect_env
end

Instance Attribute Details

#bodyObject (readonly)

Returns the value of attribute body.



26
27
28
# File 'lib/gloo/web_svr/request.rb', line 26

def body
  @body
end

#dbObject (readonly)

Returns the value of attribute db.



27
28
29
# File 'lib/gloo/web_svr/request.rb', line 27

def db
  @db
end

#elapsedObject (readonly)

Returns the value of attribute elapsed.



27
28
29
# File 'lib/gloo/web_svr/request.rb', line 27

def elapsed
  @elapsed
end

#hostObject (readonly)

Returns the value of attribute host.



26
27
28
# File 'lib/gloo/web_svr/request.rb', line 26

def host
  @host
end

#idObject

Returns the value of attribute id.



28
29
30
# File 'lib/gloo/web_svr/request.rb', line 28

def id
  @id
end

#ipObject (readonly)

Returns the value of attribute ip.



26
27
28
# File 'lib/gloo/web_svr/request.rb', line 26

def ip
  @ip
end

#methodObject (readonly)

Returns the value of attribute method.



26
27
28
# File 'lib/gloo/web_svr/request.rb', line 26

def method
  @method
end

#pathObject (readonly)

Returns the value of attribute path.



26
27
28
# File 'lib/gloo/web_svr/request.rb', line 26

def path
  @path
end

#queryObject (readonly)

Returns the value of attribute query.



26
27
28
# File 'lib/gloo/web_svr/request.rb', line 26

def query
  @query
end

Instance Method Details

#body_paramsObject

Get the hash of body parameters.



151
152
153
# File 'lib/gloo/web_svr/request.rb', line 151

def body_params
  return @body ? @body : {}
end

#check_body_methodObject

Check the body to see if there is a PATCH or a PUT in the method override.



134
135
136
137
138
# File 'lib/gloo/web_svr/request.rb', line 134

def check_body_method
  if @body[ '_method' ]
    @method = @body[ '_method' ].upcase
  end
end

#detect_envObject

Write the request information to the log.



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/gloo/web_svr/request.rb', line 81

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

#finish_timerObject

Write the request completion time to the log.



118
119
120
121
122
123
# File 'lib/gloo/web_svr/request.rb', line 118

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

#logObject

Write the request information to the log.



158
159
160
161
162
# File 'lib/gloo/web_svr/request.rb', line 158

def log
  @log.info "#{@method} #{@host}#{@path}"
  @log.info "Parameters: #{@query}"
  @log.info "Body: #{@body}" unless @body.empty?
end

#processObject

Process the request and return a result.



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/gloo/web_svr/request.rb', line 56

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, page_obj = @handler.handle self
  finish_timer

  # Run the on_response script if there is one.
  @handler.server_obj.set_response_data( self, result, page_obj )
  @handler.server_obj.run_on_response

  return result
end

#query_paramsObject

Get the hash of query parameters.



143
144
145
146
# File 'lib/gloo/web_svr/request.rb', line 143

def query_params
  return {} unless @query
  return Rack::Utils.parse_query( @query )
end

#start_timerObject

Keep track of the request start time.



110
111
112
113
# File 'lib/gloo/web_svr/request.rb', line 110

def start_timer
  @start = Time.now
  @engine.running_app.reset_db_time
end