22using System ;
33using System . Collections . Generic ;
44using System . IO ;
5+ using System . Linq ;
56using System . Net ;
67using System . Net . Http ;
78using System . Text ;
@@ -21,6 +22,7 @@ internal class ImageOperations : IImageOperations
2122 } ;
2223
2324 private const string RegistryAuthHeaderKey = "X-Registry-Auth" ;
25+ private const string RegistryConfigHeaderKey = "X-Registry-Config" ;
2426 private const string TarContentType = "application/x-tar" ;
2527 private const string ImportFromBodySource = "-" ;
2628
@@ -43,7 +45,7 @@ internal ImageOperations(DockerClient client)
4345 return this . _client . JsonSerializer . DeserializeObject < ImagesListResponse [ ] > ( response . Body ) ;
4446 }
4547
46- public Task < Stream > BuildImageFromDockerfileAsync ( Stream contents , ImageBuildParameters parameters , CancellationToken cancellationToken = default ( CancellationToken ) )
48+ public Task BuildImageFromDockerfileAsync ( ImageBuildParameters parameters , Stream contents , IEnumerable < AuthConfig > authConfigs , IDictionary < string , string > headers , IProgress < JSONMessage > progress , CancellationToken cancellationToken = default ( CancellationToken ) )
4749 {
4850 if ( contents == null )
4951 {
@@ -55,9 +57,34 @@ internal ImageOperations(DockerClient client)
5557 throw new ArgumentNullException ( nameof ( parameters ) ) ;
5658 }
5759
60+ HttpMethod httpMethod = HttpMethod . Post ;
61+
5862 var data = new BinaryRequestContent ( contents , TarContentType ) ;
63+
5964 IQueryString queryParameters = new QueryString < ImageBuildParameters > ( parameters ) ;
60- return this . _client . MakeRequestForStreamAsync ( this . _client . NoErrorHandlers , HttpMethod . Post , "build" , queryParameters , data , cancellationToken ) ;
65+
66+ Dictionary < string , string > customHeaders = RegistryConfigHeaders ( authConfigs ) ;
67+
68+ if ( headers != null )
69+ {
70+ foreach ( string key in headers . Keys )
71+ {
72+ customHeaders [ key ] = headers [ key ] ;
73+ }
74+ }
75+
76+ return StreamUtil . MonitorResponseForMessagesAsync (
77+ this . _client . MakeRequestForRawResponseAsync (
78+ httpMethod ,
79+ "build" ,
80+ queryParameters ,
81+ data ,
82+ customHeaders ,
83+ cancellationToken ) ,
84+ this . _client ,
85+ cancellationToken ,
86+ progress
87+ ) ;
6188 }
6289
6390 public Task CreateImageAsync ( ImagesCreateParameters parameters , AuthConfig authConfig , IProgress < JSONMessage > progress , CancellationToken cancellationToken = default ( CancellationToken ) )
@@ -269,5 +296,17 @@ private Dictionary<string, string> RegistryAuthHeaders(AuthConfig authConfig)
269296 }
270297 } ;
271298 }
299+
300+ private Dictionary < string , string > RegistryConfigHeaders ( IEnumerable < AuthConfig > authConfig )
301+ {
302+ var configDictionary = ( authConfig ?? new AuthConfig [ 0 ] ) . ToDictionary ( e => e . ServerAddress , e => e ) ;
303+ return new Dictionary < string , string >
304+ {
305+ {
306+ RegistryConfigHeaderKey ,
307+ Convert . ToBase64String ( Encoding . UTF8 . GetBytes ( this . _client . JsonSerializer . SerializeObject ( configDictionary ) ) )
308+ }
309+ } ;
310+ }
272311 }
273312}
0 commit comments