-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Expand file tree
/
Copy pathBasicRequest.cs
More file actions
201 lines (153 loc) · 6.44 KB
/
Copy pathBasicRequest.cs
File metadata and controls
201 lines (153 loc) · 6.44 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
using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.IO;
using System.Net;
using System.Threading.Tasks;
using ServiceStack.Configuration;
using ServiceStack.IO;
using ServiceStack.Messaging;
using ServiceStack.Text;
using ServiceStack.Web;
namespace ServiceStack.Host
{
public class BasicRequest : IRequest, IHasResolver, IHasVirtualFiles
#if NETSTANDARD2_0
, IHasServiceScope
#endif
{
public object Dto { get; set; }
public IMessage Message { get; set; }
public object OriginalRequest { get; private set; }
public IResponse Response { get; set; }
#if NETSTANDARD2_0
public Microsoft.Extensions.DependencyInjection.IServiceScope ServiceScope { get; set; }
#endif
private IResolver resolver;
public IResolver Resolver
{
get => resolver ?? Service.GlobalResolver;
set => resolver = value;
}
public BasicRequest(object requestDto,
RequestAttributes requestAttributes = RequestAttributes.LocalSubnet | RequestAttributes.MessageQueue)
: this(MessageFactory.Create(requestDto), requestAttributes) {}
public BasicRequest(IMessage message = null,
RequestAttributes requestAttributes = RequestAttributes.LocalSubnet | RequestAttributes.MessageQueue)
{
Message = message ?? new Message();
Dto = Message.Body;
ContentType = this.ResponseContentType = MimeTypes.Json;
this.Headers = new NameValueCollection();
if (Dto != null)
{
PathInfo = "/json/oneway/" + OperationName;
RawUrl = AbsoluteUri = "mq://" + PathInfo;
Headers = Message.ToHeaders().ToNameValueCollection();
}
this.IsLocal = true;
Response = new BasicResponse(this);
this.RequestAttributes = requestAttributes;
this.Verb = HttpMethods.Post;
this.Cookies = new Dictionary<string, Cookie>();
this.Items = new Dictionary<string, object>();
this.QueryString = new NameValueCollection();
this.FormData = new NameValueCollection();
this.Files = TypeConstants<IHttpFile>.EmptyArray;
this.RemoteIp = IPAddress.IPv6Loopback.ToString();
}
private string operationName;
public string OperationName
{
get => operationName ??= Dto?.GetType().GetOperationName();
set => operationName = value;
}
public T TryResolve<T>()
{
#if NETSTANDARD2_0
if (ServiceScope != null)
{
var instance = ServiceScope.ServiceProvider.GetService(typeof(T));
if (instance != null)
return (T)instance;
}
#endif
return this.TryResolveInternal<T>();
}
public object GetService(Type serviceType)
{
var mi = typeof(BasicRequest).GetMethod(nameof(TryResolve));
var genericMi = mi.MakeGenericMethod(serviceType);
return genericMi.Invoke(this, TypeConstants.EmptyObjectArray);
}
public string UserHostAddress { get; set; }
public string GetHeader(string headerName)
{
var headerValue = Headers[headerName];
return headerValue;
}
public Dictionary<string, object> Items { get; set; }
public string UserAgent { get; private set; }
public IDictionary<string, Cookie> Cookies { get; set; }
public string Verb { get; set; }
public RequestAttributes RequestAttributes { get; set; }
private IRequestPreferences requestPreferences;
public IRequestPreferences RequestPreferences => requestPreferences ??= new RequestPreferences(this);
public string ContentType { get; set; }
public bool IsLocal { get; private set; }
public string ResponseContentType { get; set; }
public bool HasExplicitResponseContentType { get; set; }
public string CompressionType { get; set; }
public string AbsoluteUri { get; set; }
public string PathInfo { get; set; }
public string OriginalPathInfo => PathInfo;
public IHttpFile[] Files { get; set; }
public Uri UrlReferrer { get; set; }
public NameValueCollection Headers { get; set; }
public NameValueCollection QueryString { get; set; }
public NameValueCollection FormData { get; set; }
public bool UseBufferedStream { get; set; }
private string body;
public string GetRawBody() => body ??= (Message.Body ?? "").Dump();
public Task<string> GetRawBodyAsync() => Task.FromResult(GetRawBody());
public string RawUrl { get; set; }
public string RemoteIp { get; set; }
public string Authorization
{
get => string.IsNullOrEmpty(Headers[HttpHeaders.Authorization])
? null
: Headers[HttpHeaders.Authorization];
set => Headers[HttpHeaders.Authorization] = value;
}
public bool IsSecureConnection
{
get => (RequestAttributes & RequestAttributes.Secure) == RequestAttributes.Secure;
set
{
if (value)
RequestAttributes |= RequestAttributes.Secure;
else
RequestAttributes &= ~RequestAttributes.Secure;
}
}
public string[] AcceptTypes { get; set; }
public Stream InputStream { get; set; } = Stream.Null;
public long ContentLength => (GetRawBody() ?? "").Length;
public BasicRequest PopulateWith(IRequest request)
{
this.Headers = request.Headers;
this.Cookies = request.Cookies;
this.Items = request.Items;
this.UserAgent = request.UserAgent;
this.RemoteIp = request.RemoteIp;
this.UserHostAddress = request.UserHostAddress;
this.IsSecureConnection = request.IsSecureConnection;
this.AcceptTypes = request.AcceptTypes;
return this;
}
public IVirtualFile GetFile() => HostContext.VirtualFileSources.GetFile(PathInfo);
public IVirtualDirectory GetDirectory() => HostContext.VirtualFileSources.GetDirectory(PathInfo);
public bool IsFile { get; set; }
public bool IsDirectory { get; set; }
}
}