fix sqlite3 string escaping

This commit is contained in:
Noah Levitt 2018-02-12 11:42:35 -08:00
parent b927789c4b
commit 6d6f2c9aa0
2 changed files with 8 additions and 12 deletions

View File

@ -40,7 +40,7 @@ except:
setuptools.setup( setuptools.setup(
name='warcprox', name='warcprox',
version='2.4b2.dev148', version='2.4b2.dev149',
description='WARC writing MITM HTTP/S proxy', description='WARC writing MITM HTTP/S proxy',
url='https://github.com/internetarchive/warcprox', url='https://github.com/internetarchive/warcprox',
author='Noah Levitt', author='Noah Levitt',

View File

@ -103,17 +103,13 @@ class TroughClient(object):
elif isinstance(x, bool): elif isinstance(x, bool):
return int(x) return int(x)
elif isinstance(x, str) or isinstance(x, bytes): elif isinstance(x, str) or isinstance(x, bytes):
# py3: repr(u'abc') => 'abc' # the only character that needs escaped in sqlite string literals
# repr(b'abc') => b'abc' # is single-quote, which is escaped as two single-quotes
# py2: repr(u'abc') => u'abc' if isinstance(x, bytes):
# repr(b'abc') => 'abc' s = x.decode('utf-8')
# Repr gives us a prefix we don't want in different situations
# depending on whether this is py2 or py3. Chop it off either way.
r = repr(x)
if r[:1] == "'":
return r
else: else:
return r[1:] s = x
return "'" + s.replace("'", "''") + "'"
elif isinstance(x, (int, float)): elif isinstance(x, (int, float)):
return x return x
else: else:
@ -196,7 +192,7 @@ class TroughClient(object):
response.status_code, response.reason, response.text, response.status_code, response.reason, response.text,
write_url, sql) write_url, sql)
return return
self.logger.debug('posted %r to %s', sql, write_url) self.logger.debug('posted to %s: %r', write_url, sql)
def read(self, segment_id, sql_tmpl, values=()): def read(self, segment_id, sql_tmpl, values=()):
read_url = self.read_url(segment_id) read_url = self.read_url(segment_id)