Browse Source

Close connection before throwing exception

chinpei215 10 years ago
parent
commit
5b05db69de
1 changed files with 7 additions and 1 deletions
  1. 7 1
      src/Network/Http/Adapter/Stream.php

+ 7 - 1
src/Network/Http/Adapter/Stream.php

@@ -265,6 +265,7 @@ class Stream
         $url = $request->url();
         $this->_open($url);
         $content = '';
+        $timedOut = false;
 
         while (!feof($this->_stream)) {
             if ($deadline !== false) {
@@ -275,12 +276,17 @@ class Stream
 
             $meta = stream_get_meta_data($this->_stream);
             if ($meta['timed_out'] || ($deadline !== false && time() > $deadline)) {
-                throw new Exception('Connection timed out ' . $url);
+                $timedOut = true;
+                break;
             }
         }
         $meta = stream_get_meta_data($this->_stream);
         fclose($this->_stream);
 
+        if ($timedOut) {
+            throw new Exception('Connection timed out ' . $url);
+        }
+
         $headers = $meta['wrapper_data'];
         if (isset($headers['headers']) && is_array($headers['headers'])) {
             $headers = $headers['headers'];