Esempio n. 1
0
        private static async Task <ReceivedMessage> WrapRequestInSeekableMessageAsync(HttpListenerRequest request, long contentLength)
        {
            Logger.Trace("Start copying to VirtualStream");
            var dest = new VirtualStream(
                request.ContentLength64 > VirtualStream.ThresholdMax
                    ? VirtualStream.MemoryFlag.OnlyToDisk
                    : VirtualStream.MemoryFlag.AutoOverFlowToDisk,
                forAsync: true);

            if (contentLength > 0)
            {
                dest.SetLength(contentLength);
            }

            await request.InputStream
            .CopyToFastAsync(dest)
            .ConfigureAwait(false);

            dest.Position = 0;

            return(new ReceivedMessage(
                       underlyingStream: dest,
                       contentType: request.ContentType,
                       origin: request.UserHostAddress,
                       length: request.ContentLength64));
        }
Esempio n. 2
0
        private static void DecompressAttachment(IEnumerable <PartInfo> payloadInfo, Attachment attachment)
        {
            PartInfo referenced = payloadInfo.FirstOrDefault(attachment.Matches);

            if (referenced == null)
            {
                throw new InvalidDataException(
                          $"Can't decompress Attachment {attachment.Id} because no matching PartInfo was found");
            }

            if (String.IsNullOrWhiteSpace(referenced.MimeType))
            {
                throw new InvalidDataException(
                          $"Cannot decompress attachment {attachment.Id}: MimeType is not specified in referenced <PartInfo/> element");
            }

            if (referenced.MimeType.IndexOf("/", StringComparison.OrdinalIgnoreCase) < 0)
            {
                throw new InvalidDataException(
                          $"Cannot decompress attachment {attachment.Id}: Invalid MimeType {referenced.MimeType} in referenced <PartInfo/> element");
            }

            attachment.ResetContentPosition();

            long unzipLength = StreamUtilities.DetermineOriginalSizeOfCompressedStream(attachment.Content);

            VirtualStream outputStream =
                VirtualStream.Create(
                    unzipLength > -1 ? unzipLength : VirtualStream.ThresholdMax);

            if (unzipLength > 0)
            {
                outputStream.SetLength(unzipLength);
            }

            Stream decompressed = DecompressStream(attachment.Content);

            referenced.CompressionType = CompressionType;
            attachment.CompressionType = CompressionType;
            attachment.MimeType        = referenced.MimeType;
            attachment.UpdateContent(decompressed, referenced.MimeType);
        }
Esempio n. 3
0
        private static Stream DecompressStream(Stream input)
        {
            long unzipLength = StreamUtilities.DetermineOriginalSizeOfCompressedStream(input);

            VirtualStream outputStream =
                VirtualStream.Create(
                    unzipLength > -1 ? unzipLength : VirtualStream.ThresholdMax);

            if (unzipLength > 0)
            {
                outputStream.SetLength(unzipLength);
            }

            using (var gzipCompression = new GZipStream(input, CompressionMode.Decompress, true))
            {
                gzipCompression.CopyTo(outputStream);
                outputStream.Position = 0;

                return(outputStream);
            }
        }