Example #1
0
		private static int CompareNamesUsingSpecialCompare(String a, String b)
		{
			char lasta = '\0';
			if (a.Length > 0 && a[a.Length - 1] == '/')
			{
				lasta = '/';
				a = a.Slice(0, a.Length - 1);
			}
			byte[] abytes = a.getBytes("ISO-8859-1");
			char lastb = '\0';
			if (b.Length > 0 && b[b.Length - 1] == '/')
			{
				lastb = '/';
				b = b.Slice(0, b.Length - 1);
			}
			byte[] bbytes = b.getBytes("ISO-8859-1");
            return Core.Tree.CompareNames(abytes, bbytes, lasta, lastb);
		}
Example #2
0
 private int compareNamesUsingSpecialCompare(String a, String b)
 {
     char lasta = '\0';
     byte[] abytes;
     if (a.Length > 0 && a[a.Length - 1] == '/')
     {
         lasta = '/';
         a = a.Slice(0, a.Length - 1);
     }
     abytes = Encoding.GetEncoding("ISO-8859-1").GetBytes(a);
     char lastb = '\0';
     byte[] bbytes;
     if (b.Length > 0 && b[b.Length - 1] == '/')
     {
         lastb = '/';
         b = b.Slice(0, b.Length - 1);
     }
     bbytes = Encoding.GetEncoding("ISO-8859-1").GetBytes(b);
     return Tree.CompareNames(abytes, bbytes, lasta, lastb);
 }
Example #3
0
        public static dynamic Load(String uri, ICredentials credentials, Json args = null)
        {
            if (uri == null) return null;

            var is_remote = uri.StartsWith("http://") || uri.StartsWith("https://");
            if (is_remote)
            {
                var req = (HttpWebRequest)WebRequest.Create(uri);
                req.Credentials = credentials ?? CredentialCache.DefaultCredentials;
                req.Accept = "application/json,*/*";

                req.Method = args == null ? "GET" : "POST";
                if (args != null) req.GetRequestStream().WriteJson(args);

                try
                {
                    var resp = (HttpWebResponse)req.GetResponse();
                    if (resp.StatusCode != HttpStatusCode.OK)
                    {
                        throw new Exception(String.Format("GET for \"{1}\" has failed: {2}{0}{3}",
                            Environment.NewLine, uri, resp.StatusCode, resp.GetResponseStream().DumpToString()));
                    }

                    var s_json = resp.GetResponseStream().DumpToString();
                    try { return Parse(s_json); }
                    catch (Exception ex)
                    {
                        throw new Exception(String.Format("Failed to parse JSON response from \"{1}\":{2}{0}{3}",
                            Environment.NewLine, uri, ex.Message, s_json));
                    }
                }
                catch (WebException wex)
                {
                    if (wex.Response != null)
                    {
                        var resp = (HttpWebResponse)wex.Response;
                        throw new Exception(String.Format("GET for \"{1}\" has failed: {2}{0}{3}",
                            Environment.NewLine, uri, resp.StatusCode, resp.GetResponseStream().DumpToString()), wex);
                    }
                    else
                    {
                        throw new Exception(String.Format("GET for \"{1}\" has failed: {0}{2}",
                            Environment.NewLine, uri, wex));
                    }
                }
                catch (Exception ex)
                {
                    throw new Exception(String.Format("GET for \"{1}\" has failed: {0}{2}",
                        Environment.NewLine, uri, ex));
                }
            }
            else
            {
                if (uri.StartsWith("file:///")) uri = uri.Slice("file:///".Length);

                var is_web = HttpContext.Current != null;
                var path = is_web && uri.StartsWith("~") ? HttpContext.Current.Server.MapPath(uri) : uri;
                path = Path.GetFullPath(path);
                if (!File.Exists(path)) throw new Exception(String.Format(
                    "READ for \"{0}\" has failed: file \"{1}\" does not exist", uri, path));

                var s_json = File.ReadAllText(path);
                try { return Parse(s_json); }
                catch (Exception ex)
                {
                    throw new Exception(String.Format("Failed to parse JSON from \"{1}\":{2}{0}{3}",
                        Environment.NewLine, uri, ex.Message, s_json));
                }
            }
        }
