From 9d18a4e13ade24a5fd1535069daf39b6809a90dc Mon Sep 17 00:00:00 2001 From: Dimitrie Stefanescu Date: Fri, 30 Mar 2018 17:44:17 +0100 Subject: [PATCH] fixes some request payload length issues on empty zipping up of POST requests --- SpeckleCore/Converter.cs | 3 +- SpeckleCore/GzipContent.cs | 74 +++++++++++++------------ SpeckleCore/SpeckleApiClientApiCalls.cs | 1 - 3 files changed, 41 insertions(+), 37 deletions(-) diff --git a/SpeckleCore/Converter.cs b/SpeckleCore/Converter.cs index c72be52..b58d2ce 100644 --- a/SpeckleCore/Converter.cs +++ b/SpeckleCore/Converter.cs @@ -107,7 +107,8 @@ public static List Serialise( IEnumerable objectList ) /// A native type, a SpeckleAbstract if no explicit conversion found, or null. public static List Deserialise( IEnumerable objectList ) { - return objectList.Select( obj => Deserialise( obj ) ).ToList(); + var copy = objectList.ToArray(); + return copy.Select( obj => Deserialise( obj ) ).ToList(); } /// diff --git a/SpeckleCore/GzipContent.cs b/SpeckleCore/GzipContent.cs index 307dc03..c6f9bb1 100644 --- a/SpeckleCore/GzipContent.cs +++ b/SpeckleCore/GzipContent.cs @@ -10,46 +10,50 @@ namespace SpeckleCore { - /// - /// https://cymbeline.ch/2014/03/16/gzip-encoding-an-http-post-request-body/ - /// - internal sealed class GzipContent : HttpContent - { - private readonly HttpContent content; - - public GzipContent(HttpContent content) - { - this.content = content; - + /// + /// https://cymbeline.ch/2014/03/16/gzip-encoding-an-http-post-request-body/ + /// + internal sealed class GzipContent : HttpContent + { + private readonly HttpContent content; - // Keep the original content's headers ... - if (content != null) - foreach (KeyValuePair> header in content.Headers) - { - Headers.TryAddWithoutValidation(header.Key, header.Value); - } + public GzipContent( HttpContent content ) + { + if(content == null) + { + return; + } - // ... and let the server know we've Gzip-compressed the body of this request. - Headers.ContentEncoding.Add("gzip"); - } + this.content = content; - protected override async Task SerializeToStreamAsync(Stream stream, TransportContext context) + // Keep the original content's headers ... + if ( content != null ) + foreach ( KeyValuePair> header in content.Headers ) { - // Open a GZipStream that writes to the specified output stream. - using (GZipStream gzip = new GZipStream(stream, CompressionMode.Compress, true)) - { - // Copy all the input content to the GZip stream. - if (content != null) - await content.CopyToAsync(gzip); - else - await (new System.Net.Http.StringContent(string.Empty)).CopyToAsync(gzip); - } + Headers.TryAddWithoutValidation( header.Key, header.Value ); } - protected override bool TryComputeLength(out long length) - { - length = -1; - return false; - } + // ... and let the server know we've Gzip-compressed the body of this request. + Headers.ContentEncoding.Add( "gzip" ); + } + + protected override async Task SerializeToStreamAsync( Stream stream, TransportContext context ) + { + // Open a GZipStream that writes to the specified output stream. + using ( GZipStream gzip = new GZipStream( stream, CompressionMode.Compress, true ) ) + { + // Copy all the input content to the GZip stream. + if ( content != null ) + await content.CopyToAsync( gzip ); + else + await ( new System.Net.Http.StringContent( string.Empty ) ).CopyToAsync( gzip ); + } + } + + protected override bool TryComputeLength( out long length ) + { + length = -1; + return false; } + } } diff --git a/SpeckleCore/SpeckleApiClientApiCalls.cs b/SpeckleCore/SpeckleApiClientApiCalls.cs index 29efd5b..1b32e2b 100644 --- a/SpeckleCore/SpeckleApiClientApiCalls.cs +++ b/SpeckleCore/SpeckleApiClientApiCalls.cs @@ -2724,7 +2724,6 @@ public async System.Threading.Tasks.Task StreamCloneAsync( { using ( var request_ = new System.Net.Http.HttpRequestMessage() ) { - request_.Content = new System.Net.Http.StringContent( string.Empty, System.Text.Encoding.UTF8, "application/json" ); request_.Method = new System.Net.Http.HttpMethod( "POST" ); request_.Headers.Accept.Add( new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue( "application/json" ) );