Ejemplo n.º 1
0
        /// <summary>
        /// Initializes a new instance of the <see cref="Austin.Net.HttpServer"></see> class.
        /// </summary>
        protected HttpServer()
        {
            bool isServer = false;
            foreach (Attribute a in this.GetType().GetCustomAttributes(true))
            {
                if (a.GetType() == typeof(HttpServerAttribute))
                {
                    HttpServerAttribute ha = (HttpServerAttribute)a;
                    this.m_server.Prefixes.Add(ha.Prefix);
                    this.m_serverName = ha.Name;
                    this.m_pathPrefix = new Uri(ha.Prefix.Replace("+", "localhost").Replace("*", "localhost")).AbsolutePath;

                    isServer = true;
                    break;
                }
            }
            if (!isServer)
                throw new HttpServerException("There is no HttpServerAttribute on this class.");

            foreach (MethodInfo m in this.GetType().GetMethods(BindingFlags.Public | BindingFlags.Instance))
            {
                //check to see if it is marked as a HttpServerMethod
                if (m.GetCustomAttributes(typeof(HttpServerMethodAttribute), true).Length == 1)
                {
                    HttpServerMethod hm = new HttpServerMethod(m);
                    this.m_functions.Add(hm.Name, hm);
                }
            }
        }
Ejemplo n.º 2
0
        private void writeContent(HttpServerMethod method, NameValueCollection query, XmlWriter writer)
        {
            try
            {
                StringBuilder content = new StringBuilder();
                XmlWriter wrc = XmlWriter.Create(content, CreateSettings(ConformanceLevel.Fragment));

                List<object> parameters = new List<object>();
                parameters.Add(wrc);
                foreach (HttpServerMethodParameter p in method.Parameters)
                {
                    string value = query[p.Name];
                    if (value == null)
                        throw new HttpServerException("Parameter missing.");

                    switch (p.Type)
                    {
                        case HttpServerMethodParameterType.Int32:
                            parameters.Add(Convert.ToInt32(value, CultureInfo.InvariantCulture));
                            break;
                        case HttpServerMethodParameterType.String:
                            parameters.Add(value);
                            break;
                    }
                }

                method.Invoke(this, parameters);

                wrc.Close();
                writer.WriteRaw(content.ToString());
            }
            catch (Exception ex)
            {
                writer.WriteStartElement("pre");
                WriteError(writer, ex.ToString());
                writer.WriteEndElement();
            }
        }