Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
G
gitlab-workhorse
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Kazuhiko Shiozaki
gitlab-workhorse
Commits
a7666718
Commit
a7666718
authored
Dec 17, 2015
by
Jacob Vosmaer
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Remove upstream from gitRequest
parent
b0525d6c
Changes
12
Hide whitespace changes
Inline
Side-by-side
Showing
12 changed files
with
57 additions
and
57 deletions
+57
-57
artifacts.go
artifacts.go
+2
-2
authorization.go
authorization.go
+3
-3
authorization_test.go
authorization_test.go
+2
-3
git-http.go
git-http.go
+2
-2
lfs.go
lfs.go
+4
-4
main.go
main.go
+21
-21
main_test.go
main_test.go
+3
-2
proxy.go
proxy.go
+2
-2
proxy_test.go
proxy_test.go
+8
-8
uploads.go
uploads.go
+3
-3
uploads_test.go
uploads_test.go
+7
-5
upstream.go
upstream.go
+0
-2
No files found.
artifacts.go
View file @
a7666718
package
main
func
artifactsAuthorizeHandler
(
handleFunc
serviceHandleFunc
)
serviceHandleFunc
{
return
preAuthorizeHandler
(
handleFunc
,
"/authorize"
)
func
(
u
*
upstream
)
artifactsAuthorizeHandler
(
handleFunc
serviceHandleFunc
)
serviceHandleFunc
{
return
u
.
preAuthorizeHandler
(
handleFunc
,
"/authorize"
)
}
authorization.go
View file @
a7666718
...
...
@@ -51,15 +51,15 @@ func (u *upstream) newUpstreamRequest(r *http.Request, body io.Reader, suffix st
return
authReq
,
nil
}
func
preAuthorizeHandler
(
handleFunc
serviceHandleFunc
,
suffix
string
)
serviceHandleFunc
{
func
(
u
*
upstream
)
preAuthorizeHandler
(
handleFunc
serviceHandleFunc
,
suffix
string
)
serviceHandleFunc
{
return
func
(
w
http
.
ResponseWriter
,
r
*
gitRequest
)
{
authReq
,
err
:=
r
.
u
.
newUpstreamRequest
(
r
.
Request
,
nil
,
suffix
)
authReq
,
err
:=
u
.
newUpstreamRequest
(
r
.
Request
,
nil
,
suffix
)
if
err
!=
nil
{
fail500
(
w
,
fmt
.
Errorf
(
"preAuthorizeHandler: newUpstreamRequest: %v"
,
err
))
return
}
authResponse
,
err
:=
r
.
u
.
httpClient
.
Do
(
authReq
)
authResponse
,
err
:=
u
.
httpClient
.
Do
(
authReq
)
if
err
!=
nil
{
fail500
(
w
,
fmt
.
Errorf
(
"preAuthorizeHandler: do %v: %v"
,
authReq
.
URL
.
Path
,
err
))
return
...
...
authorization_test.go
View file @
a7666718
...
...
@@ -23,14 +23,13 @@ func runPreAuthorizeHandler(t *testing.T, suffix string, url *regexp.Regexp, aut
if
err
!=
nil
{
t
.
Fatal
(
err
)
}
u
:=
newUpstream
(
ts
.
URL
,
nil
)
request
:=
gitRequest
{
Request
:
httpRequest
,
u
:
newUpstream
(
ts
.
URL
,
nil
),
}
response
:=
httptest
.
NewRecorder
()
preAuthorizeHandler
(
okHandler
,
suffix
)(
response
,
&
request
)
u
.
preAuthorizeHandler
(
okHandler
,
suffix
)(
response
,
&
request
)
assertResponseCode
(
t
,
response
,
expectedCode
)
return
response
}
...
...
git-http.go
View file @
a7666718
...
...
@@ -26,8 +26,8 @@ func looksLikeRepo(p string) bool {
return
true
}
func
repoPreAuthorizeHandler
(
handleFunc
serviceHandleFunc
)
serviceHandleFunc
{
return
preAuthorizeHandler
(
func
(
w
http
.
ResponseWriter
,
r
*
gitRequest
)
{
func
(
u
*
upstream
)
repoPreAuthorizeHandler
(
handleFunc
serviceHandleFunc
)
serviceHandleFunc
{
return
u
.
preAuthorizeHandler
(
func
(
w
http
.
ResponseWriter
,
r
*
gitRequest
)
{
if
r
.
RepoPath
==
""
{
fail500
(
w
,
errors
.
New
(
"repoPreAuthorizeHandler: RepoPath empty"
))
return
...
...
lfs.go
View file @
a7666718
...
...
@@ -17,8 +17,8 @@ import (
"path/filepath"
)
func
lfsAuthorizeHandler
(
handleFunc
serviceHandleFunc
)
serviceHandleFunc
{
return
preAuthorizeHandler
(
func
(
w
http
.
ResponseWriter
,
r
*
gitRequest
)
{
func
(
u
*
upstream
)
lfsAuthorizeHandler
(
handleFunc
serviceHandleFunc
)
serviceHandleFunc
{
return
u
.
preAuthorizeHandler
(
func
(
w
http
.
ResponseWriter
,
r
*
gitRequest
)
{
if
r
.
StoreLFSPath
==
""
{
fail500
(
w
,
errors
.
New
(
"lfsAuthorizeHandler: StoreLFSPath empty"
))
...
...
@@ -39,7 +39,7 @@ func lfsAuthorizeHandler(handleFunc serviceHandleFunc) serviceHandleFunc {
},
"/authorize"
)
}
func
handleStoreLfsObject
(
w
http
.
ResponseWriter
,
r
*
gitRequest
)
{
func
(
u
*
upstream
)
handleStoreLfsObject
(
w
http
.
ResponseWriter
,
r
*
gitRequest
)
{
file
,
err
:=
ioutil
.
TempFile
(
r
.
StoreLFSPath
,
r
.
LfsOid
)
if
err
!=
nil
{
fail500
(
w
,
fmt
.
Errorf
(
"handleStoreLfsObject: create tempfile: %v"
,
err
))
...
...
@@ -75,5 +75,5 @@ func handleStoreLfsObject(w http.ResponseWriter, r *gitRequest) {
r
.
ContentLength
=
0
// And proxy the request
proxyRequest
(
w
,
r
)
u
.
proxyRequest
(
w
,
r
)
}
main.go
View file @
a7666718
...
...
@@ -59,34 +59,34 @@ const ciAPIPattern = `^/ci/api/`
// see upstream.ServeHTTP
var
httpRoutes
[]
httpRoute
func
compileRoutes
()
{
func
compileRoutes
(
u
*
upstream
)
{
httpRoutes
=
[]
httpRoute
{
// Git Clone
httpRoute
{
"GET"
,
regexp
.
MustCompile
(
gitProjectPattern
+
`info/refs\z`
),
repoPreAuthorizeHandler
(
handleGetInfoRefs
)},
httpRoute
{
"POST"
,
regexp
.
MustCompile
(
gitProjectPattern
+
`git-upload-pack\z`
),
repoPreAuthorizeHandler
(
contentEncodingHandler
(
handlePostRPC
))},
httpRoute
{
"POST"
,
regexp
.
MustCompile
(
gitProjectPattern
+
`git-receive-pack\z`
),
repoPreAuthorizeHandler
(
contentEncodingHandler
(
handlePostRPC
))},
httpRoute
{
"PUT"
,
regexp
.
MustCompile
(
gitProjectPattern
+
`gitlab-lfs/objects/([0-9a-f]{64})/([0-9]+)\z`
),
lfsAuthorizeHandler
(
handleStoreLfsObject
)},
httpRoute
{
"GET"
,
regexp
.
MustCompile
(
gitProjectPattern
+
`info/refs\z`
),
u
.
repoPreAuthorizeHandler
(
handleGetInfoRefs
)},
httpRoute
{
"POST"
,
regexp
.
MustCompile
(
gitProjectPattern
+
`git-upload-pack\z`
),
u
.
repoPreAuthorizeHandler
(
contentEncodingHandler
(
handlePostRPC
))},
httpRoute
{
"POST"
,
regexp
.
MustCompile
(
gitProjectPattern
+
`git-receive-pack\z`
),
u
.
repoPreAuthorizeHandler
(
contentEncodingHandler
(
handlePostRPC
))},
httpRoute
{
"PUT"
,
regexp
.
MustCompile
(
gitProjectPattern
+
`gitlab-lfs/objects/([0-9a-f]{64})/([0-9]+)\z`
),
u
.
lfsAuthorizeHandler
(
u
.
handleStoreLfsObject
)},
// Repository Archive
httpRoute
{
"GET"
,
regexp
.
MustCompile
(
projectPattern
+
`repository/archive\z`
),
repoPreAuthorizeHandler
(
handleGetArchive
)},
httpRoute
{
"GET"
,
regexp
.
MustCompile
(
projectPattern
+
`repository/archive.zip\z`
),
repoPreAuthorizeHandler
(
handleGetArchive
)},
httpRoute
{
"GET"
,
regexp
.
MustCompile
(
projectPattern
+
`repository/archive.tar\z`
),
repoPreAuthorizeHandler
(
handleGetArchive
)},
httpRoute
{
"GET"
,
regexp
.
MustCompile
(
projectPattern
+
`repository/archive.tar.gz\z`
),
repoPreAuthorizeHandler
(
handleGetArchive
)},
httpRoute
{
"GET"
,
regexp
.
MustCompile
(
projectPattern
+
`repository/archive.tar.bz2\z`
),
repoPreAuthorizeHandler
(
handleGetArchive
)},
httpRoute
{
"GET"
,
regexp
.
MustCompile
(
projectPattern
+
`repository/archive\z`
),
u
.
repoPreAuthorizeHandler
(
handleGetArchive
)},
httpRoute
{
"GET"
,
regexp
.
MustCompile
(
projectPattern
+
`repository/archive.zip\z`
),
u
.
repoPreAuthorizeHandler
(
handleGetArchive
)},
httpRoute
{
"GET"
,
regexp
.
MustCompile
(
projectPattern
+
`repository/archive.tar\z`
),
u
.
repoPreAuthorizeHandler
(
handleGetArchive
)},
httpRoute
{
"GET"
,
regexp
.
MustCompile
(
projectPattern
+
`repository/archive.tar.gz\z`
),
u
.
repoPreAuthorizeHandler
(
handleGetArchive
)},
httpRoute
{
"GET"
,
regexp
.
MustCompile
(
projectPattern
+
`repository/archive.tar.bz2\z`
),
u
.
repoPreAuthorizeHandler
(
handleGetArchive
)},
// Repository Archive API
httpRoute
{
"GET"
,
regexp
.
MustCompile
(
projectsAPIPattern
+
`repository/archive\z`
),
repoPreAuthorizeHandler
(
handleGetArchive
)},
httpRoute
{
"GET"
,
regexp
.
MustCompile
(
projectsAPIPattern
+
`repository/archive.zip\z`
),
repoPreAuthorizeHandler
(
handleGetArchive
)},
httpRoute
{
"GET"
,
regexp
.
MustCompile
(
projectsAPIPattern
+
`repository/archive.tar\z`
),
repoPreAuthorizeHandler
(
handleGetArchive
)},
httpRoute
{
"GET"
,
regexp
.
MustCompile
(
projectsAPIPattern
+
`repository/archive.tar.gz\z`
),
repoPreAuthorizeHandler
(
handleGetArchive
)},
httpRoute
{
"GET"
,
regexp
.
MustCompile
(
projectsAPIPattern
+
`repository/archive.tar.bz2\z`
),
repoPreAuthorizeHandler
(
handleGetArchive
)},
httpRoute
{
"GET"
,
regexp
.
MustCompile
(
projectsAPIPattern
+
`repository/archive\z`
),
u
.
repoPreAuthorizeHandler
(
handleGetArchive
)},
httpRoute
{
"GET"
,
regexp
.
MustCompile
(
projectsAPIPattern
+
`repository/archive.zip\z`
),
u
.
repoPreAuthorizeHandler
(
handleGetArchive
)},
httpRoute
{
"GET"
,
regexp
.
MustCompile
(
projectsAPIPattern
+
`repository/archive.tar\z`
),
u
.
repoPreAuthorizeHandler
(
handleGetArchive
)},
httpRoute
{
"GET"
,
regexp
.
MustCompile
(
projectsAPIPattern
+
`repository/archive.tar.gz\z`
),
u
.
repoPreAuthorizeHandler
(
handleGetArchive
)},
httpRoute
{
"GET"
,
regexp
.
MustCompile
(
projectsAPIPattern
+
`repository/archive.tar.bz2\z`
),
u
.
repoPreAuthorizeHandler
(
handleGetArchive
)},
// CI Artifacts API
httpRoute
{
"POST"
,
regexp
.
MustCompile
(
ciAPIPattern
+
`v1/builds/[0-9]+/artifacts\z`
),
artifactsAuthorizeHandler
(
contentEncodingHandler
(
handleFileUploads
))},
httpRoute
{
"POST"
,
regexp
.
MustCompile
(
ciAPIPattern
+
`v1/builds/[0-9]+/artifacts\z`
),
u
.
artifactsAuthorizeHandler
(
contentEncodingHandler
(
u
.
handleFileUploads
))},
// Explicitly proxy API requests
httpRoute
{
""
,
regexp
.
MustCompile
(
apiPattern
),
proxyRequest
},
httpRoute
{
""
,
regexp
.
MustCompile
(
ciAPIPattern
),
proxyRequest
},
httpRoute
{
""
,
regexp
.
MustCompile
(
apiPattern
),
u
.
proxyRequest
},
httpRoute
{
""
,
regexp
.
MustCompile
(
ciAPIPattern
),
u
.
proxyRequest
},
// Serve assets
httpRoute
{
""
,
regexp
.
MustCompile
(
`^/assets/`
),
...
...
@@ -94,7 +94,7 @@ func compileRoutes() {
handleDevelopmentMode
(
developmentMode
,
handleDeployPage
(
documentRoot
,
handleRailsError
(
documentRoot
,
proxyRequest
,
u
.
proxyRequest
,
),
),
),
...
...
@@ -106,7 +106,7 @@ func compileRoutes() {
handleServeFile
(
documentRoot
,
CacheDisabled
,
handleDeployPage
(
documentRoot
,
handleRailsError
(
documentRoot
,
proxyRequest
,
u
.
proxyRequest
,
),
),
),
...
...
@@ -173,6 +173,6 @@ func main() {
}
upstream
:=
newUpstream
(
*
authBackend
,
proxyTransport
)
compileRoutes
()
compileRoutes
(
upstream
)
log
.
Fatal
(
http
.
Serve
(
listener
,
upstream
))
}
main_test.go
View file @
a7666718
...
...
@@ -326,8 +326,9 @@ func testAuthServer(url *regexp.Regexp, code int, body interface{}) *httptest.Se
}
func
startWorkhorseServer
(
authBackend
string
)
*
httptest
.
Server
{
compileRoutes
()
return
httptest
.
NewServer
(
newUpstream
(
authBackend
,
nil
))
u
:=
newUpstream
(
authBackend
,
nil
)
compileRoutes
(
u
)
return
httptest
.
NewServer
(
u
)
}
func
runOrFail
(
t
*
testing
.
T
,
cmd
*
exec
.
Cmd
)
{
...
...
proxy.go
View file @
a7666718
...
...
@@ -51,7 +51,7 @@ func headerClone(h http.Header) http.Header {
return
h2
}
func
proxyRequest
(
w
http
.
ResponseWriter
,
r
*
gitRequest
)
{
func
(
u
*
upstream
)
proxyRequest
(
w
http
.
ResponseWriter
,
r
*
gitRequest
)
{
// Clone request
req
:=
*
r
.
Request
req
.
Header
=
headerClone
(
r
.
Header
)
...
...
@@ -61,5 +61,5 @@ func proxyRequest(w http.ResponseWriter, r *gitRequest) {
rw
:=
newSendFileResponseWriter
(
w
,
&
req
)
defer
rw
.
Flush
()
r
.
u
.
httpProxy
.
ServeHTTP
(
&
rw
,
&
req
)
u
.
httpProxy
.
ServeHTTP
(
&
rw
,
&
req
)
}
proxy_test.go
View file @
a7666718
...
...
@@ -41,11 +41,11 @@ func TestProxyRequest(t *testing.T) {
request
:=
gitRequest
{
Request
:
httpRequest
,
u
:
newUpstream
(
ts
.
URL
,
nil
),
}
u
:=
newUpstream
(
ts
.
URL
,
nil
)
w
:=
httptest
.
NewRecorder
()
proxyRequest
(
w
,
&
request
)
u
.
proxyRequest
(
w
,
&
request
)
assertResponseCode
(
t
,
w
,
202
)
assertResponseBody
(
t
,
w
,
"RESPONSE"
)
...
...
@@ -67,11 +67,11 @@ func TestProxyError(t *testing.T) {
request
:=
gitRequest
{
Request
:
httpRequest
,
u
:
newUpstream
(
"http://localhost:655575/"
,
&
transport
),
}
u
:=
newUpstream
(
"http://localhost:655575/"
,
&
transport
)
w
:=
httptest
.
NewRecorder
()
proxyRequest
(
w
,
&
request
)
u
.
proxyRequest
(
w
,
&
request
)
assertResponseCode
(
t
,
w
,
502
)
assertResponseBody
(
t
,
w
,
"dial tcp: invalid port 655575"
)
}
...
...
@@ -100,11 +100,11 @@ func TestProxyReadTimeout(t *testing.T) {
request
:=
gitRequest
{
Request
:
httpRequest
,
u
:
newUpstream
(
ts
.
URL
,
transport
),
}
u
:=
newUpstream
(
ts
.
URL
,
transport
)
w
:=
httptest
.
NewRecorder
()
proxyRequest
(
w
,
&
request
)
u
.
proxyRequest
(
w
,
&
request
)
assertResponseCode
(
t
,
w
,
502
)
assertResponseBody
(
t
,
w
,
"net/http: timeout awaiting response headers"
)
}
...
...
@@ -127,11 +127,11 @@ func TestProxyHandlerTimeout(t *testing.T) {
request
:=
gitRequest
{
Request
:
httpRequest
,
u
:
newUpstream
(
ts
.
URL
,
transport
),
}
u
:=
newUpstream
(
ts
.
URL
,
transport
)
w
:=
httptest
.
NewRecorder
()
proxyRequest
(
w
,
&
request
)
u
.
proxyRequest
(
w
,
&
request
)
assertResponseCode
(
t
,
w
,
503
)
assertResponseBody
(
t
,
w
,
"Request took too long"
)
}
uploads.go
View file @
a7666718
...
...
@@ -83,7 +83,7 @@ func rewriteFormFilesFromMultipart(r *gitRequest, writer *multipart.Writer) (cle
return
cleanup
,
nil
}
func
handleFileUploads
(
w
http
.
ResponseWriter
,
r
*
gitRequest
)
{
func
(
u
*
upstream
)
handleFileUploads
(
w
http
.
ResponseWriter
,
r
*
gitRequest
)
{
if
r
.
TempPath
==
""
{
fail500
(
w
,
errors
.
New
(
"handleFileUploads: TempPath empty"
))
return
...
...
@@ -97,7 +97,7 @@ func handleFileUploads(w http.ResponseWriter, r *gitRequest) {
cleanup
,
err
:=
rewriteFormFilesFromMultipart
(
r
,
writer
)
if
err
!=
nil
{
if
err
==
http
.
ErrNotMultipart
{
proxyRequest
(
w
,
r
)
u
.
proxyRequest
(
w
,
r
)
}
else
{
fail500
(
w
,
fmt
.
Errorf
(
"handleFileUploads: extract files from multipart: %v"
,
err
))
}
...
...
@@ -117,5 +117,5 @@ func handleFileUploads(w http.ResponseWriter, r *gitRequest) {
r
.
Header
.
Set
(
"Content-Type"
,
writer
.
FormDataContentType
())
// Proxy the request
proxyRequest
(
w
,
r
)
u
.
proxyRequest
(
w
,
r
)
}
uploads_test.go
View file @
a7666718
...
...
@@ -21,7 +21,7 @@ func TestUploadTempPathRequirement(t *testing.T) {
TempPath
:
""
,
},
}
handleFileUploads
(
response
,
&
request
)
newUpstream
(
"http://localhost"
,
nil
)
.
handleFileUploads
(
response
,
&
request
)
assertResponseCode
(
t
,
response
,
500
)
}
...
...
@@ -55,12 +55,13 @@ func TestUploadHandlerForwardingRawData(t *testing.T) {
response
:=
httptest
.
NewRecorder
()
request
:=
gitRequest
{
Request
:
httpRequest
,
u
:
newUpstream
(
ts
.
URL
,
nil
),
authorizationResponse
:
authorizationResponse
{
TempPath
:
tempPath
,
},
}
handleFileUploads
(
response
,
&
request
)
u
:=
newUpstream
(
ts
.
URL
,
nil
)
u
.
handleFileUploads
(
response
,
&
request
)
assertResponseCode
(
t
,
response
,
202
)
if
response
.
Body
.
String
()
!=
"RESPONSE"
{
t
.
Fatal
(
"Expected RESPONSE in response body"
)
...
...
@@ -135,12 +136,13 @@ func TestUploadHandlerRewritingMultiPartData(t *testing.T) {
response
:=
httptest
.
NewRecorder
()
request
:=
gitRequest
{
Request
:
httpRequest
,
u
:
newUpstream
(
ts
.
URL
,
nil
),
authorizationResponse
:
authorizationResponse
{
TempPath
:
tempPath
,
},
}
handleFileUploads
(
response
,
&
request
)
u
:=
newUpstream
(
ts
.
URL
,
nil
)
u
.
handleFileUploads
(
response
,
&
request
)
assertResponseCode
(
t
,
response
,
202
)
if
_
,
err
:=
os
.
Stat
(
filePath
);
!
os
.
IsNotExist
(
err
)
{
...
...
upstream.go
View file @
a7666718
...
...
@@ -57,7 +57,6 @@ type authorizationResponse struct {
type
gitRequest
struct
{
*
http
.
Request
authorizationResponse
u
*
upstream
// This field contains the URL.Path stripped from RelativeUrlRoot
relativeURIPath
string
...
...
@@ -140,7 +139,6 @@ func (u *upstream) ServeHTTP(ow http.ResponseWriter, r *http.Request) {
request
:=
gitRequest
{
Request
:
r
,
relativeURIPath
:
relativeURIPath
,
u
:
u
,
}
g
.
handleFunc
(
&
w
,
&
request
)
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment