@@ -93,25 +93,22 @@ public function reset():void{
9393 }
9494
9595 /**
96- * @return \Psr\Http\Message\ResponseInterface
96+ * @return array
9797 */
98- public function init (){
99-
100- $ v = $ this ->request ->getProtocolVersion ();
98+ protected function initCurlOptions ():array {
10199
102- switch ($ v ){
103- case '1.0 ' : $ v = CURL_HTTP_VERSION_1_0 ; break ;
104- case '1.1 ' : $ v = CURL_HTTP_VERSION_1_1 ; break ;
105- case '2.0 ' : $ v = CURL_HTTP_VERSION_2_0 ; break ;
106- default : $ v = CURL_HTTP_VERSION_NONE ;
107- }
100+ $ v = [
101+ '1.0 ' => CURL_HTTP_VERSION_1_0 ,
102+ '1.1 ' => CURL_HTTP_VERSION_1_1 ,
103+ '2.0 ' => CURL_HTTP_VERSION_2_0 ,
104+ ];
108105
109- $ options = [
106+ return [
110107 CURLOPT_HEADER => false ,
111108 CURLOPT_RETURNTRANSFER => false ,
112109 CURLOPT_FOLLOWLOCATION => false ,
113110 CURLOPT_URL => (string )$ this ->request ->getUri ()->withFragment ('' ),
114- CURLOPT_HTTP_VERSION => $ v ,
111+ CURLOPT_HTTP_VERSION => $ v[ $ this -> request -> getProtocolVersion ()] ?? CURL_HTTP_VERSION_NONE ,
115112 CURLOPT_USERAGENT => $ this ->options ->user_agent ,
116113 CURLOPT_PROTOCOLS => CURLPROTO_HTTP | CURLPROTO_HTTPS ,
117114 CURLOPT_SSL_VERIFYPEER => true ,
@@ -122,59 +119,50 @@ public function init(){
122119 CURLOPT_WRITEFUNCTION => [$ this , 'writefunction ' ],
123120 CURLOPT_HEADERFUNCTION => [$ this , 'headerfunction ' ],
124121 ];
122+ }
125123
126- $ userinfo = $ this ->request ->getUri ()->getUserInfo ();
127-
128- if (!empty ($ userinfo )){
129- $ options [CURLOPT_USERPWD ] = $ userinfo ;
130- }
131-
132- /*
133- * Some HTTP methods cannot have payload:
134- *
135- * - GET — cURL will automatically change method to PUT or POST
136- * if we set CURLOPT_UPLOAD or CURLOPT_POSTFIELDS.
137- * - HEAD — cURL treats HEAD as GET request with a same restrictions.
138- * - TRACE — According to RFC7231: a client MUST NOT send a message body in a TRACE request.
139- */
140- $ method = $ this ->request ->getMethod ();
124+ /**
125+ * @param array $options
126+ *
127+ * @return void
128+ */
129+ protected function setBodyOptions (array &$ options ):void {
141130 $ body = $ this ->request ->getBody ();
142131 $ bodySize = $ body ->getSize ();
143132
144- if (in_array ($ method , ['DELETE ' , 'PATCH ' , 'POST ' , 'PUT ' ], true ) && $ bodySize !== 0 ){
145-
146- if ($ body ->isSeekable ()){
147- $ body ->rewind ();
148- }
133+ if ($ bodySize === 0 ){
134+ return ;
135+ }
149136
150- // Message has non empty body.
151- if ($ bodySize === null || $ bodySize > 1 << 20 ){
152- // Avoid full loading large or unknown size body into memory
153- $ options [CURLOPT_UPLOAD ] = true ;
137+ if ($ body ->isSeekable ()){
138+ $ body ->rewind ();
139+ }
154140
155- if ($ bodySize !== null ){
156- $ options [CURLOPT_INFILESIZE ] = $ bodySize ;
157- }
141+ // Message has non empty body.
142+ if ($ bodySize === null || $ bodySize > 1 << 20 ){
143+ // Avoid full loading large or unknown size body into memory
144+ $ options [CURLOPT_UPLOAD ] = true ;
158145
159- $ options [CURLOPT_READFUNCTION ] = [$ this , 'readfunction ' ];
160- }
161- // Small body can be loaded into memory
162- else {
163- $ options [CURLOPT_POSTFIELDS ] = (string )$ body ;
146+ if ($ bodySize !== null ){
147+ $ options [CURLOPT_INFILESIZE ] = $ bodySize ;
164148 }
165149
150+ $ options [CURLOPT_READFUNCTION ] = [$ this , 'readfunction ' ];
166151 }
167-
168- // This will set HTTP method to "HEAD".
169- if ($ method === 'HEAD ' ){
170- $ options [CURLOPT_NOBODY ] = true ;
171- }
172- // GET is a default method. Other methods should be specified explicitly.
173- elseif ($ method !== 'GET ' ){
174- $ options [CURLOPT_CUSTOMREQUEST ] = $ method ;
152+ // Small body can be loaded into memory
153+ else {
154+ $ options [CURLOPT_POSTFIELDS ] = (string )$ body ;
175155 }
176156
177- $ curlHeaders = [];
157+ }
158+
159+ /**
160+ * @param array $options
161+ *
162+ * @return array
163+ */
164+ protected function initCurlHeaders (array $ options ):array {
165+ $ headers = [];
178166
179167 foreach ($ this ->request ->getHeaders () as $ name => $ values ){
180168 $ header = strtolower ($ name );
@@ -202,22 +190,59 @@ public function init(){
202190
203191 // cURL requires a special format for empty headers.
204192 // See https://github.com/guzzle/guzzle/issues/1882 for more details.
205- $ curlHeaders [] = $ value === ''
206- ? $ name .'; '
207- : $ name .': ' .$ value ;
193+ $ headers [] = $ value === '' ? $ name .'; ' : $ name .': ' .$ value ;
208194 }
209195
210196 }
211197
212- $ options [CURLOPT_HTTPHEADER ] = $ curlHeaders ;
198+ return $ headers ;
199+ }
200+
201+ /**
202+ * @return resource cURL handle
203+ */
204+ public function init (){
205+ $ options = $ this ->initCurlOptions ();
206+
207+ $ userinfo = $ this ->request ->getUri ()->getUserInfo ();
208+
209+ if (!empty ($ userinfo )){
210+ $ options [CURLOPT_USERPWD ] = $ userinfo ;
211+ }
212+
213+ /*
214+ * Some HTTP methods cannot have payload:
215+ *
216+ * - GET — cURL will automatically change method to PUT or POST
217+ * if we set CURLOPT_UPLOAD or CURLOPT_POSTFIELDS.
218+ * - HEAD — cURL treats HEAD as GET request with a same restrictions.
219+ * - TRACE — According to RFC7231: a client MUST NOT send a message body in a TRACE request.
220+ */
221+ $ method = $ this ->request ->getMethod ();
222+
223+ if (in_array ($ method , ['DELETE ' , 'PATCH ' , 'POST ' , 'PUT ' ], true )){
224+ $ this ->setBodyOptions ($ options );
225+ }
226+
227+ // This will set HTTP method to "HEAD".
228+ if ($ method === 'HEAD ' ){
229+ $ options [CURLOPT_NOBODY ] = true ;
230+ }
231+
232+ // GET is a default method. Other methods should be specified explicitly.
233+ if ($ method !== 'GET ' ){
234+ $ options [CURLOPT_CUSTOMREQUEST ] = $ method ;
235+ }
236+
237+ $ options [CURLOPT_HTTPHEADER ] = $ this ->initCurlHeaders ($ options );
213238
214239 // If the Expect header is not present, prevent curl from adding it
215- if (!$ this ->request ->hasHeader ('Expect ' )) {
240+ if (!$ this ->request ->hasHeader ('Expect ' )){
216241 $ options [CURLOPT_HTTPHEADER ][] = 'Expect: ' ;
217242 }
218243
219244 // cURL sometimes adds a content-type by default. Prevent this.
220- if (!$ this ->request ->hasHeader ('Content-Type ' )) {
245+ if (!$ this ->request ->hasHeader ('Content-Type ' )){
221246 $ options [CURLOPT_HTTPHEADER ][] = 'Content-Type: ' ;
222247 }
223248
0 commit comments