Skip to content

Instantly share code, notes, and snippets.

@mstefarov
Created April 12, 2018 01:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mstefarov/8c5f775dc9185dc2d38a5ae59e6e8f80 to your computer and use it in GitHub Desktop.
Save mstefarov/8c5f775dc9185dc2d38a5ae59e6e8f80 to your computer and use it in GitHub Desktop.
// Redirects requests to a given file
class FileBasedHttpResponder : HttpClientHandler
{
private readonly FileInfo _targetFileInfo;
public FileBasedHttpResponder(string targetPath)
{
_targetFileInfo = new FileInfo(targetPath);
}
protected override Task<HttpResponseMessage> SendAsync(
HttpRequestMessage request,
CancellationToken cancellationToken)
{
HttpResponseMessage response;
if (_targetFileInfo.Exists)
{
response = new HttpResponseMessage(HttpStatusCode.OK)
{
RequestMessage = request,
Content = new StreamContent(_targetFileInfo.OpenRead())
};
}
else
{
response = new HttpResponseMessage(HttpStatusCode.NotFound);
}
return Task.FromResult(response);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment