Beispiel #1
0
        /**
         * This method runs the interface.
         */
        protected void Run()
        {
            while (_running)
            {
                HttpListenerContext context = null;

                try {
                    context = _listener.GetContext();
                } catch (HttpListenerException e) {
                    if (!_running)
                    {
                        return;
                    }
                    Console.WriteLine(e);
                }

                HttpListenerRequest  request  = context.Request;
                HttpListenerResponse response = context.Response;
                string responseString         = String.Empty;
                response.ContentType = "text/xml";

                // Process api post request from the Javascript interface
                if (request.RawUrl == "/api")
                {
                    StreamReader reader = new StreamReader(request.InputStream,
                                                           request.ContentEncoding);

                    string postData = reader.ReadToEnd();
                    request.InputStream.Close();
                    reader.Close();
                    Brunet.ProtocolLog.Write(SocialLog.SVPNLog, postData);
                    responseString = Process(SocialUtils.DecodeUrl(postData));
                }
                else if (request.RawUrl.StartsWith("/getapi"))
                {
                    string getData = request.RawUrl.Substring(8);
                    Brunet.ProtocolLog.Write(SocialLog.SVPNLog, getData);
                    responseString = Process(SocialUtils.DecodeUrl(getData));
                }
                // Cross-domain request made by Flash clients
                else if (request.RawUrl == "/crossdomain.xml")
                {
                    responseString = CrossDomainXML;
                }
                else if (request.RawUrl == "/socialvpn.js")
                {
                    using (StreamReader text = new StreamReader("socialvpn.js"))
                    {
                        responseString = text.ReadToEnd();
                    }
                    response.ContentType = "text/javascript";
                }
                else if (request.RawUrl == "/socialvpn.css")
                {
                    using (StreamReader text = new StreamReader("socialvpn.css"))
                    {
                        responseString = text.ReadToEnd();
                    }
                    response.ContentType = "text/css";
                }
                // Return html content for page display
                else
                {
                    responseString       = HTMLText;
                    response.ContentType = "text/html";
                }

                byte[] buffer = System.Text.Encoding.UTF8.GetBytes(responseString);
                response.ContentLength64 = buffer.Length;
                response.AddHeader("Cache-Control", "No-cache");
                System.IO.Stream output = response.OutputStream;
                output.Write(buffer, 0, buffer.Length);
                output.Close();
            }
        }