Exemple #1
0
        public static string GetHttpResponse(HttpListenerRequest request)
        {
            string response = "";

            string body;

            using (var breader = new StreamReader(request.InputStream, request.ContentEncoding))
            {
                body = breader.ReadToEnd();
            }

            switch (request.HttpMethod)
            {
            case "POST":
                JObject json;
                try
                {
                    json = JObject.Parse(body);
                }
                catch (Exception e)
                {
                    Console.WriteLine("[JSON]:{0}:{1}", e.Message, e.StackTrace);
                    return("{\"error\":\"Invalid Json\"}");
                }
                string[] path = request.Url.AbsolutePath.ToLower().Split('/');
                path = path.Where(w => w != path[0]).ToArray();
                string at = "";
                if (path.Length >= 1)
                {
                    if (accessToken.Length > 0)
                    {
                        if (path.Length > 1)
                        {
                            if (path[0].ToLower() != accessToken)
                            {
                                response = "{\"error\":\"token invalid\"}";
                                break;
                            }
                            else
                            {
                                at = path[0];
                                List <string> pathl = new List <string>(path);
                                pathl.RemoveAt(0);
                                path = pathl.ToArray();
                            }
                        }
                        else
                        {
                            response = "{\"error\":\"token error\"}";
                            break;
                        }
                    }
                    switch (path[0])
                    {
                    case "service":

                        List <CommandResult> results = new List <CommandResult>();

                        if (json["commands"].GetType() == typeof(Newtonsoft.Json.Linq.JArray))
                        {
                            JArray commands = (JArray)json["commands"];
                            foreach (JObject command in commands)
                            {
                                var type = Type.GetType("SteelCityAutomatonServiceCommands." + ((string)command["command"]).ToLower());
                                if (type != null)
                                {
                                    ServiceCommand cmd = (ServiceCommand)Activator.CreateInstance(type);
                                    cmd.Setup(command);
                                    cmd.Excecute(automatons, at);
                                    results.Add(cmd.result);
                                }
                                else
                                {
                                    CommandResult result = new CommandResult(((string)command["command"]).ToLower());
                                    result.success = false;
                                    result.data    = "unrecognized command";
                                    results.Add(result);
                                    Console.WriteLine("CMD NOT RECOGNIZED - {0} : {1}", ((string)command["command"]).ToLower(), "null");
                                }
                            }
                            CommandResponse resp = new CommandResponse((string)json["label"], results);
                            response = resp.serialise();
                        }
                        break;

                    case "automaton":
                        if (path.Length > 1)
                        {
                            string session = path[1];
                            if (automatons.ContainsKey(at + session))
                            {
                                response = automatons[at + session].processCommands(json);
                            }
                            else
                            {
                                response = "{\"error\":\"unrecognized session id\"}";
                            }
                        }
                        else
                        {
                            response = "{\"error\":\"missing session id\"}";
                        }
                        break;

                    default:
                        Console.WriteLine("------Parts------");
                        foreach (string s in path)
                        {
                            Console.WriteLine(s);
                        }
                        Console.WriteLine("-----------------");
                        response = "{\"error\":\"unrecognized endpoint, not automaton or service\"}";
                        break;
                    }
                }
                else
                {
                    response = "{\"error\":\"unrecognized endpoint, not specifed\"}";
                }
                break;

            case "GET":
                if (webPage)
                {
                    string pathf = request.Url.AbsolutePath.ToLower();
                    if (pathf.Length < 3)
                    {
                        pathf = "BasicWebTest.html";
                    }
                    pathf = "Resources/" + pathf;
                    //string f = @"Resources/BasicWebTest.html";
                    if (File.Exists(pathf))
                    {
                        response = File.ReadAllText(pathf);
                    }
                    else
                    {
                        response = "{\"error\":\"BasicWebTest Missing\"}";
                    }
                }
                else
                {
                    response = "{\"error\":\"Web Page not enabled\"}";
                }
                break;

            default:
                response = "{\"error\":\"Unsuported Method\"}";
                break;
            }

            return(response);
        }