11module Goliath
2+ # Goliath::Request is responsible for processing a request and returning
3+ # the result back to the client.
4+ #
25 # @private
36 class Request
47 include EM ::Deferrable
@@ -31,6 +34,9 @@ def initialize(app, conn, env)
3134 # Invoked by connection when header parsing is complete.
3235 # This method is invoked only once per request.
3336 #
37+ # @param h [Hash] Request headers
38+ # @param parser [Http::Parser] The parsed used to parse the request
39+ # @return [Nil]
3440 def parse_header ( h , parser )
3541 h . each do |k , v |
3642 @env [ HTTP_PREFIX + k . gsub ( '-' , '_' ) . upcase ] = v
@@ -50,23 +56,29 @@ def parse_header(h, parser)
5056 # Invoked by connection when new body data is
5157 # parsed from the existing TCP stream.
5258 #
53- # Note: in theory, we can make this stream the
59+ # @note In theory, we can make this stream the
5460 # data into the processing step for async
5561 # uploads, etc. This would also require additional
5662 # callbacks for headers, etc.. Maybe something to
5763 # explore later.
5864 #
65+ # @param data [String] The received data
66+ # @return [Nil]
5967 def parse ( data )
6068 @body << data
6169 end
6270
71+ # Called to determine if the request has received all data from the client
72+ #
73+ # @return [Boolean] True if all data is received, false otherwise
6374 def finished?
6475 @state == :finished
6576 end
6677
6778 # Invoked by connection when upstream client
6879 # terminates the current TCP session.
6980 #
81+ # @return [Nil]
7082 def close
7183 @response . close rescue nil
7284 @env [ ASYNC_CLOSE ] . succeed if @env [ ASYNC_CLOSE ]
@@ -78,6 +90,7 @@ def close
7890 # an immediate response is served, or an async
7991 # response is indicated.
8092 #
93+ # @return [Nil]
8194 def process
8295 begin
8396 @state = :finished
@@ -105,6 +118,8 @@ def process
105118 # so the actual time to serve both requests in scenario
106119 # above, should be ~1s + data transfer time.
107120 #
121+ # @param results [Array] The status, headers and body to return to the client
122+ # @return [Nil]
108123 def post_process ( results )
109124 begin
110125 status , headers , body = results
@@ -131,24 +146,30 @@ def post_process(results)
131146
132147 private
133148
134- def server_exception ( e )
135- @env [ LOGGER ] . error ( "#{ e . message } \n #{ e . backtrace . join ( "\n " ) } " )
136- post_process ( [ 500 , { } , 'An error happened' ] )
137- end
149+ # Handles logging server exceptions
150+ #
151+ # @param e [Exception] The exception to log
152+ # @return [Nil]
153+ def server_exception ( e )
154+ @env [ LOGGER ] . error ( "#{ e . message } \n #{ e . backtrace . join ( "\n " ) } " )
155+ post_process ( [ 500 , { } , 'An error happened' ] )
156+ end
138157
139- def keep_alive
140- case @env [ HTTP_VERSION ]
141- # HTTP 1.1: all requests are persistent requests, client
142- # must send a Connection:close header to indicate otherwise
143- when '1.1' then
144- ( @env [ HTTP_PREFIX + CONNECTION ] . downcase != 'close' ) rescue true
145-
146- # HTTP 1.0: all requests are non keep-alive, client must
147- # send a Connection: Keep-Alive to indicate otherwise
148- when '1.0' then
149- ( @env [ HTTP_PREFIX + CONNECTION ] . downcase == 'keep-alive' ) rescue false
150- end
158+ # Used to determine if the connection should be kept open
159+ #
160+ # @return [Boolean] True to keep the connection open, false otherwise
161+ def keep_alive
162+ case @env [ HTTP_VERSION ]
163+ # HTTP 1.1: all requests are persistent requests, client
164+ # must send a Connection:close header to indicate otherwise
165+ when '1.1' then
166+ ( @env [ HTTP_PREFIX + CONNECTION ] . downcase != 'close' ) rescue true
167+
168+ # HTTP 1.0: all requests are non keep-alive, client must
169+ # send a Connection: Keep-Alive to indicate otherwise
170+ when '1.0' then
171+ ( @env [ HTTP_PREFIX + CONNECTION ] . downcase == 'keep-alive' ) rescue false
151172 end
152-
173+ end
153174 end
154175end
0 commit comments