diff options
author | Antonio Jesus Navarro Perez <antnavper@gmail.com> | 2017-06-16 16:06:32 +0200 |
---|---|---|
committer | Antonio Jesus Navarro Perez <antnavper@gmail.com> | 2017-07-04 11:17:26 +0200 |
commit | 06f26e2d7096a7e8458e9e9b41e05c8bb83babf6 (patch) | |
tree | 98ae3a8168532ce40c06ef2e6a144c6346c42ef3 /plumbing/transport/http/common.go | |
parent | ce6f5b7c82fc6c2c4d41880ed6b26f921dd9c1c3 (diff) | |
download | go-git-06f26e2d7096a7e8458e9e9b41e05c8bb83babf6.tar.gz |
transport: http push
Diffstat (limited to 'plumbing/transport/http/common.go')
-rw-r--r-- | plumbing/transport/http/common.go | 73 |
1 files changed, 73 insertions, 0 deletions
diff --git a/plumbing/transport/http/common.go b/plumbing/transport/http/common.go index 04b6121..6b40d42 100644 --- a/plumbing/transport/http/common.go +++ b/plumbing/transport/http/common.go @@ -2,14 +2,69 @@ package http import ( + "bytes" "fmt" "net/http" + "strconv" "gopkg.in/src-d/go-git.v4/plumbing" "gopkg.in/src-d/go-git.v4/plumbing/protocol/packp" "gopkg.in/src-d/go-git.v4/plumbing/transport" ) +// it requires a bytes.Buffer, because we need to know the length +func applyHeadersToRequest(req *http.Request, content *bytes.Buffer, host string, requestType string) { + req.Header.Add("User-Agent", "git/1.0") + req.Header.Add("Host", host) // host:port + + if content == nil { + req.Header.Add("Accept", "*/*") + return + } + + req.Header.Add("Accept", fmt.Sprintf("application/x-%s-result", requestType)) + req.Header.Add("Content-Type", fmt.Sprintf("application/x-%s-request", requestType)) + req.Header.Add("Content-Length", strconv.Itoa(content.Len())) +} + +func advertisedReferences(s *session, serviceName string) (*packp.AdvRefs, error) { + url := fmt.Sprintf( + "%s/info/refs?service=%s", + s.endpoint.String(), serviceName, + ) + + req, err := http.NewRequest(http.MethodGet, url, nil) + if err != nil { + return nil, err + } + + s.applyAuthToRequest(req) + applyHeadersToRequest(req, nil, s.endpoint.Host(), serviceName) + 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 +} + type client struct { c *http.Client } @@ -54,6 +109,24 @@ type session struct { advRefs *packp.AdvRefs } +func newSession(c *http.Client, ep transport.Endpoint, auth transport.AuthMethod) (*session, error) { + s := &session{ + auth: basicAuthFromEndpoint(ep), + client: c, + endpoint: ep, + } + if auth != nil { + a, ok := auth.(AuthMethod) + if !ok { + return nil, transport.ErrInvalidAuthMethod + } + + s.auth = a + } + + return s, nil +} + func (*session) Close() error { return nil } |