Ejemplo n.º 1
0
        /// <summary>
        /// Unpack the package
        /// </summary>
        public AppletManifest Unpack()
        {
            switch (this.Compression)
            {
            case "lzma":
                using (MemoryStream ms = new MemoryStream(this.Manifest))
                    using (var dfs = new LZipStream(ms, SharpCompress.Compressors.CompressionMode.Decompress, true))
                        return(AppletManifest.Load(dfs));

            case "bzip2":
                using (MemoryStream ms = new MemoryStream(this.Manifest))
                    using (var dfs = new BZip2Stream(ms, SharpCompress.Compressors.CompressionMode.Decompress, true))
                        return(AppletManifest.Load(dfs));

            case "gzip":
                using (MemoryStream ms = new MemoryStream(this.Manifest))
                    using (GZipStream dfs = new GZipStream(ms, CompressionMode.Decompress))
                        return(AppletManifest.Load(dfs));

            default:
                using (MemoryStream ms = new MemoryStream(this.Manifest))
                    using (DeflateStream dfs = new DeflateStream(ms, CompressionMode.Decompress))
                        return(AppletManifest.Load(dfs));
            }
        }
Ejemplo n.º 2
0
        public TarWriter(Stream destination, TarWriterOptions options)
            : base(ArchiveType.Tar, options)
        {
            finalizeArchiveOnClose = options.FinalizeArchiveOnClose;

            if (!destination.CanWrite)
            {
                throw new ArgumentException("Tars require writable streams.");
            }
            switch (options.CompressionType)
            {
                case CompressionType.None:
                    break;
                case CompressionType.BZip2:
                {
                    destination = new BZip2Stream(destination, CompressionMode.Compress, true);
                }
                    break;
                case CompressionType.GZip:
                {
                    destination = new GZipStream(destination, CompressionMode.Compress, true);
                }
                    break;
                case CompressionType.LZip:
                {
                    destination = new LZipStream(destination, CompressionMode.Compress, true);
                }
                    break;
                default:
                {
                    throw new InvalidFormatException("Tar does not support compression: " + options.CompressionType);
                }
            }
            InitalizeStream(destination);
        }
Ejemplo n.º 3
0
 /// <summary>
 /// Decompresssed a block of data using the LZMA algorithm.
 /// </summary>
 /// <param name="inData">The compressed data block.</param>
 /// <returns>The decompressed data.</returns>
 public static byte[] DecompressLZMA(byte[] inData)
 {
     using var ms               = new MemoryStream(inData);
     using var input            = new LZipStream(ms, CompressionMode.Decompress);
     using var decompressedData = new MemoryStream();
     input.CopyTo(decompressedData);
     return(decompressedData.ToArray());
 }
 /// <summary>
 /// Restore the configuration
 /// </summary>
 public SanteDBConfiguration Restore()
 {
     using (var lzs = new LZipStream(File.OpenRead(Path.ChangeExtension(this.m_configPath, "bak.7z")), SharpCompress.Compressors.CompressionMode.Decompress))
     {
         var retVal = SanteDBConfiguration.Load(lzs);
         this.Save(retVal);
         ApplicationContext.Current?.ConfigurationManager.Reload();
         return(retVal);
     }
 }
Ejemplo n.º 5
0
 /// <summary>
 /// 压缩
 /// </summary>
 /// <param name="bytes">需要压缩的字节流</param>
 /// <returns>压缩后的结果</returns>
 public byte[] Compress(byte[] bytes)
 {
     using (var ms = new MemoryStream())
     {
         var lzma = new LZipStream(ms, CompressionMode.Compress, true);
         lzma.Write(bytes, 0, bytes.Length);
         lzma.Close();
         return(ms.ToArray());
     }
 }
Ejemplo n.º 6
0
        public void DecompressFile(string source, string destination)
        {
            var    inFs    = new FileStream(source, FileMode.Open, FileAccess.Read);
            var    outFs   = new FileStream(destination, FileMode.Create, FileAccess.Write);
            Stream zStream = new LZipStream(inFs, CompressionMode.Decompress);

            zStream.CopyTo(outFs);

            outFs.Close();
            zStream.Close();
            inFs.Close();
        }
Ejemplo n.º 7
0
 /// <summary>
 /// 解压缩
 /// </summary>
 /// <param name="bytes">需要解压缩的字节流</param>
 /// <returns>解压缩的结果</returns>
 public byte[] Decompress(byte[] bytes)
 {
     using (var ms = new MemoryStream())
     {
         var lzma  = new LZipStream(new MemoryStream(bytes), CompressionMode.Decompress, true);
         var count = 0;
         var data  = new byte[4096];
         while ((count = lzma.Read(data, 0, data.Length)) != 0)
         {
             ms.Write(data, 0, count);
         }
         lzma.Close();
         return(ms.ToArray());
     }
 }
Ejemplo n.º 8
0
        /// <summary>
        /// Opens a TarReader for Non-seeking usage with a single volume
        /// </summary>
        /// <param name="stream"></param>
        /// <param name="options"></param>
        /// <returns></returns>
        public static TarReader Open(Stream stream, ReaderOptions options = null)
        {
            stream.CheckNotNull(nameof(stream));
            options = options ?? new ReaderOptions();
            RewindableStream rewindableStream = new RewindableStream(stream);

            rewindableStream.StartRecording();
            if (GZipArchive.IsGZipFile(rewindableStream))
            {
                rewindableStream.Rewind(false);
                GZipStream testStream = new GZipStream(rewindableStream, CompressionMode.Decompress);
                if (TarArchive.IsTarFile(testStream))
                {
                    rewindableStream.Rewind(true);
                    return(new TarReader(rewindableStream, options, CompressionType.GZip));
                }
                throw new InvalidFormatException("Not a tar file.");
            }

            rewindableStream.Rewind(false);
            if (BZip2Stream.IsBZip2(rewindableStream))
            {
                rewindableStream.Rewind(false);
                BZip2Stream testStream = new BZip2Stream(rewindableStream, CompressionMode.Decompress, false);
                if (TarArchive.IsTarFile(testStream))
                {
                    rewindableStream.Rewind(true);
                    return(new TarReader(rewindableStream, options, CompressionType.BZip2));
                }
                throw new InvalidFormatException("Not a tar file.");
            }

            rewindableStream.Rewind(false);
            if (LZipStream.IsLZipFile(rewindableStream))
            {
                rewindableStream.Rewind(false);
                LZipStream testStream = new LZipStream(rewindableStream, CompressionMode.Decompress);
                if (TarArchive.IsTarFile(testStream))
                {
                    rewindableStream.Rewind(true);
                    return(new TarReader(rewindableStream, options, CompressionType.LZip));
                }
                throw new InvalidFormatException("Not a tar file.");
            }
            rewindableStream.Rewind(true);
            return(new TarReader(rewindableStream, options, CompressionType.None));
        }
Ejemplo n.º 9
0
        /// <summary>
        /// Create an unsigned package
        /// </summary>
        /// <returns>The package.</returns>
        public AppletPackage CreatePackage(String compression = null)
        {
            AppletPackage retVal = new AppletPackage()
            {
                Meta        = this.Info,
                Compression = compression
            };

            using (MemoryStream ms = new MemoryStream())
            {
                Stream compressStream = null;
                try {
                    switch (compression)
                    {
                    case "lzma":
                        compressStream = new LZipStream(ms, SharpCompress.Compressors.CompressionMode.Compress, leaveOpen: true);
                        break;

                    case "bzip2":
                        compressStream = new BZip2Stream(ms, SharpCompress.Compressors.CompressionMode.Compress, leaveOpen: true);
                        break;

                    case "gzip":
                        compressStream = new GZipStream(ms, SharpCompress.Compressors.CompressionMode.Compress, leaveOpen: true);
                        break;

                    case "none":
                        compressStream = ms;
                        break;

                    default:
                        compressStream = new DeflateStream(ms, SharpCompress.Compressors.CompressionMode.Compress, leaveOpen: true);
                        break;
                    }
                    XmlSerializer xsz = new XmlSerializer(typeof(AppletManifest));
                    xsz.Serialize(compressStream, this);
                }
                finally
                {
                    compressStream.Dispose();
                }
                retVal.Manifest = ms.ToArray();
            }
            return(retVal);
        }
Ejemplo n.º 10
0
        /// <summary>
        /// Create an unsigned package
        /// </summary>
        /// <returns>The package.</returns>
        public AppletPackage CreatePackage()
        {
            AppletPackage retVal = new AppletPackage()
            {
                Meta = this.Info
            };

            this.Info.TimeStamp = DateTime.Now;
            using (var ms = new MemoryStream())
            {
                using (var ls = new LZipStream(ms, SharpCompress.Compressors.CompressionMode.Compress))
                {
                    x_xsz.Serialize(ls, this);
                }
                retVal.Manifest = ms.ToArray();
            }
            return(retVal);
        }
Ejemplo n.º 11
0
 public int OnExecute(IConsole console)
 {
     try
     {
         using (var istm = Util.OpenInputStream(InputFile))
             using (var ostm = Util.OpenOutputStream(OutputFile, true))
             {
                 using (var izstm = new LZipStream(istm, CompressionMode.Decompress))
                 {
                     izstm.CopyTo(ostm);
                 }
             }
     }
     catch (Exception e)
     {
         console.Error.WriteLine($"failed lzip decompression:{e}");
         return(1);
     }
     return(0);
 }
Ejemplo n.º 12
0
        public void CompressFile(string source, string destination)
        {
            var    inFs    = new FileStream(source, FileMode.Open, FileAccess.Read);
            var    outFs   = new FileStream(destination, FileMode.CreateNew, FileAccess.Write);
            Stream zStream = new LZipStream(outFs, CompressionMode.Compress);

            byte[] buffer = new byte[BUFFER_SIZE];

            SetProgressBounds?.Invoke(this, new ProgressBoundsEventArgs
            {
                Minimum = 0,
                Maximum = inFs.Length
            });

            while (inFs.Position + BUFFER_SIZE <= inFs.Length)
            {
                SetProgress?.Invoke(this, new ProgressEventArgs
                {
                    Value = inFs.Position
                });

                inFs.Read(buffer, 0, buffer.Length);
                zStream.Write(buffer, 0, buffer.Length);
            }

            buffer = new byte[inFs.Length - inFs.Position];

            SetProgressBounds?.Invoke(this, new ProgressBoundsEventArgs
            {
                Minimum = 0,
                Maximum = inFs.Length
            });

            inFs.Read(buffer, 0, buffer.Length);
            zStream.Write(buffer, 0, buffer.Length);

            inFs.Close();
            zStream.Close();
            outFs.Dispose();
        }
Ejemplo n.º 13
0
        protected override void Dispose(bool isDisposing)
        {
            if (isDisposing)
            {
                if (finalizeArchiveOnClose) {
                    PadTo512(0, true);
                    PadTo512(0, true);
                }

                if(OutputStream.GetType()==typeof(BZip2Stream))

                {
                    BZip2Stream b =(BZip2Stream) OutputStream;
                    b.Finish();
                }
                else if(OutputStream.GetType() == typeof(LZipStream))

                {
                    LZipStream l =(LZipStream) OutputStream;
                    l.Finish();
                }
                //switch (OutputStream)
                //{
                //    case BZip2Stream b:
                //    {
                //        b.Finish();
                //        break;
                //    }
                //    case LZipStream l:
                //    {
                //        l.Finish();
                //        break;
                //    }
                //}
            }
            base.Dispose(isDisposing);
        }
