88import h2 .exceptions
99from h2 .settings import SettingCodes
1010
11+ from .headers import HeaderDict , sanitize_headers
1112from .status import Status
1213from .config import GRPCConfiguration
1314from .events import MessageReceived , RequestReceived , RequestEnded , ResponseReceived , ResponseEnded
@@ -75,13 +76,13 @@ def _request_received(self, event: h2.events.RequestReceived):
7576 if event .stream_ended :
7677 raise ProtocolError ("Stream ended before data was sent" )
7778 request = RequestReceived .parse_from_stream_id_and_headers_destructive (
78- event .stream_id , dict (event .headers ))
79+ event .stream_id , HeaderDict (event .headers ))
7980 self .message_read_buffers [event .stream_id ] = MessageReadBuffer (request .message_encoding ,
8081 self .config .max_message_length )
8182 return [request ]
8283
8384 def _response_received (self , event : h2 .events .ResponseReceived ):
84- headers = dict (event .headers )
85+ headers = HeaderDict (event .headers )
8586 response_received = ResponseReceived .parse_from_stream_id_and_headers_destructive (
8687 event .stream_id , headers )
8788 if event .stream_ended :
@@ -97,7 +98,7 @@ def _response_received(self, event: h2.events.ResponseReceived):
9798
9899 def _trailers_received (self , event : h2 .events .TrailersReceived ):
99100 response_ended = ResponseEnded .parse_from_stream_id_and_headers_destructive (
100- event .stream_id , dict (event .headers ))
101+ event .stream_id , HeaderDict (event .headers ))
101102 return [response_ended ]
102103
103104 def _informational_response_received (self , event : h2 .events .InformationalResponseReceived ):
@@ -228,7 +229,6 @@ def start_request(self, stream_id: int, scheme: str, service_name: str, method_n
228229 (":path" , "/{}/{}" .format (service_name , method_name )),
229230 ("te" , "trailers" ),
230231 ("content-type" , "application/grpc" + content_type_suffix ),
231- * custom_metadata
232232 ]
233233 if authority is not None :
234234 headers .insert (3 , (":authority" , authority ))
@@ -250,6 +250,7 @@ def start_request(self, stream_id: int, scheme: str, service_name: str, method_n
250250 headers .append (("grpc-accept-encoding" , self .config ._message_accept_encoding ))
251251 if self .config ._user_agent is not None :
252252 headers .append (("user-agent" , self .config ._user_agent ))
253+ headers .extend (sanitize_headers (custom_metadata ))
253254 self .h2_connection .send_headers (stream_id , headers , end_stream = False )
254255
255256 def end_request (self , stream_id : int ):
@@ -259,12 +260,12 @@ def start_response(self, stream_id: int, content_type_suffix="", custom_metadata
259260 headers = [
260261 (":status" , "200" ),
261262 ("content-type" , "application/grpc" + content_type_suffix ),
262- * custom_metadata ,
263263 ]
264264 if self .config ._message_encoding is not None :
265265 headers .append (("grpc-encoding" , self .config ._message_encoding ))
266266 if self .config ._message_accept_encoding is not None :
267267 headers .append (("grpc-accept-encoding" , self .config ._message_accept_encoding ))
268+ headers .extend (sanitize_headers (custom_metadata ))
268269 self .h2_connection .send_headers (stream_id , headers , end_stream = False )
269270
270271 def respond_status (self , stream_id : int , status : Status , content_type_suffix = "" ,
@@ -273,19 +274,19 @@ def respond_status(self, stream_id: int, status: Status, content_type_suffix="",
273274 (":status" , "200" ),
274275 ("content-type" , "application/grpc" + content_type_suffix ),
275276 ("grpc-status" , str (status .int_value )),
276- * custom_metadata ,
277277 ]
278278 if status .status_message :
279279 # TODO: should be percent encoded
280280 trailers .append (("grpc-message" , status .status_message ))
281+ trailers .extend (sanitize_headers (custom_metadata ))
281282 self .h2_connection .send_headers (stream_id , trailers , end_stream = True )
282283
283284 def end_response (self , stream_id : int , status : Status , custom_metadata = ()):
284285 trailers = [
285286 ("grpc-status" , str (status .int_value )),
286- * custom_metadata ,
287287 ]
288288 if status .status_message :
289289 # TODO: should be percent encoded
290290 trailers .append (("grpc-message" , status .status_message ))
291+ trailers .extend (sanitize_headers (custom_metadata ))
291292 self .h2_connection .send_headers (stream_id , trailers , end_stream = True )
0 commit comments