/
AddHeaderAttribute.cs
169 lines (149 loc) · 4.46 KB
/
AddHeaderAttribute.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
using System;
using System.Net;
using ServiceStack.Web;
namespace ServiceStack
{
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, Inherited = true, AllowMultiple = true)]
public class AddHeaderAttribute : RequestFilterAttribute
{
public string Name { get; set; }
public string Value { get; set; }
public HttpStatusCode Status
{
get { return (HttpStatusCode)StatusCode.GetValueOrDefault(200); }
set { StatusCode = (int)value; }
}
public int? StatusCode { get; set; }
public string StatusDescription { get; set; }
public AddHeaderAttribute() { }
public AddHeaderAttribute(string name, string value)
{
Name = name;
Value = value;
}
public AddHeaderAttribute(HttpStatusCode status, string statusDescription = null)
{
Status = status;
StatusDescription = statusDescription;
}
public override void Execute(IRequest req, IResponse res, object requestDto)
{
if (StatusCode != null)
{
res.StatusCode = StatusCode.Value;
}
if (StatusDescription != null)
{
res.StatusDescription = StatusDescription;
}
if (!string.IsNullOrEmpty(Name) && !string.IsNullOrEmpty(Value))
{
if (Name.EqualsIgnoreCase(HttpHeaders.ContentType))
{
req.ResponseContentType = Value; //Looked at in WriteRespone
}
else if (Name == "DefaultContentType")
{
if (!req.HasExplicitResponseContentType)
{
req.ResponseContentType = Value; //Looked at in WriteRespone
}
}
else
{
res.AddHeader(Name, Value);
}
}
}
public string ContentType
{
get { return Name == HttpHeaders.ContentType ? Value : null; }
set
{
Name = HttpHeaders.ContentType;
Value = value;
}
}
public string DefaultContentType
{
get { return Name == "DefaultContentType" ? Value : null; }
set
{
Name = "DefaultContentType";
Value = value;
}
}
public string ContentEncoding
{
get { return Name == HttpHeaders.ContentEncoding ? Value : null; }
set
{
Name = HttpHeaders.ContentEncoding;
Value = value;
}
}
public string ContentLength
{
get { return Name == HttpHeaders.ContentLength ? Value : null; }
set
{
Name = HttpHeaders.ContentLength;
Value = value;
}
}
public string ContentDisposition
{
get { return Name == HttpHeaders.ContentDisposition ? Value : null; }
set
{
Name = HttpHeaders.ContentDisposition;
Value = value;
}
}
public string Location
{
get { return Name == HttpHeaders.Location ? Value : null; }
set
{
Name = HttpHeaders.Location;
Value = value;
}
}
public string SetCookie
{
get { return Name == HttpHeaders.SetCookie ? Value : null; }
set
{
Name = HttpHeaders.SetCookie;
Value = value;
}
}
public string ETag
{
get { return Name == HttpHeaders.ETag ? Value : null; }
set
{
Name = HttpHeaders.ETag;
Value = value;
}
}
public string CacheControl
{
get { return Name == HttpHeaders.CacheControl ? Value : null; }
set
{
Name = HttpHeaders.CacheControl;
Value = value;
}
}
public string LastModified
{
get { return Name == HttpHeaders.LastModified ? Value : null; }
set
{
Name = HttpHeaders.LastModified;
Value = value;
}
}
}
}