Ejemplo n.º 1
0
//GetRepositoryImagesV1Handler will return images json data.
func GetRepositoryImagesV1Handler(ctx *macaron.Context) (int, []byte) {
	var username string
	var err error

	if username, _, err = utils.DecodeBasicAuth(ctx.Req.Header.Get("Authorization")); err != nil {
		log.Errorf("[%s] decode Authorization error: %s", ctx.Req.RequestURI, err.Error())

		result, _ := json.Marshal(map[string]string{"Error": "Decode Authorization Error"})
		return http.StatusUnauthorized, result
	}

	namespace := ctx.Params(":namespace")
	repository := ctx.Params(":repository")

	r := new(models.DockerV1)
	if v1, err := r.Get(namespace, repository); err != nil {
		log.Errorf("[%s] get repository images data error: %s", ctx.Req.RequestURI, err.Error())

		result, _ := json.Marshal(map[string]string{"Error": "Get Repository Images Error"})
		return http.StatusBadRequest, result
	} else {

		//If the Docker client use "X-Docker-Token", will return a randon token value.
		if ctx.Req.Header.Get("X-Docker-Token") == "true" {
			token := fmt.Sprintf("Token signature=%v,repository=\"%v/%v\",access=%v",
				utils.MD5(username), namespace, repository, "read")

			ctx.Resp.Header().Set("X-Docker-Token", token)
			ctx.Resp.Header().Set("WWW-Authenticate", token)
		}

		ctx.Resp.Header().Set("Content-Length", fmt.Sprint(len(v1.JSON)))

		return http.StatusOK, []byte(v1.JSON)
	}

}