Ejemplo n.º 14
0
        /// <summary>
        /// Gets the specified URL in raw format
        /// </summary>
        public byte[] Get(String url)
        {
            NameValueCollection parameters = new NameValueCollection();

            try
            {
                var requestEventArgs = new RestRequestEventArgs("GET", url, null, null, null);
                this.Requesting?.Invoke(this, requestEventArgs);
                if (requestEventArgs.Cancel)
                {
                    s_tracer.TraceVerbose("HTTP request cancelled");
                    return(null);
                }

                // Invoke
                var httpWebReq = this.CreateHttpRequest(url, requestEventArgs.Query);
                httpWebReq.Method = "GET";

                // Get the responst
                byte[]    retVal           = null;
                Exception requestException = null;
                var       httpTask         = httpWebReq.GetResponseAsync().ContinueWith(o =>
                {
                    if (o.IsFaulted)
                    {
                        requestException = o.Exception.InnerExceptions.First();
                    }
                    else
                    {
                        try
                        {
                            this.Responding?.Invoke(this, new RestResponseEventArgs("GET", url, null, null, null, 200, o.Result.ContentLength));

                            byte[] buffer = new byte[2048];
                            int br        = 1;
                            using (var ms = new MemoryStream())
                                using (var httpStream = o.Result.GetResponseStream())
                                {
                                    while (br > 0)
                                    {
                                        br = httpStream.Read(buffer, 0, 2048);
                                        ms.Write(buffer, 0, br);
                                        // Raise event
                                        this.FireProgressChanged(o.Result.ContentType, ms.Length / (float)o.Result.ContentLength);
                                    }


                                    ms.Seek(0, SeekOrigin.Begin);

                                    switch (o.Result.Headers["Content-Encoding"])
                                    {
                                    case "deflate":
                                        using (var dfs = new DeflateStream(ms, CompressionMode.Decompress, leaveOpen: true))
                                            using (var oms = new MemoryStream())
                                            {
                                                dfs.CopyTo(oms);
                                                retVal = oms.ToArray();
                                            }
                                        break;

                                    case "gzip":
                                        using (var gzs = new GZipStream(ms, CompressionMode.Decompress, leaveOpen: true))
                                            using (var oms = new MemoryStream())
                                            {
                                                gzs.CopyTo(oms);
                                                retVal = oms.ToArray();
                                            }
                                        break;

                                    case "bzip2":
                                        using (var lzmas = new BZip2Stream(ms, CompressionMode.Decompress, leaveOpen: true))
                                            using (var oms = new MemoryStream())
                                            {
                                                lzmas.CopyTo(oms);
                                                retVal = oms.ToArray();
                                            }
                                        break;

                                    case "lzma":
                                        using (var lzmas = new LZipStream(ms, CompressionMode.Decompress, leaveOpen: true))
                                            using (var oms = new MemoryStream())
                                            {
                                                lzmas.CopyTo(oms);
                                                retVal = oms.ToArray();
                                            }
                                        break;

                                    default:
                                        retVal = ms.ToArray();
                                        break;
                                    }
                                }
                        }
                        catch (Exception e)
                        {
                            s_tracer.TraceError("Error downloading {0}: {1}", url, e.Message);
                        }
                    }
                }, TaskContinuationOptions.LongRunning);
                httpTask.Wait();
                if (requestException != null)
                {
                    throw requestException;
                }


                this.Responded?.Invoke(this, new RestResponseEventArgs("GET", url, null, null, null, 200, 0));

                return(retVal);
            }
            catch (WebException e)
            {
                switch (this.ValidateResponse(e.Response))
                {
                case ServiceClientErrorType.Valid:
                    return(this.Get(url));

                default:
                    throw new RestClientException <byte[]>(
                              null,
                              e,
                              e.Status,
                              e.Response);
                }
            }
            catch (Exception e)
            {
                s_tracer.TraceError("Error invoking HTTP: {0}", e.Message);
                this.Responded?.Invoke(this, new RestResponseEventArgs("GET", url, null, null, null, 500, 0));
                throw;
            }
        }
Ejemplo n.º 15
0
        public static void ClamScanFileFromRepo(DbFile file)
        {
            try
            {
                if (Context.ClamdVersion == null)
                {
                    Failed?.Invoke("clamd is not usable");
                    return;
                }

                if (clam == null)
                {
                    Failed?.Invoke("clamd is not initalized");
                }

                string   repoPath;
                AlgoEnum algorithm;

                if (File.Exists(Path.Combine(Settings.Current.RepositoryPath, file.Sha256[0].ToString(),
                                             file.Sha256[1].ToString(), file.Sha256[2].ToString(),
                                             file.Sha256[3].ToString(), file.Sha256[4].ToString(), file.Sha256 + ".gz")))
                {
                    repoPath = Path.Combine(Settings.Current.RepositoryPath, file.Sha256[0].ToString(),
                                            file.Sha256[1].ToString(), file.Sha256[2].ToString(),
                                            file.Sha256[3].ToString(), file.Sha256[4].ToString(), file.Sha256 + ".gz");
                    algorithm = AlgoEnum.GZip;
                }
                else if (File.Exists(Path.Combine(Settings.Current.RepositoryPath, file.Sha256[0].ToString(),
                                                  file.Sha256[1].ToString(), file.Sha256[2].ToString(),
                                                  file.Sha256[3].ToString(), file.Sha256[4].ToString(),
                                                  file.Sha256 + ".bz2")))
                {
                    repoPath = Path.Combine(Settings.Current.RepositoryPath, file.Sha256[0].ToString(),
                                            file.Sha256[1].ToString(), file.Sha256[2].ToString(),
                                            file.Sha256[3].ToString(), file.Sha256[4].ToString(), file.Sha256 + ".bz2");
                    algorithm = AlgoEnum.BZip2;
                }
                else if (File.Exists(Path.Combine(Settings.Current.RepositoryPath, file.Sha256[0].ToString(),
                                                  file.Sha256[1].ToString(), file.Sha256[2].ToString(),
                                                  file.Sha256[3].ToString(), file.Sha256[4].ToString(),
                                                  file.Sha256 + ".lzma")))
                {
                    repoPath = Path.Combine(Settings.Current.RepositoryPath, file.Sha256[0].ToString(),
                                            file.Sha256[1].ToString(), file.Sha256[2].ToString(),
                                            file.Sha256[3].ToString(), file.Sha256[4].ToString(),
                                            file.Sha256 + ".lzma");
                    algorithm = AlgoEnum.LZMA;
                }
                else if (File.Exists(Path.Combine(Settings.Current.RepositoryPath, file.Sha256[0].ToString(),
                                                  file.Sha256[1].ToString(), file.Sha256[2].ToString(),
                                                  file.Sha256[3].ToString(), file.Sha256[4].ToString(),
                                                  file.Sha256 + ".lz")))
                {
                    repoPath = Path.Combine(Settings.Current.RepositoryPath, file.Sha256[0].ToString(),
                                            file.Sha256[1].ToString(), file.Sha256[2].ToString(),
                                            file.Sha256[3].ToString(), file.Sha256[4].ToString(), file.Sha256 + ".lz");
                    algorithm = AlgoEnum.LZip;
                }
                else
                {
                    Failed?.Invoke($"Cannot find file with hash {file.Sha256} in the repository");
                    return;
                }

                ClamScanResult result  = null;
                Stream         zStream = null;

                if (Settings.Current.ClamdIsLocal)
                {
                    if (algorithm == AlgoEnum.LZMA || algorithm == AlgoEnum.LZip)
                    {
                        string     tmpFile = Path.Combine(Settings.Current.TemporaryFolder, Path.GetTempFileName());
                        FileStream outFs   = new FileStream(tmpFile, FileMode.Create, FileAccess.Write);
                        FileStream inFs    = new FileStream(repoPath, FileMode.Open, FileAccess.Read);

                        if (algorithm == AlgoEnum.LZMA)
                        {
                            byte[] properties = new byte[5];
                            inFs.Read(properties, 0, 5);
                            inFs.Seek(8, SeekOrigin.Current);
                            zStream = new LzmaStream(properties, inFs, inFs.Length - 13, file.Length);
                        }
                        else
                        {
                            zStream = new LZipStream(inFs, CompressionMode.Decompress);
                        }

                        UpdateProgress?.Invoke("Uncompressing file...", null, 0, 0);

                        #if DEBUG
                        stopwatch.Restart();
                        #endif
                        zStream.CopyTo(outFs);
                        zStream.Close();
                        outFs.Close();
                        #if DEBUG
                        stopwatch.Stop();
                        Console.WriteLine("Core.ClamScanFileFromRepo({0}): Uncompressing took {1} seconds", file,
                                          stopwatch.Elapsed.TotalSeconds);
                        #endif

                        UpdateProgress?.Invoke("Requesting local scan to clamd server...", null, 0, 0);

                        #if DEBUG
                        stopwatch.Restart();
                        #endif
                        Task.Run(async() => { result = await clam.ScanFileOnServerMultithreadedAsync(tmpFile); })
                        .Wait();
                        #if DEBUG
                        stopwatch.Stop();
                        Console.WriteLine("Core.ClamScanFileFromRepo({0}): Clamd took {1} seconds to scan", file,
                                          stopwatch.Elapsed.TotalSeconds);
                        #endif

                        File.Delete(tmpFile);
                    }
                    else
                    {
                        UpdateProgress?.Invoke("Requesting local scan to clamd server...", null, 0, 0);

                        #if DEBUG
                        stopwatch.Restart();
                        #endif
                        Task.Run(async() => { result = await clam.ScanFileOnServerMultithreadedAsync(repoPath); })
                        .Wait();
                        #if DEBUG
                        stopwatch.Stop();
                        Console.WriteLine("Core.ClamScanFileFromRepo({0}): Clamd took {1} seconds to scan", file,
                                          stopwatch.Elapsed.TotalSeconds);
                        #endif
                    }
                }
                else
                {
                    FileStream inFs = new FileStream(repoPath, FileMode.Open, FileAccess.Read);

                    switch (algorithm)
                    {
                    case AlgoEnum.GZip:
                        zStream = new GZipStream(inFs, CompressionMode.Decompress);
                        break;

                    case AlgoEnum.BZip2:
                        zStream = new BZip2Stream(inFs, CompressionMode.Decompress);
                        break;

                    case AlgoEnum.LZMA:
                        byte[] properties = new byte[5];
                        inFs.Read(properties, 0, 5);
                        inFs.Seek(8, SeekOrigin.Current);
                        zStream = new LzmaStream(properties, inFs, inFs.Length - 13, file.Length);
                        break;

                    case AlgoEnum.LZip:
                        zStream = new LZipStream(inFs, CompressionMode.Decompress);
                        break;
                    }

                    UpdateProgress?.Invoke("Uploading file to clamd server...", null, 0, 0);

                    #if DEBUG
                    stopwatch.Restart();
                    #endif
                    Task.Run(async() => { result = await clam.SendAndScanFileAsync(zStream); }).Wait();
                    #if DEBUG
                    stopwatch.Stop();
                    Console.WriteLine("Core.ClamScanFileFromRepo({0}): Clamd took {1} seconds to scan", file,
                                      stopwatch.Elapsed.TotalSeconds);
                    #endif
                    zStream.Close();
                }

                if (result.InfectedFiles != null && result.InfectedFiles.Count > 0)
                {
                    file.HasVirus = true;
                    file.Virus    = result.InfectedFiles[0].VirusName;
                }
                else if (file.HasVirus == null)
                {
                    // If no scan has been done, mark as false.
                    // If a positive has already existed don't overwrite it.
                    file.HasVirus = false;
                    file.Virus    = null;
                }

                file.ClamTime = DateTime.UtcNow;

                dbCore.DbOps.UpdateFile(file);

                ScanFinished?.Invoke(file);
            }
            catch (ThreadAbortException) { }
            catch (Exception ex)
            {
                Failed?.Invoke($"Exception {ex.Message} when calling clamd");
                #if DEBUG
                Console.WriteLine("Exception {0}\n{1}", ex.Message, ex.InnerException);
                #endif
            }
        }
