Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
// 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