Esempio n. 1
0
 private async Task WriteResponseAsync(HttpServerResponse response, StreamSocket socket)
 {
     using (IOutputStream output = socket.OutputStream)
     {
         await output.WriteAsync(response.ToBytes().AsBuffer());
         await output.FlushAsync();
     }
 }
        private static HttpServerResponse GetFileNotFoundResponse(string localPath)
        {
            var notFoundResponse = new HttpServerResponse(new Version(1, 1), HttpResponseStatus.NotFound)
            {
                Content = Encoding.UTF8.GetBytes($"File at {localPath} not found."),
                ContentType = "text/plain",
                ContentCharset = "utf-8"
            };

            return notFoundResponse;
        }
        private static HttpServerResponse GetMethodNotAllowedResponse(HttpMethod? method)
        {
            // https://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html
            // The method specified in the Request-Line is not allowed for the resource identified by the Request-URI. The response MUST include an Allow header containing a list of valid methods for the requested resource.
            var methodNotAllowedResponse = new HttpServerResponse(new Version(1, 1), HttpResponseStatus.MethodNotAllowed)
            {
                Content = Encoding.UTF8.GetBytes($"Unsupported method {method}."),
                Allow = new[] { HttpMethod.GET },
                ContentType = "text/plain",
                ContentCharset = "utf-8"
            };

            return methodNotAllowedResponse;
        }