@@ -30,46 +30,46 @@ public class HttpConnection : IConnection
30
30
private readonly object _lock = new object ( ) ;
31
31
private readonly ConcurrentDictionary < int , HttpClient > _clients = new ConcurrentDictionary < int , HttpClient > ( ) ;
32
32
33
- private string DefaultContentType => "application/json" ;
33
+ private string DefaultContentType => "application/json" ;
34
34
35
35
public HttpConnection ( ) { }
36
-
36
+
37
37
private HttpClient GetClient ( RequestData requestData )
38
38
{
39
39
var hashCode = requestData . GetHashCode ( ) ;
40
40
HttpClient client ;
41
41
if ( this . _clients . TryGetValue ( hashCode , out client ) ) return client ;
42
- lock ( _lock )
42
+ lock ( _lock )
43
43
{
44
44
if ( this . _clients . TryGetValue ( hashCode , out client ) ) return client ;
45
45
46
- var handler = new HttpClientHandler
47
- {
48
- AutomaticDecompression = requestData . HttpCompression ? GZip | Deflate : None
49
- } ;
50
-
51
- if ( ! requestData . ProxyAddress . IsNullOrEmpty ( ) )
52
- {
53
- var uri = new Uri ( requestData . ProxyAddress ) ;
54
- var proxy = new WebProxy ( uri ) ;
55
- var credentials = new NetworkCredential ( requestData . ProxyUsername , requestData . ProxyPassword ) ;
56
- proxy . Credentials = credentials ;
57
- handler . Proxy = proxy ;
58
- }
59
-
60
- if ( requestData . DisableAutomaticProxyDetection )
61
- handler . Proxy = null ;
62
-
63
- client = new HttpClient ( handler , false )
64
- {
65
- Timeout = requestData . RequestTimeout
66
- } ;
67
-
68
- client . DefaultRequestHeaders . ExpectContinue = false ;
69
-
70
- //TODO add headers
71
- //client.DefaultRequestHeaders =
72
- this . _clients . TryAdd ( hashCode , client ) ;
46
+ var handler = new HttpClientHandler
47
+ {
48
+ AutomaticDecompression = requestData . HttpCompression ? GZip | Deflate : None
49
+ } ;
50
+
51
+ if ( ! requestData . ProxyAddress . IsNullOrEmpty ( ) )
52
+ {
53
+ var uri = new Uri ( requestData . ProxyAddress ) ;
54
+ var proxy = new WebProxy ( uri ) ;
55
+ var credentials = new NetworkCredential ( requestData . ProxyUsername , requestData . ProxyPassword ) ;
56
+ proxy . Credentials = credentials ;
57
+ handler . Proxy = proxy ;
58
+ }
59
+
60
+ if ( requestData . DisableAutomaticProxyDetection )
61
+ handler . Proxy = null ;
62
+
63
+ client = new HttpClient ( handler , false )
64
+ {
65
+ Timeout = requestData . RequestTimeout
66
+ } ;
67
+
68
+ client . DefaultRequestHeaders . ExpectContinue = false ;
69
+
70
+ //TODO add headers
71
+ //client.DefaultRequestHeaders =
72
+ this . _clients . TryAdd ( hashCode , client ) ;
73
73
return client ;
74
74
}
75
75
@@ -122,13 +122,27 @@ private static HttpRequestMessage CreateHttpRequestMessage(RequestData requestDa
122
122
var method = ConvertHttpMethod ( requestData . Method ) ;
123
123
var requestMessage = new HttpRequestMessage ( method , requestData . Uri ) ;
124
124
125
- foreach ( string key in requestData . Headers )
125
+ foreach ( string key in requestData . Headers )
126
126
{
127
127
requestMessage . Headers . TryAddWithoutValidation ( key , requestData . Headers . GetValues ( key ) ) ;
128
128
}
129
129
130
130
requestMessage . Headers . Accept . Add ( new MediaTypeWithQualityHeaderValue ( requestData . ContentType ) ) ;
131
131
132
+ if ( ! requestData . RunAs . IsNullOrEmpty ( ) )
133
+ requestMessage . Headers . Add ( "es-shield-runas-user" , requestData . RunAs ) ;
134
+
135
+ string userInfo = null ;
136
+ if ( ! requestData . Uri . UserInfo . IsNullOrEmpty ( ) )
137
+ userInfo = Uri . UnescapeDataString ( requestData . Uri . UserInfo ) ;
138
+ else if ( requestData . BasicAuthorizationCredentials != null )
139
+ userInfo = requestData . BasicAuthorizationCredentials . ToString ( ) ;
140
+ if ( ! userInfo . IsNullOrEmpty ( ) )
141
+ {
142
+ var credentials = Convert . ToBase64String ( Encoding . UTF8 . GetBytes ( userInfo ) ) ;
143
+ requestMessage . Headers . Authorization = new AuthenticationHeaderValue ( "Basic" , credentials ) ;
144
+ }
145
+
132
146
var data = requestData . PostData ;
133
147
134
148
if ( data != null )
@@ -153,18 +167,10 @@ private static HttpRequestMessage CreateHttpRequestMessage(RequestData requestDa
153
167
else
154
168
{
155
169
// Set content in order to set a Content-Type header.
156
- // Content gets diposed so can't be shared instance
170
+ // Content gets diposed so can't be shared instance
157
171
requestMessage . Content = new ByteArrayContent ( new byte [ 0 ] ) ;
158
172
}
159
-
160
173
requestMessage . Content . Headers . ContentType = new MediaTypeHeaderValue ( requestData . ContentType ) ;
161
-
162
- if ( ! string . IsNullOrWhiteSpace ( requestData . Uri . UserInfo ) )
163
- {
164
- var credentials = Convert . ToBase64String ( Encoding . UTF8 . GetBytes ( requestMessage . RequestUri . UserInfo ) ) ;
165
- requestMessage . Headers . Authorization = new AuthenticationHeaderValue ( "Basic" , credentials ) ;
166
- }
167
-
168
174
return requestMessage ;
169
175
}
170
176
@@ -186,7 +192,7 @@ private static System.Net.Http.HttpMethod ConvertHttpMethod(HttpMethod httpMetho
186
192
187
193
protected virtual void DisposeManagedResources ( )
188
194
{
189
- foreach ( var c in _clients )
195
+ foreach ( var c in _clients )
190
196
c . Value . Dispose ( ) ;
191
197
}
192
198
}
0 commit comments