Ejemplo n.º 16
0
 /// <summary>
 /// Restore the configuration
 /// </summary>
 public void Restore()
 {
     using (var lzs = new LZipStream(File.OpenRead(Path.ChangeExtension(this.m_configPath, "bak.7z")), SharpCompress.Compressors.CompressionMode.Decompress))
         this.m_configuration = OpenIZConfiguration.Load(lzs);
     this.Save();
 }
Ejemplo n.º 17
0
        public static bool TryOpen(Stream stream, ReaderOptions options, ArchiveTypeMask archiveTypes, out IReader reader)
        {
            stream.CheckNotNull("stream");
            options = options ?? new ReaderOptions()
            {
                LeaveStreamOpen = false
            };
            RewindableStream rewindableStream = new RewindableStream(stream);

            rewindableStream.StartRecording();
            if (archiveTypes.HasFlag(ArchiveTypeMask.Zip) && ZipArchive.IsZipFile(rewindableStream, options.Password))
            {
                rewindableStream.Rewind(true);
                reader = ZipReader.Open(rewindableStream, options);
                return(true);
            }
            rewindableStream.Rewind(false);
            if (archiveTypes.HasFlag(ArchiveTypeMask.GZip) && GZipArchive.IsGZipFile(rewindableStream))
            {
                rewindableStream.Rewind(false);
                GZipStream testStream = new GZipStream(rewindableStream, CompressionMode.Decompress);
                if (archiveTypes.HasFlag(ArchiveTypeMask.Tar) && TarArchive.IsTarFile(testStream))
                {
                    rewindableStream.Rewind(true);
                    reader = new TarReader(rewindableStream, options, CompressionType.GZip);
                    return(true);
                }
                rewindableStream.Rewind(true);
                reader = GZipReader.Open(rewindableStream, options);
                return(true);
            }

            rewindableStream.Rewind(false);
            if (archiveTypes.HasFlag(ArchiveTypeMask.BZip2) && BZip2Stream.IsBZip2(rewindableStream))
            {
                rewindableStream.Rewind(false);
                BZip2Stream testStream = new BZip2Stream(new NonDisposingStream(rewindableStream), CompressionMode.Decompress, false);
                if (archiveTypes.HasFlag(ArchiveTypeMask.Tar) && TarArchive.IsTarFile(testStream))
                {
                    rewindableStream.Rewind(true);
                    reader = new TarReader(rewindableStream, options, CompressionType.BZip2);
                    return(true);
                }
            }

            rewindableStream.Rewind(false);
            if (archiveTypes.HasFlag(ArchiveTypeMask.LZip) && LZipStream.IsLZipFile(rewindableStream))
            {
                rewindableStream.Rewind(false);
                LZipStream testStream = new LZipStream(new NonDisposingStream(rewindableStream), CompressionMode.Decompress);
                if (archiveTypes.HasFlag(ArchiveTypeMask.Tar) && TarArchive.IsTarFile(testStream))
                {
                    rewindableStream.Rewind(true);
                    reader = new TarReader(rewindableStream, options, CompressionType.LZip);
                    return(true);
                }
            }
            rewindableStream.Rewind(false);
            if (archiveTypes.HasFlag(ArchiveTypeMask.Rar) && RarArchive.IsRarFile(rewindableStream, options))
            {
                rewindableStream.Rewind(true);
                reader = RarReader.Open(rewindableStream, options);
                return(true);
            }

            rewindableStream.Rewind(false);
            if (archiveTypes.HasFlag(ArchiveTypeMask.Tar) && TarArchive.IsTarFile(rewindableStream))
            {
                rewindableStream.Rewind(true);
                reader = TarReader.Open(rewindableStream, options);
                return(true);
            }
            rewindableStream.Rewind(false);
            if (XZStream.IsXZStream(rewindableStream))
            {
                rewindableStream.Rewind(true);
                XZStream testStream = new XZStream(rewindableStream);
                if (archiveTypes.HasFlag(ArchiveTypeMask.Tar) && TarArchive.IsTarFile(testStream))
                {
                    rewindableStream.Rewind(true);
                    reader = new TarReader(rewindableStream, options, CompressionType.Xz);
                    return(true);
                }
            }
            reader = null;
            return(false);
        }
Ejemplo n.º 18
0
        public static void VirusTotalFileFromRepo(DbFile file)
        {
            try
            {
                if (!Context.VirusTotalEnabled)
                {
                    Failed?.Invoke("VirusTotal is not usable");

                    return;
                }

                if (vTotal == null)
                {
                    Failed?.Invoke("VirusTotal is not initalized");
                }

                FileReport fResult = null;

                UpdateProgress?.Invoke("Requesting existing report to VirusTotal", null, 0, 0);

            #if DEBUG
                stopwatch.Restart();
            #endif
                Task.Run(async() =>
                {
                    fResult = await vTotal.GetFileReportAsync(file.Sha256);
                }).Wait();
            #if DEBUG
                stopwatch.Stop();

                Console.WriteLine("Core.VirusTotalFileFromRepo({0}): VirusTotal took {1} seconds to answer for SHA256 request",
                                  file, stopwatch.Elapsed.TotalSeconds);
            #endif

                if (fResult.ResponseCode == FileReportResponseCode.NotPresent)
                {
                    Failed?.Invoke(fResult.VerboseMsg);

                    return;
                }

                if (fResult.ResponseCode != FileReportResponseCode.Queued)
                {
                    if (fResult.ResponseCode == FileReportResponseCode.Present)
                    {
                        if (fResult.Positives > 0)
                        {
                            file.HasVirus = true;

                            if (fResult.Scans != null)
                            {
                                foreach (KeyValuePair <string, ScanEngine> engine in fResult.Scans)
                                {
                                    if (!engine.Value.Detected)
                                    {
                                        continue;
                                    }

                                    file.Virus          = engine.Value.Result;
                                    file.VirusTotalTime = engine.Value.Update;
                                    dbCore.DbOps.UpdateFile(file);

                                    ScanFinished?.Invoke(file);

                                    return;
                                }
                            }
                        }
                        else
                        {
                            // If no scan has been done, mark as false.
                            // If a positive has already existed don't overwrite it.
                            file.HasVirus       = false;
                            file.Virus          = null;
                            file.VirusTotalTime = DateTime.UtcNow;

                            dbCore.DbOps.UpdateFile(file);

                            ScanFinished?.Invoke(file);

                            return;
                        }
                    }

                    string   repoPath;
                    AlgoEnum algorithm;

                    if (File.Exists(Path.Combine(Settings.Current.RepositoryPath, file.Sha256[0].ToString(),
                                                 file.Sha256[1].ToString(), file.Sha256[2].ToString(),
                                                 file.Sha256[3].ToString(), file.Sha256[4].ToString(),
                                                 file.Sha256 + ".gz")))
                    {
                        repoPath = Path.Combine(Settings.Current.RepositoryPath, file.Sha256[0].ToString(),
                                                file.Sha256[1].ToString(), file.Sha256[2].ToString(),
                                                file.Sha256[3].ToString(), file.Sha256[4].ToString(),
                                                file.Sha256 + ".gz");

                        algorithm = AlgoEnum.GZip;
                    }
                    else if (File.Exists(Path.Combine(Settings.Current.RepositoryPath, file.Sha256[0].ToString(),
                                                      file.Sha256[1].ToString(), file.Sha256[2].ToString(),
                                                      file.Sha256[3].ToString(), file.Sha256[4].ToString(),
                                                      file.Sha256 + ".bz2")))
                    {
                        repoPath = Path.Combine(Settings.Current.RepositoryPath, file.Sha256[0].ToString(),
                                                file.Sha256[1].ToString(), file.Sha256[2].ToString(),
                                                file.Sha256[3].ToString(), file.Sha256[4].ToString(),
                                                file.Sha256 + ".bz2");

                        algorithm = AlgoEnum.BZip2;
                    }
                    else if (File.Exists(Path.Combine(Settings.Current.RepositoryPath, file.Sha256[0].ToString(),
                                                      file.Sha256[1].ToString(), file.Sha256[2].ToString(),
                                                      file.Sha256[3].ToString(), file.Sha256[4].ToString(),
                                                      file.Sha256 + ".lzma")))
                    {
                        repoPath = Path.Combine(Settings.Current.RepositoryPath, file.Sha256[0].ToString(),
                                                file.Sha256[1].ToString(), file.Sha256[2].ToString(),
                                                file.Sha256[3].ToString(), file.Sha256[4].ToString(),
                                                file.Sha256 + ".lzma");

                        algorithm = AlgoEnum.LZMA;
                    }
                    else if (File.Exists(Path.Combine(Settings.Current.RepositoryPath, file.Sha256[0].ToString(),
                                                      file.Sha256[1].ToString(), file.Sha256[2].ToString(),
                                                      file.Sha256[3].ToString(), file.Sha256[4].ToString(),
                                                      file.Sha256 + ".lz")))
                    {
                        repoPath = Path.Combine(Settings.Current.RepositoryPath, file.Sha256[0].ToString(),
                                                file.Sha256[1].ToString(), file.Sha256[2].ToString(),
                                                file.Sha256[3].ToString(), file.Sha256[4].ToString(),
                                                file.Sha256 + ".lz");

                        algorithm = AlgoEnum.LZip;
                    }
                    else
                    {
                        Failed?.Invoke($"Cannot find file with hash {file.Sha256} in the repository");

                        return;
                    }

                    UpdateProgress?.Invoke("Uncompressing file...", null, 0, 0);

                    var    inFs    = new FileStream(repoPath, FileMode.Open, FileAccess.Read);
                    Stream zStream = null;

                    switch (algorithm)
                    {
                    case AlgoEnum.GZip:
                        zStream = new GZipStream(inFs, CompressionMode.Decompress);

                        break;

                    case AlgoEnum.BZip2:
                        zStream = new BZip2Stream(inFs, CompressionMode.Decompress);

                        break;

                    case AlgoEnum.LZMA:
                        byte[] properties = new byte[5];
                        inFs.Read(properties, 0, 5);
                        inFs.Seek(8, SeekOrigin.Current);
                        zStream = new LzmaStream(properties, inFs, inFs.Length - 13, file.Length);

                        break;

                    case AlgoEnum.LZip:
                        zStream = new LZipStream(inFs, CompressionMode.Decompress);

                        break;
                    }

                    ScanResult sResult = null;

                #if DEBUG
                    stopwatch.Restart();
                #endif

                    // Cannot use zStream directly, VirusTotal.NET requests the size *sigh*
                    string tmpFile = Path.Combine(Settings.Current.TemporaryFolder, Path.GetTempFileName());
                    var    outFs   = new FileStream(tmpFile, FileMode.Create, FileAccess.ReadWrite);
                    zStream?.CopyTo(outFs);
                    zStream?.Close();
                    outFs.Seek(0, SeekOrigin.Begin);
                #if DEBUG
                    stopwatch.Stop();

                    Console.WriteLine("Core.VirusTotalFileFromRepo({0}): Uncompressing took {1} seconds", file,
                                      stopwatch.Elapsed.TotalSeconds);
                #endif

                    UpdateProgress?.Invoke("Uploading file to VirusTotal...", null, 0, 0);

                #if DEBUG
                    stopwatch.Restart();
                #endif
                    Task.Run(async() =>
                    {
                        sResult = await vTotal.ScanFileAsync(outFs, file.Sha256); // Keep filename private, sorry!
                    }).Wait();
                #if DEBUG
                    stopwatch.Stop();

                    Console.WriteLine("Core.VirusTotalFileFromRepo({0}): Upload to VirusTotal took {1} seconds", file,
                                      stopwatch.Elapsed.TotalSeconds);
                #endif
                    outFs.Close();

                    File.Delete(tmpFile);

                    if (sResult == null ||
                        sResult.ResponseCode == ScanFileResponseCode.Error)
                    {
                        if (sResult == null)
                        {
                            Failed?.Invoke("Cannot send file to VirusTotal");
                        }
                        else
                        {
                            Failed(sResult.VerboseMsg);
                        }

                        return;
                    }

                    // Seems that we are faster than them, getting a lot of "not queued" responses...
                    Thread.Sleep(2500);

                    Task.Run(async() =>
                    {
                        fResult = await vTotal.GetFileReportAsync(file.Sha256);
                    }).Wait();
                }

                UpdateProgress?.Invoke("Waiting for VirusTotal analysis...", null, 0, 0);

            #if DEBUG
                stopwatch.Restart();
            #endif
                int counter = 0;

                while (fResult.ResponseCode == FileReportResponseCode.Queued)
                {
                    // Timeout...
                    if (counter == 10)
                    {
                        break;
                    }

                    // Wait 15 seconds so we fall in the 4 requests/minute
                    Thread.Sleep(15000);

                    Task.Run(async() =>
                    {
                        fResult = await vTotal.GetFileReportAsync(file.Sha256);
                    }).Wait();

                    counter++;
                }
            #if DEBUG
                stopwatch.Stop();

                Console.WriteLine("Core.VirusTotalFileFromRepo({0}): VirusTotal took {1} seconds to do the analysis",
                                  file, stopwatch.Elapsed.TotalSeconds);
            #endif

                if (fResult.ResponseCode != FileReportResponseCode.Present)
                {
                    Failed?.Invoke(fResult.VerboseMsg);

                    return;
                }

                if (fResult.Positives > 0)
                {
                    file.HasVirus = true;

                    if (fResult.Scans == null)
                    {
                        return;
                    }

                    foreach (KeyValuePair <string, ScanEngine> engine in fResult.Scans)
                    {
                        if (!engine.Value.Detected)
                        {
                            continue;
                        }

                        file.Virus          = engine.Value.Result;
                        file.VirusTotalTime = engine.Value.Update;
                        dbCore.DbOps.UpdateFile(file);

                        ScanFinished?.Invoke(file);

                        return;
                    }
                }
                else
                {
                    // If no scan has been done, mark as false.
                    // If a positive has already existed don't overwrite it.
                    file.HasVirus       = false;
                    file.Virus          = null;
                    file.VirusTotalTime = DateTime.UtcNow;

                    dbCore.DbOps.UpdateFile(file);

                    ScanFinished?.Invoke(file);
                }
            }
            catch (Exception ex)
            {
                Failed?.Invoke($"Exception {ex.InnerException.Message} when calling VirusTotal");
            #if DEBUG
                Console.WriteLine("Exception {0}\n{1}", ex.Message, ex.InnerException);
            #endif
            }
        }
