Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
WwwFormCodec.cs
1using System;
2using System.Collections.Generic;
3using System.Text;
4using System.Threading.Tasks;
6
8{
13 {
17 public const string ContentType = "application/x-www-form-urlencoded";
18
22 public WwwFormCodec()
23 {
24 }
25
29 public string[] ContentTypes => new string[] { ContentType };
30
34 public string[] FileExtensions => new string[] { "webform" };
35
42 public bool Decodes(string ContentType, out Grade Grade)
43 {
45 {
46 Grade = Grade.Excellent;
47 return true;
48 }
49 else
50 {
51 Grade = Grade.NotAtAll;
52 return false;
53 }
54 }
55
66 public Task<object> DecodeAsync(string ContentType, byte[] Data, Encoding Encoding, KeyValuePair<string, string>[] Fields, Uri BaseUri)
67 {
68 Dictionary<string, string> Form = new Dictionary<string, string>();
69 Dictionary<string, List<string>> Form2 = null;
70 string s = CommonTypes.GetString(Data, Encoding);
71 string Key, Value;
72 int i;
73
74 foreach (string Parameter in s.Split('&'))
75 {
76 if (string.IsNullOrEmpty(Parameter))
77 continue;
78
79 i = Parameter.IndexOf('=');
80
81 if (i >= 0)
82 {
83 Key = Uri.UnescapeDataString(Parameter.Substring(0, i).Replace("+", " "));
84 Value = Uri.UnescapeDataString(Parameter.Substring(i + 1).Replace("+", " "));
85 }
86 else
87 {
88 Key = Parameter;
89 Value = string.Empty;
90 }
91
92 if (Form2 is null)
93 {
94 if (Form.ContainsKey(Key))
95 {
96 Form2 = new Dictionary<string, List<string>>();
97
98 foreach (KeyValuePair<string, string> P in Form)
99 Form2[P.Key] = new List<string>() { P.Value };
100 }
101 else
102 {
103 Form[Key] = Value;
104 continue;
105 }
106 }
107
108 if (!Form2.TryGetValue(Key, out List<string> Values))
109 {
110 Values = new List<string>();
111 Form2[Key] = Values;
112 }
113
114 Values.Add(Value);
115 }
116
117 if (Form2 is null)
118 return Task.FromResult<object>(Form);
119
120 Dictionary<string, string[]> Form3 = new Dictionary<string, string[]>();
121
122 foreach (KeyValuePair<string, List<string>> P in Form2)
123 Form3[P.Key] = P.Value.ToArray();
124
125 return Task.FromResult<object>(Form3);
126 }
127
134 public bool TryGetContentType(string FileExtension, out string ContentType)
135 {
136 if (string.Compare(FileExtension, "webform", true) == 0)
137 {
139 return true;
140 }
141 else
142 {
143 ContentType = string.Empty;
144 return false;
145 }
146 }
147
154 public bool TryGetFileExtension(string ContentType, out string FileExtension)
155 {
156 switch (ContentType.ToLower())
157 {
159 FileExtension = "webform";
160 return true;
161
162 default:
163 FileExtension = string.Empty;
164 return false;
165 }
166 }
167
175 public bool Encodes(object Object, out Grade Grade, params string[] AcceptedContentTypes)
176 {
177 if (Object is Dictionary<string, string> &&
178 InternetContent.IsAccepted(this.ContentTypes, AcceptedContentTypes))
179 {
180 Grade = Grade.Ok;
181 return true;
182 }
183 else
184 {
185 Grade = Grade.NotAtAll;
186 return false;
187 }
188 }
189
198 public Task<KeyValuePair<byte[], string>> EncodeAsync(object Object, Encoding Encoding, params string[] AcceptedContentTypes)
199 {
200 if (Object is Dictionary<string, string> Form)
201 {
202 StringBuilder sb = new StringBuilder();
203 string ContentType;
204 bool First = true;
205 byte[] Bin;
206
207 foreach (KeyValuePair<string, string> Pair in Form)
208 {
209 if (First)
210 First = false;
211 else
212 sb.Append('&');
213
214 sb.Append(Uri.EscapeDataString(Pair.Key));
215 sb.Append('=');
216 sb.Append(Uri.EscapeDataString(Pair.Value));
217 }
218
219 if (Encoding is null)
220 {
221 ContentType = WwwFormCodec.ContentType + "; charset=utf-8";
222 Bin = Encoding.UTF8.GetBytes(sb.ToString());
223 }
224 else
225 {
226 ContentType = WwwFormCodec.ContentType + "; charset=" + Encoding.WebName;
227 Bin = Encoding.GetBytes(sb.ToString());
228 }
229
230 return Task.FromResult(new KeyValuePair<byte[], string>(Bin, ContentType));
231 }
232 else
233 throw new ArgumentException("Unable to encode object, or content type not accepted.", nameof(Object));
234 }
235
236 }
237}
Helps with parsing of commong data types.
Definition: CommonTypes.cs:13
static string GetString(byte[] Data, Encoding DefaultEncoding)
Gets a string from its binary representation, taking any Byte Order Mark (BOM) into account.
Static class managing encoding and decoding of internet content.
static bool IsAccepted(string ContentType, params string[] AcceptedContentTypes)
Checks if a given content type is acceptable.
Codec of URL encoded web forms.
Definition: WwwFormCodec.cs:13
bool Decodes(string ContentType, out Grade Grade)
If the decoder decodes an object with a given content type.
Definition: WwwFormCodec.cs:42
bool Encodes(object Object, out Grade Grade, params string[] AcceptedContentTypes)
If the encoder encodes a given object.
Task< KeyValuePair< byte[], string > > EncodeAsync(object Object, Encoding Encoding, params string[] AcceptedContentTypes)
Encodes an object.
WwwFormCodec()
Codec of URL encoded web forms.
Definition: WwwFormCodec.cs:22
bool TryGetFileExtension(string ContentType, out string FileExtension)
Tries to get the file extension of an item, given its Content-Type.
string[] FileExtensions
Supported file extensions.
Definition: WwwFormCodec.cs:34
Task< object > DecodeAsync(string ContentType, byte[] Data, Encoding Encoding, KeyValuePair< string, string >[] Fields, Uri BaseUri)
Decodes an object.
Definition: WwwFormCodec.cs:66
string[] ContentTypes
Supported content types.
Definition: WwwFormCodec.cs:29
const string ContentType
application/x-www-form-urlencoded
Definition: WwwFormCodec.cs:17
bool TryGetContentType(string FileExtension, out string ContentType)
Tries to get the content type of an item, given its file extension.
Basic interface for Internet Content decoders. A class implementing this interface and having a defau...
Basic interface for Internet Content encoders. A class implementing this interface and having a defau...
Grade
Grade enumeration
Definition: Grade.cs:7