예제 #1
0
        public PanResponse OnRequest(PanRequest request)
        {
            if (request.Address.Length < 1)
            {
                return(PanResponse.ReturnFile("./Website/index.html"));
            }
            else if (request.Address[0].ToLower() == "index")
            {
                return(PanResponse.ReturnFile("./Website/index.html"));
            }
            else if (request.Address[0].ToLower() == "styles.css")
            {
                return(PanResponse.ReturnFile("./Website/styles.css"));
            }
            else if (request.Address[0].ToLower() == "app.js")
            {
                return(PanResponse.ReturnFile("./Website/app.js"));
            }
            else if (request.Address[0].ToLower() == "getsignals")
            {
                return(PanResponse.ReturnJson(signals));
            }
            else if (request.Address[0].ToLower() == "getdevices")
            {
                return(PanResponse.ReturnJson(devices));
            }
            else if (request.Address[0].ToLower() == "setsignals")
            {
                try
                {
                    string        json    = System.Web.HttpUtility.UrlDecode(request.Data["signals"]);
                    List <Signal> signals = new List <Signal>();
                    signals = Newtonsoft.Json.JsonConvert.DeserializeObject <List <Signal> >(json);

                    XmlDocument xml   = new XmlDocument();
                    XmlElement  xRoot = xml.CreateElement("signals");
                    xml.AppendChild(xRoot);

                    foreach (var s in signals)
                    {
                        XmlElement sig = xml.CreateElement("signal");
                        sig.SetAttribute("id", s.id.ToString());
                        sig.SetAttribute("name", s.name);
                        foreach (var d in s.devices)
                        {
                            XmlElement dev = xml.CreateElement("device");
                            dev.SetAttribute("id", d.id.ToString());
                            dev.SetAttribute("name", d.name);
                            sig.AppendChild(dev);
                        }
                        xRoot.AppendChild(sig);
                    }

                    MemoryStream xmlstream = new MemoryStream();
                    xml.Save(xmlstream);
                    xmlstream.Position = 0;

                    return(PanResponse.ReturnFile(xmlstream, "text/xml"));
                }
                catch (Exception ex)
                {
                    return(PanResponse.ReturnCode(500, ex.Message));
                }
            }

            return(PanResponse.ReturnEmtry());
        }
예제 #2
0
        protected void WebsiteLife()
        {
            try
            {
                while (Listener.IsListening)
                {
                    HttpListenerContext context = Listener.GetContext();
                    Task.Factory.StartNew(() =>
                    {
                        Stream output = context.Response.OutputStream;
                        //output.Position = 0;

                        // GET Cookies
                        List <PanCookie> cookies = new List <PanCookie>();
                        foreach (Cookie c in context.Request.Cookies)
                        {
                            cookies.Add(new PanCookie(c.Name, c.Value, c.Path, c.Expires));
                        }

                        // GET Headers
                        Dictionary <string, string[]> headers = new Dictionary <string, string[]>();
                        System.Collections.Specialized.NameValueCollection cheaders = context.Request.Headers;
                        foreach (string key in cheaders.AllKeys)
                        {
                            string current_key     = key;
                            string[] currentvalues = cheaders.GetValues(current_key);
                        }

                        // GET Data
                        string url         = context.Request.RawUrl;        // Url
                        string method      = context.Request.HttpMethod;    // Method
                        Stream inputStream = new MemoryStream();            // Body
                        bool hasEntityBody = context.Request.HasEntityBody; // Has Entity Body
                        if (hasEntityBody)
                        {
                            context.Request.InputStream.CopyTo(inputStream);
                            inputStream.Position = 0;
                        }
                        string[] acceptTypes      = context.Request.AcceptTypes;     // Accept Types
                        Encoding contentEncoding  = context.Request.ContentEncoding; // Content Encoding
                        string contentType        = context.Request.ContentType;     // Content Type
                        bool isLocal              = context.Request.IsLocal;         // Is Local
                        string userAgent          = context.Request.UserAgent;       // User Agent
                        string[] userLanguages    = context.Request.UserLanguages;   // User Languages
                        IPEndPoint remoteEndPoint = context.Request.RemoteEndPoint;  // User IP
                        string userIP             = remoteEndPoint.Address.ToString();

                        PanRequest request   = new PanRequest(method, url, inputStream, cookies, hasEntityBody, acceptTypes, contentEncoding, contentType, headers, isLocal, userAgent, userLanguages, userIP);
                        PanResponse response = onRequest.Invoke(request);

                        // SET Code
                        int code = response.Code;
                        context.Response.StatusCode = code;

                        // SET
                        context.Response.ContentType = response.MIME;

                        // SET Cookies
                        if (response.Cookies == null)
                        {
                            response.Cookies = new List <PanCookie>();
                        }
                        foreach (PanCookie c in response.Cookies)
                        {
                            string cookie = "";
                            cookie       += (c.Name + "=" + (c.Value == null ? "" : c.Value));
                            if (c.Expires != null)
                            {
                                cookie += ("; Expires=" + c.Expires.ToString());
                            }
                            cookie += ("; Path=" + c.Path);
                            context.Response.Headers.Add("Set-Cookie", cookie);
                        }

                        response.OutputStream.CopyTo(output);
                        response.OutputStream.Close();
                        response.OutputStream.Dispose();
                        output.Close();
                        context.Response.Close();
                    });
                }
            }
            catch (ThreadAbortException ex)
            {
                //
            }
            catch (Exception ex)
            {
                throw new WebsiteException(ex);
            }
        }