Example #1
0
 /**
  * Execute a HTTP request to MusicBrainz server, errorMessage attribute is set in case of error.
  *
  * @return bool|string MusicBrainz informations or false on failure
  */
 private function executeCall()
 {
     //request JSON output format
     $this->setRequestedFormat('json');
     //create a HTTP request object and call
     require_once $_SERVER['DOCUMENT_ROOT'] . '/server/lib/HttpRequest.php';
     $request = new HttpRequest();
     if (!$request->execute($this->endpoint, 'GET', null, null, $this->queryParameters, $response, $responseHeaders)) {
         $this->errorMessage = 'Error during request';
         //error during HTTP request
         return false;
     }
     //decode response
     if ($this->format === 'json') {
         $response = json_decode($response);
         if (json_last_error() !== JSON_ERROR_NONE) {
             $this->errorMessage = 'Invalid response received';
             //error on JSON parsing
             return false;
         }
         //return object
         return $response;
     }
     //return false by default
     return false;
 }
 /**
  * Execute a HTTP request to Cover Art Archive server, errorMessage attribute is set in case of error.
  *
  * @return bool|string MusicBrainz informations or false on failure
  */
 private function executeCall()
 {
     //create a HTTP request object and call
     require_once $_SERVER['DOCUMENT_ROOT'] . '/server/lib/HttpRequest.php';
     $request = new HttpRequest();
     if (!$request->execute($this->endpoint, 'GET', null, null, null, $response, $responseHeaders)) {
         $this->errorMessage = 'Error during request';
         //error during HTTP request
         return false;
     }
     //return object
     return $response;
 }
 function test_cache_entry_exists_and_no_validation_required()
 {
     $response = new HttpResponse();
     $response->status_code = 200;
     $response->body = 'scooby';
     $request = new HttpRequest('GET', 'http://example.org/');
     $cache = new FakeCache();
     $cache->save($response, $request->cache_id());
     $request->set_cache($cache);
     $request->always_validate_cache(FALSE);
     $new_response = $request->execute();
     $this->assertEquals('scooby', $new_response->body);
 }
Example #4
0
 public function execute()
 {
     parent::execute();
     // At least on Centos 4.8 with PHP 5.1.6, using max_redirects to follow redirects
     // causes a segfault
     if (version_compare('5.1.7', phpversion(), '>')) {
         $this->manuallyRedirect = true;
     }
     if ($this->parsedUrl['scheme'] != 'http') {
         $this->status->fatal('http-invalid-scheme', $this->parsedUrl['scheme']);
     }
     $this->reqHeaders['Accept'] = "*/*";
     if ($this->method == 'POST') {
         // Required for HTTP 1.0 POSTs
         $this->reqHeaders['Content-Length'] = strlen($this->postData);
         $this->reqHeaders['Content-type'] = "application/x-www-form-urlencoded";
     }
     $options = array();
     if ($this->proxy && !$this->noProxy) {
         $options['proxy'] = $this->urlToTCP($this->proxy);
         $options['request_fulluri'] = true;
     }
     if (!$this->followRedirects || $this->manuallyRedirect) {
         $options['max_redirects'] = 0;
     } else {
         $options['max_redirects'] = $this->maxRedirects;
     }
     $options['method'] = $this->method;
     $options['header'] = implode("\r\n", $this->getHeaderList());
     // Note that at some future point we may want to support
     // HTTP/1.1, but we'd have to write support for chunking
     // in version of PHP < 5.3.1
     $options['protocol_version'] = "1.0";
     // This is how we tell PHP we want to deal with 404s (for example) ourselves.
     // Only works on 5.2.10+
     $options['ignore_errors'] = true;
     if ($this->postData) {
         $options['content'] = $this->postData;
     }
     $oldTimeout = false;
     if (version_compare('5.2.1', phpversion(), '>')) {
         $oldTimeout = ini_set('default_socket_timeout', $this->timeout);
     } else {
         $options['timeout'] = $this->timeout;
     }
     $context = stream_context_create(array('http' => $options));
     $this->headerList = array();
     $reqCount = 0;
     $url = $this->url;
     do {
         $again = false;
         $reqCount++;
         wfSuppressWarnings();
         $fh = fopen($url, "r", false, $context);
         wfRestoreWarnings();
         if ($fh) {
             $result = stream_get_meta_data($fh);
             $this->headerList = $result['wrapper_data'];
             $this->parseHeader();
             $url = $this->getResponseHeader("Location");
             $again = $this->manuallyRedirect && $this->followRedirects && $url && $this->isRedirect() && $this->maxRedirects > $reqCount;
         }
     } while ($again);
     if ($oldTimeout !== false) {
         ini_set('default_socket_timeout', $oldTimeout);
     }
     $this->setStatus();
     if ($fh === false) {
         $this->status->fatal('http-request-error');
         return $this->status;
     }
     if ($result['timed_out']) {
         $this->status->fatal('http-timed-out', $this->url);
         return $this->status;
     }
     if ($this->status->isOK()) {
         while (!feof($fh)) {
             $buf = fread($fh, 8192);
             if ($buf === false) {
                 $this->status->fatal('http-read-error');
                 break;
             }
             if (strlen($buf)) {
                 call_user_func($this->callback, $fh, $buf);
             }
         }
     }
     fclose($fh);
     return $this->status;
 }