Skip to content

Commit 4f44c2e

Browse files
authored
Post query json parse fix (#711)
* post append query: fix json parsing of lists to be identical to cdxj-indexer if json parsing errors occur, log to stderr fixes #709 in a better way * update CHANGES.rst
1 parent 09f7084 commit 4f44c2e

File tree

2 files changed

+14
-6
lines changed

2 files changed

+14
-6
lines changed

CHANGES.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ pywb 2.6.7 changelist
33

44
* dependency: bump gevent to latest (21.12.0)
55
* rewrite: fix eval rewriting where '._eval' was accidentally being rewritten
6+
* post-to-get conversion: properly handle json with top-level lists, to match cdxj-indexer, print parse errors, fixes `#709 <https://github.com/webrecorder/pywb/pull/709>`_
67

78
pywb 2.6.6 changelist
89
~~~~~~~~~~~~~~~~~~~~~

pywb/warcserver/inputrequest.py

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import base64
1212
import cgi
1313
import json
14+
import sys
1415

1516

1617
#=============================================================================
@@ -277,6 +278,7 @@ def handle_binary(query):
277278
try:
278279
query = self.json_parse(query)
279280
except Exception as e:
281+
sys.stderr.write("Ignoring query, error parsing as json: " + query.decode("utf-8") + "\n")
280282
query = ''
281283

282284
elif mime.startswith('text/plain'):
@@ -316,12 +318,17 @@ def get_key(n):
316318
dupes[n] += 1
317319
return n + "." + str(dupes[n]) + "_";
318320

319-
def _parser(dict_var):
320-
for n, v in dict_var.items():
321-
if isinstance(v, dict):
322-
_parser(v)
323-
else:
324-
data[get_key(n)] = str(v)
321+
def _parser(json_obj, name=""):
322+
if isinstance(json_obj, dict):
323+
for n, v in json_obj.items():
324+
_parser(v, n)
325+
326+
elif isinstance(json_obj, list):
327+
for v in json_obj:
328+
_parser(v, name)
329+
330+
elif name:
331+
data[get_key(name)] = str(json_obj)
325332

326333
_parser(json.loads(string))
327334
return urlencode(data)

0 commit comments

Comments
 (0)