MCPcopy
hub / github.com/benoitc/gunicorn / handle_http2_request

Method handle_http2_request

gunicorn/workers/base_async.py:144–197  ·  view source on GitHub ↗

Handle a single HTTP/2 request.

(self, listener_name, req, sock, addr, h2_conn)

Source from the content-addressed store, hash-verified

142 self.log.exception("HTTP/2 connection error: %s", e)
143
144 def handle_http2_request(self, listener_name, req, sock, addr, h2_conn):
145 """Handle a single HTTP/2 request."""
146 stream_id = req.stream.stream_id
147 request_start = datetime.now()
148 environ = {}
149 resp = None
150
151 try:
152 self.cfg.pre_request(self, req)
153 resp, environ = wsgi.create(req, sock, addr, listener_name, self.cfg)
154 environ["wsgi.multithread"] = True
155 environ["HTTP_VERSION"] = "2"
156
157 self.nr += 1
158 if self.nr >= self.max_requests:
159 if self.alive:
160 self.log.info("Autorestarting worker after current request.")
161 self.alive = False
162
163 # Run WSGI app
164 respiter = self.wsgi(environ, resp.start_response)
165 if self.is_already_handled(respiter):
166 return
167
168 # Collect response body
169 response_body = b''
170 try:
171 if hasattr(respiter, '__iter__'):
172 for item in respiter:
173 if item:
174 response_body += item
175 finally:
176 if hasattr(respiter, "close"):
177 respiter.close()
178
179 # Send response via HTTP/2
180 h2_conn.send_response(
181 stream_id,
182 resp.status_code,
183 resp.headers,
184 response_body
185 )
186
187 request_time = datetime.now() - request_start
188 self.log.access(resp, req, environ, request_time)
189
190 except Exception:
191 self.log.exception("Error handling HTTP/2 request")
192 raise
193 finally:
194 try:
195 self.cfg.post_request(self, req, environ, resp)
196 except Exception:
197 self.log.exception("Exception in post_request hook")
198
199 def handle_request(self, listener_name, req, sock, addr):
200 request_start = datetime.now()

Callers 1

handle_http2Method · 0.95

Calls 11

is_already_handledMethod · 0.95
nowMethod · 0.80
pre_requestMethod · 0.80
createMethod · 0.80
infoMethod · 0.45
wsgiMethod · 0.45
closeMethod · 0.45
send_responseMethod · 0.45
accessMethod · 0.45
exceptionMethod · 0.45
post_requestMethod · 0.45

Tested by

no test coverage detected