| 1 |
package http |
| 2 |
|
| 3 |
import ( |
| 4 |
"bytes" |
| 5 |
"context" |
| 6 |
"fmt" |
| 7 |
"io" |
| 8 |
"net/http" |
| 9 |
|
| 10 |
"github.com/go-git/go-git/v5/plumbing" |
| 11 |
"github.com/go-git/go-git/v5/plumbing/protocol/packp" |
| 12 |
"github.com/go-git/go-git/v5/plumbing/protocol/packp/capability" |
| 13 |
"github.com/go-git/go-git/v5/plumbing/protocol/packp/sideband" |
| 14 |
"github.com/go-git/go-git/v5/plumbing/transport" |
| 15 |
"github.com/go-git/go-git/v5/utils/ioutil" |
| 16 |
) |
| 17 |
|
| 18 |
type rpSession struct { |
| 19 |
*session |
| 20 |
} |
| 21 |
|
| 22 |
func newReceivePackSession(c *client, ep *transport.Endpoint, auth transport.AuthMethod) (transport.ReceivePackSession, error) { |
| 23 |
s, err := newSession(c, ep, auth) |
| 24 |
return &rpSession{s}, err |
| 25 |
} |
| 26 |
|
| 27 |
func (s *rpSession) AdvertisedReferences() (*packp.AdvRefs, error) { |
| 28 |
return advertisedReferences(context.TODO(), s.session, transport.ReceivePackServiceName) |
| 29 |
} |
| 30 |
|
| 31 |
func (s *rpSession) AdvertisedReferencesContext(ctx context.Context) (*packp.AdvRefs, error) { |
| 32 |
return advertisedReferences(ctx, s.session, transport.ReceivePackServiceName) |
| 33 |
} |
| 34 |
|
| 35 |
func (s *rpSession) ReceivePack(ctx context.Context, req *packp.ReferenceUpdateRequest) ( |
| 36 |
*packp.ReportStatus, error) { |
| 37 |
url := fmt.Sprintf( |
| 38 |
"%s/%s", |
| 39 |
s.endpoint.String(), transport.ReceivePackServiceName, |
| 40 |
) |
| 41 |
|
| 42 |
buf := bytes.NewBuffer(nil) |
| 43 |
if err := req.Encode(buf); err != nil { |
| 44 |
return nil, err |
| 45 |
} |
| 46 |
|
| 47 |
res, err := s.doRequest(ctx, http.MethodPost, url, buf) |
| 48 |
if err != nil { |
| 49 |
return nil, err |
| 50 |
} |
| 51 |
|
| 52 |
r, err := ioutil.NonEmptyReader(res.Body) |
| 53 |
if err == ioutil.ErrEmptyReader { |
| 54 |
return nil, nil |
| 55 |
} |
| 56 |
|
| 57 |
if err != nil { |
| 58 |
return nil, err |
| 59 |
} |
| 60 |
|
| 61 |
var d *sideband.Demuxer |
| 62 |
if req.Capabilities.Supports(capability.Sideband64k) { |
| 63 |
d = sideband.NewDemuxer(sideband.Sideband64k, r) |
| 64 |
} else if req.Capabilities.Supports(capability.Sideband) { |
| 65 |
d = sideband.NewDemuxer(sideband.Sideband, r) |
| 66 |
} |
| 67 |
if d != nil { |
| 68 |
d.Progress = req.Progress |
| 69 |
r = d |
| 70 |
} |
| 71 |
|
| 72 |
rc := ioutil.NewReadCloser(r, res.Body) |
| 73 |
|
| 74 |
report := packp.NewReportStatus() |
| 75 |
if err := report.Decode(rc); err != nil { |
| 76 |
return nil, err |
| 77 |
} |
| 78 |
|
| 79 |
return report, report.Error() |
| 80 |
} |
| 81 |
|
| 82 |
func (s *rpSession) doRequest( |
| 83 |
ctx context.Context, method, url string, content *bytes.Buffer, |
| 84 |
) (*http.Response, error) { |
| 85 |
|
| 86 |
var body io.Reader |
| 87 |
if content != nil { |
| 88 |
body = content |
| 89 |
} |
| 90 |
|
| 91 |
req, err := http.NewRequest(method, url, body) |
| 92 |
if err != nil { |
| 93 |
return nil, plumbing.NewPermanentError(err) |
| 94 |
} |
| 95 |
|
| 96 |
applyHeadersToRequest(req, content, s.endpoint.Host, transport.ReceivePackServiceName) |
| 97 |
s.ApplyAuthToRequest(req) |
| 98 |
|
| 99 |
res, err := s.client.Do(req.WithContext(ctx)) |
| 100 |
if err != nil { |
| 101 |
return nil, plumbing.NewUnexpectedError(err) |
| 102 |
} |
| 103 |
|
| 104 |
if err := NewErr(res); err != nil { |
| 105 |
return nil, err |
| 106 |
} |
| 107 |
|
| 108 |
return res, nil |
| 109 |
} |