Non puoi selezionare più di 25 argomenti Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.
 
 
 
 
 
 

103 righe
4.1 KiB

  1. # Copyright 2016 OpenMarket Ltd
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. import logging
  15. from typing import Dict
  16. from twisted.web.resource import Resource
  17. from synapse.http.server import UnrecognizedRequestResource
  18. logger = logging.getLogger(__name__)
  19. def create_resource_tree(
  20. desired_tree: Dict[str, Resource], root_resource: Resource
  21. ) -> Resource:
  22. """Create the resource tree for this homeserver.
  23. This in unduly complicated because Twisted does not support putting
  24. child resources more than 1 level deep at a time.
  25. Args:
  26. desired_tree: Dict from desired paths to desired resources.
  27. root_resource: The root resource to add the tree to.
  28. Returns:
  29. The ``root_resource`` with a tree of child resources added to it.
  30. """
  31. # ideally we'd just use getChild and putChild but getChild doesn't work
  32. # unless you give it a Request object IN ADDITION to the name :/ So
  33. # instead, we'll store a copy of this mapping so we can actually add
  34. # extra resources to existing nodes. See self._resource_id for the key.
  35. resource_mappings: Dict[str, Resource] = {}
  36. for full_path_str, res in desired_tree.items():
  37. # twisted requires all resources to be bytes
  38. full_path = full_path_str.encode("utf-8")
  39. logger.info("Attaching %s to path %s", res, full_path)
  40. last_resource = root_resource
  41. for path_seg in full_path.split(b"/")[1:-1]:
  42. if path_seg not in last_resource.listNames():
  43. # resource doesn't exist, so make a "dummy resource"
  44. child_resource: Resource = UnrecognizedRequestResource()
  45. last_resource.putChild(path_seg, child_resource)
  46. res_id = _resource_id(last_resource, path_seg)
  47. resource_mappings[res_id] = child_resource
  48. last_resource = child_resource
  49. else:
  50. # we have an existing Resource, use that instead.
  51. res_id = _resource_id(last_resource, path_seg)
  52. last_resource = resource_mappings[res_id]
  53. # ===========================
  54. # now attach the actual desired resource
  55. last_path_seg = full_path.split(b"/")[-1]
  56. # if there is already a resource here, thieve its children and
  57. # replace it
  58. res_id = _resource_id(last_resource, last_path_seg)
  59. if res_id in resource_mappings:
  60. # there is a dummy resource at this path already, which needs
  61. # to be replaced with the desired resource.
  62. existing_dummy_resource = resource_mappings[res_id]
  63. for child_name in existing_dummy_resource.listNames():
  64. child_res_id = _resource_id(existing_dummy_resource, child_name)
  65. child_resource = resource_mappings[child_res_id]
  66. # steal the children
  67. res.putChild(child_name, child_resource)
  68. # finally, insert the desired resource in the right place
  69. last_resource.putChild(last_path_seg, res)
  70. res_id = _resource_id(last_resource, last_path_seg)
  71. resource_mappings[res_id] = res
  72. return root_resource
  73. def _resource_id(resource: Resource, path_seg: bytes) -> str:
  74. """Construct an arbitrary resource ID so you can retrieve the mapping
  75. later.
  76. If you want to represent resource A putChild resource B with path C,
  77. the mapping should looks like _resource_id(A,C) = B.
  78. Args:
  79. resource: The *parent* Resourceb
  80. path_seg: The name of the child Resource to be attached.
  81. Returns:
  82. A unique string which can be a key to the child Resource.
  83. """
  84. return "%s-%r" % (resource, path_seg)