1
0
Fork 0
mirror of https://github.com/moby/moby.git synced 2022-11-09 12:21:53 -05:00

Move httputils/mimtype to the single consumser, and remove unused function.

Signed-off-by: Daniel Nephin <dnephin@docker.com>
This commit is contained in:
Daniel Nephin 2017-06-01 15:46:27 -04:00
parent aebcdf21b1
commit c91521be68
8 changed files with 25 additions and 106 deletions

View file

@ -14,7 +14,6 @@ import (
"github.com/docker/docker/builder/dockerfile/parser" "github.com/docker/docker/builder/dockerfile/parser"
"github.com/docker/docker/builder/dockerignore" "github.com/docker/docker/builder/dockerignore"
"github.com/docker/docker/pkg/fileutils" "github.com/docker/docker/pkg/fileutils"
"github.com/docker/docker/pkg/httputils"
"github.com/docker/docker/pkg/symlink" "github.com/docker/docker/pkg/symlink"
"github.com/docker/docker/pkg/urlutil" "github.com/docker/docker/pkg/urlutil"
"github.com/pkg/errors" "github.com/pkg/errors"
@ -93,7 +92,7 @@ func newURLRemote(url string, dockerfilePath string, progressReader func(in io.R
var dockerfile io.ReadCloser var dockerfile io.ReadCloser
dockerfileFoundErr := errors.New("found-dockerfile") dockerfileFoundErr := errors.New("found-dockerfile")
c, err := MakeRemoteContext(url, map[string]func(io.ReadCloser) (io.ReadCloser, error){ c, err := MakeRemoteContext(url, map[string]func(io.ReadCloser) (io.ReadCloser, error){
httputils.MimeTypes.TextPlain: func(rc io.ReadCloser) (io.ReadCloser, error) { mimeTypes.TextPlain: func(rc io.ReadCloser) (io.ReadCloser, error) {
dockerfile = rc dockerfile = rc
return nil, dockerfileFoundErr return nil, dockerfileFoundErr
}, },

View file

@ -1,29 +1,27 @@
package httputils package remotecontext
import ( import (
"mime" "mime"
"net/http" "net/http"
) )
// MimeTypes stores the MIME content type. // mimeTypes stores the MIME content type.
var MimeTypes = struct { var mimeTypes = struct {
TextPlain string TextPlain string
OctetStream string OctetStream string
}{"text/plain", "application/octet-stream"} }{"text/plain", "application/octet-stream"}
// DetectContentType returns a best guess representation of the MIME // detectContentType returns a best guess representation of the MIME
// content type for the bytes at c. The value detected by // content type for the bytes at c. The value detected by
// http.DetectContentType is guaranteed not be nil, defaulting to // http.DetectContentType is guaranteed not be nil, defaulting to
// application/octet-stream when a better guess cannot be made. The // application/octet-stream when a better guess cannot be made. The
// result of this detection is then run through mime.ParseMediaType() // result of this detection is then run through mime.ParseMediaType()
// which separates the actual MIME string from any parameters. // which separates the actual MIME string from any parameters.
func DetectContentType(c []byte) (string, map[string]string, error) { func detectContentType(c []byte) (string, map[string]string, error) {
ct := http.DetectContentType(c) ct := http.DetectContentType(c)
contentType, args, err := mime.ParseMediaType(ct) contentType, args, err := mime.ParseMediaType(ct)
if err != nil { if err != nil {
return "", nil, err return "", nil, err
} }
return contentType, args, nil return contentType, args, nil
} }

View file

@ -0,0 +1,16 @@
package remotecontext
import (
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestDetectContentType(t *testing.T) {
input := []byte("That is just a plain text")
contentType, _, err := detectContentType(input)
require.NoError(t, err)
assert.Equal(t, "text/plain", contentType)
}

View file

@ -94,8 +94,8 @@ func inspectResponse(ct string, r io.ReadCloser, clen int64) (string, io.ReadClo
// content type for files without an extension (e.g. 'Dockerfile') // content type for files without an extension (e.g. 'Dockerfile')
// so if we receive this value we better check for text content // so if we receive this value we better check for text content
contentType := ct contentType := ct
if len(ct) == 0 || ct == httputils.MimeTypes.OctetStream { if len(ct) == 0 || ct == mimeTypes.OctetStream {
contentType, _, err = httputils.DetectContentType(preamble) contentType, _, err = detectContentType(preamble)
if err != nil { if err != nil {
return contentType, bodyReader, err return contentType, bodyReader, err
} }

View file

@ -11,7 +11,6 @@ import (
"github.com/docker/docker/builder" "github.com/docker/docker/builder"
"github.com/docker/docker/pkg/archive" "github.com/docker/docker/pkg/archive"
"github.com/docker/docker/pkg/httputils"
) )
var binaryContext = []byte{0xFD, 0x37, 0x7A, 0x58, 0x5A, 0x00} //xz magic var binaryContext = []byte{0xFD, 0x37, 0x7A, 0x58, 0x5A, 0x00} //xz magic
@ -188,7 +187,7 @@ func TestMakeRemoteContext(t *testing.T) {
mux.Handle("/", http.FileServer(http.Dir(contextDir))) mux.Handle("/", http.FileServer(http.Dir(contextDir)))
remoteContext, err := MakeRemoteContext(remoteURL, map[string]func(io.ReadCloser) (io.ReadCloser, error){ remoteContext, err := MakeRemoteContext(remoteURL, map[string]func(io.ReadCloser) (io.ReadCloser, error){
httputils.MimeTypes.TextPlain: func(rc io.ReadCloser) (io.ReadCloser, error) { mimeTypes.TextPlain: func(rc io.ReadCloser) (io.ReadCloser, error) {
dockerfile, err := ioutil.ReadAll(rc) dockerfile, err := ioutil.ReadAll(rc)
if err != nil { if err != nil {
return nil, err return nil, err

View file

@ -1,20 +1,12 @@
package httputils package httputils
import ( import (
"errors"
"fmt" "fmt"
"net/http" "net/http"
"regexp"
"strings"
"github.com/docker/docker/pkg/jsonmessage" "github.com/docker/docker/pkg/jsonmessage"
) )
var (
headerRegexp = regexp.MustCompile(`^(?:(.+)/(.+?))\((.+)\).*$`)
errInvalidHeader = errors.New("Bad header, should be in format `docker/version (platform)`")
)
// Download requests a given URL and returns an io.Reader. // Download requests a given URL and returns an io.Reader.
func Download(url string) (resp *http.Response, err error) { func Download(url string) (resp *http.Response, err error) {
if resp, err = http.Get(url); err != nil { if resp, err = http.Get(url); err != nil {
@ -33,24 +25,3 @@ func NewHTTPRequestError(msg string, res *http.Response) error {
Code: res.StatusCode, Code: res.StatusCode,
} }
} }
// ServerHeader contains the server information.
type ServerHeader struct {
App string // docker
Ver string // 1.8.0-dev
OS string // windows or linux
}
// ParseServerHeader extracts pieces from an HTTP server header
// which is in the format "docker/version (os)" e.g. docker/1.8.0-dev (windows).
func ParseServerHeader(hdr string) (*ServerHeader, error) {
matches := headerRegexp.FindStringSubmatch(hdr)
if len(matches) != 4 {
return nil, errInvalidHeader
}
return &ServerHeader{
App: strings.TrimSpace(matches[1]),
Ver: strings.TrimSpace(matches[2]),
OS: strings.TrimSpace(matches[3]),
}, nil
}

View file

@ -62,54 +62,3 @@ func TestNewHTTPRequestError(t *testing.T) {
t.Fatalf("Expected err to be %q, got %q", errorMessage, err) t.Fatalf("Expected err to be %q, got %q", errorMessage, err)
} }
} }
func TestParseServerHeader(t *testing.T) {
inputs := map[string][]string{
"bad header": {"error"},
"(bad header)": {"error"},
"(without/spaces)": {"error"},
"(header/with spaces)": {"error"},
"foo/bar (baz)": {"foo", "bar", "baz"},
"foo/bar": {"error"},
"foo": {"error"},
"foo/bar (baz space)": {"foo", "bar", "baz space"},
" f f / b b ( b s ) ": {"f f", "b b", "b s"},
"foo/bar (baz) ignore": {"foo", "bar", "baz"},
"foo/bar ()": {"error"},
"foo/bar()": {"error"},
"foo/bar(baz)": {"foo", "bar", "baz"},
"foo/bar/zzz(baz)": {"foo/bar", "zzz", "baz"},
"foo/bar(baz/abc)": {"foo", "bar", "baz/abc"},
"foo/bar(baz (abc))": {"foo", "bar", "baz (abc)"},
}
for header, values := range inputs {
serverHeader, err := ParseServerHeader(header)
if err != nil {
if err != errInvalidHeader {
t.Fatalf("Failed to parse %q, and got some unexpected error: %q", header, err)
}
if values[0] == "error" {
continue
}
t.Fatalf("Header %q failed to parse when it shouldn't have", header)
}
if values[0] == "error" {
t.Fatalf("Header %q parsed ok when it should have failed(%q).", header, serverHeader)
}
if serverHeader.App != values[0] {
t.Fatalf("Expected serverHeader.App for %q to equal %q, got %q", header, values[0], serverHeader.App)
}
if serverHeader.Ver != values[1] {
t.Fatalf("Expected serverHeader.Ver for %q to equal %q, got %q", header, values[1], serverHeader.Ver)
}
if serverHeader.OS != values[2] {
t.Fatalf("Expected serverHeader.OS for %q to equal %q, got %q", header, values[2], serverHeader.OS)
}
}
}

View file

@ -1,13 +0,0 @@
package httputils
import (
"testing"
)
func TestDetectContentType(t *testing.T) {
input := []byte("That is just a plain text")
if contentType, _, err := DetectContentType(input); err != nil || contentType != "text/plain" {
t.Error("TestDetectContentType failed")
}
}