예제 #1
0
 public void MimeTypeWithSuffix()
 {
     var original = "application/html+xml";
     var mt = new MimeType(original);
     Assert.AreEqual("application", mt.GeneralType);
     Assert.AreEqual("html", mt.MediaType);
     Assert.AreEqual("xml", mt.Suffix);
     Assert.AreEqual(0, mt.Keys.Count());
     Assert.AreEqual(original, mt.ToString());
 }
예제 #2
0
 public void MimeTypeWithInvalidFormat()
 {
     var original = "app;yo=there";
     var mt = new MimeType(original);
     Assert.AreEqual("app;yo=there", mt.GeneralType);
     Assert.AreEqual("", mt.MediaType);
     Assert.AreEqual("", mt.Suffix);
     Assert.AreEqual(0, mt.Keys.Count());
     Assert.AreEqual(original, mt.ToString());
 }
예제 #3
0
 public void MimeTypeWithSuffixAndParameter()
 {
     var original = "application/html+xml;foo=bar";
     var mt = new MimeType(original);
     Assert.AreEqual("application", mt.GeneralType);
     Assert.AreEqual("html", mt.MediaType);
     Assert.AreEqual("xml", mt.Suffix);
     Assert.AreEqual(1, mt.Keys.Count());
     Assert.AreEqual("foo", mt.Keys.First());
     Assert.AreEqual("bar", mt.GetParameter("foo"));
     Assert.AreEqual(original, mt.ToString());
 }
        /// <summary>
        /// Creates new document creation options. Selects the source from the
        /// response by potentially using the encoding from the configuration.
        /// </summary>
        /// <param name="response">The response to hand over.</param>
        /// <param name="configuration">The configuration to use.</param>
        public CreateDocumentOptions(IResponse response, IConfiguration configuration)
        {
            var contentType = response.GetContentType(MimeTypeNames.Html);
            var encoding = configuration.DefaultEncoding();
            var charset = contentType.GetParameter(AttributeNames.Charset);

            if (!String.IsNullOrEmpty(charset) && TextEncoding.IsSupported(charset))
                encoding = TextEncoding.Resolve(charset);

            _source = new TextSource(response.Content, encoding);
            _contentType = contentType;
            _response = response;
        }
예제 #5
0
 public void MimeTypeWithMultipleParameters()
 {
     var original = "application/html;foo=bar;cool=dude";
     var mt = new MimeType(original);
     Assert.AreEqual("application", mt.GeneralType);
     Assert.AreEqual("html", mt.MediaType);
     Assert.AreEqual("", mt.Suffix);
     Assert.AreEqual(2, mt.Keys.Count());
     Assert.AreEqual("foo", mt.Keys.First());
     Assert.AreEqual("cool", mt.Keys.Last());
     Assert.AreEqual("bar", mt.GetParameter("foo"));
     Assert.AreEqual("dude", mt.GetParameter("cool"));
     Assert.AreEqual(original, mt.ToString());
 }
예제 #6
0
        /// <summary>
        /// Creates a new set of document options from the given response with
        /// the provided configuration.
        /// </summary>
        /// <param name="response">The response to pass on.</param>
        /// <param name="configuration">The configuration to use.</param>
        /// <param name="ancestor">The optional import ancestor.</param>
        public CreateDocumentOptions(IResponse response, IConfiguration configuration, IDocument ancestor = null)
        {
            var contentType = response.GetContentType(MimeTypeNames.Html);
            var charset = contentType.GetParameter(AttributeNames.Charset);
            var source = new TextSource(response.Content, configuration.DefaultEncoding());

            if (!String.IsNullOrEmpty(charset) && TextEncoding.IsSupported(charset))
            {
                source.CurrentEncoding = TextEncoding.Resolve(charset);
            }

            _source = source;
            _contentType = contentType;
            _response = response;
            _ancestor = ancestor;
        }
