diff --git a/koboldcpp.py b/koboldcpp.py index 51656a933..8b6fa790d 100644 --- a/koboldcpp.py +++ b/koboldcpp.py @@ -1992,6 +1992,39 @@ Enter Prompt:
}}).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