UpdateItemAsset.cs 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437
  1. using System;
  2. using System.Collections;
  3. using System.IO;
  4. using System.Net;
  5. using System.Text;
  6. using System.Threading;
  7. using OpenMetaverse;
  8. using OpenMetaverse.StructuredData;
  9. using OpenSim.Framework;
  10. using OpenSim.Framework.Capabilities;
  11. using OpenSim.Region.Framework.Scenes;
  12. using OpenSim.Framework.Servers.HttpServer;
  13. using OSDMap = OpenMetaverse.StructuredData.OSDMap;
  14. namespace OpenSim.Region.ClientStack.Linden
  15. {
  16. public delegate UUID UpdateItem(UUID itemID, UUID objectID, byte[] data);
  17. public delegate UUID ItemUpdatedCallback(UUID userID, UUID itemID, UUID objectID, byte[] data);
  18. public partial class BunchOfCaps
  19. {
  20. public void UpdateNotecardItemAsset(IOSHttpRequest httpRequest, IOSHttpResponse httpResponse, OSDMap map)
  21. {
  22. UpdateInventoryItemAsset(httpRequest, httpResponse, map, (byte)AssetType.Notecard);
  23. }
  24. public void UpdateAnimSetItemAsset(IOSHttpRequest httpRequest, IOSHttpResponse httpResponse, OSDMap map)
  25. {
  26. //UpdateInventoryItemAsset(httpRequest, httpResponse, map, CustomInventoryType.AnimationSet);
  27. }
  28. public void UpdateScriptItemAsset(IOSHttpRequest httpRequest, IOSHttpResponse httpResponse, OSDMap map)
  29. {
  30. UpdateInventoryItemAsset(httpRequest, httpResponse, map, (byte)AssetType.LSLText);
  31. }
  32. public void UpdateSettingsItemAsset(IOSHttpRequest httpRequest, IOSHttpResponse httpResponse, OSDMap map)
  33. {
  34. UpdateInventoryItemAsset(httpRequest, httpResponse, map, (byte)AssetType.Settings);
  35. }
  36. public void UpdateMaterialItemAsset(IOSHttpRequest httpRequest, IOSHttpResponse httpResponse, OSDMap map)
  37. {
  38. UpdateInventoryItemAsset(httpRequest, httpResponse, map, (byte)AssetType.Material);
  39. }
  40. public void UpdateGestureItemAsset(IOSHttpRequest httpRequest, IOSHttpResponse httpResponse, OSDMap map)
  41. {
  42. UpdateInventoryItemAsset(httpRequest, httpResponse, map, (byte)AssetType.Gesture);
  43. }
  44. private void UpdateInventoryItemAsset(IOSHttpRequest httpRequest, IOSHttpResponse httpResponse, OSDMap map, byte atype, bool taskSript = false)
  45. {
  46. m_log.Debug("[CAPS]: UpdateInventoryItemAsset Request in region: " + m_regionName + "\n");
  47. httpResponse.StatusCode = (int)HttpStatusCode.OK;
  48. UUID itemID = UUID.Zero;
  49. UUID objectID = UUID.Zero;
  50. try
  51. {
  52. if (map.TryGetValue("item_id", out OSD itmp))
  53. itemID = itmp;
  54. if (map.TryGetValue("task_id", out OSD tmp))
  55. objectID = tmp;
  56. }
  57. catch { }
  58. if (itemID.IsZero())
  59. {
  60. LLSDAssetUploadError error = new LLSDAssetUploadError();
  61. error.message = "failed to recode request";
  62. error.identifier = UUID.Zero;
  63. httpResponse.RawBuffer = Util.UTF8NBGetbytes(LLSDHelpers.SerialiseLLSDReply(error));
  64. return;
  65. }
  66. if (!objectID.IsZero())
  67. {
  68. SceneObjectPart sop = m_Scene.GetSceneObjectPart(objectID);
  69. if (sop == null)
  70. {
  71. LLSDAssetUploadError error = new LLSDAssetUploadError();
  72. error.message = "object not found";
  73. error.identifier = UUID.Zero;
  74. httpResponse.RawBuffer = Util.UTF8NBGetbytes(LLSDHelpers.SerialiseLLSDReply(error));
  75. return;
  76. }
  77. if (!m_Scene.Permissions.CanEditObjectInventory(objectID, m_AgentID))
  78. {
  79. LLSDAssetUploadError error = new LLSDAssetUploadError();
  80. error.message = "No permissions to edit objec";
  81. error.identifier = UUID.Zero;
  82. httpResponse.RawBuffer = Util.UTF8NBGetbytes(LLSDHelpers.SerialiseLLSDReply(error));
  83. return;
  84. }
  85. }
  86. string uploaderPath = GetNewCapPath();
  87. string protocol = m_HostCapsObj.SSLCaps ? "https://" : "http://";
  88. string uploaderURL = protocol + m_HostCapsObj.HostName + ":" + m_HostCapsObj.Port.ToString() + uploaderPath;
  89. LLSDAssetUploadResponse uploadResponse = new LLSDAssetUploadResponse();
  90. uploadResponse.uploader = uploaderURL;
  91. uploadResponse.state = "upload";
  92. ItemUpdater uploader = new ItemUpdater(itemID, objectID, atype, uploaderPath, m_HostCapsObj.HttpListener, m_dumpAssetsToFile);
  93. uploader.m_remoteAdress = httpRequest.RemoteIPEndPoint.Address;
  94. uploader.OnUpLoad += ItemUpdated;
  95. var uploaderHandler = new SimpleBinaryHandler("POST", uploaderPath, uploader.process);
  96. uploaderHandler.MaxDataSize = 10000000; // change per asset type?
  97. m_HostCapsObj.HttpListener.AddSimpleStreamHandler(uploaderHandler);
  98. // m_log.InfoFormat("[CAPS]: UpdateAgentInventoryAsset response: {0}",
  99. // LLSDHelpers.SerialiseLLSDReply(uploadResponse)));
  100. httpResponse.RawBuffer = Util.UTF8NBGetbytes(LLSDHelpers.SerialiseLLSDReply(uploadResponse));
  101. }
  102. /// <summary>
  103. /// Called when new asset data for an inventory item update has been uploaded.
  104. /// </summary>
  105. /// <param name="itemID">Item to update</param>
  106. /// <param name="data">New asset data</param>
  107. /// <returns></returns>
  108. public UUID ItemUpdated(UUID itemID, UUID objectID, byte[] data)
  109. {
  110. if (ItemUpdatedCall != null)
  111. {
  112. return ItemUpdatedCall(m_HostCapsObj.AgentID, itemID, objectID, data);
  113. }
  114. return UUID.Zero;
  115. }
  116. /// <summary>
  117. /// Called by the script task update handler. Provides a URL to which the client can upload a new asset.
  118. /// </summary>
  119. /// <param name="httpRequest">HTTP request header object</param>
  120. /// <param name="httpResponse">HTTP response header object</param>
  121. /// <returns></returns>
  122. public void UpdateScriptTaskInventory(IOSHttpRequest httpRequest, IOSHttpResponse httpResponse, OSDMap map)
  123. {
  124. httpResponse.StatusCode = (int)HttpStatusCode.OK;
  125. try
  126. {
  127. //m_log.Debug("[CAPS]: ScriptTaskInventory Request in region: " + m_regionName);
  128. //m_log.DebugFormat("[CAPS]: request: {0}, path: {1}, param: {2}", request, path, param);
  129. UUID itemID = UUID.Zero;
  130. UUID objectID = UUID.Zero;
  131. bool is_script_running = false;
  132. OSD tmp;
  133. try
  134. {
  135. if (map.TryGetValue("item_id", out tmp))
  136. itemID = tmp;
  137. if (map.TryGetValue("task_id", out tmp))
  138. objectID = tmp;
  139. if (map.TryGetValue("is_script_running", out tmp))
  140. is_script_running = tmp;
  141. }
  142. catch { }
  143. if (itemID.IsZero() || objectID.IsZero())
  144. {
  145. LLSDAssetUploadError error = new LLSDAssetUploadError();
  146. error.message = "failed to recode request";
  147. error.identifier = UUID.Zero;
  148. httpResponse.RawBuffer = Util.UTF8NBGetbytes(LLSDHelpers.SerialiseLLSDReply(error));
  149. return;
  150. }
  151. SceneObjectPart sop = m_Scene.GetSceneObjectPart(objectID);
  152. if (sop == null)
  153. {
  154. LLSDAssetUploadError error = new LLSDAssetUploadError();
  155. error.message = "object not found";
  156. error.identifier = UUID.Zero;
  157. httpResponse.RawBuffer = Util.UTF8NBGetbytes(LLSDHelpers.SerialiseLLSDReply(error));
  158. return;
  159. }
  160. if (!m_Scene.Permissions.CanEditObjectInventory(objectID, m_AgentID))
  161. {
  162. LLSDAssetUploadError error = new LLSDAssetUploadError();
  163. error.message = "No permissions to edit objec";
  164. error.identifier = UUID.Zero;
  165. httpResponse.RawBuffer = Util.UTF8NBGetbytes(LLSDHelpers.SerialiseLLSDReply(error));
  166. return;
  167. }
  168. if (!m_Scene.Permissions.CanEditScript(itemID, objectID, m_AgentID))
  169. {
  170. LLSDAssetUploadError error = new LLSDAssetUploadError();
  171. error.message = "No permissions to edit script";
  172. error.identifier = UUID.Zero;
  173. httpResponse.RawBuffer = Util.UTF8NBGetbytes(LLSDHelpers.SerialiseLLSDReply(error));
  174. return;
  175. }
  176. string uploaderPath = GetNewCapPath();
  177. string protocol = m_HostCapsObj.SSLCaps ? "https://" : "http://";
  178. string uploaderURL = protocol + m_HostCapsObj.HostName + ":" + m_HostCapsObj.Port.ToString() + uploaderPath;
  179. LLSDAssetUploadResponse uploadResponse = new LLSDAssetUploadResponse();
  180. uploadResponse.uploader = uploaderURL;
  181. uploadResponse.state = "upload";
  182. TaskInventoryScriptUpdater uploader = new TaskInventoryScriptUpdater(itemID, objectID, is_script_running,
  183. uploaderPath, m_HostCapsObj.HttpListener, httpRequest.RemoteIPEndPoint.Address, m_dumpAssetsToFile);
  184. uploader.OnUpLoad += TaskScriptUpdated;
  185. var uploaderHandler = new SimpleBinaryHandler("POST", uploaderPath, uploader.process);
  186. uploaderHandler.MaxDataSize = 10000000; // change per asset type?
  187. m_HostCapsObj.HttpListener.AddSimpleStreamHandler(uploaderHandler);
  188. // m_log.InfoFormat("[CAPS]: " +
  189. // "ScriptTaskInventory response: {0}",
  190. // LLSDHelpers.SerialiseLLSDReply(uploadResponse)));
  191. httpResponse.RawBuffer = Util.UTF8NBGetbytes(LLSDHelpers.SerialiseLLSDReply(uploadResponse));
  192. }
  193. catch (Exception e)
  194. {
  195. m_log.Error("[UpdateScriptTaskInventory]: " + e.ToString());
  196. }
  197. }
  198. /// <summary>
  199. /// Called when new asset data for an agent inventory item update has been uploaded.
  200. /// </summary>
  201. /// <param name="itemID">Item to update</param>
  202. /// <param name="primID">Prim containing item to update</param>
  203. /// <param name="isScriptRunning">Signals whether the script to update is currently running</param>
  204. /// <param name="data">New asset data</param>
  205. public void TaskScriptUpdated(UUID itemID, UUID primID, bool isScriptRunning, byte[] data, ref ArrayList errors)
  206. {
  207. if (TaskScriptUpdatedCall != null)
  208. {
  209. ArrayList e = TaskScriptUpdatedCall(m_HostCapsObj.AgentID, itemID, primID, isScriptRunning, data);
  210. foreach (Object item in e)
  211. errors.Add(item);
  212. }
  213. }
  214. static public bool ValidateAssetData(byte assetType, byte[] data)
  215. {
  216. return true;
  217. }
  218. /// <summary>
  219. /// This class is a callback invoked when a client sends asset data to
  220. /// an agent inventory notecard update url
  221. /// </summary>
  222. public class ItemUpdater : ExpiringCapBase
  223. {
  224. public event UpdateItem OnUpLoad = null;
  225. private UUID m_inventoryItemID;
  226. private UUID m_objectID;
  227. private bool m_dumpAssetToFile;
  228. public IPAddress m_remoteAdress;
  229. private byte m_assetType;
  230. public ItemUpdater(UUID inventoryItem, UUID objectid, byte aType, string path, IHttpServer httpServer, bool dumpAssetToFile):
  231. base(httpServer, path)
  232. {
  233. m_dumpAssetToFile = dumpAssetToFile;
  234. m_inventoryItemID = inventoryItem;
  235. m_objectID = objectid;
  236. m_httpListener = httpServer;
  237. m_assetType = aType;
  238. Start(30000);
  239. }
  240. /// <summary>
  241. /// Handle raw uploaded asset data.
  242. /// </summary>
  243. /// <param name="data"></param>
  244. /// <param name="path"></param>
  245. /// <param name="param"></param>
  246. /// <returns></returns>
  247. public void process(IOSHttpRequest request, IOSHttpResponse response, byte[] data)
  248. {
  249. Stop();
  250. if (!request.RemoteIPEndPoint.Address.Equals(m_remoteAdress))
  251. {
  252. response.StatusCode = (int)HttpStatusCode.Unauthorized;
  253. return;
  254. }
  255. string res = String.Empty;
  256. if (OnUpLoad == null)
  257. {
  258. response.StatusCode = (int)HttpStatusCode.Gone;
  259. return;
  260. }
  261. if (!BunchOfCaps.ValidateAssetData(m_assetType, data))
  262. {
  263. response.StatusCode = (int)HttpStatusCode.BadRequest;
  264. return;
  265. }
  266. UUID assetID = OnUpLoad(m_inventoryItemID, m_objectID, data);
  267. if (assetID.IsZero())
  268. {
  269. LLSDAssetUploadError uperror = new LLSDAssetUploadError();
  270. uperror.message = "Failed to update inventory item asset";
  271. uperror.identifier = m_inventoryItemID;
  272. res = LLSDHelpers.SerialiseLLSDReply(uperror);
  273. }
  274. else
  275. {
  276. LLSDAssetUploadComplete uploadComplete = new LLSDAssetUploadComplete();
  277. uploadComplete.new_asset = assetID.ToString();
  278. uploadComplete.new_inventory_item = m_inventoryItemID;
  279. uploadComplete.state = "complete";
  280. res = LLSDHelpers.SerialiseLLSDReply(uploadComplete);
  281. }
  282. if (m_dumpAssetToFile)
  283. {
  284. Util.SaveAssetToFile("updateditem" + Random.Shared.Next(1, 1000) + ".dat", data);
  285. }
  286. response.StatusCode = (int)HttpStatusCode.OK;
  287. response.RawBuffer = Util.UTF8NBGetbytes(res);
  288. }
  289. }
  290. /// <summary>
  291. /// This class is a callback invoked when a client sends asset data to
  292. /// a task inventory script update url
  293. /// </summary>
  294. public class TaskInventoryScriptUpdater : ExpiringCapBase
  295. {
  296. public event UpdateTaskScript OnUpLoad;
  297. private UUID m_inventoryItemID;
  298. private UUID m_primID;
  299. private bool m_isScriptRunning;
  300. private bool m_dumpAssetToFile;
  301. public IPAddress m_remoteAddress;
  302. public TaskInventoryScriptUpdater(UUID inventoryItemID, UUID primID, bool isScriptRunning,
  303. string path, IHttpServer httpServer, IPAddress address,
  304. bool dumpAssetToFile) : base(httpServer, path)
  305. {
  306. m_dumpAssetToFile = dumpAssetToFile;
  307. m_inventoryItemID = inventoryItemID;
  308. m_primID = primID;
  309. m_isScriptRunning = isScriptRunning;
  310. m_remoteAddress = address;
  311. Start(30000);
  312. }
  313. /// <summary>
  314. ///
  315. /// </summary>
  316. /// <param name="data"></param>
  317. /// <param name="path"></param>
  318. /// <param name="param"></param>
  319. /// <returns></returns>
  320. public void process(IOSHttpRequest request, IOSHttpResponse response, byte[] data)
  321. {
  322. Stop();
  323. if (!request.RemoteIPEndPoint.Address.Equals(m_remoteAddress))
  324. {
  325. response.StatusCode = (int)HttpStatusCode.Unauthorized;
  326. return;
  327. }
  328. if (OnUpLoad == null)
  329. {
  330. response.StatusCode = (int)HttpStatusCode.Gone;
  331. return;
  332. }
  333. if (!BunchOfCaps.ValidateAssetData((byte)AssetType.LSLText, data))
  334. {
  335. response.StatusCode = (int)HttpStatusCode.BadRequest;
  336. return;
  337. }
  338. response.StatusCode = (int)HttpStatusCode.OK;
  339. try
  340. {
  341. string res = String.Empty;
  342. LLSDTaskScriptUploadComplete uploadComplete = new LLSDTaskScriptUploadComplete();
  343. ArrayList errors = new ArrayList();
  344. OnUpLoad?.Invoke(m_inventoryItemID, m_primID, m_isScriptRunning, data, ref errors);
  345. uploadComplete.new_asset = m_inventoryItemID;
  346. uploadComplete.compiled = errors.Count > 0 ? false : true;
  347. uploadComplete.state = "complete";
  348. uploadComplete.errors = new OpenSim.Framework.Capabilities.OSDArray();
  349. uploadComplete.errors.Array = errors;
  350. res = LLSDHelpers.SerialiseLLSDReply(uploadComplete);
  351. if (m_dumpAssetToFile)
  352. {
  353. Util.SaveAssetToFile("updatedtaskscript" + Random.Shared.Next(1, 1000) + ".dat", data);
  354. }
  355. // m_log.InfoFormat("[CAPS]: TaskInventoryScriptUpdater.uploaderCaps res: {0}", res);
  356. response.RawBuffer = Util.UTF8NBGetbytes(res);
  357. }
  358. catch
  359. {
  360. LLSDAssetUploadError error = new LLSDAssetUploadError();
  361. error.message = "could not compile script";
  362. error.identifier = UUID.Zero;
  363. response.RawBuffer = Util.UTF8NBGetbytes(LLSDHelpers.SerialiseLLSDReply(error));
  364. return;
  365. }
  366. }
  367. }
  368. }
  369. }