release dev10
authorMichael Wallner <mike@php.net>
Fri, 30 Mar 2012 13:21:08 +0000 (13:21 +0000)
committerMichael Wallner <mike@php.net>
Fri, 30 Mar 2012 13:21:08 +0000 (13:21 +0000)
package.xml
php_http_curl_client.c
phpunit/RequestTest.php
tests/factory.phpt
tests/message001.phpt
tests/persistenthandles001.phpt
tests/requestpool001.phpt

index 5658d3a73e7dedc4b73885f17f576041ed7814bc..e8c672e121cd66325fba978c787e400f81eb91fc 100644 (file)
@@ -18,8 +18,7 @@ Extended HTTP support. Again. Keep in mind that it's got the major version 2, be
 * The utterly misunderstood HttpResponse class has been reimplemented as http\Env\Response inheriting http\Message.
 * Currently, there's only one Exception class left, http\Exception.
 * Errors triggered by the extension can be configured statically by http\Object::$defaultErrorHandling or inherited http\Object->errorHandling.
-* The request ecosystem has been modularized to support different libraries, though for the moment only libcurl is supported;
-  Nevertheless, you have to use the http\Request\Factory to create your request/pool/datashare objects.
+* The request ecosystem has been modularized to support different libraries, though for the moment only libcurl is supported.
 ]]></description>
  <lead>
   <name>Michael Wallner</name>
@@ -27,9 +26,9 @@ Extended HTTP support. Again. Keep in mind that it's got the major version 2, be
   <email>mike@php.net</email>
   <active>yes</active>
  </lead>
- <date>2012-03-23</date>
+ <date>2012-03-30</date>
  <version>
-  <release>2.0.0dev9</release>
+  <release>2.0.0dev10</release>
   <api>2.0.0</api>
  </version>
  <stability>
@@ -38,7 +37,7 @@ Extended HTTP support. Again. Keep in mind that it's got the major version 2, be
  </stability>
  <license>BSD, revised</license>
  <notes><![CDATA[
-+ 
++ This release contains the http\Request to http\Client refactoring triggered by Benjamin Eberlei. Many thanks.
 ]]></notes>
  <contents>
   <dir name="/">
