Beispiel #1
0
 /** 
  * Returns response from DAS server or registry, or exception if error.
  */
 function getResponse($url, $timeout)
 {
     $proxy = DasProxy::getProxyServer();
     $ctx = null;
     if (strlen($proxy) > 0) {
         $aContext = array('http' => array('proxy' => $proxy, 'request_fulluri' => True));
         $ctx = stream_context_create($aContext);
     }
     $url = str_replace(" ", "%20", $url);
     if ($_REQUEST['m'] == 'pdb' && file_exists(DasProxy::CACHE_PATH . $_REQUEST['q'])) {
         $_SESSION['pdb'] = DasProxy::CACHE_PATH . $_REQUEST['q'];
         return "<response>ok</response>";
     } else {
         if ($_REQUEST['m'] == 'ontology' && file_exists(DasProxy::CACHE_PATH . $_REQUEST['q'])) {
             $_SESSION['onto'] = DasProxy::CACHE_PATH . $_REQUEST['q'];
             return file_get_contents(DasProxy::CACHE_PATH . $_REQUEST['q']);
         } else {
             if (strlen($proxy) > 0) {
                 $handle = fopen($url, "rb", false, $ctx);
             } else {
                 $handle = fopen($url, "rb");
             }
             if ($handle === false) {
                 error_log("error reading from " . $url);
                 DasProxy::throwException("error reading from " . $url);
                 return;
             }
             stream_set_timeout($handle, $timeout);
             stream_set_blocking($handle, 0);
             $content = DasProxy::readUrlContent($handle);
             if (isset($content) && strlen($content) > 0) {
                 if ($_REQUEST['m'] == 'pdb') {
                     fclose($handle);
                     file_put_contents(DasProxy::CACHE_PATH . $_REQUEST['q'], $content);
                     $_SESSION['pdb'] = DasProxy::CACHE_PATH . $_REQUEST['q'];
                     return "<response>ok</response>";
                 }
                 $dasStatus = DasProxy::getHeader($handle, "x-das-status");
                 fclose($handle);
                 if (isset($dasStatus) && ($stval = intval($dasStatus)) !== 200) {
                     DasProxy::throwException($dasStatus . " " . $dasErrorCodes[$stval]);
                 } else {
                     return $content;
                 }
             } else {
                 // HTTP error
                 fclose($handle);
                 error_log("Failed to retrieve " . $url);
                 DasProxy::throwException("Failed to retrieve " . $url);
             }
         }
     }
 }