move http range parser to util

0.9.1.0-post-fixes
UbitUmarov 2018-11-30 23:10:29 +00:00
parent 7554e62861
commit b783244a50
3 changed files with 38 additions and 68 deletions

View File

@ -101,9 +101,6 @@ namespace OpenSim.Capabilities.Handlers
return responsedata; return responsedata;
} }
Hashtable headers = new Hashtable();
responsedata["headers"] = headers;
string range = String.Empty; string range = String.Empty;
if (((Hashtable)request["headers"])["range"] != null) if (((Hashtable)request["headers"])["range"] != null)
@ -111,18 +108,18 @@ namespace OpenSim.Capabilities.Handlers
else if (((Hashtable)request["headers"])["Range"] != null) else if (((Hashtable)request["headers"])["Range"] != null)
range = (string)((Hashtable)request["headers"])["Range"]; range = (string)((Hashtable)request["headers"])["Range"];
responsedata["content_type"] = "application/vnd.ll.mesh";
if (String.IsNullOrEmpty(range)) if (String.IsNullOrEmpty(range))
{ {
// full mesh // full mesh
responsedata["str_response_string"] = Convert.ToBase64String(mesh.Data); responsedata["str_response_string"] = Convert.ToBase64String(mesh.Data);
responsedata["content_type"] = "application/vnd.ll.mesh";
responsedata["int_response_code"] = (int)System.Net.HttpStatusCode.OK; responsedata["int_response_code"] = (int)System.Net.HttpStatusCode.OK;
return responsedata; return responsedata;
} }
// range request // range request
int start, end; int start, end;
if (TryParseRange(range, out start, out end)) if (Util.TryParseHttpRange(range, out start, out end))
{ {
// Before clamping start make sure we can satisfy it in order to avoid // Before clamping start make sure we can satisfy it in order to avoid
// sending back the last byte instead of an error status // sending back the last byte instead of an error status
@ -137,8 +134,10 @@ namespace OpenSim.Capabilities.Handlers
int len = end - start + 1; int len = end - start + 1;
//m_log.Debug("Serving " + start + " to " + end + " of " + texture.Data.Length + " bytes for texture " + texture.ID); //m_log.Debug("Serving " + start + " to " + end + " of " + texture.Data.Length + " bytes for texture " + texture.ID);
responsedata["int_response_code"] = (int)System.Net.HttpStatusCode.PartialContent; Hashtable headers = new Hashtable();
headers["Content-Range"] = String.Format("bytes {0}-{1}/{2}", start, end, mesh.Data.Length); headers["Content-Range"] = String.Format("bytes {0}-{1}/{2}", start, end, mesh.Data.Length);
responsedata["headers"] = headers;
responsedata["int_response_code"] = (int)System.Net.HttpStatusCode.PartialContent;
byte[] d = new byte[len]; byte[] d = new byte[len];
Array.Copy(mesh.Data, start, d, 0, len); Array.Copy(mesh.Data, start, d, 0, len);
@ -149,25 +148,8 @@ namespace OpenSim.Capabilities.Handlers
m_log.Warn("[GETMESH]: Failed to parse a range from GetMesh request, sending full asset: " + (string)request["uri"]); m_log.Warn("[GETMESH]: Failed to parse a range from GetMesh request, sending full asset: " + (string)request["uri"]);
responsedata["str_response_string"] = Convert.ToBase64String(mesh.Data); responsedata["str_response_string"] = Convert.ToBase64String(mesh.Data);
responsedata["content_type"] = "application/vnd.ll.mesh";
responsedata["int_response_code"] = (int)System.Net.HttpStatusCode.OK; responsedata["int_response_code"] = (int)System.Net.HttpStatusCode.OK;
return responsedata; return responsedata;
} }
private bool TryParseRange(string header, out int start, out int end)
{
if (header.StartsWith("bytes="))
{
string[] rangeValues = header.Substring(6).Split('-');
if (rangeValues.Length == 2)
{
if (Int32.TryParse(rangeValues[0], out start) && Int32.TryParse(rangeValues[1], out end))
return true;
}
}
start = end = 0;
return false;
}
} }
} }

View File

@ -210,7 +210,7 @@ namespace OpenSim.Capabilities.Handlers
{ {
// Range request // Range request
int start, end; int start, end;
if (TryParseRange(range, out start, out end)) if (Util.TryParseHttpRange(range, out start, out end))
{ {
// Before clamping start make sure we can satisfy it in order to avoid // Before clamping start make sure we can satisfy it in order to avoid
// sending back the last byte instead of an error status // sending back the last byte instead of an error status
@ -289,50 +289,6 @@ namespace OpenSim.Capabilities.Handlers
// texture.FullID, range, response.StatusCode, response.ContentLength, texture.Data.Length); // texture.FullID, range, response.StatusCode, response.ContentLength, texture.Data.Length);
} }
/// <summary>
/// Parse a range header.
/// </summary>
/// <remarks>
/// As per http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html,
/// this obeys range headers with two values (e.g. 533-4165) and no second value (e.g. 533-).
/// Where there is no value, -1 is returned.
/// FIXME: Need to cover the case where only a second value is specified (e.g. -4165), probably by returning -1
/// for start.</remarks>
/// <returns></returns>
/// <param name='header'></param>
/// <param name='start'>Start of the range. Undefined if this was not a number.</param>
/// <param name='end'>End of the range. Will be -1 if no end specified. Undefined if there was a raw string but this was not a number.</param>
private bool TryParseRange(string header, out int start, out int end)
{
start = end = 0;
if (header.StartsWith("bytes="))
{
string[] rangeValues = header.Substring(6).Split('-');
if (rangeValues.Length == 2)
{
if (!Int32.TryParse(rangeValues[0], out start))
return false;
string rawEnd = rangeValues[1];
if (rawEnd == "")
{
end = -1;
return true;
}
else if (Int32.TryParse(rawEnd, out end))
{
return true;
}
}
}
start = end = 0;
return false;
}
private byte[] ConvertTextureData(AssetBase texture, string format) private byte[] ConvertTextureData(AssetBase texture, string format)
{ {
m_log.DebugFormat("[GETTEXTURE]: Converting texture {0} to {1}", texture.ID, format); m_log.DebugFormat("[GETTEXTURE]: Converting texture {0} to {1}", texture.ID, format);

View File

@ -2382,6 +2382,38 @@ namespace OpenSim.Framework
return sb.ToString(); return sb.ToString();
} }
public static bool TryParseHttpRange(string header, out int start, out int end)
{
start = end = 0;
if (header.StartsWith("bytes="))
{
string[] rangeValues = header.Substring(6).Split('-');
if (rangeValues.Length == 2)
{
string rawStart = rangeValues[0].Trim();
if (rawStart != "" && !Int32.TryParse(rawStart, out start))
return false;
if (start < 0)
return false;
string rawEnd = rangeValues[1].Trim();
if (rawEnd == "")
{
end = -1;
return true;
}
else if (Int32.TryParse(rawEnd, out end))
return end > 0;
}
}
start = end = 0;
return false;
}
/// <summary> /// <summary>
/// Used to trigger an early library load on Windows systems. /// Used to trigger an early library load on Windows systems.
/// </summary> /// </summary>