Пример #1
0
        public override NSCachedUrlResponse CachedResponseForRequest(NSUrlRequest request)
        {
            var uri = (Uri)request.Url;
            int index;

            if ((index = related.IndexOf(uri)) != -1)
            {
                var part = related[index] as MimePart;

                if (part != null)
                {
                    var           mimeType = part.ContentType.MimeType;
                    var           charset  = part.ContentType.Charset;
                    NSUrlResponse response;
                    NSData        data;

                    using (var content = part.ContentObject.Open())
                        data = NSData.FromStream(content);

                    response = new NSUrlResponse(request.Url, mimeType, (int)(uint)data.Length, charset);

                    return(new NSCachedUrlResponse(response, data));
                }
            }

            return(base.CachedResponseForRequest(request));
        }
Пример #2
0
            public void HtmlTagCallback(HtmlTagContext ctx, HtmlWriter htmlWriter)
            {
                if (ctx.TagId != HtmlTagId.Image || ctx.IsEndTag)
                {
                    ctx.WriteTag(htmlWriter, true);
                    return;
                }

                // write the IMG tag, but don't write out the attributes.
                ctx.WriteTag(htmlWriter, false);

                // manually write the attributes so that we can replace the SRC attributes
                foreach (var attribute in ctx.Attributes)
                {
                    if (attribute.Id == HtmlAttributeId.Src)
                    {
                        int index;
                        Uri uri;

                        // parse the <img src=...> attribute value into a Uri
                        if (Uri.IsWellFormedUriString(attribute.Value, UriKind.Absolute))
                        {
                            uri = new Uri(attribute.Value, UriKind.Absolute);
                        }
                        else
                        {
                            uri = new Uri(attribute.Value, UriKind.Relative);
                        }

                        // locate the index of the attachment within the multipart/related (if it exists)
                        if ((index = related.IndexOf(uri)) != -1)
                        {
                            var attachment = related[index] as MimePart;

                            if (attachment == null)
                            {
                                // the body part is not a basic leaf part (IOW it's a multipart or message-part)
                                htmlWriter.WriteAttribute(attribute);
                                continue;
                            }

                            var data = GetDataUri(attachment);

                            htmlWriter.WriteAttributeName(attribute.Name);
                            htmlWriter.WriteAttributeValue(data);
                        }
                        else
                        {
                            htmlWriter.WriteAttribute(attribute);
                        }
                    }
                }
            }
Пример #3
0
            public void HtmlTagCallback(HtmlTagContext ctx, HtmlWriter htmlWriter)
            {
                if (ctx.TagId != HtmlTagId.Image || ctx.IsEndTag)
                {
                    ctx.WriteTag(htmlWriter, true);
                    return;
                }

                ctx.WriteTag(htmlWriter, false);

                foreach (var attribute in ctx.Attributes)
                {
                    if (attribute.Id == HtmlAttributeId.Src)
                    {
                        int index;
                        Uri uri;

                        if (Uri.IsWellFormedUriString(attribute.Value, UriKind.Absolute))
                        {
                            uri = new Uri(attribute.Value, UriKind.Absolute);
                        }
                        else
                        {
                            uri = new Uri(attribute.Value, UriKind.Relative);
                        }

                        if ((index = related.IndexOf(uri)) != -1)
                        {
                            var attachment = related[index] as MimePart;

                            if (attachment == null)
                            {
                                htmlWriter.WriteAttribute(attribute);
                                continue;
                            }

                            var data = GetDataUri(attachment);

                            htmlWriter.WriteAttributeName(attribute.Name);
                            htmlWriter.WriteAttributeValue(data);
                        }
                        else
                        {
                            htmlWriter.WriteAttribute(attribute);
                        }
                    }
                }
            }
