github.com/vieux/docker@v0.6.3-0.20161004191708-e097c2a938c7/builder/remote.go (about) 1 package builder 2 3 import ( 4 "bytes" 5 "errors" 6 "fmt" 7 "io" 8 "io/ioutil" 9 "regexp" 10 11 "github.com/docker/docker/pkg/archive" 12 "github.com/docker/docker/pkg/httputils" 13 "github.com/docker/docker/pkg/urlutil" 14 ) 15 16 // When downloading remote contexts, limit the amount (in bytes) 17 // to be read from the response body in order to detect its Content-Type 18 const maxPreambleLength = 100 19 20 const acceptableRemoteMIME = `(?:application/(?:(?:x\-)?tar|octet\-stream|((?:x\-)?(?:gzip|bzip2?|xz)))|(?:text/plain))` 21 22 var mimeRe = regexp.MustCompile(acceptableRemoteMIME) 23 24 // MakeRemoteContext downloads a context from remoteURL and returns it. 25 // 26 // If contentTypeHandlers is non-nil, then the Content-Type header is read along with a maximum of 27 // maxPreambleLength bytes from the body to help detecting the MIME type. 28 // Look at acceptableRemoteMIME for more details. 29 // 30 // If a match is found, then the body is sent to the contentType handler and a (potentially compressed) tar stream is expected 31 // to be returned. If no match is found, it is assumed the body is a tar stream (compressed or not). 32 // In either case, an (assumed) tar stream is passed to MakeTarSumContext whose result is returned. 33 func MakeRemoteContext(remoteURL string, contentTypeHandlers map[string]func(io.ReadCloser) (io.ReadCloser, error)) (ModifiableContext, error) { 34 f, err := httputils.Download(remoteURL) 35 if err != nil { 36 return nil, fmt.Errorf("Error downloading remote context %s: %v", remoteURL, err) 37 } 38 defer f.Body.Close() 39 40 var contextReader io.ReadCloser 41 if contentTypeHandlers != nil { 42 contentType := f.Header.Get("Content-Type") 43 clen := f.ContentLength 44 45 contentType, contextReader, err = inspectResponse(contentType, f.Body, clen) 46 if err != nil { 47 return nil, fmt.Errorf("Error detecting content type for remote %s: %v", remoteURL, err) 48 } 49 defer contextReader.Close() 50 51 // This loop tries to find a content-type handler for the detected content-type. 52 // If it could not find one from the caller-supplied map, it tries the empty content-type `""` 53 // which is interpreted as a fallback handler (usually used for raw tar contexts). 54 for _, ct := range []string{contentType, ""} { 55 if fn, ok := contentTypeHandlers[ct]; ok { 56 defer contextReader.Close() 57 if contextReader, err = fn(contextReader); err != nil { 58 return nil, err 59 } 60 break 61 } 62 } 63 } 64 65 // Pass through - this is a pre-packaged context, presumably 66 // with a Dockerfile with the right name inside it. 67 return MakeTarSumContext(contextReader) 68 } 69 70 // DetectContextFromRemoteURL returns a context and in certain cases the name of the dockerfile to be used 71 // irrespective of user input. 72 // progressReader is only used if remoteURL is actually a URL (not empty, and not a Git endpoint). 73 func DetectContextFromRemoteURL(r io.ReadCloser, remoteURL string, createProgressReader func(in io.ReadCloser) io.ReadCloser) (context ModifiableContext, dockerfileName string, err error) { 74 switch { 75 case remoteURL == "": 76 context, err = MakeTarSumContext(r) 77 case urlutil.IsGitURL(remoteURL): 78 context, err = MakeGitContext(remoteURL) 79 case urlutil.IsURL(remoteURL): 80 context, err = MakeRemoteContext(remoteURL, map[string]func(io.ReadCloser) (io.ReadCloser, error){ 81 httputils.MimeTypes.TextPlain: func(rc io.ReadCloser) (io.ReadCloser, error) { 82 dockerfile, err := ioutil.ReadAll(rc) 83 if err != nil { 84 return nil, err 85 } 86 87 // dockerfileName is set to signal that the remote was interpreted as a single Dockerfile, in which case the caller 88 // should use dockerfileName as the new name for the Dockerfile, irrespective of any other user input. 89 dockerfileName = DefaultDockerfileName 90 91 // TODO: return a context without tarsum 92 return archive.Generate(dockerfileName, string(dockerfile)) 93 }, 94 // fallback handler (tar context) 95 "": func(rc io.ReadCloser) (io.ReadCloser, error) { 96 return createProgressReader(rc), nil 97 }, 98 }) 99 default: 100 err = fmt.Errorf("remoteURL (%s) could not be recognized as URL", remoteURL) 101 } 102 return 103 } 104 105 // inspectResponse looks into the http response data at r to determine whether its 106 // content-type is on the list of acceptable content types for remote build contexts. 107 // This function returns: 108 // - a string representation of the detected content-type 109 // - an io.Reader for the response body 110 // - an error value which will be non-nil either when something goes wrong while 111 // reading bytes from r or when the detected content-type is not acceptable. 112 func inspectResponse(ct string, r io.ReadCloser, clen int64) (string, io.ReadCloser, error) { 113 plen := clen 114 if plen <= 0 || plen > maxPreambleLength { 115 plen = maxPreambleLength 116 } 117 118 preamble := make([]byte, plen, plen) 119 rlen, err := r.Read(preamble) 120 if rlen == 0 { 121 return ct, r, errors.New("Empty response") 122 } 123 if err != nil && err != io.EOF { 124 return ct, r, err 125 } 126 127 preambleR := bytes.NewReader(preamble) 128 bodyReader := ioutil.NopCloser(io.MultiReader(preambleR, r)) 129 // Some web servers will use application/octet-stream as the default 130 // content type for files without an extension (e.g. 'Dockerfile') 131 // so if we receive this value we better check for text content 132 contentType := ct 133 if len(ct) == 0 || ct == httputils.MimeTypes.OctetStream { 134 contentType, _, err = httputils.DetectContentType(preamble) 135 if err != nil { 136 return contentType, bodyReader, err 137 } 138 } 139 140 contentType = selectAcceptableMIME(contentType) 141 var cterr error 142 if len(contentType) == 0 { 143 cterr = fmt.Errorf("unsupported Content-Type %q", ct) 144 contentType = ct 145 } 146 147 return contentType, bodyReader, cterr 148 } 149 150 func selectAcceptableMIME(ct string) string { 151 return mimeRe.FindString(ct) 152 }