Qwen3.8-Flash-Next on any consumer hardware: one-click install for Windows / Linux. Strata inference engine, OpenAI/Anthropic API on localhost, optional image input.
C++
serve: read the /load and /unload body before replying (#630)
Davidb6d60f01b8c5
diff --git a/serve/server.py b/serve/server.pyindex 46e3fc369..fb1dbbcd2 100644--- a/serve/server.py+++ b/serve/server.py@@ -2226,6 +2226,8 @@ def make_handler(svc: Service):if path == "/settings":self._settings()return+ if path in ("/unload", "/load") and not self._control_body():+ return# JSON from Strata's own page only, as /settings: else a plain form POST from any site unloads the modelif path in ("/unload", "/load") and not self._own_page("the model can be loaded or unloaded"):return@@ -2335,6 +2337,28 @@ def make_handler(svc: Service):props["build_info"] = "Strata " + str(version)self._json(200, props)+ def _control_body(self) -> bool:+ """Consume the unused control body before replying/closing (Windows otherwise sends a TCP reset)."""+ try:+ length = int(self.headers.get("Content-Length", "0"))+ except ValueError:+ self._json(400, {"error": {"message": "invalid Content-Length"}})+ return False+ if not 0 <= length <= 65536:+ self._json(413, {"error": {"message": "control request body is limited to 64 KiB"}})+ return False+ timeout = self.connection.gettimeout()+ try:+ self.connection.settimeout(2.0)+ complete = len(self.rfile.read(length)) == length+ except OSError:+ complete = False+ finally:+ self.connection.settimeout(timeout)+ if not complete:+ self._json(400, {"error": {"message": "incomplete control request body"}})+ return complete+def _own_page(self, what) -> bool:"""Only JSON (a form or a "simple" cross-site request can't send it without a CORS preflight, which thisserver never grants) and no foreign Origin: a web page elsewhere must not change settings or run tools."""diff --git a/serve/test_security.py b/serve/test_security.pyindex cebd756d1..429c71621 100644--- a/serve/test_security.py+++ b/serve/test_security.py@@ -238,6 +238,31 @@ class OverHttp(unittest.TestCase):{**headers, "Authorization": "Bearer s3cret"})[0], 200)# --- /unload and /load+ def test_control_body_arrives_before_the_operation_and_reply(self):+ self.start()+ for body, expected in ((b"{}", 200), (b"{", 400)):+ c = http.client.HTTPConnection("127.0.0.1", self.port, timeout=5)+ try:+ with mock.patch.object(self.svc, "unload", return_value="unloaded") as unload:+ c.putrequest("POST", "/unload")+ c.putheader("Content-Type", "application/json")+ c.putheader("Content-Length", "2")+ c.endheaders()+ c.sock.settimeout(0.1)+ with self.assertRaises(socket.timeout):+ c.sock.recv(1)+ unload.assert_not_called()+ c.sock.settimeout(5)+ c.send(body)+ if len(body) < 2:+ c.sock.shutdown(socket.SHUT_WR)+ r = c.getresponse()+ self.assertEqual(r.status, expected)+ r.read()+ self.assertEqual(unload.call_count, int(expected == 200))+ finally:+ c.close()+def test_unload_and_load_need_json_from_the_own_page(self):self.start()form = {"Content-Type": "application/x-www-form-urlencoded"}