2015-09-17 18:59:00 +02:00
|
|
|
# vim: ft=python fileencoding=utf-8 sts=4 sw=4 et:
|
|
|
|
|
2017-05-09 21:37:03 +02:00
|
|
|
# Copyright 2015-2017 Florian Bruhin (The Compiler) <mail@qutebrowser.org>
|
2015-09-17 18:59:00 +02:00
|
|
|
#
|
|
|
|
# This file is part of qutebrowser.
|
|
|
|
#
|
|
|
|
# qutebrowser is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# qutebrowser is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with qutebrowser. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
2016-05-29 18:20:00 +02:00
|
|
|
"""httpbin web server for end2end tests.
|
2015-09-17 18:59:00 +02:00
|
|
|
|
2016-05-29 18:20:00 +02:00
|
|
|
This script gets called as a QProcess from end2end/conftest.py.
|
2015-09-17 18:59:00 +02:00
|
|
|
"""
|
|
|
|
|
2015-09-17 06:53:28 +02:00
|
|
|
import sys
|
2015-11-26 14:37:47 +01:00
|
|
|
import json
|
2015-11-09 07:46:03 +01:00
|
|
|
import time
|
2015-11-20 13:24:38 +01:00
|
|
|
import signal
|
|
|
|
import os
|
2015-11-23 19:43:11 +01:00
|
|
|
import threading
|
2015-09-17 06:53:28 +02:00
|
|
|
|
|
|
|
from httpbin.core import app
|
2015-11-09 07:46:03 +01:00
|
|
|
from httpbin.structures import CaseInsensitiveDict
|
2017-01-23 07:36:17 +01:00
|
|
|
import cheroot.wsgi
|
2015-09-17 06:53:28 +02:00
|
|
|
import flask
|
|
|
|
|
|
|
|
|
2015-11-23 19:43:11 +01:00
|
|
|
_redirect_later_event = None
|
|
|
|
|
|
|
|
|
2015-09-17 07:45:12 +02:00
|
|
|
@app.route('/data/<path:path>')
|
|
|
|
def send_data(path):
|
2015-11-26 14:25:33 +01:00
|
|
|
"""Send a given data file to qutebrowser.
|
|
|
|
|
|
|
|
If a directory is requested, its index.html is sent.
|
|
|
|
"""
|
2015-09-17 21:51:09 +02:00
|
|
|
if hasattr(sys, 'frozen'):
|
|
|
|
basedir = os.path.realpath(os.path.dirname(sys.executable))
|
2016-05-29 18:20:00 +02:00
|
|
|
data_dir = os.path.join(basedir, 'end2end', 'data')
|
2015-09-17 21:51:09 +02:00
|
|
|
else:
|
2016-05-29 18:45:09 +02:00
|
|
|
basedir = os.path.join(os.path.realpath(os.path.dirname(__file__)),
|
|
|
|
'..')
|
2015-09-17 21:51:09 +02:00
|
|
|
data_dir = os.path.join(basedir, 'data')
|
2015-09-17 06:53:28 +02:00
|
|
|
print(basedir)
|
2015-11-24 20:43:20 +01:00
|
|
|
if os.path.isdir(os.path.join(data_dir, path)):
|
|
|
|
path = path + '/index.html'
|
2015-09-17 21:51:09 +02:00
|
|
|
return flask.send_from_directory(data_dir, path)
|
2015-09-17 06:53:28 +02:00
|
|
|
|
|
|
|
|
2015-11-09 07:46:03 +01:00
|
|
|
@app.route('/custom/redirect-later')
|
|
|
|
def redirect_later():
|
2015-11-26 14:25:33 +01:00
|
|
|
"""302 redirect to / after the given delay.
|
2015-11-23 19:43:11 +01:00
|
|
|
|
2015-11-26 14:25:33 +01:00
|
|
|
If delay is -1, wait until a request on redirect-later-continue is done.
|
2015-11-23 19:43:11 +01:00
|
|
|
"""
|
|
|
|
global _redirect_later_event
|
2015-11-09 07:46:03 +01:00
|
|
|
args = CaseInsensitiveDict(flask.request.args.items())
|
2015-11-23 19:43:11 +01:00
|
|
|
delay = int(args.get('delay', '1'))
|
|
|
|
if delay == -1:
|
|
|
|
_redirect_later_event = threading.Event()
|
2015-11-23 21:37:22 +01:00
|
|
|
ok = _redirect_later_event.wait(timeout=30 * 1000)
|
|
|
|
assert ok
|
2015-11-23 19:43:11 +01:00
|
|
|
_redirect_later_event = None
|
|
|
|
else:
|
|
|
|
time.sleep(delay)
|
|
|
|
x = flask.redirect('/')
|
|
|
|
return x
|
|
|
|
|
|
|
|
|
|
|
|
@app.route('/custom/redirect-later-continue')
|
|
|
|
def redirect_later_continue():
|
2015-11-23 19:46:47 +01:00
|
|
|
"""Continue a redirect-later request."""
|
2016-10-05 15:36:25 +02:00
|
|
|
if _redirect_later_event is None:
|
|
|
|
return flask.Response(b'Timed out or no redirect pending.')
|
|
|
|
else:
|
|
|
|
_redirect_later_event.set()
|
|
|
|
return flask.Response(b'Continued redirect.')
|
2015-11-09 07:46:03 +01:00
|
|
|
|
|
|
|
|
2016-09-09 16:58:39 +02:00
|
|
|
@app.route('/custom/content-size')
|
|
|
|
def content_size():
|
|
|
|
"""Send two bytes of data without a content-size."""
|
|
|
|
def generate_bytes():
|
|
|
|
yield b'*'
|
|
|
|
time.sleep(0.2)
|
|
|
|
yield b'*'
|
|
|
|
|
|
|
|
response = flask.Response(generate_bytes(), headers={
|
|
|
|
"Content-Type": "application/octet-stream",
|
|
|
|
})
|
|
|
|
response.status_code = 200
|
|
|
|
return response
|
|
|
|
|
|
|
|
|
2016-09-10 15:51:49 +02:00
|
|
|
@app.route('/custom/twenty-mb')
|
|
|
|
def twenty_mb():
|
|
|
|
"""Send 20MB of data."""
|
|
|
|
def generate_bytes():
|
|
|
|
yield b'*' * 20 * 1024 * 1024
|
|
|
|
|
|
|
|
response = flask.Response(generate_bytes(), headers={
|
|
|
|
"Content-Type": "application/octet-stream",
|
|
|
|
"Content-Length": str(20 * 1024 * 1024),
|
|
|
|
})
|
|
|
|
response.status_code = 200
|
|
|
|
return response
|
|
|
|
|
|
|
|
|
|
|
|
@app.route('/custom/redirect-self')
|
|
|
|
def redirect_self():
|
|
|
|
"""302 Redirects to itself."""
|
|
|
|
return app.make_response(flask.redirect(flask.url_for('redirect_self')))
|
|
|
|
|
|
|
|
|
2017-03-22 13:38:03 +01:00
|
|
|
@app.route('/custom/500-inline')
|
|
|
|
def internal_error_attachment():
|
|
|
|
"""A 500 error with Content-Disposition: inline."""
|
|
|
|
response = flask.Response(b"", headers={
|
|
|
|
"Content-Type": "application/octet-stream",
|
|
|
|
"Content-Disposition": 'inline; filename="attachment.jpg"',
|
|
|
|
})
|
|
|
|
response.status_code = 500
|
|
|
|
return response
|
|
|
|
|
|
|
|
|
2015-11-20 13:24:38 +01:00
|
|
|
@app.after_request
|
|
|
|
def log_request(response):
|
2015-11-26 14:25:33 +01:00
|
|
|
"""Log a webserver request."""
|
2015-11-20 13:24:38 +01:00
|
|
|
request = flask.request
|
2015-11-26 14:37:47 +01:00
|
|
|
data = {
|
|
|
|
'verb': request.method,
|
|
|
|
'path': request.full_path if request.query_string else request.path,
|
|
|
|
'status': response.status_code,
|
|
|
|
}
|
|
|
|
print(json.dumps(data), file=sys.stderr, flush=True)
|
2015-11-20 13:24:38 +01:00
|
|
|
return response
|
|
|
|
|
|
|
|
|
2017-01-23 07:36:17 +01:00
|
|
|
class WSGIServer(cheroot.wsgi.Server):
|
2015-11-20 16:23:46 +01:00
|
|
|
|
2015-11-26 14:25:33 +01:00
|
|
|
"""A custom WSGIServer that prints a line on stderr when it's ready.
|
|
|
|
|
|
|
|
Attributes:
|
|
|
|
_ready: Internal state for the 'ready' property.
|
|
|
|
_printed_ready: Whether the initial ready message was printed.
|
|
|
|
"""
|
2015-11-20 16:23:46 +01:00
|
|
|
|
2015-11-20 16:11:13 +01:00
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
super().__init__(*args, **kwargs)
|
|
|
|
self._ready = False
|
|
|
|
self._printed_ready = False
|
|
|
|
|
|
|
|
@property
|
|
|
|
def ready(self):
|
|
|
|
return self._ready
|
|
|
|
|
|
|
|
@ready.setter
|
|
|
|
def ready(self, value):
|
|
|
|
if value and not self._printed_ready:
|
|
|
|
print(' * Running on http://127.0.0.1:{}/ (Press CTRL+C to quit)'
|
|
|
|
.format(self.bind_addr[1]), file=sys.stderr, flush=True)
|
|
|
|
self._printed_ready = True
|
|
|
|
self._ready = value
|
2015-11-20 13:24:38 +01:00
|
|
|
|
|
|
|
|
2015-09-17 21:51:09 +02:00
|
|
|
def main():
|
|
|
|
if hasattr(sys, 'frozen'):
|
|
|
|
basedir = os.path.realpath(os.path.dirname(sys.executable))
|
2016-05-29 18:20:00 +02:00
|
|
|
app.template_folder = os.path.join(basedir, 'end2end', 'templates')
|
2015-11-20 13:24:38 +01:00
|
|
|
port = int(sys.argv[1])
|
2015-11-20 16:17:34 +01:00
|
|
|
server = WSGIServer(('127.0.0.1', port), app)
|
2015-11-20 13:24:38 +01:00
|
|
|
|
2015-11-20 16:13:30 +01:00
|
|
|
signal.signal(signal.SIGTERM, lambda *args: server.stop())
|
2015-11-20 13:24:38 +01:00
|
|
|
|
|
|
|
try:
|
|
|
|
server.start()
|
|
|
|
except KeyboardInterrupt:
|
2015-11-20 16:13:30 +01:00
|
|
|
server.stop()
|
2015-09-17 21:51:09 +02:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
main()
|