Пример #4
0
        public void TestArgumentExceptions()
        {
            var    related = new MultipartRelated();
            string mimeType, charset;

            Assert.Throws <ArgumentNullException> (() => new MultipartRelated((MimeEntityConstructorArgs)null));
            Assert.Throws <ArgumentNullException> (() => related.Open(null, out mimeType, out charset));
            Assert.Throws <ArgumentNullException> (() => related.Open(null));
            Assert.Throws <ArgumentNullException> (() => related.Contains((Uri)null));
            Assert.Throws <ArgumentNullException> (() => related.IndexOf((Uri)null));
            Assert.Throws <ArgumentNullException> (() => related.Accept(null));
            Assert.Throws <ArgumentNullException> (() => related.Root = null);

            Assert.Throws <FileNotFoundException> (() => related.Open(new Uri("http://www.xamarin.com/logo.png"), out mimeType, out charset));
            Assert.Throws <FileNotFoundException> (() => related.Open(new Uri("http://www.xamarin.com/logo.png")));
        }
        public override WebResourceResponse ShouldInterceptRequest(WebView view, IWebResourceRequest request)
        {
            var uri = new Uri(request.Url.ToString());
            int index;

            if ((index = related.IndexOf(uri)) != -1)
            {
                var part = related[index] as MimePart;

                if (part != null)
                {
                    var mimeType = part.ContentType.MimeType;
                    var stream   = part.ContentObject.Open();
                    var charset  = part.ContentType.Charset;

                    return(new WebResourceResponse(mimeType, charset, stream));
                }
            }

            return(base.ShouldInterceptRequest(view, request));
        }
Пример #6
0
        void RenderMultipartRelated(MultipartRelated related)
        {
            var root      = related.Root;
            var multipart = root as Multipart;
            var text      = root as TextPart;

            if (multipart != null)
            {
                // Note: the root document can sometimes be a multipart/alternative.
                // A multipart/alternative is just a collection of alternate views.
                // The last part is the format that most closely matches what the
                // user saw in his or her email client's WYSIWYG editor.
                for (int i = multipart.Count; i > 0; i--)
                {
                    var body = multipart[i - 1] as TextPart;

                    if (body == null)
                    {
                        continue;
                    }

                    // our preferred mime-type is text/html
                    if (body.ContentType.Matches("text", "html"))
                    {
                        text = body;
                        break;
                    }

                    if (text == null)
                    {
                        text = body;
                    }
                }
            }

            // check if we have a text/html document
            if (text != null && text.ContentType.Matches("text", "html"))
            {
                var      doc   = new HtmlAgilityPack.HtmlDocument();
                var      saved = new Dictionary <MimePart, string> ();
                TextPart html;

                doc.LoadHtml(text.Text);

                // find references to related MIME parts and replace them with links to links to the saved attachments
                foreach (var img in doc.DocumentNode.SelectNodes("//img[@src]"))
                {
                    var src = img.Attributes["src"];
                    int index;
                    Uri uri;

                    if (src == null || src.Value == null)
                    {
                        continue;
                    }

                    // parse the <img src=...> attribute value into a Uri
                    if (Uri.IsWellFormedUriString(src.Value, UriKind.Absolute))
                    {
                        uri = new Uri(src.Value, UriKind.Absolute);
                    }
                    else
                    {
                        uri = new Uri(src.Value, UriKind.Relative);
                    }

                    // locate the index of the attachment within the multipart/related (if it exists)
                    if ((index = related.IndexOf(uri)) != -1)
                    {
                        var attachment = related[index] as MimePart;

                        // make sure the referenced part is a MimePart (as opposed to another Multipart or MessagePart)
                        if (attachment != null)
                        {
                            string fileName;

                            // save the attachment (if we haven't already saved it)
                            if (!saved.TryGetValue(attachment, out fileName))
                            {
                                fileName = attachment.FileName;

                                if (string.IsNullOrEmpty(fileName))
                                {
                                    fileName = Guid.NewGuid().ToString();
                                }

                                using (var stream = File.Create(fileName))
                                    attachment.ContentObject.DecodeTo(stream);

                                saved.Add(attachment, fileName);
                            }

                            // replace the <img src=...> value with the local file name
                            src.Value = "file://" + Path.GetFullPath(fileName);
                        }
                    }
                }

                if (saved.Count > 0)
                {
                    // we had to make some modifications to the original html part, so create a new
                    // (temporary) text/html part to render
                    html = new TextPart("html");
                    using (var writer = new StringWriter()) {
                        doc.Save(writer);

                        html.Text = writer.GetStringBuilder().ToString();
                    }
                }
                else
                {
                    html = text;
                }

                RenderTextPart(html);
            }
            else
            {
                // we don't know what we have, so render it as an entity
                RenderEntity(related.Root);
            }
        }