Example #1
0
        public override void OnHttpGet(object sender, HttpRequestEventArgs e)
        {
            var req = e.Request;
            var res = e.Response;

            var path = req.RawUrl;

            if (path == "/")
            {
                path += "index.html";
            }

            byte[] contents;
            if (!e.TryReadFile(path, out contents))
            {
                res.StatusCode = (int)HttpStatusCode.NotFound;
                return;
            }

            if (path.EndsWith(".html"))
            {
                res.ContentType     = "text/html";
                res.ContentEncoding = Encoding.UTF8;
            }
            else if (path.EndsWith(".js"))
            {
                res.ContentType     = "application/javascript";
                res.ContentEncoding = Encoding.UTF8;
            }
            res.WriteContent(contents);
        }
        private bool fileServ()
        {
            string path = request.RawUrl;

            byte[] contents;

            if (path == "/")
            {
                path += "index.html";
            }

            if (!httpEvent.TryReadFile(path, out contents))
            {
                response.StatusCode = (int)HttpStatusCode.NotFound;
                return(false);
            }

            // Temporary
            if (path.EndsWith("html"))
            {
                response.ContentType = "text/html";
            }

            else if (path.EndsWith("js"))
            {
                response.ContentType = "text/javascript";
            }

            else if (path.EndsWith("css"))
            {
                response.ContentType = "text/css";
            }

            else if (path.EndsWith("png"))
            {
                response.ContentType = "image/png";
            }

            else if (path.EndsWith("jpg"))
            {
                response.ContentType = "image/jpeg";
            }

            else
            {
                response.ContentType = "text/plain";
            }

            response.ContentEncoding = Encoding.UTF8;
            response.WriteContent(contents);

            return(true);
        }
Example #3
0
        public static void OnGetSinglePage(object sender, HttpRequestEventArgs e)
        {
            var req  = e.Request;
            var res  = e.Response;
            var path = req.Url.AbsolutePath;

            if (e.TryReadFile(path, out var contents))
            {
                if (path.EndsWith(".html"))
                {
                    res.ContentType = "text/html";
                    res.AddHeader("Feature-Policy", "wake-lock '*'");
                    res.ContentEncoding = Encoding.UTF8;
                }
                else if (path.EndsWith(".js"))
                {
                    res.ContentType     = "application/javascript";
                    res.ContentEncoding = Encoding.UTF8;
                }
                else if (path.EndsWith(".css"))
                {
                    res.ContentType     = "text/css";
                    res.ContentEncoding = Encoding.UTF8;
                }
            }
            else if (e.TryReadFile("/index.html", out contents))
            {
                res.ContentType = "text/html";
                res.AddHeader("Feature-Policy", "wake-lock '*'");
                res.ContentEncoding = Encoding.UTF8;
            }
            else
            {
                res.StatusCode = (int)HttpStatusCode.NotFound;
                return;
            }

            res.WriteContent(contents);
        }
