private void SendChunk(OSHttpRequest request, OSHttpResponse response, int X, int Y)
{
string range = request.Headers.GetOne("Range");
//m_log.DebugFormat("[GETTEXTURE]: Range {0}", range);
byte[] chunk = m_scene.Voxels.GetChunk(X, Y);
if (!String.IsNullOrEmpty(range))
{
// Range request
int start, end;
if (TryParseRange(range, out start, out end))
{
// Before clamping start make sure we can satisfy it in order to avoid
// sending back the last byte instead of an error status
if (start >= chunk.Length)
{
response.StatusCode = (int)System.Net.HttpStatusCode.RequestedRangeNotSatisfiable;
return;
}
end = Utils.Clamp(end, 0, chunk.Length - 1);
start = Utils.Clamp(start, 0, end);
int len = end - start + 1;
//m_log.Debug("Serving " + start + " to " + end + " of " + texture.Data.Length + " bytes for texture " + texture.ID);
if (len < chunk.Length)
response.StatusCode = (int)System.Net.HttpStatusCode.PartialContent;
response.ContentLength = len;
response.ContentType = "application/octet-stream";
response.AddHeader("Content-Range", String.Format("bytes {0}-{1}/{2}", start, end, chunk.Length));
response.Body.Write(chunk, start, len);
}
else
{
m_log.Warn("Malformed Range header: " + range);
response.StatusCode = (int)System.Net.HttpStatusCode.BadRequest;
}
}
else
{
// Full content request
response.ContentLength = chunk.Length;
response.ContentType = "application/octet-stream";
response.Body.Write(chunk, 0, chunk.Length);
}
}