| 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/format/pktline" |
| 12 |
"github.com/go-git/go-git/v5/plumbing/protocol/packp" |
| 13 |
"github.com/go-git/go-git/v5/plumbing/transport" |
| 14 |
"github.com/go-git/go-git/v5/plumbing/transport/internal/common" |
| 15 |
"github.com/go-git/go-git/v5/utils/ioutil" |
| 16 |
) |
| 17 |
|
| 18 |
type upSession struct { |
| 19 |
*session |
| 20 |
} |
| 21 |
|
| 22 |
func newUploadPackSession(c *client, ep *transport.Endpoint, auth transport.AuthMethod) (transport.UploadPackSession, error) { |
| 23 |
s, err := newSession(c, ep, auth) |
| 24 |
return &upSession{s}, err |
| 25 |
} |
| 26 |
|
| 27 |
func (s *upSession) AdvertisedReferences() (*packp.AdvRefs, error) { |
| 28 |
return advertisedReferences(context.TODO(), s.session, transport.UploadPackServiceName) |
| 29 |
} |
| 30 |
|
| 31 |
func (s *upSession) AdvertisedReferencesContext(ctx context.Context) (*packp.AdvRefs, error) { |
| 32 |
return advertisedReferences(ctx, s.session, transport.UploadPackServiceName) |
| 33 |
} |
| 34 |
|
| 35 |
func (s *upSession) UploadPack( |
| 36 |
ctx context.Context, req *packp.UploadPackRequest, |
| 37 |
) (*packp.UploadPackResponse, error) { |
| 38 |
|
| 39 |
if req.IsEmpty() { |
| 40 |
return nil, transport.ErrEmptyUploadPackRequest |
| 41 |
} |
| 42 |
|
| 43 |
if err := req.Validate(); err != nil { |
| 44 |
return nil, err |
| 45 |
} |
| 46 |
|
| 47 |
url := fmt.Sprintf( |
| 48 |
"%s/%s", |
| 49 |
s.endpoint.String(), transport.UploadPackServiceName, |
| 50 |
) |
| 51 |
|
| 52 |
content, err := uploadPackRequestToReader(req) |
| 53 |
if err != nil { |
| 54 |
return nil, err |
| 55 |
} |
| 56 |
|
| 57 |
res, err := s.doRequest(ctx, http.MethodPost, url, content) |
| 58 |
if err != nil { |
| 59 |
return nil, err |
| 60 |
} |
| 61 |
|
| 62 |
r, err := ioutil.NonEmptyReader(res.Body) |
| 63 |
if err != nil { |
| 64 |
if err == ioutil.ErrEmptyReader || err == io.ErrUnexpectedEOF { |
| 65 |
return nil, transport.ErrEmptyUploadPackRequest |
| 66 |
} |
| 67 |
|
| 68 |
return nil, err |
| 69 |
} |
| 70 |
|
| 71 |
rc := ioutil.NewReadCloser(r, res.Body) |
| 72 |
return common.DecodeUploadPackResponse(rc, req) |
| 73 |
} |
| 74 |
|
| 75 |
// Close does nothing. |
| 76 |
func (s *upSession) Close() error { |
| 77 |
return nil |
| 78 |
} |
| 79 |
|
| 80 |
func (s *upSession) doRequest( |
| 81 |
ctx context.Context, method, url string, content *bytes.Buffer, |
| 82 |
) (*http.Response, error) { |
| 83 |
|
| 84 |
var body io.Reader |
| 85 |
if content != nil { |
| 86 |
body = content |
| 87 |
} |
| 88 |
|
| 89 |
req, err := http.NewRequest(method, url, body) |
| 90 |
if err != nil { |
| 91 |
return nil, plumbing.NewPermanentError(err) |
| 92 |
} |
| 93 |
|
| 94 |
applyHeadersToRequest(req, content, s.endpoint.Host, transport.UploadPackServiceName) |
| 95 |
s.ApplyAuthToRequest(req) |
| 96 |
|
| 97 |
res, err := s.client.Do(req.WithContext(ctx)) |
| 98 |
if err != nil { |
| 99 |
return nil, plumbing.NewUnexpectedError(err) |
| 100 |
} |
| 101 |
|
| 102 |
if err := NewErr(res); err != nil { |
| 103 |
return nil, err |
| 104 |
} |
| 105 |
|
| 106 |
return res, nil |
| 107 |
} |
| 108 |
|
| 109 |
func uploadPackRequestToReader(req *packp.UploadPackRequest) (*bytes.Buffer, error) { |
| 110 |
buf := bytes.NewBuffer(nil) |
| 111 |
e := pktline.NewEncoder(buf) |
| 112 |
|
| 113 |
if err := req.UploadRequest.Encode(buf); err != nil { |
| 114 |
return nil, fmt.Errorf("sending upload-req message: %s", err) |
| 115 |
} |
| 116 |
|
| 117 |
if err := req.UploadHaves.Encode(buf, false); err != nil { |
| 118 |
return nil, fmt.Errorf("sending haves message: %s", err) |
| 119 |
} |
| 120 |
|
| 121 |
if err := e.EncodeString("done\n"); err != nil { |
| 122 |
return nil, err |
| 123 |
} |
| 124 |
|
| 125 |
return buf, nil |
| 126 |
} |