예제 #1
0
        public static bool TryConvertUnsafeString(IManosContext ctx, Type type, ParameterInfo param, UnsafeString unsafe_str_value, out object data)
        {
            if (type == typeof(UnsafeString))
            {
                data = unsafe_str_value;
                return(true);
            }

            string str_value = unsafe_str_value == null ? null : unsafe_str_value.SafeValue;

            if (TryConvertFormData(type, str_value, out data))
            {
                return(true);
            }

            if (str_value == null && param.DefaultValue != DBNull.Value)
            {
                data = param.DefaultValue;
                return(true);
            }

            try {
                data = Convert.ChangeType(str_value, type);
            } catch {
                Console.Error.WriteLine("Error while converting '{0}' to '{1}'.", str_value, type);

                data = null;
                return(false);
            }

            return(true);
        }
예제 #2
0
        public static bool TryGetDataForParamList(ParameterInfo [] parameters, ManosApp app, IManosContext ctx, out object [] data)
        {
            data = new object [parameters.Length];

            int param_start = 1;

            data [0] = ctx;

            if (typeof(ManosApp).IsAssignableFrom(parameters [1].ParameterType))
            {
                data [1] = app;
                ++param_start;
            }

            for (int i = param_start; i < data.Length; i++)
            {
                string       name = parameters [i].Name;
                UnsafeString strd = ctx.Request.Data.Get(name);

                if (!TryConvertType(ctx, parameters [i].ParameterType, strd, out data [i]))
                {
                    return(false);
                }
            }

            return(true);
        }
예제 #3
0
        /// <summary>
        /// Assign a value into this dictionary with the specified key.
        /// </summary>
        /// <param name="key">
        /// A <see cref="System.String"/>
        /// </param>
        /// <param name="value">
        /// A <see cref="UnsafeString"/>
        /// </param>
        public void Set(string key, UnsafeString value)
        {
            int open = key.IndexOf('[');

            if (open == -1)
            {
                dictionary [key] = value;
                return;
            }

            string elkey = key.Substring(0, open);
            int    close = key.IndexOf(']');

            if (close == -1 || close < open)
            {
                dictionary [elkey] = value;
                return;
            }

            object col;

            if (close == open + 1)
            {
                List <UnsafeString> list = null;

                if (dictionary.TryGetValue(elkey, out col))
                {
                    list = col as List <UnsafeString>;
                    if (list != null)
                    {
                        list.Add(value);
                        return;
                    }
                }

                list = new List <UnsafeString> ();
                list.Add(value);
                dictionary [elkey] = list;

                return;
            }

            Dictionary <string, UnsafeString> dict = null;
            string dname = UnsafeString.Escape(key.Substring(open + 1, close - open - 1));

            if (dictionary.TryGetValue(elkey, out col))
            {
                dict = col as Dictionary <string, UnsafeString>;
                if (dict != null)
                {
                    dict [dname] = value;
                    return;
                }
            }

            dict               = new Dictionary <string, UnsafeString> ();
            dict [dname]       = value;
            dictionary [elkey] = dict;
        }
예제 #4
0
        /// <summary>
        /// Get a "safe" string from the dictionary, or, if the key doesn't exist in the dictionary, null.
        /// </summary>
        /// <param name="key">
        /// </param>
        /// <returns>
        /// The "safe" version of the value that is stored in the dictionary.
        /// </returns>
        public string GetString(string key)
        {
            UnsafeString str = Get(key);

            if (str == null)
            {
                return(null);
            }

            return(str.SafeValue);
        }
예제 #5
0
        /// <summary>
        /// The "unsafe" version of the value that is stored in this dictionary, or "null" if no value is stored for the specified key.
        /// </summary>
        /// <param name="key">
        /// A <see cref="System.String"/>
        /// </param>
        /// <returns>
        /// A <see cref="UnsafeString"/>
        /// </returns>
        public UnsafeString Get(string key)
        {
            UnsafeString value = null;

            if (dictionary.TryGetValue(key, out value))
            {
                return(value);
            }

            if (children != null)
            {
                children.Where(d => (value = d.Get(key)) != null).FirstOrDefault();
            }

            return(value);
        }
예제 #6
0
        public static bool TryConvertType(IManosContext ctx, Type type, UnsafeString unsafe_str_value, out object data)
        {
            if (type == typeof (UnsafeString)) {
                data = unsafe_str_value;
                return true;
            }

            string str_value = unsafe_str_value.SafeValue;

            try {
                data = Convert.ChangeType (str_value, type);
            } catch {
                Console.Error.WriteLine ("Error while converting '{0}' to '{1}'.", str_value, type);

                data = null;
                return false;
            }

            return true;
        }
예제 #7
0
        public byte [] GetBody()
        {
            StringBuilder data = null;

            if (PostBody != null)
            {
                data = new StringBuilder();
                data.Append(PostBody);
            }

            if (post_data != null)
            {
                data = new StringBuilder();
                bool first = true;
                foreach (string key in post_data.Keys)
                {
                    if (!first)
                    {
                        data.Append('&');
                    }
                    first = false;

                    UnsafeString s = post_data.Get(key);
                    if (s != null)
                    {
                        data.AppendFormat("{0}={1}", key, s.UnsafeValue);
                        continue;
                    }
                }
            }

            if (data == null)
            {
                return(null);
            }

            return(ContentEncoding.GetBytes(data.ToString()));
        }
