Skip to content

Commit

Permalink
return 304 is status isn't modified in start and stop
Browse files Browse the repository at this point in the history
Docker-DCO-1.1-Signed-off-by: Victor Vieux <vieux@docker.com> (github: vieux)
vieux committed Jun 24, 2014
1 parent e23e3a1 commit 53b0360
Showing 4 changed files with 30 additions and 3 deletions.
15 changes: 13 additions & 2 deletions api/server/server.go
Original file line number Diff line number Diff line change
@@ -693,8 +693,11 @@ func postContainersStart(eng *engine.Engine, version version.Version, w http.Res
if vars == nil {
return fmt.Errorf("Missing parameter")
}
name := vars["name"]
job := eng.Job("start", name)
var (
name = vars["name"]
job = eng.Job("start", name)
)

// allow a nil body for backwards compatibility
if r.Body != nil {
if api.MatchesContentType(r.Header.Get("Content-Type"), "application/json") {
@@ -704,6 +707,10 @@ func postContainersStart(eng *engine.Engine, version version.Version, w http.Res
}
}
if err := job.Run(); err != nil {
if err.Error() == "Container already started" {
w.WriteHeader(http.StatusNotModified)
return nil
}
return err
}
w.WriteHeader(http.StatusNoContent)
@@ -720,6 +727,10 @@ func postContainersStop(eng *engine.Engine, version version.Version, w http.Resp
job := eng.Job("stop", vars["name"])
job.Setenv("t", r.Form.Get("t"))
if err := job.Run(); err != nil {
if err.Error() == "Container already stopped" {
w.WriteHeader(http.StatusNotModified)
return nil
}
return err
}
w.WriteHeader(http.StatusNoContent)
8 changes: 7 additions & 1 deletion docs/sources/reference/api/docker_remote_api.md
Original file line number Diff line number Diff line change
@@ -21,7 +21,7 @@ page_keywords: API, Docker, rcli, REST, documentation
The current version of the API is v1.13

Calling `/images/<name>/insert` is the same as calling
`/v1.12/images/<name>/insert`.
`/v1.13/images/<name>/insert`.

You can still call an old version of the API using
`/v1.12/images/<name>/insert`.
@@ -38,6 +38,12 @@ You can still call an old version of the API using
`Sockets` parameter added to the `/info` endpoint listing all the sockets the
daemon is configured to listen on.

`POST /containers/(name)/start`
`POST /containers/(name)/stop`

**New!**
`start` and `stop` will now return 304 if the container's status is not modified

## v1.12

### Full Documentation
2 changes: 2 additions & 0 deletions docs/sources/reference/api/docker_remote_api_v1.13.md
Original file line number Diff line number Diff line change
@@ -429,6 +429,7 @@ Start the container `id`
Status Codes:

- **204** – no error
- **304** – container already started
- **404** – no such container
- **500** – server error

@@ -455,6 +456,7 @@ Stop the container `id`
Status Codes:

- **204** – no error
- **304** – container already stopped
- **404** – no such container
- **500** – server error

8 changes: 8 additions & 0 deletions server/server.go
Original file line number Diff line number Diff line change
@@ -2046,6 +2046,11 @@ func (srv *Server) ContainerStart(job *engine.Job) engine.Status {
if container == nil {
return job.Errorf("No such container: %s", name)
}

if container.State.IsRunning() {
return job.Errorf("Container already started")
}

// If no environment was set, then no hostconfig was passed.
if len(job.Environ()) > 0 {
hostConfig := runconfig.ContainerHostConfigFromJob(job)
@@ -2099,6 +2104,9 @@ func (srv *Server) ContainerStop(job *engine.Job) engine.Status {
t = job.GetenvInt("t")
}
if container := srv.daemon.Get(name); container != nil {
if !container.State.IsRunning() {
return job.Errorf("Container already stopped")
}
if err := container.Stop(int(t)); err != nil {
return job.Errorf("Cannot stop container %s: %s\n", name, err)
}

0 comments on commit 53b0360

Please sign in to comment.