Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support for X-Forwarded-Host header #1037

Merged
merged 1 commit into from
Mar 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ To use it, add a setting like this to appsettings
```json
"FileWSDL": {
"UrlOverride": "",
"SchemeOverride": "",
"VirtualPath": "",
"WebServiceWSDLMapping": {
"Service.asmx": {
Expand All @@ -93,6 +94,7 @@ To use it, add a setting like this to appsettings
```

* UrlOverride - can be used to override the URL in the service description. This can be useful if you are behind a firewall.
* SchemeOverride - can be used to override the HTTP Scheme in the service description. This can be useful if you are behind a firewall and the firewall sets the X-Forwarded-Host header, but the internal HTTP scheme is not the same as the external.
* VirualPath - can be used if you like to add a path between the base URL and service.
* WebServiceWSDLMapping
* UrlOverride - can be used to override the URL for a specific WSDL mapping. This can be useful if you want to host different services under different folder.
Expand Down
47 changes: 26 additions & 21 deletions src/SoapCore/SoapEndpointMiddleware.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,9 @@
using System.Net.Http.Headers;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Security.Authentication;
using System.ServiceModel;
using System.ServiceModel.Channels;
using System.Text;
using System.Threading.Tasks;
using System.Xml;
using Microsoft.AspNetCore.Connections;
Expand Down Expand Up @@ -976,6 +974,29 @@ private void SetHttpResponse(HttpContext httpContext, Message message)
}
}

private string GetServerUrl(WsdlFileOptions options, HttpContext httpContext)
{
if (!string.IsNullOrEmpty(options.UrlOverride))
{
return options.UrlOverride;
}

if (options.UrlOverrideFunc != null)
{
return options.UrlOverrideFunc(options, httpContext);
}

string scheme = string.IsNullOrEmpty(options.SchemeOverride) ? httpContext.Request.Scheme : options.SchemeOverride;
string host = httpContext.Request.Host.ToString();
var forwardedHost = httpContext.Request.Headers["X-Forwarded-Host"];
if (forwardedHost.Count != 0)
{
host = forwardedHost[0];
}

return scheme + "://" + host + "/";
}

private MetaFromFile GetMeta(HttpContext httpContext)
{
var options = _options.WsdlFileOptions;
Expand All @@ -990,16 +1011,7 @@ private MetaFromFile GetMeta(HttpContext httpContext)

meta.WSDLFolder = mapping.WSDLFolder;
meta.XsdFolder = mapping.SchemaFolder;

if (options.UrlOverride != string.Empty)
{
meta.ServerUrl = options.UrlOverride;
}
else
{
meta.ServerUrl = httpContext.Request.Scheme + "://" + httpContext.Request.Host + "/";
}

meta.ServerUrl = GetServerUrl(options, httpContext);
return meta;
}

Expand Down Expand Up @@ -1078,16 +1090,9 @@ private async Task ProcessMetaFromFile(HttpContext httpContext, bool showDocumen
meta.CurrentWebService = mapping.UrlOverride;
}

meta.XsdFolder = mapping.SchemaFolder;
meta.WSDLFolder = mapping.WSDLFolder;
if (options.UrlOverride != string.Empty)
{
meta.ServerUrl = options.UrlOverride;
}
else
{
meta.ServerUrl = httpContext.Request.Scheme + "://" + httpContext.Request.Host + "/";
}
meta.XsdFolder = mapping.SchemaFolder;
meta.ServerUrl = GetServerUrl(options, httpContext);

string wsdlfile = mapping.WsdlFile;

Expand Down
3 changes: 3 additions & 0 deletions src/SoapCore/WSDLFileOptions.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using Microsoft.AspNetCore.Http;
using System;
using System.Collections.Generic;

Expand All @@ -7,8 +8,10 @@ public class WsdlFileOptions
{
public virtual Dictionary<string, WebServiceWSDLMapping> WebServiceWSDLMapping { get; set; } = new Dictionary<string, WebServiceWSDLMapping>();
public string UrlOverride { get; set; }
public string SchemeOverride { get; set; }
public string VirtualPath { get; set; }
public string AppPath { get; set; }
public Func<WsdlFileOptions, HttpContext, string> UrlOverrideFunc { get; set; }
}

public class WsdlFileOptionsCaseInsensitive : WsdlFileOptions
Expand Down
Loading