mirror of
https://github.com/LostRuins/koboldcpp.git
synced 2025-09-10 17:14:36 +00:00
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:
parent
d8ebdde6ee
commit
ac6a0cde91
1 changed files with 33 additions and 0 deletions
33
koboldcpp.py
33
koboldcpp.py
|
@ -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
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue