move a materials cap to simpleStreamHandler

master
UbitUmarov 2020-04-24 17:50:13 +01:00
parent 84cd4b4808
commit 07caee4956
1 changed files with 62 additions and 44 deletions

View File

@ -28,8 +28,9 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.IO; using System.IO;
using System.Net;
using System.Reflection; using System.Reflection;
using System.Security.Cryptography; // for computing md5 hash using System.Text;
using log4net; using log4net;
using Mono.Addins; using Mono.Addins;
using Nini.Config; using Nini.Config;
@ -38,7 +39,6 @@ using OpenMetaverse;
using OpenMetaverse.StructuredData; using OpenMetaverse.StructuredData;
using OpenSim.Framework; using OpenSim.Framework;
using OpenSim.Framework.Servers;
using OpenSim.Framework.Servers.HttpServer; using OpenSim.Framework.Servers.HttpServer;
using OpenSim.Region.Framework.Interfaces; using OpenSim.Region.Framework.Interfaces;
using OpenSim.Region.Framework.Scenes; using OpenSim.Region.Framework.Scenes;
@ -225,35 +225,33 @@ namespace OpenSim.Region.OptionalModules.Materials
} }
} }
private void OnRegisterCaps(OpenMetaverse.UUID agentID, OpenSim.Framework.Capabilities.Caps caps) private void OnRegisterCaps(UUID agentID, OpenSim.Framework.Capabilities.Caps caps)
{ {
string capsBase = "/CAPS/" + caps.CapsObjectPath + "/"; caps.RegisterSimpleHandler("RenderMaterials",
new SimpleStreamHandler("/" + UUID.Random(),
(httpRequest, httpResponse)
=> preprocess(httpRequest, httpResponse,agentID)
));
}
IRequestHandler renderMaterialsPostHandler private void preprocess(IOSHttpRequest request, IOSHttpResponse response, UUID agentID)
= new RestStreamHandler("POST", capsBase, {
(request, path, param, httpRequest, httpResponse) switch(request.HttpMethod)
=> RenderMaterialsPostCap(request, agentID), {
"RenderMaterials", null); case "GET":
caps.RegisterHandler("RenderMaterials", renderMaterialsPostHandler); RenderMaterialsGetCap(request, response);
break;
// OpenSimulator CAPs infrastructure seems to be somewhat hostile towards any CAP that requires both GET case "PUT":
// and POST handlers, (at least at the time this was originally written), so we first set up a POST RenderMaterialsPutCap(request, response, agentID);
// handler normally and then add a GET handler via MainServer break;
case "POST":
IRequestHandler renderMaterialsGetHandler RenderMaterialsPostCap(request, response, agentID);
= new RestStreamHandler("GET", capsBase, break;
(request, path, param, httpRequest, httpResponse) default:
=> RenderMaterialsGetCap(request), response.StatusCode = (int)HttpStatusCode.NotFound;
"RenderMaterials", null); return;
MainServer.Instance.AddStreamHandler(renderMaterialsGetHandler); }
response.StatusCode = (int)HttpStatusCode.OK;
// materials viewer seems to use either POST or PUT, so assign POST handler for PUT as well
IRequestHandler renderMaterialsPutHandler
= new RestStreamHandler("PUT", capsBase,
(request, path, param, httpRequest, httpResponse)
=> RenderMaterialsPutCap(request, agentID),
"RenderMaterials", null);
MainServer.Instance.AddStreamHandler(renderMaterialsPutHandler);
} }
private void OnSimulatorFeaturesRequest(UUID agentID, ref OSDMap features) private void OnSimulatorFeaturesRequest(UUID agentID, ref OSDMap features)
@ -479,10 +477,18 @@ namespace OpenSim.Region.OptionalModules.Materials
} }
} }
public string RenderMaterialsPostCap(string request, UUID agentID) public void RenderMaterialsPostCap(IOSHttpRequest request, IOSHttpResponse response, UUID agentID)
{ {
OSDMap req = (OSDMap)OSDParser.DeserializeLLSDXml(request); OSDMap req;
OSDMap resp = new OSDMap(); try
{
req = (OSDMap)OSDParser.DeserializeLLSDXml(request.InputStream);
}
catch
{
response.StatusCode = (int)HttpStatusCode.BadRequest;
return;
}
OSDArray respArr = new OSDArray(); OSDArray respArr = new OSDArray();
OSD tmpOSD; OSD tmpOSD;
@ -536,26 +542,34 @@ namespace OpenSim.Region.OptionalModules.Materials
catch (Exception e) catch (Exception e)
{ {
m_log.Warn("[Materials]: exception decoding zipped CAP payload ", e); m_log.Warn("[Materials]: exception decoding zipped CAP payload ", e);
//return ""; response.StatusCode = (int)HttpStatusCode.BadRequest;
return;
} }
} }
OSDMap resp = new OSDMap();
resp["Zipped"] = ZCompressOSD(respArr, false); resp["Zipped"] = ZCompressOSD(respArr, false);
string response = OSDParser.SerializeLLSDXmlString(resp); response.RawBuffer = Encoding.UTF8.GetBytes(OSDParser.SerializeLLSDXmlString(resp));
//m_log.Debug("[Materials]: cap request: " + request); //m_log.Debug("[Materials]: cap request: " + request);
//m_log.Debug("[Materials]: cap request (zipped portion): " + ZippedOsdBytesToString(req["Zipped"].AsBinary())); //m_log.Debug("[Materials]: cap request (zipped portion): " + ZippedOsdBytesToString(req["Zipped"].AsBinary()));
//m_log.Debug("[Materials]: cap response: " + response); //m_log.Debug("[Materials]: cap response: " + response);
return response;
} }
public string RenderMaterialsPutCap(string request, UUID agentID) public void RenderMaterialsPutCap(IOSHttpRequest request, IOSHttpResponse response, UUID agentID)
{ {
OSDMap req = (OSDMap)OSDParser.DeserializeLLSDXml(request); OSDMap req;
OSDMap resp = new OSDMap(); try
{
req = (OSDMap)OSDParser.DeserializeLLSDXml(request.InputStream);
}
catch
{
response.StatusCode = (int)HttpStatusCode.BadRequest;
return;
}
OSDMap materialsFromViewer = null; OSDMap materialsFromViewer = null;
OSDArray respArr = new OSDArray(); OSDArray respArr = new OSDArray();
OSD tmpOSD; OSD tmpOSD;
@ -707,6 +721,8 @@ namespace OpenSim.Region.OptionalModules.Materials
catch (Exception e) catch (Exception e)
{ {
m_log.Warn("[Materials]: exception processing received material ", e); m_log.Warn("[Materials]: exception processing received material ", e);
response.StatusCode = (int)HttpStatusCode.BadRequest;
return;
} }
} }
} }
@ -714,17 +730,19 @@ namespace OpenSim.Region.OptionalModules.Materials
catch (Exception e) catch (Exception e)
{ {
m_log.Warn("[Materials]: exception decoding zipped CAP payload ", e); m_log.Warn("[Materials]: exception decoding zipped CAP payload ", e);
//return ""; response.StatusCode = (int)HttpStatusCode.BadRequest;
return;
} }
} }
OSDMap resp = new OSDMap();
resp["Zipped"] = ZCompressOSD(respArr, false); resp["Zipped"] = ZCompressOSD(respArr, false);
string response = OSDParser.SerializeLLSDXmlString(resp); response.RawBuffer = Encoding.UTF8.GetBytes(OSDParser.SerializeLLSDXmlString(resp));
//m_log.Debug("[Materials]: cap request: " + request); //m_log.Debug("[Materials]: cap request: " + request);
//m_log.Debug("[Materials]: cap request (zipped portion): " + ZippedOsdBytesToString(req["Zipped"].AsBinary())); //m_log.Debug("[Materials]: cap request (zipped portion): " + ZippedOsdBytesToString(req["Zipped"].AsBinary()));
//m_log.Debug("[Materials]: cap response: " + response); //m_log.Debug("[Materials]: cap response: " + response);
return response;
} }
private AssetBase MakeAsset(FaceMaterial fm, bool local) private AssetBase MakeAsset(FaceMaterial fm, bool local)
@ -740,7 +758,7 @@ namespace OpenSim.Region.OptionalModules.Materials
return asset; return asset;
} }
public string RenderMaterialsGetCap(string request) public void RenderMaterialsGetCap(IOSHttpRequest request, IOSHttpResponse response)
{ {
OSDMap resp = new OSDMap(); OSDMap resp = new OSDMap();
OSDArray allOsd = new OSDArray(); OSDArray allOsd = new OSDArray();
@ -762,7 +780,7 @@ namespace OpenSim.Region.OptionalModules.Materials
*/ */
resp["Zipped"] = ZCompressOSD(allOsd, false); resp["Zipped"] = ZCompressOSD(allOsd, false);
return OSDParser.SerializeLLSDXmlString(resp); response.RawBuffer = Encoding.UTF8.GetBytes(OSDParser.SerializeLLSDXmlString(resp));
} }
private static string ZippedOsdBytesToString(byte[] bytes) private static string ZippedOsdBytesToString(byte[] bytes)