Ejemplo n.º 19
0
        public static void CompressFiles()
        {
            try
            {
                if (string.IsNullOrWhiteSpace(Context.DbInfo.Developer))
                {
                    Failed?.Invoke("Developer cannot be empty");
                    return;
                }

                if (string.IsNullOrWhiteSpace(Context.DbInfo.Product))
                {
                    Failed?.Invoke("Product cannot be empty");
                    return;
                }

                if (string.IsNullOrWhiteSpace(Context.DbInfo.Version))
                {
                    Failed?.Invoke("Version cannot be empty");
                    return;
                }

                string destinationFolder = "";
                destinationFolder = Path.Combine(destinationFolder, Context.DbInfo.Developer);
                destinationFolder = Path.Combine(destinationFolder, Context.DbInfo.Product);
                destinationFolder = Path.Combine(destinationFolder, Context.DbInfo.Version);
                if (!string.IsNullOrWhiteSpace(Context.DbInfo.Languages))
                {
                    destinationFolder = Path.Combine(destinationFolder, Context.DbInfo.Languages);
                }
                if (!string.IsNullOrWhiteSpace(Context.DbInfo.Architecture))
                {
                    destinationFolder = Path.Combine(destinationFolder, Context.DbInfo.Architecture);
                }
                if (Context.DbInfo.Oem)
                {
                    destinationFolder = Path.Combine(destinationFolder, "oem");
                }
                if (!string.IsNullOrWhiteSpace(Context.DbInfo.Machine))
                {
                    destinationFolder = Path.Combine(destinationFolder, "for " + Context.DbInfo.Machine);
                }

                string destinationFile = "";
                if (!string.IsNullOrWhiteSpace(Context.DbInfo.Format))
                {
                    destinationFile += "[" + Context.DbInfo.Format + "]";
                }
                if (Context.DbInfo.Files)
                {
                    if (destinationFile != "")
                    {
                        destinationFile += "_";
                    }
                    destinationFile += "files";
                }

                if (Context.DbInfo.Netinstall)
                {
                    if (destinationFile != "")
                    {
                        destinationFile += "_";
                    }
                    destinationFile += "netinstall";
                }

                if (Context.DbInfo.Source)
                {
                    if (destinationFile != "")
                    {
                        destinationFile += "_";
                    }
                    destinationFile += "source";
                }

                if (Context.DbInfo.Update)
                {
                    if (destinationFile != "")
                    {
                        destinationFile += "_";
                    }
                    destinationFile += "update";
                }

                if (Context.DbInfo.Upgrade)
                {
                    if (destinationFile != "")
                    {
                        destinationFile += "_";
                    }
                    destinationFile += "upgrade";
                }

                if (!string.IsNullOrWhiteSpace(Context.DbInfo.Description))
                {
                    if (destinationFile != "")
                    {
                        destinationFile += "_";
                    }
                    destinationFile += Context.DbInfo.Description;
                }
                else if (destinationFile == "")
                {
                    destinationFile = "archive";
                }

                string destination = Path.Combine(destinationFolder, destinationFile) + ".zip";

                Md5Context md5 = new Md5Context();
                md5.Init();
                byte[] tmp;
                string mdid = md5.Data(Encoding.UTF8.GetBytes(destination), out tmp);
                Console.WriteLine("MDID: {0}", mdid);

                if (dbCore.DbOps.ExistsOs(mdid))
                {
                    if (File.Exists(destination))
                    {
                        Failed?.Invoke("OS already exists.");
                        return;
                    }

                    Failed?.Invoke("OS already exists in the database but not in the repository, check for inconsistencies.");
                    return;
                }

                if (File.Exists(destination))
                {
                    Failed?.Invoke("OS already exists in the repository but not in the database, check for inconsistencies.");
                    return;
                }

                Context.DbInfo.Mdid = mdid;

                string filesPath;

                if (!string.IsNullOrEmpty(Context.TmpFolder) && Directory.Exists(Context.TmpFolder))
                {
                    filesPath = Context.TmpFolder;
                }
                else
                {
                    filesPath = Context.Path;
                }

                string extension = null;

                switch (Settings.Current.CompressionAlgorithm)
                {
                case AlgoEnum.GZip:
                    extension = ".gz";
                    break;

                case AlgoEnum.BZip2:
                    extension = ".bz2";
                    break;

                case AlgoEnum.LZMA:
                    extension = ".lzma";
                    break;

                case AlgoEnum.LZip:
                    extension = ".lz";
                    break;
                }

                long totalSize = 0, currentSize = 0;
                foreach (KeyValuePair <string, DbOsFile> file in Context.Hashes)
                {
                    totalSize += file.Value.Length;
                }

                #if DEBUG
                stopwatch.Restart();
                #endif
                foreach (KeyValuePair <string, DbOsFile> file in Context.Hashes)
                {
                    UpdateProgress?.Invoke("Compressing...", file.Value.Path, currentSize, totalSize);

                    destinationFolder = Path.Combine(Settings.Current.RepositoryPath, file.Value.Sha256[0].ToString(),
                                                     file.Value.Sha256[1].ToString(), file.Value.Sha256[2].ToString(),
                                                     file.Value.Sha256[3].ToString(), file.Value.Sha256[4].ToString());
                    Directory.CreateDirectory(destinationFolder);

                    destinationFile = Path.Combine(destinationFolder, file.Value.Sha256 + extension);

                    if (!File.Exists(destinationFile))
                    {
                        FileStream inFs = new FileStream(Path.Combine(filesPath, file.Value.Path), FileMode.Open,
                                                         FileAccess.Read);
                        FileStream outFs   = new FileStream(destinationFile, FileMode.CreateNew, FileAccess.Write);
                        Stream     zStream = null;

                        switch (Settings.Current.CompressionAlgorithm)
                        {
                        case AlgoEnum.GZip:
                            zStream = new GZipStream(outFs, CompressionMode.Compress,
                                                     CompressionLevel.BestCompression);
                            break;

                        case AlgoEnum.BZip2:
                            zStream = new BZip2Stream(outFs, CompressionMode.Compress);
                            break;

                        case AlgoEnum.LZMA:
                            zStream = new LzmaStream(new LzmaEncoderProperties(), false, outFs);
                            outFs.Write(((LzmaStream)zStream).Properties, 0,
                                        ((LzmaStream)zStream).Properties.Length);
                            outFs.Write(BitConverter.GetBytes(inFs.Length), 0, 8);
                            break;

                        case AlgoEnum.LZip:
                            zStream = new LZipStream(outFs, CompressionMode.Compress);
                            break;
                        }

                        byte[] buffer = new byte[BUFFER_SIZE];

                        while (inFs.Position + BUFFER_SIZE <= inFs.Length)
                        {
                            UpdateProgress2?.Invoke($"{inFs.Position / (double)inFs.Length:P}",
                                                    $"{inFs.Position} / {inFs.Length} bytes", inFs.Position,
                                                    inFs.Length);
                            UpdateProgress?.Invoke("Compressing...", file.Value.Path, currentSize, totalSize);

                            inFs.Read(buffer, 0, buffer.Length);
                            zStream.Write(buffer, 0, buffer.Length);
                            currentSize += buffer.Length;
                        }

                        buffer = new byte[inFs.Length - inFs.Position];
                        UpdateProgress2?.Invoke($"{inFs.Position / (double)inFs.Length:P}",
                                                $"{inFs.Position} / {inFs.Length} bytes", inFs.Position, inFs.Length);
                        UpdateProgress?.Invoke("Compressing...", file.Value.Path, currentSize, totalSize);

                        inFs.Read(buffer, 0, buffer.Length);
                        zStream.Write(buffer, 0, buffer.Length);
                        currentSize += buffer.Length;

                        UpdateProgress2?.Invoke($"{inFs.Length / (double)inFs.Length:P}", "Finishing...", inFs.Length,
                                                inFs.Length);

                        inFs.Close();
                        zStream.Close();
                        outFs.Dispose();
                    }
                    else
                    {
                        currentSize += file.Value.Length;
                    }
                }
                #if DEBUG
                stopwatch.Stop();
                Console.WriteLine("Core.CompressFiles(): Took {0} seconds to compress files",
                                  stopwatch.Elapsed.TotalSeconds);
                #endif

                if (Context.Metadata != null)
                {
                    MemoryStream  xms = new MemoryStream();
                    XmlSerializer xs  = new XmlSerializer(typeof(CICMMetadataType));
                    xs.Serialize(xms, Context.Metadata);
                    xms.Position = 0;

                    JsonSerializer js = new JsonSerializer
                    {
                        Formatting        = Formatting.Indented,
                        NullValueHandling = NullValueHandling.Ignore
                    };
                    MemoryStream jms = new MemoryStream();
                    StreamWriter sw  = new StreamWriter(jms, Encoding.UTF8, 1048576, true);
                    js.Serialize(sw, Context.Metadata, typeof(CICMMetadataType));
                    sw.Close();
                    jms.Position = 0;

                    destinationFolder = Path.Combine(Settings.Current.RepositoryPath, "metadata", mdid[0].ToString(),
                                                     mdid[1].ToString(), mdid[2].ToString(), mdid[3].ToString(),
                                                     mdid[4].ToString());
                    Directory.CreateDirectory(destinationFolder);

                    FileStream xfs = new FileStream(Path.Combine(destinationFolder, mdid + ".xml"), FileMode.CreateNew,
                                                    FileAccess.Write);
                    xms.CopyTo(xfs);
                    xfs.Close();
                    FileStream jfs = new FileStream(Path.Combine(destinationFolder, mdid + ".json"), FileMode.CreateNew,
                                                    FileAccess.Write);
                    jms.CopyTo(jfs);
                    jfs.Close();

                    xms.Position = 0;
                    jms.Position = 0;
                }

                FinishedWithText?.Invoke($"Correctly added operating system with MDID {mdid}");
            }
            catch (ThreadAbortException) { }
            catch (Exception ex)
            {
                if (Debugger.IsAttached)
                {
                    throw;
                }

                Failed?.Invoke($"Exception {ex.Message}\n{ex.InnerException}");
                #if DEBUG
                Console.WriteLine("Exception {0}\n{1}", ex.Message, ex.InnerException);
                #endif
            }
        }
