2using System.Collections.Generic;
4using System.Threading.Tasks;
17 public const string ContentType =
"application/x-www-form-urlencoded";
66 public Task<object>
DecodeAsync(
string ContentType,
byte[] Data, Encoding Encoding, KeyValuePair<string, string>[] Fields, Uri BaseUri)
68 Dictionary<string, string> Form =
new Dictionary<string, string>();
69 Dictionary<string, List<string>> Form2 =
null;
74 foreach (
string Parameter
in s.Split(
'&'))
76 if (
string.IsNullOrEmpty(Parameter))
79 i = Parameter.IndexOf(
'=');
83 Key = Uri.UnescapeDataString(Parameter.Substring(0, i).Replace(
"+",
" "));
84 Value = Uri.UnescapeDataString(Parameter.Substring(i + 1).Replace(
"+",
" "));
94 if (Form.ContainsKey(Key))
96 Form2 =
new Dictionary<string, List<string>>();
98 foreach (KeyValuePair<string, string> P
in Form)
99 Form2[P.Key] =
new List<string>() { P.Value };
108 if (!Form2.TryGetValue(Key, out List<string> Values))
110 Values =
new List<string>();
118 return Task.FromResult<
object>(Form);
120 Dictionary<string, string[]> Form3 =
new Dictionary<string, string[]>();
122 foreach (KeyValuePair<
string, List<string>> P
in Form2)
123 Form3[P.Key] = P.Value.ToArray();
125 return Task.FromResult<
object>(Form3);
136 if (
string.Compare(FileExtension,
"webform",
true) == 0)
159 FileExtension =
"webform";
163 FileExtension =
string.Empty;
177 if (Object is Dictionary<string, string> &&
198 public Task<KeyValuePair<byte[], string>>
EncodeAsync(
object Object, Encoding Encoding, params
string[] AcceptedContentTypes)
200 if (Object is Dictionary<string, string> Form)
202 StringBuilder sb =
new StringBuilder();
207 foreach (KeyValuePair<string, string> Pair
in Form)
214 sb.Append(Uri.EscapeDataString(Pair.Key));
216 sb.Append(Uri.EscapeDataString(Pair.Value));
219 if (Encoding is
null)
221 ContentType = WwwFormCodec.ContentType +
"; charset=utf-8";
222 Bin = Encoding.UTF8.GetBytes(sb.ToString());
226 ContentType = WwwFormCodec.ContentType +
"; charset=" + Encoding.WebName;
227 Bin = Encoding.GetBytes(sb.ToString());
230 return Task.FromResult(
new KeyValuePair<
byte[],
string>(Bin,
ContentType));
233 throw new ArgumentException(
"Unable to encode object, or content type not accepted.", nameof(Object));
Helps with parsing of commong data types.
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.
bool Decodes(string ContentType, out Grade Grade)
If the decoder decodes an object with a given content type.
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.
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.
Task< object > DecodeAsync(string ContentType, byte[] Data, Encoding Encoding, KeyValuePair< string, string >[] Fields, Uri BaseUri)
Decodes an object.
string[] ContentTypes
Supported content types.
const string ContentType
application/x-www-form-urlencoded
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...