예제 #7
0
        /// <summary>
        /// Loads the document in the provided context from the given response.
        /// </summary>
        /// <param name="context">The browsing context.</param>
        /// <param name="response">The response to consider.</param>
        /// <param name="contentType">The content type of the response.</param>
        /// <param name="source">The source to use.</param>
        /// <param name="cancelToken">Token for cancellation.</param>
        /// <returns>The task that builds the document.</returns>
        internal async static Task<XmlDocument> LoadAsync(IBrowsingContext context, IResponse response, MimeType contentType, TextSource source, CancellationToken cancelToken)
        {
            var document = new XmlDocument(context, source);
            var evt = new HtmlParseStartEvent(document);
            var events = context.Configuration.Events;
            var parser = new XmlDomBuilder(document);
            document.ContentType = contentType.Content;
            document.Referrer = response.Headers.GetOrDefault(HeaderNames.Referer, String.Empty);
            document.DocumentUri = response.Address.Href;
            document.Cookie = response.Headers.GetOrDefault(HeaderNames.SetCookie, String.Empty);
            document.ReadyState = DocumentReadyState.Loading;
            context.NavigateTo(document);

            if (events != null)
                events.Publish(evt);

            await parser.ParseAsync(default(XmlParserOptions), cancelToken).ConfigureAwait(false);
            evt.FireEnd();
            return document;
        }
예제 #8
0
 /// <summary>
 /// Checks if the given mime-type is equivalent to the provided string
 /// representation.
 /// </summary>
 /// <param name="type">The type to check for.</param>
 /// <param name="content">THe string representation.</param>
 /// <returns>
 /// True if both (type and representation) are equivalent, else false.
 /// </returns>
 public static Boolean Represents(this MimeType type, String content)
 {
     var other = new MimeType(content);
     return type.Equals(other);
 }
 /// <summary>
 /// Creates new document creation options.
 /// </summary>
 /// <param name="response">The response to hand over.</param>
 /// <param name="contentType">The content mime-type.</param>
 /// <param name="source">The source of the document.</param>
 public CreateDocumentOptions(IResponse response, MimeType contentType, TextSource source)
 {
     _response = response;
     _contentType = contentType;
     _source = source;
 }
예제 #10
0
        /// <summary>
        /// Checks if the given mime-type is equivalent to the provided string
        /// representation.
        /// </summary>
        /// <param name="type">The type to check for.</param>
        /// <param name="content">THe string representation.</param>
        /// <returns>
        /// True if both (type and representation) are equivalent, else false.
        /// </returns>
        public static Boolean Represents(this MimeType type, String content)
        {
            var other = new MimeType(content);

            return(type.Equals(other));
        }
예제 #11
0
 public void MimeTypeWithSingleParameter()
 {
     var original = "text/html;yo=there";
     var mt = new MimeType(original);
     Assert.AreEqual("text", mt.GeneralType);
     Assert.AreEqual("html", mt.MediaType);
     Assert.AreEqual("", mt.Suffix);
     Assert.AreEqual(1, mt.Keys.Count());
     Assert.AreEqual("yo", mt.Keys.First());
     Assert.AreEqual("there", mt.GetParameter("yo"));
     Assert.AreEqual(original, mt.ToString());
 }
예제 #12
0
        static async Task<IDocument> LoadDocumentAsync(this IBrowsingContext context, IResponse response, MimeType contentType, TextSource source, CancellationToken cancel)
        {
            if (contentType.Represents(MimeTypes.Xml) || contentType.Represents(MimeTypes.ApplicationXml))
                return await XmlDocument.LoadAsync(context, response, contentType, source, cancel).ConfigureAwait(false);
            else if (contentType.Represents(MimeTypes.Svg))
                return await SvgDocument.LoadAsync(context, response, contentType, source, cancel).ConfigureAwait(false);

            return await HtmlDocument.LoadAsync(context, response, contentType, source, cancel).ConfigureAwait(false);
        }