Commit ec168f4b authored by Jacob Vosmaer's avatar Jacob Vosmaer

Use interfaces

parent 3ec457b4
...@@ -6,6 +6,7 @@ package git ...@@ -6,6 +6,7 @@ package git
import ( import (
"../helper" "../helper"
"../senddata"
"fmt" "fmt"
"io" "io"
"io/ioutil" "io/ioutil"
...@@ -19,16 +20,18 @@ import ( ...@@ -19,16 +20,18 @@ import (
"time" "time"
) )
const SendArchivePrefix = "git-archive:" type archive struct{ senddata.Prefix }
func SendArchive(w http.ResponseWriter, r *http.Request, sendData string) { var SendArchive = &archive{"git-archive:"}
func (a *archive) Handle(w http.ResponseWriter, r *http.Request, sendData string) {
var params struct { var params struct {
RepoPath string RepoPath string
ArchivePath string ArchivePath string
ArchivePrefix string ArchivePrefix string
CommitId string CommitId string
} }
if err := unpackSendData(&params, sendData, SendArchivePrefix); err != nil { if err := a.Unpack(&params, sendData); err != nil {
helper.Fail500(w, fmt.Errorf("SendArchive: unpack sendData: %v", err)) helper.Fail500(w, fmt.Errorf("SendArchive: unpack sendData: %v", err))
return return
} }
......
...@@ -2,20 +2,20 @@ package git ...@@ -2,20 +2,20 @@ package git
import ( import (
"../helper" "../helper"
"encoding/base64" "../senddata"
"encoding/json"
"fmt" "fmt"
"io" "io"
"log" "log"
"net/http" "net/http"
"strings"
) )
const SendBlobPrefix = "git-blob:" type blob struct{ senddata.Prefix }
func SendBlob(w http.ResponseWriter, r *http.Request, sendData string) { var SendBlob = &blob{"git-blob:"}
func (b *blob) Handle(w http.ResponseWriter, r *http.Request, sendData string) {
var params struct{ RepoPath, BlobId string } var params struct{ RepoPath, BlobId string }
if err := unpackSendData(&params, sendData, SendBlobPrefix); err != nil { if err := b.Unpack(&params, sendData); err != nil {
helper.Fail500(w, fmt.Errorf("SendBlob: unpack sendData: %v", err)) helper.Fail500(w, fmt.Errorf("SendBlob: unpack sendData: %v", err))
return return
} }
...@@ -43,14 +43,3 @@ func SendBlob(w http.ResponseWriter, r *http.Request, sendData string) { ...@@ -43,14 +43,3 @@ func SendBlob(w http.ResponseWriter, r *http.Request, sendData string) {
return return
} }
} }
func unpackSendData(result interface{}, sendData string, prefix string) error {
jsonBytes, err := base64.URLEncoding.DecodeString(strings.TrimPrefix(sendData, prefix))
if err != nil {
return err
}
if err := json.Unmarshal([]byte(jsonBytes), result); err != nil {
return err
}
return nil
}
...@@ -4,20 +4,17 @@ via the X-Sendfile mechanism. All that is needed in the Rails code is the ...@@ -4,20 +4,17 @@ via the X-Sendfile mechanism. All that is needed in the Rails code is the
'send_file' method. 'send_file' method.
*/ */
package senddata package inject
import ( import (
"../git" "../git"
"../helper" "../helper"
"../senddata"
"log" "log"
"net/http" "net/http"
"strings"
) )
const ( const sendFileResponseHeader = "X-Sendfile"
sendDataResponseHeader = "Gitlab-Workhorse-Send-Data"
sendFileResponseHeader = "X-Sendfile"
)
type sendFileResponseWriter struct { type sendFileResponseWriter struct {
rw http.ResponseWriter rw http.ResponseWriter
...@@ -71,18 +68,15 @@ func (s *sendFileResponseWriter) WriteHeader(status int) { ...@@ -71,18 +68,15 @@ func (s *sendFileResponseWriter) WriteHeader(status int) {
return return
} }
sendData := s.Header().Get(sendDataResponseHeader) header := s.Header().Get(senddata.Header)
s.Header().Del(sendDataResponseHeader) s.Header().Del(senddata.Header)
for _, handler := range []struct { for _, handler := range []senddata.Handler{
prefix string git.SendBlob,
f func(http.ResponseWriter, *http.Request, string) git.SendArchive,
}{
{git.SendBlobPrefix, git.SendBlob},
{git.SendArchivePrefix, git.SendArchive},
} { } {
if strings.HasPrefix(sendData, handler.prefix) { if handler.Match(header) {
s.hijacked = true s.hijacked = true
handler.f(s.rw, s.req, sendData) handler.Handle(s.rw, s.req, header)
return return
} }
} }
......
...@@ -3,7 +3,7 @@ package proxy ...@@ -3,7 +3,7 @@ package proxy
import ( import (
"../badgateway" "../badgateway"
"../helper" "../helper"
"../senddata" "../inject"
"net/http" "net/http"
"net/http/httputil" "net/http/httputil"
"net/url" "net/url"
...@@ -34,7 +34,7 @@ func (p *Proxy) ServeHTTP(w http.ResponseWriter, r *http.Request) { ...@@ -34,7 +34,7 @@ func (p *Proxy) ServeHTTP(w http.ResponseWriter, r *http.Request) {
// Set Workhorse version // Set Workhorse version
req.Header.Set("Gitlab-Workhorse", p.Version) req.Header.Set("Gitlab-Workhorse", p.Version)
rw := senddata.NewSendFileResponseWriter(w, &req) rw := inject.NewSendFileResponseWriter(w, &req)
defer rw.Flush() defer rw.Flush()
p.reverseProxy.ServeHTTP(&rw, &req) p.reverseProxy.ServeHTTP(&rw, &req)
......
package senddata
import (
"encoding/base64"
"encoding/json"
"net/http"
"strings"
)
type Handler interface {
Match(string) bool
Handle(http.ResponseWriter, *http.Request, string)
}
type Prefix string
const Header = "Gitlab-Workhorse-Send-Data"
func (p Prefix) Match(s string) bool {
return strings.HasPrefix(s, string(p))
}
func (p Prefix) Unpack(result interface{}, sendData string) error {
jsonBytes, err := base64.URLEncoding.DecodeString(strings.TrimPrefix(sendData, string(p)))
if err != nil {
return err
}
if err := json.Unmarshal([]byte(jsonBytes), result); err != nil {
return err
}
return nil
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment