This repository has been archived by the owner on Jan 24, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
ResponseExtensions.cs
208 lines (186 loc) · 8.55 KB
/
ResponseExtensions.cs
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
198
199
200
201
202
203
204
205
206
207
208
namespace Nancy
{
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using Nancy.Cookies;
using Nancy.Responses;
/// <summary>
/// Containing extensions for the <see cref="Response"/> object.
/// </summary>
public static class ResponseExtensions
{
/// <summary>
/// Force the response to be downloaded as an attachment
/// </summary>
/// <param name="response">Response object</param>
/// <param name="fileName">Filename for the download</param>
/// <param name="contentType">Optional content type</param>
/// <returns>Modified Response object</returns>
public static Response AsAttachment(this Response response, string fileName = null, string contentType = null)
{
var actualFilename = fileName;
if (actualFilename == null && response is GenericFileResponse)
{
actualFilename = ((GenericFileResponse)response).Filename;
}
if (string.IsNullOrEmpty(actualFilename))
{
throw new ArgumentException("fileName cannot be null or empty");
}
if (contentType != null)
{
response.ContentType = contentType;
}
return response.WithHeader("Content-Disposition", "attachment; filename=" + actualFilename);
}
/// <summary>
/// Adds a <see cref="INancyCookie"/> to the response.
/// </summary>
/// <param name="response">Response object</param>
/// <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>
public static Response WithCookie(this Response response, string name, string value)
{
return WithCookie(response, name, value, null, null, null);
}
/// <summary>
/// Adds a <see cref="INancyCookie"/> to the response.
/// </summary>
/// <param name="response">Response object</param>
/// <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 expire at the end of the session.</param>
/// <returns>The <see cref="Response"/> instance.</returns>
public static Response WithCookie(this Response response, string name, string value, DateTime? expires)
{
return WithCookie(response, name, value, expires, null, null);
}
/// <summary>
/// Adds a <see cref="INancyCookie"/> to the response.
/// </summary>
/// <param name="response">Response object</param>
/// <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 expire at the end of the session.</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>
public static Response WithCookie(this Response response, string name, string value, DateTime? expires, string domain, string path)
{
return WithCookie(response, new NancyCookie(name, value) { Expires = expires, Domain = domain, Path = path });
}
/// <summary>
/// Adds a <see cref="INancyCookie"/> to the response.
/// </summary>
/// <param name="response">Response object</param>
/// <param name="nancyCookie">A <see cref="INancyCookie"/> instance.</param>
/// <returns></returns>
public static Response WithCookie(this Response response, INancyCookie nancyCookie)
{
response.Cookies.Add(nancyCookie);
return response;
}
/// <summary>
/// Add a header to the response
/// </summary>
/// <param name="response">Response object</param>
/// <param name="header">Header name</param>
/// <param name="value">Header value</param>
/// <returns>Modified response</returns>
public static Response WithHeader(this Response response, string header, string value)
{
return response.WithHeaders(new { Header = header, Value = value });
}
/// <summary>
/// Adds headers to the response using anonymous types
/// </summary>
/// <param name="response">Response object</param>
/// <param name="headers">
/// Array of headers - each header should be an anonymous type with two string properties
/// 'Header' and 'Value' to represent the header name and its value.
/// </param>
/// <returns>Modified response</returns>
public static Response WithHeaders(this Response response, params object[] headers)
{
return response.WithHeaders(headers.Select(GetTuple).ToArray());
}
/// <summary>
/// Adds headers to the response using anonymous types
/// </summary>
/// <param name="response">Response object</param>
/// <param name="headers">
/// Array of headers - each header should be a Tuple with two string elements
/// for header name and header value
/// </param>
/// <returns>Modified response</returns>
public static Response WithHeaders(this Response response, params Tuple<string, string>[] headers)
{
if (response.Headers == null)
{
response.Headers = new Dictionary<string, string>();
}
foreach (var keyValuePair in headers)
{
response.Headers[keyValuePair.Item1] = keyValuePair.Item2;
}
return response;
}
/// <summary>
/// Sets the content type of the response
/// </summary>
/// <param name="response">Response object</param>
/// <param name="contentType">The type of the content</param>
/// <returns>Modified response</returns>
public static Response WithContentType(this Response response, string contentType)
{
response.ContentType = contentType;
return response;
}
/// <summary>
/// Sets the status code of the response
/// </summary>
/// <param name="response">Response object</param>
/// <param name="statusCode">The http status code</param>
/// <returns>Modified response</returns>
public static Response WithStatusCode(this Response response, HttpStatusCode statusCode)
{
response.StatusCode = statusCode;
return response;
}
/// <summary>
/// Sets the status code of the response
/// </summary>
/// <param name="response">Response object</param>
/// <param name="statusCode">The http status code</param>
/// <returns>Modified response</returns>
public static Response WithStatusCode(this Response response, int statusCode)
{
response.StatusCode = (HttpStatusCode) statusCode;
return response;
}
private static Tuple<string, string> GetTuple(object header)
{
var properties = header.GetType()
.GetTypeInfo()
.DeclaredProperties
.Where(prop => prop.CanRead && prop.PropertyType == typeof(string))
.ToArray();
var headerProperty = properties
.Where(p => string.Equals(p.Name, "Header", StringComparison.OrdinalIgnoreCase))
.FirstOrDefault();
var valueProperty = properties
.Where(p => string.Equals(p.Name, "Value", StringComparison.OrdinalIgnoreCase))
.FirstOrDefault();
if (headerProperty == null || valueProperty == null)
{
throw new ArgumentException("Unable to extract 'Header' or 'Value' properties from anonymous type.");
}
return Tuple.Create(
(string)headerProperty.GetValue(header, null),
(string)valueProperty.GetValue(header, null));
}
}
}