@@ -86,6 +86,49 @@ def start_response(
8686 "HTTP_HOST" : url .netloc or "localhost" ,
8787 }
8888
89+ # Add incoming HTTP headers to environ (WSGI spec requires HTTP_ prefix)
90+ # Use cursor-based iteration to read all header names
91+ cursor = 0
92+ while True :
93+ header_names_str , next_cursor = req .get_header_names (
94+ max_len = 8192 , cursor = cursor
95+ )
96+ if not header_names_str :
97+ break
98+
99+ # Header names are NUL-separated
100+ header_names_split = (
101+ header_names_str .rstrip ("\0 " ).split ("\0 " ) if header_names_str else []
102+ )
103+
104+ for header_name in header_names_split :
105+ if not header_name :
106+ continue
107+
108+ # Get the header value
109+ header_value_bytes = req .get_header_value (header_name , max_len = 8192 )
110+ if header_value_bytes is None :
111+ continue
112+
113+ # See https://peps.python.org/pep-3333/ - ISO-8859-1 encoding
114+ # should be used for bytes values across this boundary.
115+ header_value_str = header_value_bytes .decode ("iso-8859-1" )
116+
117+ # Special handling for Content-Type and Content-Length (no HTTP_ prefix)
118+ if header_name .lower () == "content-type" :
119+ environ ["CONTENT_TYPE" ] = header_value_str
120+ elif header_name .lower () == "content-length" :
121+ environ ["CONTENT_LENGTH" ] = header_value_str
122+ else :
123+ # Convert to WSGI format: HTTP_ prefix, uppercase, hyphens to underscores
124+ wsgi_key = "HTTP_" + header_name .upper ().replace ("-" , "_" )
125+ environ [wsgi_key ] = header_value_str
126+
127+ # If there are more headers, continue with next cursor
128+ if next_cursor is None :
129+ break
130+ cursor = next_cursor
131+
89132 try :
90133 # Call the WSGI app and collect response body chunks
91134 for body_chunk in app (environ , start_response ):
0 commit comments