Exemple #1
0
        /// <summary>
        /// Serves the given file to the client and closes the request when done.
        /// </summary>
        /// <param name="res">The res.</param>
        /// <param name="path">The path.</param>
        /// <exception cref="System.ArgumentException">File does not exist</exception>
        public static void ServeFile(this IServerResponse res, string path)
        {
            if (!File.Exists(path))
            {
                throw new ArgumentException("File does not exist");
            }

            if (!res.IsCached)
            {
                res.ContentLength64 = new FileInfo(path).Length;
            }

            var fileInfo = new FileInfo(path);

            res.ContentLength64 = fileInfo.Length;

            var stream = File.Open(path, FileMode.Open, FileAccess.Read);
            var copier = new AsyncStreamCopier(stream, res.OutputStream);

            copier.Completed += (s, e) =>
            {
                e.InputStream.Close();
                res.Close();
            };

            copier.Copy();
        }
Exemple #2
0
        /// <summary>
        /// Response with the status code:
        /// 405 Method Not Allowed
        /// A request was made of a resource using a request method not supported by that resource;
        /// for example, using GET on a form which requires data to be presented via POST,
        /// or using PUT on a read-only resource.
        /// </summary>
        /// <param name="res">The response to the client</param>
        /// <param name="allowedVerbs">The allowed verbs.</param>
        public static void Response405(this IServerResponse res, params HttpVerb[] allowedVerbs)
        {
            res.StatusCode = 405;
            var data = allowedVerbs.Aggregate("", (str, item) => str += item.ToString() + ",");

            res.Headers.Add("Allow", data.TrimEnd(','));
            res.Close();
        }
        /// <summary>
        /// Commits this instance.
        /// </summary>
        void Commit()
        {
            res.ContentLength64   = bufferStream.Length;
            bufferStream.Position = 0;
            var copier = new AsyncStreamCopier(bufferStream, res.OutputStream);

            copier.Completed += (s, e) =>
            {
                bufferStream.Flush();
                bufferStream.Close();
                res.Close();
            };

            copier.Copy();
        }
Exemple #4
0
 /// <summary>
 /// Response with the status code:
 /// 418 I'm a teapot (RFC 2324)
 /// This code was defined in 1998 as one of the traditional IETF April Fools' jokes,
 /// in RFC 2324, Hyper Text Coffee Pot Control Protocol, and is not expected to be implemented by
 /// actual HTTP servers.
 /// </summary>
 /// <param name="res">The response to the client</param>
 public static void Response418(this IServerResponse res)
 {
     res.StatusCode = 418;
     res.Close();
 }
Exemple #5
0
 /// <summary>
 /// Response with the status code:
 /// 307 Temporary Redirect
 /// In this case, the request should be repeated with another URI;
 /// however, future requests should still use the original URI.
 /// In contrast to how 302 was historically implemented,
 /// the request method is not allowed to be changed when reissuing the original request.
 /// For instance, a POST request repeated using another POST request.
 /// </summary>
 /// <param name="res">The response to the client</param>
 /// <param name="newLocation">The new location.</param>
 public static void Response307(this IServerResponse res, Uri newLocation)
 {
     res.StatusCode = 307;
     res.Headers.Add("Location", newLocation.ToString());
     res.Close();
 }
Exemple #6
0
 /// <summary>
 /// Response with the status code:
 /// 304 Not Modified
 /// Indicates the resource has not been modified since last requested.
 /// Typically, the HTTP client provides a header like the If-Modified-Since header
 /// to provide a time against which to compare.
 /// Using this saves bandwidth and reprocessing on both the server and client,
 /// as only the header data must be sent and received in comparison to the entirety
 /// of the page being re-processed by the server,
 /// then sent again using more bandwidth of the server and client.
 /// </summary>
 /// <param name="res">The response to the client</param>
 public static void Response304(this IServerResponse res)
 {
     res.StatusCode = 304;
     res.Close();
 }
Exemple #7
0
 /// <summary>
 /// Response with the status code:
 /// 303 See Other
 /// The response to the request can be found under another URI using a GET method.
 /// When received in response to a POST (or PUT/DELETE),
 /// it should be assumed that the server has received the data
 /// and the redirect should be issued with a separate GET message.
 /// </summary>
 /// <param name="res">The response to the client</param>
 /// <param name="other">The other location.</param>
 public static void Response303(this IServerResponse res, Uri other)
 {
     res.StatusCode = 303;
     res.Headers.Add("Location", other.ToString());
     res.Close();
 }