1
0
Fork 0
mirror of https://github.com/moby/moby.git synced 2022-11-09 12:21:53 -05:00

Remove hijack from api when not necessary

This commit is contained in:
Guillaume J. Charmes 2013-05-16 12:09:06 -07:00
parent e86fe7e5af
commit f29e5dc8a1
4 changed files with 27 additions and 48 deletions

40
api.go
View file

@ -283,23 +283,17 @@ func postImagesCreate(srv *Server, w http.ResponseWriter, r *http.Request, vars
src := r.Form.Get("fromSrc") src := r.Form.Get("fromSrc")
image := r.Form.Get("fromImage") image := r.Form.Get("fromImage")
repo := r.Form.Get("repo")
tag := r.Form.Get("tag") tag := r.Form.Get("tag")
repo := r.Form.Get("repo")
in, out, err := hijackServer(w)
if err != nil {
return err
}
defer in.Close()
fmt.Fprintf(out, "HTTP/1.1 200 OK\r\nContent-Type: application/vnd.docker.raw-stream\r\n\r\n")
if image != "" { //pull if image != "" { //pull
registry := r.Form.Get("registry") registry := r.Form.Get("registry")
if err := srv.ImagePull(image, tag, registry, out); err != nil { if err := srv.ImagePull(image, tag, registry, w); err != nil {
fmt.Fprintf(out, "Error: %s\n", err) return err
} }
} else { //import } else { //import
if err := srv.ImageImport(src, repo, tag, in, out); err != nil { if err := srv.ImageImport(src, repo, tag, r.Body, w); err != nil {
fmt.Fprintf(out, "Error: %s\n", err) return err
} }
} }
return nil return nil
@ -335,15 +329,9 @@ func postImagesInsert(srv *Server, w http.ResponseWriter, r *http.Request, vars
} }
name := vars["name"] name := vars["name"]
in, out, err := hijackServer(w) if err := srv.ImageInsert(name, url, path, w); err != nil {
if err != nil {
return err return err
} }
defer in.Close()
fmt.Fprintf(out, "HTTP/1.1 200 OK\r\nContent-Type: application/vnd.docker.raw-stream\r\n\r\n")
if err := srv.ImageInsert(name, url, path, out); err != nil {
fmt.Fprintf(out, "Error: %s\n", err)
}
return nil return nil
} }
@ -358,28 +346,16 @@ func postImagesPush(srv *Server, w http.ResponseWriter, r *http.Request, vars ma
} }
name := vars["name"] name := vars["name"]
in, out, err := hijackServer(w) if err := srv.ImagePush(name, registry, w); err != nil {
if err != nil {
return err return err
} }
defer in.Close()
fmt.Fprintf(out, "HTTP/1.1 200 OK\r\nContent-Type: application/vnd.docker.raw-stream\r\n\r\n")
if err := srv.ImagePush(name, registry, out); err != nil {
fmt.Fprintf(out, "Error: %s\n", err)
}
return nil return nil
} }
func postBuild(srv *Server, w http.ResponseWriter, r *http.Request, vars map[string]string) error { func postBuild(srv *Server, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
in, out, err := hijackServer(w) if err := srv.ImageCreateFromFile(r.Body, w); err != nil {
if err != nil {
return err return err
} }
defer in.Close()
fmt.Fprintf(out, "HTTP/1.1 200 OK\r\nContent-Type: application/vnd.docker.raw-stream\r\n\r\n")
if err := srv.ImageCreateFromFile(in, out); err != nil {
fmt.Fprintf(out, "Error: %s\n", err)
}
return nil return nil
} }

View file

