summaryrefslogtreecommitdiff
path: root/api/server/httputils
diff options
context:
space:
mode:
authorVincent Demeester <vincent@sbr.pm>2017-06-23 16:26:17 +0200
committerVincent Demeester <vincent@sbr.pm>2017-06-23 19:37:26 +0200
commit565aa41392b01251dfc9398eb69c23bdd8ea64e6 (patch)
treef3bb7a3fb00a284917145983125ca336ba4c90f0 /api/server/httputils
parent4f259698b07653e9e5220e097df79862f9e54b74 (diff)
downloaddocker-565aa41392b01251dfc9398eb69c23bdd8ea64e6.tar.gz
Move some `api` package functions away
- DisplayablePorts is a `cli` function, moving to `docker/cli` - Move MatchesContentType to the only package using it, `api/server/httputils` (and remove the deps on logrus for `api` package) Signed-off-by: Vincent Demeester <vincent@sbr.pm>
Diffstat (limited to 'api/server/httputils')
-rw-r--r--api/server/httputils/httputils.go15
-rw-r--r--api/server/httputils/httputils_test.go18
2 files changed, 30 insertions, 3 deletions
diff --git a/api/server/httputils/httputils.go b/api/server/httputils/httputils.go
index 2c2e8452f9..92cb67c56c 100644
--- a/api/server/httputils/httputils.go
+++ b/api/server/httputils/httputils.go
@@ -3,12 +3,12 @@ package httputils
import (
"fmt"
"io"
+ "mime"
"net/http"
"strings"
+ "github.com/Sirupsen/logrus"
"golang.org/x/net/context"
-
- "github.com/docker/docker/api"
)
// APIVersionKey is the client's requested API version.
@@ -55,7 +55,7 @@ func CheckForJSON(r *http.Request) error {
}
// Otherwise it better be json
- if api.MatchesContentType(ct, "application/json") {
+ if matchesContentType(ct, "application/json") {
return nil
}
return fmt.Errorf("Content-Type specified (%s) must be 'application/json'", ct)
@@ -86,3 +86,12 @@ func VersionFromContext(ctx context.Context) string {
return ""
}
+
+// matchesContentType validates the content type against the expected one
+func matchesContentType(contentType, expectedType string) bool {
+ mimetype, _, err := mime.ParseMediaType(contentType)
+ if err != nil {
+ logrus.Errorf("Error parsing media type: %s error: %v", contentType, err)
+ }
+ return err == nil && mimetype == expectedType
+}
diff --git a/api/server/httputils/httputils_test.go b/api/server/httputils/httputils_test.go
new file mode 100644
index 0000000000..d551b9d98a
--- /dev/null
+++ b/api/server/httputils/httputils_test.go
@@ -0,0 +1,18 @@
+package httputils
+
+import "testing"
+
+// matchesContentType
+func TestJsonContentType(t *testing.T) {
+ if !matchesContentType("application/json", "application/json") {
+ t.Fail()
+ }
+
+ if !matchesContentType("application/json; charset=utf-8", "application/json") {
+ t.Fail()
+ }
+
+ if matchesContentType("dockerapplication/json", "application/json") {
+ t.Fail()
+ }
+}