2019-04-16 00:43:55 +00:00
|
|
|
package http
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"io"
|
|
|
|
"net/http"
|
|
|
|
|
|
|
|
"github.com/moby/buildkit/session"
|
|
|
|
"github.com/moby/buildkit/session/upload"
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
)
|
|
|
|
|
2020-06-30 01:06:02 +00:00
|
|
|
func newTransport(rt http.RoundTripper, sm *session.Manager, g session.Group) http.RoundTripper {
|
|
|
|
return &sessionHandler{rt: rt, sm: sm, g: g}
|
2019-04-16 00:43:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type sessionHandler struct {
|
|
|
|
sm *session.Manager
|
|
|
|
rt http.RoundTripper
|
2020-06-30 01:06:02 +00:00
|
|
|
g session.Group
|
2019-04-16 00:43:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (h *sessionHandler) RoundTrip(req *http.Request) (*http.Response, error) {
|
|
|
|
if req.URL.Host != "buildkit-session" {
|
|
|
|
return h.rt.RoundTrip(req)
|
|
|
|
}
|
|
|
|
|
|
|
|
if req.Method != "GET" {
|
|
|
|
return nil, errors.Errorf("invalid request")
|
|
|
|
}
|
|
|
|
|
2020-06-30 01:06:02 +00:00
|
|
|
var resp *http.Response
|
|
|
|
err := h.sm.Any(context.TODO(), h.g, func(ctx context.Context, _ string, caller session.Caller) error {
|
|
|
|
up, err := upload.New(context.TODO(), caller, req.URL)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
pr, pw := io.Pipe()
|
|
|
|
go func() {
|
|
|
|
_, err := up.WriteTo(pw)
|
|
|
|
pw.CloseWithError(err)
|
|
|
|
}()
|
|
|
|
|
|
|
|
resp = &http.Response{
|
|
|
|
Status: "200 OK",
|
|
|
|
StatusCode: 200,
|
|
|
|
Body: pr,
|
|
|
|
ContentLength: -1,
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
2019-04-16 00:43:55 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return resp, nil
|
|
|
|
}
|