From 26544b69beb41a5e042afa4254309fd1be205eae Mon Sep 17 00:00:00 2001 From: Martin Zimmermann Date: Wed, 30 Oct 2013 19:18:13 +0100 Subject: [PATCH] fix empty thread title due premature HTTP connection closing --- isso/__init__.py | 17 ++++++----------- isso/utils/http.py | 25 +++++++++++++++++++------ isso/views/comment.py | 8 ++++---- 3 files changed, 29 insertions(+), 21 deletions(-) diff --git a/isso/__init__.py b/isso/__init__.py index 4f683c5..83b6a35 100644 --- a/isso/__init__.py +++ b/isso/__init__.py @@ -58,7 +58,7 @@ from jinja2 import Environment, FileSystemLoader from isso import db, migrate, views, wsgi from isso.core import ThreadedMixin, uWSGIMixin, Config -from isso.utils import parse +from isso.utils import parse, http from isso.views import comment, admin logging.getLogger('werkzeug').setLevel(logging.ERROR) @@ -164,16 +164,11 @@ def make_app(conf=None): isso = App(conf) - for line in conf.getiter("general", "host"): - try: - host, port, ssl = parse.host(line) - con = httplib.HTTPSConnection if ssl else httplib.HTTPConnection - con(host, port, timeout=5).request('GET', '/') - except (httplib.HTTPException, socket.error): - continue - else: - logger.info("connected to HTTP server") - break + for host in conf.getiter("general", "host"): + with http.curl('HEAD', host, '/', 5) as resp: + if resp is not None: + logger.info("connected to HTTP server") + break else: logger.warn("unable to connect to HTTP server") diff --git a/isso/utils/http.py b/isso/utils/http.py index 1237cc7..0be920c 100644 --- a/isso/utils/http.py +++ b/isso/utils/http.py @@ -12,14 +12,27 @@ except ImportError: from isso.utils import parse -def curl(method, host, path, timeout=3): +class curl(object): - host, port, ssl = parse.host(host) - http = httplib.HTTPSConnection if ssl else httplib.HTTPConnection + def __init__(self, method, host, path, timeout=3): + self.method = method + self.host = host + self.path = path + self.timeout = timeout + + def __enter__(self): + + host, port, ssl = parse.host(self.host) + http = httplib.HTTPSConnection if ssl else httplib.HTTPConnection + + self.con = http(host, port, timeout=self.timeout) - with closing(http(host, port, timeout=timeout)) as con: try: - con.request(method, path) + self.con.request(self.method, self.path) except (httplib.HTTPException, socket.error): return None - return con.getresponse() + + return self.con.getresponse() + + def __exit__(self, exc_type, exc_value, traceback): + self.con.close() diff --git a/isso/views/comment.py b/isso/views/comment.py index f204b19..3bcbf56 100644 --- a/isso/views/comment.py +++ b/isso/views/comment.py @@ -73,10 +73,10 @@ def new(app, environ, request, uri): with app.lock: if uri not in app.db.threads: for host in app.conf.getiter('general', 'host'): - resp = http.curl('HEAD', host, uri) - if resp and resp.status == 200: - title = parse.title(resp.read()) - break + with http.curl('GET', host, uri) as resp: + if resp and resp.status == 200: + title = parse.title(resp.read()) + break else: return Response('URI does not exist', 404)