vhost.py 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. # -*- test-case-name: twisted.web.
  2. # Copyright (c) Twisted Matrix Laboratories.
  3. # See LICENSE for details.
  4. """
  5. I am a virtual hosts implementation.
  6. """
  7. from __future__ import division, absolute_import
  8. # Twisted Imports
  9. from twisted.python import roots
  10. from twisted.web import resource
  11. class VirtualHostCollection(roots.Homogenous):
  12. """Wrapper for virtual hosts collection.
  13. This exists for configuration purposes.
  14. """
  15. entityType = resource.Resource
  16. def __init__(self, nvh):
  17. self.nvh = nvh
  18. def listStaticEntities(self):
  19. return self.nvh.hosts.items()
  20. def getStaticEntity(self, name):
  21. return self.nvh.hosts.get(self)
  22. def reallyPutEntity(self, name, entity):
  23. self.nvh.addHost(name, entity)
  24. def delEntity(self, name):
  25. self.nvh.removeHost(name)
  26. class NameVirtualHost(resource.Resource):
  27. """I am a resource which represents named virtual hosts.
  28. """
  29. default = None
  30. def __init__(self):
  31. """Initialize.
  32. """
  33. resource.Resource.__init__(self)
  34. self.hosts = {}
  35. def listStaticEntities(self):
  36. return resource.Resource.listStaticEntities(self) + [("Virtual Hosts", VirtualHostCollection(self))]
  37. def getStaticEntity(self, name):
  38. if name == "Virtual Hosts":
  39. return VirtualHostCollection(self)
  40. else:
  41. return resource.Resource.getStaticEntity(self, name)
  42. def addHost(self, name, resrc):
  43. """Add a host to this virtual host.
  44. This will take a host named `name', and map it to a resource
  45. `resrc'. For example, a setup for our virtual hosts would be::
  46. nvh.addHost('divunal.com', divunalDirectory)
  47. nvh.addHost('www.divunal.com', divunalDirectory)
  48. nvh.addHost('twistedmatrix.com', twistedMatrixDirectory)
  49. nvh.addHost('www.twistedmatrix.com', twistedMatrixDirectory)
  50. """
  51. self.hosts[name] = resrc
  52. def removeHost(self, name):
  53. """Remove a host."""
  54. del self.hosts[name]
  55. def _getResourceForRequest(self, request):
  56. """(Internal) Get the appropriate resource for the given host.
  57. """
  58. hostHeader = request.getHeader(b'host')
  59. if hostHeader == None:
  60. return self.default or resource.NoResource()
  61. else:
  62. host = hostHeader.lower().split(b':', 1)[0]
  63. return (self.hosts.get(host, self.default)
  64. or resource.NoResource("host %s not in vhost map" % repr(host)))
  65. def render(self, request):
  66. """Implementation of resource.Resource's render method.
  67. """
  68. resrc = self._getResourceForRequest(request)
  69. return resrc.render(request)
  70. def getChild(self, path, request):
  71. """Implementation of resource.Resource's getChild method.
  72. """
  73. resrc = self._getResourceForRequest(request)
  74. if resrc.isLeaf:
  75. request.postpath.insert(0,request.prepath.pop(-1))
  76. return resrc
  77. else:
  78. return resrc.getChildWithDefault(path, request)
  79. class _HostResource(resource.Resource):
  80. def getChild(self, path, request):
  81. if b':' in path:
  82. host, port = path.split(b':', 1)
  83. port = int(port)
  84. else:
  85. host, port = path, 80
  86. request.setHost(host, port)
  87. prefixLen = (3 + request.isSecure() + 4 + len(path) +
  88. len(request.prepath[-3]))
  89. request.path = b'/' + b'/'.join(request.postpath)
  90. request.uri = request.uri[prefixLen:]
  91. del request.prepath[:3]
  92. return request.site.getResourceFor(request)
  93. class VHostMonsterResource(resource.Resource):
  94. """
  95. Use this to be able to record the hostname and method (http vs. https)
  96. in the URL without disturbing your web site. If you put this resource
  97. in a URL http://foo.com/bar then requests to
  98. http://foo.com/bar/http/baz.com/something will be equivalent to
  99. http://foo.com/something, except that the hostname the request will
  100. appear to be accessing will be "baz.com". So if "baz.com" is redirecting
  101. all requests for to foo.com, while foo.com is inaccessible from the outside,
  102. then redirect and url generation will work correctly
  103. """
  104. def getChild(self, path, request):
  105. if path == b'http':
  106. request.isSecure = lambda: 0
  107. elif path == b'https':
  108. request.isSecure = lambda: 1
  109. return _HostResource()