common.go 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  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. ports.SetKey("PublicPort")
  25. ports.Sort()
  26. for _, port := range ports.Data {
  27. if port.Get("IP") == "" {
  28. result = append(result, fmt.Sprintf("%d/%s", port.GetInt("PublicPort"), port.Get("Type")))
  29. } else {
  30. result = append(result, fmt.Sprintf("%s:%d->%d/%s", port.Get("IP"), port.GetInt("PublicPort"), port.GetInt("PrivatePort"), port.Get("Type")))
  31. }
  32. }
  33. return strings.Join(result, ", ")
  34. }
  35. func MatchesContentType(contentType, expectedType string) bool {
  36. mimetype, _, err := mime.ParseMediaType(contentType)
  37. if err != nil {
  38. utils.Errorf("Error parsing media type: %s error: %s", contentType, err.Error())
  39. }
  40. return err == nil && mimetype == expectedType
  41. }