public void WriteEntireResponseFromFile(String fileName, bool keepAlive)
{
if (!File.Exists(fileName)) {
WriteErrorAndClose(404);
return;
}
// Deny the request if the contentType cannot be recognized.
string contentTypeHeader = MakeContentTypeHeader(fileName);
if (contentTypeHeader == null) {
WriteErrorAndClose(403);
return;
}
bool completed = false;
FileStream fs = null;
try {
fs = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.Read);
int len = (int)fs.Length;
byte[] fileBytes = new byte[len];
int bytesRead = fs.Read(fileBytes, 0, len);
String headers = MakeResponseHeaders(200, contentTypeHeader, bytesRead, keepAlive);
_socket.Send(Encoding.UTF8.GetBytes(headers));
_socket.Send(fileBytes, 0, bytesRead, SocketFlags.None);
completed = true;
}
catch (SocketException) {
}
finally {
if (!keepAlive || !completed)
Close();
if (fs != null)
fs.Close();
}
}