Exemple #1
0
        private Task _listen()
        {
            return(Task.Factory.StartNew(() =>
            {
                try
                {
                    while (IsListening)
                    {
                        HttpListenerContext ctx = _listener.GetContext();
                        HttpListenerRequest req = ctx.Request;
                        HttpListenerResponse resp = ctx.Response;

                        try
                        {
                            string accesslog = DateTime.Now.ToString("o");
                            accesslog += "\t" + string.Format("Request #: {0}", ++_requestCount);
                            accesslog += "\t" + req.Url.ToString();
                            accesslog += "\t" + req.HttpMethod;
                            accesslog += "\t" + req.UserHostAddress;
                            accesslog += "\t" + req.UserAgent;

                            string origin = _getOrigin(req);
                            accesslog += "\t" + origin;

                            bool forbidden = false;
                            if (config.URL == origin || config.allowedDomains.Contains(origin))
                            {
                                resp.AppendHeader("Access-Control-Allow-Origin", origin);
                            }
                            else
                            {
                                if (req.Url.AbsolutePath == "/permissions" && req.HttpMethod == "GET")
                                {
                                    resp.AppendHeader("Access-Control-Allow-Origin", origin);
                                }
                                else
                                {
                                    resp.AppendHeader("Access-Control-Allow-Origin", config.URL);
                                    forbidden = true;
                                }
                            }
                            if (req.HttpMethod == "OPTIONS")
                            {
                                resp.AddHeader("Access-Control-Allow-Headers", "*");
                                resp.StatusCode = (int)HttpStatusCode.OK;
                                resp.StatusDescription = "OK";
                                resp.Close();
                            }
                            else
                            {
                                if (forbidden)
                                {
                                    accesslog += "\tfailed\tsame origin";
                                    ServerConfig.appendLog(accesslog);
                                    responseForbidden(resp);
                                }
                                else
                                {
                                    ResponseCode respCode = ResponseCode.OK;
                                    if (req.Url.AbsolutePath == "/")
                                    {
                                        respCode = _home.handle(req, resp, accesslog);
                                    }
                                    else if (req.Url.AbsolutePath == "/permissions")
                                    {
                                        respCode = _permissions.handle(req, resp, accesslog, origin);
                                    }
                                    else if (req.Url.AbsolutePath == "/printers")
                                    {
                                        respCode = _printers.handle(req, resp, accesslog);
                                    }
                                    else if (req.Url.AbsolutePath == "/settings")
                                    {
                                        respCode = _settings.handle(req, resp, accesslog);
                                    }
                                    else
                                    {
                                        respCode = ResponseCode.NotFound;
                                    }

                                    switch (respCode)
                                    {
                                    case ResponseCode.NotFound:
                                        accesslog += "\tfailed\tnot found";
                                        ServerConfig.appendLog(accesslog);
                                        resp.StatusCode = (int)HttpStatusCode.NotFound;
                                        resp.StatusDescription = "NOT FOUND";
                                        resp.Close();
                                        break;

                                    case ResponseCode.Forbidden:
                                        responseForbidden(resp);
                                        break;
                                    }
                                }
                            }
                        }
                        catch (Exception e)
                        {
                            ServerConfig.appendLog("Error: " + e.Message + "\n" + e.StackTrace);
                            resp.StatusCode = (int)HttpStatusCode.InternalServerError;
                            resp.StatusDescription = "INTERNAL SERVER ERROR";
                            resp.Close();
                        }
                    }
                } catch (Exception e)
                {
                    if (!(e is HttpListenerException && (e as HttpListenerException).ErrorCode == 995))
                    {
                        ServerConfig.appendLog(e.GetType().Name + ": " + e.Message + "\n" + e.StackTrace);
                    }
                    return false;
                }
                return true;
            }));
        }
Exemple #2
0
 public void responseJSON(HttpListenerResponse resp, object obj)
 {
     byte[] data = Encoding.UTF8.GetBytes(ServerConfig.toJSON(obj));
     response(resp, data, "application/json");
 }