forked from ServiceStack/ServiceStack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRequestAttributes.cs
More file actions
224 lines (200 loc) · 6.32 KB
/
RequestAttributes.cs
File metadata and controls
224 lines (200 loc) · 6.32 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
//Copyright (c) ServiceStack, Inc. All Rights Reserved.
//License: https://raw.github.com/ServiceStack/ServiceStack/master/license.txt
using System;
namespace ServiceStack
{
[Flags]
public enum RequestAttributes : long
{
None = 0,
Any = AnyNetworkAccessType | AnySecurityMode | AnyHttpMethod | AnyCallStyle | AnyFormat,
AnyNetworkAccessType = External | LocalSubnet | Localhost | InProcess,
AnySecurityMode = Secure | InSecure,
AnyHttpMethod = HttpHead | HttpGet | HttpPost | HttpPut | HttpDelete | HttpPatch | HttpOptions | HttpOther,
AnyCallStyle = OneWay | Reply,
AnyFormat = Soap11 | Soap12 | Xml | Json | Jsv | Html | ProtoBuf | Csv | MsgPack | Wire | FormatOther,
AnyEndpoint = Http | MessageQueue | Tcp | EndpointOther,
InternalNetworkAccess = InProcess | Localhost | LocalSubnet,
//Whether it came from an Internal or External address
Localhost = 1 << 0,
LocalSubnet = 1 << 1,
External = 1 << 2,
//Called over a secure or insecure channel
Secure = 1 << 3,
InSecure = 1 << 4,
//HTTP request type
HttpHead = 1 << 5,
HttpGet = 1 << 6,
HttpPost = 1 << 7,
HttpPut = 1 << 8,
HttpDelete = 1 << 9,
HttpPatch = 1 << 10,
HttpOptions = 1 << 11,
HttpOther = 1 << 12,
//Call Styles
OneWay = 1 << 13,
Reply = 1 << 14,
//Different formats
Soap11 = 1 << 15,
Soap12 = 1 << 16,
//POX
Xml = 1 << 17,
//Javascript
Json = 1 << 18,
//Jsv i.e. TypeSerializer
Jsv = 1 << 19,
//e.g. protobuf-net
ProtoBuf = 1 << 20,
//e.g. text/csv
Csv = 1 << 21,
Html = 1 << 22,
Wire = 1 << 23,
MsgPack = 1 << 24,
FormatOther = 1 << 25,
//Different endpoints
Http = 1 << 26,
MessageQueue = 1 << 27,
Tcp = 1 << 28,
EndpointOther = 1 << 29,
InProcess = 1 << 30, //Service was executed within code (e.g. ResolveService<T>)
}
public enum Network : long
{
Localhost = 1 << 0,
LocalSubnet = 1 << 1,
External = 1 << 2,
}
public enum Security : long
{
Secure = 1 << 3,
InSecure = 1 << 4,
}
public enum Http : long
{
Head = 1 << 5,
Get = 1 << 6,
Post = 1 << 7,
Put = 1 << 8,
Delete = 1 << 9,
Patch = 1 << 10,
Options = 1 << 11,
Other = 1 << 12,
}
public enum CallStyle : long
{
OneWay = 1 << 13,
Reply = 1 << 14,
}
public enum Format : long
{
Soap11 = 1 << 15,
Soap12 = 1 << 16,
Xml = 1 << 17,
Json = 1 << 18,
Jsv = 1 << 19,
ProtoBuf = 1 << 20,
Csv = 1 << 21,
Html = 1 << 22,
Wire = 1 << 23,
MsgPack = 1 << 24,
Other = 1 << 25,
}
public enum Endpoint : long
{
Http = 1 << 26,
Mq = 1 << 27,
Tcp = 1 << 28,
Other = 1 << 29,
}
public static class RequestAttributesExtensions
{
public static bool IsLocalhost(this RequestAttributes attrs)
{
return (RequestAttributes.Localhost & attrs) == RequestAttributes.Localhost;
}
public static bool IsLocalSubnet(this RequestAttributes attrs)
{
return (RequestAttributes.LocalSubnet & attrs) == RequestAttributes.LocalSubnet;
}
public static bool IsExternal(this RequestAttributes attrs)
{
return (RequestAttributes.External & attrs) == RequestAttributes.External;
}
public static Format ToFormat(this string format)
{
try
{
return (Format)Enum.Parse(typeof(Format), format.ToUpper().Replace("X-", ""), true);
}
catch (Exception)
{
return Format.Other;
}
}
public static string FromFormat(this Format format)
{
var formatStr = format.ToString().ToLowerInvariant();
if (format == Format.ProtoBuf || format == Format.MsgPack)
return "x-" + formatStr;
return formatStr;
}
public static Format ToFormat(this Feature feature)
{
switch (feature)
{
case Feature.Xml:
return Format.Xml;
case Feature.Json:
return Format.Json;
case Feature.Jsv:
return Format.Jsv;
case Feature.Csv:
return Format.Csv;
case Feature.Html:
return Format.Html;
case Feature.MsgPack:
return Format.MsgPack;
case Feature.ProtoBuf:
return Format.ProtoBuf;
case Feature.Soap11:
return Format.Soap11;
case Feature.Soap12:
return Format.Soap12;
}
return Format.Other;
}
public static Feature ToFeature(this Format format)
{
switch (format)
{
case Format.Xml:
return Feature.Xml;
case Format.Json:
return Feature.Json;
case Format.Jsv:
return Feature.Jsv;
case Format.Csv:
return Feature.Csv;
case Format.Html:
return Feature.Html;
case Format.MsgPack:
return Feature.MsgPack;
case Format.ProtoBuf:
return Feature.ProtoBuf;
case Format.Soap11:
return Feature.Soap11;
case Format.Soap12:
return Feature.Soap12;
}
return Feature.CustomFormat;
}
public static Feature ToSoapFeature(this RequestAttributes attributes)
{
if ((RequestAttributes.Soap11 & attributes) == RequestAttributes.Soap11)
return Feature.Soap11;
if ((RequestAttributes.Soap12 & attributes) == RequestAttributes.Soap12)
return Feature.Soap12;
return Feature.None;
}
}
}