From 52f64dd26ff5777b2c6a2d64dc92c2c9de40c4ad Mon Sep 17 00:00:00 2001 From: Martin Zimmermann Date: Fri, 13 Sep 2013 19:52:53 +0200 Subject: [PATCH] improve url exists detection for new comment threads --- isso/utils.py | 23 +++++++++++++++++------ isso/views/comment.py | 9 ++++----- 2 files changed, 21 insertions(+), 11 deletions(-) diff --git a/isso/utils.py b/isso/utils.py index 6953447..62ca2b9 100644 --- a/isso/utils.py +++ b/isso/utils.py @@ -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") diff --git a/isso/views/comment.py b/isso/views/comment.py index fb2e99e..e29d866 100644 --- a/isso/views/comment.py +++ b/isso/views/comment.py @@ -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)