aboutsummaryrefslogtreecommitdiffstats
path: root/plumbing/transport/http/upload_pack.go
diff options
context:
space:
mode:
authorMáximo Cuadros <mcuadros@gmail.com>2017-07-05 03:09:08 -0700
committerGitHub <noreply@github.com>2017-07-05 03:09:08 -0700
commit5354ebc084d5300cd8d371a837a0d9dab408c561 (patch)
treeb7a06e810b6a4acb47b27bdd39b0ded51c89031e /plumbing/transport/http/upload_pack.go
parentce6f5b7c82fc6c2c4d41880ed6b26f921dd9c1c3 (diff)
parent10bf320dce4b2a80394b24db19fad89e1da953f7 (diff)
downloadgo-git-5354ebc084d5300cd8d371a837a0d9dab408c561.tar.gz
Merge pull request #432 from ajnavarro/feature/http-push
transport: http push
Diffstat (limited to 'plumbing/transport/http/upload_pack.go')
-rw-r--r--plumbing/transport/http/upload_pack.go73
1 files changed, 4 insertions, 69 deletions
diff --git a/plumbing/transport/http/upload_pack.go b/plumbing/transport/http/upload_pack.go
index 2d1ea45..b1181b6 100644
--- a/plumbing/transport/http/upload_pack.go
+++ b/plumbing/transport/http/upload_pack.go
@@ -5,7 +5,6 @@ import (
"fmt"
"io"
"net/http"
- "strconv"
"gopkg.in/src-d/go-git.v4/plumbing"
"gopkg.in/src-d/go-git.v4/plumbing/format/pktline"
@@ -20,62 +19,13 @@ type upSession struct {
}
func newUploadPackSession(c *http.Client, ep transport.Endpoint, auth transport.AuthMethod) (transport.UploadPackSession, error) {
- s := &session{
- auth: basicAuthFromEndpoint(ep),
- client: c,
- endpoint: ep,
- }
- if auth != nil {
- a, ok := auth.(AuthMethod)
- if !ok {
- return nil, transport.ErrInvalidAuthMethod
- }
+ s, err := newSession(c, ep, auth)
- s.auth = a
- }
-
- return &upSession{session: s}, nil
+ return &upSession{s}, err
}
func (s *upSession) AdvertisedReferences() (*packp.AdvRefs, error) {
- if s.advRefs != nil {
- return s.advRefs, nil
- }
-
- url := fmt.Sprintf(
- "%s/info/refs?service=%s",
- s.endpoint.String(), transport.UploadPackServiceName,
- )
-
- req, err := http.NewRequest(http.MethodGet, url, nil)
- if err != nil {
- return nil, err
- }
-
- s.applyAuthToRequest(req)
- s.applyHeadersToRequest(req, nil)
- res, err := s.client.Do(req)
- if err != nil {
- return nil, err
- }
-
- if err := NewErr(res); err != nil {
- _ = res.Body.Close()
- return nil, err
- }
-
- ar := packp.NewAdvRefs()
- if err := ar.Decode(res.Body); err != nil {
- if err == packp.ErrEmptyAdvRefs {
- err = transport.ErrEmptyRemoteRepository
- }
-
- return nil, err
- }
-
- transport.FilterUnsupportedCapabilities(ar.Capabilities)
- s.advRefs = ar
- return ar, nil
+ return advertisedReferences(s.session, transport.UploadPackServiceName)
}
func (s *upSession) UploadPack(req *packp.UploadPackRequest) (*packp.UploadPackResponse, error) {
@@ -131,7 +81,7 @@ func (s *upSession) doRequest(method, url string, content *bytes.Buffer) (*http.
return nil, plumbing.NewPermanentError(err)
}
- s.applyHeadersToRequest(req, content)
+ applyHeadersToRequest(req, content, s.endpoint.Host(), transport.UploadPackServiceName)
s.applyAuthToRequest(req)
res, err := s.client.Do(req)
@@ -147,21 +97,6 @@ func (s *upSession) doRequest(method, url string, content *bytes.Buffer) (*http.
return res, nil
}
-// it requires a bytes.Buffer, because we need to know the length
-func (s *upSession) applyHeadersToRequest(req *http.Request, content *bytes.Buffer) {
- req.Header.Add("User-Agent", "git/1.0")
- req.Header.Add("Host", s.endpoint.Host()) // host:port
-
- if content == nil {
- req.Header.Add("Accept", "*/*")
- return
- }
-
- req.Header.Add("Accept", "application/x-git-upload-pack-result")
- req.Header.Add("Content-Type", "application/x-git-upload-pack-request")
- req.Header.Add("Content-Length", strconv.Itoa(content.Len()))
-}
-
func uploadPackRequestToReader(req *packp.UploadPackRequest) (*bytes.Buffer, error) {
buf := bytes.NewBuffer(nil)
e := pktline.NewEncoder(buf)