Funktionierender Prototyp des Serious Games zur Vermittlung von Wissen zu Software-Engineering-Arbeitsmodellen.
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 12KB

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