コード例 #1
0
        public ProcessingResult Process(RequestContext requestcontext)
        {
            IRequest request = requestcontext.Request;
            IResponse response = requestcontext.Response;
            IHttpContext context = requestcontext.HttpContext;

            if (request.Method == Method.Post)
            {
                Console.ForegroundColor = ConsoleColor.Yellow;
                Console.WriteLine("({0}) <Web Interface> AJAX request from {1} {2} {3} {4}", DateTime.Now.ToString("hh:mm"), context.RemoteEndPoint, request.HttpVersion, request.Method, request.Uri.AbsoluteUri);

                if (HasParameter(request.Parameters, request.Parameters.Get("rooturl")))
                {
                    if (SessionManager.Current == null)
                    {
                        SessionManager.Create();
                        SessionManager.Current.FirstRun = true;
                        SessionManager.Save(SessionManager.Current);
                        Console.WriteLine("({0}) <Web Interface> Created new session with id {1}", DateTime.Now.ToString("hh:mm"), SessionManager.Current.SessionId);
                    }
                    Console.WriteLine("({0}) <Web Interface> AJAX parameters width={1}&height={2}&rooturl={3}", DateTime.Now.ToString("hh:mm"), request.Parameters.Get("width").Value, request.Parameters.Get("height").Value, request.Parameters.Get("rooturl").Value);
                    response.ContentType = new ContentTypeHeader("application/json");
                    response.Status = HttpStatusCode.OK;
                    var stream = new MemoryStream(Encoding.UTF8.GetBytes("{\"session\":\"" + SessionManager.Current.SessionId + "\",\"data\":\"\"}"));
                    response.ContentLength.Value = stream.Length;
                    var generator = new ResponseWriter();
                    generator.SendHeaders(context, response);
                    generator.SendBody(context, stream);
                    Console.WriteLine("({0}) <Web Interface> AJAX response {1}", DateTime.Now.ToString("hh:mm"), "{\"session\":\"" + SessionManager.Current.SessionId + "\",\"data\":\"\"}");
                }

                if (HasParameter(request.Parameters, request.Parameters.Get("session")))
                {
                    Console.WriteLine("({0}) <Web Interface> AJAX parameters width={1}&height={2}&session={3}", DateTime.Now.ToString("hh:mm"), request.Parameters.Get("width").Value, request.Parameters.Get("height").Value, request.Parameters.Get("session").Value);
                    //Console.WriteLine("({0}) <Web Interface> Trying to load session with id {1}", DateTime.Now.ToString("hh:mm"), request.Parameters.Get("session").Value);
                    session = SessionManager.Load(request.Parameters.Get("session").Value);

                    if (session == null)
                    {
                        Console.WriteLine("({0}) <Web Interface> Failed to load session with id {1}", DateTime.Now.ToString("hh:mm"), request.Parameters.Get("session").Value);
                        response.ContentType = new ContentTypeHeader("application/json");
                        response.Status = HttpStatusCode.OK;
                        var stream = new MemoryStream(Encoding.UTF8.GetBytes("{\"session\":\"" + request.Parameters.Get("session").Value + "\",\"data\":\"Failed to load session !\"}"));
                        response.ContentLength.Value = stream.Length;
                        var generator = new ResponseWriter();
                        generator.SendHeaders(context, response);
                        generator.SendBody(context, stream);
                        Console.WriteLine("({0}) <Web Interface> AJAX response {1}", DateTime.Now.ToString("hh:mm"), "{\"session\":\"" + request.Parameters.Get("session").Value + "\",\"data\":\"Failed to load session !\"}");
                    }
                    else
                    {
                        //Console.WriteLine("({0}) <Web Interface> Loaded session with id {1}", DateTime.Now.ToString("hh:mm"), request.Parameters.Get("session").Value);
                        if (request.Parameters.Get("session").Value == session.SessionId)
                        {
                            if (HasParameter(request.Parameters, request.Parameters.Get("keys")))
                            {
                                lastkeys = request.Parameters.Get("keys").Value;
                                while (lastkeys != null)
                                {
                                    Thread.Sleep(50);
                                    if (lastkeys == null)
                                    {
                                        if (request.Parameters.Get("session").Value == lastsession)
                                        {
                                            response.ContentType = new ContentTypeHeader("text/html; charset=utf-8");
                                            response.Status = HttpStatusCode.OK;
                                            var stream2 = new MemoryStream(Encoding.UTF8.GetBytes("OK"));
                                            response.ContentLength.Value = stream2.Length;
                                            var generator2 = new ResponseWriter();
                                            generator2.SendHeaders(context, response);
                                            generator2.SendBody(context, stream2);
                                            Console.WriteLine("({0}) <Web Interface> AJAX response {1}", DateTime.Now.ToString("hh:mm"), "OK");
                                            lastsession = null;
                                            break;
                                        }
                                    }
                                }
                            }
                            else
                            {
                                if (session.FirstRun)
                                {
                                    lastkeys = null;
                                    response.ContentType = new ContentTypeHeader("application/json");
                                    response.Status = HttpStatusCode.OK;
                                    var stream = new MemoryStream(Encoding.UTF8.GetBytes("{\"session\":\"" + session.SessionId + "\",\"data\":\"Connected to server-side.\\r\\nWelcome to WCell.Terminal !\\r\\n\"}"));
                                    response.ContentLength.Value = stream.Length;
                                    var generator = new ResponseWriter();
                                    generator.SendHeaders(context, response);
                                    generator.SendBody(context, stream);
                                    Console.WriteLine("({0}) <Web Interface> AJAX response {1}", DateTime.Now.ToString("hh:mm"), "{\"session\":\"" + session.SessionId + "\",\"data\":\"Connected to server-side. Welcome to WCell.Terminal !\"}");
                                    session.FirstRun = false;
                                    SessionManager.Save(session);
                                }
                                else
                                {
                                    while (lastkeys == null)
                                    {
                                        Thread.Sleep(50);
                                        if (lastkeys != null)
                                        {
                                            string stringValue = "";
                                            if (lastkeys.Length == 2)
                                            {
                                                int value = Convert.ToInt32(lastkeys, 16);
                                                if (value <= Int32.MaxValue)
                                                {
                                                    stringValue = Char.ConvertFromUtf32(value);
                                                }
                                                else
                                                {
                                                    /*Console.WriteLine("({0}) <Web Interface> Problem with lastkeys ({1}): {2}", DateTime.Now.ToString("hh:mm"), e, lastkeys);
                                                    stringValue = "\\r\\nProblem with lastkeys: " + lastkeys + "\\r\\n";*/
                                                    if (lastkeys.Length > 2)
                                                    {
                                                        string[] strArray = SplitString(lastkeys, 2);
                                                        if (strArray != null)
                                                        {
                                                            foreach (string str in strArray)
                                                            {
                                                                if (!String.IsNullOrWhiteSpace(str))
                                                                {
                                                                    value = Convert.ToInt32(str, 16);
                                                                    stringValue += Char.ConvertFromUtf32(value);
                                                                }
                                                            }
                                                        }
                                                    }
                                                }
                                            }
                                            else
                                            {
                                                /*Console.WriteLine("({0}) <Web Interface> Problem with lastkeys ({1}): {2}", DateTime.Now.ToString("hh:mm"), e, lastkeys);
                                                stringValue = "\\r\\nProblem with lastkeys: " + lastkeys + "\\r\\n";*/
                                                if (lastkeys.Length > 2)
                                                {
                                                    string[] strArray = SplitString(lastkeys, 2);
                                                    if (strArray != null)
                                                    {
                                                        foreach (string str in strArray)
                                                        {
                                                            if (!String.IsNullOrWhiteSpace(str))
                                                            {
                                                                int value = Convert.ToInt32(str, 16);
                                                                stringValue += Char.ConvertFromUtf32(value);
                                                            }
                                                        }
                                                    }
                                                }
                                            }
                                            if (stringValue == "\r")
                                            {
                                                stringValue = "\\r\\n";
                                            }
                                            if (stringValue == "\x7f")
                                            {
                                                stringValue = "\\b \\b";
                                            }
                                            if (stringValue == "\f")
                                            {
                                                stringValue = "\\f";
                                            }
                                            if (stringValue == "\t")
                                            {
                                                stringValue = "\\t";
                                            }
                                            if (lastsession == null)
                                            {
                                                lastsession = session.SessionId;
                                            }
                                            response.ContentType = new ContentTypeHeader("application/json");
                                            response.Status = HttpStatusCode.OK;
                                            var stream = new MemoryStream(Encoding.UTF8.GetBytes("{\"session\":\"" + lastsession + "\",\"data\":\"" + stringValue + "\"}"));
                                            response.ContentLength.Value = stream.Length;
                                            var generator = new ResponseWriter();
                                            generator.SendHeaders(context, response);
                                            generator.SendBody(context, stream);
                                            Console.WriteLine("({0}) <Web Interface> AJAX response {1}", DateTime.Now.ToString("hh:mm"), "{\"session\":\"" + lastsession + "\",\"data\":\"" + stringValue + "\"}");
                                            lastkeys = null;
                                            break;
                                        }
                                    }
                                }
                            }
                        }
                        else
                        {
                            Console.WriteLine("({0}) <Web Interface> AJAX response {1}", DateTime.Now.ToString("hh:mm"), "Invalid Session: " + request.Parameters.Get("session").Value);
                        }
                    }
                }
                return ProcessingResult.Abort;
            }
            else
            {
                if (context.IsSecure)
                {
                    Console.ForegroundColor = ConsoleColor.Yellow;
                    Console.WriteLine("({0}) <Web Interface> Secure HTTP request from {1} {2} {3} {4}", DateTime.Now.ToString("hh:mm"), context.RemoteEndPoint, request.HttpVersion, request.Method, request.Uri.AbsoluteUri);
                }
                else
                {
                    Console.ForegroundColor = ConsoleColor.Yellow;
                    Console.WriteLine("({0}) <Web Interface> HTTP request from {1} {2} {3} {4}", DateTime.Now.ToString("hh:mm"), context.RemoteEndPoint, request.HttpVersion, request.Method, request.Uri.AbsoluteUri);
                }
            }
            return ProcessingResult.Continue;
        }
コード例 #2
0
 public static void Save(SessionManager session)
 {
     _sessionProvider.Save(session);
 }