Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
DeliveryStatusDecoder.cs
1using System;
3using System.Text;
4using System.Threading.Tasks;
7
8namespace Waher.Content.Dsn
9{
16 {
20 public const string ContentType = "message/delivery-status";
21
28 {
29 }
30
34 public string[] ContentTypes => contentTypes;
35
36 private static readonly string[] contentTypes = new string[] { ContentType };
37
41 public string[] FileExtensions => new string[] { "dsn" };
42
49 public bool Decodes(string ContentType, out Grade Grade)
50 {
52 {
53 Grade = Grade.Excellent;
54 return true;
55 }
56 else
57 {
58 Grade = Grade.NotAtAll;
59 return false;
60 }
61 }
62
69 public bool TryGetContentType(string FileExtension, out string ContentType)
70 {
71 if (string.Compare(FileExtension, "dsn", true) == 0)
72 {
74 return true;
75 }
76 else
77 {
78 ContentType = string.Empty;
79 return false;
80 }
81 }
82
89 public bool TryGetFileExtension(string ContentType, out string FileExtension)
90 {
91 switch (ContentType.ToLower())
92 {
94 FileExtension = "dsn";
95 return true;
96
97 default:
98 FileExtension = string.Empty;
99 return false;
100 }
101 }
102
113 public Task<ContentResponse> DecodeAsync(string ContentType, byte[] Data, Encoding Encoding,
114 KeyValuePair<string, string>[] Fields, Uri BaseUri, ICodecProgress Progress)
115 {
116 string Dsn = Strings.GetString(Data, Encoding ?? Encoding.ASCII);
117 List<string[]> Sections = new List<string[]>();
118 List<string> Section = new List<string>();
119
120 foreach (string Row in Dsn.Replace("\r\n", "\n").Replace("\r", "\n").Split('\n'))
121 {
122 if (string.IsNullOrEmpty(Row))
123 {
124 if (Section.Count > 0)
125 {
126 Sections.Add(Section.ToArray());
127 Section.Clear();
128 }
129 }
130 else
131 {
132 if (Row[0] <= ' ' && Section.Count > 0)
133 Section[Section.Count - 1] += Row;
134 else
135 Section.Add(Row);
136 }
137 }
138
139 if (Section.Count > 0)
140 {
141 Sections.Add(Section.ToArray());
142 Section.Clear();
143 }
144
145 PerMessageFields PerMessage;
146 PerRecipientFields[] PerRecipients;
147 int i, c = Sections.Count;
148
149 if (c == 0)
150 {
151 PerMessage = new PerMessageFields(Array.Empty<string>());
152 PerRecipients = Array.Empty<PerRecipientFields>();
153 }
154 else
155 {
156 PerMessage = new PerMessageFields(Sections[0]);
157 PerRecipients = new PerRecipientFields[c - 1];
158
159 for (i = 1; i < c; i++)
160 PerRecipients[i - 1] = new PerRecipientFields(Sections[i]);
161 }
162
163 return Task.FromResult(new ContentResponse(ContentType, new DeliveryStatus(Dsn, PerMessage, PerRecipients), Data));
164 }
165 }
166}
Contains information about a response to a content request.
Decodes Delivery Status information, as defined in RFC 3464:
DeliveryStatusDecoder()
Decodes Delivery Status information, as defined in RFC 3464:
Task< ContentResponse > DecodeAsync(string ContentType, byte[] Data, Encoding Encoding, KeyValuePair< string, string >[] Fields, Uri BaseUri, ICodecProgress Progress)
Decodes an object.
bool TryGetContentType(string FileExtension, out string ContentType)
Tries to get the content type of an item, given its file extension.
string[] FileExtensions
Supported file extensions.
bool Decodes(string ContentType, out Grade Grade)
If the decoder decodes an object with a given content type.
const string ContentType
message/delivery-status
bool TryGetFileExtension(string ContentType, out string FileExtension)
Tries to get the file extension of an item, given its Content-Type.
string[] ContentTypes
Supported content types.
Delivery Status information, as defined in RFC 3464:
Information fields for the message.
Information fields for one recipient.
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...
Grade
Grade enumeration
Definition: Grade.cs:7