Example #4
0
        public static void Save(String uri, Json json, ICredentials credentials = null)
        {
            if (uri == null) return;

            var is_remote = uri.StartsWith("http://") || uri.StartsWith("https://");
            if (is_remote)
            {
                var req = (HttpWebRequest)WebRequest.Create(uri);
                req.Credentials = credentials ?? CredentialCache.DefaultCredentials;

                req.Method = json == null ? "DELETE" : "POST";
                if (json != null) new StreamWriter(req.GetRequestStream()).Write(json.ToCompactString());

                try
                {
                    var resp = (HttpWebResponse)req.GetResponse();
                    if (resp.StatusCode != HttpStatusCode.OK)
                    {
                        throw new Exception(String.Format("{4} for \"{1}\" has failed: {2}{0}{3}",
                            Environment.NewLine, uri, resp.StatusCode, resp.GetResponseStream().DumpToString(), req.Method));
                    }
                }
                catch (WebException wex)
                {
                    if (wex.Response != null)
                    {
                        var resp = (HttpWebResponse)wex.Response;
                        throw new Exception(String.Format("{4} for \"{1}\" has failed: {2}{0}{3}",
                            Environment.NewLine, uri, resp.StatusCode, resp.GetResponseStream().DumpToString(), wex, req.Method));
                    }
                    else
                    {
                        throw new Exception(String.Format("{3} for \"{1}\" has failed: {0}{2}",
                            Environment.NewLine, uri, wex, req.Method));
                    }
                }
                catch (Exception ex)
                {
                    throw new Exception(String.Format("{3} for \"{1}\" has failed: {0}{2}",
                        Environment.NewLine, uri, ex, req.Method));
                }
            }
            else
            {
                if (uri.StartsWith("file:///")) uri = uri.Slice("file:///".Length);

                var is_web = HttpContext.Current != null;
                var path = is_web && uri.StartsWith("~") ? HttpContext.Current.Server.MapPath(uri) : uri;
                path = Path.GetFullPath(path);
                if (!File.Exists(path)) throw new Exception(String.Format(
                    "{2} for \"{0}\" has failed: file \"{1}\" does not exist", uri, path, json == null ? "DELETE" : "WRITE"));

                var s_json = json == null ? null : json.ToCompactString();
                if (s_json == null) File.Delete(path);
                else File.WriteAllText(path, s_json);
            }
        }
Example #5
0
        public static XDocument Load(String uri, ICredentials credentials = null)
        {
            if (uri == null) return null;

            var is_remote = uri.StartsWith("http://") || uri.StartsWith("https://");
            if (is_remote)
            {
                var req = (HttpWebRequest)WebRequest.Create(uri);
                req.Credentials = credentials ?? CredentialCache.DefaultCredentials;
                req.Accept = "application/xml,application/atom+xml,*/*";

                try
                {
                    var resp = (HttpWebResponse)req.GetResponse();
                    if (resp.StatusCode != HttpStatusCode.OK)
                    {
                        throw new Exception(String.Format("GET for \"{1}\" has failed: {2}{0}{3}",
                            Environment.NewLine, uri, resp.StatusCode, resp.GetResponseStream().DumpToString()));
                    }

                    var s_xml = resp.GetResponseStream().DumpToString();
                    try { return Parse(s_xml); }
                    catch (Exception ex)
                    {
                        throw new Exception(String.Format("Failed to parse XML response from \"{1}\":{2}{0}{3}",
                            Environment.NewLine, uri, ex.Message, s_xml));
                    }
                }
                catch (WebException wex)
                {
                    if (wex.Response != null)
                    {
                        var resp = (HttpWebResponse)wex.Response;
                        throw new Exception(String.Format("GET for \"{1}\" has failed: {2}{0}{3}",
                            Environment.NewLine, uri, resp.StatusCode, resp.GetResponseStream().DumpToString()), wex);
                    }
                    else
                    {
                        throw new Exception(String.Format("GET for \"{1}\" has failed: {0}{2}",
                            Environment.NewLine, uri, wex));
                    }
                }
                catch (Exception ex)
                {
                    throw new Exception(String.Format("GET for \"{1}\" has failed: {0}{2}",
                        Environment.NewLine, uri, ex));
                }
            }
            else
            {
                if (uri.StartsWith("file:///")) uri = uri.Slice("file:///".Length);

                var is_web = HttpContext.Current != null;
                var path = is_web && uri.StartsWith("~") ? HttpContext.Current.Server.MapPath(uri) : uri;
                path = Path.GetFullPath(path);
                if (!File.Exists(path)) throw new Exception(String.Format(
                    "READ for \"{0}\" has failed: file \"{1}\" does not exist", uri, path));

                var s_xml = File.ReadAllText(path);
                var charset = s_xml.Extract(@"<meta\s+http-equiv=""Content-Type""\s+content=""text/html;charset=(?<charset>.*?)""");
                if (charset != null) s_xml = File.ReadAllText(path, Encoding.GetEncoding(charset));

                try { return Parse(s_xml); }
                catch (Exception ex)
                {
                    throw new Exception(String.Format("Failed to parse XML from \"{1}\":{2}{0}{3}",
                        Environment.NewLine, uri, ex.Message, s_xml));
                }
            }
        }
