check if url exists before creating a comment
This commit is contained in:
parent
2c8c97b2a8
commit
3cb623e7c2
@ -53,16 +53,19 @@ url_map = Map([
|
|||||||
|
|
||||||
class Isso:
|
class Isso:
|
||||||
|
|
||||||
|
PRODUCTION = True
|
||||||
SECRET_KEY = ',\x1e\xbaY\xbb\xdf\xe7@\x85\xe3\xd9\xb4A9\xe4G\xa6O'
|
SECRET_KEY = ',\x1e\xbaY\xbb\xdf\xe7@\x85\xe3\xd9\xb4A9\xe4G\xa6O'
|
||||||
MODERATION = False
|
MODERATION = False
|
||||||
SQLITE = None
|
SQLITE = None
|
||||||
|
|
||||||
|
HOST = 'http://localhost:8000/'
|
||||||
MAX_AGE = 15*60
|
MAX_AGE = 15*60
|
||||||
|
|
||||||
def __init__(self, conf):
|
def __init__(self, conf):
|
||||||
|
|
||||||
self.__dict__.update(dict((k, v) for k, v in conf.iteritems() if k.isupper()))
|
self.__dict__.update(dict((k, v) for k, v in conf.iteritems() if k.isupper()))
|
||||||
self.signer = URLSafeTimedSerializer(self.SECRET_KEY)
|
self.signer = URLSafeTimedSerializer(self.SECRET_KEY)
|
||||||
|
self.HOST = utils.determine(self.HOST)
|
||||||
|
|
||||||
if self.SQLITE:
|
if self.SQLITE:
|
||||||
self.db = db.SQLite(self)
|
self.db = db.SQLite(self)
|
||||||
|
@ -8,15 +8,18 @@ from werkzeug.exceptions import abort
|
|||||||
|
|
||||||
from itsdangerous import SignatureExpired, BadSignature
|
from itsdangerous import SignatureExpired, BadSignature
|
||||||
|
|
||||||
from isso import json, models
|
from isso import json, models, utils
|
||||||
|
|
||||||
|
|
||||||
def create(app, environ, request, path):
|
def create(app, environ, request, path):
|
||||||
|
|
||||||
|
if app.PRODUCTION and not utils.urlexists(app.HOST, path):
|
||||||
|
return abort(404)
|
||||||
|
|
||||||
try:
|
try:
|
||||||
rv = app.db.add(path, models.Comment.fromjson(request.data))
|
rv = app.db.add(path, models.Comment.fromjson(request.data))
|
||||||
except ValueError as e:
|
except ValueError:
|
||||||
return Response(unicode(e), 400)
|
return abort(400)
|
||||||
|
|
||||||
response = Response(json.dumps(rv), 201, content_type='application/json')
|
response = Response(json.dumps(rv), 201, content_type='application/json')
|
||||||
response.set_cookie('session', app.signer.dumps([path, rv.id]), max_age=app.MAX_AGE)
|
response.set_cookie('session', app.signer.dumps([path, rv.id]), max_age=app.MAX_AGE)
|
||||||
|
@ -4,6 +4,11 @@
|
|||||||
# License: BSD Style, 2 clauses. see isso/__init__.py
|
# License: BSD Style, 2 clauses. see isso/__init__.py
|
||||||
|
|
||||||
import json
|
import json
|
||||||
|
import socket
|
||||||
|
import httplib
|
||||||
|
import urlparse
|
||||||
|
import contextlib
|
||||||
|
|
||||||
from isso.models import Comment
|
from isso.models import Comment
|
||||||
|
|
||||||
|
|
||||||
@ -14,3 +19,21 @@ class IssoEncoder(json.JSONEncoder):
|
|||||||
return dict((field, value) for field, value in obj.iteritems())
|
return dict((field, value) for field, value in obj.iteritems())
|
||||||
|
|
||||||
return json.JSONEncoder.default(self, obj)
|
return json.JSONEncoder.default(self, obj)
|
||||||
|
|
||||||
|
|
||||||
|
def urlexists(host, path):
|
||||||
|
with contextlib.closing(httplib.HTTPConnection(host)) as con:
|
||||||
|
try:
|
||||||
|
con.request('HEAD', path)
|
||||||
|
except socket.error:
|
||||||
|
return False
|
||||||
|
return con.getresponse().status == 200
|
||||||
|
|
||||||
|
|
||||||
|
def determine(host):
|
||||||
|
"""Make `host` compatible with :py:mod:`httplib`."""
|
||||||
|
|
||||||
|
if not host.startswith(('http://', 'https://')):
|
||||||
|
host = 'http://' + host
|
||||||
|
rv = urlparse.urlparse(host)
|
||||||
|
return (rv.netloc + ':443') if rv.scheme == 'https' else rv.netloc
|
||||||
|
@ -17,7 +17,7 @@ class TestComments(unittest.TestCase):
|
|||||||
|
|
||||||
def setUp(self):
|
def setUp(self):
|
||||||
fd, self.path = tempfile.mkstemp()
|
fd, self.path = tempfile.mkstemp()
|
||||||
self.app = Isso({'SQLITE': self.path})
|
self.app = Isso({'SQLITE': self.path, 'PRODUCTION': False})
|
||||||
|
|
||||||
self.client = Client(self.app, Response)
|
self.client = Client(self.app, Response)
|
||||||
self.get = lambda *x, **z: self.client.get(*x, **z)
|
self.get = lambda *x, **z: self.client.get(*x, **z)
|
||||||
|
Loading…
Reference in New Issue
Block a user