Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
branch: master
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 198 lines (178 sloc) 9.364 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197
namespace Nancy
{
    using System;
    using System.Collections.Generic;
    using System.IO;
    using Cookies;

    /// <summary>
    /// Encapsulates HTTP-response information from an Nancy operation.
    /// </summary>
    public class Response: IDisposable
    {
        /// <summary>
        /// Null object representing no body
        /// </summary>
        public static Action<Stream> NoBody = s => { };

        /// <summary>
        /// Initializes a new instance of the <see cref="Response"/> class.
        /// </summary>
        public Response()
        {
            this.Contents = NoBody;
            this.ContentType = "text/html";
            this.Headers = new Dictionary<string, string>();
            this.StatusCode = HttpStatusCode.OK;
            this.Cookies = new List<INancyCookie>(2);
        }

        /// <summary>
        /// Gets or sets the type of the content.
        /// </summary>
        /// <value>The type of the content.</value>
        /// <remarks>The default value is <c>text/html</c>.</remarks>
        public string ContentType { get; set; }

        /// <summary>
        /// Gets the delegate that will render contents to the response stream.
        /// </summary>
        /// <value>An <see cref="Action{T}"/> delegate, containing the code that will render contents to the response stream.</value>
        /// <remarks>The host of Nancy will pass in the output stream after the response has been handed back to it by Nancy.</remarks>
        public Action<Stream> Contents { get; set; }

        /// <summary>
        /// Gets the collection of HTTP response headers that should be sent back to the client.
        /// </summary>
        /// <value>An <see cref="IDictionary{TKey,TValue}"/> instance, contaning the key/value pair of headers.</value>
        public IDictionary<string, string> Headers { get; set; }

        /// <summary>
        /// Gets or sets the HTTP status code that should be sent back to the client.
        /// </summary>
        /// <value>A <see cref="HttpStatusCode"/> value.</value>
        public HttpStatusCode StatusCode { get; set; }

        /// <summary>
        /// Gets or sets a text description of the HTTP status code returned to the client.
        /// </summary>
        /// <value>The HTTP status code description.</value>
        public string ReasonPhrase { get; set; }

        /// <summary>
        /// Gets the <see cref="INancyCookie"/> instances that are associated with the response.
        /// </summary>
        /// <value>A <see cref="IList{T}"/> instance, containing <see cref="INancyCookie"/> instances.</value>
        public IList<INancyCookie> Cookies { get; private set; }
        
        /// <summary>
        /// Adds a <see cref="INancyCookie"/> to the response.
        /// </summary>
        /// <param name="name">The name of the cookie.</param>
        /// <param name="value">The value of the cookie.</param>
        /// <returns>The <see cref="Response"/> instance.</returns>
[Obsolete("This method has been replaced with Response.WithCookie and will be removed in a subsequent release.")]
        public Response AddCookie(string name, string value)
        {
            return AddCookie(name, value, null, null, null);
        }

        /// <summary>
        /// Adds a <see cref="INancyCookie"/> to the response.
        /// </summary>
        /// <param name="name">The name of the cookie.</param>
        /// <param name="value">The value of the cookie.</param>
        /// <param name="expires">The expiration date of the cookie. Can be <see langword="null" /> if it should never expire.</param>
        /// <returns>The <see cref="Response"/> instance.</returns>
[Obsolete("This method has been replaced with Response.WithCookie and will be removed in a subsequent release.")]
        public Response AddCookie(string name, string value, DateTime? expires)
        {
            return AddCookie(name, value, expires, null, null);
        }
    
        /// <summary>
        /// Adds a <see cref="INancyCookie"/> to the response.
        /// </summary>
        /// <param name="name">The name of the cookie.</param>
        /// <param name="value">The value of the cookie.</param>
        /// <param name="expires">The expiration date of the cookie. Can be <see langword="null" /> if it should never expire.</param>
        /// <param name="domain">The domain of the cookie.</param>
        /// <param name="path">The path of the cookie.</param>
        /// <returns>The <see cref="Response"/> instance.</returns>
[Obsolete("This method has been replaced with Response.WithCookie and will be removed in a subsequent release.")]
        public Response AddCookie(string name, string value, DateTime? expires, string domain, string path)
        {
            return AddCookie(new NancyCookie(name, value){ Expires = expires, Domain = domain, Path = path });
        }

