Skip to content

Commit

Permalink
Post query json parse fix (#711)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
ikreymer authored Apr 15, 2022
1 parent 09f7084 commit 4f44c2e
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 6 deletions.
1 change: 1 addition & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ pywb 2.6.7 changelist

* dependency: bump gevent to latest (21.12.0)
* rewrite: fix eval rewriting where '._eval' was accidentally being rewritten
* 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>`_

pywb 2.6.6 changelist
~~~~~~~~~~~~~~~~~~~~~
Expand Down
19 changes: 13 additions & 6 deletions pywb/warcserver/inputrequest.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import base64
import cgi
import json
import sys


#=============================================================================
Expand Down Expand Up @@ -277,6 +278,7 @@ def handle_binary(query):
try:
query = self.json_parse(query)
except Exception as e:
sys.stderr.write("Ignoring query, error parsing as json: " + query.decode("utf-8") + "\n")
query = ''

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

def _parser(dict_var):
for n, v in dict_var.items():
if isinstance(v, dict):
_parser(v)
else:
data[get_key(n)] = str(v)
def _parser(json_obj, name=""):
if isinstance(json_obj, dict):
for n, v in json_obj.items():
_parser(v, n)

elif isinstance(json_obj, list):
for v in json_obj:
_parser(v, name)

elif name:
data[get_key(name)] = str(json_obj)

_parser(json.loads(string))
return urlencode(data)
Expand Down

0 comments on commit 4f44c2e

Please sign in to comment.