Skip to content

Commit

Permalink
XForm: Refactor HttpService slightly to allow other hosts (sort of).
Browse files Browse the repository at this point in the history
  • Loading branch information
Scott Louvau committed Jan 30, 2018
1 parent c5bad75 commit 5c593fa
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 19 deletions.
6 changes: 5 additions & 1 deletion .gitignore
Expand Up @@ -232,4 +232,8 @@ DiskCache/
# Exclude Arriba Database Configurations
Arriba/Databases/*
!Arriba/Databases/Louvau/
!Arriba/Databases/Walkthrough/
!Arriba/Databases/Walkthrough/

# Exclude Arriba.HttpShim
Arriba.HttpShim/
Arriba.HttpShim.sln
44 changes: 39 additions & 5 deletions XForm/XForm/BackgroundWebServer.cs
Expand Up @@ -11,6 +11,40 @@

namespace XForm
{
/// <summary>
/// IHttpResponse is a generic interface for interacting with System.Net.HttpListenerResponse and System.Net.Http.HttpResponseMessage uniformly.
/// </summary>
public interface IHttpResponse
{
HttpStatusCode StatusCode { get; set; }
string ContentType { get; set; }
Stream OutputStream { get; }
}

public class HttpListenerResponseWrapper : IHttpResponse
{
private HttpListenerResponse Response;

public HttpListenerResponseWrapper(HttpListenerResponse response)
{
this.Response = response;
}

public HttpStatusCode StatusCode
{
get { return (HttpStatusCode)Response.StatusCode; }
set { Response.StatusCode = (int)value; }
}

public string ContentType
{
get { return Response.ContentType; }
set { Response.ContentType = value; }
}

public Stream OutputStream => Response.OutputStream;
}

/// <summary>
/// BackgroundWebServer handles Http Requests for the XForm engine on a background thread.
/// </summary>
Expand All @@ -28,7 +62,7 @@ public class BackgroundWebServer : IDisposable
private Thread ListenerThread { get; set; }

private string DefaultDocument { get; set; }
private Dictionary<string, Action<HttpListenerContext, HttpListenerResponse>> MethodsToServe { get; set; }
private Dictionary<string, Action<HttpListenerContext, IHttpResponse>> MethodsToServe { get; set; }
private Dictionary<string, string> FilesToServe { get; set; }
private string FolderToServe { get; set; }

Expand All @@ -37,7 +71,7 @@ public BackgroundWebServer(string defaultDocument, string serveUnderRelativePath
this.IsRunning = false;

this.DefaultDocument = defaultDocument;
this.MethodsToServe = new Dictionary<string, Action<HttpListenerContext, HttpListenerResponse>>(StringComparer.OrdinalIgnoreCase);
this.MethodsToServe = new Dictionary<string, Action<HttpListenerContext, IHttpResponse>>(StringComparer.OrdinalIgnoreCase);
this.FilesToServe = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);

if (!String.IsNullOrEmpty(serveUnderRelativePath))
Expand All @@ -50,7 +84,7 @@ public BackgroundWebServer(string defaultDocument, string serveUnderRelativePath
}
}

public void AddResponder(string url, Action<HttpListenerContext, HttpListenerResponse> itemWrite)
public void AddResponder(string url, Action<HttpListenerContext, IHttpResponse> itemWrite)
{
this.MethodsToServe[url] = itemWrite;
}
Expand Down Expand Up @@ -212,14 +246,14 @@ private bool ReturnDefaultDocument(string requestUri, HttpListenerResponse respo

private bool ReturnMethodItem(string requestUri, HttpListenerContext context, HttpListenerResponse response)
{
Action<HttpListenerContext, HttpListenerResponse> writeMethod;
Action<HttpListenerContext, IHttpResponse> writeMethod;
if (this.MethodsToServe.TryGetValue(requestUri, out writeMethod))
{
response.AddHeader("Cache-Control", "no-cache, no-store");
response.ContentType = ContentType(requestUri);
response.StatusCode = 200;

writeMethod(context, response);
writeMethod(context, new HttpListenerResponseWrapper(response));
response.Close();
return true;
}
Expand Down
20 changes: 9 additions & 11 deletions XForm/XForm/HttpService.cs
Expand Up @@ -42,7 +42,7 @@ public void Run()
}
}

private void Suggest(HttpListenerContext context, HttpListenerResponse response)
private void Suggest(HttpListenerContext context, IHttpResponse response)
{
using (ITabularWriter writer = WriterForFormat("json", response))
{
Expand Down Expand Up @@ -72,7 +72,7 @@ private void Suggest(HttpListenerContext context, HttpListenerResponse response)
}
}

private void Run(HttpListenerContext context, HttpListenerResponse response)
private void Run(HttpListenerContext context, IHttpResponse response)
{
try
{
Expand All @@ -93,7 +93,7 @@ private void Run(HttpListenerContext context, HttpListenerResponse response)
}
}

private void Download(HttpListenerContext context, HttpListenerResponse response)
private void Download(HttpListenerContext context, IHttpResponse response)
{
try
{
Expand All @@ -114,7 +114,7 @@ private void Download(HttpListenerContext context, HttpListenerResponse response
}
}

private void CountWithinTimeout(HttpListenerContext context, HttpListenerResponse response)
private void CountWithinTimeout(HttpListenerContext context, IHttpResponse response)
{
try
{
Expand All @@ -133,7 +133,7 @@ private void CountWithinTimeout(HttpListenerContext context, HttpListenerRespons
}
}

private void CountWithinTimeout(string query, TimeSpan timeout, DateTime asOfDate, HttpListenerResponse response)
private void CountWithinTimeout(string query, TimeSpan timeout, DateTime asOfDate, IHttpResponse response)
{
IXTable pipeline = null;

Expand Down Expand Up @@ -173,7 +173,7 @@ private void CountWithinTimeout(string query, TimeSpan timeout, DateTime asOfDat
}
}

private void Run(string query, string format, int rowCountLimit, int colCountLimit, DateTime asOfDate, HttpListenerResponse response)
private void Run(string query, string format, int rowCountLimit, int colCountLimit, DateTime asOfDate, IHttpResponse response)
{
IXTable pipeline = null;

Expand Down Expand Up @@ -214,7 +214,7 @@ private void Run(string query, string format, int rowCountLimit, int colCountLim
}
}

private void Save(HttpListenerContext context, HttpListenerResponse response)
private void Save(HttpListenerContext context, IHttpResponse response)
{
try
{
Expand All @@ -223,7 +223,7 @@ private void Save(HttpListenerContext context, HttpListenerResponse response)
Require(context, "name"));

// Success
response.StatusCode = 200;
response.StatusCode = HttpStatusCode.OK;
}
catch (Exception ex)
{
Expand All @@ -239,7 +239,7 @@ private void Save(string query, string tableName)
_xDatabaseContext.Runner.Save(query, tableName);
}

private ITabularWriter WriterForFormat(string format, HttpListenerResponse response)
private ITabularWriter WriterForFormat(string format, IHttpResponse response)
{
Stream toStream = response.OutputStream;
toStream = new BufferedStream(toStream, 64 * 1024);
Expand All @@ -249,10 +249,8 @@ private ITabularWriter WriterForFormat(string format, HttpListenerResponse respo
case "json":
return new JsonTabularWriter(toStream);
case "csv":
response.AddHeader("Content-Disposition", "attachment; filename=\"Result.csv\"");
return new CsvWriter(toStream);
case "tsv":
response.AddHeader("Content-Disposition", "attachment; filename=\"Result.tsv\"");
return new TsvWriter(toStream);
default:
throw new ArgumentException("fmt");
Expand Down
2 changes: 1 addition & 1 deletion XForm/XForm/IO/TabularFileWriter.cs
Expand Up @@ -133,7 +133,7 @@ public void Dispose()
_writer = null;

// On Dispose, tell the StreamProvider to publish the table
_streamProvider.Publish(_outputFilePath);
if(_streamProvider != null) _streamProvider.Publish(_outputFilePath);
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion XForm/XForm/XForm.csproj
Expand Up @@ -223,6 +223,6 @@
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<PropertyGroup>
<PostBuildEvent>$(SolutionDir)\Deploy.WebSite.cmd $(TargetDir)</PostBuildEvent>
<PostBuildEvent>$(ProjectDir)\..\Deploy.WebSite.cmd $(TargetDir)</PostBuildEvent>
</PropertyGroup>
</Project>

0 comments on commit 5c593fa

Please sign in to comment.