2018-02-05 16:05:59 -05:00
|
|
|
package build // import "github.com/docker/docker/api/server/router/build"
|
2015-12-17 19:17:50 -05:00
|
|
|
|
|
|
|
import (
|
2018-05-18 18:50:10 -04:00
|
|
|
"bufio"
|
2015-10-27 20:29:21 -04:00
|
|
|
"bytes"
|
2018-04-19 18:30:59 -04:00
|
|
|
"context"
|
2015-12-17 19:17:50 -05:00
|
|
|
"encoding/base64"
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"net/http"
|
2016-06-07 15:15:50 -04:00
|
|
|
"runtime"
|
2015-12-17 19:17:50 -05:00
|
|
|
"strconv"
|
|
|
|
"strings"
|
2016-03-22 16:57:24 -04:00
|
|
|
"sync"
|
2015-12-17 19:17:50 -05:00
|
|
|
|
|
|
|
"github.com/docker/docker/api/server/httputils"
|
2016-09-06 14:46:37 -04:00
|
|
|
"github.com/docker/docker/api/types"
|
2016-03-29 18:51:14 -04:00
|
|
|
"github.com/docker/docker/api/types/backend"
|
2016-09-06 14:46:37 -04:00
|
|
|
"github.com/docker/docker/api/types/container"
|
2018-08-15 17:24:37 -04:00
|
|
|
"github.com/docker/docker/api/types/filters"
|
2016-09-06 14:46:37 -04:00
|
|
|
"github.com/docker/docker/api/types/versions"
|
2018-01-11 14:53:06 -05:00
|
|
|
"github.com/docker/docker/errdefs"
|
2015-12-17 19:17:50 -05:00
|
|
|
"github.com/docker/docker/pkg/ioutils"
|
|
|
|
"github.com/docker/docker/pkg/progress"
|
|
|
|
"github.com/docker/docker/pkg/streamformatter"
|
2018-07-02 22:31:05 -04:00
|
|
|
units "github.com/docker/go-units"
|
2017-04-13 14:37:32 -04:00
|
|
|
"github.com/pkg/errors"
|
2017-07-26 17:42:13 -04:00
|
|
|
"github.com/sirupsen/logrus"
|
2015-12-17 19:17:50 -05:00
|
|
|
)
|
|
|
|
|
2017-07-19 10:20:13 -04:00
|
|
|
type invalidIsolationError string
|
|
|
|
|
|
|
|
func (e invalidIsolationError) Error() string {
|
|
|
|
return fmt.Sprintf("Unsupported isolation: %q", string(e))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e invalidIsolationError) InvalidParameter() {}
|
|
|
|
|
2015-12-29 15:49:17 -05:00
|
|
|
func newImageBuildOptions(ctx context.Context, r *http.Request) (*types.ImageBuildOptions, error) {
|
2019-10-13 10:30:03 -04:00
|
|
|
options := &types.ImageBuildOptions{
|
|
|
|
Version: types.BuilderV1, // Builder V1 is the default, but can be overridden
|
|
|
|
Dockerfile: r.FormValue("dockerfile"),
|
|
|
|
SuppressOutput: httputils.BoolValue(r, "q"),
|
|
|
|
NoCache: httputils.BoolValue(r, "nocache"),
|
|
|
|
ForceRemove: httputils.BoolValue(r, "forcerm"),
|
|
|
|
MemorySwap: httputils.Int64ValueOrZero(r, "memswap"),
|
|
|
|
Memory: httputils.Int64ValueOrZero(r, "memory"),
|
|
|
|
CPUShares: httputils.Int64ValueOrZero(r, "cpushares"),
|
|
|
|
CPUPeriod: httputils.Int64ValueOrZero(r, "cpuperiod"),
|
|
|
|
CPUQuota: httputils.Int64ValueOrZero(r, "cpuquota"),
|
|
|
|
CPUSetCPUs: r.FormValue("cpusetcpus"),
|
|
|
|
CPUSetMems: r.FormValue("cpusetmems"),
|
|
|
|
CgroupParent: r.FormValue("cgroupparent"),
|
|
|
|
NetworkMode: r.FormValue("networkmode"),
|
|
|
|
Tags: r.Form["t"],
|
|
|
|
ExtraHosts: r.Form["extrahosts"],
|
|
|
|
SecurityOpt: r.Form["securityopt"],
|
|
|
|
Squash: httputils.BoolValue(r, "squash"),
|
|
|
|
Target: r.FormValue("target"),
|
|
|
|
RemoteContext: r.FormValue("remote"),
|
|
|
|
SessionID: r.FormValue("session"),
|
|
|
|
BuildID: r.FormValue("buildid"),
|
|
|
|
}
|
|
|
|
|
|
|
|
if runtime.GOOS != "windows" && options.SecurityOpt != nil {
|
|
|
|
return nil, errdefs.InvalidParameter(errors.New("The daemon on this platform does not support setting security options on build"))
|
|
|
|
}
|
|
|
|
|
2015-12-29 15:49:17 -05:00
|
|
|
version := httputils.VersionFromContext(ctx)
|
2016-04-19 10:56:54 -04:00
|
|
|
if httputils.BoolValue(r, "forcerm") && versions.GreaterThanOrEqualTo(version, "1.12") {
|
2015-12-29 15:49:17 -05:00
|
|
|
options.Remove = true
|
2016-04-19 10:56:54 -04:00
|
|
|
} else if r.FormValue("rm") == "" && versions.GreaterThanOrEqualTo(version, "1.12") {
|
2015-12-29 15:49:17 -05:00
|
|
|
options.Remove = true
|
|
|
|
} else {
|
|
|
|
options.Remove = httputils.BoolValue(r, "rm")
|
|
|
|
}
|
2016-04-19 10:56:54 -04:00
|
|
|
if httputils.BoolValue(r, "pull") && versions.GreaterThanOrEqualTo(version, "1.16") {
|
2015-12-29 15:49:17 -05:00
|
|
|
options.PullParent = true
|
|
|
|
}
|
2017-09-13 15:49:04 -04:00
|
|
|
if versions.GreaterThanOrEqualTo(version, "1.32") {
|
2018-07-02 22:31:05 -04:00
|
|
|
options.Platform = r.FormValue("platform")
|
2017-09-13 15:49:04 -04:00
|
|
|
}
|
2019-10-13 10:30:03 -04:00
|
|
|
if versions.GreaterThanOrEqualTo(version, "1.40") {
|
|
|
|
outputsJSON := r.FormValue("outputs")
|
|
|
|
if outputsJSON != "" {
|
|
|
|
var outputs []types.ImageBuildOutput
|
|
|
|
if err := json.Unmarshal([]byte(outputsJSON), &outputs); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
options.Outputs = outputs
|
|
|
|
}
|
|
|
|
}
|
2015-12-29 15:49:17 -05:00
|
|
|
|
2019-10-13 10:24:14 -04:00
|
|
|
if s := r.Form.Get("shmsize"); s != "" {
|
|
|
|
shmSize, err := strconv.ParseInt(s, 10, 64)
|
2015-12-29 15:49:17 -05:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
options.ShmSize = shmSize
|
|
|
|
}
|
|
|
|
|
2019-10-13 10:24:14 -04:00
|
|
|
if i := r.FormValue("isolation"); i != "" {
|
|
|
|
options.Isolation = container.Isolation(i)
|
|
|
|
if !options.Isolation.IsValid() {
|
|
|
|
return nil, invalidIsolationError(options.Isolation)
|
2015-12-29 15:49:17 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-13 10:24:14 -04:00
|
|
|
if ulimitsJSON := r.FormValue("ulimits"); ulimitsJSON != "" {
|
|
|
|
var buildUlimits = []*units.Ulimit{}
|
2016-09-22 17:38:00 -04:00
|
|
|
if err := json.Unmarshal([]byte(ulimitsJSON), &buildUlimits); err != nil {
|
2017-11-28 23:09:37 -05:00
|
|
|
return nil, errors.Wrap(errdefs.InvalidParameter(err), "error reading ulimit settings")
|
2015-12-29 15:49:17 -05:00
|
|
|
}
|
|
|
|
options.Ulimits = buildUlimits
|
|
|
|
}
|
|
|
|
|
2016-12-03 08:46:04 -05:00
|
|
|
// Note that there are two ways a --build-arg might appear in the
|
|
|
|
// json of the query param:
|
|
|
|
// "foo":"bar"
|
|
|
|
// and "foo":nil
|
|
|
|
// The first is the normal case, ie. --build-arg foo=bar
|
|
|
|
// or --build-arg foo
|
|
|
|
// where foo's value was picked up from an env var.
|
|
|
|
// The second ("foo":nil) is where they put --build-arg foo
|
|
|
|
// but "foo" isn't set as an env var. In that case we can't just drop
|
|
|
|
// the fact they mentioned it, we need to pass that along to the builder
|
|
|
|
// so that it can print a warning about "foo" being unused if there is
|
|
|
|
// no "ARG foo" in the Dockerfile.
|
2019-10-13 10:24:14 -04:00
|
|
|
if buildArgsJSON := r.FormValue("buildargs"); buildArgsJSON != "" {
|
2017-04-13 14:37:32 -04:00
|
|
|
var buildArgs = map[string]*string{}
|
2016-09-22 17:38:00 -04:00
|
|
|
if err := json.Unmarshal([]byte(buildArgsJSON), &buildArgs); err != nil {
|
2017-11-28 23:09:37 -05:00
|
|
|
return nil, errors.Wrap(errdefs.InvalidParameter(err), "error reading build args")
|
2015-12-29 15:49:17 -05:00
|
|
|
}
|
|
|
|
options.BuildArgs = buildArgs
|
|
|
|
}
|
2016-12-03 08:46:04 -05:00
|
|
|
|
2019-10-13 10:24:14 -04:00
|
|
|
if labelsJSON := r.FormValue("labels"); labelsJSON != "" {
|
2017-04-13 14:37:32 -04:00
|
|
|
var labels = map[string]string{}
|
2016-09-22 17:38:00 -04:00
|
|
|
if err := json.Unmarshal([]byte(labelsJSON), &labels); err != nil {
|
2017-11-28 23:09:37 -05:00
|
|
|
return nil, errors.Wrap(errdefs.InvalidParameter(err), "error reading labels")
|
2016-03-16 17:52:34 -04:00
|
|
|
}
|
|
|
|
options.Labels = labels
|
|
|
|
}
|
|
|
|
|
2019-10-13 10:24:14 -04:00
|
|
|
if cacheFromJSON := r.FormValue("cachefrom"); cacheFromJSON != "" {
|
2017-04-13 14:37:32 -04:00
|
|
|
var cacheFrom = []string{}
|
2016-09-22 17:38:00 -04:00
|
|
|
if err := json.Unmarshal([]byte(cacheFromJSON), &cacheFrom); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
options.CacheFrom = cacheFrom
|
|
|
|
}
|
2019-10-13 10:24:14 -04:00
|
|
|
|
|
|
|
if bv := r.FormValue("version"); bv != "" {
|
|
|
|
v, err := parseVersion(bv)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
options.Version = v
|
2018-05-18 01:47:34 -04:00
|
|
|
}
|
2016-09-22 17:38:00 -04:00
|
|
|
|
2015-12-29 15:49:17 -05:00
|
|
|
return options, nil
|
|
|
|
}
|
|
|
|
|
2018-05-18 01:47:34 -04:00
|
|
|
func parseVersion(s string) (types.BuilderVersion, error) {
|
2019-10-13 08:34:29 -04:00
|
|
|
switch types.BuilderVersion(s) {
|
|
|
|
case types.BuilderV1:
|
|
|
|
return types.BuilderV1, nil
|
|
|
|
case types.BuilderBuildKit:
|
2018-05-18 01:47:34 -04:00
|
|
|
return types.BuilderBuildKit, nil
|
2019-10-13 08:34:29 -04:00
|
|
|
default:
|
2019-10-13 10:30:03 -04:00
|
|
|
return "", errors.Errorf("invalid version %q", s)
|
2018-05-18 01:47:34 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-15 17:54:27 -04:00
|
|
|
func (br *buildRouter) postPrune(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
|
2018-08-15 17:24:37 -04:00
|
|
|
if err := httputils.ParseForm(r); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-08-28 11:38:51 -04:00
|
|
|
fltrs, err := filters.FromJSON(r.Form.Get("filters"))
|
2018-08-15 17:24:37 -04:00
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "could not parse filters")
|
|
|
|
}
|
|
|
|
ksfv := r.FormValue("keep-storage")
|
2018-10-08 19:58:54 -04:00
|
|
|
if ksfv == "" {
|
|
|
|
ksfv = "0"
|
|
|
|
}
|
2018-08-15 17:24:37 -04:00
|
|
|
ks, err := strconv.Atoi(ksfv)
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrapf(err, "keep-storage is in bytes and expects an integer, got %v", ksfv)
|
|
|
|
}
|
|
|
|
|
|
|
|
opts := types.BuildCachePruneOptions{
|
|
|
|
All: httputils.BoolValue(r, "all"),
|
2019-08-28 11:38:51 -04:00
|
|
|
Filters: fltrs,
|
2018-08-15 17:24:37 -04:00
|
|
|
KeepStorage: int64(ks),
|
|
|
|
}
|
|
|
|
|
|
|
|
report, err := br.backend.PruneCache(ctx, opts)
|
2017-05-15 17:54:27 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return httputils.WriteJSON(w, http.StatusOK, report)
|
|
|
|
}
|
|
|
|
|
2018-04-19 14:08:33 -04:00
|
|
|
func (br *buildRouter) postCancel(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
|
|
|
|
w.Header().Set("Content-Type", "application/json")
|
|
|
|
|
|
|
|
id := r.FormValue("id")
|
|
|
|
if id == "" {
|
|
|
|
return errors.Errorf("build ID not provided")
|
|
|
|
}
|
|
|
|
|
|
|
|
return br.backend.Cancel(ctx, id)
|
|
|
|
}
|
|
|
|
|
2015-12-17 19:17:50 -05:00
|
|
|
func (br *buildRouter) postBuild(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
|
2017-04-06 08:33:56 -04:00
|
|
|
var (
|
|
|
|
notVerboseBuffer = bytes.NewBuffer(nil)
|
|
|
|
version = httputils.VersionFromContext(ctx)
|
|
|
|
)
|
2015-12-17 19:17:50 -05:00
|
|
|
|
|
|
|
w.Header().Set("Content-Type", "application/json")
|
|
|
|
|
2018-05-18 18:50:10 -04:00
|
|
|
body := r.Body
|
2018-05-25 20:30:42 -04:00
|
|
|
var ww io.Writer = w
|
2018-05-18 18:50:10 -04:00
|
|
|
if body != nil {
|
|
|
|
// there is a possibility that output is written before request body
|
|
|
|
// has been fully read so we need to protect against it.
|
|
|
|
// this can be removed when
|
|
|
|
// https://github.com/golang/go/issues/15527
|
|
|
|
// https://github.com/golang/go/issues/22209
|
|
|
|
// has been fixed
|
2018-05-25 20:30:42 -04:00
|
|
|
body, ww = wrapOutputBufferedUntilRequestRead(body, ww)
|
2018-05-18 18:50:10 -04:00
|
|
|
}
|
2018-05-25 20:30:42 -04:00
|
|
|
|
|
|
|
output := ioutils.NewWriteFlusher(ww)
|
2019-08-28 11:38:51 -04:00
|
|
|
defer func() { _ = output.Close() }()
|
2018-05-25 20:30:42 -04:00
|
|
|
|
2015-12-17 19:17:50 -05:00
|
|
|
errf := func(err error) error {
|
2018-05-25 20:30:42 -04:00
|
|
|
|
2015-12-29 15:49:17 -05:00
|
|
|
if httputils.BoolValue(r, "q") && notVerboseBuffer.Len() > 0 {
|
2019-08-28 11:38:51 -04:00
|
|
|
_, _ = output.Write(notVerboseBuffer.Bytes())
|
2015-10-27 20:29:21 -04:00
|
|
|
}
|
2018-05-25 20:30:42 -04:00
|
|
|
|
2015-12-17 19:17:50 -05:00
|
|
|
// Do not write the error in the http output if it's still empty.
|
|
|
|
// This prevents from writing a 200(OK) when there is an internal error.
|
|
|
|
if !output.Flushed() {
|
|
|
|
return err
|
|
|
|
}
|
2018-05-25 20:30:42 -04:00
|
|
|
_, err = output.Write(streamformatter.FormatError(err))
|
2015-12-17 19:17:50 -05:00
|
|
|
if err != nil {
|
|
|
|
logrus.Warnf("could not write error response: %v", err)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-12-29 15:49:17 -05:00
|
|
|
buildOptions, err := newImageBuildOptions(ctx, r)
|
|
|
|
if err != nil {
|
|
|
|
return errf(err)
|
2015-12-17 19:17:50 -05:00
|
|
|
}
|
2017-04-13 14:37:32 -04:00
|
|
|
buildOptions.AuthConfigs = getAuthConfigs(r.Header)
|
|
|
|
|
|
|
|
if buildOptions.Squash && !br.daemon.HasExperimental() {
|
2017-11-28 23:09:37 -05:00
|
|
|
return errdefs.InvalidParameter(errors.New("squash is only supported with experimental mode"))
|
2017-04-13 14:37:32 -04:00
|
|
|
}
|
2015-12-17 19:17:50 -05:00
|
|
|
|
2017-05-01 14:54:56 -04:00
|
|
|
out := io.Writer(output)
|
|
|
|
if buildOptions.SuppressOutput {
|
|
|
|
out = notVerboseBuffer
|
|
|
|
}
|
|
|
|
|
2015-12-17 19:17:50 -05:00
|
|
|
// Currently, only used if context is from a remote url.
|
|
|
|
// Look at code in DetectContextFromRemoteURL for more information.
|
|
|
|
createProgressReader := func(in io.ReadCloser) io.ReadCloser {
|
2017-05-01 14:54:56 -04:00
|
|
|
progressOutput := streamformatter.NewJSONProgressOutput(out, true)
|
2017-03-20 18:22:29 -04:00
|
|
|
return progress.NewProgressReader(in, progressOutput, r.ContentLength, "Downloading context", buildOptions.RemoteContext)
|
2015-12-17 19:17:50 -05:00
|
|
|
}
|
|
|
|
|
2017-04-06 08:33:56 -04:00
|
|
|
wantAux := versions.GreaterThanOrEqualTo(version, "1.30")
|
|
|
|
|
2017-04-13 14:37:32 -04:00
|
|
|
imgID, err := br.backend.Build(ctx, backend.BuildConfig{
|
2018-05-18 18:50:10 -04:00
|
|
|
Source: body,
|
2017-04-13 14:37:32 -04:00
|
|
|
Options: buildOptions,
|
2017-04-06 08:33:56 -04:00
|
|
|
ProgressWriter: buildProgressWriter(out, wantAux, createProgressReader),
|
2017-04-13 14:37:32 -04:00
|
|
|
})
|
2015-12-17 19:17:50 -05:00
|
|
|
if err != nil {
|
|
|
|
return errf(err)
|
|
|
|
}
|
|
|
|
|
2015-10-27 20:29:21 -04:00
|
|
|
// Everything worked so if -q was provided the output from the daemon
|
|
|
|
// should be just the image ID and we'll print that to stdout.
|
2015-12-29 15:49:17 -05:00
|
|
|
if buildOptions.SuppressOutput {
|
2019-08-28 11:38:51 -04:00
|
|
|
_, _ = fmt.Fprintln(streamformatter.NewStdoutWriter(output), imgID)
|
2015-10-27 20:29:21 -04:00
|
|
|
}
|
2015-12-17 19:17:50 -05:00
|
|
|
return nil
|
|
|
|
}
|
2017-04-13 14:37:32 -04:00
|
|
|
|
|
|
|
func getAuthConfigs(header http.Header) map[string]types.AuthConfig {
|
|
|
|
authConfigs := map[string]types.AuthConfig{}
|
|
|
|
authConfigsEncoded := header.Get("X-Registry-Config")
|
|
|
|
|
|
|
|
if authConfigsEncoded == "" {
|
|
|
|
return authConfigs
|
|
|
|
}
|
|
|
|
|
|
|
|
authConfigsJSON := base64.NewDecoder(base64.URLEncoding, strings.NewReader(authConfigsEncoded))
|
|
|
|
// Pulling an image does not error when no auth is provided so to remain
|
|
|
|
// consistent with the existing api decode errors are ignored
|
2019-08-28 11:38:51 -04:00
|
|
|
_ = json.NewDecoder(authConfigsJSON).Decode(&authConfigs)
|
2017-04-13 14:37:32 -04:00
|
|
|
return authConfigs
|
|
|
|
}
|
|
|
|
|
|
|
|
type syncWriter struct {
|
|
|
|
w io.Writer
|
|
|
|
mu sync.Mutex
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *syncWriter) Write(b []byte) (count int, err error) {
|
|
|
|
s.mu.Lock()
|
|
|
|
count, err = s.w.Write(b)
|
|
|
|
s.mu.Unlock()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-04-06 08:33:56 -04:00
|
|
|
func buildProgressWriter(out io.Writer, wantAux bool, createProgressReader func(io.ReadCloser) io.ReadCloser) backend.ProgressWriter {
|
2017-04-13 14:37:32 -04:00
|
|
|
out = &syncWriter{w: out}
|
|
|
|
|
2017-04-06 08:33:56 -04:00
|
|
|
var aux *streamformatter.AuxFormatter
|
|
|
|
if wantAux {
|
|
|
|
aux = &streamformatter.AuxFormatter{Writer: out}
|
|
|
|
}
|
|
|
|
|
2017-04-13 14:37:32 -04:00
|
|
|
return backend.ProgressWriter{
|
|
|
|
Output: out,
|
2017-05-01 14:54:56 -04:00
|
|
|
StdoutFormatter: streamformatter.NewStdoutWriter(out),
|
|
|
|
StderrFormatter: streamformatter.NewStderrWriter(out),
|
2017-04-06 08:33:56 -04:00
|
|
|
AuxFormatter: aux,
|
2017-04-13 14:37:32 -04:00
|
|
|
ProgressReaderFunc: createProgressReader,
|
|
|
|
}
|
|
|
|
}
|
2018-05-18 18:50:10 -04:00
|
|
|
|
2018-05-25 20:30:42 -04:00
|
|
|
type flusher interface {
|
2018-05-18 18:50:10 -04:00
|
|
|
Flush()
|
|
|
|
}
|
|
|
|
|
2018-05-25 20:30:42 -04:00
|
|
|
func wrapOutputBufferedUntilRequestRead(rc io.ReadCloser, out io.Writer) (io.ReadCloser, io.Writer) {
|
|
|
|
var fl flusher = &ioutils.NopFlusher{}
|
|
|
|
if f, ok := out.(flusher); ok {
|
|
|
|
fl = f
|
|
|
|
}
|
|
|
|
|
2018-05-18 18:50:10 -04:00
|
|
|
w := &wcf{
|
2018-05-25 20:30:42 -04:00
|
|
|
buf: bytes.NewBuffer(nil),
|
|
|
|
Writer: out,
|
|
|
|
flusher: fl,
|
2018-05-18 18:50:10 -04:00
|
|
|
}
|
|
|
|
r := bufio.NewReader(rc)
|
|
|
|
_, err := r.Peek(1)
|
|
|
|
if err != nil {
|
|
|
|
return rc, out
|
|
|
|
}
|
|
|
|
rc = &rcNotifier{
|
|
|
|
Reader: r,
|
|
|
|
Closer: rc,
|
|
|
|
notify: w.notify,
|
|
|
|
}
|
|
|
|
return rc, w
|
|
|
|
}
|
|
|
|
|
|
|
|
type rcNotifier struct {
|
|
|
|
io.Reader
|
|
|
|
io.Closer
|
|
|
|
notify func()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *rcNotifier) Read(b []byte) (int, error) {
|
|
|
|
n, err := r.Reader.Read(b)
|
|
|
|
if err != nil {
|
|
|
|
r.notify()
|
|
|
|
}
|
|
|
|
return n, err
|
|
|
|
}
|
|
|
|
|
2018-05-25 17:58:55 -04:00
|
|
|
func (r *rcNotifier) Close() error {
|
|
|
|
r.notify()
|
|
|
|
return r.Closer.Close()
|
|
|
|
}
|
|
|
|
|
2018-05-18 18:50:10 -04:00
|
|
|
type wcf struct {
|
2018-05-25 20:30:42 -04:00
|
|
|
io.Writer
|
|
|
|
flusher
|
2018-05-18 18:50:10 -04:00
|
|
|
mu sync.Mutex
|
|
|
|
ready bool
|
|
|
|
buf *bytes.Buffer
|
|
|
|
flushed bool
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *wcf) Flush() {
|
|
|
|
w.mu.Lock()
|
|
|
|
w.flushed = true
|
|
|
|
if !w.ready {
|
|
|
|
w.mu.Unlock()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
w.mu.Unlock()
|
2018-05-25 20:30:42 -04:00
|
|
|
w.flusher.Flush()
|
2018-05-18 18:50:10 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (w *wcf) Flushed() bool {
|
|
|
|
w.mu.Lock()
|
|
|
|
b := w.flushed
|
|
|
|
w.mu.Unlock()
|
|
|
|
return b
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *wcf) Write(b []byte) (int, error) {
|
|
|
|
w.mu.Lock()
|
|
|
|
if !w.ready {
|
|
|
|
n, err := w.buf.Write(b)
|
|
|
|
w.mu.Unlock()
|
|
|
|
return n, err
|
|
|
|
}
|
|
|
|
w.mu.Unlock()
|
2018-05-25 20:30:42 -04:00
|
|
|
return w.Writer.Write(b)
|
2018-05-18 18:50:10 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (w *wcf) notify() {
|
|
|
|
w.mu.Lock()
|
|
|
|
if !w.ready {
|
|
|
|
if w.buf.Len() > 0 {
|
2019-08-28 11:38:51 -04:00
|
|
|
_, _ = io.Copy(w.Writer, w.buf)
|
2018-05-18 18:50:10 -04:00
|
|
|
}
|
|
|
|
if w.flushed {
|
2018-05-25 20:30:42 -04:00
|
|
|
w.flusher.Flush()
|
2018-05-18 18:50:10 -04:00
|
|
|
}
|
|
|
|
w.ready = true
|
|
|
|
}
|
|
|
|
w.mu.Unlock()
|
|
|
|
}
|