Example #4
0
        private void Http_OnGet(object sender, HttpRequestEventArgs e)
        {
            var req = e.Request;
            var res = e.Response;

            Debug.Print(e.Request.HttpMethod.ToString());

            var path = req.RawUrl.Split('?')[0];

            if (path == "/")
            {
                path += "index.html";
            }

            if (!e.TryReadFile(path, out byte[] contents))
Example #5
0
    /**
     * <summary>
     * Handles HTTP document GET requests by reading files and returning their content.
     * based on https://github.com/sta/websocket-sharp/blob/master/Example3/Program.cs
     * </summary>
     * <remarks>
     * FIXME: Duplicate code fragement with <see cref="BaseServer.OnHTTPGet"/>
     * </remarks>
     */
    private void OnHTTPGet(object sender, HttpRequestEventArgs eventArgs)
    {
        var req = eventArgs.Request;
        var res = eventArgs.Response;

        var path = req.RawUrl;

        // When a client tries to access /, deliver the index page instead
        if (path == "/")
        {
            path += "index.html";
        }

        // Try to read the contents of the requested file
        if (!eventArgs.TryReadFile(path, out var contents))
        {
            // Send 404 error, if there is no such file
            res.StatusCode = (int)HttpStatusCode.NotFound;

            LogWarning($"Client requested non existent file (404): {path}");

            return;
        }

        // Adjust the ContentType of response depending on the requested file
        var fileExtension = Path.GetExtension(path);

        if (fileExtension != null)
        {
            fileExtension = fileExtension.Remove(0, 1);
            if (MimeTypes.GetMimeType(fileExtension) is string mimeType)
            {
                res.ContentType = mimeType;
            }

            if (EncodingAnnotatedTypes.Contains(fileExtension))
            {
                res.ContentEncoding = Encoding.UTF8;
            }
        }

        res.ContentLength64 = contents.LongLength;
        res.Close(contents, true);
    }
        protected void HandleStaticContentRequest(HttpRequestEventArgs e)
        {
            var req = e.Request;
            var res = e.Response;

            var path = req.RawUrl;

            if (path == "/")
            {
                path += "index.html";
            }

            byte[] contents;
            if (!e.TryReadFile(path, out contents))
            {
                res.StatusCode = (int)HttpStatusCode.NotFound;
                return;
            }

            if (path.EndsWith(".html"))
            {
                res.ContentType     = "text/html";
                res.ContentEncoding = Encoding.UTF8;
            }
            else if (path.EndsWith(".js"))
            {
                res.ContentType     = "application/javascript";
                res.ContentEncoding = Encoding.UTF8;
            }
            else if (path.EndsWith(".css"))
            {
                res.ContentType     = "text/css";
                res.ContentEncoding = Encoding.UTF8;
            }
            else if (path.EndsWith(".svg"))
            {
                res.ContentType     = "image/svg+xml";
                res.ContentEncoding = Encoding.UTF8;
            }

            res.WriteContent(contents);
        }
Example #7
0
        private static void Httpsv_OnGet(object sender, HttpRequestEventArgs e)
        {
            var req = e.Request;
            var res = e.Response;

            var path = req.RawUrl;

            if (path == "/")
            {
                path += "index.html";
            }

            byte[] contents;
            if (path.Contains("?"))
            {
                path = path.Substring(0, path.IndexOf("?"));
            }
            if (!e.TryReadFile(path, out contents))
            {
                res.StatusCode = (int)HttpStatusCode.NotFound;
                return;
            }

            if (path.EndsWith(".html"))
            {
                res.ContentType     = "text/html";
                res.ContentEncoding = Encoding.UTF8;
            }
            else if (path.EndsWith(".js"))
            {
                res.ContentType     = "application/javascript";
                res.ContentEncoding = Encoding.UTF8;
            }
            if (path.EndsWith(".css"))
            {
                res.ContentType     = "text/css";
                res.ContentEncoding = Encoding.UTF8;
            }

            res.WriteContent(contents);
        }
Example #8
0
        private void SimpleHttpServer_OnGet(object sender, HttpRequestEventArgs e)
        {
            var req = e.Request;
            var res = e.Response;

            var path = req.RawUrl;

            if (path == "/")
            {
                path += "index.html";
            }

            byte[] contents;
            if (!e.TryReadFile(path, out contents))
            {
                res.StatusCode = (int)HttpStatusCode.NotFound;
                return;
            }


            res.ContentType = MimeTypes.Mappings[Path.GetExtension(path)];

            //if (path.EndsWith(".html"))
            //{
            //    res.ContentType = "text/html";
            //    res.ContentEncoding = Encoding.UTF8;
            //}
            //else if (path.EndsWith(".js"))
            //{
            //    res.ContentType = "application/javascript";
            //    res.ContentEncoding = Encoding.UTF8;
            //}
            //else if (path.EndsWith(".json"))
            //{
            //    res.ContentType = "application/json";
            //    res.ContentEncoding = Encoding.UTF8;
            //}

            res.ContentLength64 = contents.LongLength;
            res.Close(contents, true);
        }
Example #9
0
        /// <summary>
        /// Handle normal https requests to static content in the public folder
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ServerStaticContent(object sender, HttpRequestEventArgs e)
        {
            var req  = e.Request;
            var res  = e.Response;
            var path = req.RawUrl;

            if (path.Contains("!"))
            {
                // Trim away the lobby id for serving files
                path = path.Substring(0, path.IndexOf("!"));
            }

            if (path == "/")
            {
                path += "index.html";
            }

            byte[] contents;
            if (!e.TryReadFile(path, out contents))
            {
                res.StatusCode = (int)HttpStatusCode.NotFound;
                this.oHttpServer.Log.Info("Static file not found " + path);
                return;
            }

            if (path.EndsWith(".html"))
            {
                res.ContentType     = "text/html";
                res.ContentEncoding = Encoding.UTF8;
            }
            else if (path.EndsWith(".js"))
            {
                res.ContentType     = "application/javascript";
                res.ContentEncoding = Encoding.UTF8;
            }

            res.ContentLength64 = contents.LongLength;
            res.Close(contents, true);
        }
Example #10
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="e"></param>
        /// <param name="extension"></param>
        private void HandlePathWithExtension(HttpRequestEventArgs e, string extension)
        {
            // Let's parse user
            string path = UrlToPath(e.Request);

            if (e.TryReadFile(path, out byte[] contents))