@@ -258,17 +258,25 @@ const char *req_headers(request_t *req)
258258{
259259 if (req && req -> hm )
260260 {
261- static char headers_buf [1024 ];
261+ static char headers_buf [4096 ];
262262 headers_buf [0 ] = '\0' ;
263-
264- // Extract some common headers
265- struct mg_str * content_type = mg_http_get_header (req -> hm , "Content-Type" );
266- if (content_type )
267- {
268- strncat (headers_buf , "Content-Type: " , sizeof (headers_buf ) - strlen (headers_buf ) - 1 );
269- strncat (headers_buf , content_type -> buf ,
270- content_type -> len < (sizeof (headers_buf ) - strlen (headers_buf ) - 1 ) ? content_type -> len : (sizeof (headers_buf ) - strlen (headers_buf ) - 1 ));
271- strncat (headers_buf , "\r\n" , sizeof (headers_buf ) - strlen (headers_buf ) - 1 );
263+ size_t buf_len = 0 ;
264+ size_t buf_cap = sizeof (headers_buf );
265+
266+ for (int i = 0 ; i < MG_MAX_HTTP_HEADERS ; i ++ ) {
267+ struct mg_http_header * h = & req -> hm -> headers [i ];
268+ if (h -> name .len == 0 ) break ;
269+
270+ // Check if we have enough space: name.len + 2 (": ") + value.len + 2 ("\r\n") + 1 (null terminator)
271+ size_t needed = h -> name .len + 2 + h -> value .len + 2 + 1 ;
272+ if (buf_len + needed > buf_cap ) break ;
273+
274+ strncat (headers_buf , h -> name .buf , h -> name .len );
275+ strcat (headers_buf , ": " );
276+ strncat (headers_buf , h -> value .buf , h -> value .len );
277+ strcat (headers_buf , "\r\n" );
278+
279+ buf_len += needed - 1 ; // -1 because null terminator is overwritten/handled by strcat
272280 }
273281
274282 return headers_buf ;
0 commit comments