Ejemplo n.º 1
0
        /// <summary>
        /// Get a stream as the result of the specified stream being compressed using the specified method. Optionally specify a length
        /// for the number of bytes to read, and dispose of the original stream. The decompressed stream is returned using the specified
        /// callback method.
        /// </summary>
        public static void Compress(IAction <MemoryStream> callback, System.Net.DecompressionMethods method,
                                    CompressionLevel level, Stream source, int length = -1, bool dispose = true)
        {
            // create a new memory stream to receive the compressed stream
            MemoryStream memStream = new MemoryStream();

            // set the callback parameter
            callback.ArgA = memStream;

            // resolve the compression stream
            Stream compressionStream;

            switch (method)
            {
            case System.Net.DecompressionMethods.Deflate:
                compressionStream = new DeflateStream(memStream, level, true);
                break;

            case System.Net.DecompressionMethods.GZip:
                compressionStream = new GZipStream(memStream, level, true);
                break;

            default:
                // no compression - copy unchanged
                source.CopyTo(memStream);
                // run the callback
                callback.Run();
                return;
            }

            // add a compress task
            ManagerUpdate.Control.AddSingle(ActionSet.New(Compress, callback, memStream, compressionStream, source, length, dispose));
        }
Ejemplo n.º 2
0
        //----------------------------------//

        /// <summary>
        /// Initialize a web resource.
        /// </summary>
        public WebResource(HttpSite site, string path, string mime = null)
        {
            _lock = new LockShared();

            // persist the local path
            Path = path;

            // does the path indicate a web resource?
            if (Fs.IsWebPath(Path))
            {
                // persist the path
                FullPath = Path;
            }
            else
            {
                // persist the path
                FullPath = Fs.Combine(site.Path, Path);
            }

            _compression = System.Net.DecompressionMethods.None;

            // get the web resource extension if set
            MimeType = mime ?? Mime.GetType(Fs.GetExtension(FullPath));

            // the resource must be loaded to begin with
            _reset = true;
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Get a stream as the result of the specified stream being compressed using the specified method. Optionally specify a length
        /// for the number of bytes to read.
        /// </summary>
        public static MemoryStream Compress(System.Net.DecompressionMethods method, CompressionLevel level, Stream source, int length = -1)
        {
            // create a new memory stream to receive the compressed stream
            MemoryStream memStream = new MemoryStream(Global.BufferSizeLocal);

            // resolve the compression stream
            Stream compressionStream;

            switch (method)
            {
            case System.Net.DecompressionMethods.Deflate:
                compressionStream = new GZipStream(memStream, level, true);
                break;

            case System.Net.DecompressionMethods.GZip:
                compressionStream = new DeflateStream(memStream, level, true);
                break;

            default:
                // no compression - return uncompressed
                source.CopyTo(memStream);
                return(memStream);
            }

            // was the length specifed?
            if (length == -1)
            {
                // no, write the buffer to the compression
                source.CopyTo(compressionStream, Global.BufferSizeLocal);
            }
            else
            {
                // yes, create a buffer for the copy
                byte[] buffer = BufferCache.Get();
                int    count  = Global.BufferSizeLocal;

                // while the buffer is filled by reading from the stream
                while (count == Global.BufferSizeLocal)
                {
                    // read from the stream
                    count = length < Global.BufferSizeLocal ?
                            source.Read(buffer, 0, length) :
                            source.Read(buffer, 0, Global.BufferSizeLocal);

                    // write to the compression stream
                    compressionStream.Write(buffer, 0, count);
                    // decrement the remaining bytes
                    length -= count;
                }

                BufferCache.Set(buffer);
            }

            // close the compression stream
            compressionStream.Close();

            // return the memory stream
            return(memStream);
        }
Ejemplo n.º 4
0
 /// <summary>
 /// Replace the web resource stream. Lock should be taken before this method is called.
 /// </summary>
 public void ReplaceStreamLocked(Stream stream, System.Net.DecompressionMethods compression)
 {
     if (_stream != null)
     {
         _stream.Close();
         _stream = null;
     }
     _stream      = stream;
     _compression = compression;
     _reset       = true;
 }
Ejemplo n.º 5
0
 /// <summary>
 /// Replace the web resource stream.
 /// </summary>
 public void ReplaceStream(Stream stream, System.Net.DecompressionMethods compression)
 {
     _lock.Take();
     if (_stream != null)
     {
         _stream.Close();
         _stream = null;
     }
     _stream      = stream;
     _compression = compression;
     _reset       = true;
     _lock.Release();
 }
Ejemplo n.º 6
0
        /// <summary>
        /// Compress the specified byte buffer.
        /// </summary>
        public static Stream Decompress(System.Net.DecompressionMethods method, Stream source, ref int count)
        {
            // create a new memory stream to receive the compressed stream
            MemoryStream memStream = new MemoryStream();

            // resolve the compression stream
            Stream compressionStream;

            switch (method)
            {
            case System.Net.DecompressionMethods.Deflate:
                compressionStream = new DeflateStream(memStream, CompressionMode.Decompress, true);
                break;

            case System.Net.DecompressionMethods.GZip:
                compressionStream = new GZipStream(memStream, CompressionMode.Decompress, true);
                break;

            default:
                // no compression - return uncompressed
                return(source);
            }

            // write the buffer to the compression
            var buffer      = BufferCache.Get();
            int bufferCount = Global.BufferSizeLocal;

            while (bufferCount == Global.BufferSizeLocal)
            {
                bufferCount = source.Read(buffer, 0, count > Global.BufferSizeLocal ? Global.BufferSizeLocal : count);
                count      -= bufferCount;
                compressionStream.Write(buffer, 0, bufferCount);
            }

            // pass the buffer back to the cache
            BufferCache.Set(buffer);

            // close the stream
            compressionStream.Dispose();

            // get the count
            memStream.Position = 0L;
            count = (int)memStream.Length;

            // get the bytes from the memory stream
            return(memStream);
        }
Ejemplo n.º 7
0
        /// <summary>
        /// Compress the specified byte buffer.
        /// </summary>
        public static byte[] Decompress(System.Net.DecompressionMethods method, byte[] buffer, int offset, ref int count)
        {
            // create a new memory stream to receive the compressed stream
            using (MemoryStream memStream = new MemoryStream()) {
                // resolve the compression stream
                Stream compressionStream;
                switch (method)
                {
                case System.Net.DecompressionMethods.Deflate:
                    compressionStream = new DeflateStream(memStream, CompressionMode.Decompress, true);
                    break;

                case System.Net.DecompressionMethods.GZip:
                    compressionStream = new GZipStream(memStream, CompressionMode.Decompress, true);
                    break;

                default:
                    // no compression - return uncompressed
                    return(buffer);
                }

                // write the buffer to the compression
                compressionStream.Write(buffer, offset, count);
                // close the stream
                compressionStream.Dispose();

                // get the count
                memStream.Position = 0L;
                count = (int)memStream.Length;

                // get the bytes from the memory stream
                buffer = memStream.ToArray();

                // dispose of the memory stream
                memStream.Dispose();

                // return the bytes
                return(buffer);
            }
        }
Ejemplo n.º 8
0
 /// <summary>
 /// GET请求
 /// </summary>
 /// <param name="url">请求地址</param>
 /// <param name="autDecom">文件压缩解压编码格式</param>
 /// <param name="headers">默认标头</param>
 /// <param name="x509cert">安全证书</param>
 /// <returns>返回请求页面响应内容</returns>
 public static HttpResponseMessage Get(Uri url, Dictionary <string, string> headers = null, System.Net.DecompressionMethods autDecom = System.Net.DecompressionMethods.None, params X509Certificate[] x509cert)
 {
     return(GetAsync(url, headers, autDecom, x509cert).Result);
 }
Ejemplo n.º 9
0
 /// <summary>
 /// POST请求
 /// </summary>
 /// <param name="url">请求地址</param>
 /// <param name="param">POST内容</param>
 /// <param name="name">HTTP内容的名称</param>
 /// <param name="filename">HTTP内容文件名</param>
 /// <param name="autDecom">文件压缩解压编码格式</param>
 /// <param name="headers">默认标头</param>
 /// <param name="x509cert">安全证书</param>
 /// <returns>返回请求页面响应内容</returns>
 public static HttpResponseMessage PostFile(Uri url, Stream param, string name, string filename, Dictionary <string, string> headers = null, System.Net.DecompressionMethods autDecom = System.Net.DecompressionMethods.None, params X509Certificate[] x509cert)
 {
     return(PostFileAsync(url, param, name, filename, headers, autDecom, x509cert).Result);
 }
Ejemplo n.º 10
0
 /// <summary>
 /// POST请求
 /// </summary>
 /// <param name="url">请求地址</param>
 /// <param name="param">POST内容</param>
 /// <param name="name">HTTP内容的名称</param>
 /// <param name="filename">HTTP内容文件名</param>
 /// <param name="autDecom">文件压缩解压编码格式</param>
 /// <param name="headers">默认标头</param>
 /// <param name="x509cert">安全证书</param>
 /// <returns>返回请求页面响应内容</returns>
 public static async Task <HttpResponseMessage> PostFileAsync(Uri url, byte[] param, string name, string filename, Dictionary <string, string> headers = null, System.Net.DecompressionMethods autDecom = System.Net.DecompressionMethods.None, params X509Certificate[] x509cert)
 {
     using (MemoryStream stream = new MemoryStream(param)) {
         return(await PostFileAsync(url, stream, name, filename, headers, autDecom, x509cert));
     }
 }
Ejemplo n.º 11
0
        /// <summary>
        /// GET请求
        /// </summary>
        /// <param name="url">请求地址</param>
        /// <param name="autDecom">文件压缩解压编码格式</param>
        /// <param name="headers">默认标头</param>
        /// <param name="x509cert">安全证书</param>
        /// <returns>返回请求页面响应内容</returns>
        public static async Task <HttpResponseMessage> GetAsync(Uri url, Dictionary <string, string> headers = null, System.Net.DecompressionMethods autDecom = System.Net.DecompressionMethods.None, params X509Certificate[] x509cert)
        {
            using (HttpClientHandler handler = new HttpClientHandler()) {
                CreateHandlerDecompressionMethods(handler, autDecom);
                CreateHandlerX509(handler, x509cert);

                using (HttpClient http = handler == null ? new HttpClient() : new HttpClient(handler)) {
                    AddHeaders(http, headers);
                    return(await http.GetAsync(url));
                }
            }
        }
Ejemplo n.º 12
0
 /// <summary>
 /// 向指定的HttpClientHandler加入文件压缩解压编码格式
 /// </summary>
 /// <param name="handler">指定HttpClientHandler</param>
 /// <param name="autDecom">文件压缩解压编码格式</param>
 static void CreateHandlerDecompressionMethods(HttpClientHandler handler = null, System.Net.DecompressionMethods autDecom = System.Net.DecompressionMethods.None)
 {
     handler = handler ?? new HttpClientHandler();
     handler.AutomaticDecompression = autDecom;
 }
Ejemplo n.º 13
0
 /// <summary>
 /// POST请求
 /// </summary>
 /// <param name="url">请求地址</param>
 /// <param name="param">POST内容</param>
 /// <param name="mediaType">要用于该内容的媒体(默认:application/json;charset=utf-8)</param>
 /// <param name="headers">默认标头</param>
 /// <param name="autDecom">文件压缩解压编码格式</param>
 /// <param name="x509cert">安全证书</param>
 /// <returns>返回请求页面响应内容</returns>
 public static async Task <HttpResponseMessage> PostAsync(Uri url, string param, MediaTypeHeaderValue mediaType, Dictionary <string, string> headers = null, System.Net.DecompressionMethods autDecom = System.Net.DecompressionMethods.None, params X509Certificate[] x509cert)
 {
     using (HttpClientHandler handler = new HttpClientHandler()) {
         CreateHandlerDecompressionMethods(handler, autDecom);
         CreateHandlerX509(handler, x509cert);
         using (HttpClient http = handler == null ? new HttpClient() : new HttpClient(handler)) {
             AddHeaders(http, headers);
             return(await http.PostAsync(url, new StringContent (param, Encoding.GetEncoding(mediaType.CharSet), mediaType.MediaType)));
         }
     }
 }
Ejemplo n.º 14
0
 /// <summary>
 /// POST请求
 /// </summary>
 /// <param name="url">请求地址</param>
 /// <param name="param">POST内容</param>
 /// <param name="mediaType">要用于该内容的媒体(默认:application/json;charset=utf-8)</param>
 /// <param name="headers">默认标头</param>
 /// <param name="autDecom">文件压缩解压编码格式</param>
 /// <param name="x509cert">安全证书</param>
 /// <returns>返回请求页面响应内容</returns>
 public static HttpResponseMessage Post(Uri url, string param, MediaTypeHeaderValue mediaType, Dictionary <string, string> headers = null, System.Net.DecompressionMethods autDecom = System.Net.DecompressionMethods.None, params X509Certificate[] x509cert)
 {
     return(PostAsync(url, param, mediaType, headers, autDecom, x509cert).Result);
 }
Ejemplo n.º 15
0
 /// <summary>
 /// POST请求
 /// </summary>
 /// <param name="url">请求地址</param>
 /// <param name="param">POST内容</param>
 /// <param name="headers">默认标头</param>
 /// <param name="autDecom">文件压缩解压编码格式</param>
 /// <param name="x509cert">安全证书</param>
 /// <returns>返回请求页面响应内容</returns>
 public static async Task <HttpResponseMessage> PostFormUrlEncodedAsync(Uri url, KeyValuePair <String, String>[] param, Dictionary <string, string> headers = null, System.Net.DecompressionMethods autDecom = System.Net.DecompressionMethods.None, params X509Certificate[] x509cert)
 {
     using (HttpClientHandler handler = new HttpClientHandler()) {
         CreateHandlerDecompressionMethods(handler, autDecom);
         CreateHandlerX509(handler, x509cert);
         using (HttpClient http = handler == null ? new HttpClient() : new HttpClient(handler)) {
             AddHeaders(http, headers);
             return(await http.PostAsync(url, new FormUrlEncodedContent (param)));
         }
     }
 }
Ejemplo n.º 16
0
 /// <summary>
 /// POST请求
 /// </summary>
 /// <param name="url">请求地址</param>
 /// <param name="param">POST内容</param>
 /// <param name="headers">默认标头</param>
 /// <param name="autDecom">文件压缩解压编码格式</param>
 /// <param name="x509cert">安全证书</param>
 /// <returns>返回请求页面响应内容</returns>
 public static HttpResponseMessage PostFormUrlEncoded(Uri url, KeyValuePair <String, String>[] param, Dictionary <string, string> headers = null, System.Net.DecompressionMethods autDecom = System.Net.DecompressionMethods.None, params X509Certificate[] x509cert)
 {
     return(PostFormUrlEncodedAsync(url, param, headers, autDecom, x509cert).Result);
 }
Ejemplo n.º 17
0
 /// <summary>
 /// POST请求
 /// </summary>
 /// <param name="url">请求地址</param>
 /// <param name="content">POST内容</param>
 /// <param name="autDecom">文件压缩解压编码格式</param>
 /// <param name="headers">默认标头</param>
 /// <param name="x509cert">安全证书</param>
 /// <returns>返回请求页面响应内容</returns>
 public static async Task <HttpResponseMessage> PostMultipartFormDataAsync(Uri url, MultipartFormDataContent content, Dictionary <string, string> headers = null, System.Net.DecompressionMethods autDecom = System.Net.DecompressionMethods.None, params X509Certificate[] x509cert)
 {
     if (content is null)
     {
         throw new ArgumentNullException("$param值不能为空。");
     }
     using (HttpClientHandler handler = new HttpClientHandler()) {
         CreateHandlerDecompressionMethods(handler, autDecom);
         CreateHandlerX509(handler, x509cert);
         using (HttpClient http = handler == null ? new HttpClient() : new HttpClient(handler)) {
             return(await http.PostAsync(url, content));
         }
     }
 }
Ejemplo n.º 18
0
 /// <summary>
 /// POST请求
 /// </summary>
 /// <param name="url">请求地址</param>
 /// <param name="param">POST内容</param>
 /// <param name="name">HTTP内容的名称</param>
 /// <param name="filename">HTTP内容文件名</param>
 /// <param name="autDecom">文件压缩解压编码格式</param>
 /// <param name="headers">默认标头</param>
 /// <param name="x509cert">安全证书</param>
 /// <returns>返回请求页面响应内容</returns>
 public static async Task <HttpResponseMessage> PostFileAsync(Uri url, Stream param, string name, string filename, Dictionary <string, string> headers = null, System.Net.DecompressionMethods autDecom = System.Net.DecompressionMethods.None, params X509Certificate[] x509cert)
 {
     using (MultipartFormDataContent content = new MultipartFormDataContent()) {
         using (StreamContent file = new StreamContent(param)) {
             content.Add(file, name, filename);
             return(await PostMultipartFormDataAsync(url, content, headers, autDecom, x509cert));
         }
     }
 }