@@ -53,6 +53,8 @@ protected function process(Request $request)
5353 $ headers [] = "$ name: $ value " ;
5454 }
5555
56+ $ responseHeaders = NULL ;
57+
5658 $ softOptions = [
5759 CURLOPT_CONNECTTIMEOUT => 10 ,
5860 CURLOPT_SSL_VERIFYHOST => 2 ,
@@ -62,7 +64,6 @@ protected function process(Request $request)
6264
6365 $ hardOptions = [
6466 CURLOPT_FOLLOWLOCATION => FALSE , # Github sets the Location header for 201 code too and redirection is not required for us
65- CURLOPT_HEADER => TRUE ,
6667 CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1 ,
6768 CURLOPT_PROTOCOLS => CURLPROTO_HTTP | CURLPROTO_HTTPS ,
6869 CURLOPT_CUSTOMREQUEST => $ request ->getMethod (),
@@ -71,6 +72,19 @@ protected function process(Request $request)
7172 CURLOPT_HTTPHEADER => $ headers ,
7273 CURLOPT_RETURNTRANSFER => TRUE ,
7374 CURLOPT_POSTFIELDS => $ request ->getContent (),
75+ CURLOPT_HEADER => FALSE ,
76+ CURLOPT_HEADERFUNCTION => function ($ curl , $ line ) use (& $ responseHeaders ) {
77+ if ($ responseHeaders === NULL ) {
78+ $ responseHeaders = [];
79+ # and skip 1st line, it is HTTP code
80+
81+ } elseif ($ line !== "\r\n" ) {
82+ list ($ name , $ value ) = explode (': ' , $ line , 2 );
83+ $ responseHeaders [trim ($ name )] = trim ($ value );
84+ }
85+
86+ return strlen ($ line );
87+ },
7488 ];
7589
7690 if (!$ this ->curl ) {
@@ -85,31 +99,22 @@ protected function process(Request $request)
8599 throw new BadResponseException ('Setting cURL options failed: ' . curl_error ($ this ->curl ), curl_errno ($ this ->curl ));
86100 }
87101
88- $ result = curl_exec ($ this ->curl );
89- if ($ result === FALSE ) {
102+ $ responseHeaders = NULL ;
103+ $ content = curl_exec ($ this ->curl );
104+ if ($ content === FALSE ) {
90105 throw new BadResponseException (curl_error ($ this ->curl ), curl_errno ($ this ->curl ));
91106 }
92107
93- $ headersLength = curl_getinfo ($ this ->curl , CURLINFO_HEADER_SIZE );
94- if ($ headersLength === FALSE ) {
95- throw new BadResponseException (curl_error ($ this ->curl ), curl_errno ($ this ->curl ));
108+ if (!is_array ($ responseHeaders )) {
109+ throw new BadResponseException ('Response headers were not fetched. ' );
96110 }
97111
98112 $ code = curl_getinfo ($ this ->curl , CURLINFO_HTTP_CODE );
99113 if ($ code === FALSE ) {
100114 throw new BadResponseException ('HTTP status code is missing: ' . curl_error ($ this ->curl ), curl_errno ($ this ->curl ));
101115 }
102116
103- $ headersStr = trim (substr ($ result , 0 , $ headersLength ));
104- $ content = (string ) substr ($ result , $ headersLength );
105-
106- $ headers = [];
107- foreach (array_slice (explode ("\r\n" , $ headersStr ), 1 ) as $ header ) {
108- list ($ name , $ value ) = explode (': ' , $ header );
109- $ headers [$ name ] = $ value ;
110- }
111-
112- return new Response ($ code , $ headers , $ content );
117+ return new Response ($ code , $ responseHeaders , $ content );
113118 }
114119
115120}
0 commit comments