Skip to content

Commit

Permalink
Yet another port fix, also make bodyless requests work again (#7792)
Browse files Browse the repository at this point in the history
  • Loading branch information
lmolkova authored Feb 29, 2024
1 parent d68aa4a commit 481137c
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 17 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk.Web">
<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<VersionPrefix>0.2.0</VersionPrefix>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,13 @@ private async Task<UpstreamResponse> SendUpstreamRequest(HttpRequest request, st

using (var upstreamRequest = new HttpRequestMessage(new HttpMethod(request.Method), upstreamUri))
{
upstreamRequest.Content = new StreamContent(request.Body);
foreach (var header in request.Headers.Where(h => Utils.ContentRequestHeaders.Contains(h.Key)))
{
upstreamRequest.Content.Headers.Add(header.Key, values: header.Value);
if (Utils.HasBody(request))
{
upstreamRequest.Content = new StreamContent(request.Body);
foreach (var header in request.Headers.Where(h => Utils.ContentRequestHeaders.Contains(h.Key)))
{
upstreamRequest.Content.Headers.Add(header.Key, values: header.Value);
}
}

foreach (var header in request.Headers.Where(h => !Utils.ExcludedRequestHeaders.Contains(h.Key) && !Utils.ContentRequestHeaders.Contains(h.Key)))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@
using OpenTelemetry.Logs;
using OpenTelemetry.Resources;
using System;
using System.IO;
using System.Net.Http;
using System.Reflection;

namespace Azure.Sdk.Tools.HttpFaultInjector
{
Expand All @@ -28,6 +30,7 @@ public static void Main(string[] args)
{
settings.CaseSensitive = false;
settings.HelpWriter = Console.Error;
settings.IgnoreUnknownArguments = true;
});

parser.ParseArguments<Options>(args).WithParsed(options => Run(options, args));
Expand All @@ -36,7 +39,12 @@ public static void Main(string[] args)
private static void Run(Options options, string[] args)
{
TimeSpan keepAlive = TimeSpan.FromSeconds(options.KeepAliveTimeout);
var builder = WebApplication.CreateBuilder(args);
var builder = WebApplication.CreateBuilder(new WebApplicationOptions()
{
Args = args,
ContentRootPath = Directory.GetParent(Assembly.GetExecutingAssembly().Location)?.FullName
});

builder.WebHost.ConfigureKestrel(kestrelOptions =>
{
kestrelOptions.Limits.KeepAliveTimeout = keepAlive;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection.Metadata;
using Microsoft.AspNetCore.Http;
using OpenTelemetry.Trace;

namespace Azure.Sdk.Tools.HttpFaultInjector
{
Expand Down Expand Up @@ -34,15 +38,29 @@ public static class Utils
ResponseSelectionHeader
};

// Headers which must be set on HttpContent instead of HttpRequestMessage
// Headers which must be set on HttpContent instead of HttpRequestMessage, values from HttpContentHeaders
public static readonly string[] ContentRequestHeaders = new string[] {
"Allow",
"Content-Disposition",
"Content-Encoding",
"Content-Language",
"Content-Length",
"Content-Location",
"Content-MD5",
"Content-Range",
"Content-Type",
"Expires",
"Last-Modified"
};

public const string ResponseSelectionHeader = "x-ms-faultinjector-response-option";
public const string UpstreamBaseUriHeader = "X-Upstream-Base-Uri";

public static bool HasBody(HttpRequest request)
{
return request.ContentLength > 0 || request.Headers.TransferEncoding.Contains("chunked");
}

public static string ReadSelectionFromConsole()
{
string fault;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,5 @@
"Microsoft.AspNetCore.HttpLogging.HttpLoggingMiddleware": "Information"
}
},
"Kestrel": {
"Endpoints": {
"http": {
"Url": "http://+:7777"
},
"https": {
"Url": "https://+:7778"
}
}
}
"urls": "http://+:7777;https://+:7778"
}

0 comments on commit 481137c

Please sign in to comment.