@@ -134,14 +134,11 @@ public function connect($uri)
134134
135135 // keep buffering data until headers are complete
136136 $ buffer = '' ;
137- $ fn = function ($ chunk ) use (&$ buffer , & $ fn , $ deferred , $ stream ) {
137+ $ fn = function ($ chunk ) use (&$ buffer , $ deferred , $ stream ) {
138138 $ buffer .= $ chunk ;
139139
140140 $ pos = strpos ($ buffer , "\r\n\r\n" );
141141 if ($ pos !== false ) {
142- // end of headers received => stop buffering
143- $ stream ->removeListener ('data ' , $ fn );
144-
145142 // try to parse headers as response message
146143 try {
147144 $ response = Psr7 \parse_response (substr ($ buffer , 0 , $ pos ));
@@ -191,7 +188,11 @@ public function connect($uri)
191188
192189 $ stream ->write ("CONNECT " . $ host . ": " . $ port . " HTTP/1.1 \r\nHost: " . $ host . ": " . $ port . "\r\n" . $ auth . "\r\n" );
193190
194- return $ deferred ->promise ();
191+ return $ deferred ->promise ()->then (function (ConnectionInterface $ stream ) use ($ fn ) {
192+ // Stop buffering when connection has been established.
193+ $ stream ->removeListener ('data ' , $ fn );
194+ return new Promise \FulfilledPromise ($ stream );
195+ });
195196 }, function (Exception $ e ) use ($ proxyUri ) {
196197 throw new RuntimeException ('Unable to connect to proxy (ECONNREFUSED) ' , defined ('SOCKET_ECONNREFUSED ' ) ? SOCKET_ECONNREFUSED : 111 , $ e );
197198 });
0 commit comments