forked from PeterWaher/IoTGateway
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUPnPHeaders.cs
More file actions
247 lines (207 loc) · 5.99 KB
/
UPnPHeaders.cs
File metadata and controls
247 lines (207 loc) · 5.99 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
using System;
using System.Collections.Generic;
using System.Text;
namespace Waher.Networking.UPnP
{
/// <summary>
/// Direction of message
/// </summary>
public enum HttpDirection
{
/// <summary>
/// Message is a request message.
/// </summary>
Request,
/// <summary>
/// Message is a response message.
/// </summary>
Response,
/// <summary>
/// Unknown HTTP message format.
/// </summary>
Unknown
}
/// <summary>
/// Class managing any HTTP headers in a UPnP UDP request/response.
/// </summary>
public class UPnPHeaders : IEnumerable<KeyValuePair<string, string>>
{
private readonly Dictionary<string, string> headers = new Dictionary<string, string>();
private readonly HttpDirection direction = HttpDirection.Unknown;
private readonly string[] rows;
private readonly string searchTarget = string.Empty;
private readonly string server = string.Empty;
private readonly string location = string.Empty;
private readonly string uniqueServiceName = string.Empty;
private readonly string verb = string.Empty;
private readonly string parameter = string.Empty;
private readonly string responseMessage = string.Empty;
private readonly string host = string.Empty;
private readonly string cacheControl = string.Empty;
private readonly string notificationType = string.Empty;
private readonly string notificationSubType = string.Empty;
private readonly double httpVersion = 0;
private readonly int responseCode = 0;
internal UPnPHeaders(string Header)
{
string s, Key, Value;
int i, j, c;
this.rows = Header.Split(CRLF, StringSplitOptions.RemoveEmptyEntries);
c = this.rows.Length;
if (c > 0)
{
string[] P = this.rows[0].Split(' ');
if (P.Length == 3 && P[2].StartsWith("HTTP/"))
{
this.direction = HttpDirection.Request;
this.verb = P[0];
this.parameter = P[1];
if (!double.TryParse(P[2].Substring(5).Replace(".", System.Globalization.NumberFormatInfo.CurrentInfo.NumberDecimalSeparator), out this.httpVersion))
this.httpVersion = 0;
}
else if (P.Length >= 3 && P[0].StartsWith("HTTP/"))
{
this.direction = HttpDirection.Response;
if (!double.TryParse(P[0].Substring(5).Replace(".", System.Globalization.NumberFormatInfo.CurrentInfo.NumberDecimalSeparator), out this.httpVersion))
this.httpVersion = 0;
if (!int.TryParse(P[1], out this.responseCode))
this.responseCode = 0;
this.responseMessage = null;
for (i = 2; i < P.Length; i++)
{
if (this.responseMessage is null)
this.responseMessage = P[i];
else
this.responseMessage += " " + P[i];
}
}
}
for (i = 1; i < c; i++)
{
s = rows[i];
j = s.IndexOf(':');
Key = s.Substring(0, j).ToUpper();
Value = s.Substring(j + 1).TrimStart();
this.headers[Key] = Value;
switch (Key)
{
case "ST":
this.searchTarget = Value;
break;
case "SERVER":
this.server = Value;
break;
case "LOCATION":
this.location = Value;
break;
case "USN":
this.uniqueServiceName = Value;
break;
case "HOST":
this.host = Value;
break;
case "CACHE-CONTROL":
this.cacheControl = Value;
break;
case "NT":
this.notificationType = Value;
break;
case "NTS":
this.notificationSubType = Value;
break;
}
}
}
/// <summary>
/// CR or LF characters.
/// </summary>
internal static readonly char[] CRLF = new char[] { '\r', '\n' };
/// <summary>
/// Gets the value of the corresponding key. If the key is not found, the empty string is returned.
/// </summary>
/// <param name="Key">Key</param>
/// <returns>Value</returns>
public string this[string Key]
{
get
{
if (this.headers.TryGetValue(Key, out string Value))
return Value;
else
return string.Empty;
}
}
/// <summary>
/// Message direction.
/// </summary>
public HttpDirection Direction { get { return this.direction; } }
/// <summary>
/// HTTP Verb
/// </summary>
public string Verb { get { return this.verb; } }
/// <summary>
/// HTTP Parameter
/// </summary>
public string Parameter { get { return this.parameter; } }
/// <summary>
/// HTTP Version
/// </summary>
public double HttpVersion { get { return this.httpVersion; } }
/// <summary>
/// Search Target header
/// </summary>
public string SearchTarget { get { return this.searchTarget; } }
/// <summary>
/// Server header
/// </summary>
public string Server { get { return this.server; } }
/// <summary>
/// Location header
/// </summary>
public string Location { get { return this.location; } }
/// <summary>
/// Unique Service Name (USN) header
/// </summary>
public string UniqueServiceName { get { return this.uniqueServiceName; } }
/// <summary>
/// Response message
/// </summary>
public string ResponseMessage { get { return this.responseMessage; } }
/// <summary>
/// Host
/// </summary>
public string Host { get { return this.host; } }
/// <summary>
/// Cache Control
/// </summary>
public string CacheControl { get { return this.cacheControl; } }
/// <summary>
/// Notification Type
/// </summary>
public string NotificationType { get { return this.notificationType; } }
/// <summary>
/// Notification Sub-type
/// </summary>
public string NotificationSubType { get { return this.notificationSubType; } }
/// <summary>
/// Response Code
/// </summary>
public int ResponseCode { get { return this.responseCode; } }
/// <summary>
/// Gets an enumerator, enumerating all headers.
/// </summary>
/// <returns>Enumerator</returns>
public IEnumerator<KeyValuePair<string, string>> GetEnumerator()
{
return this.headers.GetEnumerator();
}
/// <summary>
/// Gets an enumerator, enumerating all headers.
/// </summary>
/// <returns>Enumerator</returns>
System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
{
return this.headers.GetEnumerator();
}
}
}