@param string $requestedUri
@param array  $rewrites

@return string
549486
private static function applyRewrites(string $requestedUri, array $rewrites): string
    {
        foreach ($rewrites as $route) {
            $pattern      = $route['pattern'];
            $replacement  = $route['replacement'];
            $requestedUri = preg_replace($pattern, $replacement, $requestedUri);

            if (isset($route['last'])) {
                break;
            }
        }

        return $requestedUri;
    }
285786
private function getRoutesFromIndex (&$routes, &$routesIndex, &$requestMethod, &$requestPathParts, $requestPathIndex = 0) {
        if (!empty($requestPathParts[$requestPathIndex])) {
            $requestPathPart = $requestPathParts[$requestPathIndex];
            if (array_key_exists($requestPathPart, $routesIndex)) {
                $this->getRoutesFromIndex($routes,$routesIndex[$requestPathPart], $requestMethod, $requestPathParts, $requestPathIndex + 1);
            }
            if (array_key_exists(self::ROUTE_PARAMETER_WILDCARD, $routesIndex)) {
                $this->getRoutesFromIndex($routes,$routesIndex[self::ROUTE_PARAMETER_WILDCARD], $requestMethod, $requestPathParts, $requestPathIndex + 1);
            }
        }
        else if (array_key_exists(self::ROUTE_ACTIONS_KEY, $routesIndex)) {
            $testRoutes = &$routesIndex[self::ROUTE_ACTIONS_KEY];
            if (array_key_exists($requestMethod, $testRoutes)) {
                $this->getRoutesFromIndexActions($routes,$testRoutes[$requestMethod], $requestMethod,$requestPathParts);
            }
            if (array_key_exists(self::ROUTE_GENERIC_METHOD, $testRoutes)) {
                $this->getRoutesFromIndexActions($routes,$testRoutes[self::ROUTE_GENERIC_METHOD], $requestMethod, $requestPathParts);
            }
        }

        if (array_key_exists(self::ROUTE_GENERIC_ACTIONS_KEY, $routesIndex)) {
            $testRoutes = &$routesIndex[self::ROUTE_GENERIC_ACTIONS_KEY];
            if (array_key_exists($requestMethod, $testRoutes)) {
                $this->getRoutesFromIndexActions($routes,$testRoutes[$requestMethod], $requestMethod, $requestPathParts);
            }
            if (array_key_exists(self::ROUTE_GENERIC_METHOD, $testRoutes)) {
                $this->getRoutesFromIndexActions($routes,$testRoutes[self::ROUTE_GENERIC_METHOD], $requestMethod, $requestPathParts);
            }
        }
    }
1232232
private static function getFilteredQueryString(UriInterface $uri, array $keys)
    {
        $current = $uri->getQuery();

        if ($current === '') {
            return [];
        }

        $decodedKeys = array_map('rawurldecode', $keys);

        return array_filter(explode('&', $current), function ($part) use ($decodedKeys) {
            return !in_array(rawurldecode(explode('=', $part)[0]), $decodedKeys, true);
        });
    }
187575
protected function rewriteAbsolute($uri)
    {
        $request = $this->getRequest();

        $path = strtr($this->assetDirectory, '/', DIRECTORY_SEPARATOR).DIRECTORY_SEPARATOR.strtr($uri, '/', DIRECTORY_SEPARATOR);

        foreach ($this->symlinks as $link => $target) {
            if (strpos($path, $target) === 0) {
                $path = $link.substr($path, strlen($target));

                break;
            }
        }

        $base = isset($_SERVER['REQUEST_URI']) ? $_SERVER['REQUEST_URI'] : null;

        if ($request->getHost()) {
            $base = $request->getSchemeAndHttpHost().$request->getBaseUrl();
        }

        // Strip the document root from the path.
        if (strpos($path, app('path.public')) !== false) {
            $path = str_replace(app('path.public'), '', $path);
        } elseif (strpos($path, app('path.resources')) !== false) {
            $path = str_replace(app('path.resources'), '', $path);
        }

        $uri = strtr($path, '/\\', '//');
        $uri = $this->removeDots($uri);

        return $uri;
    }
