Merge branch 'MDL-42951-master' of git://github.com/FMCorz/moodle
authorSam Hemelryk <sam@moodle.com>
Sun, 1 Dec 2013 21:52:11 +0000 (10:52 +1300)
committerSam Hemelryk <sam@moodle.com>
Sun, 1 Dec 2013 21:52:11 +0000 (10:52 +1300)
lib/filelib.php
lib/tests/filelib_test.php

index a32b36f..12990e7 100644 (file)
@@ -3082,6 +3082,10 @@ class curl {
      * private callback function
      * Formatting HTTP Response Header
      *
+     * We only keep the last headers returned. For example during a redirect the
+     * redirect headers will not appear in {@link self::getResponse()}, if you need
+     * to use those headers, refer to {@link self::get_raw_response()}.
+     *
      * @param resource $ch Apparently not used
      * @param string $header
      * @return int The strlen of the header
@@ -3090,15 +3094,17 @@ class curl {
         $this->rawresponse[] = $header;
 
         if (trim($header, "\r\n") === '') {
-            if ($this->responsefinished) {
-                // Multiple headers means redirect, keep just the latest one.
-                $this->response = array();
-                return strlen($header);
-            }
+            // This must be the last header.
             $this->responsefinished = true;
         }
 
         if (strlen($header) > 2) {
+            if ($this->responsefinished) {
+                // We still have headers after the supposedly last header, we must be
+                // in a redirect so let's empty the response to keep the last headers.
+                $this->responsefinished = false;
+                $this->response = array();
+            }
             list($key, $value) = explode(" ", rtrim($header, "\r\n"), 2);
             $key = rtrim($key, ':');
             if (!empty($this->response[$key])) {
@@ -3402,9 +3408,6 @@ class curl {
                     }
                 }
 
-                $this->responsefinished = false;
-                $this->response = array();
-
                 curl_setopt($curl, CURLOPT_URL, $redirecturl);
                 $ret = curl_exec($curl);
 
index 3f9f041..657a50e 100644 (file)
@@ -177,12 +177,12 @@ class core_filelib_testcase extends advanced_testcase {
     }
 
     /**
-     * Test curl class.
+     * Test curl basics.
      */
-    public function test_curl_class() {
+    public function test_curl_basics() {
         global $CFG;
 
-        // Test https success.
+        // Test HTTP success.
         $testhtml = $this->getExternalTestFileUrl('/test.html');
 
         $curl = new curl();
@@ -210,11 +210,24 @@ class core_filelib_testcase extends advanced_testcase {
         $this->assertSame($contents, file_get_contents($tofile));
         @unlink($tofile);
 
+        // Test 404 request.
+        $curl = new curl();
+        $contents = $curl->get($this->getExternalTestFileUrl('/i.do.not.exist'));
+        $response = $curl->getResponse();
+        $this->assertSame('404 Not Found', reset($response));
+        $this->assertSame(0, $curl->get_errno());
+    }
+
+    public function test_curl_redirects() {
+        global $CFG;
+
         // Test full URL redirects.
         $testurl = $this->getExternalTestFileUrl('/test_redir.php');
 
         $curl = new curl();
         $contents = $curl->get("$testurl?redir=2", array(), array('CURLOPT_MAXREDIRS'=>2));
+        $response = $curl->getResponse();
+        $this->assertSame('200 OK', reset($response));
         $this->assertSame(0, $curl->get_errno());
         $this->assertSame(2, $curl->info['redirect_count']);
         $this->assertSame('done', $contents);
@@ -222,12 +235,16 @@ class core_filelib_testcase extends advanced_testcase {
         $curl = new curl();
         $curl->emulateredirects = true;
         $contents = $curl->get("$testurl?redir=2", array(), array('CURLOPT_MAXREDIRS'=>2));
+        $response = $curl->getResponse();
+        $this->assertSame('200 OK', reset($response));
         $this->assertSame(0, $curl->get_errno());
         $this->assertSame(2, $curl->info['redirect_count']);
         $this->assertSame('done', $contents);
 
         $curl = new curl();
         $contents = $curl->get("$testurl?redir=3", array(), array('CURLOPT_FOLLOWLOCATION'=>0));
+        $response = $curl->getResponse();
+        $this->assertSame('302 Found', reset($response));
         $this->assertSame(0, $curl->get_errno());
         $this->assertSame(302, $curl->info['http_code']);
         $this->assertSame('', $contents);
@@ -235,6 +252,8 @@ class core_filelib_testcase extends advanced_testcase {
         $curl = new curl();
         $curl->emulateredirects = true;
         $contents = $curl->get("$testurl?redir=3", array(), array('CURLOPT_FOLLOWLOCATION'=>0));
+        $response = $curl->getResponse();
+        $this->assertSame('302 Found', reset($response));
         $this->assertSame(0, $curl->get_errno());
         $this->assertSame(302, $curl->info['http_code']);
         $this->assertSame('', $contents);
@@ -291,12 +310,16 @@ class core_filelib_testcase extends advanced_testcase {
         $this->assertFileExists($tofile);
         $this->assertSame('done', file_get_contents($tofile));
         @unlink($tofile);
+    }
 
+    public function test_curl_relative_redirects() {
         // Test relative location redirects.
         $testurl = $this->getExternalTestFileUrl('/test_relative_redir.php');
 
         $curl = new curl();
         $contents = $curl->get($testurl);
+        $response = $curl->getResponse();
+        $this->assertSame('200 OK', reset($response));
         $this->assertSame(0, $curl->get_errno());
         $this->assertSame(1, $curl->info['redirect_count']);
         $this->assertSame('done', $contents);
@@ -304,6 +327,8 @@ class core_filelib_testcase extends advanced_testcase {
         $curl = new curl();
         $curl->emulateredirects = true;
         $contents = $curl->get($testurl);
+        $response = $curl->getResponse();
+        $this->assertSame('200 OK', reset($response));
         $this->assertSame(0, $curl->get_errno());
         $this->assertSame(1, $curl->info['redirect_count']);
         $this->assertSame('done', $contents);
@@ -313,6 +338,8 @@ class core_filelib_testcase extends advanced_testcase {
 
         $curl = new curl();
         $contents = $curl->get("$testurl?type=301");
+        $response = $curl->getResponse();
+        $this->assertSame('200 OK', reset($response));
         $this->assertSame(0, $curl->get_errno());
         $this->assertSame(1, $curl->info['redirect_count']);
         $this->assertSame('done', $contents);
@@ -320,12 +347,16 @@ class core_filelib_testcase extends advanced_testcase {
         $curl = new curl();
         $curl->emulateredirects = true;
         $contents = $curl->get("$testurl?type=301");
+        $response = $curl->getResponse();
+        $this->assertSame('200 OK', reset($response));
         $this->assertSame(0, $curl->get_errno());
         $this->assertSame(1, $curl->info['redirect_count']);
         $this->assertSame('done', $contents);
 
         $curl = new curl();
         $contents = $curl->get("$testurl?type=302");
+        $response = $curl->getResponse();
+        $this->assertSame('200 OK', reset($response));
         $this->assertSame(0, $curl->get_errno());
         $this->assertSame(1, $curl->info['redirect_count']);
         $this->assertSame('done', $contents);
@@ -333,12 +364,16 @@ class core_filelib_testcase extends advanced_testcase {
         $curl = new curl();
         $curl->emulateredirects = true;
         $contents = $curl->get("$testurl?type=302");
+        $response = $curl->getResponse();
+        $this->assertSame('200 OK', reset($response));
         $this->assertSame(0, $curl->get_errno());
         $this->assertSame(1, $curl->info['redirect_count']);
         $this->assertSame('done', $contents);
 
         $curl = new curl();
         $contents = $curl->get("$testurl?type=303");
+        $response = $curl->getResponse();
+        $this->assertSame('200 OK', reset($response));
         $this->assertSame(0, $curl->get_errno());
         $this->assertSame(1, $curl->info['redirect_count']);
         $this->assertSame('done', $contents);
@@ -346,12 +381,16 @@ class core_filelib_testcase extends advanced_testcase {
         $curl = new curl();
         $curl->emulateredirects = true;
         $contents = $curl->get("$testurl?type=303");
+        $response = $curl->getResponse();
+        $this->assertSame('200 OK', reset($response));
         $this->assertSame(0, $curl->get_errno());
         $this->assertSame(1, $curl->info['redirect_count']);
         $this->assertSame('done', $contents);
 
         $curl = new curl();
         $contents = $curl->get("$testurl?type=307");
+        $response = $curl->getResponse();
+        $this->assertSame('200 OK', reset($response));
         $this->assertSame(0, $curl->get_errno());
         $this->assertSame(1, $curl->info['redirect_count']);
         $this->assertSame('done', $contents);
@@ -359,12 +398,16 @@ class core_filelib_testcase extends advanced_testcase {
         $curl = new curl();
         $curl->emulateredirects = true;
         $contents = $curl->get("$testurl?type=307");
+        $response = $curl->getResponse();
+        $this->assertSame('200 OK', reset($response));
         $this->assertSame(0, $curl->get_errno());
         $this->assertSame(1, $curl->info['redirect_count']);
         $this->assertSame('done', $contents);
 
         $curl = new curl();
         $contents = $curl->get("$testurl?type=308");
+        $response = $curl->getResponse();
+        $this->assertSame('200 OK', reset($response));
         $this->assertSame(0, $curl->get_errno());
         $this->assertSame(1, $curl->info['redirect_count']);
         $this->assertSame('done', $contents);
@@ -372,10 +415,18 @@ class core_filelib_testcase extends advanced_testcase {
         $curl = new curl();
         $curl->emulateredirects = true;
         $contents = $curl->get("$testurl?type=308");
+        $response = $curl->getResponse();
+        $this->assertSame('200 OK', reset($response));
         $this->assertSame(0, $curl->get_errno());
         $this->assertSame(1, $curl->info['redirect_count']);
         $this->assertSame('done', $contents);
 
+    }
+
+    public function test_curl_proxybypass() {
+        global $CFG;
+        $testurl = $this->getExternalTestFileUrl('/test.html');
+
         $oldproxy = $CFG->proxyhost;
         $oldproxybypass = $CFG->proxybypass;
 
@@ -383,15 +434,15 @@ class core_filelib_testcase extends advanced_testcase {
         $CFG->proxyhost = 'i.do.not.exist';
         $CFG->proxybypass = '';
         $curl = new curl();
-        $contents = $curl->get($testhtml);
+        $contents = $curl->get($testurl);
         $this->assertNotEquals(0, $curl->get_errno());
         $this->assertNotEquals('47250a973d1b88d9445f94db4ef2c97a', md5($contents));
 
         // Test with proxy bypass.
-        $testhtmlhost = parse_url($testhtml, PHP_URL_HOST);
-        $CFG->proxybypass = $testhtmlhost;
+        $testurlhost = parse_url($testurl, PHP_URL_HOST);
+        $CFG->proxybypass = $testurlhost;
         $curl = new curl();
-        $contents = $curl->get($testhtml);
+        $contents = $curl->get($testurl);
         $this->assertSame(0, $curl->get_errno());
         $this->assertSame('47250a973d1b88d9445f94db4ef2c97a', md5($contents));
 
@@ -399,6 +450,27 @@ class core_filelib_testcase extends advanced_testcase {
         $CFG->proxybypass = $oldproxybypass;
     }
 
+    public function test_curl_post() {
+        $testurl = $this->getExternalTestFileUrl('/test_post.php');
+
+        // Test post request.
+        $curl = new curl();
+        $contents = $curl->post($testurl, 'data=moodletest');
+        $response = $curl->getResponse();
+        $this->assertSame('200 OK', reset($response));
+        $this->assertSame(0, $curl->get_errno());
+        $this->assertSame('OK', $contents);
+
+        // Test 100 requests.
+        $curl = new curl();
+        $curl->setHeader('Expect: 100-continue');
+        $contents = $curl->post($testurl, 'data=moodletest');
+        $response = $curl->getResponse();
+        $this->assertSame('200 OK', reset($response));
+        $this->assertSame(0, $curl->get_errno());
+        $this->assertSame('OK', $contents);
+    }
+
     /**
      * Testing prepare draft area
      *