rewrite_parse_chunked_req

This commit is contained in:
dobefore 2021-09-15 21:39:44 +08:00 committed by Vikash Kothary
parent 443f3b6fe3
commit cdfa5c4191
2 changed files with 53 additions and 23 deletions

View File

@ -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
##########################################################################

View File

@ -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 = {}