34016
protected function getSystemUri($requestUri = null, $refresh = false)
    {
        if ($this->psrUri && !$refresh) {
            return $this->psrUri;
        }

        $uri = $requestUri ? new PsrUri($requestUri) : $this->getRequest()->getUri();

        $server = $this->getRequest()->getServerParams();

        // If we are working from a CGI SAPI with the 'cgi.fix_pathinfo' directive disabled we use PHP_SELF.
        if (strpos(PHP_SAPI, 'cgi') !== false && !ini_get('cgi.fix_pathinfo') && !empty($server['REQUEST_URI'])) {
            // We aren't expecting PATH_INFO within PHP_SELF so this should work.
            $uri = $uri->withPath(rtrim(\dirname((string) ServerHelper::getValue($server, 'PHP_SELF')), '/\\'));
        } else {
            // Pretty much everything else should be handled with SCRIPT_NAME.
            $uri = $uri->withPath(rtrim(\dirname((string) ServerHelper::getValue($server, 'SCRIPT_NAME')), '/\\'));
        }

        // Clear the unused parts of the requested URI.
        $uri = $uri->withFragment('');

        return $this->psrUri = $uri;
    }
1272006
private function extractAndGroupUriFromParameters(array $requestParameters, array $filterNames)
    {
        if (empty($filterNames)) {
            return [];
        }

        $groupedUris = [];

        foreach ($this->flattenArray($requestParameters) as $key => $value) {
            $encodedUri = $this->getEncodedUri($value);

            if (in_array($key, $filterNames, true) && common_Utils::isUri($encodedUri)) {
                $groupedUris[$key][] = $encodedUri;
            }
        }

        return $groupedUris;
    }
1188939
protected function getActualUri( $uri )
	{
		foreach ( $this->rewriteURIs as $prefix => $rewriteUri )
		{
			if ( strpos( $uri, $prefix ) !== 0 ) continue;
			$actualUri =  str_replace( $prefix, $rewriteUri, $uri );
			$actualUri = XBRL::normalizePath( $this->getFirstFolderName() . "/" . XBRL_TaxonomyPackage::metaFolderName . "/" . $actualUri );
			return $actualUri;
		}

		return $uri;
	}
320962
public function match(CalledUri $calledUri): MatchingRoutes
    {
        $allowedMethods = [];
        $matching       = [];
        foreach ($this->routes as $route) {
            /* @var $route \stubbles\webapp\routing\Route */
            if ($route->matches($calledUri)) {
                return new MatchingRoutes(
                        ['exact' => $route],
                        $route->allowedRequestMethods()
                );
            } elseif ($route->matchesPath($calledUri)) {
                $matching[] = $route;
                $allowedMethods = array_merge(
                        $allowedMethods,
                        $route->allowedRequestMethods()
                );
            }
        }

        return new MatchingRoutes($matching, array_unique($allowedMethods));
    }
1115677
protected function normalizeUri(string $uri, bool $trailingSlash): string
    {
        // add starting '/' and cut ending '/' if necessary
        $uri = strlen($uri) > 0 && $uri[0] === '/' ? $uri : '/' . $uri;
        $prefixLen = strlen($uri);
        $uri = $prefixLen > 1 && substr($uri, -1) === '/' ? substr($uri, 0, $prefixLen - 1) : $uri;

        // feature: trailing slashes are possible when asked
        $uri = $trailingSlash === true && substr($uri, -1) !== '/' ? $uri . '/' : $uri;

        return $uri;
    }