Ejemplo n.º 20
0
        /// <summary>
        /// Invokes the specified method against the url provided
        /// </summary>
        /// <param name="method">Method.</param>
        /// <param name="url">URL.</param>
        /// <param name="contentType">Content type.</param>
        /// <param name="body">Body.</param>
        /// <param name="query">Query.</param>
        /// <typeparam name="TBody">The 1st type parameter.</typeparam>
        /// <typeparam name="TResult">The 2nd type parameter.</typeparam>
        protected override TResult InvokeInternal <TBody, TResult>(string method, string url, string contentType, WebHeaderCollection additionalHeaders, out WebHeaderCollection responseHeaders, TBody body, NameValueCollection query)
        {
            if (String.IsNullOrEmpty(method))
            {
                throw new ArgumentNullException(nameof(method));
            }
            //if (String.IsNullOrEmpty(url))
            //    throw new ArgumentNullException(nameof(url));

            // Three times:
            // 1. With provided credential
            // 2. With challenge
            // 3. With challenge again
            for (int i = 0; i < 2; i++)
            {
                // Credentials provided ?
                HttpWebRequest requestObj = this.CreateHttpRequest(url, query) as HttpWebRequest;
                if (!String.IsNullOrEmpty(contentType))
                {
                    requestObj.ContentType = contentType;
                }
                requestObj.Method = method;

                // Additional headers
                if (additionalHeaders != null)
                {
                    foreach (var hdr in additionalHeaders.AllKeys)
                    {
                        if (hdr == "If-Modified-Since")
                        {
                            requestObj.IfModifiedSince = DateTime.Parse(additionalHeaders[hdr]);
                        }
                        else
                        {
                            requestObj.Headers.Add(hdr, additionalHeaders[hdr]);
                        }
                    }
                }

#if PERFMON
                Stopwatch sw = new Stopwatch();
                sw.Start();
#endif
                // Body was provided?
                try
                {
                    // Try assigned credentials
                    IBodySerializer serializer = null;
                    if (body != null)
                    {
                        // GET Stream,
                        Stream requestStream = null;
                        try
                        {
                            // Get request object
                            using (var requestTask = Task.Run(async() => { return(await requestObj.GetRequestStreamAsync()); }))
                            {
                                try
                                {
                                    if (!requestTask.Wait(this.Description.Endpoint[0].Timeout))
                                    {
                                        requestObj.Abort();
                                        throw new TimeoutException();
                                    }

                                    requestStream = requestTask.Result;
                                }
                                catch (AggregateException e)
                                {
                                    requestObj.Abort();
                                    throw e.InnerExceptions.First();
                                }
                            }

                            if (contentType == null && typeof(TResult) != typeof(Object))
                            {
                                throw new ArgumentNullException(nameof(contentType));
                            }

                            serializer = this.Description.Binding.ContentTypeMapper.GetSerializer(contentType, typeof(TBody));
                            // Serialize and compress with deflate
                            using (MemoryStream ms = new MemoryStream())
                            {
                                if (this.Description.Binding.Optimize)
                                {
                                    switch ((this.Description.Binding as ServiceClientBinding)?.OptimizationMethod)
                                    {
                                    case OptimizationMethod.Lzma:
                                        requestObj.Headers.Add("Content-Encoding", "lzma");
                                        using (var df = new LZipStream(requestStream, CompressionMode.Compress, leaveOpen: true))
                                            serializer.Serialize(df, body);
                                        break;

                                    case OptimizationMethod.Bzip2:
                                        requestObj.Headers.Add("Content-Encoding", "bzip2");
                                        using (var df = new BZip2Stream(requestStream, CompressionMode.Compress, leaveOpen: true))
                                            serializer.Serialize(df, body);
                                        break;

                                    case OptimizationMethod.Gzip:
                                        requestObj.Headers.Add("Content-Encoding", "gzip");
                                        using (var df = new GZipStream(requestStream, CompressionMode.Compress, leaveOpen: true))
                                            serializer.Serialize(df, body);
                                        break;

                                    case OptimizationMethod.Deflate:
                                        requestObj.Headers.Add("Content-Encoding", "deflate");
                                        using (var df = new DeflateStream(requestStream, CompressionMode.Compress, leaveOpen: true))
                                            serializer.Serialize(df, body);
                                        break;

                                    case OptimizationMethod.None:
                                    default:
                                        serializer.Serialize(ms, body);
                                        break;
                                    }
                                }
                                else
                                {
                                    serializer.Serialize(ms, body);
                                }

                                // Trace
                                if (this.Description.Trace)
                                {
                                    this.m_tracer.TraceVerbose("HTTP >> {0}", Convert.ToBase64String(ms.ToArray()));
                                }

                                using (var nms = new MemoryStream(ms.ToArray()))
                                    nms.CopyTo(requestStream);
                            }
                        }
                        finally
                        {
                            if (requestStream != null)
                            {
                                requestStream.Dispose();
                            }
                        }
                    }

                    // Response
                    HttpWebResponse response = null;
                    try
                    {
                        // Get request object
                        using (var responseTask = Task.Run(async() => { return(await requestObj.GetResponseAsync()); }))
                        {
                            try
                            {
                                if (!responseTask.Wait(this.Description.Endpoint[0].Timeout))
                                {
                                    requestObj.Abort();
                                    throw new TimeoutException();
                                }
                                response = (HttpWebResponse)responseTask.Result;
                            }
                            catch (AggregateException e)
                            {
                                requestObj.Abort();
                                throw e.InnerExceptions.First();
                            }
                        }


#if PERFMON
                        sw.Stop();
                        ApplicationContext.Current.PerformanceLog(nameof(RestClient), "InvokeInternal", $"{nameof(TBody)}-RCV", sw.Elapsed);
                        sw.Reset();
                        sw.Start();
#endif

                        responseHeaders = response.Headers;
                        var validationResult = this.ValidateResponse(response);
                        if (validationResult != ServiceClientErrorType.Valid)
                        {
                            this.m_tracer.TraceError("Response failed validation : {0}", validationResult);
                            throw new WebException(Strings.err_response_failed_validation, null, WebExceptionStatus.Success, response);
                        }

                        // No content - does the result want a pointer maybe?
                        if (response.StatusCode == HttpStatusCode.NoContent)
                        {
                            return(default(TResult));
                        }
                        else
                        {
                            // De-serialize
                            var responseContentType = response.ContentType;
                            if (String.IsNullOrEmpty(responseContentType))
                            {
                                return(default(TResult));
                            }

                            if (responseContentType.Contains(";"))
                            {
                                responseContentType = responseContentType.Substring(0, responseContentType.IndexOf(";"));
                            }

                            if (response.StatusCode == HttpStatusCode.NotModified)
                            {
                                return(default(TResult));
                            }

                            serializer = this.Description.Binding.ContentTypeMapper.GetSerializer(responseContentType, typeof(TResult));

                            TResult retVal = default(TResult);
                            // Compression?
                            using (MemoryStream ms = new MemoryStream())
                            {
                                if (this.Description.Trace)
                                {
                                    this.m_tracer.TraceVerbose("Received response {0} : {1} bytes", response.ContentType, response.ContentLength);
                                }

                                response.GetResponseStream().CopyTo(ms);

#if PERFMON
                                sw.Stop();
                                ApplicationContext.Current.PerformanceLog(nameof(RestClient), "InvokeInternal", $"{nameof(TBody)}-INT", sw.Elapsed);
                                sw.Reset();
                                sw.Start();
#endif

                                ms.Seek(0, SeekOrigin.Begin);

                                // Trace
                                if (this.Description.Trace)
                                {
                                    this.m_tracer.TraceVerbose("HTTP << {0}", Convert.ToBase64String(ms.ToArray()));
                                }

                                switch (response.Headers[HttpResponseHeader.ContentEncoding])
                                {
                                case "deflate":
                                    using (DeflateStream df = new DeflateStream(ms, CompressionMode.Decompress, leaveOpen: true))
                                        retVal = (TResult)serializer.DeSerialize(df);
                                    break;

                                case "gzip":
                                    using (GZipStream df = new GZipStream(ms, CompressionMode.Decompress, leaveOpen: true))
                                        retVal = (TResult)serializer.DeSerialize(df);
                                    break;

                                case "bzip2":
                                    using (var bzs = new BZip2Stream(ms, CompressionMode.Decompress, leaveOpen: true))
                                        retVal = (TResult)serializer.DeSerialize(bzs);
                                    break;

                                case "lzma":
                                    using (var lzmas = new LZipStream(ms, CompressionMode.Decompress, leaveOpen: true))
                                        retVal = (TResult)serializer.DeSerialize(lzmas);
                                    break;

                                default:
                                    retVal = (TResult)serializer.DeSerialize(ms);
                                    break;
                                }
                                //retVal = (TResult)serializer.DeSerialize(ms);
                            }

#if PERFMON
                            sw.Stop();
                            ApplicationContext.Current.PerformanceLog(nameof(RestClient), "InvokeInternal", $"{nameof(TBody)}-RET", sw.Elapsed);
                            sw.Reset();
                            sw.Start();
#endif

                            return(retVal);
                        }
                    }
                    finally
                    {
                        if (response != null)
                        {
                            response.Close();
                            response.Dispose();
                        }
                        //responseTask.Dispose();
                    }
                }
                catch (TimeoutException e)
                {
                    this.m_tracer.TraceError("Request timed out:{0}", e.Message);
                    throw;
                }
                catch (WebException e)
                {
                    var errorResponse = (e.Response as HttpWebResponse);
                    if (errorResponse?.StatusCode == HttpStatusCode.NotModified)
                    {
                        this.m_tracer.TraceInfo("Server indicates not modified {0} {1} : {2}", method, url, e.Message);
                        responseHeaders = errorResponse?.Headers;
                        return(default(TResult));
                    }

                    this.m_tracer.TraceError("Error executing {0} {1} : {2}", method, url, e.Message);

                    // status
                    switch (e.Status)
                    {
                    case WebExceptionStatus.ProtocolError:

                        // Deserialize
                        object errorResult = default(ErrorResult);

                        var responseContentType = errorResponse.ContentType;
                        if (responseContentType.Contains(";"))
                        {
                            responseContentType = responseContentType.Substring(0, responseContentType.IndexOf(";"));
                        }

                        var ms = new MemoryStream();     // copy response to memory
                        errorResponse.GetResponseStream().CopyTo(ms);
                        ms.Seek(0, SeekOrigin.Begin);

                        try
                        {
                            var serializer = this.Description.Binding.ContentTypeMapper.GetSerializer(responseContentType, typeof(TResult));

                            try
                            {
                                switch (errorResponse.Headers[HttpResponseHeader.ContentEncoding])
                                {
                                case "deflate":
                                    using (DeflateStream df = new DeflateStream(ms, CompressionMode.Decompress, leaveOpen: true))
                                        errorResult = (TResult)serializer.DeSerialize(df);
                                    break;

                                case "gzip":
                                    using (GZipStream df = new GZipStream(ms, CompressionMode.Decompress, leaveOpen: true))
                                        errorResult = (TResult)serializer.DeSerialize(df);
                                    break;

                                case "bzip2":
                                    using (var bzs = new BZip2Stream(ms, CompressionMode.Decompress, leaveOpen: true))
                                        errorResult = (TResult)serializer.DeSerialize(bzs);
                                    break;

                                case "lzma":
                                    using (var lzmas = new LZipStream(ms, CompressionMode.Decompress, leaveOpen: true))
                                        errorResult = (TResult)serializer.DeSerialize(lzmas);
                                    break;

                                default:
                                    errorResult = (TResult)serializer.DeSerialize(ms);
                                    break;
                                }
                            }
                            catch
                            {
                                serializer = this.Description.Binding.ContentTypeMapper.GetSerializer(responseContentType, typeof(ErrorResult));

                                ms.Seek(0, SeekOrigin.Begin);     // rewind and try generic error codes
                                switch (errorResponse.Headers[HttpResponseHeader.ContentEncoding])
                                {
                                case "deflate":
                                    using (DeflateStream df = new DeflateStream(ms, CompressionMode.Decompress, leaveOpen: true))
                                        errorResult = (ErrorResult)serializer.DeSerialize(df);
                                    break;

                                case "gzip":
                                    using (GZipStream df = new GZipStream(ms, CompressionMode.Decompress, leaveOpen: true))
                                        errorResult = (ErrorResult)serializer.DeSerialize(df);
                                    break;

                                case "bzip2":
                                    using (var bzs = new BZip2Stream(ms, CompressionMode.Decompress, leaveOpen: true))
                                        errorResult = (ErrorResult)serializer.DeSerialize(bzs);
                                    break;

                                case "lzma":
                                    using (var lzmas = new LZipStream(ms, CompressionMode.Decompress, leaveOpen: true))
                                        errorResult = (ErrorResult)serializer.DeSerialize(lzmas);
                                    break;

                                default:
                                    errorResult = (ErrorResult)serializer.DeSerialize(ms);
                                    break;
                                }
                            }
                            //result = (TResult)serializer.DeSerialize(errorResponse.GetResponseStream());
                        }
                        catch (Exception dse)
                        {
                            this.m_tracer.TraceError("Could not de-serialize error response! {0}", dse.Message);
                        }

                        Exception exception = null;
                        if (errorResult is TResult)
                        {
                            exception = new RestClientException <TResult>((TResult)errorResult, e, e.Status, e.Response);
                        }
                        else
                        {
                            exception = new RestClientException <ErrorResult>((ErrorResult)errorResult, e, e.Status, e.Response);
                        }

                        switch (errorResponse.StatusCode)
                        {
                        case HttpStatusCode.Unauthorized:         // Validate the response
                            if (this.ValidateResponse(errorResponse) != ServiceClientErrorType.Valid)
                            {
                                throw exception;
                            }
                            break;

                        case HttpStatusCode.NotModified:
                            responseHeaders = errorResponse?.Headers;
                            return(default(TResult));

                        default:
                            throw exception;
                        }
                        break;

                    case WebExceptionStatus.ConnectFailure:
                        if ((e.InnerException as SocketException)?.SocketErrorCode == SocketError.TimedOut)
                        {
                            throw new TimeoutException();
                        }
                        else
                        {
                            throw;
                        }

                    case WebExceptionStatus.Timeout:
                        throw new TimeoutException();

                    default:
                        throw;
                    }
                }
            }

            responseHeaders = new WebHeaderCollection();
            return(default(TResult));
        }