@ -104,7 +104,7 @@ func (cli *DockerCli) CmdInsert(args ...string) error {
v.Set("url", cmd.Arg(1)) v.Set("url", cmd.Arg(1))
v.Set("path", cmd.Arg(2)) v.Set("path", cmd.Arg(2))
err := cli.hijack("POST", "/images/"+cmd.Arg(0)+"?"+v.Encode(), false) err := cli.stream("POST", "/images/"+cmd.Arg(0)+"?"+v.Encode(), nil, os.Stdout)
if err != nil { if err != nil {
return err return err
} }
@ -117,7 +117,7 @@ func (cli *DockerCli) CmdBuild(args ...string) error {
return nil return nil
} }
err := cli.hijack("POST", "/build", false) err := cli.stream("POST", "/build", nil, os.Stdout)
if err != nil { if err != nil {
return err return err
} }
@ -571,7 +571,7 @@ func (cli *DockerCli) CmdImport(args ...string) error {
v.Set("tag", tag) v.Set("tag", tag)
v.Set("fromSrc", src) v.Set("fromSrc", src)
err := cli.hijack("POST", "/images/create?"+v.Encode(), false) err := cli.stream("POST", "/images/create?"+v.Encode(), os.Stdin, os.Stdout)
if err != nil { if err != nil {
return err return err
} }
@ -628,7 +628,7 @@ func (cli *DockerCli) CmdPush(args ...string) error {
v := url.Values{} v := url.Values{}
v.Set("registry", *registry) v.Set("registry", *registry)
if err := cli.hijack("POST", "/images/"+name+"/push?"+v.Encode(), false); err != nil { if err := cli.stream("POST", "/images/"+name+"/push?"+v.Encode(), nil, os.Stdout); err != nil {
return err return err
} }
return nil return nil
@ -659,7 +659,7 @@ func (cli *DockerCli) CmdPull(args ...string) error {
v.Set("tag", *tag) v.Set("tag", *tag)
v.Set("registry", *registry) v.Set("registry", *registry)
if err := cli.hijack("POST", "/images/create?"+v.Encode(), false); err != nil { if err := cli.stream("POST", "/images/create?"+v.Encode(), nil, os.Stdout); err != nil {
return err return err
} }
@ -864,7 +864,7 @@ func (cli *DockerCli) CmdExport(args ...string) error {
return nil return nil
} }
if err := cli.stream("GET", "/containers/"+cmd.Arg(0)+"/export"); err != nil { if err := cli.stream("GET", "/containers/"+cmd.Arg(0)+"/export", nil, os.Stdout); err != nil {
return err return err
} }
return nil return nil
@ -1086,7 +1086,7 @@ func (cli *DockerCli) CmdRun(args ...string) error {
if statusCode == 404 { if statusCode == 404 {
v := url.Values{} v := url.Values{}
v.Set("fromImage", config.Image) v.Set("fromImage", config.Image)
err = cli.hijack("POST", "/images/create?"+v.Encode(), false) err = cli.stream("POST", "/images/create?"+v.Encode(), nil, os.Stderr)
if err != nil { if err != nil {
return err return err
} }
@ -1179,8 +1179,11 @@ func (cli *DockerCli) call(method, path string, data interface{}) ([]byte, int,
return body, resp.StatusCode, nil return body, resp.StatusCode, nil
} }
func (cli *DockerCli) stream(method, path string) error { func (cli *DockerCli) stream(method, path string, in io.Reader, out io.Writer) error {
req, err := http.NewRequest(method, fmt.Sprintf("http://%s:%d%s", cli.host, cli.port, path), nil) if (method == "POST" || method == "PUT") && in == nil {
in = bytes.NewReader([]byte{})
}
req, err := http.NewRequest(method, fmt.Sprintf("http://%s:%d%s", cli.host, cli.port, path), in)
if err != nil { if err != nil {
return err return err
} }
@ -1204,7 +1207,7 @@ func (cli *DockerCli) stream(method, path string) error {
return fmt.Errorf("error: %s", body) return fmt.Errorf("error: %s", body)
} }
if _, err := io.Copy(os.Stdout, resp.Body); err != nil { if _, err := io.Copy(out, resp.Body); err != nil {
return err return err
} }
return nil return nil

View file

@ -175,7 +175,6 @@ func (r *Registry) GetRemoteTags(registries []string, repository string, token [
} }
func (r *Registry) GetRepositoryData(remote string) (*RepositoryData, error) { func (r *Registry) GetRepositoryData(remote string) (*RepositoryData, error) {
utils.Debugf("Pulling repository %s from %s\r\n", remote, auth.IndexServerAddress())
repositoryTarget := auth.IndexServerAddress() + "/repositories/" + remote + "/images" repositoryTarget := auth.IndexServerAddress() + "/repositories/" + remote + "/images"
req, err := http.NewRequest("GET", repositoryTarget, nil) req, err := http.NewRequest("GET", repositoryTarget, nil)

View file

@ -2,6 +2,7 @@ package docker
import ( import (
"fmt" "fmt"
"github.com/dotcloud/docker/auth"
"github.com/dotcloud/docker/registry" "github.com/dotcloud/docker/registry"
"github.com/dotcloud/docker/utils" "github.com/dotcloud/docker/utils"
"io" "io"
@ -322,8 +323,8 @@ func (srv *Server) pullImage(out io.Writer, imgId, registry string, token []stri
return nil return nil
} }
func (srv *Server) pullRepository(stdout io.Writer, remote, askedTag string) error { func (srv *Server) pullRepository(out io.Writer, remote, askedTag string) error {
utils.Debugf("Retrieving repository data") fmt.Fprintf(out, "Pulling repository %s from %s\r\n", remote, auth.IndexServerAddress())
repoData, err := srv.registry.GetRepositoryData(remote) repoData, err := srv.registry.GetRepositoryData(remote)
if err != nil { if err != nil {
return err return err
@ -349,11 +350,11 @@ func (srv *Server) pullRepository(stdout io.Writer, remote, askedTag string) err
if askedTag != "" && askedTag != img.Tag { if askedTag != "" && askedTag != img.Tag {
continue continue
} }
fmt.Fprintf(stdout, "Pulling image %s (%s) from %s\n", img.Id, img.Tag, remote) fmt.Fprintf(out, "Pulling image %s (%s) from %s\n", img.Id, img.Tag, remote)
success := false success := false
for _, ep := range repoData.Endpoints { for _, ep := range repoData.Endpoints {
if err := srv.pullImage(stdout, img.Id, "https://"+ep+"/v1", repoData.Tokens); err != nil { if err := srv.pullImage(out, img.Id, "https://"+ep+"/v1", repoData.Tokens); err != nil {
fmt.Fprintf(stdout, "Error while retrieving image for tag: %s (%s); checking next endpoint\n", askedTag, err) fmt.Fprintf(out, "Error while retrieving image for tag: %s (%s); checking next endpoint\n", askedTag, err)
continue continue
} }
if err := srv.runtime.repositories.Set(remote, img.Tag, img.Id, true); err != nil { if err := srv.runtime.repositories.Set(remote, img.Tag, img.Id, true); err != nil {