twcgi.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  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. }
  72. ip = request.getClientIP()
  73. if ip is not None:
  74. env['REMOTE_ADDR'] = ip
  75. pp = request.postpath
  76. if pp:
  77. env["PATH_INFO"] = "/" + "/".join(pp)
  78. if hasattr(request, "content"):
  79. # 'request.content' is either a StringIO or a TemporaryFile, and
  80. # the file pointer is sitting at the beginning (seek(0,0))
  81. request.content.seek(0, 2)
  82. length = request.content.tell()
  83. request.content.seek(0, 0)
  84. env['CONTENT_LENGTH'] = str(length)
  85. try:
  86. qindex = request.uri.index(b'?')
  87. except ValueError:
  88. env['QUERY_STRING'] = ''
  89. qargs = []
  90. else:
  91. qs = env['QUERY_STRING'] = request.uri[qindex+1:]
  92. if '=' in qs:
  93. qargs = []
  94. else:
  95. qargs = [urllib.unquote(x) for x in qs.split('+')]
  96. # Propagate HTTP headers
  97. for title, header in request.getAllHeaders().items():
  98. envname = title.replace(b'-', b'_').upper()
  99. if title not in (b'content-type', b'content-length', b'proxy'):
  100. envname = b"HTTP_" + envname
  101. env[envname] = header
  102. # Propagate our environment
  103. for key, value in os.environ.items():
  104. if key not in env:
  105. env[key] = value
  106. # And they're off!
  107. self.runProcess(env, request, qargs)
  108. return server.NOT_DONE_YET
  109. def runProcess(self, env, request, qargs=[]):
  110. """
  111. Run the cgi script.
  112. @type env: A L{dict} of L{str}, or L{None}
  113. @param env: The environment variables to pass to the process that will
  114. get spawned. See
  115. L{twisted.internet.interfaces.IReactorProcess.spawnProcess} for
  116. more information about environments and process creation.
  117. @type request: L{twisted.web.http.Request}
  118. @param request: An HTTP request.
  119. @type qargs: A L{list} of L{str}
  120. @param qargs: The command line arguments to pass to the process that
  121. will get spawned.
  122. """
  123. p = CGIProcessProtocol(request)
  124. self._reactor.spawnProcess(p, self.filename, [self.filename] + qargs,
  125. env, os.path.dirname(self.filename))
  126. class FilteredScript(CGIScript):
  127. """
  128. I am a special version of a CGI script, that uses a specific executable.
  129. This is useful for interfacing with other scripting languages that adhere
  130. to the CGI standard. My C{filter} attribute specifies what executable to
  131. run, and my C{filename} init parameter describes which script to pass to
  132. the first argument of that script.
  133. To customize me for a particular location of a CGI interpreter, override
  134. C{filter}.
  135. @type filter: L{str}
  136. @ivar filter: The absolute path to the executable.
  137. """
  138. filter = '/usr/bin/cat'
  139. def runProcess(self, env, request, qargs=[]):
  140. """
  141. Run a script through the C{filter} executable.
  142. @type env: A L{dict} of L{str}, or L{None}
  143. @param env: The environment variables to pass to the process that will
  144. get spawned. See
  145. L{twisted.internet.interfaces.IReactorProcess.spawnProcess}
  146. for more information about environments and process creation.
  147. @type request: L{twisted.web.http.Request}
  148. @param request: An HTTP request.
  149. @type qargs: A L{list} of L{str}
  150. @param qargs: The command line arguments to pass to the process that
  151. will get spawned.
  152. """
  153. p = CGIProcessProtocol(request)
  154. self._reactor.spawnProcess(p, self.filter,
  155. [self.filter, self.filename] + qargs, env,
  156. os.path.dirname(self.filename))
  157. class CGIProcessProtocol(protocol.ProcessProtocol, pb.Viewable):
  158. handling_headers = 1
  159. headers_written = 0
  160. headertext = b''
  161. errortext = b''
  162. _log = Logger()
  163. # Remotely relay producer interface.
  164. def view_resumeProducing(self, issuer):
  165. self.resumeProducing()
  166. def view_pauseProducing(self, issuer):
  167. self.pauseProducing()
  168. def view_stopProducing(self, issuer):
  169. self.stopProducing()
  170. def resumeProducing(self):
  171. self.transport.resumeProducing()
  172. def pauseProducing(self):
  173. self.transport.pauseProducing()
  174. def stopProducing(self):
  175. self.transport.loseConnection()
  176. def __init__(self, request):
  177. self.request = request
  178. def connectionMade(self):
  179. self.request.registerProducer(self, 1)
  180. self.request.content.seek(0, 0)
  181. content = self.request.content.read()
  182. if content:
  183. self.transport.write(content)
  184. self.transport.closeStdin()
  185. def errReceived(self, error):
  186. self.errortext = self.errortext + error
  187. def outReceived(self, output):
  188. """
  189. Handle a chunk of input
  190. """
  191. # First, make sure that the headers from the script are sorted
  192. # out (we'll want to do some parsing on these later.)
  193. if self.handling_headers:
  194. text = self.headertext + output
  195. headerEnds = []
  196. for delimiter in b'\n\n', b'\r\n\r\n', b'\r\r', b'\n\r\n':
  197. headerend = text.find(delimiter)
  198. if headerend != -1:
  199. headerEnds.append((headerend, delimiter))
  200. if headerEnds:
  201. # The script is entirely in control of response headers;
  202. # disable the default Content-Type value normally provided by
  203. # twisted.web.server.Request.
  204. self.request.defaultContentType = None
  205. headerEnds.sort()
  206. headerend, delimiter = headerEnds[0]
  207. self.headertext = text[:headerend]
  208. # This is a final version of the header text.
  209. linebreak = delimiter[:len(delimiter)//2]
  210. headers = self.headertext.split(linebreak)
  211. for header in headers:
  212. br = header.find(b': ')
  213. if br == -1:
  214. self._log.error(
  215. 'ignoring malformed CGI header: {header!r}',
  216. header=header)
  217. else:
  218. headerName = header[:br].lower()
  219. headerText = header[br+2:]
  220. if headerName == b'location':
  221. self.request.setResponseCode(http.FOUND)
  222. if headerName == b'status':
  223. try:
  224. # "XXX <description>" sometimes happens.
  225. statusNum = int(headerText[:3])
  226. except:
  227. self._log.error("malformed status header")
  228. else:
  229. self.request.setResponseCode(statusNum)
  230. else:
  231. # Don't allow the application to control
  232. # these required headers.
  233. if headerName.lower() not in (b'server', b'date'):
  234. self.request.responseHeaders.addRawHeader(
  235. headerName, headerText)
  236. output = text[headerend+len(delimiter):]
  237. self.handling_headers = 0
  238. if self.handling_headers:
  239. self.headertext = text
  240. if not self.handling_headers:
  241. self.request.write(output)
  242. def processEnded(self, reason):
  243. if reason.value.exitCode != 0:
  244. self._log.error("CGI {uri} exited with exit code {exitCode}",
  245. uri=self.request.uri, exitCode=reason.value.exitCode)
  246. if self.errortext:
  247. self._log.error("Errors from CGI {uri}: {errorText}",
  248. uri=self.request.uri, errorText=self.errortext)
  249. if self.handling_headers:
  250. self._log.error("Premature end of headers in {uri}: {headerText}",
  251. uri=self.request.uri, headerText=self.headertext)
  252. self.request.write(
  253. resource.ErrorPage(http.INTERNAL_SERVER_ERROR,
  254. "CGI Script Error",
  255. "Premature end of script headers.").render(self.request))
  256. self.request.unregisterProducer()
  257. self.request.finish()