712819
protected function prepareRequestUri()
    {
        $requestUri = '';

        if ($this->headers->has('X_ORIGINAL_URL')) {
            // IIS with Microsoft Rewrite Module
            $requestUri = $this->headers->get('X_ORIGINAL_URL');
            $this->headers->remove('X_ORIGINAL_URL');
            $this->server->remove('HTTP_X_ORIGINAL_URL');
            $this->server->remove('UNENCODED_URL');
            $this->server->remove('IIS_WasUrlRewritten');
        } elseif ($this->headers->has('X_REWRITE_URL')) {
            // IIS with ISAPI_Rewrite
            $requestUri = $this->headers->get('X_REWRITE_URL');
            $this->headers->remove('X_REWRITE_URL');
        } elseif ($this->server->get('IIS_WasUrlRewritten') == '1' &&
            $this->server->get('UNENCODED_URL') != '') {
            // IIS7 with URL Rewrite: make sure we get the unencoded URL (double slash problem)
            $requestUri = $this->server->get('UNENCODED_URL');
            $this->server->remove('UNENCODED_URL');
            $this->server->remove('IIS_WasUrlRewritten');
        } elseif ($this->server->has('REQUEST_URI')) {
            $requestUri = $this->server->get('REQUEST_URI');
            // HTTP proxy reqs setup request URI with scheme and host [and port] + the URL path, only use URL path
            $schemeAndHttpHost = $this->getSchemeAndHttpHost();
            if (strpos($requestUri, $schemeAndHttpHost) === 0) {
                $requestUri = substr($requestUri, strlen($schemeAndHttpHost));
            }
        } elseif ($this->server->has('ORIG_PATH_INFO')) {
            // IIS 5.0, PHP as CGI
            $requestUri = $this->server->get('ORIG_PATH_INFO');
            if ('' != $this->server->get('QUERY_STRING')) {
                $requestUri .= '?'.$this->server->get('QUERY_STRING');
            }
            $this->server->remove('ORIG_PATH_INFO');
        }

        // normalize the request URI to ease creating sub-requests from this request
        $this->server->set('REQUEST_URI', $requestUri);

        return $requestUri;
    }
1192707
private function getApplicable(CalledUri $calledUri, array $interceptors): array
    {
        $applicable = [];
        foreach ($interceptors as  $interceptor) {
            if ($calledUri->satisfies($interceptor['requestMethod'], $interceptor['path'])) {
                $applicable[] = $interceptor['interceptor'];
            }
        }

        return $applicable;
    }
82760
private function getUriRegexFromParameters($uri, $params)
    {
        $regex = str_replace('/', '\/', $uri);
        foreach ($params as $param) {
            $regex = str_replace('{' . $param . '}', '([^\/]+)', $regex);
        }
        return $regex;
    }
139441
protected function rewriteRelative($uri)
    {
        $path = strtr($this->assetDirectory, '/', DIRECTORY_SEPARATOR).DIRECTORY_SEPARATOR.strtr($uri, '/', DIRECTORY_SEPARATOR);

        foreach ($this->symlinks as $link => $target)
        {
            if (strpos($path, $target) === 0)
            {
                $path = $link.substr($path, strlen($target));

                break;
            }
        }

        // Strip the document root from the path.
        $path = substr($path, strlen($this->documentRoot));

        $uri = strtr($path, '/\\', '//');
        $uri = $this->removeDots($uri);

        return $uri;
    }
945573
public function matches(CalledUri $calledUri): bool
    {
        if (!$this->matchesPath($calledUri)) {
            return false;
        }

        if (in_array($calledUri->method(), $this->allowedRequestMethods)) {
            return true;
        }

        if (in_array(Http::GET, $this->allowedRequestMethods)) {
            return $calledUri->methodEquals(Http::HEAD);
        }

        return false;
    }
318789
public function pathToString($path, $rewrite = false)
    {
        if (is_string($path)) {
            return $path;
        }
        $str = $this->basePath;
        if ($str == '/') {
            $str = '';
        }
        if (! ($this->rewrite or $rewrite)) {
            $str .= '/' . $this->scriptName;
        }
        $str .= '/' . implode('/', array_map('urlencode', $path));
        $str = rtrim($str, '/');
        if ($str == '') {
            return '/';
        }
        return $str;
    }
49859
public function resume($resumeUri)
  {
     $this->resumeUri = $resumeUri;
     $headers = array(
       'content-range' => "bytes */$this->size",
       'content-length' => 0,
     );
     $httpRequest = new Request(
         $this->resumeUri,
         'PUT',
         $headers
     );
     return $this->makePutRequest($httpRequest);
  }
1126913
public static function rewrite_answers_urls($answer, $rewriteanswer = true) {
        global $PAGE;

        $context = context_module::instance($PAGE->cm->id);
        if ($rewriteanswer) {
            $answer->answer = file_rewrite_pluginfile_urls($answer->answer, 'pluginfile.php', $context->id,
                    'mod_lesson', 'page_answers', $answer->id);
        }
        $answer->response = file_rewrite_pluginfile_urls($answer->response, 'pluginfile.php', $context->id,
                'mod_lesson', 'page_responses', $answer->id);

        return $answer;
    }