Exemple #1
0
        public static PhpArray parse_url(string url)
        {
            url ??= string.Empty;

            if (url.Length == 0)
            {
                // empty URL results in following array to be returned:
                return(new PhpArray(1)
                {
                    { "path", string.Empty },
                });
            }

            var match = ParseUrlMethods.ParseUrlRegEx.Match(url);

            if (match == null || !match.Success || match.Groups["port"].Value.Length > 5)   // not matching or port number too long
            {
                //PhpException.Throw(PhpError.Warning, LibResources.GetString("invalid_url", FileSystemUtils.StripPassword(url))); // PHP 5.3.3+ does not emit warning
                return(null);
            }

            string scheme   = ParseUrlMethods.MatchedString(match.Groups["scheme"]);
            string user     = ParseUrlMethods.MatchedString(match.Groups["user"]);
            string pass     = ParseUrlMethods.MatchedString(match.Groups["pass"]);
            string host     = ParseUrlMethods.MatchedString(match.Groups["host"]);
            string port     = ParseUrlMethods.MatchedString(match.Groups["port"]);
            string path     = ParseUrlMethods.MatchedString(match.Groups["path"]);
            string query    = ParseUrlMethods.MatchedString(match.Groups["query"]);
            string fragment = ParseUrlMethods.MatchedString(match.Groups["fragment"]);

            string scheme_separator = match.Groups["scheme_separator"].Value;   // cannot be null

            int tmp;

            // some exceptions
            if (host != null && scheme != null && scheme_separator.Length == 0 && int.TryParse(host, out tmp))
            {   // domain:port/path
                port   = host;
                host   = scheme;
                scheme = null;
            }
            else if (scheme_separator.Length != 2 && host != null)
            {   // mailto:user@host
                // st:xx/zzz
                // mydomain.com/path
                // mydomain.com:port/path

                // dismiss user and pass
                if (user != null || pass != null)
                {
                    if (pass != null)
                    {
                        user = user + ":" + pass;
                    }
                    host = user + "@" + host;

                    user = null;
                    pass = null;
                }

                // dismiss port
                if (port != null)
                {
                    host += ":" + port;
                    port  = null;
                }

                // everything as a path
                path = scheme_separator + host + path;
                host = null;
            }

            // parse the port number,
            // invalid port number causes the function to return FALSE
            var port_int = port != null?uint.Parse(port) : 0;

            if (port_int > ushort.MaxValue || port_int < 0)
            {
                return(null);
            }

            var result = new PhpArray(8);

            const char neutralChar = '_';

            // store segments into the array (same order as it is in PHP)
            if (scheme != null)
            {
                result["scheme"] = ParseUrlMethods.ReplaceControlCharset(scheme, neutralChar);
            }
            if (host != null)
            {
                result["host"] = ParseUrlMethods.ReplaceControlCharset(host, neutralChar);
            }
            if (port != null)
            {
                result["port"] = port_int;
            }
            if (user != null)
            {
                result["user"] = ParseUrlMethods.ReplaceControlCharset(user, neutralChar);
            }
            if (pass != null)
            {
                result["pass"] = ParseUrlMethods.ReplaceControlCharset(pass, neutralChar);
            }
            if (path != null)
            {
                result["path"] = ParseUrlMethods.ReplaceControlCharset(path, neutralChar);
            }
            if (query != null)
            {
                result["query"] = ParseUrlMethods.ReplaceControlCharset(query, neutralChar);
            }
            if (fragment != null)
            {
                result["fragment"] = ParseUrlMethods.ReplaceControlCharset(fragment, neutralChar);
            }

            return(result);
        }
Exemple #2
0
        public static PhpArray ParseUrl(string url)
        {
            Match match = ParseUrlMethods.ParseUrlRegEx.Match(url ?? string.Empty);

            if (match == null || !match.Success || match.Groups["port"].Value.Length > 5)   // not matching or port number too long
            {
                PhpException.Throw(PhpError.Warning, LibResources.GetString("invalid_url", FileSystemUtils.StripPassword(url)));
                return(null);
            }

            string scheme   = ParseUrlMethods.MatchedString(match.Groups["scheme"]);
            string user     = ParseUrlMethods.MatchedString(match.Groups["user"]);
            string pass     = ParseUrlMethods.MatchedString(match.Groups["pass"]);
            string host     = ParseUrlMethods.MatchedString(match.Groups["host"]);
            string port     = ParseUrlMethods.MatchedString(match.Groups["port"]);
            string path     = ParseUrlMethods.MatchedString(match.Groups["path"]);
            string query    = ParseUrlMethods.MatchedString(match.Groups["query"]);
            string fragment = ParseUrlMethods.MatchedString(match.Groups["fragment"]);

            string scheme_separator = match.Groups["scheme_separator"].Value;   // cannot be null

            int tmp;

            // some exceptions
            if (host != null && scheme != null && scheme_separator.Length == 0 && int.TryParse(host, out tmp))
            {   // domain:port/path
                port   = host;
                host   = scheme;
                scheme = null;
            }
            else if (scheme_separator.Length != 2 && host != null)
            {   // mailto:user@host
                // st:xx/zzz
                // mydomain.com/path
                // mydomain.com:port/path

                // dismiss user and pass
                if (user != null || pass != null)
                {
                    if (pass != null)
                    {
                        user = user + ":" + pass;
                    }
                    host = user + "@" + host;

                    user = null;
                    pass = null;
                }

                // dismiss port
                if (port != null)
                {
                    host += ":" + port;
                    port  = null;
                }

                // everything as a path
                path = scheme_separator + host + path;
                host = null;
            }

            PhpArray result = new PhpArray(0, 8);

            const char neutralChar = '_';

            // store segments into the array (same order as it is in PHP)
            if (scheme != null)
            {
                result["scheme"] = ParseUrlMethods.ReplaceControlCharset(scheme, neutralChar);
            }
            if (host != null)
            {
                result["host"] = ParseUrlMethods.ReplaceControlCharset(host, neutralChar);
            }
            if (port != null)
            {
                result["port"] = (int)unchecked ((ushort)uint.Parse(port));              // PHP overflows in this way
            }
            if (user != null)
            {
                result["user"] = ParseUrlMethods.ReplaceControlCharset(user, neutralChar);
            }
            if (pass != null)
            {
                result["pass"] = ParseUrlMethods.ReplaceControlCharset(pass, neutralChar);
            }
            if (path != null)
            {
                result["path"] = ParseUrlMethods.ReplaceControlCharset(path, neutralChar);
            }
            if (query != null)
            {
                result["query"] = ParseUrlMethods.ReplaceControlCharset(query, neutralChar);
            }
            if (fragment != null)
            {
                result["fragment"] = ParseUrlMethods.ReplaceControlCharset(fragment, neutralChar);
            }

            return(result);
        }