remote.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. package remotecontext
  2. import (
  3. "bytes"
  4. "errors"
  5. "fmt"
  6. "io"
  7. "io/ioutil"
  8. "regexp"
  9. "github.com/docker/docker/builder"
  10. "github.com/docker/docker/pkg/httputils"
  11. )
  12. // When downloading remote contexts, limit the amount (in bytes)
  13. // to be read from the response body in order to detect its Content-Type
  14. const maxPreambleLength = 100
  15. const acceptableRemoteMIME = `(?:application/(?:(?:x\-)?tar|octet\-stream|((?:x\-)?(?:gzip|bzip2?|xz)))|(?:text/plain))`
  16. var mimeRe = regexp.MustCompile(acceptableRemoteMIME)
  17. // MakeRemoteContext downloads a context from remoteURL and returns it.
  18. //
  19. // If contentTypeHandlers is non-nil, then the Content-Type header is read along with a maximum of
  20. // maxPreambleLength bytes from the body to help detecting the MIME type.
  21. // Look at acceptableRemoteMIME for more details.
  22. //
  23. // If a match is found, then the body is sent to the contentType handler and a (potentially compressed) tar stream is expected
  24. // to be returned. If no match is found, it is assumed the body is a tar stream (compressed or not).
  25. // In either case, an (assumed) tar stream is passed to MakeTarSumContext whose result is returned.
  26. func MakeRemoteContext(remoteURL string, contentTypeHandlers map[string]func(io.ReadCloser) (io.ReadCloser, error)) (builder.Source, error) {
  27. f, err := httputils.Download(remoteURL)
  28. if err != nil {
  29. return nil, fmt.Errorf("error downloading remote context %s: %v", remoteURL, err)
  30. }
  31. defer f.Body.Close()
  32. var contextReader io.ReadCloser
  33. if contentTypeHandlers != nil {
  34. contentType := f.Header.Get("Content-Type")
  35. clen := f.ContentLength
  36. contentType, contextReader, err = inspectResponse(contentType, f.Body, clen)
  37. if err != nil {
  38. return nil, fmt.Errorf("error detecting content type for remote %s: %v", remoteURL, err)
  39. }
  40. defer contextReader.Close()
  41. // This loop tries to find a content-type handler for the detected content-type.
  42. // If it could not find one from the caller-supplied map, it tries the empty content-type `""`
  43. // which is interpreted as a fallback handler (usually used for raw tar contexts).
  44. for _, ct := range []string{contentType, ""} {
  45. if fn, ok := contentTypeHandlers[ct]; ok {
  46. defer contextReader.Close()
  47. if contextReader, err = fn(contextReader); err != nil {
  48. return nil, err
  49. }
  50. break
  51. }
  52. }
  53. }
  54. // Pass through - this is a pre-packaged context, presumably
  55. // with a Dockerfile with the right name inside it.
  56. return MakeTarSumContext(contextReader)
  57. }
  58. // inspectResponse looks into the http response data at r to determine whether its
  59. // content-type is on the list of acceptable content types for remote build contexts.
  60. // This function returns:
  61. // - a string representation of the detected content-type
  62. // - an io.Reader for the response body
  63. // - an error value which will be non-nil either when something goes wrong while
  64. // reading bytes from r or when the detected content-type is not acceptable.
  65. func inspectResponse(ct string, r io.ReadCloser, clen int64) (string, io.ReadCloser, error) {
  66. plen := clen
  67. if plen <= 0 || plen > maxPreambleLength {
  68. plen = maxPreambleLength
  69. }
  70. preamble := make([]byte, plen, plen)
  71. rlen, err := r.Read(preamble)
  72. if rlen == 0 {
  73. return ct, r, errors.New("empty response")
  74. }
  75. if err != nil && err != io.EOF {
  76. return ct, r, err
  77. }
  78. preambleR := bytes.NewReader(preamble[:rlen])
  79. bodyReader := ioutil.NopCloser(io.MultiReader(preambleR, r))
  80. // Some web servers will use application/octet-stream as the default
  81. // content type for files without an extension (e.g. 'Dockerfile')
  82. // so if we receive this value we better check for text content
  83. contentType := ct
  84. if len(ct) == 0 || ct == httputils.MimeTypes.OctetStream {
  85. contentType, _, err = httputils.DetectContentType(preamble)
  86. if err != nil {
  87. return contentType, bodyReader, err
  88. }
  89. }
  90. contentType = selectAcceptableMIME(contentType)
  91. var cterr error
  92. if len(contentType) == 0 {
  93. cterr = fmt.Errorf("unsupported Content-Type %q", ct)
  94. contentType = ct
  95. }
  96. return contentType, bodyReader, cterr
  97. }
  98. func selectAcceptableMIME(ct string) string {
  99. return mimeRe.FindString(ct)
  100. }