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
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Kirill Smelkov
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
Hide 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 {
type
httpHandleFunc
func
(
http
.
ResponseWriter
,
*
http
.
Request
)
func
(
h
httpHandleFunc
)
ServeHTTP
(
w
http
.
ResponseWriter
,
r
*
http
.
Request
)
{
h
(
w
,
r
)
}
const
projectPattern
=
`^/[^/]+/[^/]+/`
const
gitProjectPattern
=
`^/[^/]+/[^/]+\.git/`
...
...
@@ -86,7 +90,7 @@ func compileRoutes(u *upstream) {
httpRoute
{
"GET"
,
regexp
.
MustCompile
(
projectsAPIPattern
+
`repository/archive.tar.bz2\z`
),
api
.
repoPreAuthorizeHandler
(
handleGetArchive
)},
// 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
httpRoute
{
""
,
regexp
.
MustCompile
(
apiPattern
),
proxy
.
ServeHTTP
},
...
...
uploads.go
View file @
570911ae
...
...
@@ -85,41 +85,43 @@ func rewriteFormFilesFromMultipart(r *http.Request, writer *multipart.Writer, te
return
cleanup
,
nil
}
func
(
p
*
Proxy
)
handleFileUploads
(
w
http
.
ResponseWriter
,
r
*
http
.
Request
)
{
tempPath
:=
r
.
Header
.
Get
(
tempPathHeader
)
if
tempPath
==
""
{
fail500
(
w
,
errors
.
New
(
"handleFileUploads: TempPath empty"
))
return
}
r
.
Header
.
Del
(
tempPathHeader
)
func
handleFileUploads
(
h
http
.
Handler
)
httpHandleFunc
{
return
func
(
w
http
.
ResponseWriter
,
r
*
http
.
Request
)
{
tempPath
:=
r
.
Header
.
Get
(
tempPathHeader
)
if
tempPath
==
""
{
fail500
(
w
,
errors
.
New
(
"handleFileUploads: TempPath empty"
))
return
}
r
.
Header
.
Del
(
tempPathHeader
)
var
body
bytes
.
Buffer
writer
:=
multipart
.
NewWriter
(
&
body
)
defer
writer
.
Close
()
var
body
bytes
.
Buffer
writer
:=
multipart
.
NewWriter
(
&
body
)
defer
writer
.
Close
()
// Rewrite multipart form data
cleanup
,
err
:=
rewriteFormFilesFromMultipart
(
r
,
writer
,
tempPath
)
if
err
!=
nil
{
if
err
==
http
.
ErrNotMultipart
{
p
.
ServeHTTP
(
w
,
r
)
}
else
{
fail500
(
w
,
fmt
.
Errorf
(
"handleFileUploads: extract files from multipart: %v"
,
err
))
// Rewrite multipart form data
cleanup
,
err
:=
rewriteFormFilesFromMultipart
(
r
,
writer
,
tempPath
)
if
err
!=
nil
{
if
err
==
http
.
ErrNotMultipart
{
h
.
ServeHTTP
(
w
,
r
)
}
else
{
fail500
(
w
,
fmt
.
Errorf
(
"handleFileUploads: extract files from multipart: %v"
,
err
))
}
return
}
return
}
if
cleanup
!=
nil
{
defer
cleanup
()
}
if
cleanup
!=
nil
{
defer
cleanup
()
}
// Close writer
writer
.
Close
()
// Close writer
writer
.
Close
()
// Hijack the request
r
.
Body
=
ioutil
.
NopCloser
(
&
body
)
r
.
ContentLength
=
int64
(
body
.
Len
())
r
.
Header
.
Set
(
"Content-Type"
,
writer
.
FormDataContentType
())
// Hijack the request
r
.
Body
=
ioutil
.
NopCloser
(
&
body
)
r
.
ContentLength
=
int64
(
body
.
Len
())
r
.
Header
.
Set
(
"Content-Type"
,
writer
.
FormDataContentType
())
// Proxy the request
p
.
ServeHTTP
(
w
,
r
)
// Proxy the request
h
.
ServeHTTP
(
w
,
r
)
}
}
uploads_test.go
View file @
570911ae
...
...
@@ -17,7 +17,7 @@ import (
func
TestUploadTempPathRequirement
(
t
*
testing
.
T
)
{
response
:=
httptest
.
NewRecorder
()
request
:=
&
http
.
Request
{}
dummyUpstream
.
Proxy
.
handleFileUploads
(
response
,
request
)
handleFileUploads
(
dummyUpstream
.
Proxy
)
.
ServeHTTP
(
response
,
request
)
assertResponseCode
(
t
,
response
,
500
)
}
...
...
@@ -53,7 +53,7 @@ func TestUploadHandlerForwardingRawData(t *testing.T) {
httpRequest
.
Header
.
Set
(
tempPathHeader
,
tempPath
)
u
:=
newUpstream
(
ts
.
URL
,
nil
)
u
.
Proxy
.
handleFileUploads
(
response
,
httpRequest
)
handleFileUploads
(
u
.
Proxy
)
.
ServeHTTP
(
response
,
httpRequest
)
assertResponseCode
(
t
,
response
,
202
)
if
response
.
Body
.
String
()
!=
"RESPONSE"
{
t
.
Fatal
(
"Expected RESPONSE in response body"
)
...
...
@@ -128,7 +128,7 @@ func TestUploadHandlerRewritingMultiPartData(t *testing.T) {
response
:=
httptest
.
NewRecorder
()
u
:=
newUpstream
(
ts
.
URL
,
nil
)
u
.
Proxy
.
handleFileUploads
(
response
,
httpRequest
)
handleFileUploads
(
u
.
Proxy
)
.
ServeHTTP
(
response
,
httpRequest
)
assertResponseCode
(
t
,
response
,
202
)
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