Ejemplo n.º 21
0
        /// <summary>
        /// Invokes the specified method against the url provided
        /// </summary>
        /// <param name="method">Method.</param>
        /// <param name="url">URL.</param>
        /// <param name="contentType">Content type.</param>
        /// <param name="body">Body.</param>
        /// <param name="query">Query.</param>
        /// <typeparam name="TBody">The 1st type parameter.</typeparam>
        /// <typeparam name="TResult">The 2nd type parameter.</typeparam>
        protected override TResult InvokeInternal <TBody, TResult>(string method, string url, string contentType, WebHeaderCollection requestHeaders, out WebHeaderCollection responseHeaders, TBody body, NameValueCollection query)
        {
            if (String.IsNullOrEmpty(method))
            {
                throw new ArgumentNullException(nameof(method));
            }
            if (String.IsNullOrEmpty(url))
            {
                throw new ArgumentNullException(nameof(url));
            }

            // Three times:
            // 1. With provided credential
            // 2. With challenge
            // 3. With challenge again
            for (int i = 0; i < 2; i++)
            {
                // Credentials provided ?
                HttpWebRequest requestObj = this.CreateHttpRequest(url, query) as HttpWebRequest;
                if (!String.IsNullOrEmpty(contentType))
                {
                    requestObj.ContentType = contentType;
                }
                requestObj.Method = method;

                // Additional headers
                if (requestHeaders != null)
                {
                    foreach (var hdr in requestHeaders.AllKeys)
                    {
                        if (hdr == "If-Modified-Since")
                        {
                            requestObj.IfModifiedSince = DateTime.Parse(requestHeaders[hdr]);
                        }
                        else
                        {
                            requestObj.Headers.Add(hdr, requestHeaders[hdr]);
                        }
                    }
                }

                // Body was provided?
                try
                {
                    // Try assigned credentials
                    IBodySerializer serializer = null;
                    if (body != null)
                    {
                        // GET Stream,
                        Stream    requestStream    = null;
                        Exception requestException = null;

                        try
                        {
                            //requestStream = requestObj.GetRequestStream();
                            var requestTask = requestObj.GetRequestStreamAsync().ContinueWith(r =>
                            {
                                if (r.IsFaulted)
                                {
                                    requestException = r.Exception.InnerExceptions.First();
                                }
                                else
                                {
                                    requestStream = r.Result;
                                }
                            }, TaskContinuationOptions.LongRunning);

                            if (!requestTask.Wait(this.Description.Endpoint[0].Timeout))
                            {
                                throw new TimeoutException();
                            }
                            else if (requestException != null)
                            {
                                throw requestException;
                            }

                            if (contentType == null)
                            {
                                throw new ArgumentNullException(nameof(contentType));
                            }

                            serializer = this.Description.Binding.ContentTypeMapper.GetSerializer(contentType, typeof(TBody));
                            // Serialize and compress with deflate
                            if (this.Description.Binding.Optimize)
                            {
#if HTTP_LZMA
                                requestObj.Headers.Add("Content-Encoding", "lzma");
                                using (var df = new LZipStream(requestStream, CompressionMode.Compress, leaveOpen: true))
                                    serializer.Serialize(df, body);
#elif HTTP_GZ
                                requestObj.Headers.Add("Content-Encoding", "gzip");
                                using (var df = new GZipStream(requestStream, CompressionMode.Compress, leaveOpen: true))
                                    serializer.Serialize(df, body);
#else
                                requestObj.Headers.Add("Content-Encoding", "deflate");
                                using (var df = new DeflateStream(requestStream, CompressionMode.Compress, leaveOpen: true))
                                    serializer.Serialize(df, body);
#endif
                            }
                            else
                            {
                                serializer.Serialize(requestStream, body);
                            }
                        }
                        finally
                        {
                            if (requestStream != null)
                            {
                                requestStream.Dispose();
                            }
                        }
                    }

                    // Response
                    HttpWebResponse response      = null;
                    Exception       responseError = null;

                    try
                    {
                        var responseTask = requestObj.GetResponseAsync().ContinueWith(r =>
                        {
                            if (r.IsFaulted)
                            {
                                responseError = r.Exception.InnerExceptions.First();
                            }
                            else
                            {
                                response = r.Result as HttpWebResponse;
                            }
                        }, TaskContinuationOptions.LongRunning);

                        if (!responseTask.Wait(this.Description.Endpoint[0].Timeout))
                        {
                            throw new TimeoutException();
                        }
                        else
                        {
                            if (responseError != null)
                            {
                                responseHeaders = new WebHeaderCollection();
                                if (((responseError as WebException)?.Response as HttpWebResponse)?.StatusCode == HttpStatusCode.NotModified)
                                {
                                    return(default(TResult));
                                }
                                else
                                {
                                    throw responseError;
                                }
                            }
                            responseHeaders = response.Headers;
                        }

                        var validationResult = this.ValidateResponse(response);
                        if (validationResult != ServiceClientErrorType.Valid)
                        {
                            this.traceSource.TraceEvent(TraceEventType.Error, 0, "Response failed validation : {0}", validationResult);
                            throw new WebException("Response failed validation", null, WebExceptionStatus.Success, response);
                        }
                        // De-serialize
                        var responseContentType = response.ContentType;
                        if (responseContentType.Contains(";"))
                        {
                            responseContentType = responseContentType.Substring(0, responseContentType.IndexOf(";"));
                        }

                        if (response.StatusCode == HttpStatusCode.NotModified ||
                            response.StatusCode == HttpStatusCode.NoContent ||
                            typeof(TResult) == typeof(object))
                        {
                            return(default(TResult));
                        }
                        serializer = this.Description.Binding.ContentTypeMapper.GetSerializer(responseContentType, typeof(TResult));

                        TResult retVal = default(TResult);
                        using (var ms = new MemoryStream())
                        {
                            response.GetResponseStream().CopyTo(ms);
                            ms.Seek(0, SeekOrigin.Begin);
                            // Compression?
                            switch (response.Headers[HttpResponseHeader.ContentEncoding])
                            {
                            case "deflate":
                                using (DeflateStream df = new DeflateStream(ms, CompressionMode.Decompress, leaveOpen: true))
                                    retVal = (TResult)serializer.DeSerialize(df);
                                break;

                            case "gzip":
                                using (GZipStream df = new GZipStream(ms, CompressionMode.Decompress, leaveOpen: true))
                                    retVal = (TResult)serializer.DeSerialize(df);
                                break;

                            case "bzip2":
                                using (var bzs = new BZip2Stream(ms, CompressionMode.Decompress, leaveOpen: true))
                                    retVal = (TResult)serializer.DeSerialize(bzs);
                                break;

                            case "lzma":
                                using (var lzmas = new LZipStream(ms, CompressionMode.Decompress, leaveOpen: true))
                                    retVal = (TResult)serializer.DeSerialize(lzmas);

                                break;

                            default:
                                retVal = (TResult)serializer.DeSerialize(ms);
                                break;
                            }
                        }

                        return(retVal);
                    }
                    finally
                    {
                        if (response != null)
                        {
                            response.Dispose();
                        }
                    }
                }
                catch (TimeoutException e)
                {
                    this.traceSource.TraceEvent(TraceEventType.Error, 0, "Request timed out:{0}", e);
                    throw;
                }
                catch (WebException e)
                {
                    this.traceSource.TraceEvent(TraceEventType.Error, 0, e.ToString());

                    // status
                    switch (e.Status)
                    {
                    case WebExceptionStatus.ProtocolError:

                        // Deserialize
                        TResult result              = default(TResult);
                        var     errorResponse       = (e.Response as HttpWebResponse);
                        var     responseContentType = errorResponse.ContentType;
                        if (responseContentType.Contains(";"))
                        {
                            responseContentType = responseContentType.Substring(0, responseContentType.IndexOf(";"));
                        }
                        try
                        {
                            var serializer = this.Description.Binding.ContentTypeMapper.GetSerializer(responseContentType, typeof(TResult));
                            switch (errorResponse.Headers[HttpResponseHeader.ContentEncoding])
                            {
                            case "deflate":
                                using (DeflateStream df = new DeflateStream(errorResponse.GetResponseStream(), CompressionMode.Decompress, leaveOpen: true))
                                    result = (TResult)serializer.DeSerialize(df);
                                break;

                            case "gzip":
                                using (GZipStream df = new GZipStream(errorResponse.GetResponseStream(), CompressionMode.Decompress, leaveOpen: true))
                                    result = (TResult)serializer.DeSerialize(df);
                                break;

                            case "bzip2":
                                using (var bzs = new BZip2Stream(errorResponse.GetResponseStream(), CompressionMode.Decompress, leaveOpen: true))
                                    result = (TResult)serializer.DeSerialize(bzs);
                                break;

                            case "lzma":
                                using (var lzmas = new LZipStream(errorResponse.GetResponseStream(), CompressionMode.Decompress, leaveOpen: true))
                                    result = (TResult)serializer.DeSerialize(lzmas);

                                break;

                            default:
                                result = (TResult)serializer.DeSerialize(errorResponse.GetResponseStream());
                                break;
                            }
                        }
                        catch (Exception dse)
                        {
                            this.traceSource.TraceEvent(TraceEventType.Error, 0, "Could not de-serialize error response! {0}", dse);
                        }

                        switch (errorResponse.StatusCode)
                        {
                        case HttpStatusCode.Unauthorized:         // Validate the response
                            if (this.ValidateResponse(errorResponse) != ServiceClientErrorType.Valid)
                            {
                                throw new RestClientException <TResult>(
                                          result,
                                          e,
                                          e.Status,
                                          e.Response);
                            }

                            break;

                        default:
                            throw new RestClientException <TResult>(
                                      result,
                                      e,
                                      e.Status,
                                      e.Response);
                        }
                        break;

                    default:
                        throw;
                    }
                }
            }

            responseHeaders = new WebHeaderCollection();
            return(default(TResult));
        }
