Beispiel #1
0
        /// <summary>
        /// Fetches a single view from the requested message.
        /// </summary>
        /// <param name="info">The associated info object, this token can be obtained from the messages body structure.</param>
        /// <returns>The requested view.</returns>
        public View FetchView2(ViewInfo info)
        {
            var text = FetchEntityBody(info);

            if (string.IsNullOrEmpty(text))
            {
                return(null);
            }
            var paramDictionary = ((IDictionary <string, string>)info.Parameters);
            var hasValidCharset = paramDictionary.ContainsKey("charset");
            var charset         = hasValidCharset ? paramDictionary["charset"] : Charsets.Utf8;
            var decoded         = TransferEncoder.Decode(text, info.ContentTransferEncoding, charset);

            return(new View {
                MediaType = info.MediaType, Text = decoded
            });
        }
Beispiel #2
0
        public void Deserialize(string literals)
        {
            var      isBeginning = true;
            LineInfo?last        = null;

            using (var reader = new StringReader(literals)) {
                while (true)
                {
                    var line = reader.ReadLine();
                    if (line == null)
                    {
                        break;
                    }

                    // darn new lines
                    if (line == string.Empty && isBeginning)
                    {
                        continue;
                    }

                    isBeginning = false;
                    var current = new LineInfo(line);
                    if (current.IsField)
                    {
                        if (last.HasValue)
                        {
                            CommitHeaderField(last.Value);
                        }

                        last = current;
                        continue;
                    }

                    if (current.IsContinuation)
                    {
                        if (last.HasValue)
                        {
                            last = last.Value.Merge(current);
                        }
                        continue;
                    }

                    if (!current.IsBoundaryStart && IsBoundaryExpected)
                    {
                        continue;
                    }

                    // process final field
                    if (last.HasValue)
                    {
                        CommitHeaderField(last.Value);
                        last = null;
                    }

                    if (!IsBoundaryExpected)
                    {
                        var charset  = DetermineCharset();
                        var encoding = DetermineEncoding();
                        var content  = reader.ReadToEnd() ?? string.Empty;
                        content.Trim();

                        if (!string.IsNullOrEmpty(content))
                        {
                            if (HasContentType)
                            {
                                var contentType = ContentTypeHeaderField.MediaType;
                                if (encoding == ContentTransferEncodings.Base64 && !contentType.StartsWith("text"))
                                {
                                    Bytes = string.IsNullOrEmpty(content) ? new byte[0] : Convert.FromBase64String(content);
                                }
                                else
                                {
                                    Text = TransferEncoder.Decode(content, encoding, charset);
                                }
                            }
                            else
                            {
                                Text = TransferEncoder.Decode(content, encoding, charset);
                            }
                        }
                        break;
                    }

                    if (current.IsBoundaryStart && IsExpectedBoundary(current))
                    {
                        var parts = ReadBoundaryBlocks(current.BoundaryName, reader);
                        foreach (var part in parts)
                        {
                            var entity = new Entity();
                            entity.Deserialize(part);
                            Children.Add(entity);
                        }
                    }
                }
            }
        }