github.com/zhuohuang-hust/src-cbuild@v0.0.0-20230105071821-c7aab3e7c840/api/server/router/plugin/plugin_routes.go (about)

     1  package plugin
     2  
     3  import (
     4  	"encoding/base64"
     5  	"encoding/json"
     6  	"net/http"
     7  	"strconv"
     8  	"strings"
     9  
    10  	"github.com/docker/docker/api/server/httputils"
    11  	"github.com/docker/docker/api/types"
    12  	"golang.org/x/net/context"
    13  )
    14  
    15  func parseHeaders(headers http.Header) (map[string][]string, *types.AuthConfig) {
    16  
    17  	metaHeaders := map[string][]string{}
    18  	for k, v := range headers {
    19  		if strings.HasPrefix(k, "X-Meta-") {
    20  			metaHeaders[k] = v
    21  		}
    22  	}
    23  
    24  	// Get X-Registry-Auth
    25  	authEncoded := headers.Get("X-Registry-Auth")
    26  	authConfig := &types.AuthConfig{}
    27  	if authEncoded != "" {
    28  		authJSON := base64.NewDecoder(base64.URLEncoding, strings.NewReader(authEncoded))
    29  		if err := json.NewDecoder(authJSON).Decode(authConfig); err != nil {
    30  			authConfig = &types.AuthConfig{}
    31  		}
    32  	}
    33  
    34  	return metaHeaders, authConfig
    35  }
    36  
    37  func (pr *pluginRouter) getPrivileges(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
    38  	if err := httputils.ParseForm(r); err != nil {
    39  		return err
    40  	}
    41  
    42  	metaHeaders, authConfig := parseHeaders(r.Header)
    43  
    44  	privileges, err := pr.backend.Privileges(r.FormValue("name"), metaHeaders, authConfig)
    45  	if err != nil {
    46  		return err
    47  	}
    48  	return httputils.WriteJSON(w, http.StatusOK, privileges)
    49  }
    50  
    51  func (pr *pluginRouter) pullPlugin(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
    52  	if err := httputils.ParseForm(r); err != nil {
    53  		return err
    54  	}
    55  
    56  	var privileges types.PluginPrivileges
    57  	if err := json.NewDecoder(r.Body).Decode(&privileges); err != nil {
    58  		return err
    59  	}
    60  
    61  	metaHeaders, authConfig := parseHeaders(r.Header)
    62  
    63  	if err := pr.backend.Pull(r.FormValue("name"), metaHeaders, authConfig, privileges); err != nil {
    64  		return err
    65  	}
    66  	w.WriteHeader(http.StatusCreated)
    67  	return nil
    68  }
    69  
    70  func (pr *pluginRouter) createPlugin(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
    71  	if err := httputils.ParseForm(r); err != nil {
    72  		return err
    73  	}
    74  
    75  	options := &types.PluginCreateOptions{
    76  		RepoName: r.FormValue("name")}
    77  
    78  	if err := pr.backend.CreateFromContext(ctx, r.Body, options); err != nil {
    79  		return err
    80  	}
    81  	//TODO: send progress bar
    82  	w.WriteHeader(http.StatusNoContent)
    83  	return nil
    84  }
    85  
    86  func (pr *pluginRouter) enablePlugin(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
    87  	if err := httputils.ParseForm(r); err != nil {
    88  		return err
    89  	}
    90  
    91  	name := vars["name"]
    92  	timeout, err := strconv.Atoi(r.Form.Get("timeout"))
    93  	if err != nil {
    94  		return err
    95  	}
    96  	config := &types.PluginEnableConfig{Timeout: timeout}
    97  
    98  	return pr.backend.Enable(name, config)
    99  }
   100  
   101  func (pr *pluginRouter) disablePlugin(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
   102  	return pr.backend.Disable(vars["name"])
   103  }
   104  
   105  func (pr *pluginRouter) removePlugin(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
   106  	if err := httputils.ParseForm(r); err != nil {
   107  		return err
   108  	}
   109  
   110  	name := vars["name"]
   111  	config := &types.PluginRmConfig{
   112  		ForceRemove: httputils.BoolValue(r, "force"),
   113  	}
   114  	return pr.backend.Remove(name, config)
   115  }
   116  
   117  func (pr *pluginRouter) pushPlugin(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
   118  	if err := httputils.ParseForm(r); err != nil {
   119  		return err
   120  	}
   121  
   122  	metaHeaders, authConfig := parseHeaders(r.Header)
   123  
   124  	return pr.backend.Push(vars["name"], metaHeaders, authConfig)
   125  }
   126  
   127  func (pr *pluginRouter) setPlugin(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
   128  	var args []string
   129  	if err := json.NewDecoder(r.Body).Decode(&args); err != nil {
   130  		return err
   131  	}
   132  	if err := pr.backend.Set(vars["name"], args); err != nil {
   133  		return err
   134  	}
   135  	w.WriteHeader(http.StatusNoContent)
   136  	return nil
   137  }
   138  
   139  func (pr *pluginRouter) listPlugins(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
   140  	l, err := pr.backend.List()
   141  	if err != nil {
   142  		return err
   143  	}
   144  	return httputils.WriteJSON(w, http.StatusOK, l)
   145  }
   146  
   147  func (pr *pluginRouter) inspectPlugin(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
   148  	result, err := pr.backend.Inspect(vars["name"])
   149  	if err != nil {
   150  		return err
   151  	}
   152  	return httputils.WriteJSON(w, http.StatusOK, result)
   153  }