Ejemplo n.º 22
0
 /// <summary>
 /// Unpack the package
 /// </summary>
 public AppletManifest Unpack()
 {
     using (MemoryStream ms = new MemoryStream(this.Manifest))
         using (LZipStream gs = new LZipStream(new NonDisposingStream(ms), CompressionMode.Decompress))
             return(AppletManifest.Load(gs));
 }
Ejemplo n.º 23
0
        /// <summary>
        /// Opens a Reader for Non-seeking usage
        /// </summary>
        /// <param name="stream"></param>
        /// <param name="options"></param>
        /// <returns></returns>
        public static IReader Open(Stream stream, ReaderOptions options = null)
        {
            stream.CheckNotNull("stream");
            options = options ?? new ReaderOptions()
            {
                LeaveStreamOpen = false
            };
            RewindableStream rewindableStream = new RewindableStream(stream);

            rewindableStream.StartRecording();
            if (ZipArchive.IsZipFile(rewindableStream, options.Password))
            {
                rewindableStream.Rewind(true);
                return(ZipReader.Open(rewindableStream, options));
            }
            rewindableStream.Rewind(false);
            if (GZipArchive.IsGZipFile(rewindableStream))
            {
                rewindableStream.Rewind(false);
                GZipStream testStream = new GZipStream(rewindableStream, CompressionMode.Decompress);
                if (TarArchive.IsTarFile(testStream))
                {
                    rewindableStream.Rewind(true);
                    return(new TarReader(rewindableStream, options, CompressionType.GZip));
                }
                rewindableStream.Rewind(true);
                return(GZipReader.Open(rewindableStream, options));
            }

            rewindableStream.Rewind(false);
            if (BZip2Stream.IsBZip2(rewindableStream))
            {
                rewindableStream.Rewind(false);
                BZip2Stream testStream = new BZip2Stream(rewindableStream, CompressionMode.Decompress, true);
                if (TarArchive.IsTarFile(testStream))
                {
                    rewindableStream.Rewind(true);
                    return(new TarReader(rewindableStream, options, CompressionType.BZip2));
                }
            }

            rewindableStream.Rewind(false);
            if (LZipStream.IsLZipFile(rewindableStream))
            {
                rewindableStream.Rewind(false);
                LZipStream testStream = new LZipStream(rewindableStream, CompressionMode.Decompress, true);
                if (TarArchive.IsTarFile(testStream))
                {
                    rewindableStream.Rewind(true);
                    return(new TarReader(rewindableStream, options, CompressionType.LZip));
                }
            }
            rewindableStream.Rewind(false);
            if (RarArchive.IsRarFile(rewindableStream, options))
            {
                rewindableStream.Rewind(true);
                return(RarReader.Open(rewindableStream, options));
            }

            rewindableStream.Rewind(false);
            if (TarArchive.IsTarFile(rewindableStream))
            {
                rewindableStream.Rewind(true);
                return(TarReader.Open(rewindableStream, options));
            }
            rewindableStream.Rewind(false);
            if (XZStream.IsXZStream(rewindableStream))
            {
                rewindableStream.Rewind(true);
                XZStream testStream = new XZStream(rewindableStream);
                if (TarArchive.IsTarFile(testStream))
                {
                    rewindableStream.Rewind(true);
                    return(new TarReader(rewindableStream, options, CompressionType.Xz));
                }
            }
            throw new InvalidOperationException("Cannot determine compressed stream type.  Supported Reader Formats: Zip, GZip, BZip2, Tar, Rar, LZip, XZ");
        }
Ejemplo n.º 24
0
        /// <summary>
        /// Performs an installation
        /// </summary>
        public virtual bool Install(AppletPackage package, bool isUpgrade = false)
        {
            this.m_tracer.TraceWarning("Installing {0}", package.Meta);

            // TODO: Verify package hash / signature
            if (!this.VerifyPackage(package))
            {
                throw new SecurityException("Applet failed validation");
            }
            else if (!this.m_appletCollection.VerifyDependencies(package.Meta))
            {
                this.m_tracer.TraceWarning($"Applet {package.Meta} depends on : [{String.Join(", ", package.Meta.Dependencies.Select(o => o.ToString()))}] which are missing or incompatible");
            }
            var    appletSection = ApplicationContext.Current.Configuration.GetSection <AppletConfigurationSection>();
            String appletPath    = Path.Combine(appletSection.AppletDirectory, package.Meta.Id);

            try
            {
                // Desearialize an prep for install

                this.m_tracer.TraceInfo("Installing applet {0} (IsUpgrade={1})", package.Meta, isUpgrade);

                ApplicationContext.Current.SetProgress(package.Meta.GetName("en"), 0.0f);
                // TODO: Verify the package

                // Copy
                if (!Directory.Exists(appletSection.AppletDirectory))
                {
                    Directory.CreateDirectory(appletSection.AppletDirectory);
                }

                if (File.Exists(appletPath))
                {
                    if (!isUpgrade)
                    {
                        throw new InvalidOperationException(Strings.err_duplicate_package_name);
                    }

                    // Unload the loaded applet version
                    var existingApplet = this.m_appletCollection.FirstOrDefault(o => o.Info.Id == package.Meta.Id);
                    if (existingApplet != null)
                    {
                        this.UnInstallInternal(existingApplet);
                    }
                }

                var mfst = package.Unpack();
                // Migrate data.
                if (mfst.DataSetup != null)
                {
                    foreach (var itm in mfst.DataSetup.Action)
                    {
                        Type idpType = typeof(IDataPersistenceService <>);
                        idpType = idpType.MakeGenericType(new Type[] { itm.Element.GetType() });
                        var svc = ApplicationContext.Current.GetService(idpType);
                        idpType.GetMethod(itm.ActionName).Invoke(svc, new object[] { itm.Element, TransactionMode.Commit, AuthenticationContext.SystemPrincipal });
                    }
                }

                // Now export all the binary files out
                var assetDirectory = Path.Combine(appletSection.AppletDirectory, "assets", mfst.Info.Id);
                if (!Directory.Exists(assetDirectory))
                {
                    Directory.CreateDirectory(assetDirectory);
                }
                else
                {
                    Directory.Delete(assetDirectory, true);
                }

                for (int i = 0; i < mfst.Assets.Count; i++)
                {
                    var itm     = mfst.Assets[i];
                    var itmPath = Path.Combine(assetDirectory, itm.Name);
                    ApplicationContext.Current.SetProgress($"Installing {package.Meta.GetName("en")}", 0.1f + (float)(0.8 * (float)i / mfst.Assets.Count));

                    // Get dir name and create
                    if (!Directory.Exists(Path.GetDirectoryName(itmPath)))
                    {
                        Directory.CreateDirectory(Path.GetDirectoryName(itmPath));
                    }

                    // Extract content
                    if (itm.Content is byte[])
                    {
                        if (Encoding.UTF8.GetString(itm.Content as byte[], 0, 4) == "LZIP")
                        {
                            using (var fs = File.Create(itmPath))
                                using (var ms = new MemoryStream(itm.Content as byte[]))
                                    using (var lzs = new LZipStream(new NonDisposingStream(ms), SharpCompress.Compressors.CompressionMode.Decompress))
                                        lzs.CopyTo(fs);
                        }
                        else
                        {
                            File.WriteAllBytes(itmPath, itm.Content as byte[]);
                        }
                        itm.Content = null;
                    }
                    else if (itm.Content is String)
                    {
                        File.WriteAllText(itmPath, itm.Content as String);
                        itm.Content = null;
                    }
                }

                // Serialize the data to disk
                using (FileStream fs = File.Create(appletPath))
                    mfst.Save(fs);

                // For now sign with SHA256
                SHA256 sha = SHA256.Create();
                package.Meta.Hash = sha.ComputeHash(File.ReadAllBytes(appletPath));
                // HACK: Re-re-remove
                appletSection.Applets.RemoveAll(o => o.Id == package.Meta.Id);
                appletSection.Applets.Add(package.Meta.AsReference());

                ApplicationContext.Current.SetProgress(package.Meta.GetName("en"), 0.98f);

                if (ApplicationContext.Current.ConfigurationPersister.IsConfigured)
                {
                    ApplicationContext.Current.ConfigurationPersister.Save(ApplicationContext.Current.Configuration);
                }

                this.LoadApplet(mfst);
            }
            catch (Exception e)
            {
                this.m_tracer.TraceError("Error installing applet {0} : {1}", package.Meta.ToString(), e);

                // Remove
                if (File.Exists(appletPath))
                {
                    File.Delete(appletPath);
                }

                throw;
            }

            return(true);
        }
