Skip to content

Commit

Permalink
feat: Add "forwarded" headers if present to a proxy request
Browse files Browse the repository at this point in the history
  • Loading branch information
alexpop-zengenti committed Jun 25, 2024
1 parent 1f5aa44 commit a038d2e
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -546,16 +546,30 @@ private static void EnsureResponseHeadersAndStatusCode(RouteInfo routeInfo, Endp
{
if (!routeInfo.IsIisFallback)
{
if (routeInfo.ProxyId != null &&
routeInfo.ProxyId.Equals(Guid.Parse("8f2cc5be-b5dd-4e4b-b6fa-92b7fc6440e0")))
if (routeInfo is { ProxyId: not null, BlockVersionInfo: null })
{
response.Headers[Constants.Headers.SurrogateControl] = new List<string>
foreach (var proxyHeaderName in Constants.Headers.ProxyHeaders)
{
"max-age=60"
};
}
var proxyHeaderValue = routeInfo.Headers.GetFirstValueIfExists(proxyHeaderName, true);
if (proxyHeaderValue != null)
{
response.Headers[proxyHeaderName] = new List<string>
{
proxyHeaderValue
};
}
}

return;
if (routeInfo.ProxyId.Equals(Guid.Parse("8f2cc5be-b5dd-4e4b-b6fa-92b7fc6440e0")))
{
response.Headers[Constants.Headers.SurrogateControl] = new List<string>
{
"max-age=60"
};

return;
}
}
}

// Unfortunately IIS returns an empty bodied 200 rather than a 404 when
Expand Down
11 changes: 11 additions & 0 deletions src/Zengenti.Contensis.RequestHandler.Domain/Common/Constants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ public static class Headers
public const string HealthCheck = "x-healthcheck";
public const string HidePreviewToolbar = "x-hide-preview-toolbar";

public const string ProxyForwardedFor = "x-forwarded-for";
public const string ProxyForwardedProto = "x-forwarded-proto";
public const string ProxyForwardedPort = "x-forwarded-port";

public const string RequiresAlias = "x-requires-alias";
public const string RequiresProjectApiId = "x-requires-project-api-id";
public const string RequiresNodeId = "x-requires-node-id";
Expand Down Expand Up @@ -57,6 +61,13 @@ public static class Headers
RendererConfigDefault
};

public static readonly string[] ProxyHeaders =
{
ProxyForwardedFor,
ProxyForwardedProto,
ProxyForwardedPort
};

public static readonly string[] RequiresHeaders =
{
RequiresAlias,
Expand Down
2 changes: 1 addition & 1 deletion src/Zengenti.Contensis.RequestHandler.Domain/Domain.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Zengenti.Core" Version="6.1.0" />
<PackageReference Include="Zengenti.Core" Version="6.3.1" />
</ItemGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,9 +117,9 @@ private void SetValue(string key, IEnumerable<string> value)
}
}

public string? GetFirstValueIfExists(string headerName)
public string? GetFirstValueIfExists(string headerName, bool caseInsensitive = false)
{
var key = CaseInsensitiveHeaderNames.ContainsCaseInsensitive(headerName)
var key = caseInsensitive || CaseInsensitiveHeaderNames.ContainsCaseInsensitive(headerName)
? _values.Keys.SingleOrDefault(k => k.EqualsCaseInsensitive(headerName))
: headerName;

Expand Down

0 comments on commit a038d2e

Please sign in to comment.