EditorWebResources.cs 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Text.RegularExpressions;
  7. using UnityEngine;
  8. namespace ZenFulcrum.EmbeddedBrowser {
  9. /**
  10. * WebResources implementation that grabs resources directly from Assets/../BrowserAssets.
  11. */
  12. class EditorWebResources : WebResources {
  13. protected string basePath;
  14. public EditorWebResources() {
  15. //NB: If you try to read Application.dataPath later you may not be on the main thread and it won't work.
  16. basePath = Path.GetDirectoryName(Application.dataPath) + "/BrowserAssets";
  17. }
  18. /// <summary>
  19. /// Looks for "../asdf", "asdf/..\asdf", etc.
  20. /// </summary>
  21. private readonly Regex matchDots = new Regex(@"(^|[/\\])\.[2,]($|[/\\])");
  22. public override void HandleRequest(int id, string url) {
  23. var parsedURL = new Uri(url);
  24. var path = WWW.UnEscapeURL(parsedURL.AbsolutePath);
  25. if (matchDots.IsMatch(path)) {
  26. SendError(id, "Invalid path", 400);
  27. return;
  28. }
  29. var file = new FileInfo(Application.dataPath + "/../BrowserAssets/" + path);
  30. if (!file.Exists) {
  31. SendError(id, "Not found", 404);
  32. return;
  33. }
  34. //fixme: send 404 if file capitalization doesn't match. (Unfortunately, not a quick one-liner)
  35. SendFile(id, file);
  36. }
  37. }
  38. }