* Patch from jhurliman to add accept type recognition to determine the llsd content format. *should allow both json and xml serializations of llsd to work properly.

Signed-off-by: Teravus Ovares (Dan Olivares) <teravus@gmail.com>
arthursv
Teravus Ovares (Dan Olivares) 2009-08-05 16:47:48 -04:00
parent bc6de6f5f0
commit 535cb0efc1
1 changed files with 67 additions and 7 deletions

View File

@ -457,6 +457,10 @@ namespace OpenSim.Framework.Servers.HttpServer
// This has to be here to prevent a Linux/Mono crash // This has to be here to prevent a Linux/Mono crash
m_log.WarnFormat("[BASE HTTP SERVER] XmlRpcRequest issue {0}.\nNOTE: this may be spurious on Linux.", e); m_log.WarnFormat("[BASE HTTP SERVER] XmlRpcRequest issue {0}.\nNOTE: this may be spurious on Linux.", e);
} }
catch (IOException e)
{
m_log.Warn("[BASE HTTP SERVER] XmlRpcRequest issue: " + e.Message);
}
return; return;
} }
@ -464,7 +468,8 @@ namespace OpenSim.Framework.Servers.HttpServer
{ {
foreach (string strAccept in request.AcceptTypes) foreach (string strAccept in request.AcceptTypes)
{ {
if (strAccept.Contains("application/llsd+xml")) if (strAccept.Contains("application/llsd+xml") ||
strAccept.Contains("application/llsd+json"))
{ {
//m_log.Info("[Debug BASE HTTP SERVER]: Found an application/llsd+xml accept header"); //m_log.Info("[Debug BASE HTTP SERVER]: Found an application/llsd+xml accept header");
HandleLLSDRequests(request, response); HandleLLSDRequests(request, response);
@ -483,12 +488,14 @@ namespace OpenSim.Framework.Servers.HttpServer
case "application/llsd+xml": case "application/llsd+xml":
case "application/xml+llsd": case "application/xml+llsd":
case "application/llsd+json":
//m_log.Info("[Debug BASE HTTP SERVER]: found a application/llsd+xml content type"); //m_log.Info("[Debug BASE HTTP SERVER]: found a application/llsd+xml content type");
HandleLLSDRequests(request, response); HandleLLSDRequests(request, response);
return; return;
case "text/xml": case "text/xml":
case "application/xml": case "application/xml":
case "application/json":
default: default:
//m_log.Info("[Debug BASE HTTP SERVER]: in default handler"); //m_log.Info("[Debug BASE HTTP SERVER]: in default handler");
// Point of note.. the DoWeHaveA methods check for an EXACT path // Point of note.. the DoWeHaveA methods check for an EXACT path
@ -529,9 +536,9 @@ namespace OpenSim.Framework.Servers.HttpServer
// with the minimum first // with the minimum first
m_log.WarnFormat("[BASE HTTP SERVER]: HandleRequest threw {0}.\nNOTE: this may be spurious on Linux", e); m_log.WarnFormat("[BASE HTTP SERVER]: HandleRequest threw {0}.\nNOTE: this may be spurious on Linux", e);
} }
catch (EndOfStreamException e) catch (IOException e)
{ {
m_log.ErrorFormat("[BASE HTTP SERVER]: HandleRequest() threw {0}", e); m_log.ErrorFormat("[BASE HTTP SERVER] HandleRequest() threw ", e);
} }
catch (InvalidOperationException e) catch (InvalidOperationException e)
{ {
@ -760,6 +767,10 @@ namespace OpenSim.Framework.Servers.HttpServer
// This has to be here to prevent a Linux/Mono crash // This has to be here to prevent a Linux/Mono crash
m_log.WarnFormat("[BASE HTTP SERVER] XmlRpcRequest issue {0}.\nNOTE: this may be spurious on Linux.", e); m_log.WarnFormat("[BASE HTTP SERVER] XmlRpcRequest issue {0}.\nNOTE: this may be spurious on Linux.", e);
} }
catch (IOException e)
{
m_log.Warn("[BASE HTTP SERVER] XmlRpcRequest issue: " + e.Message);
}
} }
return; return;
//responseString = "Error"; //responseString = "Error";
@ -793,6 +804,10 @@ namespace OpenSim.Framework.Servers.HttpServer
// This has to be here to prevent a Linux/Mono crash // This has to be here to prevent a Linux/Mono crash
m_log.WarnFormat("[BASE HTTP SERVER] XmlRpcRequest issue {0}.\nNOTE: this may be spurious on Linux.", e); m_log.WarnFormat("[BASE HTTP SERVER] XmlRpcRequest issue {0}.\nNOTE: this may be spurious on Linux.", e);
} }
catch (IOException e)
{
m_log.Warn("[BASE HTTP SERVER] XmlRpcRequest issue: " + e.Message);
}
} }
} }
@ -823,7 +838,7 @@ namespace OpenSim.Framework.Servers.HttpServer
} }
try try
{ {
llsdRequest = OSDParser.DeserializeLLSDXml(requestBody); llsdRequest = OSDParser.Deserialize(requestBody);
} }
catch (Exception ex) catch (Exception ex)
{ {
@ -873,10 +888,10 @@ namespace OpenSim.Framework.Servers.HttpServer
} }
else else
{ {
response.ContentType = "application/llsd+xml"; // Select an appropriate response format
//m_log.Info("[Debug BASE HTTP SERVER]: Response: " + llsdResponse.ToString()); buffer = BuildLLSDResponse(request, response, llsdResponse);
buffer = OSDParser.SerializeLLSDXmlBytes(llsdResponse);
} }
response.SendChunked = false; response.SendChunked = false;
response.ContentLength64 = buffer.Length; response.ContentLength64 = buffer.Length;
response.ContentEncoding = Encoding.UTF8; response.ContentEncoding = Encoding.UTF8;
@ -912,6 +927,47 @@ namespace OpenSim.Framework.Servers.HttpServer
} }
} }
private byte[] BuildLLSDResponse(OSHttpRequest request, OSHttpResponse response, OSD llsdResponse)
{
if (request.AcceptTypes != null && request.AcceptTypes.Length > 0)
{
foreach (string strAccept in request.AcceptTypes)
{
switch (strAccept)
{
case "application/llsd+xml":
case "application/xml":
case "text/xml":
response.ContentType = strAccept;
return OSDParser.SerializeLLSDXmlBytes(llsdResponse);
case "application/llsd+json":
case "application/json":
response.ContentType = strAccept;
return Encoding.UTF8.GetBytes(OSDParser.SerializeJsonString(llsdResponse));
}
}
}
if (!String.IsNullOrEmpty(request.ContentType))
{
switch (request.ContentType)
{
case "application/llsd+xml":
case "application/xml":
case "text/xml":
response.ContentType = request.ContentType;
return OSDParser.SerializeLLSDXmlBytes(llsdResponse);
case "application/llsd+json":
case "application/json":
response.ContentType = request.ContentType;
return Encoding.UTF8.GetBytes(OSDParser.SerializeJsonString(llsdResponse));
}
}
response.ContentType = "application/llsd+json";
return Encoding.UTF8.GetBytes(OSDParser.SerializeJsonString(llsdResponse));
}
/// <summary> /// <summary>
/// Checks if we have an Exact path in the LLSD handlers for the path provided /// Checks if we have an Exact path in the LLSD handlers for the path provided
/// </summary> /// </summary>
@ -1404,6 +1460,10 @@ namespace OpenSim.Framework.Servers.HttpServer
// This has to be here to prevent a Linux/Mono crash // This has to be here to prevent a Linux/Mono crash
m_log.WarnFormat("[BASE HTTP SERVER] XmlRpcRequest issue {0}.\nNOTE: this may be spurious on Linux.", e); m_log.WarnFormat("[BASE HTTP SERVER] XmlRpcRequest issue {0}.\nNOTE: this may be spurious on Linux.", e);
} }
catch (IOException e)
{
m_log.Warn("[BASE HTTP SERVER] XmlRpcRequest issue: " + e.Message);
}
} }
} }