forked from RestCode/WebApiProxy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ProxyHandler.cs
45 lines (39 loc) · 1.61 KB
/
ProxyHandler.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
using System;
using System.Linq;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Threading.Tasks;
using System.Web.Http;
using WebApiProxy.Server.Templates;
//testing ci 1
namespace WebApiProxy.Server
{
public class ProxyHandler : DelegatingHandler
{
private MetadataProvider _metadataProvider;
HttpConfiguration _config;
public ProxyHandler(HttpConfiguration config)
{
_metadataProvider = new MetadataProvider();
_config = config;
}
protected async override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, System.Threading.CancellationToken cancellationToken)
{
return await Task.Run(() =>
{
var metadata = _metadataProvider.GetMetadata(request, _config);
if (request.Headers.Any(h => h.Key == "X-Proxy-Type" && h.Value.Contains("metadata")))
{
return request.CreateResponse(System.Net.HttpStatusCode.OK, metadata);
}
var template = new JsProxyTemplate(metadata);
var js = new StringContent(template.TransformText());
js.Headers.ContentType = new MediaTypeHeaderValue("application/javascript");
js.Headers.Expires = DateTime.Now.AddDays(30).ToUniversalTime();
var result = new HttpResponseMessage { Content = js }; ;
result.Headers.CacheControl = CacheControlHeaderValue.Parse("public");
return result;
});
}
}
}