        /// <summary>
        /// Adds a <see cref="INancyCookie"/> to the response.
        /// </summary>
        /// <param name="nancyCookie">A <see cref="INancyCookie"/> instance.</param>
        /// <returns></returns>
[Obsolete("This method has been replaced with Response.WithCookie and will be removed in a subsequent release.")]
        public Response AddCookie(INancyCookie nancyCookie)
        {
            Cookies.Add(nancyCookie);
            return this;
        }

        /// <summary>
        /// Implicitly cast an <see cref="HttpStatusCode"/> value to a <see cref="Response"/> instance, with the <see cref="StatusCode"/>
        /// set to the value of the <see cref="HttpStatusCode"/>.
        /// </summary>
        /// <param name="statusCode">The <see cref="HttpStatusCode"/> value that is being cast from.</param>
        /// <returns>A <see cref="Response"/> instance.</returns>
        public static implicit operator Response(HttpStatusCode statusCode)
        {
            return new Response { StatusCode = statusCode };
        }

        /// <summary>
        /// Implicitly cast an int value to a <see cref="Response"/> instance, with the <see cref="StatusCode"/>
        /// set to the value of the int.
        /// </summary>
        /// <param name="statusCode">The int value that is being cast from.</param>
        /// <returns>A <see cref="Response"/> instance.</returns>
        public static implicit operator Response(int statusCode)
        {
            return new Response { StatusCode = (HttpStatusCode)statusCode };
        }

        /// <summary>
        /// Implicitly cast an string instance to a <see cref="Response"/> instance, with the <see cref="Contents"/>
        /// set to the value of the string.
        /// </summary>
        /// <param name="contents">The string that is being cast from.</param>
        /// <returns>A <see cref="Response"/> instance.</returns>
        public static implicit operator Response(string contents)
        {
            return new Response { Contents = GetStringContents(contents) };
        }

        /// <summary>
        /// Implicitly cast an <see cref="Action{T}"/>, where T is a <see cref="Stream"/>, instance to
        /// a <see cref="Response"/> instance, with the <see cref="Contents"/> set to the value of the action.
        /// </summary>
        /// <param name="streamFactory">The <see cref="Action{T}"/> instance that is being cast from.</param>
        /// <returns>A <see cref="Response"/> instance.</returns>
        public static implicit operator Response(Action<Stream> streamFactory)
        {
            return new Response { Contents = streamFactory };
        }

        /// <summary>
        /// Implicitly cast a <see cref="DynamicDictionaryValue"/> instance to a <see cref="Response"/> instance,
        /// with the <see cref="Contents"/> set to the value of the <see cref="DynamicDictionaryValue"/>.
        /// </summary>
        /// <param name="value">The <see cref="DynamicDictionaryValue"/> instance that is being cast from.</param>
        /// <returns>A <see cref="Response"/> instance.</returns>
        public static implicit operator Response(DynamicDictionaryValue value)
        {
            return new Response { Contents = GetStringContents(value) };
        }

        /// <summary>
        /// Converts a string content value to a response action.
        /// </summary>
        /// <param name="contents">The string containing the content.</param>
        /// <returns>A response action that will write the content of the string to the response stream.</returns>
        protected static Action<Stream> GetStringContents(string contents)
        {
            return stream =>
            {
                var writer = new StreamWriter(stream) { AutoFlush = true };
                writer.Write(contents);
            };
        }

        /// <summary>
        /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
        /// </summary>
        /// <remarks>This method can be overriden in sub-classes to dispose of response specific resources.</remarks>
        public virtual void Dispose()
        {
        }
    }
}
Something went wrong with that request. Please try again.