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.

extension_simple.py 4.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. # This is an ISAPI extension purely for testing purposes. It is NOT
  2. # a 'demo' (even though it may be useful!)
  3. #
  4. # Install this extension, then point your browser to:
  5. # "http://localhost/pyisapi_test/test1"
  6. # This will execute the method 'test1' below. See below for the list of
  7. # test methods that are acceptable.
  8. import urllib.error
  9. import urllib.parse
  10. import urllib.request
  11. # If we have no console (eg, am running from inside IIS), redirect output
  12. # somewhere useful - in this case, the standard win32 trace collector.
  13. import win32api
  14. import winerror
  15. from isapi import ExtensionError, isapicon, threaded_extension
  16. from isapi.simple import SimpleFilter
  17. try:
  18. win32api.GetConsoleTitle()
  19. except win32api.error:
  20. # No console - redirect
  21. import win32traceutil
  22. # The ISAPI extension - handles requests in our virtual dir, and sends the
  23. # response to the client.
  24. class Extension(threaded_extension.ThreadPoolExtension):
  25. "Python ISAPI Tester"
  26. def Dispatch(self, ecb):
  27. print('Tester dispatching "%s"' % (ecb.GetServerVariable("URL"),))
  28. url = ecb.GetServerVariable("URL")
  29. test_name = url.split("/")[-1]
  30. meth = getattr(self, test_name, None)
  31. if meth is None:
  32. raise AttributeError("No test named '%s'" % (test_name,))
  33. result = meth(ecb)
  34. if result is None:
  35. # This means the test finalized everything
  36. return
  37. ecb.SendResponseHeaders("200 OK", "Content-type: text/html\r\n\r\n", False)
  38. print("<HTML><BODY>Finished running test <i>", test_name, "</i>", file=ecb)
  39. print("<pre>", file=ecb)
  40. print(result, file=ecb)
  41. print("</pre>", file=ecb)
  42. print("</BODY></HTML>", file=ecb)
  43. ecb.DoneWithSession()
  44. def test1(self, ecb):
  45. try:
  46. ecb.GetServerVariable("foo bar")
  47. raise RuntimeError("should have failed!")
  48. except ExtensionError as err:
  49. assert err.errno == winerror.ERROR_INVALID_INDEX, err
  50. return "worked!"
  51. def test_long_vars(self, ecb):
  52. qs = ecb.GetServerVariable("QUERY_STRING")
  53. # Our implementation has a default buffer size of 8k - so we test
  54. # the code that handles an overflow by ensuring there are more
  55. # than 8k worth of chars in the URL.
  56. expected_query = "x" * 8500
  57. if len(qs) == 0:
  58. # Just the URL with no query part - redirect to myself, but with
  59. # a huge query portion.
  60. me = ecb.GetServerVariable("URL")
  61. headers = "Location: " + me + "?" + expected_query + "\r\n\r\n"
  62. ecb.SendResponseHeaders("301 Moved", headers)
  63. ecb.DoneWithSession()
  64. return None
  65. if qs == expected_query:
  66. return "Total length of variable is %d - test worked!" % (len(qs),)
  67. else:
  68. return "Unexpected query portion! Got %d chars, expected %d" % (
  69. len(qs),
  70. len(expected_query),
  71. )
  72. def test_unicode_vars(self, ecb):
  73. # We need to check that we are running IIS6! This seems the only
  74. # effective way from an extension.
  75. ver = float(ecb.GetServerVariable("SERVER_SOFTWARE").split("/")[1])
  76. if ver < 6.0:
  77. return "This is IIS version %g - unicode only works in IIS6 and later" % ver
  78. us = ecb.GetServerVariable("UNICODE_SERVER_NAME")
  79. if not isinstance(us, str):
  80. raise RuntimeError("unexpected type!")
  81. if us != str(ecb.GetServerVariable("SERVER_NAME")):
  82. raise RuntimeError("Unicode and non-unicode values were not the same")
  83. return "worked!"
  84. # The entry points for the ISAPI extension.
  85. def __ExtensionFactory__():
  86. return Extension()
  87. if __name__ == "__main__":
  88. # If run from the command-line, install ourselves.
  89. from isapi.install import *
  90. params = ISAPIParameters()
  91. # Setup the virtual directories - this is a list of directories our
  92. # extension uses - in this case only 1.
  93. # Each extension has a "script map" - this is the mapping of ISAPI
  94. # extensions.
  95. sm = [ScriptMapParams(Extension="*", Flags=0)]
  96. vd = VirtualDirParameters(
  97. Name="pyisapi_test",
  98. Description=Extension.__doc__,
  99. ScriptMaps=sm,
  100. ScriptMapUpdate="replace",
  101. )
  102. params.VirtualDirs = [vd]
  103. HandleCommandLine(params)