Skip to content

Commit e8ca939

Browse files
feat: add proxy header. Closes #83 (#89)
Co-authored-by: Gavin Barron <gavinbarron@microsoft.com>
1 parent b16a88d commit e8ca939

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

msgraph-developer-proxy/ChaosEngine.cs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
// Copyright (c) Microsoft Corporation.
22
// Licensed under the MIT License.
33

4+
using System.Diagnostics;
45
using System.Net;
6+
using System.Reflection;
57
using System.Text.Json;
68
using System.Text.RegularExpressions;
79
using Titanium.Web.Proxy;
@@ -83,6 +85,22 @@ public class ChaosEngine {
8385
// used for deciding which URLs to decrypt for further inspection
8486
private List<Regex> hostsToWatch = new List<Regex>();
8587

88+
89+
private static string __productVersion = string.Empty;
90+
private static string _productVersion {
91+
get {
92+
if (__productVersion == string.Empty) {
93+
var assembly = Assembly.GetExecutingAssembly();
94+
if (assembly != null) {
95+
var fileVersionInfo = FileVersionInfo.GetVersionInfo(assembly.Location);
96+
__productVersion = fileVersionInfo?.ProductVersion!;
97+
}
98+
}
99+
100+
return __productVersion;
101+
}
102+
}
103+
86104
public ChaosEngine(ProxyConfiguration config) {
87105
_config = config ?? throw new ArgumentNullException(nameof(config));
88106
_config.InitResponsesWatcher();
@@ -291,6 +309,7 @@ private void HandleRequest(SessionEventArgs e) {
291309
}
292310

293311
if (failMode == FailMode.PassThru && _config.FailureRate != 100) {
312+
AddProxyHeader(e.HttpClient.Request);
294313
Console.WriteLine($"\tPassed through {e.HttpClient.Request.Url}");
295314
return;
296315
}
@@ -307,6 +326,12 @@ private void HandleRequest(SessionEventArgs e) {
307326
UpdateProxyResponse(e, responseComponents, matchingResponse);
308327
}
309328

329+
private static void AddProxyHeader(Request r) {
330+
if (r.Headers is not null) {
331+
r.Headers.AddHeader("Via", $"{r.HttpVersion} graph-proxy/{_productVersion}");
332+
}
333+
}
334+
310335
private static string BuildUseSdkMessage(Request r) => $"To handle API errors more easily, use the Graph SDK. More info at {GetMoveToSdkUrl(r)}";
311336

312337
private static string BuildUseSelectMessage(Request r) => $"To improve performance of your application, use the $select parameter. More info at {GetSelectParameterGuidanceUrl(r)}";

0 commit comments

Comments
 (0)