3
3
using System . Net . Http ;
4
4
using System . Threading . Tasks ;
5
5
using Microsoft . AspNetCore . Http ;
6
+ using Microsoft . Extensions . Logging ;
7
+ using Microsoft . SourceBrowser . SourceIndexServer . Models ;
6
8
7
9
namespace Microsoft . SourceBrowser . SourceIndexServer
8
10
{
9
11
public static class Helpers
10
12
{
11
- public static async Task ProxyRequestAsync ( this HttpContext context , HttpClient client , string targetUrl , Action < HttpRequestMessage > configureRequest = null )
13
+ public static async Task ProxyRequestAsync ( this HttpContext context , string targetPath , Action < HttpRequestMessage > configureRequest = null )
12
14
{
13
- using ( var req = new HttpRequestMessage ( HttpMethod . Get , targetUrl ) )
15
+ var fs = new AzureBlobFileSystem ( IndexProxyUrl ) ;
16
+ var props = fs . FileProperties ( targetPath ) ;
17
+ context . Response . Headers . Append ( "Content-Md5" , Convert . ToBase64String ( props . ContentHash ) ) ;
18
+ context . Response . Headers . Append ( "Content-Type" , props . ContentType ) ;
19
+ context . Response . Headers . Append ( "Etag" , props . ETag . ToString ( ) ) ;
20
+ context . Response . Headers . Append ( "Last-Modified" , props . LastModified . ToString ( "R" ) ) ;
21
+ using ( var data = fs . OpenSequentialReadStream ( targetPath ) )
14
22
{
15
- foreach ( var ( key , values ) in context . Request . Headers )
16
- {
17
- switch ( key . ToLower ( ) )
18
- {
19
- // We shouldn't copy any of these request headers
20
- case "host" :
21
- case "authorization" :
22
- case "cookie" :
23
- case "content-length" :
24
- case "content-type" :
25
- continue ;
26
- default :
27
- req . Headers . TryAddWithoutValidation ( key , values . ToArray ( ) ) ;
28
- break ;
29
- }
30
- }
31
-
32
- configureRequest ? . Invoke ( req ) ;
33
-
34
- HttpResponseMessage res = await client . SendAsync ( req , HttpCompletionOption . ResponseHeadersRead ) . ConfigureAwait ( false ) ;
35
- context . Response . RegisterForDispose ( res ) ;
36
-
37
- foreach ( var ( key , values ) in res . Headers )
38
- {
39
- switch ( key . ToLower ( ) )
40
- {
41
- // Remove headers that the response doesn't need
42
- case "set-cookie" :
43
- case "x-powered-by" :
44
- case "x-aspnet-version" :
45
- case "server" :
46
- case "transfer-encoding" :
47
- case "access-control-expose-headers" :
48
- case "access-control-allow-origin" :
49
- continue ;
50
- default :
51
- if ( ! context . Response . Headers . ContainsKey ( key ) )
52
- {
53
- context . Response . Headers [ key ] = values . ToArray ( ) ;
54
- }
55
-
56
- break ;
57
- }
58
- }
59
-
60
- context . Response . StatusCode = ( int ) res . StatusCode ;
61
- if ( res . Content != null )
62
- {
63
- foreach ( var ( key , values ) in res . Content . Headers )
64
- {
65
- if ( ! context . Response . Headers . ContainsKey ( key ) )
66
- {
67
- context . Response . Headers [ key ] = values . ToArray ( ) ;
68
- }
69
- }
70
-
71
- using ( var data = await res . Content . ReadAsStreamAsync ( ) . ConfigureAwait ( false ) )
72
- {
73
- await data . CopyToAsync ( context . Response . Body ) . ConfigureAwait ( false ) ;
74
- }
75
- }
23
+ await data . CopyToAsync ( context . Response . Body ) . ConfigureAwait ( false ) ;
76
24
}
77
25
}
78
26
79
- private static readonly HttpClient s_client = new HttpClient ( new HttpClientHandler ( ) { CheckCertificateRevocationList = true } ) ;
80
-
81
- private static async Task < bool > UrlExistsAsync ( string proxyRequestUrl )
27
+ private static bool UrlExists ( string proxyRequestPath )
82
28
{
83
- using ( var res = new HttpRequestMessage ( HttpMethod . Head , proxyRequestUrl ) )
84
- using ( var req = await s_client . SendAsync ( res ) . ConfigureAwait ( false ) )
85
- {
86
- if ( req . IsSuccessStatusCode )
87
- {
88
- return true ;
89
- }
90
- }
91
-
92
- return false ;
29
+ var fs = new AzureBlobFileSystem ( IndexProxyUrl ) ;
30
+ return fs . FileExists ( proxyRequestPath ) ;
93
31
}
94
32
95
33
public static async Task ServeProxiedIndex ( HttpContext context , Func < Task > next )
@@ -109,15 +47,15 @@ public static async Task ServeProxiedIndex(HttpContext context, Func<Task> next)
109
47
return ;
110
48
}
111
49
112
- var proxyRequestUrl = proxyUri + ( path . StartsWith ( "/" , StringComparison . Ordinal ) ? path : "/" + path ) . ToLowerInvariant ( ) ;
50
+ var proxyRequestPathSuffix = ( path . StartsWith ( "/" , StringComparison . Ordinal ) ? path : "/" + path ) . ToLowerInvariant ( ) ;
113
51
114
- if ( ! await UrlExistsAsync ( proxyRequestUrl ) . ConfigureAwait ( false ) )
52
+ if ( ! UrlExists ( proxyRequestPathSuffix ) )
115
53
{
116
54
await next ( ) . ConfigureAwait ( false ) ;
117
55
return ;
118
56
}
119
57
120
- await context . ProxyRequestAsync ( s_client , proxyRequestUrl ) . ConfigureAwait ( false ) ;
58
+ await context . ProxyRequestAsync ( proxyRequestPathSuffix ) . ConfigureAwait ( false ) ;
121
59
}
122
60
123
61
public static string IndexProxyUrl => Environment . GetEnvironmentVariable ( "SOURCE_BROWSER_INDEX_PROXY_URL" ) ;
0 commit comments