예제 #1
0
        private HttpServerResponse GenerateResponse(HttpListenerContext context)
        {
            string url = context.Request.Url.AbsolutePath;

            if (url == autoRefresher.ServerEndpoint)
            {
                return(autoRefresher.SendPollResponse());
                // don't log refresh polling to the console on purpose, because it's too noisy.
            }

            Console.WriteLine($"{DateTime.Now:HH:mm:ss} - HTTP {context.Request.HttpMethod} {url}");
            string filename = WebUtility.UrlDecode(url.TrimStart('/'));

            string exactFile = Path.Combine(path, filename);

            if (File.Exists(exactFile))
            {
                return(HttpServerResponse.FileResponse(exactFile));
            }

            string defaultIndexFile = Path.Combine(path, filename, IndexFile);

            if (File.Exists(defaultIndexFile))
            {
                return(HttpServerResponse.FileResponse(defaultIndexFile));
            }

            if (Directory.Exists(exactFile))
            {
                var listing = GenerateDirectoryListing(exactFile);
                return(HttpServerResponse.HtmlResponse(listing));
            }

            return(HttpServerResponse.HtmlResponse("Not Found", HttpStatusCode.NotFound));
        }
예제 #2
0
        /// <summary>
        /// Process the received context from the <see cref="HttpListener"/>.
        /// The context represents a single request/response.
        /// </summary>
        private async Task ProcessContext(HttpListenerContext context, CancellationToken token)
        {
            HttpServerResponse response;

            try
            {
                response = GenerateResponse(context);
            }
            catch (Exception e)
            {
                ReportError(e);
                response = HttpServerResponse.HtmlResponse("ERROR: " + e.Message, HttpStatusCode.InternalServerError);
            }

            if (!token.IsCancellationRequested)
            {
                await StreamResponseAsync(context, response, token).ConfigureAwait(false);
            }
        }