diff --git a/pkg/deviceshifu/deviceshifuhttp/deviceshifuhttp.go b/pkg/deviceshifu/deviceshifuhttp/deviceshifuhttp.go index 20b26f866..84721a38c 100644 --- a/pkg/deviceshifu/deviceshifuhttp/deviceshifuhttp.go +++ b/pkg/deviceshifu/deviceshifuhttp/deviceshifuhttp.go @@ -181,7 +181,7 @@ func (handler DeviceCommandHandlerHTTP) commandHandleFunc() http.HandlerFunc { } switch reqType { - case http.MethodPost: + case http.MethodPost, http.MethodPut: requestBody, parseErr = io.ReadAll(r.Body) if parseErr != nil { http.Error(w, "Error on parsing body", http.StatusBadRequest) @@ -222,6 +222,7 @@ func (handler DeviceCommandHandlerHTTP) commandHandleFunc() http.HandlerFunc { } if resp != nil { + defer resp.Body.Close() // Handling deviceshifu stuck when responseBody is a stream instructionFuncName, shouldUsePythonCustomProcessing := deviceshifubase.CustomInstructionsPython[handlerInstruction] if !shouldUsePythonCustomProcessing { @@ -385,6 +386,7 @@ func (handler DeviceCommandHandlerHTTPCommandline) commandHandleFunc() http.Hand } if resp != nil { + defer resp.Body.Close() utils.CopyHeader(w.Header(), resp.Header) w.WriteHeader(resp.StatusCode) @@ -463,6 +465,7 @@ func (ds *DeviceShifuHTTP) collectHTTPTelemtries() (bool, error) { } if resp != nil { + defer resp.Body.Close() if resp.StatusCode >= http.StatusOK && resp.StatusCode < http.StatusMultipleChoices { instructionFuncName, pythonCustomExist := deviceshifubase.CustomInstructionsPython[instruction] if pythonCustomExist { diff --git a/pkg/deviceshifu/deviceshifuhttp/deviceshifuhttp_test.go b/pkg/deviceshifu/deviceshifuhttp/deviceshifuhttp_test.go index 0b747706b..97f514f32 100644 --- a/pkg/deviceshifu/deviceshifuhttp/deviceshifuhttp_test.go +++ b/pkg/deviceshifu/deviceshifuhttp/deviceshifuhttp_test.go @@ -251,6 +251,10 @@ func TestCommandHandleHTTPFunc(t *testing.T) { r = dc.Post().Do(context.TODO()) assert.Nil(t, r.Error()) + // test put method + r = dc.Put().Do(context.TODO()) + assert.Nil(t, r.Error()) + // test not supported http method case r = dc.Delete().Do(context.TODO()) assert.Equal(t, "the server rejected our request for an unknown reason", r.Error().Error())