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;
3using System.Text;
4using System.Threading.Tasks;
8
10{
15 {
19 public const string ContentType = "application/x-www-form-urlencoded";
20
24 public WwwFormCodec()
25 {
26 }
27
31 public string[] ContentTypes => contentTypes;
32
33 private static readonly string[] contentTypes = new string[] { ContentType };
34
38 public string[] FileExtensions => new string[] { "webform" };
39
46 public bool Decodes(string ContentType, out Grade Grade)
47 {
49 {
50 Grade = Grade.Excellent;
51 return true;
52 }
53 else
54 {
55 Grade = Grade.NotAtAll;
56 return false;
57 }
58 }
59
70 public Task<ContentResponse> DecodeAsync(string ContentType, byte[] Data, Encoding Encoding,
71 KeyValuePair<string, string>[] Fields, Uri BaseUri, ICodecProgress Progress)
72 {
73 Dictionary<string, string> Form = new Dictionary<string, string>();
74 Dictionary<string, ChunkedList<string>> Form2 = null;
75 string s = Strings.GetString(Data, Encoding);
76 string Key, Value;
77 int i;
78
79 foreach (string Parameter in s.Split('&'))
80 {
81 if (string.IsNullOrEmpty(Parameter))
82 continue;
83
84 i = Parameter.IndexOf('=');
85
86 if (i >= 0)
87 {
88 Key = Uri.UnescapeDataString(Parameter.Substring(0, i).Replace("+", " "));
89 Value = Uri.UnescapeDataString(Parameter.Substring(i + 1).Replace("+", " "));
90 }
91 else
92 {
93 Key = Parameter;
94 Value = string.Empty;
95 }
96
97 if (Form2 is null)
98 {
99 if (Form.ContainsKey(Key))
100 {
101 Form2 = new Dictionary<string, ChunkedList<string>>();
102
103 foreach (KeyValuePair<string, string> P in Form)
104 Form2[P.Key] = new ChunkedList<string>() { P.Value };
105 }
106 else
107 {
108 Form[Key] = Value;
109 continue;
110 }
111 }
112
113 if (!Form2.TryGetValue(Key, out ChunkedList<string> Values))
114 {
115 Values = new ChunkedList<string>();
116 Form2[Key] = Values;
117 }
118
119 Values.Add(Value);
120 }
121
122 if (Form2 is null)
123 return Task.FromResult(new ContentResponse(ContentType, Form, Data));
124
125 Dictionary<string, string[]> Form3 = new Dictionary<string, string[]>();
126
127 foreach (KeyValuePair<string, ChunkedList<string>> P in Form2)
128 Form3[P.Key] = P.Value.ToArray();
129
130 return Task.FromResult(new ContentResponse(ContentType, Form3, Data));
131 }
132
139 public bool TryGetContentType(string FileExtension, out string ContentType)
140 {
141 if (string.Compare(FileExtension, "webform", true) == 0)
142 {
144 return true;
145 }
146 else
147 {
148 ContentType = string.Empty;
149 return false;
150 }
151 }
152
159 public bool TryGetFileExtension(string ContentType, out string FileExtension)
160 {
161 switch (ContentType.ToLower())
162 {
164 FileExtension = "webform";
165 return true;
166
167 default:
168 FileExtension = string.Empty;
169 return false;
170 }
171 }
172
180 public bool Encodes(object Object, out Grade Grade, params string[] AcceptedContentTypes)
181 {
182 if (Object is Dictionary<string, string> &&
183 InternetContent.IsAccepted(contentTypes, AcceptedContentTypes))
184 {
185 Grade = Grade.Ok;
186 return true;
187 }
188 else
189 {
190 Grade = Grade.NotAtAll;
191 return false;
192 }
193 }
194
203 public Task<ContentResponse> EncodeAsync(object Object, Encoding Encoding,
204 ICodecProgress Progress, params string[] AcceptedContentTypes)
205 {
206 if (Object is Dictionary<string, string> Form)
207 {
208 StringBuilder sb = new StringBuilder();
209 string ContentType;
210 bool First = true;
211 byte[] Bin;
212
213 foreach (KeyValuePair<string, string> Pair in Form)
214 {
215 if (First)
216 First = false;
217 else
218 sb.Append('&');
219
220 sb.Append(Uri.EscapeDataString(Pair.Key));
221 sb.Append('=');
222 if (!(Pair.Value is null))
223 sb.Append(Uri.EscapeDataString(Pair.Value));
224 }
225
226 if (Encoding is null)
227 {
228 ContentType = WwwFormCodec.ContentType + "; charset=utf-8";
229 Bin = Encoding.UTF8.GetBytes(sb.ToString());
230 }
231 else
232 {
233 ContentType = WwwFormCodec.ContentType + "; charset=" + Encoding.WebName;
234 Bin = Encoding.GetBytes(sb.ToString());
235 }
236
237 return Task.FromResult(new ContentResponse(ContentType, Object, Bin));
238 }
239 else
240 return Task.FromResult(new ContentResponse(new ArgumentException("Unable to encode object, or content type not accepted.", nameof(Object))));
241 }
242
243 }
244}
Contains information about a response to a content request.
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:15
Task< ContentResponse > EncodeAsync(object Object, Encoding Encoding, ICodecProgress Progress, params string[] AcceptedContentTypes)
Encodes an object.
bool Decodes(string ContentType, out Grade Grade)
If the decoder decodes an object with a given content type.
Definition: WwwFormCodec.cs:46
bool Encodes(object Object, out Grade Grade, params string[] AcceptedContentTypes)
If the encoder encodes a given object.
WwwFormCodec()
Codec of URL encoded web forms.
Definition: WwwFormCodec.cs:24
bool TryGetFileExtension(string ContentType, out string FileExtension)
Tries to get the file extension of an item, given its Content-Type.
Task< ContentResponse > DecodeAsync(string ContentType, byte[] Data, Encoding Encoding, KeyValuePair< string, string >[] Fields, Uri BaseUri, ICodecProgress Progress)
Decodes an object.
Definition: WwwFormCodec.cs:70
string[] FileExtensions
Supported file extensions.
Definition: WwwFormCodec.cs:38
string[] ContentTypes
Supported content types.
Definition: WwwFormCodec.cs:31
const string ContentType
application/x-www-form-urlencoded
Definition: WwwFormCodec.cs:19
bool TryGetContentType(string FileExtension, out string ContentType)
Tries to get the content type of an item, given its file extension.
A chunked list is a linked list of chunks of objects of type T .
Definition: ChunkedList.cs:54
Static class managing binary representations of strings.
Definition: Strings.cs:10
static string GetString(byte[] Data, int Offset, int Count, Encoding DefaultEncoding)
Gets a string from its binary representation, taking any Byte Order Mark (BOM) into account.
Definition: Strings.cs:148
Interface for reporting progress about an encoding or decoding.
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