From cdfa5c419151b6269ebd5d78208065d32f495f67 Mon Sep 17 00:00:00 2001 From: dobefore <1432338032@qq.com> Date: Wed, 15 Sep 2021 21:39:44 +0800 Subject: [PATCH] rewrite_parse_chunked_req --- src/ankisyncd/sync.py | 2 +- src/ankisyncd/sync_app.py | 74 +++++++++++++++++++++++++++------------ 2 files changed, 53 insertions(+), 23 deletions(-) diff --git a/src/ankisyncd/sync.py b/src/ankisyncd/sync.py index e194cad..7c9f98d 100644 --- a/src/ankisyncd/sync.py +++ b/src/ankisyncd/sync.py @@ -332,7 +332,7 @@ from notes where %s""" % lim, self.maxUsn) self.col.db.executemany( "insert or replace into notes values (?,?,?,?,?,?,?,?,?,?,?)", rows) - self.col.after_note_updates([f[0] for f in rows],True) + self.col.after_note_updates([f[0] for f in rows], mark_modified=False, generate_cards=False) # Col config ########################################################################## diff --git a/src/ankisyncd/sync_app.py b/src/ankisyncd/sync_app.py index f2dba4a..ab14ee0 100644 --- a/src/ankisyncd/sync_app.py +++ b/src/ankisyncd/sync_app.py @@ -410,38 +410,67 @@ class Requests(object): length = 0 if content_len == '' else int(content_len) body=b'' d={} - + if length == 0: if input is None: return if env.get('HTTP_TRANSFER_ENCODING','0') == 'chunked': # readlines and read(no argument) will block + # convert byte str to number base 16 leng=int(input.readline(),16) + c=0 + bdry=b'' + data=[] + data_other=[] while leng >0: - body += input.read(leng+2) - leng = int(input.readline(),16) - boundary=body.splitlines()[0] - items=body.split(boundary) - items.pop() - items.pop(0) - # parse data - data_raw=items[0].strip() - cd=data_raw.splitlines()[0] - data_without_cd=data_raw[len(cd):].strip() - data=b'' - if b'\r\n' in data_without_cd: - # seems b'\r\n' should not exist in data - data=re.sub(b'\r\n',b'',data_without_cd,flags=re.M) - else: - data=data_without_cd - d['data']=data - # - others=items[1:] + c+=1 + dt = input.read(leng+2) + if c==1: + bdry=dt + elif c>=3: + # data + data_other.append(dt) + line=input.readline() + leng = int(line,16) + print(line) + data_other=[item for item in data_other if item!=b'\r\n\r\n'] + for item in data_other: + if bdry in item: + break + data.append(item.strip()) + d['data']=b''.join(data) + others=data_other[len(data):] + boundary=others[0] + others=b''.join(others).split(boundary.strip()) + others.pop() + others.pop(0) for i in others: i=i.splitlines() - key=re.findall(b'name="(.*?)"',i[2])[0].decode('utf-8') - v=i[-3].decode('utf-8') + key=re.findall(b'name="(.*?)"',i[2],flags=re.M)[0].decode('utf-8') + v=i[-1].decode('utf-8') d[key]=v + # boundary=body.splitlines()[0] + # items=body.split(boundary) + # items.pop() + # items.pop(0) + # # parse data + # data_raw=items[0].strip() + # cd=data_raw.splitlines()[0] + # data_without_cd=data_raw[len(cd):].strip() + # data=b'' + # if b'\r\n' in data_without_cd: + # # seems b'\r\n' should not exist in data + # data=re.sub(b'\r\n',b'',data_without_cd,flags=re.M) + # else: + # data=data_without_cd + # d['data']=data + # # + # others=items[1:] + # for i in others: + # i=i.splitlines() + # key=re.findall(b'name="(.*?)"',i[2])[0].decode('utf-8') + # v=i[-3].decode('utf-8') + # d[key]=v return d @@ -616,6 +645,7 @@ class SyncApp: try: data = p['data'] + print(compression) data = self._decode_data(data, compression) except KeyError: data = {}