コード例 #1
0
ファイル: Entity.cs プロジェクト: zellus/coapp.powershell
        /// <summary>
        ///     Begins processing an entity.
        /// </summary>
        /// <param name="parent">The parent of this entity.</param>
        /// <param name="baseUri">The base Uri for processing this entity within.</param>
        public void Open(Entity parent, Uri baseUri)
        {
            this.m_parent = parent;
            if (parent != null)
            {
                this.m_isHtml = parent.IsHtml;
            }
            this.m_line = 1;
            if (m_isInternal)
            {
                if (this.m_literal != null)
                {
                    this.m_stm = new StringReader(this.m_literal);
                }
            }
            else if (this.m_uri == null)
            {
                this.Error("Unresolvable entity '{0}'", this.m_name);
            }
            else
            {
                if (baseUri != null)
                {
                    this.m_resolvedUri = new Uri(baseUri, this.m_uri);
                }
                else
                {
                    this.m_resolvedUri = new Uri(this.m_uri);
                }

                Stream stream = null;
                var    e      = Encoding.Default;
                switch (this.m_resolvedUri.Scheme)
                {
                case "file": {
                    var path = this.m_resolvedUri.LocalPath;
                    stream = new FileStream(path, FileMode.Open, FileAccess.Read);
                }
                break;

                default:
                    Console.WriteLine("Fetching:" + ResolvedUri.AbsoluteUri);
                    var wr = (HttpWebRequest)WebRequest.Create(ResolvedUri);
                    wr.UserAgent = "Mozilla/4.0 (compatible;);";
                    wr.Timeout   = 10000;   // in case this is running in an ASPX page.
                    if (m_proxy != null)
                    {
                        wr.Proxy = new WebProxy(m_proxy);
                    }
                    wr.PreAuthenticate = false;
                    // Pass the credentials of the process.
                    wr.Credentials = CredentialCache.DefaultCredentials;

                    var resp   = wr.GetResponse();
                    var actual = resp.ResponseUri;
                    if (!string.Equals(actual.AbsoluteUri, this.m_resolvedUri.AbsoluteUri, StringComparison.OrdinalIgnoreCase))
                    {
                        this.m_resolvedUri = actual;
                    }
                    var contentType = resp.ContentType.ToLowerInvariant();
                    var mimeType    = contentType;
                    var i           = contentType.IndexOf(';');
                    if (i >= 0)
                    {
                        mimeType = contentType.Substring(0, i);
                    }

                    if (StringUtilities.EqualsIgnoreCase(mimeType, "text/html"))
                    {
                        this.m_isHtml = true;
                    }

                    i = contentType.IndexOf("charset");
                    e = Encoding.Default;
                    if (i >= 0)
                    {
                        var j = contentType.IndexOf("=", i);
                        var k = contentType.IndexOf(";", j);
                        if (k < 0)
                        {
                            k = contentType.Length;
                        }

                        if (j > 0)
                        {
                            j++;
                            var charset = contentType.Substring(j, k - j).Trim();
                            try {
                                e = Encoding.GetEncoding(charset);
                            } catch (ArgumentException) {
                            }
                        }
                    }

                    stream = resp.GetResponseStream();
                    break;
                }

                this.m_weOwnTheStream = true;
                var html = new HtmlStream(stream, e);
                this.m_encoding = html.Encoding;
                this.m_stm      = html;
            }
        }
コード例 #2
0
        /// <summary>
        ///   Begins processing an entity.
        /// </summary>
        /// <param name = "parent">The parent of this entity.</param>
        /// <param name = "baseUri">The base Uri for processing this entity within.</param>
        public void Open(Entity parent, Uri baseUri) {
            this.m_parent = parent;
            if(parent != null) {
                this.m_isHtml = parent.IsHtml;
            }
            this.m_line = 1;
            if(m_isInternal) {
                if(this.m_literal != null) {
                    this.m_stm = new StringReader(this.m_literal);
                }
            }
            else if(this.m_uri == null) {
                this.Error("Unresolvable entity '{0}'", this.m_name);
            }
            else {
                if(baseUri != null) {
                    this.m_resolvedUri = new Uri(baseUri, this.m_uri);
                }
                else {
                    this.m_resolvedUri = new Uri(this.m_uri);
                }

                Stream stream = null;
                var e = Encoding.Default;
                switch(this.m_resolvedUri.Scheme) {
                    case "file": {
                        var path = this.m_resolvedUri.LocalPath;
                        stream = new FileStream(path, FileMode.Open, FileAccess.Read);
                    }
                        break;
                    default:
                        Console.WriteLine("Fetching:" + ResolvedUri.AbsoluteUri);
                        var wr = (HttpWebRequest) WebRequest.Create(ResolvedUri);
                        wr.UserAgent = "Mozilla/4.0 (compatible;);";
                        wr.Timeout = 10000; // in case this is running in an ASPX page.
                        if(m_proxy != null) {
                            wr.Proxy = new WebProxy(m_proxy);
                        }
                        wr.PreAuthenticate = false;
                        // Pass the credentials of the process. 
                        wr.Credentials = CredentialCache.DefaultCredentials;

                        var resp = wr.GetResponse();
                        var actual = resp.ResponseUri;
                        if(!string.Equals(actual.AbsoluteUri, this.m_resolvedUri.AbsoluteUri, StringComparison.OrdinalIgnoreCase)) {
                            this.m_resolvedUri = actual;
                        }
                        var contentType = resp.ContentType.ToLowerInvariant();
                        var mimeType = contentType;
                        var i = contentType.IndexOf(';');
                        if(i >= 0) {
                            mimeType = contentType.Substring(0, i);
                        }

                        if(StringUtilities.EqualsIgnoreCase(mimeType, "text/html")) {
                            this.m_isHtml = true;
                        }

                        i = contentType.IndexOf("charset");
                        e = Encoding.Default;
                        if(i >= 0) {
                            var j = contentType.IndexOf("=", i);
                            var k = contentType.IndexOf(";", j);
                            if(k < 0) {
                                k = contentType.Length;
                            }

                            if(j > 0) {
                                j++;
                                var charset = contentType.Substring(j, k - j).Trim();
                                try {
                                    e = Encoding.GetEncoding(charset);
                                }
                                catch(ArgumentException) {
                                }
                            }
                        }

                        stream = resp.GetResponseStream();
                        break;
                }

                this.m_weOwnTheStream = true;
                var html = new HtmlStream(stream, e);
                this.m_encoding = html.Encoding;
                this.m_stm = html;
            }
        }