public static HttpContentStream Create(Stream rawStream, HttpContentEncoding encoding, long?contentLength) { switch (encoding) { case HttpContentEncoding.Identity: return(new HttpContentStreamIdentity(rawStream, contentLength)); case HttpContentEncoding.Gzip: return(new HttpContentStreamGzip(rawStream, contentLength)); default: throw new NotImplementedException($"Transfer encoding {encoding} is not implemented."); } }
/// <summary> /// decompresses a compressed array of bytes via the specified HTTP compression type /// </summary> /// <returns>decompressed array of bytes</returns> private byte[] Decompress(byte[] b, HttpContentEncoding CompressionType) { Stream s = default(Stream); switch (CompressionType) { case HttpContentEncoding.Deflate: s = new InflaterInputStream(new MemoryStream(b), new ICSharpCode.SharpZipLib.Zip.Compression.Inflater(true)); break; case HttpContentEncoding.Gzip: s = new GZipInputStream(new MemoryStream(b)); break; default: return(b); } MemoryStream ms = new MemoryStream(); int sizeRead = 0; const int chunkSize = 2048; byte[] unzipBytes = new byte[chunkSize + 1]; while (true) { sizeRead = s.Read(unzipBytes, 0, chunkSize); if (sizeRead > 0) { ms.Write(unzipBytes, 0, sizeRead); } else { break; // TODO: might not be correct. Was : Exit While } } s.Close(); return(ms.ToArray()); }
/// <summary> /// decompresses a compressed array of bytes via the specified HTTP compression type /// </summary> /// <returns>decompressed array of bytes</returns> private byte[] Decompress(byte[] b, HttpContentEncoding CompressionType) { Stream s = default(Stream); switch (CompressionType) { case HttpContentEncoding.Deflate: s = new InflaterInputStream(new MemoryStream(b), new ICSharpCode.SharpZipLib.Zip.Compression.Inflater(true)); break; case HttpContentEncoding.Gzip: s = new GZipInputStream(new MemoryStream(b)); break; default: return b; } MemoryStream ms = new MemoryStream(); int sizeRead = 0; const int chunkSize = 2048; byte[] unzipBytes = new byte[chunkSize + 1]; while (true) { sizeRead = s.Read(unzipBytes, 0, chunkSize); if (sizeRead > 0) { ms.Write(unzipBytes, 0, sizeRead); } else { break; // TODO: might not be correct. Was : Exit While } } s.Close(); return ms.ToArray(); }
/// <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); } } } }
private async Task <HttpStatusCode> PostEvents( byte[] serializedEvents, CancellationToken cancellationToken) { // encode data HttpResponseMessage response = null; string serverReply = null; HttpStatusCode responseCode = HttpStatusCode.OK; try { // post data HttpEventCollectorHandler postEvents = (t, s) => { HttpContent content = new ByteArrayContent(serializedEvents); content.Headers.ContentType = HttpContentHeaderValue; if (this.applyHttpVersion10Hack) { HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post, HttpEventCollectorPath); request.Version = HttpVersion.Version10; request.Content = content; return(httpClient.SendAsync(request)); } return(httpClient.PostAsync(httpEventCollectorEndpointUri, content)); }; response = await postEvents(token, serializedEvents); responseCode = response.StatusCode; if (responseCode != HttpStatusCode.OK && response.Content != null) { // record server reply serverReply = await response.Content.ReadAsStringAsync(); OnError(new HttpEventCollectorException( code: responseCode, webException: null, reply: serverReply, response: response, serializedEvents: HttpContentEncoding.GetString(serializedEvents) )); } } catch (HttpEventCollectorException e) { responseCode = responseCode == HttpStatusCode.OK ? (e.Response?.StatusCode ?? e.StatusCode) : responseCode; e.SerializedEvents = e.SerializedEvents ?? HttpContentEncoding.GetString(serializedEvents); OnError(e); } catch (Exception e) { responseCode = responseCode == HttpStatusCode.OK ? HttpStatusCode.BadRequest : responseCode; OnError(new HttpEventCollectorException( code: responseCode, webException: e, reply: serverReply, response: response, serializedEvents: HttpContentEncoding.GetString(serializedEvents) )); } return(responseCode); }