public Entity(string name, string pubid, string uri, string proxy) { Name = name; PublicId = pubid; Uri = uri; Proxy = proxy; Html = (name != null && StringUtilities.EqualsIgnoreCase(name, "html")); }
public void Open(Entity parent, Uri baseUri) { Parent = parent; if (parent != null) { this.Html = parent.Html; } this.Line = 1; if (Internal) { if (this.Literal != null) { this.stm = new StringReader(this.Literal); } } else if (this.Uri == null) { this.Error("Unresolvable entity '{0}'", this.Name); } else { if (baseUri != null) { this.resolvedUri = new Uri(baseUri, this.Uri); } else { this.resolvedUri = new Uri(this.Uri); } Stream stream = null; Encoding e = Encoding.Default; switch (this.resolvedUri.Scheme) { case "file": { string path = this.resolvedUri.LocalPath; stream = new FileStream(path, FileMode.Open, FileAccess.Read); } break; default: HttpWebRequest wr = (HttpWebRequest)WebRequest.Create(ResolvedUri); wr.UserAgent = "Mozilla/4.0 (compatible;);"; wr.Timeout = 10000; if (Proxy != null) { wr.Proxy = new WebProxy(Proxy); } wr.PreAuthenticate = false; wr.Credentials = CredentialCache.DefaultCredentials; WebResponse resp = wr.GetResponse(); Uri actual = resp.ResponseUri; if (actual.AbsoluteUri != this.resolvedUri.AbsoluteUri) { this.resolvedUri = actual; } string contentType = resp.ContentType.ToLower(); string mimeType = contentType; int i = contentType.IndexOf(';'); if (i >= 0) { mimeType = contentType.Substring(0, i); } if (StringUtilities.EqualsIgnoreCase(mimeType, "text/html")) { this.Html = true; } i = contentType.IndexOf("charset"); e = Encoding.Default; if (i >= 0) { int j = contentType.IndexOf("=", i); int k = contentType.IndexOf(";", j); if (k < 0) { k = contentType.Length; } if (j > 0) { j++; string charset = contentType.Substring(j, k - j).Trim(); try { e = Encoding.GetEncoding(charset); } catch (Exception) { } } } stream = resp.GetResponseStream(); break; } this.weOwnTheStream = true; HtmlStream html = new HtmlStream(stream, e); this.encoding = html.Encoding; this.stm = html; } }
internal Decoder SniffMeta() { int i = ReadChar(); while (i != EOF) { char ch = (char)i; if (ch == '<') { string name = SniffName(); if (name != null && StringUtilities.EqualsIgnoreCase(name, "meta")) { string httpequiv = null; string content = null; while (true) { string value = SniffAttribute(out name); if (name == null) { break; } if (StringUtilities.EqualsIgnoreCase(name, "http-equiv")) { httpequiv = value; } else if (StringUtilities.EqualsIgnoreCase(name, "content")) { content = value; } } if (httpequiv != null && StringUtilities.EqualsIgnoreCase(httpequiv, "content-type") && content != null) { int j = content.IndexOf("charset"); if (j >= 0) { j = content.IndexOf("=", j); if (j >= 0) { j++; int k = content.IndexOf(";", j); if (k < 0) { k = content.Length; } string charset = content.Substring(j, k - j).Trim(); try { Encoding e = Encoding.GetEncoding(charset); this.encoding = e; return(e.GetDecoder()); } catch { } } } } } } i = ReadChar(); } return(null); }