forked from ServiceStack/ServiceStack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHttpResponseFilter.cs
More file actions
308 lines (246 loc) · 12.3 KB
/
Copy pathHttpResponseFilter.cs
File metadata and controls
308 lines (246 loc) · 12.3 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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using ServiceStack.ServiceHost;
using ServiceStack.ServiceModel.Serialization;
using ServiceStack.Text;
namespace ServiceStack.Common.Web
{
public class HttpResponseFilter : IContentTypeFilter
{
private static readonly UTF8Encoding UTF8EncodingWithoutBom = new UTF8Encoding(false);
public static HttpResponseFilter Instance = new HttpResponseFilter();
public Dictionary<string, StreamSerializerDelegate> ContentTypeSerializers
= new Dictionary<string, StreamSerializerDelegate>();
public Dictionary<string, ResponseSerializerDelegate> ContentTypeResponseSerializers
= new Dictionary<string, ResponseSerializerDelegate>();
public Dictionary<string, StreamDeserializerDelegate> ContentTypeDeserializers
= new Dictionary<string, StreamDeserializerDelegate>();
public HttpResponseFilter()
{
this.ContentTypeFormats = new Dictionary<string, string>();
}
public void ClearCustomFilters()
{
this.ContentTypeFormats = new Dictionary<string, string>();
this.ContentTypeSerializers = new Dictionary<string, StreamSerializerDelegate>();
this.ContentTypeDeserializers = new Dictionary<string, StreamDeserializerDelegate>();
}
public Dictionary<string, string> ContentTypeFormats { get; set; }
public string GetFormatContentType(string format)
{
//built-in formats
if (format == "json")
return ContentType.Json;
if (format == "xml")
return ContentType.Xml;
if (format == "jsv")
return ContentType.Jsv;
string registeredFormats;
ContentTypeFormats.TryGetValue(format, out registeredFormats);
return registeredFormats;
}
public void Register(string contentType, StreamSerializerDelegate streamSerializer, StreamDeserializerDelegate streamDeserializer)
{
if (contentType.IsNullOrEmpty())
throw new ArgumentNullException("contentType");
var parts = contentType.Split('/');
var format = parts[parts.Length - 1];
this.ContentTypeFormats[format] = contentType;
SetContentTypeSerializer(contentType, streamSerializer);
SetContentTypeDeserializer(contentType, streamDeserializer);
}
public void Register(string contentType, ResponseSerializerDelegate responseSerializer,
StreamDeserializerDelegate streamDeserializer)
{
if (contentType.IsNullOrEmpty())
throw new ArgumentNullException("contentType");
var parts = contentType.Split('/');
var format = parts[parts.Length - 1];
this.ContentTypeFormats[format] = contentType;
this.ContentTypeResponseSerializers[contentType] = responseSerializer;
SetContentTypeDeserializer(contentType, streamDeserializer);
}
public void SetContentTypeSerializer(string contentType, StreamSerializerDelegate streamSerializer)
{
this.ContentTypeSerializers[contentType] = streamSerializer;
}
public void SetContentTypeDeserializer(string contentType, StreamDeserializerDelegate streamDeserializer)
{
this.ContentTypeDeserializers[contentType] = streamDeserializer;
}
public byte[] SerializeToBytes(IRequestContext requestContext, object response)
{
var contentType = requestContext.ResponseContentType;
StreamSerializerDelegate responseStreamWriter;
if (this.ContentTypeSerializers.TryGetValue(contentType, out responseStreamWriter) ||
this.ContentTypeSerializers.TryGetValue(ContentType.GetRealContentType(contentType), out responseStreamWriter))
{
using (var ms = new MemoryStream())
{
responseStreamWriter(requestContext, response, ms);
ms.Position = 0;
return ms.ToArray();
}
}
ResponseSerializerDelegate responseWriter;
if (this.ContentTypeResponseSerializers.TryGetValue(contentType, out responseWriter) ||
this.ContentTypeResponseSerializers.TryGetValue(ContentType.GetRealContentType(contentType), out responseWriter))
{
using (var ms = new MemoryStream())
{
var httpRes = new HttpResponseStreamWrapper(ms);
responseWriter(requestContext, response, httpRes);
ms.Position = 0;
return ms.ToArray();
}
}
var contentTypeAttr = ContentType.GetEndpointAttributes(contentType);
switch (contentTypeAttr)
{
case EndpointAttributes.Xml:
return XmlSerializer.SerializeToString(response).ToUtf8Bytes();
case EndpointAttributes.Json:
return JsonDataContractSerializer.Instance.SerializeToString(response).ToUtf8Bytes();
case EndpointAttributes.Jsv:
return TypeSerializer.SerializeToString(response).ToUtf8Bytes();
}
throw new NotSupportedException("ContentType not supported: " + contentType);
}
public string SerializeToString(IRequestContext requestContext, object response)
{
var contentType = requestContext.ResponseContentType;
StreamSerializerDelegate responseStreamWriter;
if (this.ContentTypeSerializers.TryGetValue(contentType, out responseStreamWriter) ||
this.ContentTypeSerializers.TryGetValue(ContentType.GetRealContentType(contentType), out responseStreamWriter))
{
using (var ms = new MemoryStream())
{
responseStreamWriter(requestContext, response, ms);
ms.Position = 0;
var result = new StreamReader(ms, UTF8EncodingWithoutBom).ReadToEnd();
return result;
}
}
ResponseSerializerDelegate responseWriter;
if (this.ContentTypeResponseSerializers.TryGetValue(contentType, out responseWriter) ||
this.ContentTypeResponseSerializers.TryGetValue(ContentType.GetRealContentType(contentType), out responseWriter))
{
using (var ms = new MemoryStream())
{
var httpRes = new HttpResponseStreamWrapper(ms) {
KeepOpen = true, //Don't let view engines close the OutputStream
};
responseWriter(requestContext, response, httpRes);
var bytes = ms.ToArray();
var result = bytes.FromUtf8Bytes();
httpRes.ForceClose(); //Manually close the OutputStream
return result;
}
}
var contentTypeAttr = ContentType.GetEndpointAttributes(contentType);
switch (contentTypeAttr)
{
case EndpointAttributes.Xml:
return XmlSerializer.SerializeToString(response);
case EndpointAttributes.Json:
return JsonDataContractSerializer.Instance.SerializeToString(response);
case EndpointAttributes.Jsv:
return TypeSerializer.SerializeToString(response);
}
throw new NotSupportedException("ContentType not supported: " + contentType);
}
public void SerializeToStream(IRequestContext requestContext, object response, Stream responseStream)
{
var contentType = requestContext.ResponseContentType;
var serializer = GetResponseSerializer(contentType);
if (serializer == null)
throw new NotSupportedException("ContentType not supported: " + contentType);
var httpRes = new HttpResponseStreamWrapper(responseStream);
serializer(requestContext, response, httpRes);
}
public void SerializeToResponse(IRequestContext requestContext, object response, IHttpResponse httpResponse)
{
var contentType = requestContext.ResponseContentType;
var serializer = GetResponseSerializer(contentType);
if (serializer == null)
throw new NotSupportedException("ContentType not supported: " + contentType);
serializer(requestContext, response, httpResponse);
}
public ResponseSerializerDelegate GetResponseSerializer(string contentType)
{
ResponseSerializerDelegate responseWriter;
if (this.ContentTypeResponseSerializers.TryGetValue(contentType, out responseWriter) ||
this.ContentTypeResponseSerializers.TryGetValue(ContentType.GetRealContentType(contentType), out responseWriter))
{
return responseWriter;
}
var serializer = GetStreamSerializer(contentType);
if (serializer == null) return null;
return (httpReq, dto, httpRes) => serializer(httpReq, dto, httpRes.OutputStream);
}
public StreamSerializerDelegate GetStreamSerializer(string contentType)
{
StreamSerializerDelegate responseWriter;
if (this.ContentTypeSerializers.TryGetValue(contentType, out responseWriter) ||
this.ContentTypeSerializers.TryGetValue(ContentType.GetRealContentType(contentType), out responseWriter))
{
return responseWriter;
}
var contentTypeAttr = ContentType.GetEndpointAttributes(contentType);
switch (contentTypeAttr)
{
case EndpointAttributes.Xml:
return (r, o, s) => XmlSerializer.SerializeToStream(o, s);
case EndpointAttributes.Json:
return (r, o, s) => JsonDataContractSerializer.Instance.SerializeToStream(o, s);
case EndpointAttributes.Jsv:
return (r, o, s) => TypeSerializer.SerializeToStream(o, s);
}
return null;
}
public object DeserializeFromString(string contentType, Type type, string request)
{
var contentTypeAttr = ContentType.GetEndpointAttributes(contentType);
switch (contentTypeAttr)
{
case EndpointAttributes.Xml:
return XmlSerializer.DeserializeFromString(request, type);
case EndpointAttributes.Json:
return JsonDataContractDeserializer.Instance.DeserializeFromString(request, type);
case EndpointAttributes.Jsv:
return TypeSerializer.DeserializeFromString(request, type);
default:
throw new NotSupportedException("ContentType not supported: " + contentType);
}
}
public object DeserializeFromStream(string contentType, Type type, Stream fromStream)
{
var deserializer = GetStreamDeserializer(contentType);
if (deserializer == null)
throw new NotSupportedException("ContentType not supported: " + contentType);
return deserializer(type, fromStream);
}
public StreamDeserializerDelegate GetStreamDeserializer(string contentType)
{
StreamDeserializerDelegate streamReader;
var realContentType = contentType.Split(';')[0].Trim();
if (this.ContentTypeDeserializers.TryGetValue(realContentType, out streamReader))
{
return streamReader;
}
var contentTypeAttr = ContentType.GetEndpointAttributes(contentType);
switch (contentTypeAttr)
{
case EndpointAttributes.Xml:
return XmlSerializer.DeserializeFromStream;
case EndpointAttributes.Json:
return JsonDataContractDeserializer.Instance.DeserializeFromStream;
case EndpointAttributes.Jsv:
return TypeSerializer.DeserializeFromStream;
}
return null;
}
}
}