fix empty thread title due premature HTTP connection closing

legacy/0.2
Martin Zimmermann 11 years ago
parent 319492fa42
commit 26544b69be

@ -58,7 +58,7 @@ from jinja2 import Environment, FileSystemLoader
from isso import db, migrate, views, wsgi from isso import db, migrate, views, wsgi
from isso.core import ThreadedMixin, uWSGIMixin, Config from isso.core import ThreadedMixin, uWSGIMixin, Config
from isso.utils import parse from isso.utils import parse, http
from isso.views import comment, admin from isso.views import comment, admin
logging.getLogger('werkzeug').setLevel(logging.ERROR) logging.getLogger('werkzeug').setLevel(logging.ERROR)
@ -164,16 +164,11 @@ def make_app(conf=None):
isso = App(conf) isso = App(conf)
for line in conf.getiter("general", "host"): for host in conf.getiter("general", "host"):
try: with http.curl('HEAD', host, '/', 5) as resp:
host, port, ssl = parse.host(line) if resp is not None:
con = httplib.HTTPSConnection if ssl else httplib.HTTPConnection logger.info("connected to HTTP server")
con(host, port, timeout=5).request('GET', '/') break
except (httplib.HTTPException, socket.error):
continue
else:
logger.info("connected to HTTP server")
break
else: else:
logger.warn("unable to connect to HTTP server") logger.warn("unable to connect to HTTP server")

@ -12,14 +12,27 @@ except ImportError:
from isso.utils import parse from isso.utils import parse
def curl(method, host, path, timeout=3): class curl(object):
host, port, ssl = parse.host(host) def __init__(self, method, host, path, timeout=3):
http = httplib.HTTPSConnection if ssl else httplib.HTTPConnection 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: try:
con.request(method, path) self.con.request(self.method, self.path)
except (httplib.HTTPException, socket.error): except (httplib.HTTPException, socket.error):
return None return None
return con.getresponse()
return self.con.getresponse()
def __exit__(self, exc_type, exc_value, traceback):
self.con.close()

@ -73,10 +73,10 @@ def new(app, environ, request, uri):
with app.lock: with app.lock:
if uri not in app.db.threads: if uri not in app.db.threads:
for host in app.conf.getiter('general', 'host'): for host in app.conf.getiter('general', 'host'):
resp = http.curl('HEAD', host, uri) with http.curl('GET', host, uri) as resp:
if resp and resp.status == 200: if resp and resp.status == 200:
title = parse.title(resp.read()) title = parse.title(resp.read())
break break
else: else:
return Response('URI does not exist', 404) return Response('URI does not exist', 404)

Loading…
Cancel
Save