Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
UPnPHeaders.cs
1using System;
2using System.Collections.Generic;
3using Waher.Content;
4
6{
10 public enum HttpDirection
11 {
15 Request,
16
20 Response,
21
25 Unknown
26 }
27
31 public class UPnPHeaders : IEnumerable<KeyValuePair<string, string>>, IHostReference
32 {
33 private readonly Dictionary<string, string> headers = new Dictionary<string, string>();
34 private readonly HttpDirection direction = HttpDirection.Unknown;
35 private readonly string[] rows;
36 private readonly string searchTarget = string.Empty;
37 private readonly string server = string.Empty;
38 private readonly string location = string.Empty;
39 private readonly string uniqueServiceName = string.Empty;
40 private readonly string verb = string.Empty;
41 private readonly string parameter = string.Empty;
42 private readonly string responseMessage = string.Empty;
43 private readonly string host = string.Empty;
44 private readonly string cacheControl = string.Empty;
45 private readonly string notificationType = string.Empty;
46 private readonly string notificationSubType = string.Empty;
47 private readonly double httpVersion = 0;
48 private readonly int responseCode = 0;
49
50 internal UPnPHeaders(string Header)
51 {
52 string s, Key, Value;
53 int i, j, c;
54
55 this.rows = Header.Split(CRLF, StringSplitOptions.RemoveEmptyEntries);
56 c = this.rows.Length;
57
58 if (c > 0)
59 {
60 string[] P = this.rows[0].Split(' ');
61 if (P.Length == 3 && P[2].StartsWith("HTTP/"))
62 {
63 this.direction = HttpDirection.Request;
64 this.verb = P[0];
65 this.parameter = P[1];
66
67 if (!double.TryParse(P[2].Substring(5).Replace(".", System.Globalization.NumberFormatInfo.CurrentInfo.NumberDecimalSeparator), out this.httpVersion))
68 this.httpVersion = 0;
69 }
70 else if (P.Length >= 3 && P[0].StartsWith("HTTP/"))
71 {
72 this.direction = HttpDirection.Response;
73
74 if (!double.TryParse(P[0].Substring(5).Replace(".", System.Globalization.NumberFormatInfo.CurrentInfo.NumberDecimalSeparator), out this.httpVersion))
75 this.httpVersion = 0;
76
77 if (!int.TryParse(P[1], out this.responseCode))
78 this.responseCode = 0;
79
80 this.responseMessage = null;
81 for (i = 2; i < P.Length; i++)
82 {
83 if (this.responseMessage is null)
84 this.responseMessage = P[i];
85 else
86 this.responseMessage += " " + P[i];
87 }
88 }
89 }
90
91 for (i = 1; i < c; i++)
92 {
93 s = rows[i];
94 j = s.IndexOf(':');
95
96 Key = s.Substring(0, j).ToUpper();
97 Value = s.Substring(j + 1).TrimStart();
98
99 this.headers[Key] = Value;
100
101 switch (Key)
102 {
103 case "ST":
104 this.searchTarget = Value;
105 break;
106
107 case "SERVER":
108 this.server = Value;
109 break;
110
111 case "LOCATION":
112 this.location = Value;
113 break;
114
115 case "USN":
116 this.uniqueServiceName = Value;
117 break;
118
119 case "HOST":
120 this.host = Value;
121 break;
122
123 case "CACHE-CONTROL":
124 this.cacheControl = Value;
125 break;
126
127 case "NT":
128 this.notificationType = Value;
129 break;
130
131 case "NTS":
132 this.notificationSubType = Value;
133 break;
134 }
135 }
136 }
137
141 internal static readonly char[] CRLF = new char[] { '\r', '\n' };
142
148 public string this[string Key]
149 {
150 get
151 {
152 if (this.headers.TryGetValue(Key, out string Value))
153 return Value;
154 else
155 return string.Empty;
156 }
157 }
158
162 public HttpDirection Direction => this.direction;
163
167 public string Verb => this.verb;
168
172 public string Parameter => this.parameter;
173
177 public double HttpVersion => this.httpVersion;
178
182 public string SearchTarget => this.searchTarget;
183
187 public string Server => this.server;
188
192 public string Location => this.location;
193
197 public string UniqueServiceName => this.uniqueServiceName;
198
202 public string ResponseMessage => this.responseMessage;
203
207 public string Host => this.host;
208
212 public string CacheControl => this.cacheControl;
213
217 public string NotificationType => this.notificationType;
218
222 public string NotificationSubType => this.notificationSubType;
223
227 public int ResponseCode => this.responseCode;
228
233 public IEnumerator<KeyValuePair<string, string>> GetEnumerator()
234 {
235 return this.headers.GetEnumerator();
236 }
237
242 System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
243 {
244 return this.headers.GetEnumerator();
245 }
246 }
247}
Class managing any HTTP headers in a UPnP UDP request/response.
Definition: UPnPHeaders.cs:32
string Parameter
HTTP Parameter
Definition: UPnPHeaders.cs:172
double HttpVersion
HTTP Version
Definition: UPnPHeaders.cs:177
string ResponseMessage
Response message
Definition: UPnPHeaders.cs:202
string SearchTarget
Search Target header
Definition: UPnPHeaders.cs:182
string NotificationType
Notification Type
Definition: UPnPHeaders.cs:217
string NotificationSubType
Notification Sub-type
Definition: UPnPHeaders.cs:222
string CacheControl
Cache Control
Definition: UPnPHeaders.cs:212
IEnumerator< KeyValuePair< string, string > > GetEnumerator()
Gets an enumerator, enumerating all headers.
Definition: UPnPHeaders.cs:233
string UniqueServiceName
Unique Service Name (USN) header
Definition: UPnPHeaders.cs:197
string Location
Location header
Definition: UPnPHeaders.cs:192
HttpDirection Direction
Message direction.
Definition: UPnPHeaders.cs:162
Interface for objects that contain a reference to a host.
HttpDirection
Direction of message
Definition: UPnPHeaders.cs:11