예제 #8
0
 protected override void Write(UnsafeString message)
 {
     Console.WriteLine($"Thread {Thread.CurrentThread.ManagedThreadId} \u00A6 " + message);
 }
예제 #9
0
 public void Set(string key, UnsafeString value)
 {
     dictionary [key] = value;
 }
예제 #10
0
 public void Set(string key, string value)
 {
     dictionary [key] = new UnsafeString (value);
 }
        public static bool TryConvertUnsafeString(IContext ctx, Type type, ParameterInfo param, UnsafeString unsafe_str_value, out object data)
        {
            if (type == typeof (UnsafeString)) {
                data = unsafe_str_value;
                return true;
            }

            string str_value = unsafe_str_value == null ? null : unsafe_str_value.SafeValue;

            if (TryConvertFormData (type, str_value, out data))
                return true;

            if (str_value == null && param.DefaultValue != DBNull.Value) {
                data = param.DefaultValue;
                return true;
            }

            try {
                data = Convert.ChangeType (str_value, type);
            } catch {
                Console.Error.WriteLine ("Error while converting '{0}' to '{1}'.", str_value, type);

                data = null;
                return false;
            }

            return true;
        }
예제 #12
0
        public static bool TryConvertType(IManosContext ctx, string name, ParameterInfo param, out object data)
        {
            Type dest = param.ParameterType;

            if (dest.IsArray)
            {
                var list = ctx.Request.Data.GetList(name);
                if (list != null)
                {
                    Type  element = dest.GetElementType();
                    IList arr     = Array.CreateInstance(element, list.Count);
                    for (int i = 0; i < list.Count; i++)
                    {
                        object elem_data;
                        if (!TryConvertUnsafeString(ctx, element, param, list [i], out elem_data))
                        {
                            data = null;
                            return(false);
                        }
                        arr [i] = elem_data;
                    }
                    data = arr;
                    return(true);
                }
            }

            if (dest.GetInterface("IDictionary") != null)
            {
                var dict = ctx.Request.Data.GetDict(name);
                if (dict != null)
                {
                    Type        eltype = typeof(UnsafeString);
                    IDictionary dd     = (IDictionary)Activator.CreateInstance(dest);
                    if (dest.IsGenericType)
                    {
                        Type [] args = dest.GetGenericArguments();
                        if (args.Length != 2)
                        {
                            throw new Exception("Generic Dictionaries must contain two generic type arguments.");
                        }
                        if (args [0] != typeof(string))
                        {
                            throw new Exception("Generic Dictionaries must use strings for their keys.");
                        }
                        eltype = args [1];                         // ie the TValue in Dictionary<TKey,TValue>
                    }
                    foreach (string key in dict.Keys)
                    {
                        object elem_data;
                        if (!TryConvertUnsafeString(ctx, eltype, param, dict [key], out elem_data))
                        {
                            data = null;
                            return(false);
                        }
                        dd.Add(key, elem_data);
                    }
                    data = dd;
                    return(true);
                }
            }

            UnsafeString strd = ctx.Request.Data.Get(name);

            return(TryConvertUnsafeString(ctx, dest, param, strd, out data));
        }
예제 #13
0
 /// <summary>
 /// Assign a value into this dictionary with the specified key.
 /// </summary>
 /// <param name="key">
 /// A <see cref="System.String"/>
 /// </param>
 /// <param name="value">
 /// A <see cref="UnsafeString"/>
 /// </param>
 public void Set(string key, UnsafeString value)
 {
     dictionary [key] = value;
 }
예제 #14
0
 /// <summary>
 /// Assign a value into this dictionary with the specified key.
 /// </summary>
 /// <param name="key">
 /// A <see cref="System.String"/>
 /// </param>
 /// <param name="value">
 /// A <see cref="System.String"/>
 /// </param>
 public void Set(string key, string value)
 {
     dictionary [key] = new UnsafeString(value);
 }
        protected override void Write(UnsafeString message)
        {
            var log           = ((ServiceStackLoggingTypeSource)TypeSource).Log;
            var messageString = message.ToString();

            switch (Level)
            {
            case LogLevel.None:
                break;

            case LogLevel.Trace:
            case LogLevel.Debug:
                if (Exception == null)
                {
                    log.Debug(messageString);
                }
                else
                {
                    log.Debug(messageString, Exception);
                }

                break;

            case LogLevel.Info:
                if (Exception == null)
                {
                    log.Info(messageString);
                }
                else
                {
                    log.Info(messageString, Exception);
                }

                break;

            case LogLevel.Warning:
                if (Exception == null)
                {
                    log.Warn(messageString);
                }
                else
                {
                    log.Warn(messageString, Exception);
                }

                break;

            case LogLevel.Error:
                if (Exception == null)
                {
                    log.Error(messageString);
                }
                else
                {
                    log.Error(messageString, Exception);
                }

                break;

            case LogLevel.Critical:
                if (Exception == null)
                {
                    log.Fatal(messageString);
                }
                else
                {
                    log.Fatal(messageString, Exception);
                }

                break;
            }
        }
예제 #16
0
 /// <inheritdoc />
 protected override void Write(UnsafeString message)
 {
     Log.Write(TranslateLevel(this.Level), "PostSharp", this, null, this.Exception, LogWriteMode.Queued,
               null, this.TypeSource.Role + "." + this.RecordKind.ToString(), message.ToString(), null);
 }