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
570911ae
Commit
570911ae
authored
Dec 18, 2015
by
Jacob Vosmaer
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Start using Proxy as a http.Handler
parent
c7e3abe4
Changes
3
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
40 additions
and
34 deletions
+40
-34
main.go
main.go
+5
-1
uploads.go
uploads.go
+32
-30
uploads_test.go
uploads_test.go
+3
-3
No files found.
main.go
View file @
570911ae
...
@@ -48,6 +48,10 @@ type httpRoute struct {
...
@@ -48,6 +48,10 @@ type httpRoute struct {
type
httpHandleFunc
func
(
http
.
ResponseWriter
,
*
http
.
Request
)
type
httpHandleFunc
func
(
http
.
ResponseWriter
,
*
http
.
Request
)
func
(
h
httpHandleFunc
)
ServeHTTP
(
w
http
.
ResponseWriter
,
r
*
http
.
Request
)
{
h
(
w
,
r
)
}
const
projectPattern
=
`^/[^/]+/[^/]+/`
const
projectPattern
=
`^/[^/]+/[^/]+/`
const
gitProjectPattern
=
`^/[^/]+/[^/]+\.git/`
const
gitProjectPattern
=
`^/[^/]+/[^/]+\.git/`
...
@@ -86,7 +90,7 @@ func compileRoutes(u *upstream) {
...
@@ -86,7 +90,7 @@ func compileRoutes(u *upstream) {
httpRoute
{
"GET"
,
regexp
.
MustCompile
(
projectsAPIPattern
+
`repository/archive.tar.bz2\z`
),
api
.
repoPreAuthorizeHandler
(
handleGetArchive
)},
httpRoute
{
"GET"
,
regexp
.
MustCompile
(
projectsAPIPattern
+
`repository/archive.tar.bz2\z`
),
api
.
repoPreAuthorizeHandler
(
handleGetArchive
)},
// CI Artifacts API
// CI Artifacts API
httpRoute
{
"POST"
,
regexp
.
MustCompile
(
ciAPIPattern
+
`v1/builds/[0-9]+/artifacts\z`
),
contentEncodingHandler
(
api
.
artifactsAuthorizeHandler
(
proxy
.
handleFileUploads
))},
httpRoute
{
"POST"
,
regexp
.
MustCompile
(
ciAPIPattern
+
`v1/builds/[0-9]+/artifacts\z`
),
contentEncodingHandler
(
api
.
artifactsAuthorizeHandler
(
handleFileUploads
(
proxy
)
))},
// Explicitly proxy API requests
// Explicitly proxy API requests
httpRoute
{
""
,
regexp
.
MustCompile
(
apiPattern
),
proxy
.
ServeHTTP
},
httpRoute
{
""
,
regexp
.
MustCompile
(
apiPattern
),
proxy
.
ServeHTTP
},
...
...
uploads.go
View file @
570911ae
...
@@ -85,7 +85,8 @@ func rewriteFormFilesFromMultipart(r *http.Request, writer *multipart.Writer, te
...
@@ -85,7 +85,8 @@ func rewriteFormFilesFromMultipart(r *http.Request, writer *multipart.Writer, te
return
cleanup
,
nil
return
cleanup
,
nil
}
}
func
(
p
*
Proxy
)
handleFileUploads
(
w
http
.
ResponseWriter
,
r
*
http
.
Request
)
{
func
handleFileUploads
(
h
http
.
Handler
)
httpHandleFunc
{
return
func
(
w
http
.
ResponseWriter
,
r
*
http
.
Request
)
{
tempPath
:=
r
.
Header
.
Get
(
tempPathHeader
)
tempPath
:=
r
.
Header
.
Get
(
tempPathHeader
)
if
tempPath
==
""
{
if
tempPath
==
""
{
fail500
(
w
,
errors
.
New
(
"handleFileUploads: TempPath empty"
))
fail500
(
w
,
errors
.
New
(
"handleFileUploads: TempPath empty"
))
...
@@ -101,7 +102,7 @@ func (p *Proxy) handleFileUploads(w http.ResponseWriter, r *http.Request) {
...
@@ -101,7 +102,7 @@ func (p *Proxy) handleFileUploads(w http.ResponseWriter, r *http.Request) {
cleanup
,
err
:=
rewriteFormFilesFromMultipart
(
r
,
writer
,
tempPath
)
cleanup
,
err
:=
rewriteFormFilesFromMultipart
(
r
,
writer
,
tempPath
)
if
err
!=
nil
{
if
err
!=
nil
{
if
err
==
http
.
ErrNotMultipart
{
if
err
==
http
.
ErrNotMultipart
{
p
.
ServeHTTP
(
w
,
r
)
h
.
ServeHTTP
(
w
,
r
)
}
else
{
}
else
{
fail500
(
w
,
fmt
.
Errorf
(
"handleFileUploads: extract files from multipart: %v"
,
err
))
fail500
(
w
,
fmt
.
Errorf
(
"handleFileUploads: extract files from multipart: %v"
,
err
))
}
}
...
@@ -121,5 +122,6 @@ func (p *Proxy) handleFileUploads(w http.ResponseWriter, r *http.Request) {
...
@@ -121,5 +122,6 @@ func (p *Proxy) handleFileUploads(w http.ResponseWriter, r *http.Request) {
r
.
Header
.
Set
(
"Content-Type"
,
writer
.
FormDataContentType
())
r
.
Header
.
Set
(
"Content-Type"
,
writer
.
FormDataContentType
())
// Proxy the request
// Proxy the request
p
.
ServeHTTP
(
w
,
r
)
h
.
ServeHTTP
(
w
,
r
)
}
}
}
uploads_test.go
View file @
570911ae
...
@@ -17,7 +17,7 @@ import (
...
@@ -17,7 +17,7 @@ import (
func
TestUploadTempPathRequirement
(
t
*
testing
.
T
)
{
func
TestUploadTempPathRequirement
(
t
*
testing
.
T
)
{
response
:=
httptest
.
NewRecorder
()
response
:=
httptest
.
NewRecorder
()
request
:=
&
http
.
Request
{}
request
:=
&
http
.
Request
{}
dummyUpstream
.
Proxy
.
handleFileUploads
(
response
,
request
)
handleFileUploads
(
dummyUpstream
.
Proxy
)
.
ServeHTTP
(
response
,
request
)
assertResponseCode
(
t
,
response
,
500
)
assertResponseCode
(
t
,
response
,
500
)
}
}
...
@@ -53,7 +53,7 @@ func TestUploadHandlerForwardingRawData(t *testing.T) {
...
@@ -53,7 +53,7 @@ func TestUploadHandlerForwardingRawData(t *testing.T) {
httpRequest
.
Header
.
Set
(
tempPathHeader
,
tempPath
)
httpRequest
.
Header
.
Set
(
tempPathHeader
,
tempPath
)
u
:=
newUpstream
(
ts
.
URL
,
nil
)
u
:=
newUpstream
(
ts
.
URL
,
nil
)
u
.
Proxy
.
handleFileUploads
(
response
,
httpRequest
)
handleFileUploads
(
u
.
Proxy
)
.
ServeHTTP
(
response
,
httpRequest
)
assertResponseCode
(
t
,
response
,
202
)
assertResponseCode
(
t
,
response
,
202
)
if
response
.
Body
.
String
()
!=
"RESPONSE"
{
if
response
.
Body
.
String
()
!=
"RESPONSE"
{
t
.
Fatal
(
"Expected RESPONSE in response body"
)
t
.
Fatal
(
"Expected RESPONSE in response body"
)
...
@@ -128,7 +128,7 @@ func TestUploadHandlerRewritingMultiPartData(t *testing.T) {
...
@@ -128,7 +128,7 @@ func TestUploadHandlerRewritingMultiPartData(t *testing.T) {
response
:=
httptest
.
NewRecorder
()
response
:=
httptest
.
NewRecorder
()
u
:=
newUpstream
(
ts
.
URL
,
nil
)
u
:=
newUpstream
(
ts
.
URL
,
nil
)
u
.
Proxy
.
handleFileUploads
(
response
,
httpRequest
)
handleFileUploads
(
u
.
Proxy
)
.
ServeHTTP
(
response
,
httpRequest
)
assertResponseCode
(
t
,
response
,
202
)
assertResponseCode
(
t
,
response
,
202
)
if
_
,
err
:=
os
.
Stat
(
filePath
);
!
os
.
IsNotExist
(
err
)
{
if
_
,
err
:=
os
.
Stat
(
filePath
);
!
os
.
IsNotExist
(
err
)
{
...
...
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