High-performance browser automation bridge and multi-instance orchestrator with advanced stealth injection and real-time dashboard.
fix(orchestrator): decode chunked instance requests (PIN-306)
Luigi Agostifcca5bae69cf
diff --git a/internal/orchestrator/handlers_instances.go b/internal/orchestrator/handlers_instances.goindex 9fde85ca..6ebcfd57 100644--- a/internal/orchestrator/handlers_instances.go+++ b/internal/orchestrator/handlers_instances.go@@ -5,6 +5,7 @@ import ("encoding/json""errors""fmt"+ "io""net/http""net/url""strings"@@ -51,8 +52,8 @@ func (o *Orchestrator) handleLaunchByName(w http.ResponseWriter, r *http.RequestName string `json:"name,omitempty"`}- if r.ContentLength > 0 {- if err := httpx.DecodeJSONBody(w, r, 0, &req); err != nil {+ if r.Body != nil {+ if err := httpx.DecodeJSONBody(w, r, 0, &req); err != nil && !errors.Is(err, io.EOF) {httpx.Error(w, httpx.StatusForJSONDecodeError(err), err)return}@@ -228,8 +229,8 @@ func (o *Orchestrator) handleLogsStreamByID(w http.ResponseWriter, r *http.Requefunc (o *Orchestrator) handleStartInstance(w http.ResponseWriter, r *http.Request) {var req startInstanceRequest- if r.ContentLength > 0 {- if err := httpx.DecodeJSONBody(w, r, 0, &req); err != nil {+ if r.Body != nil {+ if err := httpx.DecodeJSONBody(w, r, 0, &req); err != nil && !errors.Is(err, io.EOF) {httpx.Error(w, httpx.StatusForJSONDecodeError(err), err)return}diff --git a/internal/orchestrator/handlers_instances_test.go b/internal/orchestrator/handlers_instances_test.goindex 85d03981..585a4597 100644--- a/internal/orchestrator/handlers_instances_test.go+++ b/internal/orchestrator/handlers_instances_test.go@@ -34,30 +34,36 @@ func TestHandleLaunchByNameRejectsNameField(t *testing.T) {func TestInstanceStartEndpointsRejectUnknownProfileKeysWithoutLaunching(t *testing.T) {for _, path := range []string{"/instances/start", "/instances/launch"} {for _, key := range []string{"profile", "profileName"} {- t.Run(path+"/"+key, func(t *testing.T) {- runner := &mockRunner{portAvail: true}- o := NewOrchestratorWithRunner(t.TempDir(), runner)- req := httptest.NewRequest(http.MethodPost, path, strings.NewReader(fmt.Sprintf(`{"%s":"work"}`, key)))- w := httptest.NewRecorder()-- if path == "/instances/start" {- o.handleStartInstance(w, req)- } else {- o.handleLaunchByName(w, req)- }-- if w.Code != http.StatusBadRequest {- t.Fatalf("status = %d, want 400: %s", w.Code, w.Body.String())- }- for _, want := range []string{`unknown field \"` + key + `\"`, "profileId"} {- if !strings.Contains(w.Body.String(), want) {- t.Fatalf("body %q does not contain %q", w.Body.String(), want)+ for _, chunked := range []bool{false, true} {+ t.Run(fmt.Sprintf("%s/%s/chunked=%t", path, key, chunked), func(t *testing.T) {+ runner := &mockRunner{portAvail: true}+ o := NewOrchestratorWithRunner(t.TempDir(), runner)+ req := httptest.NewRequest(http.MethodPost, path, strings.NewReader(fmt.Sprintf(`{"%s":"work"}`, key)))+ if chunked {+ req.ContentLength = -1+ req.TransferEncoding = []string{"chunked"}}- }- if runner.runCalled || len(o.List()) != 0 {- t.Fatal("an invalid profile key started an instance")- }- })+ w := httptest.NewRecorder()++ if path == "/instances/start" {+ o.handleStartInstance(w, req)+ } else {+ o.handleLaunchByName(w, req)+ }++ if w.Code != http.StatusBadRequest {+ t.Fatalf("status = %d, want 400: %s", w.Code, w.Body.String())+ }+ for _, want := range []string{`unknown field \"` + key + `\"`, "profileId"} {+ if !strings.Contains(w.Body.String(), want) {+ t.Fatalf("body %q does not contain %q", w.Body.String(), want)+ }+ }+ if runner.runCalled || len(o.List()) != 0 {+ t.Fatal("an invalid profile key started an instance")+ }+ })+ }}}}