index a285397fb124613cee728fea306b8343dcb8c790..a2e0d3fc4ceb07eb9524b0a49182e53ddf1a8032 100644 (file)
@@ -1026,6 +1026,7 @@ static STATUS php_http_curl_client_reset(php_http_client_t *h)
 
 STATUS php_http_curl_client_prepare(php_http_client_t *h, php_http_message_t *msg)
 {
+       size_t body_size;
        php_http_curl_client_t *curl = h->ctx;
        php_http_curl_client_storage_t *storage = get_storage(curl->handle);
        TSRMLS_FETCH_FROM_CTX(h->ts);
@@ -1096,15 +1097,14 @@ STATUS php_http_curl_client_prepare(php_http_client_t *h, php_http_message_t *ms
        }
 
        /* attach request body */
-       {
+       if ((body_size = php_http_message_body_size(&msg->body))) {
                /* RFC2616, section 4.3 (para. 4) states that »a message-body MUST NOT be included in a request if the
                 * specification of the request method (section 5.1.1) does not allow sending an entity-body in request.«
                 * Following the clause in section 5.1.1 (para. 2) that request methods »MUST be implemented with the
                 * same semantics as those specified in section 9« reveal that not any single defined HTTP/1.1 method
                 * does not allow a request body.
                 */
-               size_t body_size = php_http_message_body_size(&msg->body);
-
+               php_stream_rewind(php_http_message_body_stream(&msg->body));
                curl_easy_setopt(curl->handle, CURLOPT_IOCTLDATA, &msg->body);
                curl_easy_setopt(curl->handle, CURLOPT_READDATA, &msg->body);
                curl_easy_setopt(curl->handle, CURLOPT_INFILESIZE, body_size);
index fccad01edfec3b93feda3119c8f5c9f6ff9e08c8..56053ee148d5a487db4ee7d091a5a1f20874ac88 100644 (file)
@@ -28,7 +28,7 @@ class RequestTest extends PHPUnit_Framework_TestCase
     protected $r;
 
     function setUp() {
-        $this->r = (new http\Request\Factory)->createRequest();
+        $this->r = (new http\Client\Factory)->createClient();
         $this->r->setOptions(
             array(
                 "connecttimeout"    => 30,
@@ -38,10 +38,8 @@ class RequestTest extends PHPUnit_Framework_TestCase
     }
 
     function testClone() {
-        $this->r->setUrl("http://dev.iworks.at/ext-http/.print_request.php");
         $c = clone $this->r;
-        $c->setUrl("http://dev.iworks.at/ext-http/.print_headers.php");
-        $this->assertNotEquals($this->r->send(), $c->send());
+        $this->assertNotSame($this->r, $c);
     }
 
     function testObserver() {
@@ -61,51 +59,52 @@ class RequestTest extends PHPUnit_Framework_TestCase
                 }
             )
         );
-        $this->r->setUrl("http://dev.iworks.at/ext-http/")->send();
+        $this->r->setRequest(new http\Client\Request("GET", "http://dev.iworks.at/ext-http/"))->send(null);
         $this->assertRegexp("/(\.-)+/", $this->r->pi);
         $this->assertCount(3, $this->r->getObservers());
     }
 
     function testCookies() {
-        $this->r->setUrl("http://dev.iworks.at/ext-http/.cookie.php")->send();
+        $this->r->setRequest(new http\Client\Request("GET", "http://dev.iworks.at/ext-http/.cookie.php"))->send(null);
         $this->assertNotContains("Cookie", (string) $this->r->getRequestMessage());
-        $this->r->send();
+        $this->r->send(null);
         $this->assertNotContains("Cookie", (string) $this->r->getRequestMessage());
-        $this->r->enableCookies()->send();
+        $this->r->enableCookies()->send(null);
         $this->assertNotContains("Cookie", (string) $this->r->getRequestMessage());
-        $this->r->send();
+        $this->r->send(null);
         $this->assertContains("Cookie", (string) $this->r->getRequestMessage());
-        $this->assertCount(2, $this->r->getResponseCookies());
+        $this->assertCount(2, $this->r->getResponseMessage()->getCookies());
     }
 
     function testResetCookies() {
-        $this->r->setUrl("http://dev.iworks.at/ext-http/.cookie.php");
+        $this->r->setRequest(new http\Client\Request("GET", "http://dev.iworks.at/ext-http/.cookie.php"));
 
         $this->r->enableCookies();
-        $this->r->send();
+        $this->r->send(null);
 
         $f = function ($a) { return $a->getCookies(); };
-        $c = array_map($f, $this->r->getResponseCookies());
+        $c = array_map($f, $this->r->getResponseMessage()->getCookies());
 
-        $this->r->send();
-        $this->assertEquals($c, array_map($f, $this->r->getResponseCookies()));
+        $this->r->send(null);
+        $this->assertEquals($c, array_map($f, $this->r->getResponseMessage()->getCookies()));
         
         $this->r->resetCookies();
-        $this->r->send();
-        $this->assertNotEquals($c, array_map($f, $this->r->getResponseCookies()));
+        $this->r->send(null);
+        $this->assertNotEquals($c, array_map($f, $this->r->getResponseMessage()->getCookies()));
     }
 
     function testHistory() {
         $body = new http\Message\Body;
         $body->append("foobar");
-        $this->r->setBody($body);
 
-        $this->r->recordHistory = true;
+        $request = new http\Client\Request;
+        $request->setBody($body);
+        $request->setRequestMethod("POST");
+        $request->setRequestUrl("http://dev.iworks.at/ext-http/.print_request.php");
 
-        $this->r->setMethod("POST");
-        $this->r->setUrl("http://dev.iworks.at/ext-http/.print_request.php");
+        $this->r->recordHistory = true;
+        $this->r->send($request);
 
-        $this->r->send();
         $this->assertStringMatchesFormat(<<<HTTP
 POST /ext-http/.print_request.php HTTP/1.1
 User-Agent: %s
@@ -130,7 +129,9 @@ HTTP
         , str_replace("\r", "", $this->r->getHistory()->toString(true))
         );
 
-        $this->r->send();
+
+        $this->r->send($request);
+
         $this->assertStringMatchesFormat(<<<HTTP
 POST /ext-http/.print_request.php HTTP/1.1
 User-Agent: %s
index d80bc82ef43afaaa2cf0d5a78ab38f80114d1c4c..4267e3650308c63fa0e5623aa97ee5e6630f7a56 100644 (file)
@@ -3,28 +3,28 @@ factory
 --SKIPIF--
 <?php
 include "skipif.inc";
-in_array("curl", http\Request\Factory::getAvailableDrivers()) or die ("skip CURL support missing");
+in_array("curl", http\Client\Factory::getAvailableDrivers()) or die ("skip CURL support missing");
 ?>
 --FILE--
 <?php
 echo "Test\n";
 
-class MyRequest extends http\Request {}
-class MyPool extends http\Request\Pool {}
-class MyShare extends http\Request\DataShare {}
+class MyClient extends http\Curl\Client {}
+class MyPool extends http\Curl\Client\Pool {}
+class MyShare extends http\Curl\Client\DataShare {}
   
-class MyFactory extends http\Request\Factory {
+class MyFactory extends http\Client\Factory {
        protected $driver = "curl";
        protected $persistentHandleId = "My";
-       protected $requestClass = "MyRequest";
-       protected $requestPoolClass = "MyPool";
-       protected $requestDataShareClass = "MyShare";
+       protected $clientClass = "MyClient";
+       protected $clientPoolClass = "MyPool";
+       protected $clientDataShareClass = "MyShare";
        
        protected $dummy = "foo";
 }
 
 $f = new MyFactory(array("driver" => "curl"));
-$r = $f->createRequest();
+$r = $f->createClient();
 $p = $f->createPool();
 $s = $f->createDataShare();
 
@@ -33,9 +33,9 @@ var_dump(
        $f->getDriver()
 );
 
-foreach (array("Request", "Pool", "DataShare") as $type) {
+foreach (array("Client", "Pool", "DataShare") as $type) {
        try {
-               var_dump((new http\Request\Factory(array("driver" => "nonexistant")))->{"create$type"}());
+               var_dump((new http\Client\Factory(array("driver" => "nonexistant")))->{"create$type"}());
        } catch (Exception $e) {
                echo $e->getMessage(), "\n";
        }
@@ -49,14 +49,14 @@ array(4) {
   [0]=>
   string(9) "MyFactory"
   [1]=>
-  string(9) "MyRequest"
+  string(8) "MyClient"
   [2]=>
   string(6) "MyPool"
   [3]=>
   string(7) "MyShare"
 }
 string(4) "curl"
-requests are not supported by this driver
+clients are not supported by this driver
 pools are not supported by this driver
 datashares are not supported by this driver
 Done
index aa01e6d53e0302deb62831272d9f33ef5c25dc17..e0eeddda1f4cf13f578d31ee4dfd0af652208b89 100644 (file)
@@ -90,7 +90,7 @@ echo $m->getParentMessage();
 echo "Done\n";
 --EXPECTF--
 Test
-string(3) "0.0"
+string(3) "1.1"
 bool(true)
 array(0) {
 }
index dd7b8c7047c56fe5974c204f5436ca4521aaa871..01f5069bea86b75e511a9aaa3f09d3e7838b89f5 100644 (file)
@@ -4,32 +4,26 @@ persistent handles
 <?php include "skipif.inc"; ?>
 --FILE--
 <?php
-(new http\Request\Factory(array("persistentHandleId" => "foo")))
-    ->createRequest("http://dev.iworks.at")
+(new http\Client\Factory(array("persistentHandleId" => "foo")))
+    ->createClient()->setRequest(new http\Client\Request("GET", "http://dev.iworks.at"))
     ->setOptions(array("connecttimeout"=> 90, "timeout" =>300))
-    ->send(); 
-$r = (new http\Request\Factory(array("persistentHandleId" => "bar")))
-    ->createRequest("http://dev.iworks.at")
+    ->send(null); 
+$r = (new http\Client\Factory(array("persistentHandleId" => "bar")))
+    ->createClient()->setRequest(new http\Client\Request("GET", "http://dev.iworks.at"))
     ->setOptions(array("connecttimeout"=> 90, "timeout" =>300));
     
 var_dump(http\Env::statPersistentHandles()); 
 http\Env::cleanPersistentHandles(); 
 var_dump(http\Env::statPersistentHandles());
 
-$r->send();
+$r->send(null);
 
 var_dump(http\Env::statPersistentHandles());
 ?>
 DONE
 --EXPECTF--
 object(stdClass)#%d (3) {
-  ["http_request_datashare.curl"]=>
-  array(0) {
-  }
-  ["http_request_pool.curl"]=>
-  array(0) {
-  }
-  ["http_request.curl"]=>
+  ["http_client.curl"]=>
   array(2) {
     ["foo"]=>
     array(2) {
@@ -46,15 +40,15 @@ object(stdClass)#%d (3) {
       int(0)
     }
   }
-}
-object(stdClass)#%d (3) {
-  ["http_request_datashare.curl"]=>
+  ["http_client_pool.curl"]=>
   array(0) {
   }
-  ["http_request_pool.curl"]=>
+  ["http_client_datashare.curl"]=>
   array(0) {
   }
-  ["http_request.curl"]=>
+}
+object(stdClass)#%d (3) {
+  ["http_client.curl"]=>
   array(1) {
     ["bar"]=>
     array(2) {
@@ -64,15 +58,15 @@ object(stdClass)#%d (3) {
       int(0)
     }
   }
-}
-object(stdClass)#%d (3) {
-  ["http_request_datashare.curl"]=>
+  ["http_client_pool.curl"]=>
   array(0) {
   }
-  ["http_request_pool.curl"]=>
+  ["http_client_datashare.curl"]=>
   array(0) {
   }
-  ["http_request.curl"]=>
+}
+object(stdClass)#%d (3) {
+  ["http_client.curl"]=>
   array(1) {
     ["bar"]=>
     array(2) {
@@ -82,5 +76,11 @@ object(stdClass)#%d (3) {
       int(0)
     }
   }
+  ["http_client_pool.curl"]=>
+  array(0) {
+  }
+  ["http_client_datashare.curl"]=>
+  array(0) {
+  }
 }
 DONE
index 790823c7310fac3d4f1715079a44653976b1489c..ff91a406801c2a2037c90256dc4969b851bbc4cc 100644 (file)
@@ -7,18 +7,13 @@ include 'skipif.inc';
 --FILE--
 <?php
 
-use http\request\Factory as HttpRequestFactory;
-use http\request\Pool as HttpRequestPool;
-use http\Exception as HttpRequestException;
-use http\Exception as HttpSocketException;
-
 echo "-TEST\n";
 
 set_time_limit(0);
 ini_set('error_reporting', E_ALL);
 ini_set('html_errors', 0);
 
-class Pool extends HttpRequestPool
+class Pool extends \http\Curl\Client\Pool
 {
        private $all;
        private $rem;
@@ -50,9 +45,7 @@ class Pool extends HttpRequestPool
                
                foreach ($now as $url => $file) {
                        $this->attach(
-                               $this->factory->createRequest(
-                                       $url,
-                                       "GET",
+                               $this->factory->createClient(
                                        array(
                                                'redirect'      => 5,
                                                'compress'  => GZIP,
@@ -60,13 +53,13 @@ class Pool extends HttpRequestPool
                                                'connecttimeout' => TOUT,
                                                'lastmodified' => is_file($file)?filemtime($file):0
                                        )
-                               )
+                               )->setRequest(new http\Client\Request("GET", $url))
                        );
                }
                
                while ($this->once()) {
                        if (!$this->wait()) {
-                               throw new HttpSocketException;
+                               throw new http\Exception;
                        }
                }
        }
@@ -75,19 +68,19 @@ class Pool extends HttpRequestPool
        {
                try {
                        $rc = parent::once();
-               } catch (HttpRequestException $x) {
+               } catch (http\Exception $x) {
                        // a request may have thrown an exception,
                        // but it is still save to continue
                        echo $x->getMessage(), "\n";
                }
                
-               foreach ($this->getFinishedRequests() as $r) {
+               foreach ($this->getFinished() as $r) {
                        $this->detach($r);
                        
-                       $u = $r->getUrl();
-                       $c = $r->getResponseCode();
+                       $u = $r->getRequest()->getRequestUrl();
+                       $c = $r->getResponseMessage()->getResponseCode();
             try {
-                       $b = $r->getResponseBody();
+                       $b = $r->getResponseMessage()->getBody();
             } catch (\Exception $e) {
                 echo $e->getMessage(), "\n";
                 $b = "";
@@ -102,9 +95,7 @@ class Pool extends HttpRequestPool
                        if ($a = each($this->rem)) {
                                list($url, $file) = $a;
                                $this->attach(
-                                       $this->factory->createRequest(
-                                               $url,
-                                               "GET",
+                                       $this->factory->createClient(
                                                array(
                                                        'redirect'      => 5,
                                                        'compress'      => GZIP,
@@ -112,7 +103,7 @@ class Pool extends HttpRequestPool
                                                        'connecttimeout' => TOUT,
                                                        'lastmodified' => is_file($file)?filemtime($file):0
                                                )
-                                       )
+                                       )->setRequest(new http\Client\Request("GET", $url))
                                );
                        }
                }
@@ -126,7 +117,7 @@ define('RMAX', 10);
 chdir(__DIR__);
 
 $time = microtime(true);
-$factory = new HttpRequestFactory(array("driver" => "curl", "requestPoolClass" => "Pool"));
+$factory = new http\Client\Factory(array("driver" => "curl", "clientPoolClass" => "Pool"));
 $factory->createPool()->run($factory);
 printf("Elapsed: %0.3fs\n", microtime(true)-$time);