@@ -134,14 +134,11 @@ public function connect($uri)
134
134
135
135
// keep buffering data until headers are complete
136
136
$ buffer = '' ;
137
- $ fn = function ($ chunk ) use (&$ buffer , & $ fn , $ deferred , $ stream ) {
137
+ $ fn = function ($ chunk ) use (&$ buffer , $ deferred , $ stream ) {
138
138
$ buffer .= $ chunk ;
139
139
140
140
$ pos = strpos ($ buffer , "\r\n\r\n" );
141
141
if ($ pos !== false ) {
142
- // end of headers received => stop buffering
143
- $ stream ->removeListener ('data ' , $ fn );
144
-
145
142
// try to parse headers as response message
146
143
try {
147
144
$ response = Psr7 \parse_response (substr ($ buffer , 0 , $ pos ));
@@ -191,7 +188,11 @@ public function connect($uri)
191
188
192
189
$ stream ->write ("CONNECT " . $ host . ": " . $ port . " HTTP/1.1 \r\nHost: " . $ host . ": " . $ port . "\r\n" . $ auth . "\r\n" );
193
190
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
+ });
195
196
}, function (Exception $ e ) use ($ proxyUri ) {
196
197
throw new RuntimeException ('Unable to connect to proxy (ECONNREFUSED) ' , defined ('SOCKET_ECONNREFUSED ' ) ? SOCKET_ECONNREFUSED : 111 , $ e );
197
198
});
0 commit comments