Skip to content

Commit ac6a0cd

Browse files
mkarrLostRuins
andauthored
Support chunked encoding. (LostRuins#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 <[email protected]>
1 parent d8ebdde commit ac6a0cd

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed

koboldcpp.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1992,6 +1992,39 @@ def do_POST(self):
19921992
}}).encode())
19931993
return
19941994
body = self.rfile.read(content_length)
1995+
elif self.headers.get('transfer-encoding', '').lower()=="chunked":
1996+
content_length = 0
1997+
chunklimit = 0 # do not process more than 512 chunks, prevents bad actors
1998+
body = b''
1999+
try:
2000+
while True:
2001+
chunklimit += 1
2002+
line = self.rfile.readline().strip()
2003+
if line:
2004+
chunk_length = max(0,int(line, 16))
2005+
content_length += chunk_length
2006+
if not line or chunklimit > 512 or content_length > (1024*1024*32): #32mb payload limit
2007+
self.send_response(500)
2008+
self.end_headers(content_type='application/json')
2009+
self.wfile.write(json.dumps({"detail": {
2010+
"msg": "Payload is too big. Max payload size is 32MB.",
2011+
"type": "bad_input",
2012+
}}).encode())
2013+
return
2014+
if chunk_length != 0:
2015+
chunk = self.rfile.read(chunk_length)
2016+
body += chunk
2017+
self.rfile.readline()
2018+
if chunk_length == 0:
2019+
break
2020+
except Exception as e:
2021+
self.send_response(500)
2022+
self.end_headers(content_type='application/json')
2023+
self.wfile.write(json.dumps({"detail": {
2024+
"msg": "Failed to parse chunked request.",
2025+
"type": "bad_input",
2026+
}}).encode())
2027+
return
19952028

19962029
self.path = self.path.rstrip('/')
19972030
response_body = None

0 commit comments

Comments
 (0)