Skip to content

Commit 7e8f468

Browse files
committed
Adding http forwarding support
1 parent eda8870 commit 7e8f468

File tree

2 files changed

+53
-2
lines changed

2 files changed

+53
-2
lines changed

src/Azure.WebJobs.Extensions.HttpApi/HttpFunctionBase.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,10 +57,10 @@ protected IUrlHelper Url
5757
}
5858
}
5959

60-
public IObjectModelValidator ObjectValidator
60+
protected IObjectModelValidator ObjectValidator
6161
=> _objectModelValidator ??= HttpContext?.RequestServices?.GetRequiredService<IObjectModelValidator>();
6262

63-
public ProblemDetailsFactory ProblemDetailsFactory
63+
protected ProblemDetailsFactory ProblemDetailsFactory
6464
=> _problemDetailsFactory ??= HttpContext?.RequestServices?.GetRequiredService<ProblemDetailsFactory>();
6565

6666
#region IActionResult helpers
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
using System;
2+
using System.Net.Http;
3+
using System.Threading.Tasks;
4+
5+
using Microsoft.AspNetCore.Http;
6+
7+
namespace Azure.WebJobs.Extensions.HttpApi
8+
{
9+
public abstract class ProxyFunctionBase
10+
{
11+
protected ProxyFunctionBase(IHttpClientFactory httpClientFactory, IHttpContextAccessor httpContextAccessor)
12+
{
13+
_httpClient = httpClientFactory.CreateClient();
14+
_httpContextAccessor = httpContextAccessor;
15+
}
16+
17+
private readonly HttpClient _httpClient;
18+
private readonly IHttpContextAccessor _httpContextAccessor;
19+
20+
protected async Task SendAsync(string destinationPrefix)
21+
{
22+
var context = _httpContextAccessor.HttpContext;
23+
24+
var request = new HttpRequestMessage();
25+
26+
request.Method = GetHttpMethod(context.Request.Method);
27+
28+
foreach (var (name, value) in context.Request.Headers)
29+
{
30+
request.Headers.TryAddWithoutValidation(name, (string)value);
31+
}
32+
33+
var response = await _httpClient.SendAsync(request);
34+
}
35+
36+
private static HttpMethod GetHttpMethod(string method)
37+
{
38+
return method switch
39+
{
40+
{ } when HttpMethods.IsGet(method) => HttpMethod.Get,
41+
{ } when HttpMethods.IsPost(method) => HttpMethod.Post,
42+
{ } when HttpMethods.IsPut(method) => HttpMethod.Put,
43+
{ } when HttpMethods.IsDelete(method) => HttpMethod.Delete,
44+
{ } when HttpMethods.IsOptions(method) => HttpMethod.Options,
45+
{ } when HttpMethods.IsHead(method) => HttpMethod.Head,
46+
{ } when HttpMethods.IsPatch(method) => HttpMethod.Patch,
47+
_ => throw new ArgumentOutOfRangeException(nameof(method), method, null)
48+
};
49+
}
50+
}
51+
}

0 commit comments

Comments
 (0)