Skip to content

Commit

Permalink
Merge pull request #1170 from hajkmap/feature/proxy-base-url
Browse files Browse the repository at this point in the history
Added support to mimic proxyBaseUrl in GeoServer
  • Loading branch information
jesade-vbg committed Sep 13, 2022
2 parents 97db6ec + a9335f1 commit 431b2f1
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 14 deletions.
95 changes: 81 additions & 14 deletions proxy/GeoServerProxy/Proxy/Controllers/GeoServerController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
using System.Web.Mvc;
using System.Configuration;
using log4net;
using System.IO.Compression;

namespace Proxy.Controllers
{
Expand All @@ -24,7 +25,7 @@ public class GeoServerController : Controller
{
ILog _log = LogManager.GetLogger(typeof(MyActionResult));
// Static -> only read once from Web.config
static private string _headerAttributeName, _localhostServer;
static private string _headerAttributeName, _localhostServer, _proxyBaseUrl;
static private int _removeDomainFromUserName = -1; // -1 = not initialized from Web.config. 0 = Do not remove, 1 = Remove

private string GetlocalhostServer()
Expand Down Expand Up @@ -64,6 +65,20 @@ private string GetHeaderAttributeName()
return _headerAttributeName;
}

private string GetProxyBaseUrl()
{
if (_proxyBaseUrl == null)
{
_proxyBaseUrl = ConfigurationManager.AppSettings["proxyBaseUrl"];
if (_proxyBaseUrl == null)
{
_proxyBaseUrl = "";
_log.DebugFormat("No config found in Web.config for 'proxyBaseUrl'. No replacement of 'localhostServer' is performed");
}
}
return _proxyBaseUrl;
}

private async Task DoMethod(string method, string urlPath, string queryString, string body, string contentType, Encoding contentEncoding)
{
try
Expand Down Expand Up @@ -124,23 +139,51 @@ private async Task HandleResponse(HttpWebRequest request)
using (var resp = await request.GetResponseAsync())
{
using (var stream = resp.GetResponseStream())
{
var bytes = new byte[BUFFER_SIZE];
while (true)
{
if (GetProxyBaseUrl().Length > 0 && (resp.ContentType.StartsWith("application/vnd.ogc.wms_xml") || resp.ContentType.StartsWith( "text/xml")))
{
var n = stream.Read(bytes, 0, BUFFER_SIZE);
if (n == 0)
var ms = new MemoryStream();
stream.CopyTo(ms);
byte[] bytes = ms.ToArray();

_log.DebugFormat("Response handled the new way, ContentType={0}", resp.ContentType);

_log.DebugFormat("Response - content-encodig: {0}", resp.Headers["content-encoding"]);

bool compressionUsed = resp.Headers["content-encoding"] == "gzip" ? true : false;
if (compressionUsed)
{
break;
bytes = Compress(bytes, CompressionMode.Decompress);
}
Response.OutputStream.Write(bytes, 0, n);
}
if (Response.ContentType == "application/vnd.ogc.wms_xml")
{
var strResponse = Encoding.UTF8.GetString(bytes); // Assume that GeoServer returns GetCapabilities in UTF8. Maybe add this to Web.config
//_log.DebugFormat("Response stream, before replacement: {0}", strResponse);

var strResponseReplaced = strResponse.Replace(GetlocalhostServer(), GetProxyBaseUrl());
_log.DebugFormat("Response stream, after replacement: {0}", strResponseReplaced);

byte[] byteResponseReplaced = Encoding.UTF8.GetBytes(strResponseReplaced);
if (compressionUsed)
{
byteResponseReplaced = Compress(byteResponseReplaced, CompressionMode.Compress);
}

Response.OutputStream.Write(byteResponseReplaced, 0, byteResponseReplaced.Length);
Response.ContentType = "text/xml";
}
else
{
_log.DebugFormat("Response handled the old way, ContentType={0}", resp.ContentType);

var bytes = new byte[BUFFER_SIZE];
while (true)
{
var n = stream.Read(bytes, 0, BUFFER_SIZE);
if (n == 0)
{
break;
}
Response.OutputStream.Write(bytes, 0, n);
}
Response.ContentType = resp.ContentType;
}
}
Expand All @@ -163,6 +206,31 @@ private async Task HandleResponse(HttpWebRequest request)
}
}

private byte[] Compress(byte[] inByteArray, CompressionMode mode)
{
// Create a GZIP stream with decompression mode.
// ... Then create a buffer and write into while reading from the GZIP stream.
using (GZipStream stream = new GZipStream(new MemoryStream(inByteArray), CompressionMode.Decompress))
{
const int size = 4096;
byte[] buffer = new byte[size];
using (MemoryStream memory = new MemoryStream())
{
int count = 0;
do
{
count = stream.Read(buffer, 0, size);
if (count > 0)
{
memory.Write(buffer, 0, count);
}
}
while (count > 0);
return memory.ToArray();
}
}
}

private int GetHttpStatusCode(WebException ex)
{
var response = ex.Response as HttpWebResponse;
Expand All @@ -182,14 +250,13 @@ public async Task<MyActionResult> EndPoint(string url)
{
_log.DebugFormat("EndPoint url: {0}", url);

if(!string.IsNullOrEmpty(url) && url.StartsWith("web/"))
if (!string.IsNullOrEmpty(url) && url.StartsWith("web/"))
{
_log.Warn("Not allowed to use GeoServer Web-interface through proxy: {0}");
Response.StatusCode = 400;
Response.StatusDescription = "Not allowed to use GeoServer Web-interface";
}

if (Request.HttpMethod != "GET" && Request.HttpMethod != "POST")
else if (Request.HttpMethod != "GET" && Request.HttpMethod != "POST")
{
_log.WarnFormat("EndPoint called with not supported HTTP method: {0}", Request.HttpMethod);
Response.StatusCode = 405;
Expand Down
6 changes: 6 additions & 0 deletions proxy/GeoServerProxy/Proxy/Web.config
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
</configSections>
<appSettings>
<add key="localhostServer" value="http://localhost:8092/geoserver/"/>
<!-- Uncomment "proxyBaseUrl" to support replacement of the "localhostServer" value with "proxyBaseUrl" value in responses from GeoServer -->
<!-- <add key="proxyBaseUrl" value="https://kommungis-utv.varberg.se/geoserver/"/> -->
<add key="removeDomainNameFromUser" value="1"/>
<add key="headerAttributeName" value="X-Control-Header"/>
</appSettings>
Expand Down Expand Up @@ -76,6 +78,10 @@
<assemblyIdentity name="WebGrease" publicKeyToken="31bf3856ad364e35" culture="neutral"/>
<bindingRedirect oldVersion="0.0.0.0-1.5.2.14234" newVersion="1.5.2.14234"/>
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="Newtonsoft.Json" culture="neutral" publicKeyToken="30ad4fe6b2a6aeed" />
<bindingRedirect oldVersion="0.0.0.0-6.0.0.0" newVersion="6.0.0.0" />
</dependentAssembly>
</assemblyBinding>
</runtime>
</configuration>

0 comments on commit 431b2f1

Please sign in to comment.