diff options
| author | Alon Levy <alon@pobox.com> | 2014-11-16 13:12:15 +0200 |
|---|---|---|
| committer | Alon Levy <alon@pobox.com> | 2014-11-16 13:13:15 +0200 |
| commit | b7b260edc37451fad7eb94a806cab23d2d22003a (patch) | |
| tree | 9220169939e6948830d3c64914a8c4d6ebe7c2e8 /tests/util | |
| parent | 76d1de512a58f023ae348ae8500100490fba9915 (diff) | |
Adding helper server - launch with python tests/util/RhiziHTTPServer.py
SimpleHTTPServer does a wrong 301 for query parameters, i.e.:
http://bla:foo/?hello=1
301 =>
http://bla:foo/?hello=1/
which kills our query parameter parsing. Instead of making the client
handle non compliant servers I rather fix the test server to not 301.
Real servers (need to check) don't do this I hope.
Diffstat (limited to 'tests/util')
| -rw-r--r-- | tests/util/RhiziHTTPServer.py | 78 |
1 files changed, 78 insertions, 0 deletions
diff --git a/tests/util/RhiziHTTPServer.py b/tests/util/RhiziHTTPServer.py new file mode 100644 index 00000000..29e8980d --- /dev/null +++ b/tests/util/RhiziHTTPServer.py @@ -0,0 +1,78 @@ +""" +Inherits from python-2 SimpleHTTPServer (same bug exists in python3 +http.server) to fix issue where query argument is mistakenly treated as a +improperly non slash terminated path. +""" + + +import os +import BaseHTTPServer +import SimpleHTTPServer + +try: + from cStringIO import StringIO +except ImportError: + from StringIO import StringIO + + +class MyHTTPRequestHandler(SimpleHTTPServer.SimpleHTTPRequestHandler): + + """ + Fix lack of handling for query parameters in SimpleHTTPRequestHandler + """ + + def send_head(self): + """Common code for GET and HEAD commands. + + This sends the response code and MIME headers. + + Return value is either a file object (which has to be copied + to the outputfile by the caller unless the command was HEAD, + and must be closed by the caller under all circumstances), or + None, in which case the caller has nothing further to do. + + """ + path = self.translate_path(self.path) + f = None + if os.path.isdir(path): + if not path.endswith('/'): + # redirect browser - doing basically what apache does + self.send_response(301) + self.send_header("Location", self.path + "/") + self.end_headers() + return None + for index in "index.html", "index.htm": + index = os.path.join(path, index) + if os.path.exists(index): + path = index + break + else: + return self.list_directory(path) + ctype = self.guess_type(path) + try: + # Always read in binary mode. Opening files in text mode may cause + # newline translations, making the actual size of the content + # transmitted *less* than the content-length! + f = open(path, 'rb') + except IOError: + self.send_error(404, "File not found") + return None + try: + self.send_response(200) + self.send_header("Content-type", ctype) + fs = os.fstat(f.fileno()) + self.send_header("Content-Length", str(fs[6])) + self.send_header("Last-Modified", self.date_time_string(fs.st_mtime)) + self.end_headers() + return f + except: + f.close() + raise + +def test(HandlerClass = MyHTTPRequestHandler, + ServerClass = BaseHTTPServer.HTTPServer): + BaseHTTPServer.test(HandlerClass, ServerClass) + + +if __name__ == '__main__': + test() |
