2014-06-03 18:32:44 +00:00
|
|
|
#!/usr/bin/env python
|
|
|
|
|
|
2017-11-30 18:33:11 +00:00
|
|
|
# Copyright 2012 - 2017, New York University and the TUF contributors
|
|
|
|
|
# SPDX-License-Identifier: MIT OR Apache-2.0
|
|
|
|
|
|
2014-03-31 22:28:54 +00:00
|
|
|
"""
|
|
|
|
|
<Program>
|
|
|
|
|
simple_server.py
|
2017-11-30 18:33:11 +00:00
|
|
|
|
2014-03-31 22:28:54 +00:00
|
|
|
<Author>
|
2014-04-22 19:03:42 +00:00
|
|
|
Konstantin Andrianov.
|
2014-03-31 22:28:54 +00:00
|
|
|
|
|
|
|
|
<Started>
|
2014-04-22 19:03:42 +00:00
|
|
|
February 15, 2012.
|
2017-11-30 18:33:11 +00:00
|
|
|
|
2014-03-31 22:28:54 +00:00
|
|
|
<Copyright>
|
2018-02-05 16:31:19 +00:00
|
|
|
See LICENSE-MIT or LICENSE for licensing information.
|
2014-03-31 22:28:54 +00:00
|
|
|
|
|
|
|
|
<Purpose>
|
2017-11-30 18:33:11 +00:00
|
|
|
This is a basic server that was designed to be used in conjunction with
|
|
|
|
|
test_download.py to test download.py module.
|
2014-03-31 22:28:54 +00:00
|
|
|
|
2014-04-22 19:03:42 +00:00
|
|
|
<Reference>
|
2014-03-31 22:28:54 +00:00
|
|
|
SimpleHTTPServer:
|
|
|
|
|
http://docs.python.org/library/simplehttpserver.html#module-SimpleHTTPServer
|
|
|
|
|
"""
|
|
|
|
|
|
2014-04-29 18:27:34 +00:00
|
|
|
# Help with Python 3 compatibility, where the print statement is a function, an
|
|
|
|
|
# implicit relative import is invalid, and the '/' operator performs true
|
|
|
|
|
# division. Example: print 'hello world' raises a 'SyntaxError' exception.
|
|
|
|
|
from __future__ import print_function
|
|
|
|
|
from __future__ import absolute_import
|
|
|
|
|
from __future__ import division
|
|
|
|
|
from __future__ import unicode_literals
|
|
|
|
|
|
2014-03-31 22:28:54 +00:00
|
|
|
import sys
|
|
|
|
|
import random
|
Fix failing AppVeyor Python2.7 tests
Since #885 the tests in TestUpdater and TestKeyRevocation fail on
Appveyor Python 2.7 builds. After some live debugging, it turns out
that the tests fail due to the extra amount of http requests to
the simple http server (see tests/simple_server.py) that were
added in #885.
The simple server runs in a subprocess and is re-used for the
entire TestCase. After a certain amount of requests it becomes
unresponsive. Note that neither the subprocess exits (ps -W), nor
does the port get closed (netstat -a). It just doesn't serve the
request, making it time out and fail the test.
The following script can be used to reproduce the issue (run in
tests directory):
```python
import subprocess
import requests
import random
counter = 0
port = random.randint(30000, 45000)
command = ['python', 'simple_server.py', str(port)]
server_process = subprocess.Popen(command, stderr=subprocess.PIPE)
url = 'http://localhost:'+str(port) + '/'
sess = requests.Session()
try:
while True:
sess.get(url, timeout=3)
counter +=1
finally:
print(counter)
server_process.kill()
```
It fails repeatedly on the 69th request, but only if
`stderr=subprocess.PIPE` is passed to Popen. Given that for each
request the simple server writes about ~60 characters to stderr,
e.g. ...
```
127.0.0.1 - - [24/Feb/2020 12:01:23] "GET / HTTP/1.1" 200 -
```
... it looks a lot like a full pipe buffer of size 4096. Note that the
`bufsize` argument to Popen does not change anything.
As a simple work around we silence the test server on
Windows/Python2 to not fill the buffer.
Signed-off-by: Lukas Puehringer <lukas.puehringer@nyu.edu>
2020-02-24 13:51:28 +00:00
|
|
|
import platform
|
2014-04-22 19:03:42 +00:00
|
|
|
|
2015-06-02 14:28:02 +00:00
|
|
|
import six
|
Fix failing AppVeyor Python2.7 tests
Since #885 the tests in TestUpdater and TestKeyRevocation fail on
Appveyor Python 2.7 builds. After some live debugging, it turns out
that the tests fail due to the extra amount of http requests to
the simple http server (see tests/simple_server.py) that were
added in #885.
The simple server runs in a subprocess and is re-used for the
entire TestCase. After a certain amount of requests it becomes
unresponsive. Note that neither the subprocess exits (ps -W), nor
does the port get closed (netstat -a). It just doesn't serve the
request, making it time out and fail the test.
The following script can be used to reproduce the issue (run in
tests directory):
```python
import subprocess
import requests
import random
counter = 0
port = random.randint(30000, 45000)
command = ['python', 'simple_server.py', str(port)]
server_process = subprocess.Popen(command, stderr=subprocess.PIPE)
url = 'http://localhost:'+str(port) + '/'
sess = requests.Session()
try:
while True:
sess.get(url, timeout=3)
counter +=1
finally:
print(counter)
server_process.kill()
```
It fails repeatedly on the 69th request, but only if
`stderr=subprocess.PIPE` is passed to Popen. Given that for each
request the simple server writes about ~60 characters to stderr,
e.g. ...
```
127.0.0.1 - - [24/Feb/2020 12:01:23] "GET / HTTP/1.1" 200 -
```
... it looks a lot like a full pipe buffer of size 4096. Note that the
`bufsize` argument to Popen does not change anything.
As a simple work around we silence the test server on
Windows/Python2 to not fill the buffer.
Signed-off-by: Lukas Puehringer <lukas.puehringer@nyu.edu>
2020-02-24 13:51:28 +00:00
|
|
|
from six.moves.SimpleHTTPServer import SimpleHTTPRequestHandler
|
2014-03-31 22:28:54 +00:00
|
|
|
|
|
|
|
|
PORT = 0
|
|
|
|
|
|
|
|
|
|
def _port_gen():
|
|
|
|
|
return random.randint(30000, 45000)
|
|
|
|
|
|
|
|
|
|
if len(sys.argv) > 1:
|
|
|
|
|
try:
|
|
|
|
|
PORT = int(sys.argv[1])
|
|
|
|
|
if PORT < 30000 or PORT > 45000:
|
|
|
|
|
raise ValueError
|
2017-11-30 18:33:11 +00:00
|
|
|
|
2014-03-31 22:28:54 +00:00
|
|
|
except ValueError:
|
|
|
|
|
PORT = _port_gen()
|
2014-04-22 19:03:42 +00:00
|
|
|
|
2014-03-31 22:28:54 +00:00
|
|
|
else:
|
|
|
|
|
PORT = _port_gen()
|
|
|
|
|
|
Fix failing AppVeyor Python2.7 tests
Since #885 the tests in TestUpdater and TestKeyRevocation fail on
Appveyor Python 2.7 builds. After some live debugging, it turns out
that the tests fail due to the extra amount of http requests to
the simple http server (see tests/simple_server.py) that were
added in #885.
The simple server runs in a subprocess and is re-used for the
entire TestCase. After a certain amount of requests it becomes
unresponsive. Note that neither the subprocess exits (ps -W), nor
does the port get closed (netstat -a). It just doesn't serve the
request, making it time out and fail the test.
The following script can be used to reproduce the issue (run in
tests directory):
```python
import subprocess
import requests
import random
counter = 0
port = random.randint(30000, 45000)
command = ['python', 'simple_server.py', str(port)]
server_process = subprocess.Popen(command, stderr=subprocess.PIPE)
url = 'http://localhost:'+str(port) + '/'
sess = requests.Session()
try:
while True:
sess.get(url, timeout=3)
counter +=1
finally:
print(counter)
server_process.kill()
```
It fails repeatedly on the 69th request, but only if
`stderr=subprocess.PIPE` is passed to Popen. Given that for each
request the simple server writes about ~60 characters to stderr,
e.g. ...
```
127.0.0.1 - - [24/Feb/2020 12:01:23] "GET / HTTP/1.1" 200 -
```
... it looks a lot like a full pipe buffer of size 4096. Note that the
`bufsize` argument to Popen does not change anything.
As a simple work around we silence the test server on
Windows/Python2 to not fill the buffer.
Signed-off-by: Lukas Puehringer <lukas.puehringer@nyu.edu>
2020-02-24 13:51:28 +00:00
|
|
|
|
|
|
|
|
class QuietHTTPRequestHandler(SimpleHTTPRequestHandler):
|
|
|
|
|
"""A SimpleHTTPRequestHandler that does not write incoming requests to
|
|
|
|
|
stderr. """
|
|
|
|
|
def log_request(self, code='-', size='-'):
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
# NOTE: On Windows/Python2 tests that use this simple_server.py in a
|
|
|
|
|
# subprocesses hang after a certain amount of requests (~68), if a PIPE is
|
2020-05-07 14:58:37 +00:00
|
|
|
# passed as Popen's stderr argument. This problem doesn't emerge if
|
|
|
|
|
# we silence the HTTP messages.
|
|
|
|
|
# If you decide to receive the HTTP messages, then this bug
|
|
|
|
|
# could reappear.
|
|
|
|
|
use_quiet_http_request_handler = True
|
2020-05-05 18:25:52 +00:00
|
|
|
|
2020-05-07 14:58:37 +00:00
|
|
|
if len(sys.argv) > 2:
|
|
|
|
|
use_quiet_http_request_handler = sys.argv[2]
|
2020-05-05 18:25:52 +00:00
|
|
|
|
2020-05-07 14:58:37 +00:00
|
|
|
if use_quiet_http_request_handler:
|
|
|
|
|
handler = QuietHTTPRequestHandler
|
|
|
|
|
else:
|
|
|
|
|
handler = SimpleHTTPRequestHandler
|
Fix failing AppVeyor Python2.7 tests
Since #885 the tests in TestUpdater and TestKeyRevocation fail on
Appveyor Python 2.7 builds. After some live debugging, it turns out
that the tests fail due to the extra amount of http requests to
the simple http server (see tests/simple_server.py) that were
added in #885.
The simple server runs in a subprocess and is re-used for the
entire TestCase. After a certain amount of requests it becomes
unresponsive. Note that neither the subprocess exits (ps -W), nor
does the port get closed (netstat -a). It just doesn't serve the
request, making it time out and fail the test.
The following script can be used to reproduce the issue (run in
tests directory):
```python
import subprocess
import requests
import random
counter = 0
port = random.randint(30000, 45000)
command = ['python', 'simple_server.py', str(port)]
server_process = subprocess.Popen(command, stderr=subprocess.PIPE)
url = 'http://localhost:'+str(port) + '/'
sess = requests.Session()
try:
while True:
sess.get(url, timeout=3)
counter +=1
finally:
print(counter)
server_process.kill()
```
It fails repeatedly on the 69th request, but only if
`stderr=subprocess.PIPE` is passed to Popen. Given that for each
request the simple server writes about ~60 characters to stderr,
e.g. ...
```
127.0.0.1 - - [24/Feb/2020 12:01:23] "GET / HTTP/1.1" 200 -
```
... it looks a lot like a full pipe buffer of size 4096. Note that the
`bufsize` argument to Popen does not change anything.
As a simple work around we silence the test server on
Windows/Python2 to not fill the buffer.
Signed-off-by: Lukas Puehringer <lukas.puehringer@nyu.edu>
2020-02-24 13:51:28 +00:00
|
|
|
|
|
|
|
|
httpd = six.moves.socketserver.TCPServer(('', PORT), handler)
|
2014-03-31 22:28:54 +00:00
|
|
|
|
|
|
|
|
httpd.serve_forever()
|