forked from ServiceStack/ServiceStack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRequestExtensions.cs
More file actions
152 lines (128 loc) · 5.1 KB
/
Copy pathRequestExtensions.cs
File metadata and controls
152 lines (128 loc) · 5.1 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
using System;
using ServiceStack.Caching;
using ServiceStack.Host;
using ServiceStack.Web;
namespace ServiceStack
{
public static class RequestExtensions
{
public static AuthUserSession ReloadSession(this IRequest request)
{
return request.GetSession() as AuthUserSession;
}
public static string GetCompressionType(this IRequest request)
{
if (request.RequestPreferences.AcceptsDeflate)
return CompressionTypes.Deflate;
if (request.RequestPreferences.AcceptsGzip)
return CompressionTypes.GZip;
return null;
}
public static string GetHeader(this IRequest request, string headerName)
{
return request.Headers.Get(headerName);
}
public static string GetParamInRequestHeader(this IRequest request, string name)
{
//Avoid reading request body for non x-www-form-urlencoded requests
return request.Headers[name]
?? request.QueryString[name]
?? (!HostContext.Config.SkipFormDataInCreatingRequest && request.ContentType.MatchesContentType(MimeTypes.FormUrlEncoded)
? request.FormData[name]
: null);
}
/// <summary>
/// Returns the optimized result for the IRequestContext.
/// Does not use or store results in any cache.
/// </summary>
/// <param name="request"></param>
/// <param name="dto"></param>
/// <returns></returns>
public static object ToOptimizedResult<T>(this IRequest request, T dto)
{
request.Response.Dto = dto;
string serializedDto = HostContext.ContentTypes.SerializeToString(request, dto);
var compressionType = request.GetCompressionType();
if (compressionType == null)
return (object)serializedDto;
byte[] compressedBytes = serializedDto.Compress(compressionType);
return new CompressedResult(compressedBytes, compressionType, request.ResponseContentType) {
Status = request.Response.StatusCode
};
}
/// <summary>
/// Overload for the <see cref="ContentCacheManager.Resolve"/> method returning the most
/// optimized result based on the MimeType and CompressionType from the IRequestContext.
/// </summary>
public static object ToOptimizedResultUsingCache<T>(
this IRequest requestContext, ICacheClient cacheClient, string cacheKey,
Func<T> factoryFn)
{
return requestContext.ToOptimizedResultUsingCache(cacheClient, cacheKey, null, factoryFn);
}
/// <summary>
/// Overload for the <see cref="ContentCacheManager.Resolve"/> method returning the most
/// optimized result based on the MimeType and CompressionType from the IRequestContext.
/// <param name="expireCacheIn">How long to cache for, null is no expiration</param>
/// </summary>
public static object ToOptimizedResultUsingCache<T>(
this IRequest requestContext, ICacheClient cacheClient, string cacheKey,
TimeSpan? expireCacheIn, Func<T> factoryFn)
{
var cacheResult = cacheClient.ResolveFromCache(cacheKey, requestContext);
if (cacheResult != null)
return cacheResult;
cacheResult = cacheClient.Cache(cacheKey, factoryFn(), requestContext, expireCacheIn);
return cacheResult;
}
/// <summary>
/// Clears all the serialized and compressed caches set
/// by the 'Resolve' method for the cacheKey provided
/// </summary>
/// <param name="requestContext"></param>
/// <param name="cacheClient"></param>
/// <param name="cacheKeys"></param>
public static void RemoveFromCache(
this IRequest requestContext, ICacheClient cacheClient, params string[] cacheKeys)
{
cacheClient.ClearCaches(cacheKeys);
}
/// <summary>
/// Store an entry in the IHttpRequest.Items Dictionary
/// </summary>
public static void SetItem(this IRequest httpReq, string key, object value)
{
if (httpReq == null) return;
httpReq.Items[key] = value;
}
/// <summary>
/// Get an entry from the IHttpRequest.Items Dictionary
/// </summary>
public static object GetItem(this IRequest httpReq, string key)
{
if (httpReq == null) return null;
object value;
httpReq.Items.TryGetValue(key, out value);
return value;
}
public static RequestBaseWrapper ToHttpRequestBase(this IRequest httpReq)
{
return new RequestBaseWrapper((IHttpRequest) httpReq);
}
public static void SetInProcessRequest(this IRequest httpReq)
{
if (httpReq == null) return;
httpReq.RequestAttributes |= RequestAttributes.InProcess;
}
public static bool IsInProcessRequest(this IRequest httpReq)
{
if (httpReq == null) return false;
return (RequestAttributes.InProcess & httpReq.RequestAttributes) == RequestAttributes.InProcess;
}
public static void ReleaseIfInProcessRequest(this IRequest httpReq)
{
if (httpReq == null) return;
httpReq.RequestAttributes = httpReq.RequestAttributes & ~RequestAttributes.InProcess;
}
}
}