/** * Resolves a relative URI using this URI as the base URI. */ public function resolve($relUri) { // If it is a string, then convert it to a parsed object if (is_string($relUri)) { $relUri = new EasyRdf_ParsedUri($relUri); } // This code is based on the pseudocode in section 5.2.2 of RFC3986 $target = new EasyRdf_ParsedUri(); if ($relUri->_scheme) { $target->_scheme = $relUri->_scheme; $target->_authority = $relUri->_authority; $target->_path = $relUri->_path; $target->_query = $relUri->_query; } else { if ($relUri->_authority) { $target->_authority = $relUri->_authority; $target->_path = $relUri->_path; $target->_query = $relUri->_query; } else { if (empty($relUri->_path)) { $target->_path = $this->_path; if ($relUri->_query) { $target->_query = $relUri->_query; } else { $target->_query = $this->_query; } } else { if (substr($relUri->_path, 0, 1) == '/') { $target->_path = $relUri->_path; } else { $path = $this->_path; $lastSlash = strrpos($path, '/'); if ($lastSlash !== false) { $path = substr($path, 0, $lastSlash + 1); } else { $path = '/'; } $target->_path .= $path . $relUri->_path; } $target->_query = $relUri->_query; } $target->_authority = $this->_authority; } $target->_scheme = $this->_scheme; } $target->_fragment = $relUri->_fragment; $target->normalise(); return $target; }
public function testSetFragmentNull() { $uri = new EasyRdf_ParsedUri('http://www.ietf.org/rfc/rfc2396.txt#foobar'); $uri->setFragment(NULL); $this->assertStringEquals('http://www.ietf.org/rfc/rfc2396.txt', $uri); }
protected function processUri($node, &$context, $value, $isProp = false) { if (preg_match("/^\\[(.*)\\]\$/", $value, $matches)) { // Safe CURIE return $this->expandCurie($node, $context, $matches[1]); } elseif (preg_match(self::TERM_REGEXP, $value) and $isProp) { $term = strtolower($value); if ($context['vocab']) { return $context['vocab'] . $value; } elseif (isset($context['terms'][$term])) { return $context['terms'][$term]; } } elseif (substr($value, 0, 2) === '_:' and $isProp) { return NULL; } else { $uri = $this->expandCurie($node, $context, $value); if ($uri) { return $uri; } else { $parsed = new EasyRdf_ParsedUri($value); if ($parsed->isAbsolute()) { return $value; } elseif ($isProp) { // Properties can't be relative URIs return NULL; } elseif ($this->_baseUri) { return $this->_baseUri->resolve($parsed); } } } }
/** * Load RDF data into the graph from a URI. * * If no URI is given, then the URI of the graph will be used. * * The document type is optional but should be specified if it * can't be guessed or got from the HTTP headers. * * @param string $uri The URI of the data to load * @param string $format Optional format of the data (eg. rdfxml) * @return integer The number of triples added to the graph */ public function load($uri = null, $format = null) { $this->checkResourceParam($uri, true); if (!$uri) { throw new EasyRdf_Exception("No URI given to load() and the graph does not have a URI."); } // Setup the HTTP client $client = EasyRdf_Http::getDefaultHttpClient(); $client->resetParameters(true); $client->setConfig(array('maxredirects' => 0)); $client->setMethod('GET'); $client->setHeaders('Accept', EasyRdf_Format::getHttpAcceptHeader()); $requestUrl = $uri; $response = null; $redirectCounter = 0; do { // Have we already loaded it into the graph? $requestUrl = EasyRdf_Utils::removeFragmentFromUri($requestUrl); if (in_array($requestUrl, $this->loaded)) { return 0; } // Make the HTTP request $client->setHeaders('host', null); $client->setUri($requestUrl); $response = $client->request(); // Add the URL to the list of URLs loaded $this->loaded[] = $requestUrl; if ($response->isRedirect() and $location = $response->getHeader('location')) { // Avoid problems with buggy servers that add whitespace $location = trim($location); // Some servers return relative URLs in the location header // resolve it in relation to previous request $baseUri = new EasyRdf_ParsedUri($requestUrl); $requestUrl = $baseUri->resolve($location)->toString(); $requestUrl = EasyRdf_Utils::removeFragmentFromUri($requestUrl); // If it is a 303 then drop the parameters if ($response->getStatus() == 303) { $client->resetParameters(); } ++$redirectCounter; } elseif ($response->isSuccessful()) { // If we didn't get any location, stop redirecting break; } else { throw new EasyRdf_Http_Exception("HTTP request for {$requestUrl} failed: " . $response->getMessage(), $response->getStatus(), null, $response->getBody()); } } while ($redirectCounter < $this->maxRedirects); if (!$format or $format == 'guess') { list($format, $params) = EasyRdf_Utils::parseMimeType($response->getHeader('Content-Type')); } // Parse the data return $this->parse($response->getBody(), $format, $uri); }
/** * Send the HTTP request and return an HTTP response object * * @return EasyRdf_Http_Response * @throws EasyRdf_Exception */ public function request($method = null) { if (!$this->uri) { throw new EasyRdf_Exception("Set URI before calling EasyRdf_Http_Client->request()"); } if ($method) { $this->setMethod($method); } $this->redirectCounter = 0; $response = null; // Send the first request. If redirected, continue. do { // Clone the URI and add the additional GET parameters to it $uri = parse_url($this->uri); if ($uri['scheme'] === 'http') { $host = $uri['host']; } elseif ($uri['scheme'] === 'https') { $host = 'ssl://' . $uri['host']; } else { throw new EasyRdf_Exception("Unsupported URI scheme: " . $uri['scheme']); } if (isset($uri['port'])) { $port = $uri['port']; } else { if ($uri['scheme'] === 'https') { $port = 443; } else { $port = 80; } } if (!empty($this->paramsGet)) { if (!empty($uri['query'])) { $uri['query'] .= '&'; } else { $uri['query'] = ''; } $uri['query'] .= http_build_query($this->paramsGet, null, '&'); } $headers = $this->prepareHeaders($uri['host'], $port); // Open socket to remote server $socket = @fsockopen($host, $port, $errno, $errstr, $this->config['timeout']); if (!$socket) { throw new EasyRdf_Exception("Unable to connect to {$host}:{$port} ({$errstr})"); } // Write the request $path = $uri['path']; if (empty($path)) { $path = '/'; } if (isset($uri['query'])) { $path .= '?' . $uri['query']; } fwrite($socket, "{$this->method} {$path} HTTP/1.1\r\n"); foreach ($headers as $k => $v) { if (is_string($k)) { $v = ucfirst($k) . ": {$v}"; } fwrite($socket, "{$v}\r\n"); } fwrite($socket, "\r\n"); // Send the request body, if there is one set if (isset($this->rawPostData)) { fwrite($socket, $this->rawPostData); } // Read in the response $content = ''; while (!feof($socket)) { $content .= fgets($socket); } // FIXME: support HTTP/1.1 100 Continue // Close the socket @fclose($socket); // Parse the response string $response = EasyRdf_Http_Response::fromString($content); // If we got redirected, look for the Location header if ($response->isRedirect() && ($location = $response->getHeader('location'))) { // Avoid problems with buggy servers that add whitespace at the // end of some headers (See ZF-11283) $location = trim($location); // Some servers return relative URLs in the location header // resolve it in relation to previous request $baseUri = new EasyRdf_ParsedUri($this->uri); $location = $baseUri->resolve($location)->toString(); // If it is a 303 then drop the parameters and send a GET request if ($response->getStatus() == 303) { $this->resetParameters(); $this->setMethod('GET'); } // If we got a well formed absolute URI if (parse_url($location)) { $this->setHeaders('host', null); $this->setUri($location); } else { throw new EasyRdf_Exception("Failed to parse Location header returned by " . $this->uri); } ++$this->redirectCounter; } else { // If we didn't get any location, stop redirecting break; } } while ($this->redirectCounter < $this->config['maxredirects']); return $response; }
private function guessContainerUri() { $parsedUrl = new ParsedUri(rtrim($this->context->getQueryStrippedUri(), '/')); $parsedUrl->normalise()->setPath(dirname($parsedUrl->getPath())); return $parsedUrl->toString(); }