示例#1
0
        void server_OnRequest(Request req, Server server)
        {
            try
            {
                req.Response.MimeType = "text/html";
                req.Response.Code     = Server.StatusCodes.OK;

                if (req.Headers.URL.StartsWith("/video/") && req.Headers.URL.Length > 7)
                {
                    string device = req.Headers.URL.Substring(7);
                    foreach (DeviceWrapper dv in Devices)
                    {
                        if (dv.Hash == device)
                        {
                            if (req.Headers.AuthenticatedUser == Settings.Get("WebServer_UserName", "dave") &&
                                req.Headers.AuthenticatedPassword == Settings.Get("WebServer_Password", "dave"))
                            {
                                dv.Timeout = 0;
                                if (!dv.IsActive)
                                {
                                    dv.Activate();
                                }

                                int quality = 100;
                                if (req.Headers.QueryString.ContainsKey("q"))
                                {
                                    quality = int.Parse(req.Headers.QueryString["q"]);
                                }

                                if (dv.LastFrame != null)
                                {
                                    req.Response.Body     = GetJpeg(dv.LastFrame, quality);
                                    req.Response.MimeType = "image/jpeg";
                                }
                            }
                            else
                            {
                                req.Response.Code = Server.StatusCodes.UNAUTHORIZED;
                                req.Response.AddHeader("WWW-Authenticate", "Basic realm=\"CamServer3\"");
                                req.Response.BodyString = "Authentication is required for this resource. (Device-Level Auth Failure)";
                            }
                            return;
                        }
                    }
                }

                if (Settings.Get("WebServer_UserName", "") != "" &&
                    Settings.Get("WebServer_Password", "") != "")
                {
                    if (req.Headers.AuthenticatedUser != Settings.Get("WebServer_UserName", "") ||
                        req.Headers.AuthenticatedPassword != Settings.Get("WebServer_Password", ""))
                    {
                        req.Response.Code = Server.StatusCodes.UNAUTHORIZED;
                        req.Response.AddHeader("WWW-Authenticate", "Basic realm=\"CamServer3\"");
                        req.Response.BodyString = "Authentication is required for this resource. (Server-Level Auth Failure)";
                        return;
                    }
                }

                if (req.Headers.URL.StartsWith("/ctl/") && req.Headers.URL.Length > 5)
                {
                    string device = req.Headers.URL.Substring(7);
                    foreach (DeviceWrapper dv in Devices)
                    {
                        if (dv.Hash == device)
                        {
                            // PTZ, I/O, Sliders, Modes
                            if (req.Headers.QueryString.ContainsKey("ptz"))
                            {
                                DevicePT pt = (DevicePT)int.Parse(req.Headers.QueryString["ptz"]);
                                if (dv.ptdev != null)
                                {
                                    dv.ptdev.Move(pt);
                                }
                            }
                            return;
                        }
                    }
                }

                if (req.Headers.URL == "/list")
                {
                    string list = "";
                    foreach (DeviceWrapper dv in Devices)
                    {
                        list += dv.Name + "\r\n" + dv.Hash + "\r\n";
                    }
                    req.Response.BodyString = list;
                    return;
                }

                string resource = req.Headers.URL.Replace('/', System.IO.Path.DirectorySeparatorChar);
                resource = resource.Replace('\\', System.IO.Path.DirectorySeparatorChar);
                resource = resource.Replace("..", "");
                resource = resource.Replace(":", "");

                if (resource.Trim() == System.IO.Path.DirectorySeparatorChar.ToString())
                {
                    resource = "default.htm";
                }

                if (File.Exists("html" + System.IO.Path.DirectorySeparatorChar + resource))
                {
                    req.Response.Body     = File.ReadAllBytes("html" + System.IO.Path.DirectorySeparatorChar + resource);
                    req.Response.MimeType = MimeType.DetermineFromFile("html" + System.IO.Path.DirectorySeparatorChar + resource);
                    return;
                }

                req.Response.Code       = Server.StatusCodes.NOT_FOUND;
                req.Response.BodyString = "404: Resource Not Found";
            }
            catch (Exception ex)
            {
                Trace.WriteLine("Fatal HTTP Error: " + ex);
            }
        }