-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
ServerRequest.Owin.cs
214 lines (189 loc) · 6.27 KB
/
ServerRequest.Owin.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
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
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using SignalR.Server.Infrastructure;
namespace SignalR.Server
{
public partial class ServerRequest
{
private readonly IDictionary<string, object> _env;
public ServerRequest(IDictionary<string, object> env)
{
_env = env;
}
private T Get<T>(string key)
{
object value;
return _env.TryGetValue(key, out value) ? (T)value : default(T);
}
private string RequestMethod
{
get { return Get<string>(OwinConstants.RequestMethod); }
}
public IDictionary<string, string[]> RequestHeaders
{
get { return Get<IDictionary<string, string[]>>(OwinConstants.RequestHeaders); }
}
private Stream RequestBody
{
get { return Get<Stream>(OwinConstants.RequestBody); }
}
private string RequestScheme
{
get { return Get<string>(OwinConstants.RequestScheme); }
}
private string RequestPathBase
{
get { return Get<string>(OwinConstants.RequestPathBase); }
}
private string RequestPath
{
get { return Get<string>(OwinConstants.RequestPath); }
}
private string RequestQueryString
{
get { return Get<string>(OwinConstants.RequestQueryString); }
}
public Action DisableRequestBuffering
{
get { return Get<Action>(OwinConstants.DisableRequestBuffering) ?? (() => { }); }
}
private bool TryParseHostHeader(out IPAddress address, out string host, out int port)
{
address = null;
host = null;
port = 0;
var hostHeader = RequestHeaders.GetHeader("Host");
if (String.IsNullOrWhiteSpace(hostHeader))
{
return false;
}
if (hostHeader.StartsWith("[", StringComparison.Ordinal))
{
var portIndex = hostHeader.LastIndexOf("]:", StringComparison.Ordinal);
if (portIndex != -1 && int.TryParse(hostHeader.Substring(portIndex + 2), out port))
{
if (IPAddress.TryParse(hostHeader.Substring(1, portIndex - 1), out address))
{
host = null;
return true;
}
host = hostHeader.Substring(0, portIndex + 1);
return true;
}
if (hostHeader.EndsWith("]", StringComparison.Ordinal))
{
if (IPAddress.TryParse(hostHeader.Substring(1, hostHeader.Length - 2), out address))
{
host = null;
port = 0;
return true;
}
}
}
else
{
if (IPAddress.TryParse(hostHeader, out address))
{
host = null;
port = 0;
return true;
}
var portIndex = hostHeader.LastIndexOf(':');
if (portIndex != -1 && int.TryParse(hostHeader.Substring(portIndex + 1), out port))
{
host = hostHeader.Substring(0, portIndex);
return true;
}
}
host = hostHeader;
return true;
}
private string RequestHost
{
get
{
IPAddress address;
string host;
int port;
if (TryParseHostHeader(out address, out host, out port))
{
return host ?? address.ToString();
}
return Get<string>(OwinConstants.LocalIpAddress) ?? IPAddress.Loopback.ToString();
}
}
private int RequestPort
{
get
{
IPAddress address;
string host;
int port;
if (TryParseHostHeader(out address, out host, out port) && port != 0)
{
return port;
}
var portString = Get<string>(OwinConstants.LocalPort);
if (int.TryParse(portString, out port) && port != 0)
{
return port;
}
return String.Equals(RequestScheme, "https", StringComparison.OrdinalIgnoreCase) ? 443 : 80;
}
}
private string ContentType
{
get
{
return RequestHeaders.GetHeader("Content-Type");
}
}
private string MediaType
{
get
{
var contentType = ContentType;
if (contentType == null)
return null;
var delimiterPos = contentType.IndexOfAny(CommaSemicolon);
return delimiterPos < 0 ? contentType : contentType.Substring(0, delimiterPos);
}
}
private bool HasFormData
{
get
{
var mediaType = MediaType;
return (RequestMethod == "POST" && String.IsNullOrEmpty(mediaType))
|| mediaType == "application/x-www-form-urlencoded"
|| mediaType == "multipart/form-data";
}
}
private bool HasParseableData
{
get
{
var mediaType = MediaType;
return mediaType == "application/x-www-form-urlencoded"
|| mediaType == "multipart/form-data";
}
}
private IEnumerable<KeyValuePair<string, string>> ReadForm()
{
if (!HasFormData && !HasParseableData)
{
return Enumerable.Empty<KeyValuePair<string, string>>();
}
var body = RequestBody;
if (body.CanSeek)
{
body.Seek(0, SeekOrigin.Begin);
}
var text = new StreamReader(body).ReadToEnd();
return ParamDictionary.ParseToEnumerable(text, null);
}
}
}