You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

twcgi.py 11KB

5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  1. # -*- test-case-name: twisted.web.test.test_cgi -*-
  2. # Copyright (c) Twisted Matrix Laboratories.
  3. # See LICENSE for details.
  4. """
  5. I hold resource classes and helper classes that deal with CGI scripts.
  6. """
  7. # System Imports
  8. import os
  9. import urllib
  10. # Twisted Imports
  11. from twisted.internet import protocol
  12. from twisted.logger import Logger
  13. from twisted.python import filepath
  14. from twisted.spread import pb
  15. from twisted.web import http, resource, server, static
  16. class CGIDirectory(resource.Resource, filepath.FilePath):
  17. def __init__(self, pathname):
  18. resource.Resource.__init__(self)
  19. filepath.FilePath.__init__(self, pathname)
  20. def getChild(self, path, request):
  21. fnp = self.child(path)
  22. if not fnp.exists():
  23. return static.File.childNotFound
  24. elif fnp.isdir():
  25. return CGIDirectory(fnp.path)
  26. else:
  27. return CGIScript(fnp.path)
  28. return resource.NoResource()
  29. def render(self, request):
  30. notFound = resource.NoResource(
  31. "CGI directories do not support directory listing.")
  32. return notFound.render(request)
  33. class CGIScript(resource.Resource):
  34. """
  35. L{CGIScript} is a resource which runs child processes according to the CGI
  36. specification.
  37. The implementation is complex due to the fact that it requires asynchronous
  38. IPC with an external process with an unpleasant protocol.
  39. """
  40. isLeaf = 1
  41. def __init__(self, filename, registry=None, reactor=None):
  42. """
  43. Initialize, with the name of a CGI script file.
  44. """
  45. self.filename = filename
  46. if reactor is None:
  47. # This installs a default reactor, if None was installed before.
  48. # We do a late import here, so that importing the current module
  49. # won't directly trigger installing a default reactor.
  50. from twisted.internet import reactor
  51. self._reactor = reactor
  52. def render(self, request):
  53. """
  54. Do various things to conform to the CGI specification.
  55. I will set up the usual slew of environment variables, then spin off a
  56. process.
  57. @type request: L{twisted.web.http.Request}
  58. @param request: An HTTP request.
  59. """
  60. scriptName = b"/" + b"/".join(request.prepath)
  61. serverName = request.getRequestHostname().split(b':')[0]
  62. env = {"SERVER_SOFTWARE": server.version,
  63. "SERVER_NAME": serverName,
  64. "GATEWAY_INTERFACE": "CGI/1.1",
  65. "SERVER_PROTOCOL": request.clientproto,
  66. "SERVER_PORT": str(request.getHost().port),
  67. "REQUEST_METHOD": request.method,
  68. "SCRIPT_NAME": scriptName,
  69. "SCRIPT_FILENAME": self.filename,
  70. "REQUEST_URI": request.uri}
  71. ip = request.getClientAddress().host
  72. if ip is not None:
  73. env['REMOTE_ADDR'] = ip
  74. pp = request.postpath
  75. if pp:
  76. env["PATH_INFO"] = "/" + "/".join(pp)
  77. if hasattr(request, "content"):
  78. # 'request.content' is either a StringIO or a TemporaryFile, and
  79. # the file pointer is sitting at the beginning (seek(0,0))
  80. request.content.seek(0, 2)
  81. length = request.content.tell()
  82. request.content.seek(0, 0)
  83. env['CONTENT_LENGTH'] = str(length)
  84. try:
  85. qindex = request.uri.index(b'?')
  86. except ValueError:
  87. env['QUERY_STRING'] = ''
  88. qargs = []
  89. else:
  90. qs = env['QUERY_STRING'] = request.uri[qindex+1:]
  91. if '=' in qs:
  92. qargs = []
  93. else:
  94. qargs = [urllib.unquote(x) for x in qs.split('+')]
  95. # Propagate HTTP headers
  96. for title, header in request.getAllHeaders().items():
  97. envname = title.replace(b'-', b'_').upper()
  98. if title not in (b'content-type', b'content-length', b'proxy'):
  99. envname = b"HTTP_" + envname
  100. env[envname] = header
  101. # Propagate our environment
  102. for key, value in os.environ.items():
  103. if key not in env:
  104. env[key] = value
  105. # And they're off!
  106. self.runProcess(env, request, qargs)
  107. return server.NOT_DONE_YET
  108. def runProcess(self, env, request, qargs=[]):
  109. """
  110. Run the cgi script.
  111. @type env: A L{dict} of L{str}, or L{None}
  112. @param env: The environment variables to pass to the process that will
  113. get spawned. See
  114. L{twisted.internet.interfaces.IReactorProcess.spawnProcess} for
  115. more information about environments and process creation.
  116. @type request: L{twisted.web.http.Request}
  117. @param request: An HTTP request.
  118. @type qargs: A L{list} of L{str}
  119. @param qargs: The command line arguments to pass to the process that
  120. will get spawned.
  121. """
  122. p = CGIProcessProtocol(request)
  123. self._reactor.spawnProcess(p, self.filename, [self.filename] + qargs,
  124. env, os.path.dirname(self.filename))
  125. class FilteredScript(CGIScript):
  126. """
  127. I am a special version of a CGI script, that uses a specific executable.
  128. This is useful for interfacing with other scripting languages that adhere
  129. to the CGI standard. My C{filter} attribute specifies what executable to
  130. run, and my C{filename} init parameter describes which script to pass to
  131. the first argument of that script.
  132. To customize me for a particular location of a CGI interpreter, override
  133. C{filter}.
  134. @type filter: L{str}
  135. @ivar filter: The absolute path to the executable.
  136. """
  137. filter = '/usr/bin/cat'
  138. def runProcess(self, env, request, qargs=[]):
  139. """
  140. Run a script through the C{filter} executable.
  141. @type env: A L{dict} of L{str}, or L{None}
  142. @param env: The environment variables to pass to the process that will
  143. get spawned. See
  144. L{twisted.internet.interfaces.IReactorProcess.spawnProcess}
  145. for more information about environments and process creation.
  146. @type request: L{twisted.web.http.Request}
  147. @param request: An HTTP request.
  148. @type qargs: A L{list} of L{str}
  149. @param qargs: The command line arguments to pass to the process that
  150. will get spawned.
  151. """
  152. p = CGIProcessProtocol(request)
  153. self._reactor.spawnProcess(p, self.filter,
  154. [self.filter, self.filename] + qargs, env,
  155. os.path.dirname(self.filename))
  156. class CGIProcessProtocol(protocol.ProcessProtocol, pb.Viewable):
  157. handling_headers = 1
  158. headers_written = 0
  159. headertext = b''
  160. errortext = b''
  161. _log = Logger()
  162. # Remotely relay producer interface.
  163. def view_resumeProducing(self, issuer):
  164. self.resumeProducing()
  165. def view_pauseProducing(self, issuer):
  166. self.pauseProducing()
  167. def view_stopProducing(self, issuer):
  168. self.stopProducing()
  169. def resumeProducing(self):
  170. self.transport.resumeProducing()
  171. def pauseProducing(self):
  172. self.transport.pauseProducing()
  173. def stopProducing(self):
  174. self.transport.loseConnection()
  175. def __init__(self, request):
  176. self.request = request
  177. def connectionMade(self):
  178. self.request.registerProducer(self, 1)
  179. self.request.content.seek(0, 0)
  180. content = self.request.content.read()
  181. if content:
  182. self.transport.write(content)
  183. self.transport.closeStdin()
  184. def errReceived(self, error):
  185. self.errortext = self.errortext + error
  186. def outReceived(self, output):
  187. """
  188. Handle a chunk of input
  189. """
  190. # First, make sure that the headers from the script are sorted
  191. # out (we'll want to do some parsing on these later.)
  192. if self.handling_headers:
  193. text = self.headertext + output
  194. headerEnds = []
  195. for delimiter in b'\n\n', b'\r\n\r\n', b'\r\r', b'\n\r\n':
  196. headerend = text.find(delimiter)
  197. if headerend != -1:
  198. headerEnds.append((headerend, delimiter))
  199. if headerEnds:
  200. # The script is entirely in control of response headers;
  201. # disable the default Content-Type value normally provided by
  202. # twisted.web.server.Request.
  203. self.request.defaultContentType = None
  204. headerEnds.sort()
  205. headerend, delimiter = headerEnds[0]
  206. self.headertext = text[:headerend]
  207. # This is a final version of the header text.
  208. linebreak = delimiter[:len(delimiter)//2]
  209. headers = self.headertext.split(linebreak)
  210. for header in headers:
  211. br = header.find(b': ')
  212. if br == -1:
  213. self._log.error(
  214. 'ignoring malformed CGI header: {header!r}',
  215. header=header)
  216. else:
  217. headerName = header[:br].lower()
  218. headerText = header[br+2:]
  219. if headerName == b'location':
  220. self.request.setResponseCode(http.FOUND)
  221. if headerName == b'status':
  222. try:
  223. # "XXX <description>" sometimes happens.
  224. statusNum = int(headerText[:3])
  225. except:
  226. self._log.error("malformed status header")
  227. else:
  228. self.request.setResponseCode(statusNum)
  229. else:
  230. # Don't allow the application to control
  231. # these required headers.
  232. if headerName.lower() not in (b'server', b'date'):
  233. self.request.responseHeaders.addRawHeader(
  234. headerName, headerText)
  235. output = text[headerend+len(delimiter):]
  236. self.handling_headers = 0
  237. if self.handling_headers:
  238. self.headertext = text
  239. if not self.handling_headers:
  240. self.request.write(output)
  241. def processEnded(self, reason):
  242. if reason.value.exitCode != 0:
  243. self._log.error("CGI {uri} exited with exit code {exitCode}",
  244. uri=self.request.uri, exitCode=reason.value.exitCode)
  245. if self.errortext:
  246. self._log.error("Errors from CGI {uri}: {errorText}",
  247. uri=self.request.uri, errorText=self.errortext)
  248. if self.handling_headers:
  249. self._log.error("Premature end of headers in {uri}: {headerText}",
  250. uri=self.request.uri, headerText=self.headertext)
  251. self.request.write(
  252. resource.ErrorPage(http.INTERNAL_SERVER_ERROR,
  253. "CGI Script Error",
  254. "Premature end of script headers.").render(self.request))
  255. self.request.unregisterProducer()
  256. self.request.finish()