public WriteEntireResponseFromFile ( String fileName, bool keepAlive ) : void | ||
fileName | String | |
keepAlive | bool | |
return | void |
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();
}
}
public void Process() { // read the request if (!TryParseRequest()) { return; } // 100 response to POST if (_verb == "POST" && _contentLength > 0 && _preloadedContentLength < _contentLength) { _connection.Write100Continue(); } // special case for client script if (_isClientScriptPath) { _connection.WriteEntireResponseFromFile(_host.PhysicalClientScriptPath + _path.Substring(_host.NormalizedClientScriptPath.Length), false); return; } // deny access to code, bin, etc. if (IsRequestForRestrictedDirectory()) { _connection.WriteErrorAndClose(403); return; } // special case for a request to a directory (ensure / at the end and process default documents) if (ProcessDirectoryRequest()) { return; } PrepareResponse(); // Hand the processing over to HttpRuntime HttpRuntime.ProcessRequest(this); }