Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[24.0 backport] api/server: allow empty body for POST /commit again #45568

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 4 additions & 1 deletion api/server/router/container/container_routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func (s *containerRouter) postCommit(ctx context.Context, w http.ResponseWriter,
}

config, _, _, err := s.decoder.DecodeConfig(r.Body)
if err != nil && err != io.EOF { // Do not fail if body is empty.
if err != nil && !errors.Is(err, io.EOF) { // Do not fail if body is empty.
return err
}

Expand Down Expand Up @@ -486,6 +486,9 @@ func (s *containerRouter) postContainersCreate(ctx context.Context, w http.Respo

config, hostConfig, networkingConfig, err := s.decoder.DecodeConfig(r.Body)
if err != nil {
if errors.Is(err, io.EOF) {
return errdefs.InvalidParameter(errors.New("invalid JSON: got EOF while reading request body"))
}
return err
}
version := httputils.VersionFromContext(ctx)
Expand Down
5 changes: 1 addition & 4 deletions runconfig/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,10 +77,7 @@ func decodeContainerConfig(src io.Reader, si *sysinfo.SysInfo) (*container.Confi
func loadJSON(src io.Reader, out interface{}) error {
dec := json.NewDecoder(src)
if err := dec.Decode(&out); err != nil {
if err == io.EOF {
return validationError("invalid JSON: got EOF while reading request body")
}
return validationError("invalid JSON: " + err.Error())
return invalidJSONError{Err: err}
}
if dec.More() {
return validationError("unexpected content after JSON")
Expand Down
14 changes: 14 additions & 0 deletions runconfig/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,17 @@ func (e validationError) Error() string {
}

func (e validationError) InvalidParameter() {}

type invalidJSONError struct {
Err error
}

func (e invalidJSONError) Error() string {
return "invalid JSON: " + e.Err.Error()
}

func (e invalidJSONError) Unwrap() error {
return e.Err
}

func (e invalidJSONError) InvalidParameter() {}