[~] Refactor
This commit is contained in:
parent
e7bcb225a6
commit
1865e81973
50
d1/f2.py
50
d1/f2.py
@ -226,6 +226,14 @@ class Application:
|
|||||||
content_chunks.append(chunk[1])
|
content_chunks.append(chunk[1])
|
||||||
returncode = chunk[2]
|
returncode = chunk[2]
|
||||||
|
|
||||||
|
self.op1(
|
||||||
|
json_data=[
|
||||||
|
headers_lines,
|
||||||
|
returncode,
|
||||||
|
len(chunk[1]),
|
||||||
|
len(content_chunks),
|
||||||
|
]
|
||||||
|
)
|
||||||
if len(headers_lines) > 0 and headers_lines[-1] == '':
|
if len(headers_lines) > 0 and headers_lines[-1] == '':
|
||||||
break
|
break
|
||||||
|
|
||||||
@ -257,6 +265,7 @@ class Application:
|
|||||||
sent_bytes = 0
|
sent_bytes = 0
|
||||||
|
|
||||||
content_length = None
|
content_length = None
|
||||||
|
content_length2 = None
|
||||||
|
|
||||||
def dump_headers():
|
def dump_headers():
|
||||||
self.op1(
|
self.op1(
|
||||||
@ -268,47 +277,45 @@ class Application:
|
|||||||
)
|
)
|
||||||
|
|
||||||
nonlocal content_length
|
nonlocal content_length
|
||||||
content_length2 = None
|
nonlocal content_length2
|
||||||
|
|
||||||
if 'Content-Length' in headers_detailed['headers']:
|
if 'Content-Length' in headers_detailed['headers']:
|
||||||
content_length = int(headers_detailed['headers']['Content-Length'])
|
content_length2 = int(headers_detailed['headers']['Content-Length'])
|
||||||
else:
|
else:
|
||||||
content_length = 0
|
content_length2 = 0
|
||||||
|
|
||||||
if headers_detailed['headers'].get('Transfer-Encoding') == 'chunked':
|
if headers_detailed['headers'].get('Transfer-Encoding') == 'chunked':
|
||||||
del headers_detailed['headers']['Transfer-Encoding']
|
del headers_detailed['headers']['Transfer-Encoding']
|
||||||
assert sent_bytes == 0
|
assert sent_bytes == 0
|
||||||
|
|
||||||
if finished_output:
|
if finished_output:
|
||||||
content_length = get_output_length()
|
content_length2 = get_output_length()
|
||||||
else:
|
else:
|
||||||
content_length = Application.MAX_FILE_SIZE
|
content_length2 = Application.MAX_FILE_SIZE
|
||||||
|
|
||||||
headers_detailed['headers']['Content-Length'] = \
|
headers_detailed['headers']['Content-Length'] = \
|
||||||
'%d' % content_length
|
'%d' % content_length2
|
||||||
self.op1(
|
self.op1(
|
||||||
json_data=dict(
|
json_data=dict(
|
||||||
headers_detailed=headers_detailed,
|
headers_detailed=headers_detailed,
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
elif content_length > 512 * 1024:
|
elif content_length2 > 512 * 1024:
|
||||||
content_length2 = content_length
|
content_length = min(2 * 1024 * 1024, content_length2)
|
||||||
content_length = min(2 * 1024 * 1024, content_length)
|
|
||||||
|
|
||||||
headers_detailed['headers']['Content-Length'] = '%d' % content_length
|
#headers_detailed['headers']['Content-Length'] = '%d' % content_length
|
||||||
content_range_header = headers_detailed['headers'].get('Content-Range')
|
content_range_header = headers_detailed['headers'].get('Content-Range')
|
||||||
if not content_range_header is None:
|
if not content_range_header is None and False:
|
||||||
parsed_range = re.compile(r'(\w+) (\d+)-(\d+)\/(\d+)').match(
|
parsed_range = re.compile(r'(\w+) (\d+)-(\d+)\/(\d+)').match(
|
||||||
content_range_header
|
content_range_header
|
||||||
)
|
)
|
||||||
assert parsed_range[1] == 'bytes'
|
assert parsed_range[1] == 'bytes'
|
||||||
start = int(parsed_range[2])
|
start = int(parsed_range[2])
|
||||||
total = int(parsed_range[3])
|
total = int(parsed_range[3])
|
||||||
else:
|
|
||||||
total = content_length2
|
|
||||||
start = 0
|
|
||||||
end = start + content_length
|
end = start + content_length
|
||||||
|
|
||||||
|
headers_detailed['headers']['Content-Length'] = \
|
||||||
|
'%d' % content_length
|
||||||
headers_detailed['first_line'] = 'HTTP/1.1 206 Partial Content'
|
headers_detailed['first_line'] = 'HTTP/1.1 206 Partial Content'
|
||||||
headers_detailed['headers']['Status'] = '206 Partial Content'
|
headers_detailed['headers']['Status'] = '206 Partial Content'
|
||||||
headers_detailed['headers']['Content-Range'] = \
|
headers_detailed['headers']['Content-Range'] = \
|
||||||
@ -316,6 +323,9 @@ class Application:
|
|||||||
start, end - 1, total,
|
start, end - 1, total,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
if content_length is None:
|
||||||
|
content_length = content_length2
|
||||||
|
|
||||||
headers_detailed['headers']['Connection'] = 'close'
|
headers_detailed['headers']['Connection'] = 'close'
|
||||||
|
|
||||||
output_stream.sendall(
|
output_stream.sendall(
|
||||||
@ -419,12 +429,18 @@ class Application:
|
|||||||
|
|
||||||
try:
|
try:
|
||||||
stderr_poll = select.poll()
|
stderr_poll = select.poll()
|
||||||
stderr_poll.register(p.stderr, select.POLLIN)
|
stderr_poll.register(
|
||||||
|
p.stderr,
|
||||||
|
select.POLLIN & select.POLLPRI
|
||||||
|
)
|
||||||
stdout_length = 0
|
stdout_length = 0
|
||||||
while True:
|
while True:
|
||||||
returncode = p.poll()
|
returncode = p.poll()
|
||||||
|
|
||||||
if len(stderr_poll.poll(1)) > 0:
|
poll_result = stderr_poll.poll(100)
|
||||||
|
|
||||||
|
self.op1(json_data=[pprint.pformat(poll_result)])
|
||||||
|
if len(poll_result) > 0:
|
||||||
stderr = p.stderr.readline(1024).decode('utf-8')
|
stderr = p.stderr.readline(1024).decode('utf-8')
|
||||||
stderr_lines.append(stderr)
|
stderr_lines.append(stderr)
|
||||||
else:
|
else:
|
||||||
@ -530,7 +546,7 @@ class Application:
|
|||||||
''.join([
|
''.join([
|
||||||
'HTTP/1.1 200\r\n',
|
'HTTP/1.1 200\r\n',
|
||||||
'Status: 200\r\n',
|
'Status: 200\r\n',
|
||||||
'Connection-Length: %d\r\n' % len(content),
|
'Connection-Length: %d\r\n' % (len(content) + 10),
|
||||||
'Connection: close\r\n',
|
'Connection: close\r\n',
|
||||||
'\r\n',
|
'\r\n',
|
||||||
]).encode('latin-1')
|
]).encode('latin-1')
|
||||||
|
Loading…
Reference in New Issue
Block a user