improve url exists detection for new comment threads

This commit is contained in:
Martin Zimmermann 2013-09-13 19:52:53 +02:00
parent 30fa254023
commit 52f64dd26f
2 changed files with 21 additions and 11 deletions

View File

@ -32,12 +32,23 @@ class IssoEncoder(json.JSONEncoder):
return json.JSONEncoder.default(self, obj)
def urlexists(host, path):
def normalize(host):
if not host.startswith(('http://', 'https://')):
host = 'https://' + host
rv = urlparse(host)
http = httplib.HTTPSConnection if rv.scheme == 'https' else httplib.HTTPConnection
if rv.scheme == 'https':
return (rv.netloc, 443)
return (rv.netloc.rsplit(':')[0], rv.port or 80)
with closing(http(rv.netloc, rv.port, timeout=3)) as con:
def urlexists(host, path):
host, port = normalize(host)
http = httplib.HTTPSConnection if port == 443 else httplib.HTTPConnection
with closing(http(host, port, timeout=3)) as con:
try:
con.request('HEAD', path)
except (httplib.HTTPException, socket.error):
@ -49,10 +60,10 @@ def heading(host, path):
"""Connect to `host`, GET path and start from #isso-thread to search for
a possible heading (h1). Returns `None` if nothing found."""
rv = urlparse(host)
http = httplib.HTTPSConnection if rv.scheme == 'https' else httplib.HTTPConnection
host, port = normalize(host)
http = httplib.HTTPSConnection if port == 443 else httplib.HTTPConnection
with closing(http(rv.netloc, rv.port, timeout=15)) as con:
with closing(http(host, port, timeout=15)) as con:
con.request('GET', path)
html = html5lib.parse(con.getresponse().read(), treebuilder="dom")

View File

@ -43,7 +43,7 @@ class requires:
@requires(str, 'uri')
def create(app, environ, request, uri):
if not utils.urlexists(app.ORIGIN, uri):
if app.db.threads.get(uri) is None and not utils.urlexists(app.ORIGIN, uri):
return Response('URI does not exist', 404)
try:
@ -70,13 +70,12 @@ def create(app, environ, request, uri):
hash=hashlib.md5(hash).hexdigest())
try:
title = app.db.threads.get(uri)
except KeyError:
title = app.db.threads.get(uri)
if title is None:
title = app.db.threads.add(uri, utils.heading(app.ORIGIN, uri))
try:
rv = app.db.add(uri, comment, utils.anonymize(unicode(request.remote_addr)))
rv = app.db.add(uri, comment, request.remote_addr)
except sqlite3.Error:
logging.exception('uncaught SQLite3 exception')
abort(400)