From 1612ff972679cb2ab0f89a831156b23473035f10 Mon Sep 17 00:00:00 2001 From: David Calavera Date: Wed, 22 Jul 2015 12:07:18 -0700 Subject: [PATCH] Remove duplicated code parsing parameters for the archiving handlers. Signed-off-by: David Calavera --- api/server/form.go | 27 ++++++++++++++++++++++ api/server/server.go | 55 ++++++++------------------------------------ 2 files changed, 36 insertions(+), 46 deletions(-) diff --git a/api/server/form.go b/api/server/form.go index 230d875366..6a8387a86b 100644 --- a/api/server/form.go +++ b/api/server/form.go @@ -1,6 +1,7 @@ package server import ( + "fmt" "net/http" "strconv" "strings" @@ -27,3 +28,29 @@ func int64ValueOrZero(r *http.Request, k string) int64 { } return val } + +type archiveOptions struct { + name string + path string +} + +func archiveFormValues(r *http.Request, vars map[string]string) (archiveOptions, error) { + if vars == nil { + return archiveOptions{}, fmt.Errorf("Missing parameter") + } + if err := parseForm(r); err != nil { + return archiveOptions{}, err + } + + name := vars["name"] + path := r.Form.Get("path") + + switch { + case name == "": + return archiveOptions{}, fmt.Errorf("bad parameter: 'name' cannot be empty") + case path == "": + return archiveOptions{}, fmt.Errorf("bad parameter: 'path' cannot be empty") + } + + return archiveOptions{name, path}, nil +} diff --git a/api/server/server.go b/api/server/server.go index 35ce4ebfd6..5f3a9373f2 100644 --- a/api/server/server.go +++ b/api/server/server.go @@ -1365,24 +1365,12 @@ func setContainerPathStatHeader(stat *types.ContainerPathStat, header http.Heade } func (s *Server) headContainersArchive(version version.Version, w http.ResponseWriter, r *http.Request, vars map[string]string) error { - if vars == nil { - return fmt.Errorf("Missing parameter") - } - if err := parseForm(r); err != nil { + v, err := archiveFormValues(r, vars) + if err != nil { return err } - name := vars["name"] - path := r.Form.Get("path") - - switch { - case name == "": - return fmt.Errorf("bad parameter: 'name' cannot be empty") - case path == "": - return fmt.Errorf("bad parameter: 'path' cannot be empty") - } - - stat, err := s.daemon.ContainerStatPath(name, path) + stat, err := s.daemon.ContainerStatPath(v.name, v.path) if err != nil { return err } @@ -1391,24 +1379,12 @@ func (s *Server) headContainersArchive(version version.Version, w http.ResponseW } func (s *Server) getContainersArchive(version version.Version, w http.ResponseWriter, r *http.Request, vars map[string]string) error { - if vars == nil { - return fmt.Errorf("Missing parameter") - } - if err := parseForm(r); err != nil { + v, err := archiveFormValues(r, vars) + if err != nil { return err } - name := vars["name"] - path := r.Form.Get("path") - - switch { - case name == "": - return fmt.Errorf("bad parameter: 'name' cannot be empty") - case path == "": - return fmt.Errorf("bad parameter: 'path' cannot be empty") - } - - tarArchive, stat, err := s.daemon.ContainerArchivePath(name, path) + tarArchive, stat, err := s.daemon.ContainerArchivePath(v.name, v.path) if err != nil { return err } @@ -1425,26 +1401,13 @@ func (s *Server) getContainersArchive(version version.Version, w http.ResponseWr } func (s *Server) putContainersArchive(version version.Version, w http.ResponseWriter, r *http.Request, vars map[string]string) error { - if vars == nil { - return fmt.Errorf("Missing parameter") - } - if err := parseForm(r); err != nil { + v, err := archiveFormValues(r, vars) + if err != nil { return err } - name := vars["name"] - path := r.Form.Get("path") - noOverwriteDirNonDir := boolValue(r, "noOverwriteDirNonDir") - - switch { - case name == "": - return fmt.Errorf("bad parameter: 'name' cannot be empty") - case path == "": - return fmt.Errorf("bad parameter: 'path' cannot be empty") - } - - return s.daemon.ContainerExtractToDir(name, path, noOverwriteDirNonDir, r.Body) + return s.daemon.ContainerExtractToDir(v.name, v.path, noOverwriteDirNonDir, r.Body) } func (s *Server) postContainerExecCreate(version version.Version, w http.ResponseWriter, r *http.Request, vars map[string]string) error {