common.go 1.1 KB

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