Support chunked encoding. (#1226)

* Support chunked encoding.

The koboldcpp API does not support HTTP chunked encoding. Some HTTP
libraries, notable Go's net/http can automatically choose to use chunked
encoding. This adds support for chunked encoding within the do_POST()
handler.

* refactor slightly to add additional safety checks and follow original format

---------

Co-authored-by: Concedo <39025047+LostRuins@users.noreply.github.com>
This commit is contained in:
mkarr 2024-11-21 04:24:04 -06:00 committed by GitHub
parent d8ebdde6ee
commit ac6a0cde91
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -1992,6 +1992,39 @@ Enter Prompt:<br>
}}).encode())
return
body = self.rfile.read(content_length)
elif self.headers.get('transfer-encoding', '').lower()=="chunked":
content_length = 0
chunklimit = 0 # do not process more than 512 chunks, prevents bad actors
body = b''
try:
while True:
chunklimit += 1
line = self.rfile.readline().strip()
if line:
chunk_length = max(0,int(line, 16))
content_length += chunk_length
if not line or chunklimit > 512 or content_length > (1024*1024*32): #32mb payload limit
self.send_response(500)
self.end_headers(content_type='application/json')
self.wfile.write(json.dumps({"detail": {
"msg": "Payload is too big. Max payload size is 32MB.",
"type": "bad_input",
}}).encode())
return
if chunk_length != 0:
chunk = self.rfile.read(chunk_length)
body += chunk
self.rfile.readline()
if chunk_length == 0:
break
except Exception as e:
self.send_response(500)
self.end_headers(content_type='application/json')
self.wfile.write(json.dumps({"detail": {
"msg": "Failed to parse chunked request.",
"type": "bad_input",
}}).encode())
return
self.path = self.path.rstrip('/')
response_body = None