Example #6
0
        public static void Save(String uri, XDocument xml, ICredentials credentials = null)
        {
            if (uri == null) return;

            var is_remote = uri.StartsWith("http://") || uri.StartsWith("https://");
            if (is_remote)
            {
                var req = (HttpWebRequest)WebRequest.Create(uri);
                req.Credentials = credentials ?? CredentialCache.DefaultCredentials;

                req.Method = xml == null ? "DELETE" : "POST";
                if (xml != null) new StreamWriter(req.GetRequestStream()).Write(xml.ToString());

                try
                {
                    var resp = (HttpWebResponse)req.GetResponse();
                    if (resp.StatusCode != HttpStatusCode.OK)
                    {
                        throw new Exception(String.Format("{4} for \"{1}\" has failed: {2}{0}{3}",
                            Environment.NewLine, uri, resp.StatusCode, resp.GetResponseStream().DumpToString(), req.Method));
                    }
                }
                catch (WebException wex)
                {
                    if (wex.Response != null)
                    {
                        var resp = (HttpWebResponse)wex.Response;
                        throw new Exception(String.Format("{4} for \"{1}\" has failed: {2}{0}{3}",
                            Environment.NewLine, uri, resp.StatusCode, resp.GetResponseStream().DumpToString(), wex, req.Method));
                    }
                    else
                    {
                        throw new Exception(String.Format("{3} for \"{1}\" has failed: {0}{2}",
                            Environment.NewLine, uri, wex, req.Method));
                    }
                }
                catch (Exception ex)
                {
                    throw new Exception(String.Format("{3} for \"{1}\" has failed: {0}{2}",
                        Environment.NewLine, uri, ex, req.Method));
                }
            }
            else
            {
                if (uri.StartsWith("file:///")) uri = uri.Slice("file:///".Length);

                var is_web = HttpContext.Current != null;
                var path = is_web && uri.StartsWith("~") ? HttpContext.Current.Server.MapPath(uri) : uri;
                path = Path.GetFullPath(path);
                if (!File.Exists(path)) throw new Exception(String.Format(
                    "{2} for \"{0}\" has failed: file \"{1}\" does not exist", uri, path, xml == null ? "DELETE" : "WRITE"));

                var s_xml = xml == null ? null : xml.ToString();
                if (s_xml == null) File.Delete(path);
                else
                {
                    var charset = s_xml.Extract(@"<meta\s+http-equiv=""Content-Type""\s+content=""text/html;charset=(?<charset>.*?)""");
                    File.WriteAllText(path, s_xml, charset == null ? Encoding.UTF8 : Encoding.GetEncoding(charset));
                }
            }
        }
Example #7
0
            public override String quote(String in_str)
            {
                if (new Regex("^~[A-Za-z0-9_-]+$").IsMatch(in_str))
                {
                    // If the string is just "~user" we can assume they
                    // mean "~user/".
                    //
                    return in_str + "/";
                }

                if (new Regex("^~[A-Za-z0-9_-]*/.*$").IsMatch(in_str))
                {
                    // If the string is of "~/path" or "~user/path"
                    // we must not escape ~/ or ~user/ from the shell.
                    //
                    int i = in_str.IndexOf('/') + 1;
                    if (i == in_str.Length)
                        return in_str;
                    return in_str.Slice(0, i) + base.quote(in_str.Substring(i));
                }

                return base.quote(in_str);
            }
Example #8
0
 protected override String custom_render_ptx(String core)
 {
     if (ctx.Version < SoftwareIsa.PTX_15)
     {
         return core;
     }
     else
     {
         var iof = core.IndexOf(",");
         var before = core.Slice(0, iof);
         var after = core.Slice(iof + 2, -1);
         return String.Format("{0}, [{1}];", before, after);
     }
 }