summaryrefslogtreecommitdiffstats
path: root/test/functional/util/dummy_http.py
diff options
context:
space:
mode:
authorMikhail Galanin <mgalanin@mimecast.com>2018-08-30 16:51:55 +0100
committerMikhail Galanin <mgalanin@mimecast.com>2018-08-30 16:51:55 +0100
commitb4d4cff66970d51f4f82a37e03de2db07291742e (patch)
treed425b58e1f22a7c54a21016ba1d33ed37cd9ef5a /test/functional/util/dummy_http.py
parenta78803aeb558c0ebb9ada2a0f71f960ac31f373d (diff)
downloadrspamd-b4d4cff66970d51f4f82a37e03de2db07291742e.tar.gz
rspamd-b4d4cff66970d51f4f82a37e03de2db07291742e.zip
[Test] Test for TCP library
Diffstat (limited to 'test/functional/util/dummy_http.py')
-rwxr-xr-xtest/functional/util/dummy_http.py75
1 files changed, 48 insertions, 27 deletions
diff --git a/test/functional/util/dummy_http.py b/test/functional/util/dummy_http.py
index 4f8e67ffd..4814613ea 100755
--- a/test/functional/util/dummy_http.py
+++ b/test/functional/util/dummy_http.py
@@ -1,10 +1,14 @@
#!/usr/bin/env python
import BaseHTTPServer
+import SocketServer
+import SimpleHTTPServer
+
import time
import os
import sys
import signal
+import socket
PORT = 18080
HOST_NAME = '127.0.0.1'
@@ -14,12 +18,19 @@ PID = "/tmp/dummy_http.pid"
class MyHandler(BaseHTTPServer.BaseHTTPRequestHandler):
+ def setup(self):
+ BaseHTTPServer.BaseHTTPRequestHandler.setup(self)
+ self.protocol_version = "HTTP/1.1" # allow connection: keep-alive
+
def do_HEAD(self):
self.send_response(200)
self.send_header("Content-type", "text/html")
self.end_headers()
+ self.log_message("to be closed: " + self.close_connection)
def do_GET(self):
+ response = "hello world"
+
"""Respond to a GET request."""
if self.path == "/empty":
self.finish()
@@ -33,11 +44,23 @@ class MyHandler(BaseHTTPServer.BaseHTTPRequestHandler):
else:
self.send_response(200)
+ if self.path == "/content-length":
+ self.send_header("Content-Length", str(len(response)))
+
self.send_header("Content-type", "text/plain")
self.end_headers()
- self.wfile.write("hello world")
+ self.wfile.write(response)
+ self.log_message("to be closed: %d, headers: %s, conn:'%s'" % (self.close_connection, str(self.headers), self.headers.get('Connection', "").lower()))
+
+ conntype = self.headers.get('Connection', "").lower()
+ if conntype != 'keep-alive':
+ self.close_connection = True
+
+ self.log_message("ka:'%s', pv:%s[%s]" % (str(conntype == 'keep-alive'), str(self.protocol_version >= "HTTP/1.1"), self.protocol_version))
+
def do_POST(self):
+ response = "hello post"
"""Respond to a GET request."""
if self.path == "/empty":
self.finish()
@@ -50,30 +73,35 @@ class MyHandler(BaseHTTPServer.BaseHTTPRequestHandler):
self.send_response(403)
else:
self.send_response(200)
+
+ if self.path == "/content-length":
+ self.send_header("Content-Length", str(len(response)))
self.send_header("Content-type", "text/plain")
self.end_headers()
- self.wfile.write("hello post")
-
+ self.wfile.write(response)
-class MyHttp(BaseHTTPServer.HTTPServer):
- def __init__(self, server_address, RequestHandlerClass, bind_and_activate=False):
- BaseHTTPServer.HTTPServer.__init__(self, server_address, RequestHandlerClass, bind_and_activate)
- self.keep_running = True
+class ThreadingSimpleServer(SocketServer.ThreadingMixIn,
+ BaseHTTPServer.HTTPServer):
+ def __init__(self):
+ BaseHTTPServer.HTTPServer.__init__(self, (HOST_NAME, PORT), MyHandler)
+ self.allow_reuse_address = True
+ self.timeout = 1
+
def run(self):
- self.server_bind()
- self.server_activate()
-
with open(PID, 'w+') as f:
f.write(str(os.getpid()))
f.close()
-
- while self.keep_running:
- try:
- self.handle_request()
- except Exception:
- pass
+ try:
+ while 1:
+ sys.stdout.flush()
+ server.handle_request()
+ except KeyboardInterrupt:
+ print "Interrupt"
+ except socket.error:
+ print "Socket closed"
+ pass
def stop(self):
self.keep_running = False
@@ -81,20 +109,13 @@ class MyHttp(BaseHTTPServer.HTTPServer):
if __name__ == '__main__':
- server_class = BaseHTTPServer.HTTPServer
- httpd = MyHttp((HOST_NAME, PORT), MyHandler)
- httpd.allow_reuse_address = True
- httpd.timeout = 1
+ server = ThreadingSimpleServer()
def alarm_handler(signum, frame):
- httpd.stop()
+ server.stop()
signal.signal(signal.SIGALRM, alarm_handler)
signal.signal(signal.SIGTERM, alarm_handler)
- signal.alarm(10)
+ signal.alarm(1000)
- try:
- httpd.run()
- except KeyboardInterrupt:
- pass
- httpd.server_close()
+ server.run()