common.go 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. package api
  2. import (
  3. "fmt"
  4. "mime"
  5. "strings"
  6. "github.com/docker/docker/engine"
  7. "github.com/docker/docker/pkg/parsers"
  8. "github.com/docker/docker/pkg/version"
  9. "github.com/docker/docker/utils"
  10. )
  11. const (
  12. APIVERSION version.Version = "1.14"
  13. DEFAULTHTTPHOST = "127.0.0.1"
  14. DEFAULTUNIXSOCKET = "/var/run/docker.sock"
  15. )
  16. func ValidateHost(val string) (string, error) {
  17. host, err := parsers.ParseHost(DEFAULTHTTPHOST, DEFAULTUNIXSOCKET, val)
  18. if err != nil {
  19. return val, err
  20. }
  21. return host, nil
  22. }
  23. //TODO remove, used on < 1.5 in getContainersJSON
  24. func DisplayablePorts(ports *engine.Table) string {
  25. result := []string{}
  26. ports.SetKey("PublicPort")
  27. ports.Sort()
  28. for _, port := range ports.Data {
  29. if port.Get("IP") == "" {
  30. result = append(result, fmt.Sprintf("%d/%s", port.GetInt("PrivatePort"), port.Get("Type")))
  31. } else {
  32. result = append(result, fmt.Sprintf("%s:%d->%d/%s", port.Get("IP"), port.GetInt("PublicPort"), port.GetInt("PrivatePort"), port.Get("Type")))
  33. }
  34. }
  35. return strings.Join(result, ", ")
  36. }
  37. func MatchesContentType(contentType, expectedType string) bool {
  38. mimetype, _, err := mime.ParseMediaType(contentType)
  39. if err != nil {
  40. utils.Errorf("Error parsing media type: %s error: %s", contentType, err.Error())
  41. }
  42. return err == nil && mimetype == expectedType
  43. }