public override void Parse(ExtendedBinaryReader br) { base.Parse(br); //Because the box size is not a reliable metric. So useful. Payload = br.ReadToEnd(); }
public override void Parse(ExtendedBinaryReader br) { base.Parse(br); if (BoxHeader.TotalSize == 0) br.ReadToEnd(); else if (BoxHeader.TotalSize == 1) { br.ReadChunkedBytes(BoxHeader.ExtendedSize.Value - (ulong)BoxHeader.HeaderSize).Select(s => s); } else { foreach (var s in br.ReadChunkedBytes((ulong)BoxHeader.TotalSize - (ulong)BoxHeader.HeaderSize)) ; } }
/// <summary> /// returns a collection of bytes from a Url /// </summary> /// <param name="url">The URL to retrieve</param> public void GetUrlData(string url) { Uri uri = new Uri(url); if (!uri.IsFile) { throw new UriFormatException("url is not a local file"); } FileWebRequest request = WebRequest.Create(url) as FileWebRequest; if (request == null) { this.Clear(); return; } request.Method = "GET"; // download the target URL FileWebResponse response = (FileWebResponse)request.GetResponse(); // convert response stream to byte array using (Stream stream = response.GetResponseStream()) { ExtendedBinaryReader extReader = new ExtendedBinaryReader(stream); _ResponseBytes = extReader.ReadToEnd(); } // For local operations, we consider the data are never compressed. Else, the "Content-Encoding" field // in the headers would be "gzip" or "deflate". This could be handled quite easily with SharpZipLib for instance. // sometimes URL is indeterminate, eg, "http://website.com/myfolder" // in that case the folder and file resolution MUST be done on // the server, and returned to the client as ContentLocation _ContentLocation = response.Headers["Content-Location"]; if (_ContentLocation == null) { _ContentLocation = ""; } // if we have string content, determine encoding type // (must cast to prevent null) // HACK We determine the content type based on the uri extension, // as the header returned by the FileWebResponse is always "application/octet-stream" (hard coded in .NET!!) // text/html string ext = Path.GetExtension(uri.LocalPath).TrimStart(new char[] { '.' }); switch (ext) { // What's important here is to identify TEXT mime types. Because, the default will resort to binary file. case "htm": case "html": _DetectedContentType = "text/html"; break; case "css": _DetectedContentType = "text/css"; break; case "csv": _DetectedContentType = "text/csv"; break; case "rtf": _DetectedContentType = "text/rtf"; break; case "aspx": case "xsl": case "xml": _DetectedContentType = "text/xml"; break; case "bmp": _DetectedContentType = "image/bmp"; break; case "gif": _DetectedContentType = "image/gif"; break; case "ico": _DetectedContentType = "image/x-icon"; break; case "jpg": case "jpeg": _DetectedContentType = "image/jpeg"; break; case "png": _DetectedContentType = "image/png"; break; case "tif": case "tiff": _DetectedContentType = "image/tiff"; break; case "js": _DetectedContentType = "application/x-javascript"; break; default: // Line commented: we don't change it _DetectedContentType = response.Headers["Content-Type"]; // Always "application/octet-stream" ... break; } if (_DetectedContentType == null) { _DetectedContentType = ""; } if (ResponseIsBinary) { _DetectedEncoding = null; } else if (_ForcedEncoding == null) { _DetectedEncoding = DetectEncoding(_DetectedContentType, _ResponseBytes); } }
/// <summary> /// returns a collection of bytes from a Url /// </summary> /// <param name="url">The URL to retrieve</param> public void GetUrlData(string url) { Uri uri = new Uri(url); if (!uri.IsFile) throw new UriFormatException("url is not a local file"); FileWebRequest request = WebRequest.Create(url) as FileWebRequest; if (request == null) { this.Clear(); return; } request.Method = "GET"; // download the target URL FileWebResponse response = (FileWebResponse) request.GetResponse(); // convert response stream to byte array using (Stream stream = response.GetResponseStream()) { ExtendedBinaryReader extReader = new ExtendedBinaryReader(stream); _ResponseBytes = extReader.ReadToEnd(); } // For local operations, we consider the data are never compressed. Else, the "Content-Encoding" field // in the headers would be "gzip" or "deflate". This could be handled quite easily with SharpZipLib for instance. // sometimes URL is indeterminate, eg, "http://website.com/myfolder" // in that case the folder and file resolution MUST be done on // the server, and returned to the client as ContentLocation _ContentLocation = response.Headers["Content-Location"]; if (_ContentLocation == null) _ContentLocation = ""; // if we have string content, determine encoding type // (must cast to prevent null) // HACK We determine the content type based on the uri extension, // as the header returned by the FileWebResponse is always "application/octet-stream" (hard coded in .NET!!) // text/html string ext = Path.GetExtension(uri.LocalPath).TrimStart(new char[]{'.'}); switch (ext) { // What's important here is to identify TEXT mime types. Because, the default will resort to binary file. case "htm": case "html": _DetectedContentType = "text/html"; break; case "css": _DetectedContentType = "text/css"; break; case "csv": _DetectedContentType = "text/csv"; break; case "rtf": _DetectedContentType = "text/rtf"; break; case "aspx": case "xsl": case "xml": _DetectedContentType = "text/xml"; break; case "bmp": _DetectedContentType = "image/bmp"; break; case "gif": _DetectedContentType = "image/gif"; break; case "ico": _DetectedContentType = "image/x-icon"; break; case "jpg": case "jpeg": _DetectedContentType = "image/jpeg"; break; case "png": _DetectedContentType = "image/png"; break; case "tif": case "tiff": _DetectedContentType = "image/tiff"; break; case "js": _DetectedContentType = "application/x-javascript"; break; default: // Line commented: we don't change it _DetectedContentType = response.Headers["Content-Type"]; // Always "application/octet-stream" ... break; } if (_DetectedContentType == null) _DetectedContentType = ""; if (ResponseIsBinary) _DetectedEncoding = null; else if (_ForcedEncoding == null) _DetectedEncoding = DetectEncoding(_DetectedContentType, _ResponseBytes); }
/// <summary> /// returns a collection of bytes from a Url /// </summary> /// <param name="Url">URL to retrieve</param> public void GetUrlData(string Url, DateTime ifModifiedSince) { if (!string.IsNullOrEmpty(Url)) { HttpWebRequest wreq = (HttpWebRequest)WebRequest.Create(Url); //-- do we need to use a proxy to get to the web? if (!string.IsNullOrEmpty(_ProxyUrl)) { WebProxy wp = new WebProxy(_ProxyUrl); if (_ProxyAuthenticationRequired) { if (!string.IsNullOrEmpty(_ProxyUser) & !string.IsNullOrEmpty(_ProxyPassword)) { wp.Credentials = new NetworkCredential(_ProxyUser, _ProxyPassword); } else { wp.Credentials = CredentialCache.DefaultCredentials; } wreq.Proxy = wp; } } //-- does the target website require credentials? if (_AuthenticationRequired) { if (!string.IsNullOrEmpty(_AuthenticationUser) & !string.IsNullOrEmpty(_AuthenticationPassword)) { wreq.Credentials = new NetworkCredential(_AuthenticationUser, _AuthenticationPassword); } else { wreq.Credentials = CredentialCache.DefaultCredentials; } } wreq.Method = "GET"; wreq.Timeout = _RequestTimeoutMilliseconds; wreq.UserAgent = _HttpUserAgent; wreq.Headers.Add("Accept-Encoding", _AcceptedEncodings); //-- note that, if present, this will trigger a 304 exception //-- if the URL being retrieved is not newer than the specified //-- date/time if (ifModifiedSince != DateTime.MinValue) { wreq.IfModifiedSince = ifModifiedSince; } //-- sometimes we need to transfer cookies to another URL; //-- this keeps them around in the object if (KeepCookies) { if (_PersistedCookies == null) { _PersistedCookies = new CookieContainer(); } wreq.CookieContainer = _PersistedCookies; } //-- download the target URL into a byte array HttpWebResponse wresp = (HttpWebResponse)wreq.GetResponse(); //-- convert response stream to byte array ExtendedBinaryReader ebr = new ExtendedBinaryReader(wresp.GetResponseStream()); _ResponseBytes = ebr.ReadToEnd(); //-- determine if body bytes are compressed, and if so, //-- decompress the bytes HttpContentEncoding ContentEncoding = default(HttpContentEncoding); if (wresp.Headers["Content-Encoding"] == null) { ContentEncoding = HttpContentEncoding.None; } else { switch (wresp.Headers["Content-Encoding"].ToLower()) { case "gzip": ContentEncoding = HttpContentEncoding.Gzip; break; case "deflate": ContentEncoding = HttpContentEncoding.Deflate; break; default: ContentEncoding = HttpContentEncoding.Unknown; break; } _ResponseBytes = Decompress(_ResponseBytes, ContentEncoding); } //-- sometimes URL is indeterminate, eg, "http://website.com/myfolder" //-- in that case the folder and file resolution MUST be done on //-- the server, and returned to the client as ContentLocation _ContentLocation = wresp.Headers["Content-Location"]; if (_ContentLocation == null) { _ContentLocation = ""; } //-- if we have string content, determine encoding type //-- (must cast to prevent Nothing) _DetectedContentType = wresp.Headers["Content-Type"]; if (_DetectedContentType == null) { _DetectedContentType = ""; } if (ResponseIsBinary) { _DetectedEncoding = null; } else { if (_ForcedEncoding == null) { _DetectedEncoding = DetectEncoding(_DetectedContentType, _ResponseBytes); } } } }
/// <summary> /// returns a collection of bytes from a Url /// </summary> /// <param name="Url">URL to retrieve</param> public void GetUrlData(string Url, DateTime ifModifiedSince) { if (!string.IsNullOrEmpty(Url)) { HttpWebRequest wreq = (HttpWebRequest)WebRequest.Create(Url); //-- do we need to use a proxy to get to the web? if (!string.IsNullOrEmpty(_ProxyUrl)) { WebProxy wp = new WebProxy(_ProxyUrl); if (_ProxyAuthenticationRequired) { if (!string.IsNullOrEmpty(_ProxyUser) & !string.IsNullOrEmpty(_ProxyPassword)) { wp.Credentials = new NetworkCredential(_ProxyUser, _ProxyPassword); } else { wp.Credentials = CredentialCache.DefaultCredentials; } wreq.Proxy = wp; } } //-- does the target website require credentials? if (_AuthenticationRequired) { if (!string.IsNullOrEmpty(_AuthenticationUser) & !string.IsNullOrEmpty(_AuthenticationPassword)) { wreq.Credentials = new NetworkCredential(_AuthenticationUser, _AuthenticationPassword); } else { wreq.Credentials = CredentialCache.DefaultCredentials; } } wreq.Method = "GET"; wreq.Timeout = _RequestTimeoutMilliseconds; wreq.UserAgent = _HttpUserAgent; wreq.Headers.Add("Accept-Encoding", _AcceptedEncodings); //-- note that, if present, this will trigger a 304 exception //-- if the URL being retrieved is not newer than the specified //-- date/time if (ifModifiedSince != DateTime.MinValue) { wreq.IfModifiedSince = ifModifiedSince; } //-- sometimes we need to transfer cookies to another URL; //-- this keeps them around in the object if (KeepCookies) { if (_PersistedCookies == null) { _PersistedCookies = new CookieContainer(); } wreq.CookieContainer = _PersistedCookies; } //-- download the target URL into a byte array HttpWebResponse wresp = (HttpWebResponse)wreq.GetResponse(); //-- convert response stream to byte array ExtendedBinaryReader ebr = new ExtendedBinaryReader(wresp.GetResponseStream()); _ResponseBytes = ebr.ReadToEnd(); //-- determine if body bytes are compressed, and if so, //-- decompress the bytes HttpContentEncoding ContentEncoding = default(HttpContentEncoding); if (wresp.Headers["Content-Encoding"] == null) { ContentEncoding = HttpContentEncoding.None; } else { switch (wresp.Headers["Content-Encoding"].ToLower()) { case "gzip": ContentEncoding = HttpContentEncoding.Gzip; break; case "deflate": ContentEncoding = HttpContentEncoding.Deflate; break; default: ContentEncoding = HttpContentEncoding.Unknown; break; } _ResponseBytes = Decompress(_ResponseBytes, ContentEncoding); } //-- sometimes URL is indeterminate, eg, "http://website.com/myfolder" //-- in that case the folder and file resolution MUST be done on //-- the server, and returned to the client as ContentLocation _ContentLocation = wresp.Headers["Content-Location"]; if (_ContentLocation == null) { _ContentLocation = ""; } //-- if we have string content, determine encoding type //-- (must cast to prevent Nothing) _DetectedContentType = wresp.Headers["Content-Type"]; if (_DetectedContentType == null) { _DetectedContentType = ""; } if (ResponseIsBinary) { _DetectedEncoding = null; } else { if (_ForcedEncoding == null) { _DetectedEncoding = DetectEncoding(_DetectedContentType, _ResponseBytes); } } } }