예제 #1
0
 public static void WriteResponse(Stream stream, HTTPResponse response)
 {
     byte[] responseHeader = Encoding.UTF8.GetBytes(response.ToString());
     stream.Write(responseHeader, 0, responseHeader.Length);
     stream.Write(response.Content, 0, response.Content.Length);
 }
예제 #2
0
        public string get(HTTPRequest r)
        {
            string body   = "Request method=" + r.method;
            int    status = 400;
            Dictionary <string, string> arguments = new Dictionary <string, string>();

            string[] path_query = r.url.Split('?');
            string   path       = path_query[0];


            try
            {
                if (r.method != "GET")
                {
                    throw new SamanaException(405, "Method not Allowed");
                }
                if (path_query.Length > 2)
                {
                    throw new SamanaException(500, "Bad Query");
                }

                if (path_query.Length > 1)
                {
                    string[] args = path_query[1].Split('&');
                    for (int i = 0; i < args.Length; i++)
                    {
                        string[] t = args[i].Split('=');
                        if (t.Length == 1)
                        {
                            arguments.Add(t[0], "1");
                        }
                        else if (t.Length == 2)
                        {
                            arguments.Add(t[0], t[1]);
                        }
                        else
                        {
                            throw new SamanaException(500, "Bad Query");
                        }
                    }
                }
                if (path == "/cpu")
                {
                    body   = cpudata.json();
                    status = 200;
                }
                else if (path == "/ram")
                {
                    body   = ramdata.json();
                    status = 200;
                }
                else if (path == "/uptime")
                {
                    body   = uptime.ToString();
                    status = 200;
                }
                else if (path == "/syslog")
                {
                    body   = syslog.json();
                    status = 200;
                }
                else if (path == "/applog")
                {
                    body   = applog.json();
                    status = 200;
                }
                else if (path == "/services")
                {
                    body   = Services.json();
                    status = 200;
                }
                else if (path == "/hddrives")
                {
                    body   = hdlist.json();
                    status = 200;
                }
                else if (path == "/processes")
                {
                    body   = allProcesses.json();
                    status = 200;
                }
                else if (path == "/log")
                {
                    try
                    {
                        int    e = -1;
                        int    h = 2;
                        int    l = 3;
                        string logname;

                        if (!arguments.ContainsKey("logname"))
                        {
                            throw new Exception("Missing log name");
                        }

                        logname = System.Uri.UnescapeDataString(arguments["logname"]);
                        if (arguments.ContainsKey("eventId"))
                        {
                            if (!int.TryParse(arguments["eventId"], out e))
                            {
                                e = -1;
                            }
                        }

                        if (arguments.ContainsKey("hours"))
                        {
                            if (!int.TryParse(arguments["hours"], out h))
                            {
                                h = 2;
                            }
                        }

                        if (arguments.ContainsKey("level"))
                        {
                            if (!int.TryParse(arguments["level"], out l))
                            {
                                l = 3;
                            }
                        }

                        EventList evtlist = new EventList(logname, l, h, 1, config.debug_level);
                        evtlist.Poll(1);

                        body   = evtlist.json();
                        status = 200;
                    }
                    catch (Exception e)
                    {
                        evtlog.error(e.Message + e.StackTrace, 2);
                        throw new SamanaException(500, "Bad Query");
                    }
                }
                else if (path == "/sessions")
                {
                    sessions.Poll();
                    body   = sessions.json();
                    status = 200;
                }
                else if (path == "/version")
                {
                    body   = "\"" + this.version + "\"";
                    status = 200;
                }
                else
                {
                    throw new SamanaException(404, "Not Found");
                }
            }
            catch (SamanaException e)
            {
                status = e.status;
                body   = e.body;
            }

            HTTPResponse res = new HTTPResponse(status, body);

            return(res.ToString());
        }