Ejemplo n.º 25
0
        /// <summary>
        /// Invokes the specified method against the url provided
        /// </summary>
        /// <param name="method">Method.</param>
        /// <param name="url">URL.</param>
        /// <param name="contentType">Content type.</param>
        /// <param name="body">Body.</param>
        /// <param name="query">Query.</param>
        /// <typeparam name="TBody">The 1st type parameter.</typeparam>
        /// <typeparam name="TResult">The 2nd type parameter.</typeparam>
        protected override TResult InvokeInternal <TBody, TResult>(string method, string url, string contentType, WebHeaderCollection additionalHeaders, out WebHeaderCollection responseHeaders, TBody body, NameValueCollection query)
        {
            if (String.IsNullOrEmpty(method))
            {
                throw new ArgumentNullException(nameof(method));
            }
            //if (String.IsNullOrEmpty(url))
            //    throw new ArgumentNullException(nameof(url));


            // Credentials provided ?
            HttpWebRequest requestObj = this.CreateHttpRequest(url, query) as HttpWebRequest;

            if (!String.IsNullOrEmpty(contentType))
            {
                requestObj.ContentType = contentType;
            }
            requestObj.Method = method;

            // Additional headers
            if (additionalHeaders != null)
            {
                foreach (var hdr in additionalHeaders.AllKeys)
                {
                    if (hdr == "If-Modified-Since")
                    {
                        requestObj.IfModifiedSince = DateTime.Parse(additionalHeaders[hdr]);
                    }
                    else
                    {
                        requestObj.Headers.Add(hdr, additionalHeaders[hdr]);
                    }
                }
            }

#if PERFMON
            Stopwatch sw = new Stopwatch();
            sw.Start();
#endif
            // Get request object

            // Body was provided?
            try
            {
                // Try assigned credentials
                IBodySerializer serializer = null;
                if (body != null)
                {
                    // GET Stream,
                    Stream requestStream = null;
                    try
                    {
                        // Get request object
                        var cancellationTokenSource = new CancellationTokenSource();
                        cancellationTokenSource.CancelAfter(this.Description.Endpoint[0].Timeout);
                        using (var requestTask = Task.Run(async() => { return(await requestObj.GetRequestStreamAsync()); }, cancellationTokenSource.Token))
                        {
                            try
                            {
                                requestStream = requestTask.Result;
                            }
                            catch (AggregateException e)
                            {
                                requestObj.Abort();
                                throw e.InnerExceptions.First();
                            }
                        }

                        if (contentType == null && typeof(TResult) != typeof(Object))
                        {
                            throw new ArgumentNullException(nameof(contentType));
                        }

                        serializer = this.Description.Binding.ContentTypeMapper.GetSerializer(contentType, typeof(TBody));
                        // Serialize and compress with deflate
                        using (MemoryStream ms = new MemoryStream())
                        {
                            if (this.Description.Binding.Optimize)
                            {
                                switch ((this.Description.Binding as ServiceClientBinding)?.OptimizationMethod)
                                {
                                case OptimizationMethod.Lzma:
                                    requestObj.Headers.Add("Content-Encoding", "lzma");
                                    using (var df = new LZipStream(new NonDisposingStream(requestStream), CompressionMode.Compress))
                                        serializer.Serialize(df, body);
                                    break;

                                case OptimizationMethod.Bzip2:
                                    requestObj.Headers.Add("Content-Encoding", "bzip2");
                                    using (var df = new BZip2Stream(new NonDisposingStream(requestStream), CompressionMode.Compress, false))
                                        serializer.Serialize(df, body);
                                    break;

                                case OptimizationMethod.Gzip:
                                    requestObj.Headers.Add("Content-Encoding", "gzip");
                                    using (var df = new GZipStream(new NonDisposingStream(requestStream), CompressionMode.Compress))
                                        serializer.Serialize(df, body);
                                    break;

                                case OptimizationMethod.Deflate:
                                    requestObj.Headers.Add("Content-Encoding", "deflate");
                                    using (var df = new DeflateStream(new NonDisposingStream(requestStream), CompressionMode.Compress))
                                        serializer.Serialize(df, body);
                                    break;

                                case OptimizationMethod.None:
                                default:
                                    serializer.Serialize(ms, body);
                                    break;
                                }
                            }
                            else
                            {
                                serializer.Serialize(ms, body);
                            }

                            // Trace
                            if (this.Description.Trace)
                            {
                                this.m_tracer.TraceVerbose("HTTP >> {0}", Convert.ToBase64String(ms.ToArray()));
                            }

                            using (var nms = new MemoryStream(ms.ToArray()))
                                nms.CopyTo(requestStream);
                        }
                    }
                    finally
                    {
                        if (requestStream != null)
                        {
                            requestStream.Dispose();
                        }
                    }
                }

                // Response
                HttpWebResponse response = null;
                try
                {
                    var cancellationTokenSource = new CancellationTokenSource();
                    cancellationTokenSource.CancelAfter(this.Description.Endpoint[0].Timeout);
                    using (var responseTask = Task.Run(async() => { return(await requestObj.GetResponseAsync()); }, cancellationTokenSource.Token))
                    {
                        try
                        {
                            response = (HttpWebResponse)responseTask.Result;
                        }
                        catch (AggregateException e)
                        {
                            requestObj.Abort();
                            throw e.InnerExceptions.First();
                        }
                    }

                    responseHeaders = response.Headers;

                    // No content - does the result want a pointer maybe?
                    if (response.StatusCode == HttpStatusCode.NoContent || response.StatusCode == HttpStatusCode.Continue || response.StatusCode == HttpStatusCode.NotModified)
                    {
                        return(default(TResult));
                    }
                    else if (response.StatusCode == HttpStatusCode.RedirectKeepVerb)
                    {
                        return(this.InvokeInternal <TBody, TResult>(method, response.Headers[HttpResponseHeader.Location], contentType, additionalHeaders, out responseHeaders, body, query));
                    }
                    else if (response.StatusCode == HttpStatusCode.RedirectMethod)
                    {
                        return(this.InvokeInternal <TBody, TResult>("GET", response.Headers[HttpResponseHeader.Location], contentType, additionalHeaders, out responseHeaders, default(TBody), query));
                    }
                    else
                    {
                        // De-serialize
                        var responseContentType = response.ContentType;
                        if (String.IsNullOrEmpty(responseContentType))
                        {
                            return(default(TResult));
                        }

                        if (responseContentType.Contains(";"))
                        {
                            responseContentType = responseContentType.Substring(0, responseContentType.IndexOf(";"));
                        }

                        if (response.StatusCode == HttpStatusCode.NotModified)
                        {
                            return(default(TResult));
                        }

                        serializer = this.Description.Binding.ContentTypeMapper.GetSerializer(responseContentType, typeof(TResult));

                        TResult retVal = default(TResult);
                        // Compression?
                        using (MemoryStream ms = new MemoryStream())
                        {
                            if (this.Description.Trace)
                            {
                                this.m_tracer.TraceVerbose("Received response {0} : {1} bytes", response.ContentType, response.ContentLength);
                            }

                            response.GetResponseStream().CopyTo(ms);

                            ms.Seek(0, SeekOrigin.Begin);

                            // Trace
                            if (this.Description.Trace)
                            {
                                this.m_tracer.TraceVerbose("HTTP << {0}", Convert.ToBase64String(ms.ToArray()));
                            }

                            switch (response.Headers[HttpResponseHeader.ContentEncoding])
                            {
                            case "deflate":
                                using (DeflateStream df = new DeflateStream(new NonDisposingStream(ms), CompressionMode.Decompress))
                                    retVal = (TResult)serializer.DeSerialize(df);
                                break;

                            case "gzip":
                                using (GZipStream df = new GZipStream(new NonDisposingStream(ms), CompressionMode.Decompress))
                                    retVal = (TResult)serializer.DeSerialize(df);
                                break;

                            case "bzip2":
                                using (var bzs = new BZip2Stream(new NonDisposingStream(ms), CompressionMode.Decompress, false))
                                    retVal = (TResult)serializer.DeSerialize(bzs);
                                break;

                            case "lzma":
                                using (var lzmas = new LZipStream(new NonDisposingStream(ms), CompressionMode.Decompress))
                                    retVal = (TResult)serializer.DeSerialize(lzmas);
                                break;

                            default:
                                retVal = (TResult)serializer.DeSerialize(ms);
                                break;
                            }
                            //retVal = (TResult)serializer.DeSerialize(ms);
                        }

                        return(retVal);
                    }
                }
                finally
                {
                    if (response != null)
                    {
                        response.Close();
                        response.Dispose();
                    }
                    //responseTask.Dispose();
                }
            }
            catch (TimeoutException e)
            {
                this.m_tracer.TraceError("Request timed out:{0}", e.Message);
                throw;
            }
            catch (WebException e) when(e.Response is HttpWebResponse errorResponse && errorResponse.StatusCode == HttpStatusCode.NotModified)
            {
                this.m_tracer.TraceInfo("Server indicates not modified {0} {1} : {2}", method, url, e.Message);
                responseHeaders = errorResponse?.Headers;
                return(default(TResult));
            }
            catch (WebException e) when(e.Response is HttpWebResponse errorResponse && e.Status == WebExceptionStatus.ProtocolError)
            {
                this.m_tracer.TraceError("Error executing {0} {1} : {2}", method, url, e.Message);
                // Deserialize
                object errorResult = null;

                var responseContentType = errorResponse.ContentType;

                if (responseContentType.Contains(";"))
                {
                    responseContentType = responseContentType.Substring(0, responseContentType.IndexOf(";"));
                }

                var ms = new MemoryStream(); // copy response to memory

                errorResponse.GetResponseStream().CopyTo(ms);
                ms.Seek(0, SeekOrigin.Begin);

                try
                {
                    var serializer = this.Description.Binding.ContentTypeMapper.GetSerializer(responseContentType, typeof(TResult));

                    switch (errorResponse.Headers[HttpResponseHeader.ContentEncoding])
                    {
                    case "deflate":
                        using (DeflateStream df = new DeflateStream(new NonDisposingStream(ms), CompressionMode.Decompress))
                            errorResult = serializer.DeSerialize(df);
                        break;

                    case "gzip":
                        using (GZipStream df = new GZipStream(new NonDisposingStream(ms), CompressionMode.Decompress))
                            errorResult = serializer.DeSerialize(df);
                        break;

                    case "bzip2":
                        using (var bzs = new BZip2Stream(new NonDisposingStream(ms), CompressionMode.Decompress, false))
                            errorResult = serializer.DeSerialize(bzs);
                        break;

                    case "lzma":
                        using (var lzmas = new LZipStream(new NonDisposingStream(ms), CompressionMode.Decompress))
                            errorResult = serializer.DeSerialize(lzmas);
                        break;

                    default:
                        errorResult = serializer.DeSerialize(ms);
                        break;
                    }
                }
                catch
                {
                    errorResult = new RestServiceFault(e);
                }

                Exception exception = null;

                if (errorResult is RestServiceFault rse)
                {
                    exception = new RestClientException <RestServiceFault>(rse, e, e.Status, e.Response);
                }
                else if (errorResponse is TResult)
                {
                    exception = new RestClientException <TResult>((TResult)errorResult, e, e.Status, e.Response);
                }
                else
                {
                    exception = new RestClientException <object>(errorResult, e, e.Status, e.Response);
                }

                switch (errorResponse.StatusCode)
                {
                case HttpStatusCode.Unauthorized:     // Validate the response

                    throw exception;

                case HttpStatusCode.NotModified:
                    responseHeaders = errorResponse?.Headers;
                    return(default(TResult));

                case (HttpStatusCode)422:
                    throw exception;

                default:
                    throw exception;
                }
            }
            catch (WebException e) when(e.Status == WebExceptionStatus.Timeout)
            {
                this.m_tracer.TraceError("Error executing {0} {1} : {2}", method, url, e.Message);
                throw new TimeoutException($"Timeout executing REST operation {method} {url}", e);
            }
            catch (WebException e) when(e.Status == WebExceptionStatus.ConnectFailure)
            {
                this.m_tracer.TraceError("Error executing {0} {1} : {2}", method, url, e.Message);
                if ((e.InnerException as SocketException)?.SocketErrorCode == SocketError.TimedOut)
                {
                    throw new TimeoutException();
                }
                else
                {
                    throw;
                }
            }
            catch (WebException e)
            {
                this.m_tracer.TraceError("Error executing {0} {1} : {2}", method, url, e.Message);
                throw;
            }
            catch (InvalidOperationException e)
            {
                this.m_tracer.TraceError("Invalid Operation: {0}", e.Message);
                throw;
            }

            responseHeaders = new WebHeaderCollection();
            return(default(TResult));
        }
Ejemplo n.º 26
0
 /// <summary>
 /// Backup the configuration
 /// </summary>
 public void Backup()
 {
     using (var lzs = new LZipStream(File.Create(Path.ChangeExtension(this.m_configPath, "bak.7z")), SharpCompress.Compressors.CompressionMode.Compress))
         this.m_configuration.Save(lzs);
 }