distrib.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  1. # -*- test-case-name: twisted.web.test.test_distrib -*-
  2. # Copyright (c) Twisted Matrix Laboratories.
  3. # See LICENSE for details.
  4. """
  5. Distributed web servers.
  6. This is going to have to be refactored so that argument parsing is done
  7. by each subprocess and not by the main web server (i.e. GET, POST etc.).
  8. """
  9. # System Imports
  10. import os, copy
  11. try:
  12. import pwd
  13. except ImportError:
  14. pwd = None
  15. from io import BytesIO
  16. from xml.dom.minidom import getDOMImplementation
  17. # Twisted Imports
  18. from twisted.spread import pb
  19. from twisted.spread.banana import SIZE_LIMIT
  20. from twisted.web import http, resource, server, util, static
  21. from twisted.web.http_headers import Headers
  22. from twisted.python import log
  23. from twisted.persisted import styles
  24. from twisted.internet import address, reactor
  25. class _ReferenceableProducerWrapper(pb.Referenceable):
  26. def __init__(self, producer):
  27. self.producer = producer
  28. def remote_resumeProducing(self):
  29. self.producer.resumeProducing()
  30. def remote_pauseProducing(self):
  31. self.producer.pauseProducing()
  32. def remote_stopProducing(self):
  33. self.producer.stopProducing()
  34. class Request(pb.RemoteCopy, server.Request):
  35. """
  36. A request which was received by a L{ResourceSubscription} and sent via
  37. PB to a distributed node.
  38. """
  39. def setCopyableState(self, state):
  40. """
  41. Initialize this L{twisted.web.distrib.Request} based on the copied
  42. state so that it closely resembles a L{twisted.web.server.Request}.
  43. """
  44. for k in 'host', 'client':
  45. tup = state[k]
  46. addrdesc = {'INET': 'TCP', 'UNIX': 'UNIX'}[tup[0]]
  47. addr = {'TCP': lambda: address.IPv4Address(addrdesc,
  48. tup[1], tup[2]),
  49. 'UNIX': lambda: address.UNIXAddress(tup[1])}[addrdesc]()
  50. state[k] = addr
  51. state['requestHeaders'] = Headers(dict(state['requestHeaders']))
  52. pb.RemoteCopy.setCopyableState(self, state)
  53. # Emulate the local request interface --
  54. self.content = BytesIO(self.content_data)
  55. self.finish = self.remote.remoteMethod('finish')
  56. self.setHeader = self.remote.remoteMethod('setHeader')
  57. self.addCookie = self.remote.remoteMethod('addCookie')
  58. self.setETag = self.remote.remoteMethod('setETag')
  59. self.setResponseCode = self.remote.remoteMethod('setResponseCode')
  60. self.setLastModified = self.remote.remoteMethod('setLastModified')
  61. # To avoid failing if a resource tries to write a very long string
  62. # all at once, this one will be handled slightly differently.
  63. self._write = self.remote.remoteMethod('write')
  64. def write(self, bytes):
  65. """
  66. Write the given bytes to the response body.
  67. @param bytes: The bytes to write. If this is longer than 640k, it
  68. will be split up into smaller pieces.
  69. """
  70. start = 0
  71. end = SIZE_LIMIT
  72. while True:
  73. self._write(bytes[start:end])
  74. start += SIZE_LIMIT
  75. end += SIZE_LIMIT
  76. if start >= len(bytes):
  77. break
  78. def registerProducer(self, producer, streaming):
  79. self.remote.callRemote("registerProducer",
  80. _ReferenceableProducerWrapper(producer),
  81. streaming).addErrback(self.fail)
  82. def unregisterProducer(self):
  83. self.remote.callRemote("unregisterProducer").addErrback(self.fail)
  84. def fail(self, failure):
  85. log.err(failure)
  86. pb.setUnjellyableForClass(server.Request, Request)
  87. class Issue:
  88. def __init__(self, request):
  89. self.request = request
  90. def finished(self, result):
  91. if result != server.NOT_DONE_YET:
  92. assert isinstance(result, str), "return value not a string"
  93. self.request.write(result)
  94. self.request.finish()
  95. def failed(self, failure):
  96. #XXX: Argh. FIXME.
  97. failure = str(failure)
  98. self.request.write(
  99. resource.ErrorPage(http.INTERNAL_SERVER_ERROR,
  100. "Server Connection Lost",
  101. "Connection to distributed server lost:" +
  102. util._PRE(failure)).
  103. render(self.request))
  104. self.request.finish()
  105. log.msg(failure)
  106. class ResourceSubscription(resource.Resource):
  107. isLeaf = 1
  108. waiting = 0
  109. def __init__(self, host, port):
  110. resource.Resource.__init__(self)
  111. self.host = host
  112. self.port = port
  113. self.pending = []
  114. self.publisher = None
  115. def __getstate__(self):
  116. """Get persistent state for this ResourceSubscription.
  117. """
  118. # When I unserialize,
  119. state = copy.copy(self.__dict__)
  120. # Publisher won't be connected...
  121. state['publisher'] = None
  122. # I won't be making a connection
  123. state['waiting'] = 0
  124. # There will be no pending requests.
  125. state['pending'] = []
  126. return state
  127. def connected(self, publisher):
  128. """I've connected to a publisher; I'll now send all my requests.
  129. """
  130. log.msg('connected to publisher')
  131. publisher.broker.notifyOnDisconnect(self.booted)
  132. self.publisher = publisher
  133. self.waiting = 0
  134. for request in self.pending:
  135. self.render(request)
  136. self.pending = []
  137. def notConnected(self, msg):
  138. """I can't connect to a publisher; I'll now reply to all pending
  139. requests.
  140. """
  141. log.msg("could not connect to distributed web service: %s" % msg)
  142. self.waiting = 0
  143. self.publisher = None
  144. for request in self.pending:
  145. request.write("Unable to connect to distributed server.")
  146. request.finish()
  147. self.pending = []
  148. def booted(self):
  149. self.notConnected("connection dropped")
  150. def render(self, request):
  151. """Render this request, from my server.
  152. This will always be asynchronous, and therefore return NOT_DONE_YET.
  153. It spins off a request to the pb client, and either adds it to the list
  154. of pending issues or requests it immediately, depending on if the
  155. client is already connected.
  156. """
  157. if not self.publisher:
  158. self.pending.append(request)
  159. if not self.waiting:
  160. self.waiting = 1
  161. bf = pb.PBClientFactory()
  162. timeout = 10
  163. if self.host == "unix":
  164. reactor.connectUNIX(self.port, bf, timeout)
  165. else:
  166. reactor.connectTCP(self.host, self.port, bf, timeout)
  167. d = bf.getRootObject()
  168. d.addCallbacks(self.connected, self.notConnected)
  169. else:
  170. i = Issue(request)
  171. self.publisher.callRemote('request', request).addCallbacks(i.finished, i.failed)
  172. return server.NOT_DONE_YET
  173. class ResourcePublisher(pb.Root, styles.Versioned):
  174. """
  175. L{ResourcePublisher} exposes a remote API which can be used to respond
  176. to request.
  177. @ivar site: The site which will be used for resource lookup.
  178. @type site: L{twisted.web.server.Site}
  179. """
  180. def __init__(self, site):
  181. self.site = site
  182. persistenceVersion = 2
  183. def upgradeToVersion2(self):
  184. self.application.authorizer.removeIdentity("web")
  185. del self.application.services[self.serviceName]
  186. del self.serviceName
  187. del self.application
  188. del self.perspectiveName
  189. def getPerspectiveNamed(self, name):
  190. return self
  191. def remote_request(self, request):
  192. """
  193. Look up the resource for the given request and render it.
  194. """
  195. res = self.site.getResourceFor(request)
  196. log.msg(request)
  197. result = res.render(request)
  198. if result is not server.NOT_DONE_YET:
  199. request.write(result)
  200. request.finish()
  201. return server.NOT_DONE_YET
  202. class UserDirectory(resource.Resource):
  203. """
  204. A resource which lists available user resources and serves them as
  205. children.
  206. @ivar _pwd: An object like L{pwd} which is used to enumerate users and
  207. their home directories.
  208. """
  209. userDirName = 'public_html'
  210. userSocketName = '.twistd-web-pb'
  211. template = """
  212. <html>
  213. <head>
  214. <title>twisted.web.distrib.UserDirectory</title>
  215. <style>
  216. a
  217. {
  218. font-family: Lucida, Verdana, Helvetica, Arial, sans-serif;
  219. color: #369;
  220. text-decoration: none;
  221. }
  222. th
  223. {
  224. font-family: Lucida, Verdana, Helvetica, Arial, sans-serif;
  225. font-weight: bold;
  226. text-decoration: none;
  227. text-align: left;
  228. }
  229. pre, code
  230. {
  231. font-family: "Courier New", Courier, monospace;
  232. }
  233. p, body, td, ol, ul, menu, blockquote, div
  234. {
  235. font-family: Lucida, Verdana, Helvetica, Arial, sans-serif;
  236. color: #000;
  237. }
  238. </style>
  239. </head>
  240. <body>
  241. <h1>twisted.web.distrib.UserDirectory</h1>
  242. %(users)s
  243. </body>
  244. </html>
  245. """
  246. def __init__(self, userDatabase=None):
  247. resource.Resource.__init__(self)
  248. if userDatabase is None:
  249. userDatabase = pwd
  250. self._pwd = userDatabase
  251. def _users(self):
  252. """
  253. Return a list of two-tuples giving links to user resources and text to
  254. associate with those links.
  255. """
  256. users = []
  257. for user in self._pwd.getpwall():
  258. name, passwd, uid, gid, gecos, dir, shell = user
  259. realname = gecos.split(',')[0]
  260. if not realname:
  261. realname = name
  262. if os.path.exists(os.path.join(dir, self.userDirName)):
  263. users.append((name, realname + ' (file)'))
  264. twistdsock = os.path.join(dir, self.userSocketName)
  265. if os.path.exists(twistdsock):
  266. linkName = name + '.twistd'
  267. users.append((linkName, realname + ' (twistd)'))
  268. return users
  269. def render_GET(self, request):
  270. """
  271. Render as HTML a listing of all known users with links to their
  272. personal resources.
  273. """
  274. domImpl = getDOMImplementation()
  275. newDoc = domImpl.createDocument(None, "ul", None)
  276. listing = newDoc.documentElement
  277. for link, text in self._users():
  278. linkElement = newDoc.createElement('a')
  279. linkElement.setAttribute('href', link + '/')
  280. textNode = newDoc.createTextNode(text)
  281. linkElement.appendChild(textNode)
  282. item = newDoc.createElement('li')
  283. item.appendChild(linkElement)
  284. listing.appendChild(item)
  285. htmlDoc = self.template % ({'users': listing.toxml()})
  286. return htmlDoc.encode("utf-8")
  287. def getChild(self, name, request):
  288. if name == '':
  289. return self
  290. td = '.twistd'
  291. if name[-len(td):] == td:
  292. username = name[:-len(td)]
  293. sub = 1
  294. else:
  295. username = name
  296. sub = 0
  297. try:
  298. pw_name, pw_passwd, pw_uid, pw_gid, pw_gecos, pw_dir, pw_shell \
  299. = self._pwd.getpwnam(username)
  300. except KeyError:
  301. return resource.NoResource()
  302. if sub:
  303. twistdsock = os.path.join(pw_dir, self.userSocketName)
  304. rs = ResourceSubscription('unix',twistdsock)
  305. self.putChild(name, rs)
  306. return rs
  307. else:
  308. path = os.path.join(pw_dir, self.userDirName)
  309. if not os.path.exists(path):
  310. return resource.NoResource()
  311. return static.File(path)