@@ -14,24 +14,22 @@ class HttpUtil
1414 */
1515 public static function parseHeaders (array $ headers ): array
1616 {
17- $ headerGroups = array ();
18- $ headerList = array ();
19-
2017 // Collect matching headers into groups
21- foreach ($ headers as $ line ) {
18+ foreach ($ headers as $ i => $ line ) {
2219 list ($ key , $ value ) = explode (': ' , $ line , 2 );
23- if (!isset ($ headerGroups [$ key ])) {
24- $ headerGroups [$ key ] = array ();
20+ if (isset ($ headers [$ key ])) {
21+ if (is_array ($ headers [$ key ])) {
22+ $ headers [$ key ][] = $ value ;
23+ } else {
24+ $ headers [$ key ] = array ($ headers [$ key ], $ value );
25+ }
26+ } else {
27+ $ headers [$ key ] = $ value ;
2528 }
26- $ headerGroups [$ key ][] = $ value ;
27- }
28-
29- // Collapse groups
30- foreach ($ headerGroups as $ key => $ values ) {
31- $ headerList [$ key ] = implode (', ' , $ values );
29+ unset($ headers [$ i ]);
3230 }
3331
34- return $ headerList ;
32+ return $ headers ;
3533 }
3634
3735 /**
@@ -66,7 +64,7 @@ public static function parseStatus(string $status): array
6664 public static function parseResponse (string $ response ): array
6765 {
6866 $ response = str_replace ("HTTP/1.1 100 Continue \r\n\r\n" , '' , $ response );
69-
67+
7068 list ($ rawHeader , $ rawBody ) = explode ("\r\n\r\n" , $ response , 2 );
7169
7270 // Parse headers and status.
@@ -90,15 +88,21 @@ public static function parseRawHeader(string $rawHeader): array
9088 /**
9189 * Returns a list of headers from a key/value paired array.
9290 *
93- * @param array<string,string> $headers Headers as key/value pairs.
91+ * @param array<string,string|array<string,string> > $headers Headers as key/value pairs.
9492 * @return string[] List of headers ['Content-Type: text/html', '...'].
9593 */
9694 public static function formatHeadersForCurl (array $ headers ): array
9795 {
9896 $ curlHeaders = array ();
9997
100- foreach ($ headers as $ key => $ value ) {
101- $ curlHeaders [] = $ key . ': ' . $ value ;
98+ foreach ($ headers as $ key => $ values ) {
99+ if (is_array ($ values )) {
100+ foreach ($ values as $ value ) {
101+ $ curlHeaders [] = $ key . ': ' . $ value ;
102+ }
103+ } else {
104+ $ curlHeaders [] = $ key . ': ' . $ values ;
105+ }
102106 }
103107
104108 return $ curlHeaders ;
0 commit comments