Skip to content

Commit

Permalink
feat(slimfaas): clean proxy request system
Browse files Browse the repository at this point in the history
  • Loading branch information
guillaume-chervet committed Jun 1, 2023
1 parent 9cb45a3 commit 4d4f270
Show file tree
Hide file tree
Showing 7 changed files with 283 additions and 209 deletions.
11 changes: 2 additions & 9 deletions src/SlimFaas/CustomRequest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,11 @@

namespace SlimFaas;

public record struct CustomRequest(IList<CustomHeader> Headers, IList<CustomForm> Form, IList<CustomFormFile> FormFiles, string FunctionName, string Path, string Method)
{
public string Query { get; set; }
public string Body { get; set; }
public string ContentType { get; set; }
}
public record struct CustomRequest(IList<CustomHeader> Headers, byte[]? Body, string FunctionName, string Path,
string Method, string Query);

public record struct CustomHeader(string Key, string?[] Values);

public record struct CustomForm(string Key, string?[] Values);

public record struct CustomFormFile(string Key, byte[] Value, string Filename);

[JsonSerializable(typeof(CustomRequest))]
[JsonSourceGenerationOptions(WriteIndented = false, DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull)]
Expand Down
2 changes: 1 addition & 1 deletion src/SlimFaas/KubernetesService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public class KubernetesService : IKubernetesService
public KubernetesService(IConfiguration config, ILogger<KubernetesService> logger)
{
_logger = logger;
var useKubeConfig = bool.Parse(config["UseKubeConfig"]);
var useKubeConfig = bool.Parse(config["UseKubeConfig"] ?? "false");
_k8SConfig = !useKubeConfig ? KubernetesClientConfiguration.InClusterConfig() :
KubernetesClientConfiguration.BuildConfigFromConfigFile();
_k8SConfig.SkipTlsVerify = true;
Expand Down
2 changes: 1 addition & 1 deletion src/SlimFaas/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@

app.UseMetricServer();
app.UseHttpMetrics();
app.UseMiddleware<SlimMiddleware>();
app.UseMiddleware<SlimProxyMiddleware>();

app.Run(context =>
{
Expand Down
110 changes: 51 additions & 59 deletions src/SlimFaas/SendClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,71 +13,63 @@ public SendClient(HttpClient httpClient)
Environment.GetEnvironmentVariable("BASE_FUNCTION_URL") ?? "http://localhost:5123/"; //""http://{function_name}:8080";
}

public async Task<HttpResponseMessage> SendHttpRequestAsync(CustomRequest customRequest)
private void CopyFromOriginalRequestContentAndHeaders(CustomRequest context, HttpRequestMessage requestMessage)
{
var functionUrl = _baseFunctionUrl;
var url = functionUrl.Replace("{function_name}", customRequest.FunctionName) + customRequest.Path +
customRequest.Query;
var requestMethod = context.Method;

if (!HttpMethods.IsGet(requestMethod) &&
!HttpMethods.IsHead(requestMethod) &&
!HttpMethods.IsDelete(requestMethod) &&
!HttpMethods.IsTrace(requestMethod) &&
context.Body != null)
{
var streamContent = new StreamContent(new MemoryStream(context.Body));
requestMessage.Content = streamContent;
}

switch (customRequest.Method)
foreach (var header in context.Headers)
{
case "GET":
case "DELETE":
{
var response = await _httpClient.GetAsync(url);
return response;
}
case "POST":
case "PUT":
{
var httpRequestMessage = new HttpRequestMessage(HttpMethod.Post, url);
foreach (var customRequestHeader in customRequest.Headers)
{
foreach (var value in customRequestHeader.Values)
{
if (customRequestHeader.Key == "Content-Length" || customRequestHeader.Key == "Content-Type")
continue;
httpRequestMessage.Headers.Add(customRequestHeader.Key, value);
}
}
if (customRequest.ContentType == "multipart/form-data")
{
var requestContent = new MultipartFormDataContent();
foreach (var formData in customRequest.Form)
{
foreach (var value in formData.Values)
{
if (value != null)
{
requestContent.Add(new StringContent(value), formData.Key);
}
}
}
requestMessage.Content?.Headers.TryAddWithoutValidation(header.Key, header.Values);
}
}

private HttpRequestMessage CreateTargetMessage(CustomRequest context, Uri targetUri)
{
var requestMessage = new HttpRequestMessage();
CopyFromOriginalRequestContentAndHeaders(context, requestMessage);

foreach (var requestFormFile in customRequest.FormFiles)
{
var streamContent = new StreamContent(new MemoryStream(requestFormFile.Value));
requestContent.Add(streamContent, requestFormFile.Key, requestFormFile.Filename);
}
requestMessage.RequestUri = targetUri;
requestMessage.Headers.Host = targetUri.Host;
requestMessage.Method = GetMethod(context.Method);

httpRequestMessage.Content = requestContent;
}
else if(customRequest.ContentType == "application/json")
{
httpRequestMessage.Content = new StringContent(customRequest.Body, Encoding.UTF8, "application/json");
httpRequestMessage.Method = new HttpMethod(customRequest.Method);
}
else
{
httpRequestMessage.Content = new StringContent(customRequest.Body);
httpRequestMessage.Method = new HttpMethod(customRequest.Method);
}
return requestMessage;
}

private static HttpMethod GetMethod(string method)
{
if (HttpMethods.IsDelete(method)) return HttpMethod.Delete;
if (HttpMethods.IsGet(method)) return HttpMethod.Get;
if (HttpMethods.IsHead(method)) return HttpMethod.Head;
if (HttpMethods.IsOptions(method)) return HttpMethod.Options;
if (HttpMethods.IsPost(method)) return HttpMethod.Post;
if (HttpMethods.IsPut(method)) return HttpMethod.Put;
if (HttpMethods.IsTrace(method)) return HttpMethod.Trace;
return new HttpMethod(method);
}

public async Task<HttpResponseMessage> SendHttpRequestAsync(CustomRequest customRequest, HttpContext? context = null)
{
var functionUrl = _baseFunctionUrl;
var url = functionUrl.Replace("{function_name}", customRequest.FunctionName) + customRequest.Path +
customRequest.Query;
var targetRequestMessage = CreateTargetMessage(customRequest, new Uri(url));
if (context != null)
{
return await _httpClient.SendAsync(targetRequestMessage,
HttpCompletionOption.ResponseHeadersRead, context.RequestAborted);

var response = await _httpClient.SendAsync(httpRequestMessage);
return response;
}
default:
throw new NotImplementedException("Method not implemented");
}
return await _httpClient.SendAsync(targetRequestMessage,
HttpCompletionOption.ResponseHeadersRead);
}
}
139 changes: 0 additions & 139 deletions src/SlimFaas/SlimMiddleware.cs

This file was deleted.

Loading

0 comments on commit 4d4f270

Please sign in to comment.