Created
April 12, 2018 01:00
-
-
Save mstefarov/8c5f775dc9185dc2d38a5ae59e6e8f80 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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